Skip to content

Commit

Permalink
Implement LAN server properties tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
ACGaming committed Jan 20, 2024
1 parent f46472b commit cc97c12
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ All changes are toggleable via config files.
* **Incurable Potions:** Excludes potion effects from being curable with curative items like buckets of milk
* **Infinite Music:** Lets background music play continuously without delays
* **Item Entities:** Enables the modification of item entity properties
* **LAN Server Properties:** Enhance the vanilla 'Open to LAN' GUI for listening port customization, removal of enforced authentication and more
* **Lenient Paths:** Allows the creation of grass paths everywhere (beneath fence gates, trapdoors, ...)
* **Lightning**
* **Lightning Damage:** Sets the damage lightning bolts deal to entities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import mod.acgaming.universaltweaks.tweaks.misc.armorcurve.UTArmorCurve;
import mod.acgaming.universaltweaks.tweaks.misc.endportal.UTEndPortalParallax;
import mod.acgaming.universaltweaks.tweaks.misc.incurablepotions.UTIncurablePotions;
import mod.acgaming.universaltweaks.tweaks.misc.gui.lanserverproperties.UTLanServerProperties;
import mod.acgaming.universaltweaks.tweaks.misc.loadsound.UTLoadSound;
import mod.acgaming.universaltweaks.tweaks.misc.pickupnotification.UTPickupNotificationOverlay;
import mod.acgaming.universaltweaks.tweaks.misc.swingthroughgrass.UTSwingThroughGrassLists;
Expand Down Expand Up @@ -156,6 +157,7 @@ public void initClient(FMLInitializationEvent event)
if (UTConfigBugfixes.BLOCKS.BLOCK_OVERLAY.utBlockOverlayToggle) UTBlockOverlayLists.initLists();
if (UTConfigTweaks.BLOCKS.BETTER_PLACEMENT.utBetterPlacementToggle) MinecraftForge.EVENT_BUS.register(UTBetterPlacement.class);
if (UTConfigTweaks.MISC.utEndPortalParallaxToggle) UTEndPortalParallax.initRenderer();
if (UTConfigTweaks.MISC.utLANServerProperties) MinecraftForge.EVENT_BUS.register(UTLanServerProperties.class);
if (UTConfigTweaks.MISC.PICKUP_NOTIFICATION.utPickupNotificationToggle) UTPickupNotificationOverlay.init();
if (Loader.isModLoaded("botania")) MinecraftForge.EVENT_BUS.register(UTBotaniaFancySkybox.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,11 @@ public static class MiscCategory
@Config.Comment("Lets background music play continuously without delays")
public boolean utInfiniteMusicToggle = false;

@Config.RequiresMcRestart
@Config.Name("LAN Server Properties")
@Config.Comment("Enhance the vanilla 'Open to LAN' GUI for listening port customization, removal of enforced authentication and more")
public boolean utLANServerProperties = true;

@Config.RequiresMcRestart
@Config.Name("Linear XP Amount")
@Config.Comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public List<String> getMixinConfigs()
configs.add("mixins.tweaks.misc.commands.seed.json");
configs.add("mixins.tweaks.misc.credits.json");
configs.add("mixins.tweaks.misc.difficulty.client.json");
configs.add("mixins.tweaks.misc.gui.lanserverproperties.json");
configs.add("mixins.tweaks.misc.gui.overlaymessage.json");
configs.add("mixins.tweaks.misc.gui.selecteditemtooltip.json");
configs.add("mixins.tweaks.misc.lightning.flash.json");
Expand Down Expand Up @@ -283,6 +284,8 @@ public boolean shouldMixinConfigQueue(String mixinConfig)
return UTConfigTweaks.MISC.utCopyWorldSeedToggle;
case "mixins.tweaks.misc.credits.json":
return UTConfigTweaks.MISC.utSkipCreditsToggle;
case "mixins.tweaks.misc.gui.lanserverproperties.json":
return UTConfigTweaks.MISC.utLANServerProperties;
case "mixins.tweaks.misc.gui.overlaymessage.json":
return UTConfigTweaks.MISC.utOverlayMessageHeight != -4;
case "mixins.tweaks.misc.gui.selecteditemtooltip.json":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package mod.acgaming.universaltweaks.tweaks.misc.gui.lanserverproperties;

import java.io.IOException;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiShareToLan;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.GameType;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import mod.acgaming.universaltweaks.UniversalTweaks;
import mod.acgaming.universaltweaks.tweaks.misc.gui.lanserverproperties.mixin.GuiShareToLanAccessor;
import mod.acgaming.universaltweaks.tweaks.misc.gui.lanserverproperties.mixin.PlayerListAccessor;

// Courtesy of rikka0w0
@SideOnly(Side.CLIENT)
public class UTGuiShareToLan extends GuiShareToLan
{
public static final String ONLINE_MODE_LANG_KEY = "btn.universaltweaks.lanserverproperties.online_mode";
public static final String ONLINE_MODE_LANG_KEY_DESC = "btn.universaltweaks.lanserverproperties.online_mode_desc";
public static final String SPAWN_ANIMALS_LANG_KEY = "btn.universaltweaks.lanserverproperties.spawn_animals";
public static final String SPAWN_NPCS_LANG_KEY = "btn.universaltweaks.lanserverproperties.spawn_npcs";
public static final String ALLOW_PVP_LANG_KEY = "btn.universaltweaks.lanserverproperties.allow_pvp";
public static final String ALLOW_FLIGHT_LANG_KEY = "btn.universaltweaks.lanserverproperties.allow_flight";
public static final String PORT_LANG_KEY = "btn.universaltweaks.lanserverproperties.port";
public static final String MAX_PLAYERS_LANG_KEY = "btn.universaltweaks.lanserverproperties.max_players";

public static int validatePort(String text)
{
boolean valid = true;
int port = -1;
try
{
if (!text.isEmpty())
{
port = Integer.parseInt(text);
if (port < 0 || port > 65535) valid = false;
}
}
catch (NumberFormatException e)
{
valid = false;
}

return valid ? port : -1;
}

protected final GuiScreen lastScreen;
protected GuiTextField portTextField = null;
protected GuiTextField maxPlayersTextField = null;
protected GuiButton onlineModeButton = null;
protected GuiButton spawnAnimalsButton = null;
protected GuiButton spawnNpcsButton = null;
protected GuiButton allowPvpButton = null;
protected GuiButton allowFlightButton = null;
protected boolean onlineMode = true;
protected boolean spawnAnimals = true;
protected boolean spawnNpcs = true;
protected boolean allowPvp = true;
protected boolean allowFlight = true;

public UTGuiShareToLan(GuiScreen lastScreenIn)
{
super(lastScreenIn);
this.lastScreen = lastScreenIn;
}

@Override
public void initGui()
{
super.initGui();

// Attempt to locate the old button
GuiButton button = null;
String msg = I18n.format("lanServer.start");
for (GuiButton widget : this.buttonList)
{
if (widget.displayString.equals(msg))
{
button = widget;
break;
}
}

if (button == null)
{
UniversalTweaks.LOGGER.info("UTGuiShareToLan ::: Unable to locate start server button!");
// If we cannot find the "Start LAN Server" button
// just leave everything else there
return;
}

// Add our own widgets
// Toggle button for Online Mode
this.onlineModeButton = new GuiButton(233, this.width / 2 - 155, 124, 150, 20, getOnlineButtonText());
this.addButton(this.onlineModeButton);

// Toggle button for Spawn Animals
this.spawnAnimalsButton = new GuiButton(234, this.width / 2 + 5, 124, 150, 20, getSpawnAnimalsButtonText());
this.addButton(this.spawnAnimalsButton);

// Toggle button for Spawn NPCs
this.spawnNpcsButton = new GuiButton(235, this.width / 2 + 5, 148, 150, 20, getSpawnNpcsButtonText());
this.addButton(this.spawnNpcsButton);

// Toggle button for Allow PVP
this.allowPvpButton = new GuiButton(236, this.width / 2 + 5, 172, 150, 20, getAllowPvpButtonText());
this.addButton(this.allowPvpButton);

// Toggle button for Allow Flight
this.allowFlightButton = new GuiButton(237, this.width / 2 + 5, 196, 150, 20, getAllowFlightButtonText());
this.addButton(this.allowFlightButton);

// Text field for Port
this.portTextField = new GuiTextField(238, this.fontRenderer, this.width / 2 - 154, this.height - 54, 148, 20);
this.portTextField.setText("25565");

// Text field for Max Players
this.maxPlayersTextField = new GuiTextField(239, this.fontRenderer, this.width / 2 + 6, this.height - 54, 148, 20);
this.maxPlayersTextField.setText("4");
}

@Override
protected void actionPerformed(GuiButton button) throws IOException
{
if (button.id == 101)
{
serverStartButtonClick(button);
}
else if (button.id == 233)
{
this.onlineMode = !this.onlineMode;
button.displayString = this.getOnlineButtonText();
}
else if (button.id == 234)
{
this.spawnAnimals = !this.spawnAnimals;
button.displayString = this.getSpawnAnimalsButtonText();
}
else if (button.id == 235)
{
this.spawnNpcs = !this.spawnNpcs;
button.displayString = this.getSpawnNpcsButtonText();
}
else if (button.id == 236)
{
this.allowPvp = !this.allowPvp;
button.displayString = this.getAllowPvpButtonText();
}
else if (button.id == 237)
{
this.allowFlight = !this.allowFlight;
button.displayString = this.getAllowFlightButtonText();
}
else
{
super.actionPerformed(button);
}
}

@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks)
{
super.drawScreen(mouseX, mouseY, partialTicks);

this.drawString(this.fontRenderer, I18n.format(PORT_LANG_KEY), this.width / 2 - 155, this.height - 66, 10526880);
this.portTextField.drawTextBox();

this.drawString(this.fontRenderer, I18n.format(MAX_PLAYERS_LANG_KEY), this.width / 2 + 5, this.height - 66, 10526880);
this.maxPlayersTextField.drawTextBox();

if (this.onlineModeButton.isMouseOver()) this.drawHoveringText(I18n.format(ONLINE_MODE_LANG_KEY_DESC), mouseX, mouseY);
}

@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException
{
if (this.portTextField.textboxKeyTyped(typedChar, keyCode))
{
// Check the format, make sure the text is a valid integer
this.portTextField.setTextColor(validatePort(this.portTextField.getText()) >= 0 ? 0xFFFFFF : 0xFF0000);
}
else if (this.maxPlayersTextField.textboxKeyTyped(typedChar, keyCode))
{
// Check the format, make sure the text is a valid integer
this.maxPlayersTextField.setTextColor(validatePort(this.maxPlayersTextField.getText()) >= 0 ? 0xFFFFFF : 0xFF0000);
}
else
{
super.keyTyped(typedChar, keyCode);
}
}

@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
{
super.mouseClicked(mouseX, mouseY, mouseButton);
this.portTextField.mouseClicked(mouseX, mouseY, mouseButton);
this.maxPlayersTextField.mouseClicked(mouseX, mouseY, mouseButton);
}

private void serverStartButtonClick(GuiButton button) throws IOException
{
this.mc.displayGuiScreen(null);

String portStr = portTextField.getText();
int port = !portStr.isEmpty() ? Integer.parseInt(portStr) : 25565;

String maxPlayersStr = maxPlayersTextField.getText();
int maxPlayers = !maxPlayersStr.isEmpty() ? Integer.parseInt(maxPlayersStr) : 4;

String gameMode = ((GuiShareToLanAccessor) this).getGameMode();
boolean allowCheats = ((GuiShareToLanAccessor) this).getAllowCheats();

ITextComponent textComponent;
String newPort = this.mc.getIntegratedServer().shareToLAN(GameType.getByName(gameMode), allowCheats);
if (newPort != null)
{
this.mc.getIntegratedServer().getNetworkSystem().addEndpoint(null, port);
textComponent = new TextComponentTranslation("commands.publish.started", newPort + ", " + port);
this.mc.getIntegratedServer().setOnlineMode(onlineMode);
this.mc.getIntegratedServer().setCanSpawnAnimals(spawnAnimals);
this.mc.getIntegratedServer().setCanSpawnNPCs(spawnNpcs);
this.mc.getIntegratedServer().setAllowPvp(allowPvp);
this.mc.getIntegratedServer().setAllowFlight(allowFlight);
((PlayerListAccessor) this.mc.getIntegratedServer().getPlayerList()).setMaxPlayers(maxPlayers);
}
else
{
textComponent = new TextComponentString("commands.publish.failed");
}
this.mc.ingameGUI.getChatGUI().printChatMessage(textComponent);
}

private String getOnlineButtonText()
{
return I18n.format(ONLINE_MODE_LANG_KEY) + ": " + I18n.format(this.onlineMode ? "options.on" : "options.off");
}

private String getSpawnAnimalsButtonText()
{
return I18n.format(SPAWN_ANIMALS_LANG_KEY) + ": " + I18n.format(this.spawnAnimals ? "options.on" : "options.off");
}

private String getSpawnNpcsButtonText()
{
return I18n.format(SPAWN_NPCS_LANG_KEY) + ": " + I18n.format(this.spawnNpcs ? "options.on" : "options.off");
}

private String getAllowPvpButtonText()
{
return I18n.format(ALLOW_PVP_LANG_KEY) + ": " + I18n.format(this.allowPvp ? "options.on" : "options.off");
}

private String getAllowFlightButtonText()
{
return I18n.format(ALLOW_FLIGHT_LANG_KEY) + ": " + I18n.format(this.allowFlight ? "options.on" : "options.off");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mod.acgaming.universaltweaks.tweaks.misc.gui.lanserverproperties;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiShareToLan;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

// Courtesy of rikka0w0
public class UTLanServerProperties
{
@SubscribeEvent
public static void guiOpenEventHandler(GuiOpenEvent event)
{
GuiScreen guiScreen = event.getGui();
if (guiScreen instanceof GuiShareToLan)
{
GuiScreen lastScreen = Minecraft.getMinecraft().currentScreen;
event.setGui(new UTGuiShareToLan(lastScreen));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mod.acgaming.universaltweaks.tweaks.misc.gui.lanserverproperties.mixin;

import net.minecraft.client.gui.GuiShareToLan;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(GuiShareToLan.class)
public interface GuiShareToLanAccessor
{
@Accessor("gameMode")
String getGameMode();

@Accessor("allowCheats")
boolean getAllowCheats();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mod.acgaming.universaltweaks.tweaks.misc.gui.lanserverproperties.mixin;

import net.minecraft.server.management.PlayerList;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(PlayerList.class)
public interface PlayerListAccessor
{
@Accessor("maxPlayers")
void setMaxPlayers(int maxPlayers);
}
12 changes: 11 additions & 1 deletion src/main/resources/assets/universaltweaks/lang/de_de.lang
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ msg.universaltweaks.sleep.spawnpoint=Spawnpunkt gesetzt
enchantment.parry=Rückschlag

# PICKUP NOTIFICATION
msg.universaltweaks.tweaks.pickupnotification.xp=Erfahrung
msg.universaltweaks.tweaks.pickupnotification.xp=Erfahrung

# LAN SERVER PROPERTIES
btn.universaltweaks.lanserverproperties.allow_flight=Erlaube Flug
btn.universaltweaks.lanserverproperties.allow_pvp=Erlaube PVP
btn.universaltweaks.lanserverproperties.max_players=Maximale Spieler
btn.universaltweaks.lanserverproperties.online_mode=Online-Modus
btn.universaltweaks.lanserverproperties.online_mode_desc=Deaktivierung ermöglicht nicht authentifizierte Verbindungen
btn.universaltweaks.lanserverproperties.port=Abhörender Port
btn.universaltweaks.lanserverproperties.spawn_animals=Erschaffe Tiere
btn.universaltweaks.lanserverproperties.spawn_npcs=Erschaffe NPCs
Loading

0 comments on commit cc97c12

Please sign in to comment.