From 42306055eeb71157fa07b9129d14b212d634771b Mon Sep 17 00:00:00 2001 From: Brady Date: Wed, 9 Oct 2019 21:29:48 -0500 Subject: [PATCH] Backport anti-poz from 1.13.2 branch to 1.12.2/master --- .../api/event/events/TabCompleteEvent.java | 30 ++------- .../listener/AbstractGameEventListener.java | 5 +- .../event/listener/IGameEventListener.java | 10 +-- .../launch/mixins/MixinChatTabCompleter.java | 8 --- .../baritone/launch/mixins/MixinGuiChat.java | 45 ------------- .../launch/mixins/MixinTabCompleter.java | 65 +++---------------- src/launch/resources/mixins.baritone.json | 1 - .../baritone/command/BaritoneChatControl.java | 6 +- .../java/baritone/event/GameEventHandler.java | 7 +- .../utils/accessor/ITabCompleter.java | 10 --- 10 files changed, 21 insertions(+), 166 deletions(-) delete mode 100644 src/launch/java/baritone/launch/mixins/MixinGuiChat.java delete mode 100644 src/main/java/baritone/utils/accessor/ITabCompleter.java diff --git a/src/api/java/baritone/api/event/events/TabCompleteEvent.java b/src/api/java/baritone/api/event/events/TabCompleteEvent.java index fea1da29..cf4c57a2 100644 --- a/src/api/java/baritone/api/event/events/TabCompleteEvent.java +++ b/src/api/java/baritone/api/event/events/TabCompleteEvent.java @@ -23,31 +23,13 @@ import baritone.api.event.events.type.Overrideable; /** * @author LoganDark */ -public abstract class TabCompleteEvent extends Cancellable { +public class TabCompleteEvent extends Cancellable { - public final Overrideable prefix; - public final Overrideable completions; + public final String prefix; + public String[] completions; - TabCompleteEvent(String prefix, String[] completions) { - this.prefix = new Overrideable<>(prefix); - this.completions = new Overrideable<>(completions); - } - - public boolean wasModified() { - return prefix.wasModified() || completions.wasModified(); - } - - public static final class Pre extends TabCompleteEvent { - - public Pre(String prefix) { - super(prefix, null); - } - } - - public static final class Post extends TabCompleteEvent { - - public Post(String prefix, String[] completions) { - super(prefix, completions); - } + public TabCompleteEvent(String prefix) { + this.prefix = prefix; + this.completions = null; } } diff --git a/src/api/java/baritone/api/event/listener/AbstractGameEventListener.java b/src/api/java/baritone/api/event/listener/AbstractGameEventListener.java index e7d4dc29..9eac8de4 100644 --- a/src/api/java/baritone/api/event/listener/AbstractGameEventListener.java +++ b/src/api/java/baritone/api/event/listener/AbstractGameEventListener.java @@ -40,10 +40,7 @@ public interface AbstractGameEventListener extends IGameEventListener { default void onSendChatMessage(ChatEvent event) {} @Override - default void onPreTabComplete(TabCompleteEvent.Pre event) {} - - @Override - default void onPostTabComplete(TabCompleteEvent.Post event) {} + default void onPreTabComplete(TabCompleteEvent event) {} @Override default void onChunkEvent(ChunkEvent event) {} diff --git a/src/api/java/baritone/api/event/listener/IGameEventListener.java b/src/api/java/baritone/api/event/listener/IGameEventListener.java index b7522125..b074e978 100644 --- a/src/api/java/baritone/api/event/listener/IGameEventListener.java +++ b/src/api/java/baritone/api/event/listener/IGameEventListener.java @@ -62,15 +62,7 @@ public interface IGameEventListener { * * @param event The event */ - void onPreTabComplete(TabCompleteEvent.Pre event); - - /** - * Runs whenever the client player tries to tab complete in chat once completions have been recieved from the - * server. This will only be called if the {@link TabCompleteEvent#cancel()} method was not called. - * - * @param event The event - */ - void onPostTabComplete(TabCompleteEvent.Post event); + void onPreTabComplete(TabCompleteEvent event); /** * Runs before and after whenever a chunk is either loaded, unloaded, or populated. diff --git a/src/launch/java/baritone/launch/mixins/MixinChatTabCompleter.java b/src/launch/java/baritone/launch/mixins/MixinChatTabCompleter.java index 797b6210..f8e4d789 100644 --- a/src/launch/java/baritone/launch/mixins/MixinChatTabCompleter.java +++ b/src/launch/java/baritone/launch/mixins/MixinChatTabCompleter.java @@ -26,14 +26,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(GuiChat.ChatTabCompleter.class) public abstract class MixinChatTabCompleter extends MixinTabCompleter { - @Inject( - method = "*", - at = @At("RETURN") - ) - private void onConstruction(CallbackInfo ci) { - isChatCompleter = true; - } - @Inject( method = "complete", at = @At("HEAD"), diff --git a/src/launch/java/baritone/launch/mixins/MixinGuiChat.java b/src/launch/java/baritone/launch/mixins/MixinGuiChat.java deleted file mode 100644 index 9883c176..00000000 --- a/src/launch/java/baritone/launch/mixins/MixinGuiChat.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * This file is part of Baritone. - * - * Baritone is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Baritone is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Baritone. If not, see . - */ - -package baritone.launch.mixins; - -import baritone.utils.accessor.ITabCompleter; -import net.minecraft.client.gui.GuiChat; -import net.minecraft.util.TabCompleter; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(GuiChat.class) -public abstract class MixinGuiChat implements net.minecraft.util.ITabCompleter { - - @Shadow - private TabCompleter tabCompleter; - - @Inject( - method = "setCompletions", - at = @At("HEAD"), - cancellable = true - ) - private void onSetCompletions(String[] newCompl, CallbackInfo ci) { - if (((ITabCompleter) tabCompleter).onGuiChatSetCompletions(newCompl)) { - ci.cancel(); - } - } -} diff --git a/src/launch/java/baritone/launch/mixins/MixinTabCompleter.java b/src/launch/java/baritone/launch/mixins/MixinTabCompleter.java index cdbae3b4..7f9a66fb 100644 --- a/src/launch/java/baritone/launch/mixins/MixinTabCompleter.java +++ b/src/launch/java/baritone/launch/mixins/MixinTabCompleter.java @@ -20,7 +20,7 @@ package baritone.launch.mixins; import baritone.api.BaritoneAPI; import baritone.api.IBaritone; import baritone.api.event.events.TabCompleteEvent; -import baritone.utils.accessor.ITabCompleter; +import net.minecraft.client.gui.GuiChat; import net.minecraft.client.gui.GuiTextField; import net.minecraft.util.TabCompleter; import org.spongepowered.asm.mixin.Final; @@ -32,7 +32,7 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(TabCompleter.class) -public abstract class MixinTabCompleter implements ITabCompleter { +public abstract class MixinTabCompleter { @Shadow @Final @@ -44,36 +44,22 @@ public abstract class MixinTabCompleter implements ITabCompleter { @Shadow public abstract void setCompletions(String... newCompl); - @Unique - protected boolean isChatCompleter = false; - @Unique protected boolean dontComplete = false; - @Override - public String getPrefix() { - return textField.getText().substring(0, textField.getCursorPosition()); - } - - @Override - public void setPrefix(String prefix) { - textField.setText(prefix + textField.getText().substring(textField.getCursorPosition())); - textField.setCursorPosition(prefix.length()); - } - @Inject( method = "requestCompletions", at = @At("HEAD"), cancellable = true ) private void onRequestCompletions(String prefix, CallbackInfo ci) { - if (!isChatCompleter) { + if (!((Object) this instanceof GuiChat.ChatTabCompleter)) { return; } IBaritone baritone = BaritoneAPI.getProvider().getPrimaryBaritone(); - TabCompleteEvent.Pre event = new TabCompleteEvent.Pre(prefix); + TabCompleteEvent event = new TabCompleteEvent(prefix); baritone.getGameEventHandler().onPreTabComplete(event); if (event.isCancelled()) { @@ -81,50 +67,17 @@ public abstract class MixinTabCompleter implements ITabCompleter { return; } - if (event.prefix.wasModified()) { - setPrefix(event.prefix.get()); - } - - if (event.completions.wasModified()) { + if (event.completions != null) { ci.cancel(); - dontComplete = true; + this.dontComplete = true; try { - requestedCompletions = true; - setCompletions(event.completions.get()); + this.requestedCompletions = true; + setCompletions(event.completions); } finally { - dontComplete = false; + this.dontComplete = false; } } } - - @Override - public boolean onGuiChatSetCompletions(String[] newCompl) { - IBaritone baritone = BaritoneAPI.getProvider().getPrimaryBaritone(); - - if (baritone == null) { - return false; - } - - TabCompleteEvent.Post event = new TabCompleteEvent.Post(getPrefix(), newCompl); - baritone.getGameEventHandler().onPostTabComplete(event); - - if (event.isCancelled()) { - return true; - } - - if (event.prefix.wasModified()) { - String prefix = event.prefix.get(); - textField.setText(prefix + textField.getText().substring(textField.getCursorPosition())); - textField.setCursorPosition(prefix.length()); - } - - if (event.completions.wasModified()) { - setCompletions(event.completions.get()); - return true; - } - - return false; - } } diff --git a/src/launch/resources/mixins.baritone.json b/src/launch/resources/mixins.baritone.json index 82c1dd7f..eb31a2e7 100644 --- a/src/launch/resources/mixins.baritone.json +++ b/src/launch/resources/mixins.baritone.json @@ -19,7 +19,6 @@ "MixinEntityLivingBase", "MixinEntityPlayerSP", "MixinEntityRenderer", - "MixinGuiChat", "MixinGuiScreen", "MixinItemStack", "MixinMinecraft", diff --git a/src/main/java/baritone/command/BaritoneChatControl.java b/src/main/java/baritone/command/BaritoneChatControl.java index ad7e18e4..c41c682c 100644 --- a/src/main/java/baritone/command/BaritoneChatControl.java +++ b/src/main/java/baritone/command/BaritoneChatControl.java @@ -146,11 +146,11 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener { } @Override - public void onPreTabComplete(TabCompleteEvent.Pre event) { + public void onPreTabComplete(TabCompleteEvent event) { if (!settings.prefixControl.value) { return; } - String prefix = event.prefix.get(); + String prefix = event.prefix; String commandPrefix = settings.prefix.value; if (!prefix.startsWith(commandPrefix)) { return; @@ -161,7 +161,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener { if (args.size() == 1) { stream = stream.map(x -> commandPrefix + x); } - event.completions.set(stream.toArray(String[]::new)); + event.completions = stream.toArray(String[]::new); } public Stream tabComplete(String msg) { diff --git a/src/main/java/baritone/event/GameEventHandler.java b/src/main/java/baritone/event/GameEventHandler.java index 76ae0b4e..8916f7f3 100644 --- a/src/main/java/baritone/event/GameEventHandler.java +++ b/src/main/java/baritone/event/GameEventHandler.java @@ -70,15 +70,10 @@ public final class GameEventHandler implements IEventBus, Helper { } @Override - public void onPreTabComplete(TabCompleteEvent.Pre event) { + public void onPreTabComplete(TabCompleteEvent event) { listeners.forEach(l -> l.onPreTabComplete(event)); } - @Override - public void onPostTabComplete(TabCompleteEvent.Post event) { - listeners.forEach(l -> l.onPostTabComplete(event)); - } - @Override public final void onChunkEvent(ChunkEvent event) { EventState state = event.getState(); diff --git a/src/main/java/baritone/utils/accessor/ITabCompleter.java b/src/main/java/baritone/utils/accessor/ITabCompleter.java deleted file mode 100644 index e187c3e6..00000000 --- a/src/main/java/baritone/utils/accessor/ITabCompleter.java +++ /dev/null @@ -1,10 +0,0 @@ -package baritone.utils.accessor; - -public interface ITabCompleter { - - String getPrefix(); - - void setPrefix(String prefix); - - boolean onGuiChatSetCompletions(String[] newCompl); -}