Hotfix 1.13->1.12 command suggestions

Fixes #368
This commit is contained in:
KennyTV 2021-06-19 11:18:33 +02:00
parent 00bc4a9d6a
commit 9d41854e22
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
2 changed files with 60 additions and 24 deletions

View File

@ -187,7 +187,7 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
UUID uuid = packetWrapper.passthrough(Type.UUID); UUID uuid = packetWrapper.passthrough(Type.UUID);
if (action == 0) { // Add if (action == 0) { // Add
String name = packetWrapper.passthrough(Type.STRING); String name = packetWrapper.passthrough(Type.STRING);
storage.usernames.put(uuid, name); storage.usernames().put(uuid, name);
int nProperties = packetWrapper.passthrough(Type.VAR_INT); int nProperties = packetWrapper.passthrough(Type.VAR_INT);
for (int j = 0; j < nProperties; j++) { for (int j = 0; j < nProperties; j++) {
packetWrapper.passthrough(Type.STRING); packetWrapper.passthrough(Type.STRING);
@ -210,7 +210,7 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
packetWrapper.passthrough(Type.COMPONENT); packetWrapper.passthrough(Type.COMPONENT);
} }
} else if (action == 4) { // Remove Player } else if (action == 4) { // Remove Player
storage.usernames.remove(uuid); storage.usernames().remove(uuid);
} }
} }
} }
@ -313,11 +313,12 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class); TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class);
if (!storage.commands.isEmpty()) { if (!storage.commands().isEmpty()) {
storage.commands.clear(); storage.commands().clear();
} }
int size = wrapper.read(Type.VAR_INT); int size = wrapper.read(Type.VAR_INT);
boolean initialNodes = true;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
byte flags = wrapper.read(Type.BYTE); byte flags = wrapper.read(Type.BYTE);
wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); // Children indices wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); // Children indices
@ -326,11 +327,14 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
} }
byte nodeType = (byte) (flags & 0x03); byte nodeType = (byte) (flags & 0x03);
if (initialNodes && nodeType == 2) {
initialNodes = false;
}
if (nodeType == 1 || nodeType == 2) { // Literal/argument node if (nodeType == 1 || nodeType == 2) { // Literal/argument node
String name = wrapper.read(Type.STRING); String name = wrapper.read(Type.STRING);
if (nodeType == 1 && initialNodes) {
if (nodeType == 1) { storage.commands().add('/' + name);
storage.commands.add('/' + name);
} }
} }
@ -353,25 +357,25 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class); TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class);
if (storage.lastRequest == null) { if (storage.lastRequest() == null) {
wrapper.cancel(); wrapper.cancel();
return; 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 start = wrapper.read(Type.VAR_INT);
int length = 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 (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 wrapper.cancel(); // We can't set the length in previous versions
} }
int count = wrapper.passthrough(Type.VAR_INT); int count = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
String match = wrapper.read(Type.STRING); 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 // Ignore tooltip
if (wrapper.read(Type.BOOLEAN)) { if (wrapper.read(Type.BOOLEAN)) {
wrapper.read(Type.STRING); wrapper.read(Type.STRING);
@ -396,14 +400,14 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
if (!assumeCommand && !command.startsWith("/")) { if (!assumeCommand && !command.startsWith("/")) {
// Complete usernames for non-commands // Complete usernames for non-commands
String buffer = command.substring(command.lastIndexOf(' ') + 1); String buffer = command.substring(command.lastIndexOf(' ') + 1);
for (String value : storage.usernames.values()) { for (String value : storage.usernames().values()) {
if (startsWithIgnoreCase(value, buffer)) { if (startsWithIgnoreCase(value, buffer)) {
suggestions.add(value); 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 // Complete commands names with values from 'Declare Commands' packet
for (String value : storage.commands) { for (String value : storage.commands()) {
if (startsWithIgnoreCase(value, command)) { if (startsWithIgnoreCase(value, command)) {
suggestions.add(value); suggestions.add(value);
} }
@ -418,7 +422,7 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
response.write(Type.STRING, value); response.write(Type.STRING, value);
} }
response.scheduleSend(Protocol1_12_2To1_13.class); response.scheduleSend(Protocol1_12_2To1_13.class);
storage.lastRequest = null; storage.setLastRequest(null);
return; return;
} }
@ -430,9 +434,9 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
wrapper.write(Type.VAR_INT, id); wrapper.write(Type.VAR_INT, id);
wrapper.write(Type.STRING, command); wrapper.write(Type.STRING, command);
storage.lastId = id; storage.setLastId(id);
storage.lastAssumeCommand = assumeCommand; storage.setLastAssumeCommand(assumeCommand);
storage.lastRequest = command; storage.setLastRequest(command);
}); });
} }
}); });

View File

@ -26,9 +26,41 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
public class TabCompleteStorage implements StorableObject { public class TabCompleteStorage implements StorableObject {
public int lastId; private final Map<UUID, String> usernames = new HashMap<>();
public String lastRequest; private final Set<String> commands = new HashSet<>();
public boolean lastAssumeCommand; private int lastId;
public Map<UUID, String> usernames = new HashMap<>(); private String lastRequest;
public Set<String> commands = new HashSet<>(); private boolean lastAssumeCommand;
public Map<UUID, String> usernames() {
return usernames;
}
public Set<String> 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;
}
} }