forked from ChomeNS/chipmunkmod
136 lines
4.7 KiB
Java
136 lines
4.7 KiB
Java
package land.chipmunk.chipmunkmod.modules;
|
|
|
|
import com.google.common.hash.Hashing;
|
|
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
|
|
|
import net.kyori.adventure.text.Component;
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
|
import net.minecraft.client.MinecraftClient;
|
|
import net.minecraft.client.network.ClientPlayNetworkHandler;
|
|
import net.minecraft.client.network.ClientPlayerEntity;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Timer;
|
|
import java.util.TimerTask;
|
|
|
|
public class CustomChat {
|
|
private static final GsonComponentSerializer GSON = GsonComponentSerializer.gson();
|
|
|
|
private final MinecraftClient client;
|
|
|
|
public static final CustomChat INSTANCE = new CustomChat(MinecraftClient.getInstance());
|
|
|
|
public boolean enabled = true;
|
|
|
|
public String format;
|
|
|
|
private Timer timer;
|
|
|
|
private int total = 0;
|
|
|
|
public CustomChat (MinecraftClient client) {
|
|
this.client = client;
|
|
|
|
reloadFormat();
|
|
}
|
|
|
|
public void init () {
|
|
final TimerTask task = new TimerTask() {
|
|
public void run () {
|
|
tick();
|
|
}
|
|
};
|
|
|
|
resetTotal();
|
|
|
|
timer = new Timer();
|
|
timer.schedule(task, 0, 50);
|
|
}
|
|
|
|
private void tick () {
|
|
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
|
|
|
|
if (networkHandler != null) return;
|
|
|
|
resetTotal();
|
|
cleanup();
|
|
}
|
|
|
|
public void resetTotal() {
|
|
total = 0;
|
|
}
|
|
|
|
private void cleanup () {
|
|
if (timer == null) return;
|
|
|
|
timer.cancel();
|
|
timer.purge();
|
|
}
|
|
|
|
public void reloadFormat () {
|
|
this.format = GSON.serializeToTree(ChipmunkMod.CONFIG.customChat.format).toString();
|
|
}
|
|
|
|
public void chat (String message) {
|
|
final ClientPlayerEntity player = client.player;
|
|
|
|
if (player == null) return;
|
|
|
|
if (!enabled || !player.hasPermissionLevel(2) || !player.isCreative()) {
|
|
Chat.sendChatMessage(message, true);
|
|
return;
|
|
}
|
|
|
|
final String username = MinecraftClient.getInstance().getSession().getUsername();
|
|
|
|
final String sanitizedMessage = message
|
|
.replace("\\", "\\\\")
|
|
.replace("\"", "\\\"");
|
|
|
|
final LegacyComponentSerializer serializer = LegacyComponentSerializer.legacyAmpersand();
|
|
|
|
final String randomized = String.valueOf(Math.random());
|
|
|
|
final Component deserialized = serializer.deserialize(message);
|
|
final String messageWithColor = GsonComponentSerializer.gson().serialize(deserialized).replace("MESSAGE", randomized);
|
|
|
|
final String key = ChipmunkMod.CONFIG.bots.chomens.formatKey;
|
|
|
|
final String hash = key != null ?
|
|
Hashing.sha256()
|
|
.hashString(key + total, StandardCharsets.UTF_8)
|
|
.toString()
|
|
.substring(0, 8) :
|
|
"";
|
|
|
|
total++;
|
|
|
|
try {
|
|
// final MutablePlayerListEntry entry = Players.INSTANCE.getEntry(client.getNetworkHandler().getProfile().getId());
|
|
|
|
// final Component displayNameComponent = entry.displayName().asComponent();
|
|
|
|
// final String prefix = GsonComponentSerializer.gson().serialize(Component.join(JoinConfiguration.separator(Component.empty()), displayNameComponent.children().get(0)));
|
|
// final String displayName = GsonComponentSerializer.gson().serialize(Component.join(JoinConfiguration.separator(Component.empty()), displayNameComponent.children().get(1)));
|
|
|
|
// TODO: make this code not ohio code.,.,
|
|
String sanitizedFormat = format
|
|
// .replace("\"PREFIX\"", prefix)
|
|
// .replace("\"DISPLAYNAME\"", displayName)
|
|
.replace("USERNAME", username)
|
|
.replace("UUID", player.getUuidAsString())
|
|
.replace("HASH", hash)
|
|
.replace("{\"text\":\"MESSAGE\"}", messageWithColor)
|
|
.replace("\"extra\":[\"MESSAGE\"],\"color\":", "\"extra\":[" + messageWithColor + "],\"color\":")
|
|
.replace("MESSAGE", sanitizedMessage.replaceAll("&.", ""))
|
|
.replace(randomized, "MESSAGE"); // ohio ohio
|
|
|
|
CommandCore.INSTANCE.run((KaboomCheck.INSTANCE.isKaboom ? "minecraft:tellraw @a " : "tellraw @a ") + sanitizedFormat);
|
|
} catch (Exception e) {
|
|
if (client.player == null) return;
|
|
client.player.sendMessage(Component.text(e.toString()).color(NamedTextColor.RED));
|
|
}
|
|
}
|
|
}
|