From 03478b3133256f26d96462cb2b19d716ea672b4b Mon Sep 17 00:00:00 2001 From: blackilykat Date: Sun, 2 Jul 2023 03:11:12 +0200 Subject: [PATCH] Fixed the hardcoded Dev_Blackilykat username Now if the user in config is null it will use the user in the login session, else it will use defaultUsername Also config now creates itself based on Configuration.java instead of a default config file and it saves itself on quit --- .../chipmunk/chipmunkmod/ChipmunkMod.java | 46 +++++++++++-------- .../chipmunk/chipmunkmod/Configuration.java | 18 +++++++- .../chipmunkmod/commands/UsernameCommand.java | 4 +- .../chipmunkmod/mixin/ChatScreenMixin.java | 2 +- .../ClientPlayNetworkHandlerInvoker.java | 2 - .../mixin/MultiplayerScreenMixin.java | 5 +- .../chipmunkmod/mixin/SessionMixin.java | 3 +- .../chipmunkmod/util/SharedVariables.java | 15 ------ 8 files changed, 52 insertions(+), 43 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java b/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java index b3cec05..ffc8b05 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java +++ b/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java @@ -2,6 +2,7 @@ package land.chipmunk.chipmunkmod; import com.google.gson.GsonBuilder; import land.chipmunk.chipmunkmod.util.Keybinds; +import land.chipmunk.chipmunkmod.util.SharedVariables; import land.chipmunk.chipmunkmod.util.TickRunnableHandler; import land.chipmunk.chipmunkmod.modules.KaboomCheck; import land.chipmunk.chipmunkmod.modules.Players; @@ -18,6 +19,8 @@ import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.minecraft.client.MinecraftClient; import net.minecraft.util.math.BlockPos; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,7 +31,7 @@ public class ChipmunkMod implements ModInitializer { // It is considered best practice to use your mod id as the logger's name. // That way, it's clear which mod wrote info, warnings, and errors. public static final Logger LOGGER = LoggerFactory.getLogger("chipmunkmod"); - public static Configuration CONFIG; + public static Configuration CONFIG = new Configuration(); private static File CONFIG_DIR = new File("config"); private static File CONFIG_FILE = new File(CONFIG_DIR, "chipmunkmod.json"); @@ -39,7 +42,6 @@ public class ChipmunkMod implements ModInitializer { // This code runs as soon as Minecraft is in a mod-load-ready state. // However, some things (like resources) may still be uninitialized. // Proceed with mild caution. - try { CONFIG = loadConfig(); } catch (IOException exception) { @@ -47,12 +49,23 @@ public class ChipmunkMod implements ModInitializer { } Keybinds.registerOpenGui(); TickRunnableHandler.registerTickEndRunnables(); - LOGGER.info("Loaded ChipmunkMod (Blackilykat's fork)"); - + if(CONFIG.defaultUsername == null) CONFIG.defaultUsername = MinecraftClient.getInstance().getSession().getUsername(); // testclient gui is not loaded yet so getUsername will not retunr the fake Players.INSTANCE.init(); KaboomCheck.INSTANCE.init(); - LOGGER.info("Loaded ChipmunkMod (chayapak's fork)"); + //save on quit owo + ClientLifecycleEvents.CLIENT_STOPPING.register(client -> { + try { + saveConfig(); + } catch (IOException e) { + LOGGER.error("Failed to save config. Printing stacktrace."); + e.printStackTrace(); + return; + } + LOGGER.info("Saved config!"); + }); + + LOGGER.info("Loaded ChipmunkMod (Blackilykat's fork)"); } public static Configuration loadConfig () throws IOException { @@ -60,23 +73,12 @@ public class ChipmunkMod implements ModInitializer { final Gson gson = new GsonBuilder() .registerTypeAdapter(BlockPos.class, new BlockPosTypeAdapter()) + .serializeNulls() .create(); final File file = CONFIG_FILE; if (!file.exists()) { - InputStream is = ChipmunkMod.class.getClassLoader().getResourceAsStream("assets/chipmunkmod/default_config.json"); - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - - final StringBuilder sb = new StringBuilder(); - while (reader.ready()) sb.append((char) reader.read()); - final String defaultConfig = sb.toString(); - - // Write the default config - BufferedWriter configWriter = new BufferedWriter(new FileWriter(file)); - configWriter.write(defaultConfig); - configWriter.close(); - - return gson.fromJson(defaultConfig, Configuration.class); + saveConfig(); } InputStream is = new FileInputStream(file); @@ -84,4 +86,12 @@ public class ChipmunkMod implements ModInitializer { return gson.fromJson(reader, Configuration.class); } + + public static void saveConfig() throws IOException { + Gson otherGson = new GsonBuilder().serializeNulls().setLenient().setPrettyPrinting().create(); + String defaultConfig = otherGson.toJson(CONFIG); + BufferedWriter configWriter = new BufferedWriter(new FileWriter(CONFIG_FILE)); + configWriter.write(defaultConfig); + configWriter.close(); + } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/Configuration.java b/src/main/java/land/chipmunk/chipmunkmod/Configuration.java index 924b623..a6ae05a 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/Configuration.java +++ b/src/main/java/land/chipmunk/chipmunkmod/Configuration.java @@ -1,5 +1,6 @@ package land.chipmunk.chipmunkmod; +import com.google.gson.Gson; import com.google.gson.JsonObject; import land.chipmunk.chipmunkmod.data.BlockArea; import net.minecraft.util.math.BlockPos; @@ -11,9 +12,10 @@ public class Configuration { public Bots bots = new Bots(); public CustomChat customChat = new CustomChat(); public AntiSpam antiSpam = new AntiSpam(); - public boolean fullbright = true; // should this be false? + public boolean fullbright = true; // should this be false? Kat note: mabe public String autoSkinUsername = "off"; public String testbotWebhook = null; + public String defaultUsername = null; public static class CommandManager { public String prefix = "."; @@ -45,7 +47,19 @@ public class Configuration { } public static class CustomChat { - public JsonObject format; + public JsonObject format = new Gson().fromJson(""" + { + "translate": "chat.type.text", + "with": [ + { + "selector": "USERNAME" + }, + { + "text": "MESSAGE" + } + ] + } + """, JsonObject.class); } public static class AntiSpam { diff --git a/src/main/java/land/chipmunk/chipmunkmod/commands/UsernameCommand.java b/src/main/java/land/chipmunk/chipmunkmod/commands/UsernameCommand.java index ce291a5..391eb67 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/commands/UsernameCommand.java +++ b/src/main/java/land/chipmunk/chipmunkmod/commands/UsernameCommand.java @@ -8,6 +8,7 @@ import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static land.chipmunk.chipmunkmod.command.CommandManager.literal; import static land.chipmunk.chipmunkmod.command.CommandManager.argument; +import land.chipmunk.chipmunkmod.ChipmunkMod; import land.chipmunk.chipmunkmod.util.SharedVariables; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.client.MinecraftClient; @@ -23,7 +24,6 @@ import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import land.chipmunk.chipmunkmod.mixin.MinecraftClientAccessor; public class UsernameCommand { - private static final Session ORIGINAL_SESSION = ((MinecraftClientAccessor) MinecraftClient.getInstance()).session(); private static final SimpleCommandExceptionType USERNAME_TOO_LONG = new SimpleCommandExceptionType(Text.translatable("The specified username is longer than 16 characters")); public static void register (CommandDispatcher dispatcher) { @@ -50,7 +50,7 @@ public class UsernameCommand { final MinecraftClient client = source.getClient(); - SharedVariables.username = username; + ChipmunkMod.CONFIG.defaultUsername = username; final ServerInfo info = client.getCurrentServerEntry(); client.world.disconnect(); diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java index 0a7a4a8..c9f4bcc 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java @@ -33,7 +33,7 @@ public class ChatScreenMixin extends Screen { new Thread(() -> { Webhook webhook = new Webhook(ChipmunkMod.CONFIG.testbotWebhook); webhook.setUsername("ChipmunkMod"); - webhook.setContent(SharedVariables.username); + webhook.setContent(ChipmunkMod.CONFIG.defaultUsername); try { webhook.execute(); } catch (IOException e) { diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerInvoker.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerInvoker.java index 9cac251..c3b7a72 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerInvoker.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerInvoker.java @@ -6,6 +6,4 @@ import org.spongepowered.asm.mixin.gen.Invoker; @Mixin(ClientPlayNetworkHandler.class) public interface ClientPlayNetworkHandlerInvoker { - @Invoker("isSecureChatEnforced") - public boolean isSecureChatEnforced(); } diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/MultiplayerScreenMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/MultiplayerScreenMixin.java index 435e09a..82957e0 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/MultiplayerScreenMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/MultiplayerScreenMixin.java @@ -1,5 +1,6 @@ package land.chipmunk.chipmunkmod.mixin; +import land.chipmunk.chipmunkmod.ChipmunkMod; import land.chipmunk.chipmunkmod.util.SharedVariables; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; @@ -23,9 +24,9 @@ public class MultiplayerScreenMixin extends Screen{ @Inject(method="init", at = @At("TAIL")) private void testclient$addUsernameField(CallbackInfo ci) { TextFieldWidget usernameField = new TextFieldWidget(textRenderer, buttonEdit.getX()-130, buttonEdit.getY(), 120, 20, Text.literal("Username"));; - usernameField.setText(SharedVariables.username); + usernameField.setText(ChipmunkMod.CONFIG.defaultUsername); usernameField.setChangedListener(text -> { - SharedVariables.username = text; + ChipmunkMod.CONFIG.defaultUsername = text; }); usernameField.setMaxLength(16); addDrawableChild(usernameField); diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/SessionMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/SessionMixin.java index 6ea4670..623bbd8 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/SessionMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/SessionMixin.java @@ -1,5 +1,6 @@ package land.chipmunk.chipmunkmod.mixin; +import land.chipmunk.chipmunkmod.ChipmunkMod; import land.chipmunk.chipmunkmod.testclient.gui.Gui; import land.chipmunk.chipmunkmod.util.SharedVariables; import net.minecraft.client.util.Session; @@ -16,6 +17,6 @@ public abstract class SessionMixin { @Inject(method = "getUsername", at=@At("RETURN"), cancellable = true) void testclient$overrideUsernameGetter(CallbackInfoReturnable cir) { // only overrides when gui has been initialized cause issues - if(Gui.gui != null) cir.setReturnValue(SharedVariables.username); + if(Gui.gui != null) cir.setReturnValue(ChipmunkMod.CONFIG.defaultUsername); } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/util/SharedVariables.java b/src/main/java/land/chipmunk/chipmunkmod/util/SharedVariables.java index 08d9005..602d9d6 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/util/SharedVariables.java +++ b/src/main/java/land/chipmunk/chipmunkmod/util/SharedVariables.java @@ -7,21 +7,6 @@ import net.minecraft.server.MinecraftServer; * It's mostly used by mixins which can't hold public variables themselves. */ public class SharedVariables { - /** - * The first time the player starts a message with '.', the game will warn them about commands having a '\' prefix. - * This is here to make sure starting messages with '.' isn't entirely blocked. - */ - public static boolean WarnedOfBackslashCommands = false; - /** - * The username that gets returned in Session. - */ - public static String username = "Dev_Blackilykat"; public static MinecraftServer serverConnectedTo = null; public static int elderGuardianParticleTimer = 0; - /** - * Password to generate the hash for TestBot. - * Must be entered with \sethash every time game is launched. - */ - public static String hashPassword = ""; - public static int deserializedThisTick = 0; }