fix: ServerLoginNetworkHandlerMixin#isLocal is acting incorrectly

This commit is contained in:
iceBear67 committed 2024-07-14 00:03:39 +08:00
1 parent 3e15f1e8fa
commit 060f0230eb
4 files changed
+18 -20

No files matched your search

@@ -19,7 +19,7 @@ import static io.ib67.sfcraft.callback.Utility.*;
public interface SFCallbacks { public interface SFCallbacks {
Event<PlayerPreLoginCallback> PRE_LOGIN = EventFactory.createArrayBacked(PlayerPreLoginCallback.class, Event<PlayerPreLoginCallback> PRE_LOGIN = EventFactory.createArrayBacked(PlayerPreLoginCallback.class,
(listeners) -> (s, c, d) -> anyMatch(listeners, i -> i.onPlayerPreLogin(s, c, d))); (listeners) -> (s, c, d, o) -> forEach(listeners, i -> i.onPlayerPreLogin(s, c, d, o)));
Event<PlayerSleepCallback> PLAYER_SLEEP = EventFactory.createArrayBacked(PlayerSleepCallback.class, Event<PlayerSleepCallback> PLAYER_SLEEP = EventFactory.createArrayBacked(PlayerSleepCallback.class,
l -> (pl, pos) -> anyMatch(l, i -> i.onPlayerSleep(pl, pos), a -> a.left().isPresent()) l -> (pl, pos) -> anyMatch(l, i -> i.onPlayerSleep(pl, pos), a -> a.left().isPresent())
.orElse(getEitherUnitR())); .orElse(getEitherUnitR()));
@@ -38,10 +38,7 @@ public interface SFCallbacks {
} }
interface PlayerPreLoginCallback { interface PlayerPreLoginCallback {
/** void onPlayerPreLogin(String currentPlayer, ClientConnection connection, Consumer<Text> disconnect, boolean offline);
* @return if let in
*/
boolean onPlayerPreLogin(String currentPlayer, ClientConnection connection, Consumer<Text> disconnect);
} }
interface PlayerSleepCallback { interface PlayerSleepCallback {
@@ -44,19 +44,14 @@ public abstract class ServerLoginNetworkHandlerMixin {
} }
var offlineProfile = Uuids.getOfflinePlayerProfile(currentPlayer); var offlineProfile = Uuids.getOfflinePlayerProfile(currentPlayer);
boolean _r; boolean _r;
if (server.getUserCache() == null) { if (server.getUserCache() != null) {
// falling back to whitelist _r = server.getUserCache().getByUuid(offlineProfile.getId()).isPresent();
}else{
var wl = server.getPlayerManager().getWhitelist(); var wl = server.getPlayerManager().getWhitelist();
_r = wl.isAllowed(offlineProfile); _r = wl.isAllowed(offlineProfile);
} else {
var profile = server.getUserCache().getByUuid(offlineProfile.getId());
_r = profile.isPresent();
} }
var result = SFCallbacks.PRE_LOGIN.invoker().onPlayerPreLogin(currentPlayer, connection, this::disconnect); SFCallbacks.PRE_LOGIN.invoker().onPlayerPreLogin(currentPlayer, connection, this::disconnect, _r);
if (result) { return _r;
return _r; // (is offline -> !true -> startVerify)
}
return !_r;
} }
@@ -10,6 +10,7 @@ import io.ib67.sfcraft.util.SFConsts;
import io.ib67.sfcraft.ServerModule; import io.ib67.sfcraft.ServerModule;
import io.ib67.sfcraft.callback.SFCallbacks; import io.ib67.sfcraft.callback.SFCallbacks;
import io.ib67.sfcraft.config.SFConfig; import io.ib67.sfcraft.config.SFConfig;
import lombok.Setter;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess; import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.network.ClientConnection; import net.minecraft.network.ClientConnection;
@@ -29,6 +30,8 @@ public class OfflineExemptModule extends ServerModule {
@Inject @Inject
private SFConfig config; private SFConfig config;
private boolean state; private boolean state;
@Setter
private boolean temporallyState;
@Override @Override
public void onInitialize() { public void onInitialize() {
@@ -63,6 +66,8 @@ public class OfflineExemptModule extends ServerModule {
} }
var profile = Uuids.getOfflinePlayerProfile(player); var profile = Uuids.getOfflinePlayerProfile(player);
wl.add(new WhitelistEntry(profile)); wl.add(new WhitelistEntry(profile));
var uc = source.getServer().getUserCache();
if (uc != null) uc.add(profile);
source.sendMessage(Text.of("[" + profile.getName() + "/" + profile.getId() + "]" + " is added!")); source.sendMessage(Text.of("[" + profile.getName() + "/" + profile.getId() + "]" + " is added!"));
return 0; return 0;
} }
@@ -85,10 +90,12 @@ public class OfflineExemptModule extends ServerModule {
@Override @Override
public void onEnable() { public void onEnable() {
state = true; state = temporallyState || config.enableOfflineExempt;
} }
private boolean onPreLogin(String s, ClientConnection connection, Consumer<Text> textConsumer) { private void onPreLogin(String s, ClientConnection connection, Consumer<Text> textConsumer, boolean offline) {
return state; if (!state && offline) {
textConsumer.accept(Text.of("盗版赦免现已关闭"));
}
} }
} }
@@ -79,7 +79,7 @@ public class PlayLimitModule extends ServerModule {
} }
private boolean onPreLogin(String currentPlayer, ClientConnection connection, Consumer<Text> disconnect) { private void onPreLogin(String currentPlayer, ClientConnection connection, Consumer<Text> disconnect, boolean offline) {
if (isEnabled() && connection.getAddress() instanceof InetSocketAddress address) { if (isEnabled() && connection.getAddress() instanceof InetSocketAddress address) {
try { try {
var clock = geoIPService.clockOf(address.getAddress()); var clock = geoIPService.clockOf(address.getAddress());
@@ -91,6 +91,5 @@ public class PlayLimitModule extends ServerModule {
System.err.println("Failed to locate " + address.getAddress()); System.err.println("Failed to locate " + address.getAddress());
} }
} }
return true;
} }
} }