Skip to content

Commit

Permalink
Fix config naming convention and associated field naming
Browse files Browse the repository at this point in the history
The json config is still backward compatible
  • Loading branch information
rikka0w0 authored and Satxm committed Dec 19, 2024
1 parent ff04666 commit 9c0493e
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 154 deletions.
75 changes: 47 additions & 28 deletions src/main/java/io/github/satxm/mcwifipnp/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,44 @@
import net.minecraft.world.level.storage.LevelResource;

public class Config {
// The initial value are the defaults

// These fields require special handling and consideration
public int port = 25565;
@SerializedName(value = "enableHostCheat", alternate = {"AllowCommands"})
public boolean enableHostCheat = false; // Host cheats

@SerializedName(value = "allow-host-cheat", alternate = {"AllowCommands"})
public boolean allowHostCheat = false;

// These fields are read, synced, and save as normal
@SerializedName(value = "max-players", alternate = {"maxPlayers"})
public int maxPlayers = 8;
public GameType GameMode = GameType.SURVIVAL;

@SerializedName(value = "gamemode", alternate = {"GameMode"})
public GameType gameType = GameType.SURVIVAL;

public String motd = Component.translatable("lanServer.title").getString();
public boolean AllPlayersCheats = false;
public boolean Whitelist = false;
public boolean UseUPnP = true;
public boolean OnlineMode = true;
public boolean EnableUUIDFixer = false;
public List<String> ForceOfflinePlayers = Collections.emptyList();
public boolean PvP = true;
public boolean CopyToClipboard = true;

@SerializedName(value = "allow-everyone-cheat", alternate = {"AllPlayersCheats"})
public boolean allowEveryoneCheat = false;

@SerializedName(value = "enforce-whitelist", alternate = {"Whitelist"})
public boolean enforceWhitelist = false;

@SerializedName(value = "enable-upnp", alternate = {"UseUPnP"})
public boolean useUPnP = true;

@SerializedName(value = "online-mode", alternate = {"OnlineMode"})
public boolean onlineMode = true;

@SerializedName(value = "enable-uuid-fixer", alternate = {"EnableUUIDFixer"})
public boolean enableUUIDFixer = false;

@SerializedName(value = "forced-offline-players", alternate = {"ForceOfflinePlayers"})
public List<String> forcedOfflinePlayers = Collections.emptyList();

@SerializedName(value = "pvp", alternate = {"PvP"})
public boolean enablePvP = true;

@SerializedName(value = "get-public-ip", alternate = {"CopyToClipboard"})
public boolean getPublicIP = true;

// These fields will not be serialized
public transient Path location;
Expand Down Expand Up @@ -128,32 +147,32 @@ public void readFromRunningServer(IntegratedServer server) {

this.port = server.getPort();

this.AllPlayersCheats = playerList.isAllowCommandsForAllPlayers();
this.GameMode = server.getDefaultGameType();
this.allowEveryoneCheat = playerList.isAllowCommandsForAllPlayers();
this.gameType = server.getDefaultGameType();

this.maxPlayers = playerList.getMaxPlayers();
this.OnlineMode = server.usesAuthentication();
this.PvP = server.isPvpAllowed();
this.Whitelist = server.isEnforceWhitelist();
this.onlineMode = server.usesAuthentication();
this.enablePvP = server.isPvpAllowed();
this.enforceWhitelist = server.isEnforceWhitelist();

this.motd = server.getMotd();
this.EnableUUIDFixer = UUIDFixer.EnableUUIDFixer;
this.ForceOfflinePlayers = UUIDFixer.ForceOfflinePlayers;
this.enableUUIDFixer = UUIDFixer.tryOnlineFirst;
this.forcedOfflinePlayers = UUIDFixer.alwaysOfflinePlayers;
}

public void applyTo(IntegratedServer server) {
PlayerList playerList = server.getPlayerList();
server.setDefaultGameType(this.GameMode);
playerList.setAllowCommandsForAllPlayers(this.AllPlayersCheats);
server.setDefaultGameType(this.gameType);
playerList.setAllowCommandsForAllPlayers(this.allowEveryoneCheat);

((PlayerListAccessor) playerList).setMaxPlayers(this.maxPlayers);
server.setUsesAuthentication(this.OnlineMode);
server.setPvpAllowed(this.PvP);
server.setEnforceWhitelist(this.Whitelist);
playerList.setUsingWhiteList(this.Whitelist);
server.setUsesAuthentication(this.onlineMode);
server.setPvpAllowed(this.enablePvP);
server.setEnforceWhitelist(this.enforceWhitelist);
playerList.setUsingWhiteList(this.enforceWhitelist);

server.setMotd(this.motd);
UUIDFixer.EnableUUIDFixer = this.EnableUUIDFixer;
UUIDFixer.ForceOfflinePlayers = this.ForceOfflinePlayers;
UUIDFixer.tryOnlineFirst = this.enableUUIDFixer;
UUIDFixer.alwaysOfflinePlayers = this.forcedOfflinePlayers;
}
}
10 changes: 5 additions & 5 deletions src/main/java/io/github/satxm/mcwifipnp/ForceOfflineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void register(CommandDispatcher<CommandSourceStack> commandDispatc
MinecraftServer server = ((CommandSourceStack)commandContext.getSource()).getServer();
PlayerList playerList = server.getPlayerList();
Config cfg = Config.read(server);
List<String> ForceOfflinePlayers = cfg.ForceOfflinePlayers;
List<String> ForceOfflinePlayers = cfg.forcedOfflinePlayers;
return SharedSuggestionProvider.suggest(playerList.getPlayers().stream().filter((serverPlayer) -> {
return !ForceOfflinePlayers.contains(serverPlayer.getGameProfile().getName());
}).map((serverPlayer) -> {
Expand All @@ -47,7 +47,7 @@ public static void register(CommandDispatcher<CommandSourceStack> commandDispatc
MinecraftServer server = ((CommandSourceStack)commandContext.getSource()).getServer();
PlayerList playerList = server.getPlayerList();
Config cfg = Config.read(server);
List<String> ForceOfflinePlayers = cfg.ForceOfflinePlayers;
List<String> ForceOfflinePlayers = cfg.forcedOfflinePlayers;
return SharedSuggestionProvider.suggest(ForceOfflinePlayers.stream(), suggestionsBuilder);
}).executes((commandContext) -> {
return removePlayers((CommandSourceStack)commandContext.getSource(), GameProfileArgument.getGameProfiles(commandContext, "targets"));
Expand All @@ -58,7 +58,7 @@ private static int addPlayers(CommandSourceStack commandSourceStack, Collection<

MinecraftServer server = commandSourceStack.getServer();
Config cfg = Config.read(server);
List<String> ForceOfflinePlayers = cfg.ForceOfflinePlayers;
List<String> ForceOfflinePlayers = cfg.forcedOfflinePlayers;
int i = 0;
Iterator var4 = collection.iterator();

Expand All @@ -84,7 +84,7 @@ private static int addPlayers(CommandSourceStack commandSourceStack, Collection<
private static int removePlayers(CommandSourceStack commandSourceStack, Collection<GameProfile> collection) throws CommandSyntaxException {
MinecraftServer server = commandSourceStack.getServer();
Config cfg = Config.read(server);
List<String> ForceOfflinePlayers = cfg.ForceOfflinePlayers;
List<String> ForceOfflinePlayers = cfg.forcedOfflinePlayers;

int i = 0;
Iterator var4 = collection.iterator();
Expand Down Expand Up @@ -112,7 +112,7 @@ private static int removePlayers(CommandSourceStack commandSourceStack, Collecti
private static int showList(CommandSourceStack commandSourceStack) {
MinecraftServer server = commandSourceStack.getServer();
Config cfg = Config.read(server);
List<String> ForceOfflinePlayers = cfg.ForceOfflinePlayers;
List<String> ForceOfflinePlayers = cfg.forcedOfflinePlayers;

if (ForceOfflinePlayers.size() == 0) {
commandSourceStack.sendSuccess(() -> {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/github/satxm/mcwifipnp/MCWiFiPnPUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void publishServer(Config cfg) {
IntegratedServer server = client.getSingleplayerServer();
PlayerList playerList = server.getPlayerList();

MutableComponent component = server.publishServer(cfg.GameMode, cfg.enableHostCheat, cfg.port)
MutableComponent component = server.publishServer(cfg.gameType, cfg.allowHostCheat, cfg.port)
? PublishCommand.getSuccessMessage(cfg.port)
: Component.translatable("commands.publish.failed");
client.gui.getChat().addMessage(component);
Expand All @@ -79,7 +79,7 @@ public static void publishServer(Config cfg) {
}

public static void CopyToClipboard(Config cfg, Minecraft client) {
if (cfg.CopyToClipboard) {
if (cfg.getPublicIP) {
ArrayList<Component> IPComponentList = new ArrayList<Component>();
ArrayList<String> IPList = new ArrayList<String>();
for (int i = 0; i < IPAddressList().size(); i++) {
Expand Down Expand Up @@ -107,7 +107,7 @@ public static void CopyToClipboard(Config cfg, Minecraft client) {
+ GetGlobalIPv6().get("Type"), "[" + GetGlobalIPv6().get("IP") + "]:" + cfg.port));
IPList.add(GetGlobalIPv4().get("IP"));
}
if (cfg.UseUPnP && UPnP.getExternalIP() != null && !IPList.contains(GetGlobalIPv6().get("IP"))) {
if (cfg.useUPnP && UPnP.getExternalIP() != null && !IPList.contains(GetGlobalIPv6().get("IP"))) {
IPComponentList.add(IPComponent("UPnP IPv4", UPnP.getExternalIP() + ":" + cfg.port));
IPList.add(UPnP.getExternalIP());
}
Expand Down
67 changes: 29 additions & 38 deletions src/main/java/io/github/satxm/mcwifipnp/OnlineMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,33 @@
import net.minecraft.network.chat.Component;

public enum OnlineMode {
ONLINE(true, false,"online"),
OFFLINE(false, false,"offline"),
FIXUUID(false, true,"fixuuid");

private final boolean onlinemode, fixuuid;
private final Component displayName, toolTip;

OnlineMode(boolean onlinemode, boolean fixuuid, final String string) {
this.onlinemode = onlinemode;
this.fixuuid = fixuuid;
this.displayName = Component.translatable("mcwifipnp.gui.OnlineMode." + string);
this.toolTip = Component.translatable("mcwifipnp.gui.OnlineMode." + string +".info");
}

public Component getDisplayName() {
return this.displayName;
}

public Component gettoolTip() {
return this.toolTip;
}

public static OnlineMode of(boolean onlinemode, boolean fixuuid) {
if (onlinemode) {
return ONLINE;
} else {
return fixuuid ? FIXUUID : OFFLINE;
}
}

public boolean getOnlieMode() {
return this.onlinemode;
}

public boolean getFixUUID() {
return this.fixuuid;
}

ONLINE(true, false, "online"),
OFFLINE(false, false, "offline"),
FIX_UUID(false, true, "fixuuid");

public final boolean onlineMode, fixUUID;
private final Component displayName, toolTip;

OnlineMode(boolean onlineModeEnabled, boolean tryOnlineUUIDFirst, final String name) {
this.onlineMode = onlineModeEnabled;
this.fixUUID = tryOnlineUUIDFirst;
this.displayName = Component.translatable("mcwifipnp.gui.OnlineMode." + name);
this.toolTip = Component.translatable("mcwifipnp.gui.OnlineMode." + name + ".info");
}

public Component getDisplayName() {
return this.displayName;
}

public Component gettoolTip() {
return this.toolTip;
}

public static OnlineMode of(boolean onlineModeEnabled, boolean tryOnlineUUIDFirst) {
if (onlineModeEnabled) {
return ONLINE;
} else {
return tryOnlineUUIDFirst ? FIX_UUID : OFFLINE;
}
}
}
54 changes: 27 additions & 27 deletions src/main/java/io/github/satxm/mcwifipnp/ShareToLanScreenNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ public ShareToLanScreenNew(Screen screen, boolean serverPublished) {
} else if (this.cfg.usingDefaults) {
this.cfg.readFromRunningServer(server);
this.cfg.port = HttpUtil.getAvailablePort();
this.cfg.enableHostCheat = server.getWorldData().isAllowCommands();
this.cfg.allowHostCheat = server.getWorldData().isAllowCommands();
}

this.oldMotd = this.cfg.motd;
this.oldUPnPEnabled = this.cfg.UseUPnP;
this.oldUPnPEnabled = this.cfg.useUPnP;
}

protected void onConfirmClicked() {
IntegratedServer server = Minecraft.getInstance().getSingleplayerServer();
this.cfg.save();

if (this.serverPublished) {
if (!this.oldMotd.equals(this.cfg.motd) || this.cfg.UseUPnP ^ oldUPnPEnabled) {
if (!this.oldMotd.equals(this.cfg.motd) || this.cfg.useUPnP ^ oldUPnPEnabled) {
// Motd has changed, update UPnP display name
UPnPModule.stop(server);
UPnPModule.startIfEnabled(server, cfg);
Expand Down Expand Up @@ -187,46 +187,46 @@ public DefaultTab() {
// Row3
// Allow Cheat button (for other joined players)
if (!ShareToLanScreenNew.this.serverPublished) {
tabContents.addChild(CycleButton.onOffBuilder(cfg.enableHostCheat)
.create(Component.translatable("selectWorld.allowCommands"), (cycleButton, AllowCommands) -> {
cfg.enableHostCheat = AllowCommands;
tabContents.addChild(CycleButton.onOffBuilder(cfg.allowHostCheat)
.create(Component.translatable("selectWorld.allowCommands"), (cycleButton, allowHostCheat) -> {
cfg.allowHostCheat = allowHostCheat;
}), 2);
}

tabContents.addChild(CycleButton.onOffBuilder(cfg.Whitelist)
tabContents.addChild(CycleButton.onOffBuilder(cfg.enforceWhitelist)
.withTooltip((state) -> Tooltip.create(Component.translatable("mcwifipnp.gui.Whitelist.info")))
.create(Component.translatable("mcwifipnp.gui.Whitelist"), (cycleButton, Whitelist) -> {
cfg.Whitelist = Whitelist;
.create(Component.translatable("mcwifipnp.gui.Whitelist"), (cycleButton, enforceWhitelist) -> {
cfg.enforceWhitelist = enforceWhitelist;
}), 2);

// Row 4
tabContents.addChild(CycleButton.builder(OnlineMode::getDisplayName)
.withValues(OnlineMode.values())
.withInitialValue(OnlineMode.of(cfg.OnlineMode, cfg.EnableUUIDFixer))
.withInitialValue(OnlineMode.of(cfg.onlineMode, cfg.enableUUIDFixer))
.withTooltip((OnlineMode) -> Tooltip.create(OnlineMode.gettoolTip()))
.create(Component.translatable("mcwifipnp.gui.OnlineMode"), (cycleButton, OnlineMode) -> {
cfg.OnlineMode = OnlineMode.getOnlieMode();
cfg.EnableUUIDFixer = OnlineMode.getFixUUID();
.create(Component.translatable("mcwifipnp.gui.OnlineMode"), (cycleButton, onlineMode) -> {
cfg.onlineMode = onlineMode.onlineMode;
cfg.enableUUIDFixer = onlineMode.fixUUID;
}), 2);

tabContents.addChild(CycleButton.onOffBuilder(cfg.PvP)
tabContents.addChild(CycleButton.onOffBuilder(cfg.enablePvP)
.withTooltip((state) -> Tooltip.create(Component.translatable("mcwifipnp.gui.PvP.info")))
.create(Component.translatable("mcwifipnp.gui.PvP"), (cycleButton, PvP) -> {
cfg.PvP = PvP;
cfg.enablePvP = PvP;
}), 2);

// Row 5
tabContents.addChild(CycleButton.onOffBuilder(cfg.UseUPnP)
tabContents.addChild(CycleButton.onOffBuilder(cfg.useUPnP)
.withTooltip((state) -> Tooltip.create(Component.translatable("mcwifipnp.gui.UseUPnP.info")))
.create(Component.translatable("mcwifipnp.gui.UseUPnP"), (cycleButton, UseUPnP) -> {
cfg.UseUPnP = UseUPnP;
.create(Component.translatable("mcwifipnp.gui.UseUPnP"), (cycleButton, useUPnP) -> {
cfg.useUPnP = useUPnP;
}), 2);

if (!ShareToLanScreenNew.this.serverPublished) {
tabContents.addChild(CycleButton.onOffBuilder(cfg.CopyToClipboard)
tabContents.addChild(CycleButton.onOffBuilder(cfg.getPublicIP)
.withTooltip((state) -> Tooltip.create(Component.translatable("mcwifipnp.gui.CopyIP.info")))
.create(Component.translatable("mcwifipnp.gui.CopyIP"), (cycleButton, CopyToClipboard) -> {
cfg.CopyToClipboard = CopyToClipboard;
.create(Component.translatable("mcwifipnp.gui.CopyIP"), (cycleButton, getPublicIP) -> {
cfg.getPublicIP = getPublicIP;
}), 2);
}

Expand All @@ -242,16 +242,16 @@ public DefaultTab() {
// Row 7
// GameMode toggle button
tabContents.addChild(CycleButton.builder(GameType::getShortDisplayName)
.withValues(GameType.values()).withInitialValue(cfg.GameMode)
.create(Component.translatable("selectWorld.gameMode"), (cycleButton, gameMode) -> {
cfg.GameMode = gameMode;
.withValues(GameType.values()).withInitialValue(cfg.gameType)
.create(Component.translatable("selectWorld.gameMode"), (cycleButton, gameType) -> {
cfg.gameType = gameType;
}), 2);

// Allow Cheat button (for other joined players)
tabContents.addChild(CycleButton.onOffBuilder(cfg.AllPlayersCheats)
tabContents.addChild(CycleButton.onOffBuilder(cfg.allowEveryoneCheat)
.withTooltip((state) ->Tooltip.create(Component.translatable("mcwifipnp.gui.AllPlayersCheats.info")))
.create(Component.translatable("mcwifipnp.gui.AllPlayersCheats"), (cycleButton, AllPlayersCheats) -> {
cfg.AllPlayersCheats = AllPlayersCheats;
.create(Component.translatable("mcwifipnp.gui.AllPlayersCheats"), (cycleButton, allowEveryoneCheat) -> {
cfg.allowEveryoneCheat = allowEveryoneCheat;
}), 2);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/satxm/mcwifipnp/UPnPModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private UPnPModule(int port, String motd) {
}

public static void startIfEnabled(MinecraftServer server, Config cfg) {
if (!cfg.UseUPnP) {
if (!cfg.useUPnP) {
((IUPnPProvider) server).setUPnPInstance(null);
return;
}
Expand Down
Loading

0 comments on commit 9c0493e

Please sign in to comment.