3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Merge pull request #82 from Leymooo/force-offline

Refactor PreLoginComponentResult
Dieser Commit ist enthalten in:
Andrew Steinborn 2018-09-13 15:49:03 -04:00 committet von GitHub
Commit 7e8b1fb6a5
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23

Datei anzeigen

@ -3,8 +3,10 @@ package com.velocitypowered.api.event.connection;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.api.proxy.InboundConnection;
import java.util.Optional;
import net.kyori.text.Component; import net.kyori.text.Component;
import net.kyori.text.serializer.ComponentSerializers;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@ -54,33 +56,35 @@ public class PreLoginEvent implements ResultedEvent<PreLoginEvent.PreLoginCompon
/** /**
* Represents an "allowed/allowed with forced online\offline mode/denied" result with a reason allowed for denial. * Represents an "allowed/allowed with forced online\offline mode/denied" result with a reason allowed for denial.
*/ */
public static class PreLoginComponentResult extends ResultedEvent.ComponentResult { public static class PreLoginComponentResult implements Result {
private static final PreLoginComponentResult ALLOWED = new PreLoginComponentResult((Component) null);
private static final PreLoginComponentResult FORCE_ONLINEMODE = new PreLoginComponentResult(true, false);
private static final PreLoginComponentResult FORCE_OFFLINEMODE = new PreLoginComponentResult(false, true);
private final boolean onlineMode; private static final PreLoginComponentResult ALLOWED = new PreLoginComponentResult(Result.ALLOWED, null);
private final boolean forceOfflineMode; private static final PreLoginComponentResult FORCE_ONLINEMODE = new PreLoginComponentResult(Result.FORCE_ONLINE, null);
private static final PreLoginComponentResult FORCE_OFFLINEMODE = new PreLoginComponentResult(Result.FORCE_OFFLINE, null);
private PreLoginComponentResult(boolean allowedOnlineMode, boolean forceOfflineMode) { private final Result result;
super(true, null); private final Optional<Component> reason;
this.onlineMode = allowedOnlineMode;
this.forceOfflineMode = forceOfflineMode; private PreLoginComponentResult(Result result, @Nullable Component reason) {
this.result = result;
this.reason = Optional.ofNullable(reason);
} }
private PreLoginComponentResult(@Nullable Component reason) { @Override
super(reason == null, reason); public boolean isAllowed() {
// Don't care about this return result != Result.DISALLOWED;
this.onlineMode = false; }
this.forceOfflineMode = false;
public Optional<Component> getReason() {
return reason;
} }
public boolean isOnlineModeAllowed() { public boolean isOnlineModeAllowed() {
return this.onlineMode; return result == Result.FORCE_ONLINE;
} }
public boolean isForceOfflineMode() { public boolean isForceOfflineMode() {
return forceOfflineMode; return result == Result.FORCE_OFFLINE;
} }
@Override @Override
@ -91,12 +95,18 @@ public class PreLoginEvent implements ResultedEvent<PreLoginEvent.PreLoginCompon
if (isOnlineModeAllowed()) { if (isOnlineModeAllowed()) {
return "allowed with online mode"; return "allowed with online mode";
} }
if (isAllowed()) {
return super.toString(); return "allowed";
}
if (reason.isPresent()) {
return "denied: " + ComponentSerializers.PLAIN.serialize(reason.get());
}
return "denied";
} }
/** /**
* Returns a result indicating the connection will be allowed through the proxy. * Returns a result indicating the connection will be allowed through
* the proxy.
* @return the allowed result * @return the allowed result
*/ */
public static PreLoginComponentResult allowed() { public static PreLoginComponentResult allowed() {
@ -104,9 +114,10 @@ public class PreLoginEvent implements ResultedEvent<PreLoginEvent.PreLoginCompon
} }
/** /**
* Returns a result indicating the connection will be allowed through the proxy, but the connection will be * Returns a result indicating the connection will be allowed through
* forced to use online mode provided that the proxy is in offline mode. This acts similarly to {@link #allowed()} * the proxy, but the connection will be forced to use online mode
* on an online-mode proxy. * provided that the proxy is in offline mode. This acts similarly to
* {@link #allowed()} on an online-mode proxy.
* @return the result * @return the result
*/ */
public static PreLoginComponentResult forceOnlineMode() { public static PreLoginComponentResult forceOnlineMode() {
@ -114,8 +125,9 @@ public class PreLoginEvent implements ResultedEvent<PreLoginEvent.PreLoginCompon
} }
/** /**
* Returns a result indicating the connection will be allowed through the proxy, but the connection will be * Returns a result indicating the connection will be allowed through
* forced to use offline mode even when proxy running in online mode * the proxy, but the connection will be forced to use offline mode even
* when proxy running in online mode
* @return the result * @return the result
*/ */
public static PreLoginComponentResult forceOfflineMode() { public static PreLoginComponentResult forceOfflineMode() {
@ -127,9 +139,16 @@ public class PreLoginEvent implements ResultedEvent<PreLoginEvent.PreLoginCompon
* @param reason the reason for disallowing the connection * @param reason the reason for disallowing the connection
* @return a new result * @return a new result
*/ */
public static PreLoginComponentResult denied(@NonNull Component reason) { public static PreLoginComponentResult denied(Component reason) {
Preconditions.checkNotNull(reason, "reason"); Preconditions.checkNotNull(reason, "reason");
return new PreLoginComponentResult(reason); return new PreLoginComponentResult(Result.DISALLOWED, reason);
}
private enum Result {
ALLOWED,
FORCE_ONLINE,
FORCE_OFFLINE,
DISALLOWED
} }
} }
} }