From a7d7277bfa5274c816b93e4493447989c3d562da Mon Sep 17 00:00:00 2001 From: Torui <44932079+ToruiDev@users.noreply.github.com> Date: Sun, 8 May 2022 13:05:09 +0200 Subject: Feature/configuration (#59) * initial commit of configuration management * Updated Configuration Options * Review Feedback, IntelliJ was angry about TimerCountdown.java import Co-authored-by: Florian Rinke --- src/main/java/de/torui/coflsky/CoflSky.java | 4 +- src/main/java/de/torui/coflsky/EventRegistry.java | 2 +- .../java/de/torui/coflsky/WSCommandHandler.java | 5 +- .../de/torui/coflsky/commands/CommandType.java | 2 + .../torui/coflsky/configuration/Configuration.java | 37 +++++++ .../configuration/ConfigurationManager.java | 119 +++++++++++++++++++++ .../torui/coflsky/configuration/Description.java | 20 ++++ 7 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/torui/coflsky/configuration/Configuration.java create mode 100644 src/main/java/de/torui/coflsky/configuration/ConfigurationManager.java create mode 100644 src/main/java/de/torui/coflsky/configuration/Description.java diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index 4a48958..6e7fde7 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -6,6 +6,7 @@ import java.net.URISyntaxException; import org.lwjgl.input.Keyboard; +import de.torui.coflsky.configuration.ConfigurationManager; import de.torui.coflsky.network.WSClientWrapper; import net.minecraft.client.settings.KeyBinding; import net.minecraftforge.client.ClientCommandHandler; @@ -20,7 +21,7 @@ import net.minecraftforge.fml.relauncher.Side; public class CoflSky { public static final String MODID = "CoflSky"; - public static final String VERSION = "1.3.3-Alpha"; + public static final String VERSION = "1.4-Alpha"; public static WSClientWrapper Wrapper; public static KeyBinding[] keyBindings; @@ -39,7 +40,6 @@ public class CoflSky @EventHandler public void init(FMLInitializationEvent event) throws URISyntaxException { - System.out.println(">>>Started"); CoflSky.Wrapper = new WSClientWrapper(webSocketURIPrefix); diff --git a/src/main/java/de/torui/coflsky/EventRegistry.java b/src/main/java/de/torui/coflsky/EventRegistry.java index 238d7e7..5a1b58c 100644 --- a/src/main/java/de/torui/coflsky/EventRegistry.java +++ b/src/main/java/de/torui/coflsky/EventRegistry.java @@ -205,6 +205,6 @@ public class EventRegistry { @SubscribeEvent public void OnRenderTick(TickEvent.RenderTickEvent event) { - CountdownTimer.onRenderTick(event); + de.torui.coflsky.CountdownTimer.onRenderTick(event); } } diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index 616f860..39c4495 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -8,6 +8,7 @@ import de.torui.coflsky.commands.JsonStringCommand; import de.torui.coflsky.commands.models.ChatMessageData; import de.torui.coflsky.commands.models.FlipData; import de.torui.coflsky.commands.models.SoundData; +import de.torui.coflsky.configuration.ConfigurationManager; import de.torui.coflsky.commands.models.TimerData; import de.torui.coflsky.network.WSClient; import net.minecraft.client.Minecraft; @@ -48,6 +49,8 @@ public class WSCommandHandler { case Flip: Flip(cmd.GetAs(new TypeToken() {})); break; + case PrivacySettings: + new ConfigurationManager().UpdateConfiguration(cmd.getData()); case Countdown: StartTimer(cmd.GetAs(new TypeToken() {})); break; @@ -92,7 +95,7 @@ public class WSCommandHandler { * Starts a countdown */ private static void StartTimer(Command cmd) { - CountdownTimer.startCountdown(cmd.getData()); + de.torui.coflsky.CountdownTimer.startCountdown(cmd.getData()); } public static void Execute(String cmd, Entity sender) diff --git a/src/main/java/de/torui/coflsky/commands/CommandType.java b/src/main/java/de/torui/coflsky/commands/CommandType.java index 3d4ec05..ecece3f 100644 --- a/src/main/java/de/torui/coflsky/commands/CommandType.java +++ b/src/main/java/de/torui/coflsky/commands/CommandType.java @@ -35,6 +35,8 @@ public enum CommandType { Reset, @SerializedName("flip") Flip, + @SerializedName("privacySettings") + PrivacySettings, @SerializedName("countdown") Countdown, ; diff --git a/src/main/java/de/torui/coflsky/configuration/Configuration.java b/src/main/java/de/torui/coflsky/configuration/Configuration.java new file mode 100644 index 0000000..bdd6186 --- /dev/null +++ b/src/main/java/de/torui/coflsky/configuration/Configuration.java @@ -0,0 +1,37 @@ +package de.torui.coflsky.configuration; + +public class Configuration { + public Configuration() { + + } + + private static Configuration instance; + + public static Configuration getInstance() { + if (instance == null) + instance = new Configuration(); + return instance; + } + + public static void setInstance(Configuration config) { + instance = config; + } + + public String ChatRegex; + public boolean CollectChat; + public boolean CollectInventory; + public boolean CollectTab; + public boolean CollectScoreboard; + public boolean AllowProxy; + public boolean CollectInvClick; + public boolean CollectChatClicks; + public boolean CollectLobbyChanges; + public boolean CollectEntities; + + @Description("Wherever or not to send item descriptions for extending to the server") + public boolean ExtendDescriptions; + + @Description("Chat input starting with one of these prefixes is sent to the server") + public String[] CommandPrefixes; + +} diff --git a/src/main/java/de/torui/coflsky/configuration/ConfigurationManager.java b/src/main/java/de/torui/coflsky/configuration/ConfigurationManager.java new file mode 100644 index 0000000..7930210 --- /dev/null +++ b/src/main/java/de/torui/coflsky/configuration/ConfigurationManager.java @@ -0,0 +1,119 @@ +package de.torui.coflsky.configuration; + +import java.lang.reflect.Field; +import java.util.Arrays; + +import de.torui.coflsky.network.WSClient; +import net.minecraft.client.Minecraft; +import net.minecraft.event.HoverEvent; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.ChatStyle; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.IChatComponent; + +public class ConfigurationManager { + + public Configuration Config; + + public ConfigurationManager() { + this.Config = Configuration.getInstance(); + } + + public void UpdateConfiguration(String data) { + + Configuration newConfig = WSClient.gson.fromJson(data, Configuration.class); + + if (newConfig == null) { + System.out.println("Could not deserialize configuration " + data); + } + + + try { + if (CompareProperties(Config, newConfig)) { + Configuration.setInstance(newConfig); + } + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public boolean CompareProperties(Configuration old, Configuration newConfiguration) + throws IllegalArgumentException, IllegalAccessException { + + int updatedProperties = 0; + for (Field f : Configuration.class.getFields()) { + + switch (f.getGenericType().getTypeName()) { + + case "int": + if (f.getInt(old) != f.getInt(newConfiguration)) { + UpdatedProperty(f); + updatedProperties++; + } + break; + case "boolean": + if (f.getBoolean(old) != f.getBoolean(newConfiguration)) { + UpdatedProperty(f); + updatedProperties++; + } + break; + case "java.lang.String": + + if (f.get(old) != null && !f.get(old).equals(f.get(newConfiguration))) { + UpdatedProperty(f); + updatedProperties++; + } + break; + case "java.lang.String[]": + if (!Arrays.deepEquals((String[]) f.get(old), (String[]) f.get(newConfiguration))) { + UpdatedProperty(f); + updatedProperties++; + } + break; + + default: + throw new RuntimeException("Invalid Configuration Type " + f.getGenericType().getTypeName()); + } + + } + + return updatedProperties > 0; + } + + private IChatComponent GetNameFormatted(Field propertyName) { + Description description = propertyName.getAnnotation(Description.class); + ChatComponentText toReturn = new ChatComponentText(propertyName.getName()); + + ChatStyle style = new ChatStyle(); + style.setColor(EnumChatFormatting.WHITE); + + if (description != null) { + style.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, + new ChatComponentText(description.value()))); + } + + return toReturn.setChatStyle(style); + + } + + public void UpdatedProperty(Field propertyName) { + IChatComponent comp; + + comp = new ChatComponentText("The Configuration Setting ") + .setChatStyle(new ChatStyle().setColor(EnumChatFormatting.BLUE)) + .appendSibling(GetNameFormatted(propertyName)) + .appendSibling(new ChatComponentText(" has been updated") + .setChatStyle(new ChatStyle().setColor(EnumChatFormatting.BLUE))); + + + System.out.println("Field " + propertyName.getName() + " has no description!"); + + + Minecraft.getMinecraft().thePlayer.addChatMessage(comp); + } + +} diff --git a/src/main/java/de/torui/coflsky/configuration/Description.java b/src/main/java/de/torui/coflsky/configuration/Description.java new file mode 100644 index 0000000..4a54278 --- /dev/null +++ b/src/main/java/de/torui/coflsky/configuration/Description.java @@ -0,0 +1,20 @@ +/** + * + */ +package de.torui.coflsky.configuration; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Retention(RUNTIME) +@Target(FIELD) +/** + * @author Florian Rinke + * + */ +public @interface Description { + public String value(); +} -- cgit