Small refactors #7
7 changed files with 32 additions and 109 deletions
build.gradle
src/main/java/me/chayapak1/chomens_bot
|
@ -110,6 +110,10 @@ application {
|
|||
mainClass = 'me.chayapak1.chomens_bot.Main'
|
||||
}
|
||||
|
||||
run {
|
||||
standardInput = System.in
|
||||
}
|
||||
|
||||
tasks.withType(JavaCompile).configureEach {
|
||||
options.encoding = 'UTF-8'
|
||||
}
|
||||
|
|
|
@ -6,10 +6,11 @@ import me.chayapak1.chomens_bot.util.RandomStringUtilities;
|
|||
import net.kyori.adventure.key.Key;
|
||||
import org.geysermc.mcprotocollib.auth.GameProfile;
|
||||
import org.geysermc.mcprotocollib.network.BuiltinFlags;
|
||||
import org.geysermc.mcprotocollib.network.ClientSession;
|
||||
import org.geysermc.mcprotocollib.network.Session;
|
||||
import org.geysermc.mcprotocollib.network.event.session.*;
|
||||
import org.geysermc.mcprotocollib.network.factory.ClientNetworkSessionFactory;
|
||||
import org.geysermc.mcprotocollib.network.packet.Packet;
|
||||
import org.geysermc.mcprotocollib.network.tcp.TcpClientSession;
|
||||
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.HandPreference;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.setting.ChatVisibility;
|
||||
|
@ -42,7 +43,7 @@ public class Bot {
|
|||
|
||||
public GameProfile profile;
|
||||
|
||||
public Session session;
|
||||
public ClientSession session;
|
||||
|
||||
public boolean printDisconnectedCause = false;
|
||||
|
||||
|
@ -167,7 +168,10 @@ public class Bot {
|
|||
if (_username == null) username = RandomStringUtilities.generate(8);
|
||||
else username = _username;
|
||||
|
||||
Session session = new TcpClientSession(host, port, new MinecraftProtocol(username), null);
|
||||
final ClientSession session = ClientNetworkSessionFactory.factory()
|
||||
.setAddress(host, port)
|
||||
.setProtocol(new MinecraftProtocol(username))
|
||||
.create();
|
||||
|
||||
this.session = session;
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package me.chayapak1.chomens_bot.plugins;
|
||||
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.util.ColorUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.util.HSVLike;
|
||||
|
||||
public class BruhifyPlugin extends TickPlugin.Listener {
|
||||
private final Bot bot;
|
||||
|
@ -12,7 +12,7 @@ public class BruhifyPlugin extends TickPlugin.Listener {
|
|||
|
||||
private int startHue = 0;
|
||||
|
||||
public BruhifyPlugin (Bot bot) {
|
||||
public BruhifyPlugin(Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.tick.addListener(this);
|
||||
|
@ -29,8 +29,8 @@ public class BruhifyPlugin extends TickPlugin.Listener {
|
|||
Component component = Component.empty();
|
||||
|
||||
for (char character : displayName.toCharArray()) {
|
||||
String color = String.format("#%06x", ColorUtilities.hsvToRgb(hue, 100, 100));
|
||||
component = component.append(Component.text(character).color(TextColor.fromHexString(color)));
|
||||
component = component.append(Component.text(character)
|
||||
.color(TextColor.color(HSVLike.hsvLike(hue / 360.0f, 1, 1))));
|
||||
hue = (hue + increment) % 360;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@ import me.chayapak1.chomens_bot.voiceChat.NetworkMessage;
|
|||
import me.chayapak1.chomens_bot.voiceChat.customPayload.JoinGroupPacket;
|
||||
import me.chayapak1.chomens_bot.voiceChat.customPayload.SecretPacket;
|
||||
import me.chayapak1.chomens_bot.voiceChat.packets.*;
|
||||
import me.chayapak1.chomens_bot.voiceChat.packets.*;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.ArrayList;
|
||||
|
@ -69,17 +68,11 @@ public class VoiceChatPlugin extends Bot.Listener {
|
|||
if (_packet.getChannel().equals(Key.key("voicechat:secret"))) { // fard
|
||||
final byte[] bytes = _packet.getData();
|
||||
final FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(bytes));
|
||||
|
||||
final SecretPacket secretPacket = new SecretPacket().fromBytes(buf);
|
||||
initializationData = new InitializationData(secretPacket);
|
||||
|
||||
initializationData = new InitializationData(bot.session.getHost(), secretPacket);
|
||||
|
||||
try {
|
||||
final InetAddress address = InetAddress.getByName(bot.session.getHost());
|
||||
socketAddress = new InetSocketAddress(address, initializationData.serverPort);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
final InetSocketAddress mcAddress = (InetSocketAddress) bot.session.getRemoteAddress();
|
||||
socketAddress = new InetSocketAddress(mcAddress.getAddress(), initializationData.serverPort);
|
||||
|
||||
socket = new ClientVoiceChatSocket();
|
||||
try {
|
||||
|
|
|
@ -2,12 +2,17 @@ package me.chayapak1.chomens_bot.util;
|
|||
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.format.TextFormat;
|
||||
import net.kyori.adventure.text.serializer.legacy.CharacterAndFormat;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ColorUtilities {
|
||||
private static final Map<TextFormat, Character> formatToLegacyMap = CharacterAndFormat.defaults().stream()
|
||||
.collect(Collectors.toUnmodifiableMap(CharacterAndFormat::format, CharacterAndFormat::character));
|
||||
private static final Map<Integer, String> ansiToIrcMap = new HashMap<>();
|
||||
private static final Map<Integer, String> ansiStyleToIrcMap = new HashMap<>();
|
||||
|
||||
|
@ -16,80 +21,15 @@ public class ColorUtilities {
|
|||
|
||||
if (color.startsWith("#")) return TextColor.fromHexString(color);
|
||||
else {
|
||||
// am i reinventing the wheel here?
|
||||
return switch (color) {
|
||||
case "black" -> NamedTextColor.BLACK;
|
||||
case "dark_blue" -> NamedTextColor.DARK_BLUE;
|
||||
case "dark_green" -> NamedTextColor.DARK_GREEN;
|
||||
case "dark_aqua" -> NamedTextColor.DARK_AQUA;
|
||||
case "dark_red" -> NamedTextColor.DARK_RED;
|
||||
case "dark_purple" -> NamedTextColor.DARK_PURPLE;
|
||||
case "gold" -> NamedTextColor.GOLD;
|
||||
case "gray" -> NamedTextColor.GRAY;
|
||||
case "dark_gray" -> NamedTextColor.DARK_GRAY;
|
||||
case "blue" -> NamedTextColor.BLUE;
|
||||
case "green" -> NamedTextColor.GREEN;
|
||||
case "aqua" -> NamedTextColor.AQUA;
|
||||
case "red" -> NamedTextColor.RED;
|
||||
case "light_purple" -> NamedTextColor.LIGHT_PURPLE;
|
||||
case "yellow" -> NamedTextColor.YELLOW;
|
||||
default -> NamedTextColor.WHITE;
|
||||
};
|
||||
// am i reinventing the wheel here? Yes.
|
||||
return Optional.ofNullable(NamedTextColor.NAMES.value(color))
|
||||
.orElse(NamedTextColor.WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
// Author: ChatGPT
|
||||
public static int hsvToRgb (int hue, int saturation, int value) {
|
||||
Color color = Color.getHSBColor(hue / 360.0f, saturation / 100.0f, value / 100.0f);
|
||||
return color.getRGB() & 0xFFFFFF;
|
||||
}
|
||||
|
||||
private static final ChatColor[] COLORS = {
|
||||
new ChatColor("0", 0x000000),
|
||||
new ChatColor("1", 0x0000aa),
|
||||
new ChatColor("2", 0x00aa00),
|
||||
new ChatColor("3", 0x00aaaa),
|
||||
new ChatColor("4", 0xaa0000),
|
||||
new ChatColor("5", 0xaa00aa),
|
||||
new ChatColor("6", 0xffaa00),
|
||||
new ChatColor("7", 0xaaaaaa),
|
||||
new ChatColor("8", 0x555555),
|
||||
new ChatColor("9", 0x5555ff),
|
||||
new ChatColor("a", 0x55ff55),
|
||||
new ChatColor("b", 0x55ffff),
|
||||
new ChatColor("c", 0xff5555),
|
||||
new ChatColor("d", 0xff55ff),
|
||||
new ChatColor("e", 0xffff55),
|
||||
new ChatColor("f", 0xffffff)
|
||||
};
|
||||
|
||||
public static String getClosestChatColor(int rgb) {
|
||||
int r = (rgb >> 16) & 0xFF;
|
||||
int g = (rgb >> 8) & 0xFF;
|
||||
int b = rgb & 0xFF;
|
||||
|
||||
ChatColor closest = null;
|
||||
int smallestDiff = 0;
|
||||
|
||||
for (ChatColor color : COLORS) {
|
||||
if (color.rgb == rgb) {
|
||||
return color.colorName;
|
||||
}
|
||||
|
||||
// Check by the greatest diff of the 3 values
|
||||
int rAverage = (color.r + r) / 2;
|
||||
int rDiff = color.r - r;
|
||||
int gDiff = color.g - g;
|
||||
int bDiff = color.b - b;
|
||||
int diff = ((2 + (rAverage >> 8)) * rDiff * rDiff)
|
||||
+ (4 * gDiff * gDiff)
|
||||
+ ((2 + ((255 - rAverage) >> 8)) * bDiff * bDiff);
|
||||
if (closest == null || diff < smallestDiff) {
|
||||
closest = color;
|
||||
smallestDiff = diff;
|
||||
}
|
||||
}
|
||||
return closest.colorName;
|
||||
public static char getClosestChatColor(int rgb) {
|
||||
final NamedTextColor closestNamed = NamedTextColor.nearestTo(TextColor.color(rgb));
|
||||
return formatToLegacyMap.get(closestNamed);
|
||||
}
|
||||
|
||||
// Author: ChatGPT
|
||||
|
@ -168,19 +108,4 @@ public class ColorUtilities {
|
|||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static final class ChatColor {
|
||||
|
||||
private final String colorName;
|
||||
private final int rgb;
|
||||
private final int r, g, b;
|
||||
|
||||
public ChatColor(String colorName, int rgb) {
|
||||
this.colorName = colorName;
|
||||
this.rgb = rgb;
|
||||
r = (rgb >> 16) & 0xFF;
|
||||
g = (rgb >> 8) & 0xFF;
|
||||
b = rgb & 0xFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -216,8 +216,7 @@ public class ComponentUtilities {
|
|||
// gets the closest color to the hex
|
||||
|
||||
final int rgb = Integer.parseInt(code.substring(1), 16);
|
||||
|
||||
final String chatColor = ColorUtilities.getClosestChatColor(rgb);
|
||||
final String chatColor = Character.toString(ColorUtilities.getClosestChatColor(rgb));
|
||||
|
||||
ansiCode = ansiMap.get(chatColor);
|
||||
} else {
|
||||
|
|
|
@ -6,7 +6,6 @@ import me.chayapak1.chomens_bot.voiceChat.customPayload.SecretPacket;
|
|||
import java.util.UUID;
|
||||
|
||||
public class InitializationData {
|
||||
public final String serverIP;
|
||||
public final int serverPort;
|
||||
public final UUID playerUUID;
|
||||
public final UUID secret;
|
||||
|
@ -17,8 +16,7 @@ public class InitializationData {
|
|||
public final boolean groupsEnabled;
|
||||
public final boolean allowRecording;
|
||||
|
||||
public InitializationData(String serverIP, SecretPacket secretPacket) {
|
||||
this.serverIP = serverIP;
|
||||
public InitializationData(SecretPacket secretPacket) {
|
||||
this.serverPort = secretPacket.serverPort;
|
||||
this.playerUUID = secretPacket.playerUUID;
|
||||
this.secret = secretPacket.secret;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue