mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2025-02-21 06:21:20 +01:00
Update for the new ProtocolLib.
This commit is contained in:
parent
707268c0b5
commit
e02f2ad7f4
@ -7,6 +7,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.SimpleUpdater.ResponseHandler;
|
||||
import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker;
|
||||
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.main.HologramsCommandHandler;
|
||||
import com.gmail.filoghost.holographicdisplays.disk.Configuration;
|
||||
import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase;
|
||||
@ -46,12 +47,12 @@ public class HolographicDisplays extends JavaPlugin {
|
||||
// Used for the server pinger.
|
||||
private static boolean isPreNetty;
|
||||
|
||||
// True if ProtocolLib is installed and successfully loaded.
|
||||
private static boolean useProtocolLib;
|
||||
|
||||
// The new version found by the updater, null if there is no new version.
|
||||
private static String newVersion;
|
||||
|
||||
// Not null if ProtocolLib is installed and successfully loaded.
|
||||
private static ProtocolLibHook protocolLibHook;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
||||
@ -172,10 +173,16 @@ public class HolographicDisplays extends JavaPlugin {
|
||||
// ProtocolLib check.
|
||||
try {
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||
ProtocolLibHook protocolLibHook;
|
||||
|
||||
if (is19orGreater) {
|
||||
useProtocolLib = com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.ProtocolLibHook.load(nmsManager, this);
|
||||
protocolLibHook = new com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.ProtocolLibHookImpl();
|
||||
} else {
|
||||
useProtocolLib = com.gmail.filoghost.holographicdisplays.bridge.protocollib.pre1_9.ProtocolLibHook.load(nmsManager, this, is18orGreater);
|
||||
protocolLibHook = new com.gmail.filoghost.holographicdisplays.bridge.protocollib.pre1_9.ProtocolLibHookImpl(is18orGreater);
|
||||
}
|
||||
|
||||
if (protocolLibHook.hook(this, nmsManager)) {
|
||||
HolographicDisplays.protocolLibHook = protocolLibHook;
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
@ -295,8 +302,13 @@ public class HolographicDisplays extends JavaPlugin {
|
||||
}
|
||||
|
||||
|
||||
public static boolean useProtocolLib() {
|
||||
return useProtocolLib;
|
||||
public static boolean hasProtocolLibHook() {
|
||||
return protocolLibHook != null;
|
||||
}
|
||||
|
||||
|
||||
public static ProtocolLibHook getProtocolLibHook() {
|
||||
return protocolLibHook;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.gmail.filoghost.holographicdisplays.bridge.protocollib;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager;
|
||||
import com.gmail.filoghost.holographicdisplays.object.CraftHologram;
|
||||
|
||||
public interface ProtocolLibHook {
|
||||
|
||||
public boolean hook(Plugin plugin, NMSManager nmsManager);
|
||||
|
||||
public void sendDestroyEntitiesPacket(Player player, CraftHologram hologram);
|
||||
|
||||
public void sendCreateEntitiesPacket(Player player, CraftHologram hologram);
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.gmail.filoghost.holographicdisplays.bridge.protocollib.current;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
@ -13,10 +14,16 @@ import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.events.ListenerPriority;
|
||||
import com.comphenix.protocol.events.PacketAdapter;
|
||||
import com.comphenix.protocol.events.PacketAdapter.AdapterParameteters;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.events.PacketEvent;
|
||||
import com.comphenix.protocol.utility.MinecraftReflection;
|
||||
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
|
||||
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry;
|
||||
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer;
|
||||
import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject;
|
||||
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
|
||||
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook;
|
||||
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.WrapperPlayServerSpawnEntity.ObjectTypes;
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager;
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase;
|
||||
@ -27,31 +34,50 @@ import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine;
|
||||
import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchSlimeLine;
|
||||
import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchableLine;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Utils;
|
||||
import com.google.common.base.Optional;
|
||||
|
||||
public class ProtocolLibHook {
|
||||
public class ProtocolLibHookImpl implements ProtocolLibHook {
|
||||
|
||||
private static boolean hasProtocolLib;
|
||||
private static NMSManager nmsManager;
|
||||
private NMSManager nmsManager;
|
||||
|
||||
public static boolean load(NMSManager nmsManager, Plugin plugin) {
|
||||
ProtocolLibHook.nmsManager = nmsManager;
|
||||
private Serializer
|
||||
itemSerializer,
|
||||
intSerializer,
|
||||
byteSerializer;
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hook(Plugin plugin, NMSManager nmsManager) {
|
||||
this.nmsManager = nmsManager;
|
||||
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||
|
||||
//TODO
|
||||
Bukkit.getConsoleSender().sendMessage(
|
||||
ChatColor.RED + "[Holographic Displays] Detected development version of ProtocolLib, support disabled. " +
|
||||
"Related functions (the placeholders {player} {displayname} and the visibility API) will not work.\n" +
|
||||
"The reason is that this version of ProtocolLib is unstable and partly broken. " +
|
||||
"A new version of Holographic Displays will be out when ProtocolLib gets fixed.");
|
||||
return false;
|
||||
String version = Bukkit.getPluginManager().getPlugin("ProtocolLib").getDescription().getVersion();
|
||||
if (version.matches(Pattern.quote("3.7-SNAPSHOT") + ".+")) {
|
||||
Bukkit.getConsoleSender().sendMessage(
|
||||
ChatColor.RED + "[Holographic Displays] Detected development version of ProtocolLib, support disabled. " +
|
||||
"Related functions (the placeholders {player} {displayname} and the visibility API) will not work.\n" +
|
||||
"The reason is that this version of ProtocolLib is unstable and partly broken. " +
|
||||
"Please update ProtocolLib.");
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
hasProtocolLib = true;
|
||||
|
||||
plugin.getLogger().info("Found ProtocolLib, adding support for player relative variables.");
|
||||
itemSerializer = Registry.get(MinecraftReflection.getItemStackClass());
|
||||
intSerializer = Registry.get(Integer.class);
|
||||
byteSerializer = Registry.get(Byte.class);
|
||||
|
||||
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.SPAWN_ENTITY_LIVING, PacketType.Play.Server.SPAWN_ENTITY, PacketType.Play.Server.ENTITY_METADATA) {
|
||||
plugin.getLogger().info("Found ProtocolLib, adding support for player relative variables.");
|
||||
|
||||
AdapterParameteters params = PacketAdapter
|
||||
.params()
|
||||
.plugin(plugin)
|
||||
.types( PacketType.Play.Server.SPAWN_ENTITY_LIVING,
|
||||
PacketType.Play.Server.SPAWN_ENTITY,
|
||||
PacketType.Play.Server.ENTITY_METADATA)
|
||||
.serverSide()
|
||||
.listenerPriority(ListenerPriority.NORMAL);
|
||||
|
||||
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(params) {
|
||||
|
||||
@Override
|
||||
public void onPacketSending(PacketEvent event) {
|
||||
@ -79,20 +105,14 @@ public class ProtocolLibHook {
|
||||
return;
|
||||
}
|
||||
|
||||
WrappedDataWatcher dataWatcher = spawnEntityPacket.getMetadata();
|
||||
String customName = dataWatcher.getString(2);
|
||||
|
||||
if (customName == null) {
|
||||
WrappedWatchableObject customNameWatchableObject = spawnEntityPacket.getMetadata().getWatchableObject(2);
|
||||
if (customNameWatchableObject == null || !(customNameWatchableObject.getValue() instanceof String)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String customName = (String) customNameWatchableObject.getValue();
|
||||
if (customName.contains("{player}") || customName.contains("{displayname}")) {
|
||||
|
||||
WrappedDataWatcher dataWatcherClone = dataWatcher.deepClone();
|
||||
dataWatcherClone.setObject(2, customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName()));
|
||||
spawnEntityPacket.setMetadata(dataWatcherClone);
|
||||
event.setPacket(spawnEntityPacket.getHandle());
|
||||
|
||||
customNameWatchableObject.setValue(customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName()));
|
||||
}
|
||||
|
||||
} else if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY) {
|
||||
@ -128,8 +148,7 @@ public class ProtocolLibHook {
|
||||
return;
|
||||
}
|
||||
|
||||
if (entity.getType() != EntityType.ARMOR_STAND) {
|
||||
// Enough, only armorstands are used with custom names.
|
||||
if (!isHologramType(entity.getType())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -143,30 +162,22 @@ public class ProtocolLibHook {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
List<WrappedWatchableObject> dataWatcherValues = entityMetadataPacket.getEntityMetadata();
|
||||
|
||||
for (int i = 0; i < dataWatcherValues.size(); i++) {
|
||||
|
||||
if (dataWatcherValues.get(i).getIndex() == 2 && dataWatcherValues.get(i).getValue() != null) {
|
||||
WrappedWatchableObject watchableObject = dataWatcherValues.get(i);
|
||||
if (watchableObject.getIndex() == 2) {
|
||||
|
||||
Object customNameObject = dataWatcherValues.get(i).deepClone().getValue();
|
||||
if (customNameObject == null || customNameObject instanceof String == false) {
|
||||
Object customNameObject = watchableObject.getValue();
|
||||
if (!(customNameObject instanceof String)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String customName = (String) customNameObject;
|
||||
|
||||
if (customName.contains("{player}") || customName.contains("{displayname}")) {
|
||||
|
||||
entityMetadataPacket = new WrapperPlayServerEntityMetadata(packet.deepClone());
|
||||
List<WrappedWatchableObject> clonedList = entityMetadataPacket.getEntityMetadata();
|
||||
WrappedWatchableObject clonedElement = clonedList.get(i);
|
||||
clonedElement.setValue(customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName()));
|
||||
entityMetadataPacket.setEntityMetadata(clonedList);
|
||||
event.setPacket(entityMetadataPacket.getHandle());
|
||||
watchableObject.setValue(customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName()));
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -175,17 +186,14 @@ public class ProtocolLibHook {
|
||||
});
|
||||
|
||||
return true;
|
||||
*/
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void sendDestroyEntitiesPacket(Player player, CraftHologram hologram) {
|
||||
if (!hasProtocolLib) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void sendDestroyEntitiesPacket(Player player, CraftHologram hologram) {
|
||||
List<Integer> ids = Utils.newList();
|
||||
for (CraftHologramLine line : hologram.getLinesUnsafe()) {
|
||||
if (line.isSpawned()) {
|
||||
@ -202,11 +210,9 @@ public class ProtocolLibHook {
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendCreateEntitiesPacket(Player player, CraftHologram hologram) {
|
||||
if (!hasProtocolLib) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void sendCreateEntitiesPacket(Player player, CraftHologram hologram) {
|
||||
for (CraftHologramLine line : hologram.getLinesUnsafe()) {
|
||||
if (line.isSpawned()) {
|
||||
|
||||
@ -217,7 +223,6 @@ public class ProtocolLibHook {
|
||||
|
||||
AbstractPacket nameablePacket = new WrapperPlayServerSpawnEntityLiving(textLine.getNmsNameble().getBukkitEntityNMS());
|
||||
nameablePacket.sendPacket(player);
|
||||
|
||||
}
|
||||
|
||||
} else if (line instanceof CraftItemLine) {
|
||||
@ -228,7 +233,6 @@ public class ProtocolLibHook {
|
||||
itemPacket.sendPacket(player);
|
||||
|
||||
AbstractPacket vehiclePacket = new WrapperPlayServerSpawnEntityLiving(itemLine.getNmsVehicle().getBukkitEntityNMS());
|
||||
|
||||
vehiclePacket.sendPacket(player);
|
||||
|
||||
WrapperPlayServerMount attachPacket = new WrapperPlayServerMount();
|
||||
@ -237,12 +241,13 @@ public class ProtocolLibHook {
|
||||
attachPacket.sendPacket(player);
|
||||
|
||||
WrapperPlayServerEntityMetadata itemDataPacket = new WrapperPlayServerEntityMetadata();
|
||||
|
||||
List<WrappedWatchableObject> metadata = Utils.newList();
|
||||
metadata.add(new WrappedWatchableObject(10, itemLine.getItemStack()));
|
||||
metadata.add(new WrappedWatchableObject(1, (short) 300));
|
||||
metadata.add(new WrappedWatchableObject(0, (byte) 0));
|
||||
itemDataPacket.setEntityMetadata(metadata);
|
||||
WrappedDataWatcher dataWatcher = new WrappedDataWatcher();
|
||||
|
||||
dataWatcher.setObject(new WrappedDataWatcherObject(5, itemSerializer), Optional.of(itemLine.getNmsItem().getRawItemStack()));
|
||||
dataWatcher.setObject(new WrappedDataWatcherObject(1, intSerializer), 300);
|
||||
dataWatcher.setObject(new WrappedDataWatcherObject(0, byteSerializer), (byte) 0);
|
||||
|
||||
itemDataPacket.setEntityMetadata(dataWatcher.getWatchableObjects());
|
||||
itemDataPacket.setEntityId(itemLine.getNmsItem().getIdNMS());
|
||||
itemDataPacket.sendPacket(player);
|
||||
}
|
||||
@ -252,7 +257,7 @@ public class ProtocolLibHook {
|
||||
CraftTouchableLine touchableLine = (CraftTouchableLine) line;
|
||||
|
||||
if (touchableLine.isSpawned() && touchableLine.getTouchSlime() != null) {
|
||||
|
||||
|
||||
CraftTouchSlimeLine touchSlime = touchableLine.getTouchSlime();
|
||||
|
||||
if (touchSlime.isSpawned()) {
|
||||
@ -272,11 +277,13 @@ public class ProtocolLibHook {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isHologramType(EntityType type) {
|
||||
|
||||
private boolean isHologramType(EntityType type) {
|
||||
return type == EntityType.ARMOR_STAND || type == EntityType.DROPPED_ITEM || type == EntityType.SLIME;
|
||||
}
|
||||
|
||||
private static CraftHologram getHologram(Entity bukkitEntity) {
|
||||
|
||||
private CraftHologram getHologram(Entity bukkitEntity) {
|
||||
NMSEntityBase entity = nmsManager.getNMSEntityBase(bukkitEntity);
|
||||
if (entity != null) {
|
||||
return entity.getHologramLine().getParent();
|
@ -1,5 +1,6 @@
|
||||
package com.gmail.filoghost.holographicdisplays.bridge.protocollib.pre1_9;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
@ -17,6 +18,7 @@ import com.comphenix.protocol.events.PacketEvent;
|
||||
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
|
||||
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
|
||||
import com.gmail.filoghost.holographicdisplays.HolographicDisplays;
|
||||
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook;
|
||||
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.pre1_9.WrapperPlayServerSpawnEntity.ObjectTypes;
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager;
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase;
|
||||
@ -29,33 +31,36 @@ import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchableLine;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Utils;
|
||||
import com.gmail.filoghost.holographicdisplays.util.VersionUtils;
|
||||
|
||||
public class ProtocolLibHook {
|
||||
public class ProtocolLibHookImpl implements ProtocolLibHook {
|
||||
|
||||
private boolean is1_8;
|
||||
|
||||
private NMSManager nmsManager;
|
||||
|
||||
private int customNameWatcherIndex;
|
||||
|
||||
private static boolean hasProtocolLib;
|
||||
private static NMSManager nmsManager;
|
||||
|
||||
private static int customNameWatcherIndex;
|
||||
|
||||
public static boolean load(NMSManager nmsManager, Plugin plugin, boolean is1_8) {
|
||||
ProtocolLibHook.nmsManager = nmsManager;
|
||||
|
||||
public ProtocolLibHookImpl(boolean is1_8) {
|
||||
this.is1_8 = is1_8;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hook(Plugin plugin, NMSManager nmsManager) {
|
||||
this.nmsManager = nmsManager;
|
||||
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||
|
||||
hasProtocolLib = true;
|
||||
|
||||
|
||||
plugin.getLogger().info("Found ProtocolLib, adding support for player relative variables.");
|
||||
if (is1_8) {
|
||||
customNameWatcherIndex = 2;
|
||||
} else {
|
||||
customNameWatcherIndex = 10;
|
||||
}
|
||||
|
||||
|
||||
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.SPAWN_ENTITY_LIVING, PacketType.Play.Server.SPAWN_ENTITY, PacketType.Play.Server.ENTITY_METADATA) {
|
||||
|
||||
|
||||
@Override
|
||||
public void onPacketSending(PacketEvent event) {
|
||||
|
||||
|
||||
PacketContainer packet = event.getPacket();
|
||||
|
||||
// Spawn entity packet
|
||||
@ -63,67 +68,67 @@ public class ProtocolLibHook {
|
||||
|
||||
WrapperPlayServerSpawnEntityLiving spawnEntityPacket = new WrapperPlayServerSpawnEntityLiving(packet);
|
||||
Entity entity = spawnEntityPacket.getEntity(event);
|
||||
|
||||
|
||||
if (entity == null || !isHologramType(entity.getType())) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
CraftHologram hologram = getHologram(entity);
|
||||
if (hologram == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Player player = event.getPlayer();
|
||||
if (!hologram.getVisibilityManager().isVisibleTo(player)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
WrappedDataWatcher dataWatcher = spawnEntityPacket.getMetadata();
|
||||
String customName = dataWatcher.getString(customNameWatcherIndex);
|
||||
|
||||
|
||||
if (customName == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (customName.contains("{player}") || customName.contains("{displayname}")) {
|
||||
|
||||
WrappedDataWatcher dataWatcherClone = dataWatcher.deepClone();
|
||||
dataWatcherClone.setObject(customNameWatcherIndex, customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName()));
|
||||
spawnEntityPacket.setMetadata(dataWatcherClone);
|
||||
event.setPacket(spawnEntityPacket.getHandle());
|
||||
|
||||
|
||||
}
|
||||
|
||||
} else if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY) {
|
||||
|
||||
|
||||
WrapperPlayServerSpawnEntity spawnEntityPacket = new WrapperPlayServerSpawnEntity(packet);
|
||||
int objectId = spawnEntityPacket.getType();
|
||||
if (objectId != ObjectTypes.ITEM_STACK && objectId != ObjectTypes.WITHER_SKULL && objectId != ObjectTypes.ARMOR_STAND) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Entity entity = spawnEntityPacket.getEntity(event);
|
||||
if (entity == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
CraftHologram hologram = getHologram(entity);
|
||||
if (hologram == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Player player = event.getPlayer();
|
||||
if (!hologram.getVisibilityManager().isVisibleTo(player)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
} else if (packet.getType() == PacketType.Play.Server.ENTITY_METADATA) {
|
||||
|
||||
|
||||
WrapperPlayServerEntityMetadata entityMetadataPacket = new WrapperPlayServerEntityMetadata(packet);
|
||||
Entity entity = entityMetadataPacket.getEntity(event);
|
||||
|
||||
|
||||
if (entity == null) {
|
||||
return;
|
||||
}
|
||||
@ -132,12 +137,12 @@ public class ProtocolLibHook {
|
||||
// Enough, only horses and armorstands are used with custom names.
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
CraftHologram hologram = getHologram(entity);
|
||||
if (hologram == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Player player = event.getPlayer();
|
||||
if (!hologram.getVisibilityManager().isVisibleTo(player)) {
|
||||
event.setCancelled(true);
|
||||
@ -145,20 +150,20 @@ public class ProtocolLibHook {
|
||||
}
|
||||
|
||||
List<WrappedWatchableObject> dataWatcherValues = entityMetadataPacket.getEntityMetadata();
|
||||
|
||||
|
||||
for (int i = 0; i < dataWatcherValues.size(); i++) {
|
||||
|
||||
|
||||
if (dataWatcherValues.get(i).getIndex() == customNameWatcherIndex && dataWatcherValues.get(i).getValue() != null) {
|
||||
|
||||
Object customNameObject = dataWatcherValues.get(i).deepClone().getValue();
|
||||
|
||||
Object customNameObject = dataWatcherValues.get(i).getValue();
|
||||
if (customNameObject == null || customNameObject instanceof String == false) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
String customName = (String) customNameObject;
|
||||
|
||||
|
||||
if (customName.contains("{player}") || customName.contains("{displayname}")) {
|
||||
|
||||
|
||||
entityMetadataPacket = new WrapperPlayServerEntityMetadata(packet.deepClone());
|
||||
List<WrappedWatchableObject> clonedList = entityMetadataPacket.getEntityMetadata();
|
||||
WrappedWatchableObject clonedElement = clonedList.get(i);
|
||||
@ -166,25 +171,21 @@ public class ProtocolLibHook {
|
||||
entityMetadataPacket.setEntityMetadata(clonedList);
|
||||
event.setPacket(entityMetadataPacket.getHandle());
|
||||
return;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void sendDestroyEntitiesPacket(Player player, CraftHologram hologram) {
|
||||
if (!hasProtocolLib) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
public void sendDestroyEntitiesPacket(Player player, CraftHologram hologram) {
|
||||
List<Integer> ids = Utils.newList();
|
||||
for (CraftHologramLine line : hologram.getLinesUnsafe()) {
|
||||
if (line.isSpawned()) {
|
||||
@ -193,48 +194,44 @@ public class ProtocolLibHook {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!ids.isEmpty()) {
|
||||
WrapperPlayServerEntityDestroy packet = new WrapperPlayServerEntityDestroy();
|
||||
packet.setEntities(ids);
|
||||
packet.sendPacket(player);
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendCreateEntitiesPacket(Player player, CraftHologram hologram) {
|
||||
if (!hasProtocolLib) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
public void sendCreateEntitiesPacket(Player player, CraftHologram hologram) {
|
||||
for (CraftHologramLine line : hologram.getLinesUnsafe()) {
|
||||
if (line.isSpawned()) {
|
||||
|
||||
|
||||
if (line instanceof CraftTextLine) {
|
||||
CraftTextLine textLine = (CraftTextLine) line;
|
||||
|
||||
|
||||
if (textLine.isSpawned()) {
|
||||
|
||||
|
||||
AbstractPacket nameablePacket = new WrapperPlayServerSpawnEntityLiving(textLine.getNmsNameble().getBukkitEntityNMS());
|
||||
nameablePacket.sendPacket(player);
|
||||
|
||||
|
||||
if (textLine.getNmsSkullVehicle() != null) {
|
||||
AbstractPacket vehiclePacket = new WrapperPlayServerSpawnEntity(textLine.getNmsSkullVehicle().getBukkitEntityNMS(), ObjectTypes.WITHER_SKULL, 0);
|
||||
vehiclePacket.sendPacket(player);
|
||||
|
||||
|
||||
WrapperPlayServerAttachEntity attachPacket = new WrapperPlayServerAttachEntity();
|
||||
attachPacket.setVehicleId(textLine.getNmsSkullVehicle().getIdNMS());
|
||||
attachPacket.setEntityId(textLine.getNmsNameble().getIdNMS());
|
||||
attachPacket.sendPacket(player);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else if (line instanceof CraftItemLine) {
|
||||
CraftItemLine itemLine = (CraftItemLine) line;
|
||||
|
||||
|
||||
if (itemLine.isSpawned()) {
|
||||
AbstractPacket itemPacket = new WrapperPlayServerSpawnEntity(itemLine.getNmsItem().getBukkitEntityNMS(), ObjectTypes.ITEM_STACK, 1);
|
||||
itemPacket.sendPacket(player);
|
||||
|
||||
|
||||
AbstractPacket vehiclePacket;
|
||||
if (HolographicDisplays.is18orGreater()) {
|
||||
// In 1.8 we have armor stands, that are living entities.
|
||||
@ -242,36 +239,36 @@ public class ProtocolLibHook {
|
||||
} else {
|
||||
vehiclePacket = new WrapperPlayServerSpawnEntity(itemLine.getNmsVehicle().getBukkitEntityNMS(), ObjectTypes.WITHER_SKULL, 0);
|
||||
}
|
||||
|
||||
|
||||
vehiclePacket.sendPacket(player);
|
||||
|
||||
|
||||
WrapperPlayServerAttachEntity attachPacket = new WrapperPlayServerAttachEntity();
|
||||
attachPacket.setVehicleId(itemLine.getNmsVehicle().getIdNMS());
|
||||
attachPacket.setEntityId(itemLine.getNmsItem().getIdNMS());
|
||||
attachPacket.sendPacket(player);
|
||||
|
||||
|
||||
WrapperPlayServerEntityMetadata itemDataPacket = new WrapperPlayServerEntityMetadata();
|
||||
|
||||
|
||||
List<WrappedWatchableObject> metadata = Utils.newList();
|
||||
metadata.add(new WrappedWatchableObject(10, itemLine.getItemStack()));
|
||||
metadata.add(new WrappedWatchableObject(1, (short) 300));
|
||||
metadata.add(new WrappedWatchableObject(0, (byte) 0));
|
||||
metadata.add(createWrappedWatchableObject(10, itemLine.getItemStack()));
|
||||
metadata.add(createWrappedWatchableObject(1, (short) 300));
|
||||
metadata.add(createWrappedWatchableObject(0, (byte) 0));
|
||||
itemDataPacket.setEntityMetadata(metadata);
|
||||
itemDataPacket.setEntityId(itemLine.getNmsItem().getIdNMS());
|
||||
itemDataPacket.sendPacket(player);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Unsafe cast, however both CraftTextLine and CraftItemLine are touchable.
|
||||
CraftTouchableLine touchableLine = (CraftTouchableLine) line;
|
||||
|
||||
|
||||
if (touchableLine.isSpawned() && touchableLine.getTouchSlime() != null) {
|
||||
|
||||
|
||||
CraftTouchSlimeLine touchSlime = touchableLine.getTouchSlime();
|
||||
|
||||
|
||||
if (touchSlime.isSpawned()) {
|
||||
AbstractPacket vehiclePacket;
|
||||
|
||||
|
||||
if (HolographicDisplays.is18orGreater()) {
|
||||
// Armor stand vehicle
|
||||
vehiclePacket = new WrapperPlayServerSpawnEntityLiving(touchSlime.getNmsVehicle().getBukkitEntityNMS());
|
||||
@ -280,10 +277,10 @@ public class ProtocolLibHook {
|
||||
vehiclePacket = new WrapperPlayServerSpawnEntity(touchSlime.getNmsVehicle().getBukkitEntityNMS(), ObjectTypes.WITHER_SKULL, 0);
|
||||
}
|
||||
vehiclePacket.sendPacket(player);
|
||||
|
||||
|
||||
AbstractPacket slimePacket = new WrapperPlayServerSpawnEntityLiving(touchSlime.getNmsSlime().getBukkitEntityNMS());
|
||||
slimePacket.sendPacket(player);
|
||||
|
||||
|
||||
WrapperPlayServerAttachEntity attachPacket = new WrapperPlayServerAttachEntity();
|
||||
attachPacket.setVehicleId(touchSlime.getNmsVehicle().getIdNMS());
|
||||
attachPacket.setEntityId(touchSlime.getNmsSlime().getIdNMS());
|
||||
@ -293,17 +290,31 @@ public class ProtocolLibHook {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This is just for compiling
|
||||
private Constructor<?> wrappedWatchableObjectConstructor;
|
||||
private WrappedWatchableObject createWrappedWatchableObject(int index, Object value) {
|
||||
try {
|
||||
if (wrappedWatchableObjectConstructor == null) {
|
||||
wrappedWatchableObjectConstructor = WrappedWatchableObject.class.getConstructor(int.class, Object.class);
|
||||
}
|
||||
|
||||
return (WrappedWatchableObject) wrappedWatchableObjectConstructor.newInstance(index, value);
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException("Could not invoke WrappedWatchableObject constructor", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isHologramType(EntityType type) {
|
||||
private boolean isHologramType(EntityType type) {
|
||||
return type == EntityType.HORSE || type == EntityType.WITHER_SKULL || type == EntityType.DROPPED_ITEM || type == EntityType.SLIME || VersionUtils.isArmorstand(type); // To maintain backwards compatibility
|
||||
}
|
||||
|
||||
private static CraftHologram getHologram(Entity bukkitEntity) {
|
||||
|
||||
private CraftHologram getHologram(Entity bukkitEntity) {
|
||||
NMSEntityBase entity = nmsManager.getNMSEntityBase(bukkitEntity);
|
||||
if (entity != null) {
|
||||
return entity.getHologramLine().getParent();
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -10,4 +10,7 @@ public interface NMSItem extends NMSEntityBase, NMSCanMount {
|
||||
// Sets if this item can be picked up by players.
|
||||
public void allowPickup(boolean pickup);
|
||||
|
||||
// The raw NMS ItemStack object.
|
||||
public Object getRawItemStack();
|
||||
|
||||
}
|
||||
|
@ -208,4 +208,9 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
this.vehicle = entity;
|
||||
entity.passenger = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawItemStack() {
|
||||
return super.getItemStack();
|
||||
}
|
||||
}
|
||||
|
@ -208,4 +208,9 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
this.vehicle = entity;
|
||||
entity.passenger = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawItemStack() {
|
||||
return super.getItemStack();
|
||||
}
|
||||
}
|
||||
|
@ -208,4 +208,9 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
this.vehicle = entity;
|
||||
entity.passenger = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawItemStack() {
|
||||
return super.getItemStack();
|
||||
}
|
||||
}
|
||||
|
@ -208,4 +208,9 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
this.vehicle = entity;
|
||||
entity.passenger = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawItemStack() {
|
||||
return super.getItemStack();
|
||||
}
|
||||
}
|
||||
|
@ -213,4 +213,9 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
this.vehicle = entity;
|
||||
entity.passenger = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawItemStack() {
|
||||
return super.getItemStack();
|
||||
}
|
||||
}
|
||||
|
@ -219,4 +219,9 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
this.vehicle = entity;
|
||||
entity.passenger = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawItemStack() {
|
||||
return super.getItemStack();
|
||||
}
|
||||
}
|
||||
|
@ -219,4 +219,9 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
this.vehicle = entity;
|
||||
entity.passenger = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawItemStack() {
|
||||
return super.getItemStack();
|
||||
}
|
||||
}
|
||||
|
@ -219,4 +219,9 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
this.vehicle = entity;
|
||||
entity.passenger = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawItemStack() {
|
||||
return super.getItemStack();
|
||||
}
|
||||
}
|
||||
|
@ -258,4 +258,9 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
DebugHandler.handleDebugException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawItemStack() {
|
||||
return super.getItemStack();
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.HolographicDisplays;
|
||||
import com.gmail.filoghost.holographicdisplays.api.VisibilityManager;
|
||||
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.pre1_9.ProtocolLibHook;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Validator;
|
||||
import com.gmail.filoghost.holographicdisplays.util.VersionUtils;
|
||||
|
||||
@ -150,14 +149,14 @@ public class CraftVisibilityManager implements VisibilityManager {
|
||||
}
|
||||
|
||||
private static void sendCreatePacketIfNear(Player player, CraftHologram hologram) {
|
||||
if (HolographicDisplays.useProtocolLib() && isNear(player, hologram)) {
|
||||
ProtocolLibHook.sendCreateEntitiesPacket(player, hologram);
|
||||
if (HolographicDisplays.hasProtocolLibHook() && isNear(player, hologram)) {
|
||||
HolographicDisplays.getProtocolLibHook().sendCreateEntitiesPacket(player, hologram);
|
||||
}
|
||||
}
|
||||
|
||||
private static void sendDestroyPacketIfNear(Player player, CraftHologram hologram) {
|
||||
if (HolographicDisplays.useProtocolLib() && isNear(player, hologram)) {
|
||||
ProtocolLibHook.sendDestroyEntitiesPacket(player, hologram);
|
||||
if (HolographicDisplays.hasProtocolLibHook() && isNear(player, hologram)) {
|
||||
HolographicDisplays.getProtocolLibHook().sendDestroyEntitiesPacket(player, hologram);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,8 @@ import com.google.common.collect.ImmutableList;
|
||||
|
||||
public class VersionUtils {
|
||||
|
||||
private static Method oldGetOnlinePlayersMethod;
|
||||
private static Method getOnlinePlayersMethod;
|
||||
private static boolean getOnlinePlayersUseReflection;
|
||||
|
||||
/**
|
||||
* This method uses a regex to get the NMS package part that changes with every update.
|
||||
@ -63,20 +64,25 @@ public class VersionUtils {
|
||||
|
||||
|
||||
public static Collection<? extends Player> getOnlinePlayers() {
|
||||
if (HolographicDisplays.is18orGreater()) {
|
||||
return Bukkit.getOnlinePlayers();
|
||||
} else {
|
||||
try {
|
||||
if (oldGetOnlinePlayersMethod == null) {
|
||||
oldGetOnlinePlayersMethod = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
|
||||
try {
|
||||
|
||||
if (getOnlinePlayersMethod == null) {
|
||||
getOnlinePlayersMethod = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
|
||||
if (getOnlinePlayersMethod.getReturnType() == Player[].class) {
|
||||
getOnlinePlayersUseReflection = true;
|
||||
}
|
||||
|
||||
Player[] playersArray = (Player[]) oldGetOnlinePlayersMethod.invoke(null);
|
||||
return ImmutableList.copyOf(playersArray);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if (!getOnlinePlayersUseReflection) {
|
||||
return Bukkit.getOnlinePlayers();
|
||||
} else {
|
||||
Player[] playersArray = (Player[]) getOnlinePlayersMethod.invoke(null);
|
||||
return ImmutableList.copyOf(playersArray);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
name: HolographicDisplays
|
||||
main: com.gmail.filoghost.holographicdisplays.HolographicDisplays
|
||||
version: 2.1.11
|
||||
version: 2.1.12
|
||||
|
||||
softdepend: [Multiverse-Core, MultiWorld, My Worlds, My_Worlds, ProtocolLib]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user