Skip to content

Commit

Permalink
Fixed ProtocolLib support
Browse files Browse the repository at this point in the history
  • Loading branch information
d0by1 committed Jan 19, 2022
1 parent 276e7fc commit db075c0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
25 changes: 4 additions & 21 deletions src/main/java/eu/decentsoftware/holograms/api/DecentHolograms.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package eu.decentsoftware.holograms.api;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import eu.decentsoftware.holograms.api.animations.AnimationManager;
import eu.decentsoftware.holograms.api.commands.CommandManager;
import eu.decentsoftware.holograms.api.features.FeatureManager;
import eu.decentsoftware.holograms.api.holograms.Hologram;
import eu.decentsoftware.holograms.api.holograms.HologramManager;
import eu.decentsoftware.holograms.api.nms.NMS;
import eu.decentsoftware.holograms.api.nms.PacketHandler__ProtocolLib;
import eu.decentsoftware.holograms.api.nms.PacketListener;
import eu.decentsoftware.holograms.api.nms.NMSPacketListener;
import eu.decentsoftware.holograms.api.player.PlayerListener;
import eu.decentsoftware.holograms.api.utils.BungeeUtils;
import eu.decentsoftware.holograms.api.utils.Common;
Expand All @@ -34,11 +31,10 @@ public final class DecentHolograms {
private CommandManager commandManager;
private FeatureManager featureManager;
private AnimationManager animationManager;
private PacketListener packetListener;
private NMSPacketListener packetListener;
private Ticker ticker;
private File dataFolder;
private boolean updateAvailable;
private boolean usingProtocolLib;

/*
* Constructors
Expand All @@ -47,19 +43,14 @@ public final class DecentHolograms {
DecentHolograms(JavaPlugin plugin) {
Validate.notNull(plugin);
this.plugin = plugin;
this.usingProtocolLib = false;
}

/*
* General Methods
*/

protected void load() {
if (Common.isPluginEnabled("ProtocolLib")) {
ProtocolManager pm = ProtocolLibrary.getProtocolManager();
pm.addPacketListener(new PacketHandler__ProtocolLib());
usingProtocolLib = true;
}

}

protected void enable() {
Expand All @@ -73,7 +64,7 @@ protected void enable() {
commandManager = new CommandManager();
featureManager = new FeatureManager();
animationManager = new AnimationManager();
packetListener = new PacketListener();
packetListener = new NMSPacketListener();

PluginManager pm = Bukkit.getPluginManager();
pm.registerEvents(new PlayerListener(this), plugin);
Expand Down Expand Up @@ -132,12 +123,4 @@ public File getDataFolder() {
return dataFolder;
}

public boolean isUsingProtocolLib() {
return usingProtocolLib;
}

public void setUsingProtocolLib(boolean usingProtocolLib) {
this.usingProtocolLib = usingProtocolLib;
}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
package eu.decentsoftware.holograms.api.nms;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import eu.decentsoftware.holograms.api.DecentHologramsAPI;
import eu.decentsoftware.holograms.api.utils.Common;
import io.netty.channel.ChannelPipeline;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class PacketListener {
public class NMSPacketListener {

private static final NMS nms = NMS.getInstance();
private boolean usingProtocolLib;

public PacketListener() {
this.registerAll();
public NMSPacketListener() {
registerAll();
}

public void destroy() {
this.unregisterAll();
if (usingProtocolLib && Common.isPluginEnabled("ProtocolLib")) {
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
protocolManager.removePacketListeners(DecentHologramsAPI.get().getPlugin());
usingProtocolLib = false;
return;
}
unregisterAll();
}

public boolean register(Player player) {
if (DecentHologramsAPI.get().isUsingProtocolLib()) {
if (usingProtocolLib) {
return true;
}

this.unregister(player);
unregister(player);
ChannelPipeline pipeline = nms.getPipeline(player);
PacketHandler__Custom packetHandler = new PacketHandler__Custom(player);
try {
Expand All @@ -35,17 +45,21 @@ public boolean register(Player player) {
}

public void registerAll() {
if (DecentHologramsAPI.get().isUsingProtocolLib()) {
return;
}

for (Player player : Bukkit.getOnlinePlayers()) {
this.register(player);
// If ProtocolLib is present, use it for packet listening.
if (Common.isPluginEnabled("ProtocolLib")) {
new PacketHandler__ProtocolLib();
usingProtocolLib = true;
Common.log("Using ProtocolLib for packet listening.");
} else {
usingProtocolLib = false;
for (Player player : Bukkit.getOnlinePlayers()) {
register(player);
}
}
}

public boolean unregister(Player player) {
if (DecentHologramsAPI.get().isUsingProtocolLib()) {
if (usingProtocolLib) {
return true;
}

Expand All @@ -60,12 +74,12 @@ public boolean unregister(Player player) {
}

public void unregisterAll() {
if (DecentHologramsAPI.get().isUsingProtocolLib()) {
if (usingProtocolLib) {
return;
}

for (Player player : Bukkit.getOnlinePlayers()) {
this.unregister(player);
unregister(player);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package eu.decentsoftware.holograms.api.nms;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import eu.decentsoftware.holograms.api.DecentHologramsAPI;
import org.bukkit.entity.Player;

public class PacketHandler__ProtocolLib extends PacketAdapter {
public class PacketHandler__ProtocolLib {

public PacketHandler__ProtocolLib() {
super(DecentHologramsAPI.get().getPlugin(), ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY);
}

@Override
public void onPacketReceiving(PacketEvent event) {
if (event.getPacketType().equals(PacketType.Play.Client.USE_ENTITY)) {
PacketContainer packetContainer = event.getPacket();
Object packet = packetContainer.getHandle();
Player player = event.getPlayer();
if (PacketHandlerCommon.handlePacket(packet, player)) {
event.setCancelled(true);
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
protocolManager.addPacketListener(new PacketAdapter(DecentHologramsAPI.get().getPlugin(), ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY) {
@Override
public void onPacketReceiving(PacketEvent event) {
if (event.getPacketType().equals(PacketType.Play.Client.USE_ENTITY)) {
PacketContainer packetContainer = event.getPacket();
Object packet = packetContainer.getHandle();
Player player = event.getPlayer();
if (PacketHandlerCommon.handlePacket(packet, player)) {
event.setCancelled(true);
}
}
}
}
});
}

}

0 comments on commit db075c0

Please sign in to comment.