Add stuff - first commit
This commit is contained in:
98
src/main/java/me/chayapak1/chomensbot_mabe/Bot.java
Normal file
98
src/main/java/me/chayapak1/chomensbot_mabe/Bot.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package me.chayapak1.chomensbot_mabe;
|
||||
|
||||
import com.github.steveice10.mc.protocol.MinecraftProtocol;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.*;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.github.steveice10.packetlib.tcp.TcpClientSession;
|
||||
import lombok.Getter;
|
||||
import me.chayapak1.chomensbot_mabe.plugins.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
public class Bot {
|
||||
private final ArrayList<SessionListener>listeners = new ArrayList<>();
|
||||
|
||||
@Getter private final String host;
|
||||
@Getter private final int port;
|
||||
@Getter private final String username;
|
||||
|
||||
@Getter private final Session session;
|
||||
|
||||
@Getter private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
@Getter private final ChatPlugin chat = new ChatPlugin(this);
|
||||
@Getter private final LoggerPlugin logger = new LoggerPlugin(this);
|
||||
@Getter private final SelfCarePlugin selfCare = new SelfCarePlugin(this);
|
||||
@Getter private final ConsolePlugin console = new ConsolePlugin(this);
|
||||
@Getter private final PositionPlugin position = new PositionPlugin(this);
|
||||
@Getter private final CorePlugin core = new CorePlugin(this);
|
||||
@Getter private final CommandHandlerPlugin commandHandler = new CommandHandlerPlugin();
|
||||
@Getter private final ChatCommandHandlerPlugin chatCommandHandler = new ChatCommandHandlerPlugin(this);
|
||||
|
||||
public Bot (String host, int port, String username) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.username = username;
|
||||
Session session = new TcpClientSession(host, port, new MinecraftProtocol(username), null);
|
||||
this.session = session;
|
||||
|
||||
session.addListener(new SessionAdapter() {
|
||||
// same stuff over and over yup
|
||||
|
||||
@Override
|
||||
public void packetReceived(Session session, Packet packet) {
|
||||
for (SessionListener listener : listeners) {
|
||||
if (packet instanceof ClientboundLoginPacket) {
|
||||
listener.connected(new ConnectedEvent(session));
|
||||
}
|
||||
listener.packetReceived(session, packet);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetSending(PacketSendingEvent packetSendingEvent) {
|
||||
for (SessionListener listener : listeners) {
|
||||
listener.packetSending(packetSendingEvent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetSent(Session session, Packet packet) {
|
||||
for (SessionListener listener : listeners) {
|
||||
listener.packetSent(session, packet);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetError(PacketErrorEvent packetErrorEvent) {
|
||||
for (SessionListener listener : listeners) {
|
||||
listener.packetError(packetErrorEvent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnecting(DisconnectingEvent disconnectingEvent) {
|
||||
for (SessionListener listener : listeners) {
|
||||
listener.disconnecting(disconnectingEvent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected(DisconnectedEvent disconnectedEvent) {
|
||||
for (SessionListener listener : listeners) {
|
||||
listener.disconnected(disconnectedEvent);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
session.connect();
|
||||
}
|
||||
|
||||
public void addListener (SessionListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
}
|
||||
11
src/main/java/me/chayapak1/chomensbot_mabe/Main.java
Normal file
11
src/main/java/me/chayapak1/chomensbot_mabe/Main.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package me.chayapak1.chomensbot_mabe;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
final String host = args[0];
|
||||
final int port = Integer.parseInt(args[1]);
|
||||
final String username = args[2];
|
||||
|
||||
new Bot(host, port, username);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package me.chayapak1.chomensbot_mabe.chatParsers;
|
||||
|
||||
import me.chayapak1.chomensbot_mabe.chatParsers.data.ChatParser;
|
||||
import me.chayapak1.chomensbot_mabe.chatParsers.data.PlayerMessage;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.TranslatableComponent;
|
||||
import net.kyori.adventure.text.format.Style;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class KaboomChatParser implements ChatParser {
|
||||
|
||||
public KaboomChatParser () {
|
||||
}
|
||||
|
||||
private static final Style empty = Style.empty();
|
||||
private static final Component SEPERATOR_COLON = Component.text(":");
|
||||
private static final Component SEPERATOR_SPACE = Component.space();
|
||||
|
||||
@Override
|
||||
public PlayerMessage parse (Component message) {
|
||||
if (message instanceof TextComponent) return parse((TextComponent) message);
|
||||
if (message instanceof TranslatableComponent) return parse((TranslatableComponent) message);
|
||||
return null;
|
||||
}
|
||||
|
||||
public PlayerMessage parse (TranslatableComponent message) {
|
||||
if (message.key().equals("%s")) {
|
||||
message.args();
|
||||
if (message.args().size() == 1 && message.style().equals(empty)) return parse(message.args().get(0));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public PlayerMessage parse (TextComponent message) {
|
||||
List<Component> children = message.children();
|
||||
|
||||
if (!message.content().equals("") || !message.style().equals(empty) || children.size() < 3) return null;
|
||||
|
||||
final Map<String, Component> parameters = new HashMap<>();
|
||||
|
||||
final Component prefix = children.get(0);
|
||||
Component displayName = Component.empty();
|
||||
Component contents = Component.empty();
|
||||
|
||||
if (isSeperatorAt(children, 1)) { // Missing/blank display name
|
||||
if (children.size() > 3) contents = children.get(3);
|
||||
} else if (isSeperatorAt(children, 2)) {
|
||||
displayName = children.get(1);
|
||||
if (children.size() > 4) contents = children.get(4);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
parameters.put("sender", displayName);
|
||||
parameters.put("prefix", prefix);
|
||||
parameters.put("contents", contents);
|
||||
|
||||
return new PlayerMessage(parameters);
|
||||
}
|
||||
|
||||
private boolean isSeperatorAt (List<Component> children, int start) {
|
||||
return children.get(start).equals(SEPERATOR_COLON) && children.get(start + 1).equals(SEPERATOR_SPACE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package me.chayapak1.chomensbot_mabe.chatParsers;
|
||||
|
||||
import me.chayapak1.chomensbot_mabe.chatParsers.data.ChatParser;
|
||||
import me.chayapak1.chomensbot_mabe.chatParsers.data.PlayerMessage;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TranslatableComponent;
|
||||
import net.kyori.adventure.text.event.HoverEvent;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MinecraftChatParser implements ChatParser {
|
||||
// ? Is such a mapping necessary?
|
||||
private static final Map<String, String> typeMap = new HashMap<>();
|
||||
static {
|
||||
typeMap.put("chat.type.text", "minecraft:chat");
|
||||
typeMap.put("chat.type.announcement", "minecraft:say_command");
|
||||
typeMap.put("chat.type.command", "minecraft:msg_command");
|
||||
typeMap.put("chat.type.team.text", "minecraft:team_msg_command");
|
||||
typeMap.put("chat.type.emote", "minecraft:emote_command");
|
||||
}
|
||||
|
||||
public MinecraftChatParser () {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerMessage parse (Component message) {
|
||||
if (message instanceof TranslatableComponent) return parse((TranslatableComponent) message);
|
||||
return null;
|
||||
}
|
||||
|
||||
public PlayerMessage parse (TranslatableComponent message) {
|
||||
final List<Component> args = message.args();
|
||||
final String key = message.key();
|
||||
if (args.size() < 2 || !typeMap.containsKey(key)) return null;
|
||||
|
||||
final Map<String, Component> parameters = new HashMap<>();
|
||||
|
||||
final Component senderComponent = args.get(0);
|
||||
final Component contents = args.get(1);
|
||||
|
||||
// Find the sender and attempt to map it to a player
|
||||
final HoverEvent<?> hoverEvent = senderComponent.hoverEvent();
|
||||
if (hoverEvent == null || !hoverEvent.action().equals(HoverEvent.Action.SHOW_ENTITY)) return null;
|
||||
|
||||
parameters.put("sender", senderComponent);
|
||||
parameters.put("contents", contents);
|
||||
|
||||
return new PlayerMessage(parameters);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package me.chayapak1.chomensbot_mabe.chatParsers.data;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public interface ChatParser {
|
||||
PlayerMessage parse (Component message);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package me.chayapak1.chomensbot_mabe.chatParsers.data;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.AllArgsConstructor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class PlayerMessage {
|
||||
private Map<String, Component> parameters;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package me.chayapak1.chomensbot_mabe.command;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Command {
|
||||
String description();
|
||||
List<String> usage();
|
||||
Component execute(CommandContext context, String[] args) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package me.chayapak1.chomensbot_mabe.command;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public class CommandContext {
|
||||
@Getter public final Bot bot;
|
||||
|
||||
public CommandContext(Bot bot) {
|
||||
this.bot = bot;
|
||||
}
|
||||
|
||||
public Component displayName () { return Component.empty(); }
|
||||
public void sendOutput (Component component) {}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package me.chayapak1.chomensbot_mabe.command;
|
||||
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import me.chayapak1.chomensbot_mabe.util.ComponentUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
public class ConsoleCommandContext extends CommandContext {
|
||||
private final Bot bot;
|
||||
|
||||
public ConsoleCommandContext (Bot bot) {
|
||||
super(bot);
|
||||
this.bot = bot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendOutput (Component component) {
|
||||
final String message = ComponentUtilities.stringify(component);
|
||||
bot.logger().log(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component displayName () {
|
||||
return Component.text(bot.username()).color(NamedTextColor.YELLOW);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package me.chayapak1.chomensbot_mabe.command;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public class PlayerCommandContext extends CommandContext {
|
||||
@Getter private final String playerName;
|
||||
|
||||
private final Bot bot;
|
||||
|
||||
public PlayerCommandContext (Bot bot, String playerName) {
|
||||
super(bot);
|
||||
this.bot = bot;
|
||||
this.playerName = playerName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendOutput (Component message) {
|
||||
bot.chat().tellraw(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component displayName () {
|
||||
return Component.text(playerName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package me.chayapak1.chomensbot_mabe.commands;
|
||||
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import me.chayapak1.chomensbot_mabe.command.Command;
|
||||
import me.chayapak1.chomensbot_mabe.command.CommandContext;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandBlockCommand implements Command {
|
||||
public String description() {
|
||||
return "Executes a command in the command core";
|
||||
}
|
||||
|
||||
public List<String> usage() {
|
||||
final List<String> usages = new ArrayList<>();
|
||||
usages.add("<command>");
|
||||
|
||||
return usages;
|
||||
}
|
||||
|
||||
public Component execute(CommandContext context, String[] args) {
|
||||
final Bot bot = context.bot();
|
||||
|
||||
bot.core().run(String.join(" ", args));
|
||||
|
||||
return Component.text("success");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package me.chayapak1.chomensbot_mabe.commands;
|
||||
|
||||
import com.github.ricksbrown.cowsay.plugin.CowExecutor;
|
||||
import me.chayapak1.chomensbot_mabe.command.Command;
|
||||
import me.chayapak1.chomensbot_mabe.command.CommandContext;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CowsayCommand implements Command {
|
||||
public String description() {
|
||||
return "Moo";
|
||||
}
|
||||
|
||||
public List<String> usage() {
|
||||
final List<String> usages = new ArrayList<>();
|
||||
usages.add("<cow> <message>");
|
||||
|
||||
return usages;
|
||||
}
|
||||
|
||||
public Component execute(CommandContext context, String[] args) {
|
||||
final String cow = args[0];
|
||||
final String message = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
|
||||
|
||||
final CowExecutor cowExecutor = new CowExecutor();
|
||||
cowExecutor.setCowfile(cow);
|
||||
cowExecutor.setMessage(message);
|
||||
|
||||
final String result = cowExecutor.execute();
|
||||
|
||||
context.sendOutput(Component.text(result));
|
||||
|
||||
return Component.text("success");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package me.chayapak1.chomensbot_mabe.commands;
|
||||
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import me.chayapak1.chomensbot_mabe.command.Command;
|
||||
import me.chayapak1.chomensbot_mabe.command.CommandContext;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class EchoCommand implements Command {
|
||||
public String description() {
|
||||
return "Says a message";
|
||||
}
|
||||
|
||||
public List<String> usage() {
|
||||
final List<String> usages = new ArrayList<>();
|
||||
usages.add("<message>");
|
||||
|
||||
return usages;
|
||||
}
|
||||
|
||||
public Component execute (CommandContext context, String[] args) {
|
||||
final Bot bot = context.bot();
|
||||
|
||||
bot.chat().send(String.join(" ", args));
|
||||
|
||||
return Component.text("success");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package me.chayapak1.chomensbot_mabe.commands;
|
||||
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import me.chayapak1.chomensbot_mabe.command.Command;
|
||||
import me.chayapak1.chomensbot_mabe.command.CommandContext;
|
||||
import me.chayapak1.chomensbot_mabe.plugins.CommandHandlerPlugin;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.JoinConfiguration;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class HelpCommand implements Command {
|
||||
public String description() {
|
||||
return "Shows the help";
|
||||
}
|
||||
|
||||
public List<String> usage() {
|
||||
final List<String> usages = new ArrayList<>();
|
||||
usages.add("[command]");
|
||||
|
||||
return usages;
|
||||
}
|
||||
|
||||
public Component execute (CommandContext context, String[] args) {
|
||||
if (args.length == 0) {
|
||||
sendCommandList(context);
|
||||
return Component.text("success");
|
||||
} else {
|
||||
return sendUsages(context, args);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendCommandList(CommandContext context) {
|
||||
final List<Component> list = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<String, Command> entry : CommandHandlerPlugin.commands().entrySet()) {
|
||||
final String name = entry.getKey();
|
||||
|
||||
list.add(Component.text(name));
|
||||
}
|
||||
|
||||
final Component component = Component.empty()
|
||||
.append(Component.text("Commands ").color(NamedTextColor.GRAY))
|
||||
.append(Component.text("(").color(NamedTextColor.DARK_GRAY))
|
||||
.append(Component.text("Length: ").color(NamedTextColor.GRAY))
|
||||
.append(Component.text(list.size()).color(NamedTextColor.GREEN))
|
||||
.append(Component.text(") - ").color(NamedTextColor.DARK_GRAY))
|
||||
.append(Component.join(JoinConfiguration.separator(Component.space()), list).color(NamedTextColor.GREEN));
|
||||
|
||||
context.sendOutput(component);
|
||||
}
|
||||
|
||||
public Component sendUsages (CommandContext context, String[] args) {
|
||||
final Bot bot = context.bot();
|
||||
|
||||
final String prefix = bot.chatCommandHandler().prefix();
|
||||
|
||||
final String commandName = args[0];
|
||||
|
||||
for (Map.Entry<String, Command> entry : CommandHandlerPlugin.commands().entrySet()) {
|
||||
if (!entry.getKey().equals(commandName)) continue;
|
||||
|
||||
final List<Component> usages = new ArrayList<>();
|
||||
|
||||
final Command command = entry.getValue();
|
||||
|
||||
usages.add(
|
||||
Component.empty()
|
||||
.append(Component.text(prefix + commandName).color(NamedTextColor.GOLD))
|
||||
.append(Component.text(" - " + command.description()).color(NamedTextColor.GRAY))
|
||||
);
|
||||
|
||||
usages.add(
|
||||
Component.empty()
|
||||
.append(Component.text("Trust level: ").color(NamedTextColor.GREEN))
|
||||
.append(Component.text("TODO").color(NamedTextColor.YELLOW))
|
||||
);
|
||||
|
||||
for (String usage : command.usage()) {
|
||||
usages.add(
|
||||
Component.empty()
|
||||
.append(Component.text(prefix + commandName).color(NamedTextColor.GOLD))
|
||||
.append(Component.text(" "))
|
||||
.append(Component.text(usage).color(NamedTextColor.AQUA))
|
||||
);
|
||||
}
|
||||
|
||||
context.sendOutput(Component.join(JoinConfiguration.separator(Component.newline()), usages));
|
||||
|
||||
return Component.text("success");
|
||||
}
|
||||
|
||||
return Component.text("Unknown command").color(NamedTextColor.RED);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package me.chayapak1.chomensbot_mabe.commands;
|
||||
|
||||
import me.chayapak1.chomensbot_mabe.command.Command;
|
||||
import me.chayapak1.chomensbot_mabe.command.CommandContext;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestCommand implements Command {
|
||||
public String description() {
|
||||
return "Tests if the bot is working";
|
||||
}
|
||||
|
||||
public List<String> usage() {
|
||||
final List<String> usages = new ArrayList<>();
|
||||
usages.add("");
|
||||
|
||||
return usages;
|
||||
}
|
||||
|
||||
public Component execute(CommandContext context, String[] args) {
|
||||
context.sendOutput(
|
||||
Component.empty()
|
||||
.append(Component.text("Hello, World! Username: "))
|
||||
.append(context.displayName())
|
||||
.color(NamedTextColor.GREEN)
|
||||
);
|
||||
|
||||
return Component.text("success");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package me.chayapak1.chomensbot_mabe.commands;
|
||||
|
||||
import me.chayapak1.chomensbot_mabe.command.Command;
|
||||
import me.chayapak1.chomensbot_mabe.command.CommandContext;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ThrowCommand implements Command {
|
||||
public String description() {
|
||||
return "A command to throw an error, kinda useless";
|
||||
}
|
||||
|
||||
public List<String> usage() {
|
||||
final List<String> usages = new ArrayList<>();
|
||||
usages.add("[message]");
|
||||
|
||||
return usages;
|
||||
}
|
||||
|
||||
public Component execute(CommandContext context, String[] args) throws Exception {
|
||||
final String message = String.join(" ", args);
|
||||
|
||||
throw new Exception(message.equals("") ? "among us" : message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package me.chayapak1.chomensbot_mabe.plugins;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import me.chayapak1.chomensbot_mabe.chatParsers.data.PlayerMessage;
|
||||
import me.chayapak1.chomensbot_mabe.command.PlayerCommandContext;
|
||||
import me.chayapak1.chomensbot_mabe.util.ComponentUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
|
||||
public class ChatCommandHandlerPlugin extends ChatPlugin.ChatListener {
|
||||
@Getter private final String prefix = "j*";
|
||||
|
||||
public final Bot bot;
|
||||
|
||||
public ChatCommandHandlerPlugin(Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.chat().addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playerMessageReceived (PlayerMessage message) {
|
||||
final Component displayNameComponent = message.parameters().get("sender");
|
||||
final Component messageComponent = message.parameters().get("contents");
|
||||
if (displayNameComponent == null || messageComponent == null) return;
|
||||
|
||||
final String displayName = ComponentUtilities.stringify(displayNameComponent);
|
||||
final String contents = ComponentUtilities.stringify(messageComponent);
|
||||
|
||||
if (!contents.startsWith(prefix)) return;
|
||||
final String commandString = contents.substring(prefix.length());
|
||||
|
||||
final PlayerCommandContext context = new PlayerCommandContext(bot, displayName);
|
||||
|
||||
final Component output = CommandHandlerPlugin.executeCommand(commandString, context);
|
||||
final String textOutput = ((TextComponent) output).content();
|
||||
|
||||
if (!textOutput.equals("success")) {
|
||||
context.sendOutput(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package me.chayapak1.chomensbot_mabe.plugins;
|
||||
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatPacket;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import me.chayapak1.chomensbot_mabe.chatParsers.KaboomChatParser;
|
||||
import me.chayapak1.chomensbot_mabe.chatParsers.MinecraftChatParser;
|
||||
import me.chayapak1.chomensbot_mabe.chatParsers.data.ChatParser;
|
||||
import me.chayapak1.chomensbot_mabe.chatParsers.data.PlayerMessage;
|
||||
import me.chayapak1.chomensbot_mabe.util.ComponentUtilities;
|
||||
import me.chayapak1.chomensbot_mabe.util.UUIDUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TranslatableComponent;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ChatPlugin extends SessionAdapter {
|
||||
private final Bot bot;
|
||||
|
||||
private final List<ChatParser> chatParsers;
|
||||
|
||||
private final List<ChatListener> listeners = new ArrayList<>();
|
||||
|
||||
public ChatPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
bot.addListener(this);
|
||||
|
||||
chatParsers = new ArrayList<>();
|
||||
chatParsers.add(new MinecraftChatParser());
|
||||
chatParsers.add(new KaboomChatParser());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetReceived (Session session, Packet packet) {
|
||||
if (packet instanceof ClientboundSystemChatPacket) {
|
||||
packetReceived((ClientboundSystemChatPacket) packet);
|
||||
}
|
||||
}
|
||||
|
||||
public void packetReceived (ClientboundSystemChatPacket packet) {
|
||||
final Component component = packet.getContent();
|
||||
|
||||
try {
|
||||
final String key = ((TranslatableComponent) component).key();
|
||||
|
||||
if (
|
||||
key.equals("advMode.setCommand.success") ||
|
||||
key.equals("advMode.notAllowed")
|
||||
) return;
|
||||
} catch (ClassCastException ignored) {}
|
||||
|
||||
PlayerMessage playerMessage = null;
|
||||
|
||||
for (ChatParser parser : chatParsers) {
|
||||
playerMessage = parser.parse(component);
|
||||
if (playerMessage != null) break;
|
||||
}
|
||||
|
||||
final String message = ComponentUtilities.stringify(component);
|
||||
for (ChatListener listener : listeners) {
|
||||
listener.systemMessageReceived(message, component);
|
||||
if (playerMessage != null) listener.playerMessageReceived(playerMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public void send (String message) {
|
||||
if (message.startsWith("/")) {
|
||||
bot.session().send(new ServerboundChatCommandPacket(
|
||||
message.substring(1),
|
||||
Instant.now().toEpochMilli(),
|
||||
0L,
|
||||
new ArrayList<>(),
|
||||
false,
|
||||
new ArrayList<>(),
|
||||
null
|
||||
));
|
||||
} else {
|
||||
bot.session().send(new ServerboundChatPacket(
|
||||
message,
|
||||
Instant.now().toEpochMilli(),
|
||||
0L,
|
||||
new byte[0],
|
||||
false,
|
||||
new ArrayList<>(),
|
||||
null
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public void tellraw (Component component, String targets) {
|
||||
bot.core().run("minecraft:tellraw " + targets + " " + GsonComponentSerializer.gson().serialize(component));
|
||||
}
|
||||
|
||||
public void tellraw (Component component, UUID uuid) { tellraw(component, UUIDUtilities.selector(uuid)); }
|
||||
|
||||
public void tellraw (Component component) { tellraw(component, "@a"); }
|
||||
|
||||
public void addListener (ChatListener listener) { listeners.add(listener); }
|
||||
|
||||
public static class ChatListener {
|
||||
public void playerMessageReceived (PlayerMessage message) {}
|
||||
public void systemMessageReceived (String message, Component component) {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package me.chayapak1.chomensbot_mabe.plugins;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.chayapak1.chomensbot_mabe.command.Command;
|
||||
import me.chayapak1.chomensbot_mabe.command.CommandContext;
|
||||
import me.chayapak1.chomensbot_mabe.commands.*;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.event.HoverEvent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CommandHandlerPlugin {
|
||||
@Getter private static final Map<String, Command> commands = new HashMap<>();
|
||||
|
||||
public CommandHandlerPlugin () {
|
||||
registerCommand("cb", new CommandBlockCommand());
|
||||
registerCommand("cowsay", new CowsayCommand());
|
||||
registerCommand("echo", new EchoCommand());
|
||||
registerCommand("help", new HelpCommand());
|
||||
registerCommand("test", new TestCommand());
|
||||
registerCommand("throw", new ThrowCommand());
|
||||
}
|
||||
|
||||
public void registerCommand (String commandName, Command command) {
|
||||
commands.put(commandName, command);
|
||||
}
|
||||
|
||||
public static Component executeCommand (String input, CommandContext context) {
|
||||
final String[] splitInput = input.split(" ");
|
||||
|
||||
final String commandName = splitInput[0];
|
||||
final String[] args = Arrays.copyOfRange(splitInput, 1, splitInput.length);
|
||||
|
||||
final Command command = commands.get(commandName);
|
||||
|
||||
if (command != null) {
|
||||
try {
|
||||
return command.execute(context, args);
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
|
||||
final String stackTrace = ExceptionUtils.getStackTrace(exception);
|
||||
return Component
|
||||
.text("An error occurred while trying to execute the command, hover here for more details", NamedTextColor.RED)
|
||||
.hoverEvent(
|
||||
HoverEvent.showText(
|
||||
Component
|
||||
.text(stackTrace)
|
||||
.color(NamedTextColor.RED)
|
||||
)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return Component.text("Unknown command: " + commandName).color(NamedTextColor.RED);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package me.chayapak1.chomensbot_mabe.plugins;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import me.chayapak1.chomensbot_mabe.command.ConsoleCommandContext;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.jline.reader.EndOfFileException;
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.LineReaderBuilder;
|
||||
import org.jline.reader.UserInterruptException;
|
||||
|
||||
public class ConsolePlugin {
|
||||
private final Bot bot;
|
||||
|
||||
@Getter public final LineReader reader;
|
||||
|
||||
@Getter private final String prefix = ".";
|
||||
|
||||
public ConsolePlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
this.reader = LineReaderBuilder.builder().build();
|
||||
String prompt = "> ";
|
||||
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
String line = null;
|
||||
try {
|
||||
line = reader.readLine(prompt);
|
||||
} catch (UserInterruptException e) {
|
||||
System.exit(1); // yup
|
||||
} catch (EndOfFileException e) {
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
handleLine(line);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public void handleLine (String line) {
|
||||
if (line == null) return;
|
||||
|
||||
if (line.startsWith(prefix)) {
|
||||
final ConsoleCommandContext context = new ConsoleCommandContext(bot);
|
||||
|
||||
final Component output = CommandHandlerPlugin.executeCommand(line.substring(prefix.length()), context);
|
||||
final String textOutput = ((TextComponent) output).content();
|
||||
|
||||
if (!textOutput.equals("success")) {
|
||||
context.sendOutput(output);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bot.chat().tellraw(
|
||||
Component.translatable(
|
||||
"[%s] %s › %s",
|
||||
Component.text(bot.username() + " Console").color(NamedTextColor.GRAY),
|
||||
Component.text("chayapak").color(NamedTextColor.GREEN),
|
||||
Component.text(line).color(NamedTextColor.GRAY)
|
||||
).color(NamedTextColor.DARK_GRAY)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package me.chayapak1.chomensbot_mabe.plugins;
|
||||
|
||||
import com.github.steveice10.mc.protocol.data.game.level.block.CommandBlockMode;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetCommandBlockPacket;
|
||||
import com.nukkitx.math.vector.Vector3i;
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
|
||||
public class CorePlugin extends PositionPlugin.PositionListener {
|
||||
private final Bot bot;
|
||||
|
||||
public final Vector3i coreStart = Vector3i.from(0, 0, 0);
|
||||
public final Vector3i coreEnd = Vector3i.from(15, 2, 15);
|
||||
|
||||
public Vector3i origin;
|
||||
|
||||
public Vector3i relativeCorePosition = Vector3i.from(coreStart);
|
||||
|
||||
public CorePlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.position().addListener(this);
|
||||
}
|
||||
|
||||
public void run (String command) {
|
||||
bot.session().send(new ServerboundSetCommandBlockPacket(
|
||||
absoluteCorePosition(),
|
||||
"",
|
||||
CommandBlockMode.REDSTONE,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
));
|
||||
bot.session().send(new ServerboundSetCommandBlockPacket(
|
||||
absoluteCorePosition(),
|
||||
command,
|
||||
CommandBlockMode.REDSTONE,
|
||||
true,
|
||||
false,
|
||||
true
|
||||
));
|
||||
|
||||
incrementBlock();
|
||||
}
|
||||
|
||||
public Vector3i absoluteCorePosition () {
|
||||
return relativeCorePosition.add(origin);
|
||||
}
|
||||
|
||||
private void incrementBlock () {
|
||||
int x = relativeCorePosition.getX();
|
||||
int y = relativeCorePosition.getY();
|
||||
int z = relativeCorePosition.getZ();
|
||||
|
||||
x++;
|
||||
|
||||
if (x >= coreEnd.getX()) {
|
||||
x = coreStart.getX();
|
||||
z++;
|
||||
}
|
||||
|
||||
if (z >= coreEnd.getZ()) {
|
||||
z = coreStart.getZ();
|
||||
y++;
|
||||
}
|
||||
|
||||
if (y >= coreEnd.getY()) {
|
||||
x = coreStart.getX();
|
||||
y = coreStart.getY();
|
||||
z = coreStart.getZ();
|
||||
}
|
||||
|
||||
relativeCorePosition = Vector3i.from(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void positionChange (Vector3i position) {
|
||||
origin = Vector3i.from(
|
||||
bot.position().position().getX(),
|
||||
0,
|
||||
bot.position().position().getZ()
|
||||
);
|
||||
refill();
|
||||
}
|
||||
|
||||
public void refill () {
|
||||
final String command = String.format(
|
||||
"/minecraft:fill %s %s %s %s %s %s minecraft:command_block",
|
||||
|
||||
coreStart.getX() + origin.getX(),
|
||||
coreStart.getY(),
|
||||
coreStart.getZ() + origin.getZ(),
|
||||
|
||||
coreEnd.getX() + origin.getX(),
|
||||
coreEnd.getY(),
|
||||
coreEnd.getZ() + origin.getZ()
|
||||
);
|
||||
bot.chat().send(command);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package me.chayapak1.chomensbot_mabe.plugins;
|
||||
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public class LoggerPlugin extends ChatPlugin.ChatListener {
|
||||
private final Bot bot;
|
||||
|
||||
public LoggerPlugin(Bot bot) {
|
||||
this.bot = bot;
|
||||
bot.chat().addListener(this);
|
||||
}
|
||||
|
||||
public void log (String message) {
|
||||
bot.console().reader().printAbove(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void systemMessageReceived(String message, Component component) {
|
||||
log(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package me.chayapak1.chomensbot_mabe.plugins;
|
||||
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerPositionPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.level.ServerboundAcceptTeleportationPacket;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.nukkitx.math.vector.Vector3i;
|
||||
import lombok.Getter;
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PositionPlugin extends SessionAdapter {
|
||||
private final Bot bot;
|
||||
|
||||
private final List<PositionListener> listeners = new ArrayList<>();
|
||||
|
||||
@Getter private Vector3i position = Vector3i.from(0, 0, 0);
|
||||
|
||||
public PositionPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
bot.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetReceived (Session session, Packet packet) {
|
||||
if (packet instanceof ClientboundPlayerPositionPacket) {
|
||||
packetReceived((ClientboundPlayerPositionPacket) packet);
|
||||
}
|
||||
}
|
||||
|
||||
public void packetReceived (ClientboundPlayerPositionPacket packet) {
|
||||
position = Vector3i.from(packet.getX(), packet.getY(), packet.getZ());
|
||||
for (PositionListener listener : listeners) { listener.positionChange(position); }
|
||||
bot.session().send(new ServerboundAcceptTeleportationPacket(packet.getTeleportId()));
|
||||
}
|
||||
|
||||
public void addListener (PositionListener listener) { listeners.add(listener); }
|
||||
|
||||
public static class PositionListener {
|
||||
public void positionChange (Vector3i position) {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package me.chayapak1.chomensbot_mabe.plugins;
|
||||
|
||||
import com.github.steveice10.mc.protocol.MinecraftProtocol;
|
||||
import com.github.steveice10.mc.protocol.data.ProtocolState;
|
||||
import com.github.steveice10.mc.protocol.data.game.entity.EntityEvent;
|
||||
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
|
||||
import com.github.steveice10.mc.protocol.data.game.level.notify.GameEvent;
|
||||
import com.github.steveice10.mc.protocol.data.game.level.notify.GameEventValue;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.ClientboundEntityEventPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundGameEventPacket;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.github.steveice10.packetlib.packet.PacketProtocol;
|
||||
import me.chayapak1.chomensbot_mabe.Bot;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class SelfCarePlugin extends SessionAdapter {
|
||||
private final Bot bot;
|
||||
|
||||
private ScheduledFuture<?> futureTask;
|
||||
|
||||
private int entityId;
|
||||
private GameMode gamemode;
|
||||
private int permissionLevel;
|
||||
private boolean cspy = false;
|
||||
private boolean vanish = false;
|
||||
private boolean socialspy = false;
|
||||
private boolean muted = false;
|
||||
private boolean prefix = false;
|
||||
|
||||
public SelfCarePlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.addListener(this);
|
||||
|
||||
bot.chat().addListener(new ChatPlugin.ChatListener() {
|
||||
@Override
|
||||
public void systemMessageReceived(String message, Component component) {
|
||||
if (message.equals("Successfully enabled CommandSpy")) cspy = true;
|
||||
else if (message.equals("Successfully disabled CommandSpy")) cspy = false;
|
||||
|
||||
else if (message.equals("Vanish for " + bot.username() + ": enabled")) vanish = true;
|
||||
else if (message.equals("Vanish for " + bot.username() + ": disabled")) vanish = false;
|
||||
|
||||
else if (message.equals("SocialSpy for " + bot.username() + ": enabled")) socialspy = true;
|
||||
else if (message.equals("SocialSpy for " + bot.username() + ": disabled")) socialspy = false;
|
||||
|
||||
else if (message.startsWith("You have been muted")) muted = true;
|
||||
else if (message.equals("You have been unmuted.")) muted = false;
|
||||
|
||||
else if (message.equals("You now have the tag: [ChomeNS Bot]") || // for 1.19.2 (or 1.19?) and older clones
|
||||
message.equals("You now have the tag: &8[&eChomeNS Bot&8]")
|
||||
) {
|
||||
prefix = true;
|
||||
return;
|
||||
}
|
||||
if (message.startsWith("You no longer have a tag")) prefix = false;
|
||||
if (message.startsWith("You now have the tag: ")) prefix = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void check () {
|
||||
if (gamemode != GameMode.CREATIVE) bot.chat().send("/minecraft:gamemode creative @s[type=player]");
|
||||
else if (permissionLevel < 2) bot.chat().send("/minecraft:op @s[type=player]");
|
||||
else if (!cspy) bot.chat().send("/commandspy:commandspy on");
|
||||
else if (!vanish) bot.chat().send("/essentials:vanish enable");
|
||||
else if (!socialspy) bot.chat().send("/essentials:socialspy enable");
|
||||
else if (muted) bot.chat().send("/essentials:mute " + bot.username());
|
||||
else if (!prefix) bot.chat().send("/extras:prefix &8[&eChomeNS Bot&8]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetReceived (Session session, Packet packet) {
|
||||
if (packet instanceof ClientboundLoginPacket) packetReceived((ClientboundLoginPacket) packet);
|
||||
else if (packet instanceof ClientboundGameEventPacket) packetReceived((ClientboundGameEventPacket) packet);
|
||||
else if (packet instanceof ClientboundEntityEventPacket) packetReceived((ClientboundEntityEventPacket) packet);
|
||||
}
|
||||
|
||||
public void packetReceived (ClientboundLoginPacket packet) {
|
||||
this.entityId = packet.getEntityId();
|
||||
this.gamemode = packet.getGameMode();
|
||||
|
||||
final Runnable task = () -> {
|
||||
final Session session = bot.session();
|
||||
final PacketProtocol protocol = session.getPacketProtocol();
|
||||
if (
|
||||
!session.isConnected() ||
|
||||
(
|
||||
protocol instanceof MinecraftProtocol &&
|
||||
((MinecraftProtocol) protocol).getState() != ProtocolState.GAME
|
||||
)
|
||||
) return;
|
||||
|
||||
check();
|
||||
};
|
||||
|
||||
futureTask = bot.executor().scheduleAtFixedRate(task, 50, 500, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public void packetReceived (ClientboundGameEventPacket packet) {
|
||||
final GameEvent notification = packet.getNotification();
|
||||
final GameEventValue value = packet.getValue();
|
||||
|
||||
if (notification == GameEvent.CHANGE_GAMEMODE) gamemode = (GameMode) value;
|
||||
}
|
||||
|
||||
public void packetReceived (ClientboundEntityEventPacket packet) {
|
||||
final EntityEvent event = packet.getEvent();
|
||||
final int id = packet.getEntityId();
|
||||
|
||||
if (id != entityId) return;
|
||||
|
||||
if (event == EntityEvent.PLAYER_OP_PERMISSION_LEVEL_0) permissionLevel = 0;
|
||||
else if (event == EntityEvent.PLAYER_OP_PERMISSION_LEVEL_1) permissionLevel = 1;
|
||||
else if (event == EntityEvent.PLAYER_OP_PERMISSION_LEVEL_2) permissionLevel = 2;
|
||||
else if (event == EntityEvent.PLAYER_OP_PERMISSION_LEVEL_3) permissionLevel = 3;
|
||||
else if (event == EntityEvent.PLAYER_OP_PERMISSION_LEVEL_4) permissionLevel = 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected (DisconnectedEvent event) {
|
||||
futureTask.cancel(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package me.chayapak1.chomensbot_mabe.util;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.TranslatableComponent;
|
||||
import net.kyori.adventure.text.SelectorComponent;
|
||||
import net.kyori.adventure.text.KeybindComponent;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
// totallynotskidded™ from chipmunkbot
|
||||
public class ComponentUtilities {
|
||||
private static final Map<String, String> language = loadJsonStringMap("language.json");
|
||||
private static final Map<String, String> keybinds = loadJsonStringMap("keybinds.json");
|
||||
|
||||
public static final Pattern ARG_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([s%])");
|
||||
|
||||
private ComponentUtilities () {
|
||||
}
|
||||
|
||||
private static Map<String, String> loadJsonStringMap (String name) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
|
||||
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(name);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
JsonObject json = JsonParser.parseReader(reader).getAsJsonObject();
|
||||
|
||||
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
|
||||
map.put(entry.getKey(), json.get(entry.getKey()).getAsString());
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
private static String getOrReturnKey (String key) {
|
||||
return ComponentUtilities.language.getOrDefault(key, key);
|
||||
}
|
||||
|
||||
public static String stringify (Component message) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
builder.append(stringifyPartially(message));
|
||||
|
||||
for (Component child : message.children()) builder.append(stringify(child));
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static String stringifyPartially (Component message) {
|
||||
if (message instanceof TextComponent) return stringifyPartially((TextComponent) message);
|
||||
if (message instanceof TranslatableComponent) return stringifyPartially((TranslatableComponent) message);
|
||||
if (message instanceof SelectorComponent) return stringifyPartially((SelectorComponent) message);
|
||||
if (message instanceof KeybindComponent) return stringifyPartially((KeybindComponent) message);
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String stringifyPartially (TextComponent message) {
|
||||
return message.content();
|
||||
}
|
||||
|
||||
public static String stringifyPartially (TranslatableComponent message) {
|
||||
String format = getOrReturnKey(message.key());
|
||||
|
||||
// totallynotskidded™️ from HBot (and changed a bit)
|
||||
Matcher matcher = ARG_PATTERN.matcher(format);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
int i = 0;
|
||||
while (matcher.find()) {
|
||||
if (matcher.group().equals("%%")) {
|
||||
matcher.appendReplacement(sb, "%");
|
||||
} else {
|
||||
String idxStr = matcher.group(1);
|
||||
int idx = idxStr == null ? i++ : (Integer.parseInt(idxStr) - 1);
|
||||
if (idx >= 0 && idx < message.args().size()) {
|
||||
matcher.appendReplacement(sb, Matcher.quoteReplacement( stringify(message.args().get(idx)) ));
|
||||
} else {
|
||||
matcher.appendReplacement(sb, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String stringifyPartially (SelectorComponent message) {
|
||||
return message.pattern(); // * Client-side selector components are equivalent to text ones, and do NOT list entities.
|
||||
}
|
||||
|
||||
public static String stringifyPartially (KeybindComponent message) {
|
||||
String keybind = message.keybind();
|
||||
Component component = keybinds.containsKey(keybind) ? Component.translatable(keybind) : Component.text(keybind); // TODO: Fix some keys like `key.keyboard.a`
|
||||
return stringifyPartially(component);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package me.chayapak1.chomensbot_mabe.util;
|
||||
|
||||
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
|
||||
import java.util.UUID;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class UUIDUtilities {
|
||||
private UUIDUtilities () {}
|
||||
|
||||
public static int[] intArray (UUID uuid) {
|
||||
final ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
|
||||
buffer.putLong(0, uuid.getMostSignificantBits());
|
||||
buffer.putLong(8, uuid.getLeastSignificantBits());
|
||||
|
||||
final int[] intArray = new int[4];
|
||||
for (int i = 0; i < intArray.length; i++) intArray[i] = buffer.getInt();
|
||||
|
||||
return intArray;
|
||||
}
|
||||
|
||||
public static UUID byString (String string, int version) {
|
||||
UUID uuid = UUID.nameUUIDFromBytes(string.getBytes());
|
||||
uuid = new UUID(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits() & ~(0xfL << 12));
|
||||
uuid = new UUID(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits() | ((long) version << 12));
|
||||
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public static IntArrayTag tag (UUID uuid) {
|
||||
return new IntArrayTag("", intArray(uuid));
|
||||
}
|
||||
|
||||
public static String snbt (UUID uuid) {
|
||||
int[] array = intArray(uuid);
|
||||
return "[I;" + array[0] + "," + array[1] + "," + array[2] + "," + array[3] + "]"; // TODO: improve lol
|
||||
}
|
||||
|
||||
public static String selector (UUID uuid) { return "@a[limit=1,nbt={UUID:" + snbt(uuid) + "}]"; }
|
||||
public static String exclusiveSelector (UUID uuid) { return "@a[nbt=!{UUID:" + snbt(uuid) + "}]"; }
|
||||
}
|
||||
Reference in New Issue
Block a user