add chatcommands

This commit is contained in:
jane 2021-03-07 13:21:13 -05:00
parent 0ffbbd129b
commit bc0435cee1
8 changed files with 131 additions and 2 deletions

View File

@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.2
# Mod Properties
mod_version = 0.2.0.0-SNAPSHOT
mod_version = 0.2.1.0-SNAPSHOT
maven_group = pm.j4
archives_base_name = kerosene

View File

@ -7,6 +7,7 @@ import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.options.KeyBinding;
import pm.j4.kerosene.modules.ExampleModule;
import pm.j4.kerosene.modules.bindings.BindingManager;
import pm.j4.kerosene.modules.bindings.ChatCommands;
import pm.j4.kerosene.util.config.ConfigHolder;
import pm.j4.kerosene.util.config.ConfigManager;
import pm.j4.kerosene.util.config.Module;
@ -21,6 +22,7 @@ import pm.j4.kerosene.util.module.ModuleFunction;
@Module(ExampleModule.class)
@Module(BindingManager.class)
@Module(ChatCommands.class)
public class KeroseneMod implements ModInitializer {
@Override
public void onInitialize() {

View File

@ -0,0 +1,38 @@
package pm.j4.kerosene.mixin;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import net.minecraft.client.network.ClientPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import pm.j4.kerosene.modules.bindings.ChatCommands;
import pm.j4.kerosene.util.data.ChatCommand;
@Mixin(ClientPlayerEntity.class)
public class ChatMessageMixin {
@Inject(
at = @At("HEAD"),
method = "sendChatMessage",
cancellable = true
)
public void onChatMessage(String message, CallbackInfo ci) {
if(message.startsWith(".")) {
String[] args = message.split(" ");
String command = args[0].substring(1);
Optional<ChatCommand> matchingCommands = ChatCommands.findCommand(command);
List<String> newArgs = new ArrayList<>();
for(int i = 1;i < args.length; i++) {
newArgs.add(args[i]);
}
if (matchingCommands.isPresent()) {
matchingCommands.get().execute(newArgs);
}
ci.cancel();
}
}
}

View File

@ -8,6 +8,7 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;
import pm.j4.kerosene.util.data.ChatCommand;
import pm.j4.kerosene.util.module.ModuleBase;
import pm.j4.kerosene.util.module.ModuleFunction;
import pm.j4.kerosene.util.module.option.BooleanOption;
@ -29,6 +30,26 @@ public class ExampleModule extends ModuleBase {
true);
}
@Override
protected List<ChatCommand> getChatCommands() {
List<ChatCommand> commands = new ArrayList<>();
ChatCommand example = new ChatCommand("test") {
@Override
public void execute(List<String> args) {
System.out.println("EXECUTED TEST COMMAND");
args.forEach(
(arg) -> {
System.out.println("args: " + arg);
}
);
}
};
commands.add(example);
return commands;
}
@Override
public List<ConfigurationOption> getDefaultConfig() {
List<ConfigurationOption> options = new ArrayList<>();

View File

@ -0,0 +1,43 @@
package pm.j4.kerosene.modules.bindings;
import java.util.*;
import java.util.stream.Collectors;
import pm.j4.kerosene.util.data.ChatCommand;
import pm.j4.kerosene.util.module.ModuleBase;
public class ChatCommands extends ModuleBase {
/**
* Instantiates a new Module base.
* Parameters should be constant across restarts.
*/
public ChatCommands() {
super("kerosene",
"kerosene.chatcommands",
"kerosene.misc",
false,
true,
false);
}
private static Map<String, ChatCommand> commands = new HashMap<>();
public static void registerCommands(List<ChatCommand> commandList) {
for(ChatCommand command : commandList) {
if(commands.containsKey(command.getName())) {
return;
}
System.out.println("REGISTERING COMMAND " + command.getName());
commands.put(command.getName(), command);
}
}
public static Optional<ChatCommand> findCommand(String text) {
if (commands.containsKey(text)) {
return Optional.of(commands.get(text));
}
else {
System.out.println("COULD NOT FIND CHAT COMMAND WITH NAME " + text);
return Optional.empty();
}
}
}

View File

@ -0,0 +1,17 @@
package pm.j4.kerosene.util.data;
import java.util.List;
public abstract class ChatCommand {
public ChatCommand(String name) {
this.name = name;
}
private final String name;
public String getName() {
return name;
}
public abstract void execute(List<String> args);
}

View File

@ -9,8 +9,10 @@ import net.minecraft.text.TranslatableText;
import pm.j4.kerosene.gui.PModuleConfigEntry;
import pm.j4.kerosene.gui.PModuleConfigPane;
import pm.j4.kerosene.modules.bindings.BindingManager;
import pm.j4.kerosene.modules.bindings.ChatCommands;
import pm.j4.kerosene.util.config.ConfigHolder;
import pm.j4.kerosene.util.config.ConfigManager;
import pm.j4.kerosene.util.data.ChatCommand;
import pm.j4.kerosene.util.module.option.ConfigurationOption;
/**
@ -45,6 +47,7 @@ public abstract class ModuleBase {
*/
public void init() {
BindingManager.registerBindings(this);
ChatCommands.registerCommands(this.getChatCommands());
}
/**
@ -253,6 +256,10 @@ public abstract class ModuleBase {
return Optional.empty();
}
protected List<ChatCommand> getChatCommands() {
return new ArrayList<>();
};
private Map<KeyBinding, ModuleFunction> attachedBinds = new HashMap<>();
public Map<KeyBinding, ModuleFunction> getDefaultBindings() {

View File

@ -7,7 +7,8 @@
],
"client": [
"EntryListWidgetAccessor",
"OptionsMenuMixin"
"OptionsMenuMixin",
"ChatMessageMixin"
],
"injectors": {
"defaultRequire": 1