From 63b743f85b3f90750da35603f515a9478d8791d3 Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Thu, 7 Feb 2019 14:07:30 -0200 Subject: [PATCH] Use only one task for tab completing --- .../Protocol1_13To1_12_2.java | 25 ++++--------------- .../TabCompleteThread.java | 19 ++++++++++++++ .../storage/TabCompleteTracker.java | 21 ++++++++++++++-- 3 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/TabCompleteThread.java diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index eef8cb93d..a90c48b19 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -857,27 +857,9 @@ public class Protocol1_13To1_12_2 extends Protocol { wrapper.write(Type.OPTIONAL_POSITION, null); if (!wrapper.isCancelled() && Via.getConfig().get1_13TabCompleteDelay() > 0) { TabCompleteTracker tracker = wrapper.user().get(TabCompleteTracker.class); - if (tracker.getLastTabCompleteTask() != null) { - Via.getPlatform().cancelTask(tracker.getLastTabCompleteTask()); - } wrapper.cancel(); - wrapper.resetReader(); - final PacketWrapper delayedPacket = wrapper.create(0x1); - delayedPacket.write(Type.STRING, wrapper.read(Type.STRING)); - delayedPacket.write(Type.BOOLEAN, false); - delayedPacket.write(Type.OPTIONAL_POSITION, null); - tracker.setLastTabCompleteTask( - Via.getPlatform().runSync(new Runnable() { - @Override - public void run() { - try { - delayedPacket.sendToServer(Protocol1_13To1_12_2.class); - } catch (Exception e) { - e.printStackTrace(); - } - } - }, (long) Via.getConfig().get1_13TabCompleteDelay()) - ); + tracker.setTimeToSend(System.currentTimeMillis() + Via.getConfig().get1_13TabCompleteDelay() * 50); + tracker.setLastTabComplete(wrapper.get(Type.STRING, 0)); } } }); @@ -1164,6 +1146,9 @@ public class Protocol1_13To1_12_2 extends Protocol { userConnection.put(new BlockConnectionStorage(userConnection)); } } + if (Via.getConfig().get1_13TabCompleteDelay() > 0) { + Via.getPlatform().runRepeatingSync(new TabCompleteThread(), 1L); + } } @Override diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/TabCompleteThread.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/TabCompleteThread.java new file mode 100644 index 000000000..2f08b6217 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/TabCompleteThread.java @@ -0,0 +1,19 @@ +package us.myles.ViaVersion.protocols.protocol1_13to1_12_2; + +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker; + +public class TabCompleteThread implements Runnable { + @Override + public void run() { + for (UserConnection info : Via.getManager().getPortedPlayers().values()) { + if (info.has(ProtocolInfo.class) && info.get(ProtocolInfo.class).getPipeline().contains(Protocol1_13To1_12_2.class)) { + if (info.getChannel().isOpen()) { + info.get(TabCompleteTracker.class).sendPacketToServer(); + } + } + } + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/TabCompleteTracker.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/TabCompleteTracker.java index 24748c718..775523167 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/TabCompleteTracker.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/TabCompleteTracker.java @@ -2,18 +2,35 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage; import lombok.Getter; import lombok.Setter; +import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.platform.TaskId; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; @Getter @Setter public class TabCompleteTracker extends StoredObject { private int transactionId; private String input; - private TaskId lastTabCompleteTask; + private String lastTabComplete; + private long timeToSend; public TabCompleteTracker(UserConnection user) { super(user); } + + public void sendPacketToServer() { + if (lastTabComplete == null || timeToSend > System.currentTimeMillis()) return; + PacketWrapper wrapper = new PacketWrapper(0x01, null, getUser()); + wrapper.write(Type.STRING, lastTabComplete); + wrapper.write(Type.BOOLEAN, false); + wrapper.write(Type.OPTIONAL_POSITION, null); + try { + wrapper.sendToServer(Protocol1_13To1_12_2.class); + } catch (Exception e) { + e.printStackTrace(); + } + lastTabComplete = null; + } }