Add chat filter

This commit is contained in:
Lee 2023-03-06 20:31:07 +00:00
parent f5c7e8532a
commit ef3bf5f31e
9 changed files with 62 additions and 11 deletions

@ -1,6 +1,7 @@
package cc.fascinated.wildaddons.addon;
import cc.fascinated.wildaddons.addon.impl.chat.AutoWelcomerAddon;
import cc.fascinated.wildaddons.addon.impl.chat.MessageFilterAddon;
import cc.fascinated.wildaddons.addon.impl.ui.OverlayAddon;
import java.util.HashSet;
@ -16,6 +17,7 @@ public class AddonManager {
public AddonManager() {
registerAddon(new AutoWelcomerAddon());
registerAddon(new OverlayAddon());
registerAddon(new MessageFilterAddon());
}
/**
@ -26,4 +28,13 @@ public class AddonManager {
public void registerAddon(Addon addon) {
ADDONS.add(addon);
}
public static boolean isEnabled(Class<?> clazz) {
for (Addon addon : ADDONS) {
if (addon.getClass() == clazz) {
return addon.isEnabled();
}
}
return false;
}
}

@ -5,6 +5,7 @@ import cc.fascinated.wildaddons.addon.Addon;
import cc.fascinated.wildaddons.listener.PlayerListener;
import cc.fascinated.wildaddons.statistic.Statistic;
import cc.fascinated.wildaddons.utils.PlayerUtils;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.concurrent.ThreadLocalRandom;
@ -18,7 +19,7 @@ public class AutoWelcomerAddon extends Addon {
}
@Override
public void onChat(String message, String messageStripped) {
public void onChat(String message, String messageStripped, CallbackInfo ci) {
if (!this.isEnabled()) { // This addon is disabled
return;
}

@ -0,0 +1,42 @@
package cc.fascinated.wildaddons.addon.impl.chat;
import cc.fascinated.wildaddons.addon.Addon;
import cc.fascinated.wildaddons.statistic.Statistic;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Arrays;
import java.util.List;
public class MessageFilterAddon extends Addon {
private final List<String> toRemove = Arrays.asList(
"⛃ Blessing",
"✌ OP CRATE",
"✌ VOTE",
"RANKS",
//"has just won a",
"+ KeyFinder",
"ALERT",
"WILDPASS",
"VALUE",
"----------------------------------------------------"
);
public MessageFilterAddon() {
super("Chat Filter", "Removes all the junk messages from chat.", Category.CHAT);
}
@Override
public void onChat(String message, String messageStripped, CallbackInfo ci) {
if (message.equals("")) {
ci.cancel();
return;
}
for (String check : toRemove) {
if (messageStripped.startsWith(check)) {
ci.cancel();
Statistic.FILTERED_MESSAGES.increment();
}
}
}
}

@ -3,12 +3,9 @@ package cc.fascinated.wildaddons.addon.impl.ui;
import cc.fascinated.wildaddons.Main;
import cc.fascinated.wildaddons.addon.Addon;
import cc.fascinated.wildaddons.tpsmonitor.TpsMonitor;
import cc.fascinated.wildaddons.utils.NumberUtils;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.network.Packet;
import net.minecraft.network.packet.s2c.play.WorldTimeUpdateS2CPacket;
public class OverlayAddon extends Addon {

@ -1,6 +1,7 @@
package cc.fascinated.wildaddons.event;
import net.minecraft.network.Packet;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
public interface EventListener {
@ -12,7 +13,7 @@ public interface EventListener {
* @param message the message
* @param messageStripped the message without colors
*/
default void onChat(String message, String messageStripped) {}
default void onChat(String message, String messageStripped, CallbackInfo ci) {}
/**
* Gets called when the client receives a packet.

@ -16,7 +16,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ChatHud.class)
public class ChatHudMixin {
@Inject(method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V", at = @At("HEAD"))
@Inject(method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/ChatHudLine$Visible;<init>(ILnet/minecraft/text/OrderedText;Lnet/minecraft/client/gui/hud/MessageIndicator;Z)V"), cancellable = true)
public void onChat(Text text, MessageSignatureData signature, int ticks, MessageIndicator indicator, boolean refresh, CallbackInfo ci) {
if (!PlayerUtils.isOnWild()) { // Ignore if the player isn't on Wild
return;
@ -25,7 +25,7 @@ public class ChatHudMixin {
String stripped = TextUtils.stripColors(message); // Strip the text removing colors
for (EventListener listener : EventManager.getListeners()) {
listener.onChat(message, stripped);
listener.onChat(message, stripped, ci);
}
}
}

@ -5,7 +5,6 @@ import cc.fascinated.wildaddons.event.EventListener;
import cc.fascinated.wildaddons.event.EventManager;
import cc.fascinated.wildaddons.utils.PlayerUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;

@ -8,7 +8,8 @@ import java.util.HashMap;
@Getter @RequiredArgsConstructor
public enum Statistic {
PLAYERS_WELCOMED("players-welcomed");
PLAYERS_WELCOMED("players-welcomed"),
FILTERED_MESSAGES("filtered-messages");
/**
* The in memory storage for all statistics

@ -3,7 +3,6 @@ package cc.fascinated.wildaddons.tpsmonitor;
import cc.fascinated.wildaddons.event.EventListener;
import cc.fascinated.wildaddons.utils.NumberUtils;
import com.google.common.collect.EvictingQueue;
import lombok.Getter;
import net.minecraft.network.Packet;
import net.minecraft.network.packet.s2c.play.WorldTimeUpdateS2CPacket;
@ -50,7 +49,7 @@ public class TpsMonitor implements EventListener {
* @return the formatted tps
*/
public static String getFormattedTPS() {
float tps = calculateServerTPS();
float tps = Math.max(calculateServerTPS(), 0);
return tps > 20.00 ? "20.00*" : NumberUtils.format(tps);
}