Add an onSendChatMessage event

This commit is contained in:
Brady 2018-08-01 18:46:51 -07:00
parent 28c4636857
commit a36b223aab
No known key found for this signature in database
GPG Key ID: 73A788379A197567
6 changed files with 100 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package baritone.bot; package baritone.bot;
import baritone.bot.event.IGameEventListener; import baritone.bot.event.IGameEventListener;
import baritone.bot.event.events.ChatEvent;
import net.minecraft.client.settings.KeyBinding; import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
@ -13,9 +14,7 @@ public final class GameEventHandler implements IGameEventListener {
GameEventHandler() {} GameEventHandler() {}
@Override @Override
public final void onTick() { public final void onTick() {}
baritone.Baritone.onTick();
}
@Override @Override
public void onProcessKeyBinds() { public void onProcessKeyBinds() {
@ -33,4 +32,7 @@ public final class GameEventHandler implements IGameEventListener {
} }
} }
} }
@Override
public void onSendChatMessage(ChatEvent event) {}
} }

View File

@ -0,0 +1,28 @@
package baritone.bot.event;
import baritone.bot.event.events.ChatEvent;
/**
* An implementation of {@link IGameEventListener} that has all methods
* overriden with empty bodies, allowing inheritors of this class to choose
* which events they would like to listen in on.
*
* Has no implementors at the moment, but will likely be used with the
* manager/behavior system is ported over to the new system.
*
* @see IGameEventListener
*
* @author Brady
* @since 8/1/2018 6:29 PM
*/
public interface AbstractGameEventListener extends IGameEventListener {
@Override
default void onTick() {}
@Override
default void onProcessKeyBinds() {}
@Override
default void onSendChatMessage(ChatEvent event) {}
}

View File

@ -1,6 +1,8 @@
package baritone.bot.event; package baritone.bot.event;
import baritone.bot.event.events.ChatEvent;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
/** /**
* @author Brady * @author Brady
@ -17,4 +19,9 @@ public interface IGameEventListener {
* Run once per game tick from {@link Minecraft#processKeyBinds()} * Run once per game tick from {@link Minecraft#processKeyBinds()}
*/ */
void onProcessKeyBinds(); void onProcessKeyBinds();
/**
* Runs whenever the client player sends a message to the server {@link EntityPlayerSP#sendChatMessage(String)}
*/
void onSendChatMessage(ChatEvent event);
} }

View File

@ -0,0 +1,26 @@
package baritone.bot.event.events;
import baritone.bot.event.events.type.Cancellable;
/**
* @author Brady
* @since 8/1/2018 6:39 PM
*/
public final class ChatEvent extends Cancellable {
/**
* The message being sent
*/
private final String message;
public ChatEvent(String message) {
this.message = message;
}
/**
* @return The message being sent
*/
public final String getMessage() {
return this.message;
}
}

View File

@ -0,0 +1,27 @@
package baritone.bot.event.events.type;
/**
* @author Brady
* @since 8/1/2018 6:41 PM
*/
public class Cancellable {
/**
* Whether or not this event has been cancelled
*/
private boolean cancelled;
/**
* Cancels this event
*/
public final void cancel() {
this.cancelled = true;
}
/**
* @return Whether or not this event has been cancelled
*/
public final boolean isCancelled() {
return this.cancelled;
}
}

View File

@ -1,14 +1,13 @@
package baritone.launch.mixins; package baritone.launch.mixins;
import baritone.util.ChatCommand; import baritone.bot.Baritone;
import baritone.bot.event.events.ChatEvent;
import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.entity.EntityPlayerSP;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.lang.reflect.InvocationTargetException;
/** /**
* @author Brady * @author Brady
* @since 8/1/2018 5:06 PM * @since 8/1/2018 5:06 PM
@ -22,11 +21,9 @@ public class MixinEntityPlayerSP {
cancellable = true cancellable = true
) )
private void sendChatMessage(String msg, CallbackInfo ci) { private void sendChatMessage(String msg, CallbackInfo ci) {
try { ChatEvent event = new ChatEvent(msg);
if (ChatCommand.message(msg)) Baritone.INSTANCE.getGameEventHandler().onSendChatMessage(event);
if (event.isCancelled())
ci.cancel(); ci.cancel();
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
} }
} }