forked from chipmunkmc/chipmunkbot
Begin to actually try to get commands to work
This commit is contained in:
parent
4f5d0f4c49
commit
a67b45c4d6
9 changed files with 5640 additions and 8 deletions
|
@ -16,7 +16,8 @@ import land.chipmunk.chipmunkbot.plugins.*;
|
|||
public class ChipmunkBot extends Client {
|
||||
@Getter public final ChatPlugin chat = new ChatPlugin(this);
|
||||
@Getter public final PlayerListPlugin playerList = new PlayerListPlugin(this);
|
||||
@Getter public final CommandManagerPlugin commandManager = new CommandManagerPlugin(this);
|
||||
@Getter public final CommandManager commandManager = new CommandManager(this);
|
||||
@Getter public final ChatCommandHandler chatCommandHandler = new ChatCommandHandler(this);
|
||||
@Getter public final PositionManager position = new PositionManager(this);
|
||||
@Getter public final CommandCore core = new CommandCore(this);
|
||||
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
package land.chipmunk.chipmunkbot.chat;
|
||||
|
||||
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;
|
||||
|
||||
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 (Map<String, String> map, String key) {
|
||||
return map.containsKey(key) ? map.get(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(language, message.key());
|
||||
|
||||
// totallynotskidded™️ from HBot
|
||||
Matcher matcher = ARG_PATTERN.matcher(format);
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
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);
|
||||
if (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,32 @@
|
|||
package land.chipmunk.chipmunkbot.plugins;
|
||||
|
||||
import land.chipmunk.chipmunkbot.ChipmunkBot;
|
||||
import land.chipmunk.chipmunkbot.command.CommandSource;
|
||||
import land.chipmunk.chipmunkbot.plugins.ChatPlugin;
|
||||
import land.chipmunk.chipmunkbot.data.chat.PlayerMessage;
|
||||
import land.chipmunk.chipmunkbot.chat.ComponentUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class ChatCommandHandler extends ChatPlugin.Listener {
|
||||
private ChipmunkBot client;
|
||||
@Getter @Setter private String prefix = "'"; // TODO: Don't hardcode this
|
||||
|
||||
public ChatCommandHandler (ChipmunkBot client) {
|
||||
this.client = client;
|
||||
client.chat().addListener((ChatPlugin.Listener) this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playerMessageReceived (PlayerMessage message) {
|
||||
final Component contents = message.parameters().get("contents");
|
||||
if (contents == null) return;
|
||||
final String contentsString = ComponentUtilities.stringify(contents);
|
||||
if (!contentsString.startsWith(prefix)) return;
|
||||
final String commandString = contentsString.substring(prefix.length());
|
||||
|
||||
// System.out.println(ComponentUtilities.stringify(message.parameters().get("sender")) + " issued the command " + commandString);
|
||||
}
|
||||
}
|
|
@ -71,7 +71,7 @@ public class ChatPlugin extends SessionAdapter {
|
|||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public class Listener {
|
||||
public static class Listener {
|
||||
public void systemMessageReceived (Component component, boolean overlay) {}
|
||||
public void playerMessageReceived (PlayerMessage message) {}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package land.chipmunk.chipmunkbot.plugins;
|
||||
|
||||
import land.chipmunk.chipmunkbot.ChipmunkBot;
|
||||
import land.chipmunk.chipmunkbot.command.CommandSource;
|
||||
import land.chipmunk.chipmunkbot.chat.ComponentUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class CommandManager {
|
||||
private ChipmunkBot client;
|
||||
@Getter @Setter private CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
|
||||
@Getter @Setter private String prefix = "'"; // TODO: Don't hardcode this
|
||||
|
||||
public CommandManager (ChipmunkBot client) {
|
||||
this.client = client;
|
||||
}
|
||||
}
|
|
@ -1,14 +1,18 @@
|
|||
package land.chipmunk.chipmunkbot.plugins;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import land.chipmunk.chipmunkbot.Client;
|
||||
import land.chipmunk.chipmunkbot.ChipmunkBot;
|
||||
import land.chipmunk.chipmunkbot.command.CommandSource;
|
||||
import land.chipmunk.chipmunkbot.chat.ComponentUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class CommandManagerPlugin {
|
||||
private Client client;
|
||||
private ChipmunkBot client;
|
||||
@Getter @Setter private CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
|
||||
|
||||
public CommandManagerPlugin (Client client) { this.client = client; }
|
||||
public CommandManagerPlugin (ChipmunkBot client) {
|
||||
this.client = client;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,10 +51,10 @@ public class KaboomChatParser implements SystemChatParser {
|
|||
Component contents = Component.empty();
|
||||
|
||||
if (isSeperatorAt(children, 1)) { // Missing/blank display name
|
||||
if (children.size() >= 2) contents = children.get(1);
|
||||
if (children.size() > 3) contents = children.get(3);
|
||||
} else if (isSeperatorAt(children, 2)) {
|
||||
displayName = children.get(1);
|
||||
if (children.size() >= 3) contents = children.get(2);
|
||||
if (children.size() > 4) contents = children.get(4);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
|
36
src/main/resources/keybinds.json
Normal file
36
src/main/resources/keybinds.json
Normal file
|
@ -0,0 +1,36 @@
|
|||
{
|
||||
"key.attack": "key.mouse.left",
|
||||
"key.use": "key.mouse.right",
|
||||
"key.forward": "key.keyboard.w",
|
||||
"key.left": "key.keyboard.a",
|
||||
"key.back": "key.keyboard.s",
|
||||
"key.right": "key.keyboard.d",
|
||||
"key.jump": "key.keyboard.space",
|
||||
"key.sneak": "key.keyboard.left.shift",
|
||||
"key.sprint": "key.keyboard.left.control",
|
||||
"key.drop": "key.keyboard.q",
|
||||
"key.inventory": "key.keyboard.e",
|
||||
"key.chat": "key.keyboard.t",
|
||||
"key.playerlist": "key.keyboard.tab",
|
||||
"key.pickItem": "key.mouse.middle",
|
||||
"key.command": "key.keyboard.slash",
|
||||
"key.socialInteractions": "key.keyboard.p",
|
||||
"key.screenshot": "key.keyboard.f2",
|
||||
"key.togglePerspective": "key.keyboard.f5",
|
||||
"key.smoothCamera": "key.keyboard.unknown",
|
||||
"key.fullscreen": "key.keyboard.f11",
|
||||
"key.spectatorOutlines": "key.keyboard.unknown",
|
||||
"key.swapOffhand": "key.keyboard.f",
|
||||
"key.saveToolbarActivator": "key.keyboard.c",
|
||||
"key.loadToolbarActivator": "key.keyboard.x",
|
||||
"key.advancements": "key.keyboard.l",
|
||||
"key.hotbar.1": "key.keyboard.1",
|
||||
"key.hotbar.2": "key.keyboard.2",
|
||||
"key.hotbar.3": "key.keyboard.3",
|
||||
"key.hotbar.4": "key.keyboard.4",
|
||||
"key.hotbar.5": "key.keyboard.5",
|
||||
"key.hotbar.6": "key.keyboard.6",
|
||||
"key.hotbar.7": "key.keyboard.7",
|
||||
"key.hotbar.8": "key.keyboard.8",
|
||||
"key.hotbar.9": "key.keyboard.9"
|
||||
}
|
5435
src/main/resources/language.json
Normal file
5435
src/main/resources/language.json
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue