Skip to content
This repository was archived by the owner on Feb 19, 2019. It is now read-only.

Commit cd1a587

Browse files
committed
Implement #22
1 parent 0e02d25 commit cd1a587

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/example/java/me/zero/example/command/ExampleCommandManager.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ public final void load() {
5353
handler.registerParser(new OptionalParser());
5454
handler.registerParser(new StringParser());
5555

56-
// Subscribe the handler and a chat command listener to the event bus
57-
ClientAPI.EVENT_BUS.subscribe(
58-
handler,
59-
new ChatCommandListener(handler)
60-
);
56+
// Create a new chat command listener so chat commands can be passed through chat
57+
ClientAPI.EVENT_BUS.subscribe(new ChatCommandListener(handler));
6158
}
6259

6360
@Override

src/main/java/clientapi/command/handler/CommandHandler.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@
1616

1717
package clientapi.command.handler;
1818

19+
import clientapi.ClientAPI;
1920
import clientapi.command.Cmd;
2021
import clientapi.command.Command;
2122
import clientapi.command.exception.CommandException;
2223
import clientapi.command.exception.UnknownCommandException;
2324
import clientapi.command.exception.handler.ExceptionHandler;
2425
import clientapi.command.executor.CommandExecutor;
2526
import clientapi.command.executor.argument.ArgumentParser;
27+
import clientapi.event.defaults.game.core.KeyEvent;
2628
import clientapi.event.defaults.internal.CommandExecutionEvent;
2729
import clientapi.manage.Manager;
30+
import clientapi.util.interfaces.Helper;
2831
import me.zero.alpine.listener.EventHandler;
2932
import me.zero.alpine.listener.Listener;
33+
import net.minecraft.client.gui.GuiChat;
3034

3135
import java.lang.reflect.Type;
3236
import java.util.ArrayList;
@@ -42,7 +46,7 @@
4246
* @author Brady
4347
* @since 6/1/2017 3:03 PM
4448
*/
45-
public final class CommandHandler {
49+
public final class CommandHandler implements Helper {
4650

4751
/**
4852
* Handlers to process command exceptions
@@ -74,10 +78,27 @@ public final class CommandHandler {
7478
*/
7579
private String prefix = ".";
7680

81+
/**
82+
* Whether or not typing the prefix char should open the in-game chat gui
83+
*/
84+
private boolean openChat = false;
85+
7786
public CommandHandler(Manager<Command> commandManager) {
7887
this.commandManager = commandManager;
88+
ClientAPI.EVENT_BUS.subscribe(this);
7989
}
8090

91+
@EventHandler
92+
private final Listener<KeyEvent> keyEventListener = new Listener<>(event -> {
93+
// Check if opening chat with the prefix is supported
94+
if (!openChat || prefix == null)
95+
return;
96+
97+
// Ensure the prefix is a single character
98+
if (prefix.length() == 1 && prefix.charAt(0) == event.getCharacter())
99+
mc.displayGuiScreen(new GuiChat(prefix));
100+
});
101+
81102
@EventHandler
82103
private final Listener<CommandExecutionEvent> commandExecutionListener = new Listener<>(event -> {
83104
try {
@@ -87,7 +108,7 @@ public CommandHandler(Manager<Command> commandManager) {
87108
}
88109
throw new UnknownCommandException();
89110
} catch (CommandException e) {
90-
List<ExceptionHandler> handlers = findHandlers(e);
111+
List<ExceptionHandler> handlers = getExceptionHandlers(e);
91112
if (handlers.isEmpty()) {
92113
e.printStackTrace();
93114
return;
@@ -103,7 +124,7 @@ public CommandHandler(Manager<Command> commandManager) {
103124
* @param exception The command exception
104125
* @return The list of handlers, empty if none
105126
*/
106-
private List<ExceptionHandler> findHandlers(CommandException exception) {
127+
private List<ExceptionHandler> getExceptionHandlers(CommandException exception) {
107128
return handlers.stream().filter(handler -> handler.isTarget(exception.getClass())).collect(Collectors.toList());
108129
}
109130

@@ -180,4 +201,20 @@ public final void setPrefix(String prefix) {
180201
public final String getPrefix() {
181202
return this.prefix;
182203
}
204+
205+
/**
206+
* Sets whether or not typing the prefix char should open the in-game chat gui
207+
*
208+
* @param openChat The new state
209+
*/
210+
public final void setPrefixOpenChat(boolean openChat) {
211+
this.openChat = openChat;
212+
}
213+
214+
/**
215+
* @return Whether or not typing the prefix char should open the in-game chat gui
216+
*/
217+
public final boolean doesPrefixOpenChat() {
218+
return this.openChat;
219+
}
183220
}

0 commit comments

Comments
 (0)