Skip to content

Commit

Permalink
/forceoffline add and remote are now effective immediately
Browse files Browse the repository at this point in the history
Previously, a server restart is required to update the list.
Now, it works immediately, but only applicable to newly joined players.
  • Loading branch information
rikka0w0 committed Dec 31, 2024
1 parent 82416d4 commit c2e2e77
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/main/java/io/github/satxm/mcwifipnp/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.gson.annotations.SerializedName;

import io.github.satxm.mcwifipnp.mixin.PlayerListAccessor;
import net.minecraft.client.server.IntegratedServer;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.players.PlayerList;
Expand Down Expand Up @@ -142,7 +141,7 @@ public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext
}
}

public void readFromRunningServer(IntegratedServer server) {
public void readFromRunningServer(MinecraftServer server) {
PlayerList playerList = server.getPlayerList();

this.port = server.getPort();
Expand All @@ -160,7 +159,7 @@ public void readFromRunningServer(IntegratedServer server) {
this.forcedOfflinePlayers = UUIDFixer.alwaysOfflinePlayers;
}

public void applyTo(IntegratedServer server) {
public void applyTo(MinecraftServer server) {
PlayerList playerList = server.getPlayerList();
server.setDefaultGameType(this.gameType);
playerList.setAllowCommandsForAllPlayers(this.allowEveryoneCheat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;

import io.github.satxm.mcwifipnp.Config;
import io.github.satxm.mcwifipnp.UUIDFixer;

import java.util.Collection;

Expand Down Expand Up @@ -42,10 +43,9 @@ public static void register(CommandDispatcher<CommandSourceStack> commandDispatc
.suggests((commandContext, suggestionsBuilder) -> {
MinecraftServer server = ((CommandSourceStack) commandContext.getSource()).getServer();
PlayerList playerList = server.getPlayerList();
Config cfg = Config.read(server);
return SharedSuggestionProvider.suggest(playerList.getPlayers().stream()
.filter(
(serverPlayer) -> !cfg.forcedOfflinePlayers.contains(serverPlayer.getGameProfile().getName())
(serverPlayer) -> !UUIDFixer.alwaysOfflinePlayers.contains(serverPlayer.getGameProfile().getName())
).map(
(serverPlayer) -> serverPlayer.getGameProfile().getName()
), suggestionsBuilder);
Expand All @@ -58,9 +58,7 @@ public static void register(CommandDispatcher<CommandSourceStack> commandDispatc
RequiredArgumentBuilder<CommandSourceStack, Result> removeCmdTargets =
Commands.argument("targets", GameProfileArgument.gameProfile())
.suggests((commandContext, suggestionsBuilder) -> {
MinecraftServer server = ((CommandSourceStack) commandContext.getSource()).getServer();
Config cfg = Config.read(server);
return SharedSuggestionProvider.suggest(cfg.forcedOfflinePlayers.stream(), suggestionsBuilder);
return SharedSuggestionProvider.suggest(UUIDFixer.alwaysOfflinePlayers.stream(), suggestionsBuilder);
}).executes((commandContext) -> {
return removePlayers((CommandSourceStack) commandContext.getSource(),
GameProfileArgument.getGameProfiles(commandContext, "targets"));
Expand All @@ -75,11 +73,12 @@ private static int addPlayers(CommandSourceStack commandSourceStack, Collection<

MinecraftServer server = commandSourceStack.getServer();
Config cfg = Config.read(server);
cfg.readFromRunningServer(server);

int added = 0;
for (GameProfile gameProfile : collection) {
if (!cfg.forcedOfflinePlayers.contains(gameProfile.getName())) {
cfg.forcedOfflinePlayers.add(gameProfile.getName());
if (!UUIDFixer.alwaysOfflinePlayers.contains(gameProfile.getName())) {
UUIDFixer.alwaysOfflinePlayers.add(gameProfile.getName());
commandSourceStack.sendSuccess(() -> {
return Component.translatable("mcwifipnp.commands.forceoffline.add.success",
Component.literal(gameProfile.getName()));
Expand All @@ -101,11 +100,12 @@ private static int removePlayers(CommandSourceStack commandSourceStack, Collecti

MinecraftServer server = commandSourceStack.getServer();
Config cfg = Config.read(server);
cfg.readFromRunningServer(server);

int removed = 0;
for (GameProfile gameProfile: collection) {
if (cfg.forcedOfflinePlayers.contains(gameProfile.getName())) {
cfg.forcedOfflinePlayers.remove(gameProfile.getName());
if (UUIDFixer.alwaysOfflinePlayers.contains(gameProfile.getName())) {
UUIDFixer.alwaysOfflinePlayers.remove(gameProfile.getName());
commandSourceStack.sendSuccess(() -> {
return Component.translatable("mcwifipnp.commands.forceoffline.remove.success",
Component.literal(gameProfile.getName()));
Expand All @@ -124,20 +124,17 @@ private static int removePlayers(CommandSourceStack commandSourceStack, Collecti
}

private static int showList(CommandSourceStack commandSourceStack) {
MinecraftServer server = commandSourceStack.getServer();
Config cfg = Config.read(server);

if (cfg.forcedOfflinePlayers.size() == 0) {
if (UUIDFixer.alwaysOfflinePlayers.size() == 0) {
commandSourceStack.sendSuccess(() -> {
return Component.translatable("mcwifipnp.commands.forceoffline.none");
}, false);
} else {
commandSourceStack.sendSuccess(() -> {
return Component.translatable("mcwifipnp.commands.forceoffline.list",
cfg.forcedOfflinePlayers.size(), StringUtils.join(cfg.forcedOfflinePlayers, ", "));
UUIDFixer.alwaysOfflinePlayers.size(), StringUtils.join(UUIDFixer.alwaysOfflinePlayers, ", "));
}, false);
}

return cfg.forcedOfflinePlayers.size();
return UUIDFixer.alwaysOfflinePlayers.size();
}
}

0 comments on commit c2e2e77

Please sign in to comment.