|
| 1 | +package com.sentropic.guiapi; |
| 2 | + |
| 3 | +import com.comphenix.protocol.PacketType; |
| 4 | +import com.comphenix.protocol.ProtocolLibrary; |
| 5 | +import com.comphenix.protocol.ProtocolManager; |
| 6 | +import com.comphenix.protocol.events.PacketAdapter; |
| 7 | +import com.comphenix.protocol.events.PacketContainer; |
| 8 | +import com.comphenix.protocol.events.PacketEvent; |
| 9 | +import com.comphenix.protocol.utility.MinecraftVersion; |
| 10 | +import com.comphenix.protocol.wrappers.EnumWrappers; |
| 11 | +import com.sentropic.guiapi.gui.GUI; |
| 12 | +import net.md_5.bungee.chat.ComponentSerializer; |
| 13 | +import org.bukkit.plugin.Plugin; |
| 14 | + |
| 15 | +public class PacketManager { |
| 16 | + private final ProtocolManager protocolManager; |
| 17 | + private final PacketAdapter packetAdapter; |
| 18 | + |
| 19 | + PacketManager(Plugin plugin) { |
| 20 | + protocolManager = ProtocolLibrary.getProtocolManager(); |
| 21 | + if (MinecraftVersion.atOrAbove(new MinecraftVersion("1.17"))) { |
| 22 | + packetAdapter = new Manager_1_17(plugin); |
| 23 | + } else { |
| 24 | + packetAdapter = new Manager_1_16(plugin); |
| 25 | + } |
| 26 | + protocolManager.addPacketListener(packetAdapter); |
| 27 | + } |
| 28 | + |
| 29 | + public void disable() { protocolManager.removePacketListener(packetAdapter); } |
| 30 | + |
| 31 | + private static class Manager_1_17 extends PacketAdapter { |
| 32 | + public Manager_1_17(Plugin plugin) { |
| 33 | + super(plugin, PacketType.Play.Server.SET_ACTION_BAR_TEXT); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void onPacketSending(PacketEvent event) { |
| 38 | + if (event.isCancelled() || GUI.isSending()) { return; } |
| 39 | + boolean success = GUIAPI.getGUIManager().getGUI(event.getPlayer()).addAnonComponent( |
| 40 | + ComponentSerializer.parse(event.getPacket().getChatComponents().read(0).getJson())[0]); |
| 41 | + event.setCancelled(success); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static class Manager_1_16 extends PacketAdapter { |
| 46 | + public Manager_1_16(Plugin plugin) { |
| 47 | + super(plugin, PacketType.Play.Server.TITLE); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onPacketSending(PacketEvent event) { |
| 52 | + if (event.isCancelled() || GUI.isSending()) { return; } |
| 53 | + PacketContainer packet = event.getPacket(); |
| 54 | + if (!packet.getTitleActions().read(0).equals(EnumWrappers.TitleAction.ACTIONBAR)) { return; } |
| 55 | + boolean success = GUIAPI.getGUIManager().getGUI(event.getPlayer()).addAnonComponent( |
| 56 | + ComponentSerializer.parse(packet.getChatComponents().read(0).getJson())[0]); |
| 57 | + event.setCancelled(success); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments