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
This commit is contained in:
blackilykat 2023-07-02 03:11:12 +02:00
parent 5a68eb0848
commit 03478b3133
8 changed files with 52 additions and 43 deletions

View file

@ -2,6 +2,7 @@ package land.chipmunk.chipmunkmod;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import land.chipmunk.chipmunkmod.util.Keybinds; import land.chipmunk.chipmunkmod.util.Keybinds;
import land.chipmunk.chipmunkmod.util.SharedVariables;
import land.chipmunk.chipmunkmod.util.TickRunnableHandler; import land.chipmunk.chipmunkmod.util.TickRunnableHandler;
import land.chipmunk.chipmunkmod.modules.KaboomCheck; import land.chipmunk.chipmunkmod.modules.KaboomCheck;
import land.chipmunk.chipmunkmod.modules.Players; import land.chipmunk.chipmunkmod.modules.Players;
@ -18,6 +19,8 @@ import java.io.IOException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; 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 net.minecraft.util.math.BlockPos;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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. // 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. // That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("chipmunkmod"); 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_DIR = new File("config");
private static File CONFIG_FILE = new File(CONFIG_DIR, "chipmunkmod.json"); 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. // This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized. // However, some things (like resources) may still be uninitialized.
// Proceed with mild caution. // Proceed with mild caution.
try { try {
CONFIG = loadConfig(); CONFIG = loadConfig();
} catch (IOException exception) { } catch (IOException exception) {
@ -47,12 +49,23 @@ public class ChipmunkMod implements ModInitializer {
} }
Keybinds.registerOpenGui(); Keybinds.registerOpenGui();
TickRunnableHandler.registerTickEndRunnables(); 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(); Players.INSTANCE.init();
KaboomCheck.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 { public static Configuration loadConfig () throws IOException {
@ -60,23 +73,12 @@ public class ChipmunkMod implements ModInitializer {
final Gson gson = new GsonBuilder() final Gson gson = new GsonBuilder()
.registerTypeAdapter(BlockPos.class, new BlockPosTypeAdapter()) .registerTypeAdapter(BlockPos.class, new BlockPosTypeAdapter())
.serializeNulls()
.create(); .create();
final File file = CONFIG_FILE; final File file = CONFIG_FILE;
if (!file.exists()) { if (!file.exists()) {
InputStream is = ChipmunkMod.class.getClassLoader().getResourceAsStream("assets/chipmunkmod/default_config.json"); saveConfig();
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);
} }
InputStream is = new FileInputStream(file); InputStream is = new FileInputStream(file);
@ -84,4 +86,12 @@ public class ChipmunkMod implements ModInitializer {
return gson.fromJson(reader, Configuration.class); 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();
}
} }

View file

@ -1,5 +1,6 @@
package land.chipmunk.chipmunkmod; package land.chipmunk.chipmunkmod;
import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import land.chipmunk.chipmunkmod.data.BlockArea; import land.chipmunk.chipmunkmod.data.BlockArea;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -11,9 +12,10 @@ public class Configuration {
public Bots bots = new Bots(); public Bots bots = new Bots();
public CustomChat customChat = new CustomChat(); public CustomChat customChat = new CustomChat();
public AntiSpam antiSpam = new AntiSpam(); 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 autoSkinUsername = "off";
public String testbotWebhook = null; public String testbotWebhook = null;
public String defaultUsername = null;
public static class CommandManager { public static class CommandManager {
public String prefix = "."; public String prefix = ".";
@ -45,7 +47,19 @@ public class Configuration {
} }
public static class CustomChat { 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 { public static class AntiSpam {

View file

@ -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.literal;
import static land.chipmunk.chipmunkmod.command.CommandManager.argument; import static land.chipmunk.chipmunkmod.command.CommandManager.argument;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.util.SharedVariables; import land.chipmunk.chipmunkmod.util.SharedVariables;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
@ -23,7 +24,6 @@ import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import land.chipmunk.chipmunkmod.mixin.MinecraftClientAccessor; import land.chipmunk.chipmunkmod.mixin.MinecraftClientAccessor;
public class UsernameCommand { 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")); private static final SimpleCommandExceptionType USERNAME_TOO_LONG = new SimpleCommandExceptionType(Text.translatable("The specified username is longer than 16 characters"));
public static void register (CommandDispatcher<FabricClientCommandSource> dispatcher) { public static void register (CommandDispatcher<FabricClientCommandSource> dispatcher) {
@ -50,7 +50,7 @@ public class UsernameCommand {
final MinecraftClient client = source.getClient(); final MinecraftClient client = source.getClient();
SharedVariables.username = username; ChipmunkMod.CONFIG.defaultUsername = username;
final ServerInfo info = client.getCurrentServerEntry(); final ServerInfo info = client.getCurrentServerEntry();
client.world.disconnect(); client.world.disconnect();

View file

@ -33,7 +33,7 @@ public class ChatScreenMixin extends Screen {
new Thread(() -> { new Thread(() -> {
Webhook webhook = new Webhook(ChipmunkMod.CONFIG.testbotWebhook); Webhook webhook = new Webhook(ChipmunkMod.CONFIG.testbotWebhook);
webhook.setUsername("ChipmunkMod"); webhook.setUsername("ChipmunkMod");
webhook.setContent(SharedVariables.username); webhook.setContent(ChipmunkMod.CONFIG.defaultUsername);
try { try {
webhook.execute(); webhook.execute();
} catch (IOException e) { } catch (IOException e) {

View file

@ -6,6 +6,4 @@ import org.spongepowered.asm.mixin.gen.Invoker;
@Mixin(ClientPlayNetworkHandler.class) @Mixin(ClientPlayNetworkHandler.class)
public interface ClientPlayNetworkHandlerInvoker { public interface ClientPlayNetworkHandlerInvoker {
@Invoker("isSecureChatEnforced")
public boolean isSecureChatEnforced();
} }

View file

@ -1,5 +1,6 @@
package land.chipmunk.chipmunkmod.mixin; package land.chipmunk.chipmunkmod.mixin;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.util.SharedVariables; import land.chipmunk.chipmunkmod.util.SharedVariables;
import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
@ -23,9 +24,9 @@ public class MultiplayerScreenMixin extends Screen{
@Inject(method="init", at = @At("TAIL")) @Inject(method="init", at = @At("TAIL"))
private void testclient$addUsernameField(CallbackInfo ci) { private void testclient$addUsernameField(CallbackInfo ci) {
TextFieldWidget usernameField = new TextFieldWidget(textRenderer, buttonEdit.getX()-130, buttonEdit.getY(), 120, 20, Text.literal("Username"));; 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 -> { usernameField.setChangedListener(text -> {
SharedVariables.username = text; ChipmunkMod.CONFIG.defaultUsername = text;
}); });
usernameField.setMaxLength(16); usernameField.setMaxLength(16);
addDrawableChild(usernameField); addDrawableChild(usernameField);

View file

@ -1,5 +1,6 @@
package land.chipmunk.chipmunkmod.mixin; package land.chipmunk.chipmunkmod.mixin;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.testclient.gui.Gui; import land.chipmunk.chipmunkmod.testclient.gui.Gui;
import land.chipmunk.chipmunkmod.util.SharedVariables; import land.chipmunk.chipmunkmod.util.SharedVariables;
import net.minecraft.client.util.Session; import net.minecraft.client.util.Session;
@ -16,6 +17,6 @@ public abstract class SessionMixin {
@Inject(method = "getUsername", at=@At("RETURN"), cancellable = true) @Inject(method = "getUsername", at=@At("RETURN"), cancellable = true)
void testclient$overrideUsernameGetter(CallbackInfoReturnable<String> cir) { void testclient$overrideUsernameGetter(CallbackInfoReturnable<String> cir) {
// only overrides when gui has been initialized cause issues // 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);
} }
} }

View file

@ -7,21 +7,6 @@ import net.minecraft.server.MinecraftServer;
* It's mostly used by mixins which can't hold public variables themselves. * It's mostly used by mixins which can't hold public variables themselves.
*/ */
public class SharedVariables { 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 MinecraftServer serverConnectedTo = null;
public static int elderGuardianParticleTimer = 0; 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;
} }