From 4eb367b61bbabcf80203ade43924072cf9997cd1 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Thu, 21 May 2020 22:30:36 +1200 Subject: [PATCH] Delay tab removal to hopefully prevent skins not loading on login --- .../utilities/listeners/DisguiseListener.java | 6 +- .../utilities/packets/PacketsManager.java | 1 + .../packethandlers/PacketHandlerSpawn.java | 19 +++++- .../PacketListenerChannelRegister.java | 59 +++++++++++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerChannelRegister.java diff --git a/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java b/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java index d7566702..821c2e96 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java @@ -318,20 +318,22 @@ public class DisguiseListener implements Listener { @EventHandler public void onLogin(PlayerRegisterChannelEvent event) { + Player player = event.getPlayer(); + // If it's not a forge handshake, or we didn't register it if (!event.getChannel().equals("fml:handshake") || !Bukkit.getMessenger().isOutgoingChannelRegistered(LibsDisguises.getInstance(), "fml:handshake")) { return; } - event.getPlayer() - .sendPluginMessage(LibsDisguises.getInstance(), "fml:handshake", ModdedManager.getFmlHandshake()); + player.sendPluginMessage(LibsDisguises.getInstance(), "fml:handshake", ModdedManager.getFmlHandshake()); } @EventHandler public void onJoin(PlayerJoinEvent event) { Player p = event.getPlayer(); + p.removeMetadata("ld_loggedin", LibsDisguises.getInstance()); plugin.getUpdateChecker().notifyUpdate(p); if (DisguiseConfig.isSaveGameProfiles() && DisguiseConfig.isUpdateGameProfiles() && diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/PacketsManager.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/PacketsManager.java index 72ba2c49..eaad5df5 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/PacketsManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/PacketsManager.java @@ -37,6 +37,7 @@ public class PacketsManager { ProtocolLibrary.getProtocolManager().addPacketListener(clientInteractEntityListener); ProtocolLibrary.getProtocolManager().addPacketListener(tabListListener); + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketListenerChannelRegister()); // Now I call this and the main listener is registered! setupMainPacketsListener(); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java index 19d40118..d1ea186a 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java @@ -8,6 +8,7 @@ import com.comphenix.protocol.wrappers.WrappedAttribute; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedGameProfile; import me.libraryaddict.disguise.DisguiseConfig; +import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.*; import me.libraryaddict.disguise.disguisetypes.watchers.FallingBlockWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; @@ -28,6 +29,7 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; +import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.util.Vector; import java.util.ArrayList; @@ -178,7 +180,22 @@ public class PacketHandlerSpawn implements IPacketHandler { if (LibsPremium.getPaidInformation() == null || LibsPremium.getPaidInformation().getBuildNumber().matches("#[0-9]+")) { - packets.addDelayedPacket(deleteTab, DisguiseConfig.getTablistRemoveDelay()); + if (!observer.hasMetadata("ld_loggedin")) { + ArrayList toSend; + + if (observer.hasMetadata("ld_tabsend") && !observer.getMetadata("ld_tabsend").isEmpty()) { + toSend = (ArrayList) observer.getMetadata("ld_tabsend").get(0).value(); + } else { + toSend = new ArrayList<>(); + + observer.setMetadata("ld_tabsend", + new FixedMetadataValue(LibsDisguises.getInstance(), toSend)); + } + + toSend.add(deleteTab); + } else { + packets.addDelayedPacket(deleteTab, DisguiseConfig.getTablistRemoveDelay()); + } } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerChannelRegister.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerChannelRegister.java new file mode 100644 index 00000000..0b8830de --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerChannelRegister.java @@ -0,0 +1,59 @@ +package me.libraryaddict.disguise.utilities.packets.packetlisteners; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import me.libraryaddict.disguise.LibsDisguises; +import org.bukkit.entity.Player; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.scheduler.BukkitRunnable; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; + +/** + * Created by libraryaddict on 21/05/2020. + */ +public class PacketListenerChannelRegister extends PacketAdapter { + public PacketListenerChannelRegister() { + super(LibsDisguises.getInstance(), PacketType.Play.Client.CUSTOM_PAYLOAD); + } + + @Override + public void onPacketReceiving(PacketEvent event) { + if (!event.getPacket().getMinecraftKeys().read(0).getFullKey().equals("minecraft:brand")) { + return; + } + + new BukkitRunnable() { + @Override + public void run() { + Player player = event.getPlayer(); + + if (player.hasMetadata("ld_loggedin")) { + return; + } + + if (player.hasMetadata("ld_tabsend") && !player.getMetadata("ld_tabsend").isEmpty()) { + ArrayList packets = (ArrayList) player.getMetadata("ld_tabsend") + .get(0).value(); + + player.removeMetadata("ld_tabsend", LibsDisguises.getInstance()); + + try { + for (PacketContainer packet : packets) { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); + } + } + catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + player.setMetadata("ld_loggedin", new FixedMetadataValue(LibsDisguises.getInstance(), true)); + } + }.runTaskLater(LibsDisguises.getInstance(), 5); + } +}