From 9d41854e2248def91063ace0e7d271fa5d68f6fb Mon Sep 17 00:00:00 2001 From: KennyTV Date: Sat, 19 Jun 2021 11:18:33 +0200 Subject: [PATCH] Hotfix 1.13->1.12 command suggestions Fixes #368 --- .../packets/PlayerPacket1_13.java | 42 ++++++++++--------- .../storage/TabCompleteStorage.java | 42 ++++++++++++++++--- 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java index 26f7d35d..094ab002 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java @@ -187,7 +187,7 @@ public class PlayerPacket1_13 extends RewriterBase { UUID uuid = packetWrapper.passthrough(Type.UUID); if (action == 0) { // Add String name = packetWrapper.passthrough(Type.STRING); - storage.usernames.put(uuid, name); + storage.usernames().put(uuid, name); int nProperties = packetWrapper.passthrough(Type.VAR_INT); for (int j = 0; j < nProperties; j++) { packetWrapper.passthrough(Type.STRING); @@ -210,7 +210,7 @@ public class PlayerPacket1_13 extends RewriterBase { packetWrapper.passthrough(Type.COMPONENT); } } else if (action == 4) { // Remove Player - storage.usernames.remove(uuid); + storage.usernames().remove(uuid); } } } @@ -313,11 +313,12 @@ public class PlayerPacket1_13 extends RewriterBase { TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class); - if (!storage.commands.isEmpty()) { - storage.commands.clear(); + if (!storage.commands().isEmpty()) { + storage.commands().clear(); } int size = wrapper.read(Type.VAR_INT); + boolean initialNodes = true; for (int i = 0; i < size; i++) { byte flags = wrapper.read(Type.BYTE); wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); // Children indices @@ -326,11 +327,14 @@ public class PlayerPacket1_13 extends RewriterBase { } byte nodeType = (byte) (flags & 0x03); + if (initialNodes && nodeType == 2) { + initialNodes = false; + } + if (nodeType == 1 || nodeType == 2) { // Literal/argument node String name = wrapper.read(Type.STRING); - - if (nodeType == 1) { - storage.commands.add('/' + name); + if (nodeType == 1 && initialNodes) { + storage.commands().add('/' + name); } } @@ -353,25 +357,25 @@ public class PlayerPacket1_13 extends RewriterBase { @Override public void handle(PacketWrapper wrapper) throws Exception { TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class); - if (storage.lastRequest == null) { + if (storage.lastRequest() == null) { wrapper.cancel(); return; } - if (storage.lastId != wrapper.read(Type.VAR_INT)) wrapper.cancel(); + if (storage.lastId() != wrapper.read(Type.VAR_INT)) wrapper.cancel(); int start = wrapper.read(Type.VAR_INT); int length = wrapper.read(Type.VAR_INT); - int lastRequestPartIndex = storage.lastRequest.lastIndexOf(' ') + 1; + int lastRequestPartIndex = storage.lastRequest().lastIndexOf(' ') + 1; if (lastRequestPartIndex != start) wrapper.cancel(); // Client only replaces after space - if (length != storage.lastRequest.length() - lastRequestPartIndex) { + if (length != storage.lastRequest().length() - lastRequestPartIndex) { wrapper.cancel(); // We can't set the length in previous versions } int count = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < count; i++) { String match = wrapper.read(Type.STRING); - wrapper.write(Type.STRING, (start == 0 && !storage.lastAssumeCommand ? "/" : "") + match); + wrapper.write(Type.STRING, (start == 0 && !storage.isLastAssumeCommand() ? "/" : "") + match); // Ignore tooltip if (wrapper.read(Type.BOOLEAN)) { wrapper.read(Type.STRING); @@ -396,14 +400,14 @@ public class PlayerPacket1_13 extends RewriterBase { if (!assumeCommand && !command.startsWith("/")) { // Complete usernames for non-commands String buffer = command.substring(command.lastIndexOf(' ') + 1); - for (String value : storage.usernames.values()) { + for (String value : storage.usernames().values()) { if (startsWithIgnoreCase(value, buffer)) { suggestions.add(value); } } - } else if (!storage.commands.isEmpty() && !command.contains(" ")) { + } else if (!storage.commands().isEmpty() && !command.contains(" ")) { // Complete commands names with values from 'Declare Commands' packet - for (String value : storage.commands) { + for (String value : storage.commands()) { if (startsWithIgnoreCase(value, command)) { suggestions.add(value); } @@ -418,7 +422,7 @@ public class PlayerPacket1_13 extends RewriterBase { response.write(Type.STRING, value); } response.scheduleSend(Protocol1_12_2To1_13.class); - storage.lastRequest = null; + storage.setLastRequest(null); return; } @@ -430,9 +434,9 @@ public class PlayerPacket1_13 extends RewriterBase { wrapper.write(Type.VAR_INT, id); wrapper.write(Type.STRING, command); - storage.lastId = id; - storage.lastAssumeCommand = assumeCommand; - storage.lastRequest = command; + storage.setLastId(id); + storage.setLastAssumeCommand(assumeCommand); + storage.setLastRequest(command); }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/TabCompleteStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/TabCompleteStorage.java index e675d07f..5dac40ba 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/TabCompleteStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/TabCompleteStorage.java @@ -26,9 +26,41 @@ import java.util.Set; import java.util.UUID; public class TabCompleteStorage implements StorableObject { - public int lastId; - public String lastRequest; - public boolean lastAssumeCommand; - public Map usernames = new HashMap<>(); - public Set commands = new HashSet<>(); + private final Map usernames = new HashMap<>(); + private final Set commands = new HashSet<>(); + private int lastId; + private String lastRequest; + private boolean lastAssumeCommand; + + public Map usernames() { + return usernames; + } + + public Set commands() { + return commands; + } + + public int lastId() { + return lastId; + } + + public void setLastId(final int lastId) { + this.lastId = lastId; + } + + public String lastRequest() { + return lastRequest; + } + + public void setLastRequest(String lastRequest) { + this.lastRequest = lastRequest; + } + + public boolean isLastAssumeCommand() { + return lastAssumeCommand; + } + + public void setLastAssumeCommand(boolean lastAssumeCommand) { + this.lastAssumeCommand = lastAssumeCommand; + } }