add chatcommands
This commit is contained in:
		
							parent
							
								
									0ffbbd129b
								
							
						
					
					
						commit
						bc0435cee1
					
				
					 8 changed files with 131 additions and 2 deletions
				
			
		|  | @ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G | ||||||
| 	loader_version=0.11.2 | 	loader_version=0.11.2 | ||||||
| 
 | 
 | ||||||
| # Mod Properties | # Mod Properties | ||||||
| 	mod_version = 0.2.0.0-SNAPSHOT | 	mod_version = 0.2.1.0-SNAPSHOT | ||||||
| 	maven_group = pm.j4 | 	maven_group = pm.j4 | ||||||
| 	archives_base_name = kerosene | 	archives_base_name = kerosene | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -7,6 +7,7 @@ import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; | ||||||
| import net.minecraft.client.options.KeyBinding; | import net.minecraft.client.options.KeyBinding; | ||||||
| import pm.j4.kerosene.modules.ExampleModule; | import pm.j4.kerosene.modules.ExampleModule; | ||||||
| import pm.j4.kerosene.modules.bindings.BindingManager; | 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.ConfigHolder; | ||||||
| import pm.j4.kerosene.util.config.ConfigManager; | import pm.j4.kerosene.util.config.ConfigManager; | ||||||
| import pm.j4.kerosene.util.config.Module; | import pm.j4.kerosene.util.config.Module; | ||||||
|  | @ -21,6 +22,7 @@ import pm.j4.kerosene.util.module.ModuleFunction; | ||||||
| 
 | 
 | ||||||
| @Module(ExampleModule.class) | @Module(ExampleModule.class) | ||||||
| @Module(BindingManager.class) | @Module(BindingManager.class) | ||||||
|  | @Module(ChatCommands.class) | ||||||
| public class KeroseneMod implements ModInitializer { | public class KeroseneMod implements ModInitializer { | ||||||
| 	@Override | 	@Override | ||||||
| 	public void onInitialize() { | 	public void onInitialize() { | ||||||
|  |  | ||||||
							
								
								
									
										38
									
								
								src/main/java/pm/j4/kerosene/mixin/ChatMessageMixin.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/main/java/pm/j4/kerosene/mixin/ChatMessageMixin.java
									
										
									
									
									
										Normal 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(); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | @ -8,6 +8,7 @@ import net.minecraft.client.MinecraftClient; | ||||||
| import net.minecraft.client.options.KeyBinding; | import net.minecraft.client.options.KeyBinding; | ||||||
| import net.minecraft.client.util.InputUtil; | import net.minecraft.client.util.InputUtil; | ||||||
| import org.lwjgl.glfw.GLFW; | 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.ModuleBase; | ||||||
| import pm.j4.kerosene.util.module.ModuleFunction; | import pm.j4.kerosene.util.module.ModuleFunction; | ||||||
| import pm.j4.kerosene.util.module.option.BooleanOption; | import pm.j4.kerosene.util.module.option.BooleanOption; | ||||||
|  | @ -29,6 +30,26 @@ public class ExampleModule extends ModuleBase { | ||||||
| 				true); | 				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 | 	@Override | ||||||
| 	public List<ConfigurationOption> getDefaultConfig() { | 	public List<ConfigurationOption> getDefaultConfig() { | ||||||
| 		List<ConfigurationOption> options = new ArrayList<>(); | 		List<ConfigurationOption> options = new ArrayList<>(); | ||||||
|  |  | ||||||
|  | @ -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(); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										17
									
								
								src/main/java/pm/j4/kerosene/util/data/ChatCommand.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/main/java/pm/j4/kerosene/util/data/ChatCommand.java
									
										
									
									
									
										Normal 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); | ||||||
|  | } | ||||||
|  | @ -9,8 +9,10 @@ import net.minecraft.text.TranslatableText; | ||||||
| import pm.j4.kerosene.gui.PModuleConfigEntry; | import pm.j4.kerosene.gui.PModuleConfigEntry; | ||||||
| import pm.j4.kerosene.gui.PModuleConfigPane; | import pm.j4.kerosene.gui.PModuleConfigPane; | ||||||
| import pm.j4.kerosene.modules.bindings.BindingManager; | 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.ConfigHolder; | ||||||
| import pm.j4.kerosene.util.config.ConfigManager; | import pm.j4.kerosene.util.config.ConfigManager; | ||||||
|  | import pm.j4.kerosene.util.data.ChatCommand; | ||||||
| import pm.j4.kerosene.util.module.option.ConfigurationOption; | import pm.j4.kerosene.util.module.option.ConfigurationOption; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  | @ -45,6 +47,7 @@ public abstract class ModuleBase { | ||||||
| 	 */ | 	 */ | ||||||
| 	public void init() { | 	public void init() { | ||||||
| 		BindingManager.registerBindings(this); | 		BindingManager.registerBindings(this); | ||||||
|  | 		ChatCommands.registerCommands(this.getChatCommands()); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	/** | 	/** | ||||||
|  | @ -253,6 +256,10 @@ public abstract class ModuleBase { | ||||||
| 		return Optional.empty(); | 		return Optional.empty(); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	protected List<ChatCommand> getChatCommands() { | ||||||
|  | 		return new ArrayList<>(); | ||||||
|  | 	}; | ||||||
|  | 
 | ||||||
| 	private Map<KeyBinding, ModuleFunction> attachedBinds = new HashMap<>(); | 	private Map<KeyBinding, ModuleFunction> attachedBinds = new HashMap<>(); | ||||||
| 
 | 
 | ||||||
| 	public Map<KeyBinding, ModuleFunction> getDefaultBindings() { | 	public Map<KeyBinding, ModuleFunction> getDefaultBindings() { | ||||||
|  |  | ||||||
|  | @ -7,7 +7,8 @@ | ||||||
|   ], |   ], | ||||||
|   "client": [ |   "client": [ | ||||||
|     "EntryListWidgetAccessor", |     "EntryListWidgetAccessor", | ||||||
|     "OptionsMenuMixin" |     "OptionsMenuMixin", | ||||||
|  |     "ChatMessageMixin" | ||||||
|   ], |   ], | ||||||
|   "injectors": { |   "injectors": { | ||||||
|     "defaultRequire": 1 |     "defaultRequire": 1 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue