Backport anti-poz from 1.13.2 branch to 1.12.2/master

This commit is contained in:
Brady 2019-10-09 21:29:48 -05:00
parent ce2fc6f251
commit 42306055ee
No known key found for this signature in database
GPG Key ID: 73A788379A197567
10 changed files with 21 additions and 166 deletions

View File

@ -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<String> prefix;
public final Overrideable<String[]> 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;
}
}

View File

@ -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) {}

View File

@ -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.

View File

@ -26,14 +26,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(GuiChat.ChatTabCompleter.class)
public abstract class MixinChatTabCompleter extends MixinTabCompleter {
@Inject(
method = "<init>*",
at = @At("RETURN")
)
private void onConstruction(CallbackInfo ci) {
isChatCompleter = true;
}
@Inject(
method = "complete",
at = @At("HEAD"),

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}
}

View File

@ -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;
}
}

View File

@ -19,7 +19,6 @@
"MixinEntityLivingBase",
"MixinEntityPlayerSP",
"MixinEntityRenderer",
"MixinGuiChat",
"MixinGuiScreen",
"MixinItemStack",
"MixinMinecraft",

View File

@ -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<String> tabComplete(String msg) {

View File

@ -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();

View File

@ -1,10 +0,0 @@
package baritone.utils.accessor;
public interface ITabCompleter {
String getPrefix();
void setPrefix(String prefix);
boolean onGuiChatSetCompletions(String[] newCompl);
}