fixed antichatspam and made module memory work

This commit is contained in:
blackilykat 2023-09-07 16:39:08 +02:00
parent bc35032c02
commit a794ebd7d3
11 changed files with 141 additions and 22 deletions

View file

@ -60,6 +60,7 @@ public class ChipmunkMod implements ModInitializer {
//save on quit owo
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> {
ModuleMemory.save();
try {
saveConfig();
} catch (IOException e) {

View file

@ -5,6 +5,8 @@ import com.google.gson.JsonObject;
import land.chipmunk.chipmunkmod.data.BlockArea;
import net.minecraft.util.math.BlockPos;
import java.util.HashMap;
public class Configuration {
public CommandManager commands = new CommandManager();
@ -16,6 +18,7 @@ public class Configuration {
public String autoSkinUsername = "off";
public String testbotWebhook = null;
public String defaultUsername = null;
public Memory memory = new Memory();
public static class CommandManager {
public String prefix = ".";
@ -73,7 +76,12 @@ public class Configuration {
public static class AntiSpam {
public int matchingMessagesToBeSpam = 20;
public int messageTimeInTicks = 100;
public int blockedPatternDurationInTicks = 0; //TODO: remove
public int minimumLevenshteinDistanceToBeSpam = 20;
}
public static class Memory {
public HashMap<String, Boolean> categories = new HashMap<>();
public HashMap<String, Boolean> modules = new HashMap<>();
public HashMap<String, String> options = new HashMap<>(); // will convert all values to their type according to the method specified in the option type thing
}
}

View file

@ -1,6 +1,7 @@
package land.chipmunk.chipmunkmod.memory;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.Configuration;
import land.chipmunk.chipmunkmod.testclient.gui.Gui;
import land.chipmunk.chipmunkmod.testclient.gui.components.Category;
import land.chipmunk.chipmunkmod.testclient.gui.components.Module;
@ -11,17 +12,20 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import static land.chipmunk.chipmunkmod.ChipmunkMod.LOGGER;
// i have realised this can also be used to save profiles
// like it would be so simple to implement that feature
// not 1.1.0 tho it's taking too long
// whyt he fuck is this here lmao i have a working config jsut use that ??1'11'!?
public class ModuleMemory {
private static final File MEMORY_FILE = new File(ChipmunkMod.CONFIG_DIR, "chipmunkmodmemory.data");
public ArrayList<Category> categories = new ArrayList<>();
public void load() throws IOException {
public void loadButOld() throws IOException {
if(!MEMORY_FILE.exists()) {
saveDefaults();
return;
@ -246,9 +250,6 @@ public class ModuleMemory {
public void saveDefaults() {
}
public void save() {
}
public <T> void setRealOptionValue(land.chipmunk.chipmunkmod.testclient.gui.components.Option<T> real, Option<?> fake) {
@ -312,4 +313,87 @@ public class ModuleMemory {
}
public Class<T> getType() {return (Class<T>) value.getClass();} // ignore warning cause intellij has the stupid
}
public static void load() {
for (Map.Entry<String, Boolean> categoryInMemory : ChipmunkMod.CONFIG.memory.categories.entrySet()) {
for (land.chipmunk.chipmunkmod.testclient.gui.components.Category categoryInGui : Gui.categoryList) {
if(categoryInMemory.getKey().equals(categoryInGui.getMessage().getString())) {
categoryInGui.isExtended = categoryInMemory.getValue();
}
}
}
for (Map.Entry<String, Boolean> moduleInMemory : ChipmunkMod.CONFIG.memory.modules.entrySet()) {
String[] split = moduleInMemory.getKey().split("\\.");
String category = split[0];
String module = split[1];
for (land.chipmunk.chipmunkmod.testclient.gui.components.Category categoryInGui : Gui.categoryList) {
if(!categoryInGui.getMessage().getString().equals(category)) continue;
for (land.chipmunk.chipmunkmod.testclient.gui.components.Module moduleInGui : categoryInGui.moduleList) {
if(module.equals(moduleInGui.getMessage().getString())) {
moduleInGui.isEnabled = moduleInMemory.getValue();
}
}
}
}
for (Map.Entry<String, Boolean> moduleInMemory : ChipmunkMod.CONFIG.memory.modules.entrySet()) {
String[] split = moduleInMemory.getKey().split("\\.");
String category = split[0];
String module = split[1];
for (land.chipmunk.chipmunkmod.testclient.gui.components.Category categoryInGui : Gui.categoryList) {
if(!categoryInGui.getMessage().getString().equals(category)) continue;
for (land.chipmunk.chipmunkmod.testclient.gui.components.Module moduleInGui : categoryInGui.moduleList) {
if(module.equals(moduleInGui.getMessage().getString())) {
moduleInGui.isEnabled = moduleInMemory.getValue();
}
}
}
}
for (Map.Entry<String, String> optionInMemory : ChipmunkMod.CONFIG.memory.options.entrySet()) {
String[] split = optionInMemory.getKey().split("\\.");
String category = split[0];
String module = split[1];
String option = split[2];
for (land.chipmunk.chipmunkmod.testclient.gui.components.Category categoryInGui : Gui.categoryList) {
if(!categoryInGui.getMessage().getString().equals(category)) continue;
for (land.chipmunk.chipmunkmod.testclient.gui.components.Module moduleInGui : categoryInGui.moduleList) {
if(!module.equals(moduleInGui.getMessage().getString())) continue;
for (land.chipmunk.chipmunkmod.testclient.gui.components.Option<?> optionInGui : moduleInGui.optionList) {
if(option.equals(optionInGui.name)) {
optionInGui.setValueFromString(optionInMemory.getValue());
}
}
}
}
}
LOGGER.info("Loaded module memory!");
}
public static void save() {
for (land.chipmunk.chipmunkmod.testclient.gui.components.Category categoryInGui : Gui.categoryList) {
String categoryKey = categoryInGui.getMessage().getString();
ChipmunkMod.CONFIG.memory.categories.put(categoryKey, categoryInGui.isExtended);
for (land.chipmunk.chipmunkmod.testclient.gui.components.Module moduleInGui : categoryInGui.moduleList) {
String moduleKey = categoryKey + "." + moduleInGui.getMessage().getString();
ChipmunkMod.CONFIG.memory.modules.put(moduleKey, moduleInGui.isEnabled);
for (land.chipmunk.chipmunkmod.testclient.gui.components.Option<?> optionInGui : moduleInGui.optionList) {
String key = moduleKey + "." + optionInGui.name;
ChipmunkMod.CONFIG.memory.options.put(key, optionInGui.getValueAsString());
}
}
}
}
}

View file

@ -1,5 +1,6 @@
package land.chipmunk.chipmunkmod.mixin;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.listeners.Listener;
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
import land.chipmunk.chipmunkmod.modules.RainbowName;
@ -52,11 +53,13 @@ public abstract class ChatHudMixin {
@Inject(at = @At("HEAD"), method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V", cancellable = true)
public void chipmunkmod$generalAddMessageMixin(Text message, MessageSignatureData signature, int ticks, MessageIndicator indicator, boolean refresh, CallbackInfo ci) {
// ChipmunkMod.LOGGER.info("gex");
if(AntiChatSpamModule.instance.isEnabled && message.equals(AntiChatSpamModule.latestPassedThroughMessage)) {
AntiChatSpamModule.latestPassedThroughMessage = Text.empty();
logChatMessage(message, indicator);
return;
}
// ChipmunkMod.LOGGER.info("gex2");
try {
if (RainbowName.INSTANCE.enabled) {
if (message.getString().contains("Your nickname is now ") || message.getString().contains("Nickname changed.")) {
@ -78,6 +81,7 @@ public abstract class ChatHudMixin {
// for (AntiChatSpamModule.BlockedPattern pattern : AntiChatSpamModule.instance.patterns) {
// if(pattern.pattern().matcher(message.getString()).matches()) ci.cancel();
// }
// ChipmunkMod.LOGGER.info("gex3");
if(AntiChatSpamModule.instance.isEnabled) {
Executor.antiChatSpamService.submit(() -> {
try {

View file

@ -1,6 +1,7 @@
package land.chipmunk.chipmunkmod.mixin;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.memory.ModuleMemory;
import land.chipmunk.chipmunkmod.testclient.gui.Gui;
import net.minecraft.client.gui.screen.SplashTextRenderer;
import net.minecraft.client.gui.screen.TitleScreen;
@ -26,13 +27,7 @@ public class TitleScreenMixin {
Gui.initAutoRefresher();
Gui.addComponents();
Gui.gui = new Gui();
try {
ChipmunkMod.MEMORY.load();
ChipmunkMod.MEMORY.apply();
} catch (IOException e) {
ChipmunkMod.LOGGER.error("Could not load memory due to an IOException.");
e.printStackTrace();
}
ModuleMemory.load();
}
}
}

View file

@ -15,5 +15,7 @@ public abstract class Option<ValueType> {
public ModuleMemory.Option<ValueType> toMemoryOption() {return new ModuleMemory.Option<>(name, optionValue);}
public Class<ValueType> getType() {return (Class<ValueType>) optionValue.getClass();} // ignore this error intellij has the stupid
public abstract void setValueFromString(String string);
public abstract String getValueAsString();
// these two should match perfectly, meaning that setValueFromString(getValueAsString()); should do nothing
}

View file

@ -53,4 +53,11 @@ public class DoubleSliderOption extends Option<Double> {
long tmp = Math.round(value);
return (double) tmp / factor;
}
public void setValueFromString(String string) {
optionValue = Double.valueOf(string);
}
public String getValueAsString() {
return Double.toString(optionValue);
}
}

View file

@ -35,4 +35,14 @@ public class IntSliderOption extends Option<Integer> {
this.minValue = minValue;
this.maxValue = maxValue;
}
@Override
public void setValueFromString(String string) {
optionValue = Integer.valueOf(string);
}
@Override
public String getValueAsString() {
return Integer.toString(optionValue);
}
}

View file

@ -15,5 +15,15 @@ public class StringOption extends Option<String> {
});
widget = textFieldWidget;
}
@Override
public void setValueFromString(String string) {
optionValue = string; // pro conversion
}
@Override
public String getValueAsString() {
return optionValue;
}
}

View file

@ -23,6 +23,7 @@ public class AntiChatSpamModule extends Module {
super("Anti chat spam");
isEnabled = true;
ClientTickEvents.END_CLIENT_TICK.register(client -> {
// ChipmunkMod.LOGGER.info("gex");
for (int i = 0; i < messages.size(); i++) {
if(messages.get(i) == null) continue;
Debug.debug("Ticked message: " + messages.get(i).content, debugTickedCaller);
@ -46,7 +47,7 @@ public class AntiChatSpamModule extends Module {
for (int i = 0; i < chatMessages.size(); i++) {
ChatMessage message = chatMessages.get(i);
if(message == null) continue;
int distance = ld.apply(this.getContent(), message.getContent());
int distance = ld.apply(content, message.content);
Debug.debug("Distance: " + distance, debugLevenshteinDistanceCaller);
if (distance <= ChipmunkMod.CONFIG.antiSpam.minimumLevenshteinDistanceToBeSpam) similarMessages++;
// Pattern pattern = getPattern(new ComparableString(this.content()), new ComparableString(message.content()));

View file

@ -4,6 +4,7 @@
"package": "land.chipmunk.chipmunkmod.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"ChatHudMixin",
"ChatInputSuggestorMixin",
"ChatScreenMixin",
"ClientConnectionAccessor",
@ -12,27 +13,23 @@
"ClientPlayerEntityMixin",
"ClientPlayNetworkHandlerAccessor",
"ClientPlayNetworkHandlerMixin",
"MinecraftClientAccessor",
"DecoderHandlerMixin",
"StringHelperMixin",
"NbtIoMixin",
"DecoratedPotBlockEntitySherdsMixin",
"ElderGuardianAppearanceParticleMixin",
"FontStorageMixin",
"WorldRendererMixin",
"IdentifierMixin",
"KeyboardInputMixin",
"KeyboardMixin",
"MinecraftClientAccessor",
"MultiplayerScreenMixin",
"NbtIoMixin",
"PlayerListEntryAccessor",
"SessionMixin",
"SharedConstantsMixin",
"StringHelperMixin",
"TextMixin",
"ClientConnectionInvoker",
"TitleScreenMixin",
"PlayerListEntryAccessor",
"SharedConstantsMixin"
"WorldRendererMixin"
],
"injectors": {
"defaultRequire": 1