Allow to enable online mode for player connection (#51)
This commit is contained in:
committed by
Andrew Steinborn
parent
e6e3ccaa95
commit
a3c4522ca0
@@ -1,6 +1,7 @@
|
||||
package com.velocitypowered.api.event;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import net.kyori.text.Component;
|
||||
import net.kyori.text.serializer.ComponentSerializers;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -72,7 +73,7 @@ public interface ResultedEvent<R extends ResultedEvent.Result> {
|
||||
private final boolean allowed;
|
||||
private final @Nullable Component reason;
|
||||
|
||||
private ComponentResult(boolean allowed, @Nullable Component reason) {
|
||||
protected ComponentResult(boolean allowed, @Nullable Component reason) {
|
||||
this.allowed = allowed;
|
||||
this.reason = reason;
|
||||
}
|
||||
|
@@ -2,22 +2,28 @@ package com.velocitypowered.api.event.connection;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.velocitypowered.api.event.ResultedEvent;
|
||||
import com.velocitypowered.api.event.ResultedEvent.ComponentResult;
|
||||
import com.velocitypowered.api.proxy.InboundConnection;
|
||||
|
||||
import net.kyori.text.Component;
|
||||
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
/**
|
||||
* This event is fired when a player has initiated a connection with the proxy but before the proxy authenticates the
|
||||
* player with Mojang or before the player's proxy connection is fully established (for offline mode).
|
||||
*/
|
||||
public class PreLoginEvent implements ResultedEvent<ResultedEvent.ComponentResult> {
|
||||
public class PreLoginEvent implements ResultedEvent<PreLoginEvent.PreLoginComponentResult> {
|
||||
private final InboundConnection connection;
|
||||
private final String username;
|
||||
private ComponentResult result;
|
||||
private PreLoginComponentResult result;
|
||||
|
||||
public PreLoginEvent(InboundConnection connection, String username) {
|
||||
this.connection = Preconditions.checkNotNull(connection, "connection");
|
||||
this.username = Preconditions.checkNotNull(username, "username");
|
||||
this.result = ComponentResult.allowed();
|
||||
this.result = PreLoginComponentResult.allowed();
|
||||
}
|
||||
|
||||
public InboundConnection getConnection() {
|
||||
@@ -29,12 +35,12 @@ public class PreLoginEvent implements ResultedEvent<ResultedEvent.ComponentResul
|
||||
}
|
||||
|
||||
@Override
|
||||
public ComponentResult getResult() {
|
||||
public PreLoginComponentResult getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResult(@NonNull ComponentResult result) {
|
||||
public void setResult(@NonNull PreLoginComponentResult result) {
|
||||
this.result = Preconditions.checkNotNull(result, "result");
|
||||
}
|
||||
|
||||
@@ -46,4 +52,56 @@ public class PreLoginEvent implements ResultedEvent<ResultedEvent.ComponentResul
|
||||
", result=" + result +
|
||||
'}';
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents an "allowed/allowed with online mode/denied" result with a reason allowed for denial.
|
||||
*/
|
||||
public static class PreLoginComponentResult extends ComponentResult {
|
||||
|
||||
private static final PreLoginComponentResult ALLOWED = new PreLoginComponentResult((Component) null);
|
||||
private static final PreLoginComponentResult FORCE_ONLINEMODE = new PreLoginComponentResult(true);
|
||||
|
||||
private final boolean onlineMode;
|
||||
/**
|
||||
* Allows to enable a online mode for the player connection, when Velocity running in offline mode
|
||||
* Does not have any sense if velocity running in onlineMode;
|
||||
* @param allowedOnlineMode if true, offline uuid will be used for player connection if Velocity run in offlineMode
|
||||
*/
|
||||
private PreLoginComponentResult(boolean allowedOnlineMode) {
|
||||
super(true, null);
|
||||
this.onlineMode = allowedOnlineMode;
|
||||
}
|
||||
|
||||
private PreLoginComponentResult(@Nullable Component reason) {
|
||||
super(reason == null, reason);
|
||||
// Don't care about this
|
||||
this.onlineMode = false;
|
||||
}
|
||||
|
||||
public boolean isOnlineModeAllowed() {
|
||||
return this.onlineMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (isOnlineModeAllowed()) {
|
||||
return "allowed with online mode";
|
||||
}
|
||||
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
public static PreLoginComponentResult allowed() {
|
||||
return ALLOWED;
|
||||
}
|
||||
|
||||
public static PreLoginComponentResult forceOnlineMode() {
|
||||
return FORCE_ONLINEMODE;
|
||||
}
|
||||
|
||||
public static PreLoginComponentResult denied(@NonNull Component reason) {
|
||||
Preconditions.checkNotNull(reason, "reason");
|
||||
return new PreLoginComponentResult(reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,53 @@
|
||||
package com.velocitypowered.api.event.player.gameprofile;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.velocitypowered.api.util.GameProfile;
|
||||
|
||||
|
||||
public class GameProfileRequestEvent {
|
||||
private final String username;
|
||||
private final GameProfile originalProfile;
|
||||
private final boolean onlineMode;
|
||||
private GameProfile gameProfile;
|
||||
|
||||
public GameProfileRequestEvent(GameProfile originalProfile, boolean onlinemode) {
|
||||
this.originalProfile = Preconditions.checkNotNull(originalProfile, "profile");
|
||||
this.username = originalProfile.getName();
|
||||
this.onlineMode = onlinemode;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public GameProfile getOriginalProfile() {
|
||||
return originalProfile;
|
||||
}
|
||||
|
||||
public boolean isOnlineMode() {
|
||||
return onlineMode;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return a GameProfile, can be null
|
||||
*/
|
||||
public GameProfile getGameProfile() {
|
||||
return gameProfile;
|
||||
}
|
||||
|
||||
public void setGameProfile(@Nullable GameProfile gameProfile) {
|
||||
this.gameProfile = gameProfile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "GameProfileRequestEvent{"+
|
||||
"username=" + username +
|
||||
", gameProfile=" + gameProfile +
|
||||
"}";
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,13 +1,16 @@
|
||||
package com.velocitypowered.api.proxy;
|
||||
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.event.connection.LoginEvent;
|
||||
import com.velocitypowered.api.proxy.messages.ChannelMessageSink;
|
||||
import com.velocitypowered.api.proxy.messages.ChannelMessageSource;
|
||||
import com.velocitypowered.api.server.ServerInfo;
|
||||
import com.velocitypowered.api.util.GameProfile.Property;
|
||||
import com.velocitypowered.api.util.MessagePosition;
|
||||
import net.kyori.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
@@ -25,7 +25,7 @@ public class GameProfile {
|
||||
public UUID idAsUuid() {
|
||||
return UuidUtils.fromUndashed(id);
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
Reference in New Issue
Block a user