Workaround for ProtocolLib bugs

This commit is contained in:
filoghost 2019-08-11 18:32:44 +02:00
parent 467fe7493e
commit ffb8680930
7 changed files with 71 additions and 10 deletions

View File

@ -38,4 +38,8 @@ public interface NMSManager {
public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity);
default org.bukkit.entity.Entity getEntityFromID(org.bukkit.World bukkitWorld, int entityID) {
throw new IllegalStateException("Not implemented");
}
}

View File

@ -136,4 +136,16 @@ public class NmsManagerImpl implements NMSManager {
return null;
}
@Override
public org.bukkit.entity.Entity getEntityFromID(org.bukkit.World bukkitWorld, int entityID) {
WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle();
Entity nmsEntity = nmsWorld.getEntity(entityID);
if (nmsEntity == null) {
return null;
}
return nmsEntity.getBukkitEntity();
}
}

View File

@ -39,8 +39,10 @@ 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.HolographicDisplays;
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook;
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.packet.AbstractPacket;
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.packet.EntityRelatedPacketWrapper;
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.packet.WrapperPlayServerAttachEntity;
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.packet.WrapperPlayServerEntityDestroy;
import com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.packet.WrapperPlayServerEntityMetadata;
@ -78,6 +80,8 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
private int itemstackMetadataWatcherIndex;
private int customNameWatcherIndex;
private boolean useGetEntityWorkaround;
@Override
public boolean hook(Plugin plugin, NMSManager nmsManager) {
this.nmsManager = nmsManager;
@ -131,7 +135,7 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY_LIVING) {
WrapperPlayServerSpawnEntityLiving spawnEntityPacket = new WrapperPlayServerSpawnEntityLiving(packet);
Entity entity = spawnEntityPacket.getEntity(event);
Entity entity = getEntity(event, spawnEntityPacket);
CraftHologramLine hologramLine = getHologramLine(entity);
if (hologramLine == null) {
@ -155,7 +159,7 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
} else if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY) {
WrapperPlayServerSpawnEntity spawnEntityPacket = new WrapperPlayServerSpawnEntity(packet);
Entity entity = spawnEntityPacket.getEntity(event);
Entity entity = getEntity(event, spawnEntityPacket);
CraftHologramLine hologramLine = getHologramLine(entity);
if (hologramLine == null) {
@ -170,7 +174,7 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
} else if (packet.getType() == PacketType.Play.Server.ENTITY_METADATA) {
WrapperPlayServerEntityMetadata entityMetadataPacket = new WrapperPlayServerEntityMetadata(packet);
Entity entity = entityMetadataPacket.getEntity(event);
Entity entity = getEntity(event, entityMetadataPacket);
CraftHologramLine hologramLine = getHologramLine(entity);
if (hologramLine == null) {
@ -209,6 +213,20 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
}
private Entity getEntity(PacketEvent packetEvent, EntityRelatedPacketWrapper packetWrapper) {
if (!useGetEntityWorkaround) {
try {
return packetWrapper.getEntity(packetEvent);
} catch (RuntimeException e) {
useGetEntityWorkaround = true;
}
}
// Use workaround, get entity from its ID through NMS
return HolographicDisplays.getNMSManager().getEntityFromID(packetEvent.getPlayer().getWorld(), packetWrapper.getEntityID());
}
private boolean replaceRelativePlaceholders(WrappedWatchableObject customNameWatchableObject, Player player, Collection<RelativePlaceholder> relativePlaceholders) {
if (customNameWatchableObject == null) {
return true;
@ -337,7 +355,7 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
}
itemDataPacket.setEntityMetadata(dataWatcher.getWatchableObjects());
itemDataPacket.setEntityId(itemLine.getNmsItem().getIdNMS());
itemDataPacket.setEntityID(itemLine.getNmsItem().getIdNMS());
itemDataPacket.sendPacket(player);
}
}
@ -384,7 +402,7 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
dataWatcher.setObject(new WrappedDataWatcherObject(11, byteSerializer), (byte) (0x01 | 0x08 | 0x10)); // Armor stand data: small, no base plate, marker
dataPacket.setEntityMetadata(dataWatcher.getWatchableObjects());
dataPacket.setEntityId(armorStand.getIdNMS());
dataPacket.setEntityID(armorStand.getIdNMS());
dataPacket.sendPacket(receiver);
} else {

View File

@ -0,0 +1,27 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.packet;
import org.bukkit.entity.Entity;
import com.comphenix.protocol.events.PacketEvent;
public interface EntityRelatedPacketWrapper {
public int getEntityID();
public Entity getEntity(PacketEvent event);
}

View File

@ -28,7 +28,7 @@ import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
public class WrapperPlayServerEntityMetadata extends AbstractPacket {
public class WrapperPlayServerEntityMetadata extends AbstractPacket implements EntityRelatedPacketWrapper {
public static final PacketType TYPE = PacketType.Play.Server.ENTITY_METADATA;
public WrapperPlayServerEntityMetadata() {
@ -44,7 +44,7 @@ public class WrapperPlayServerEntityMetadata extends AbstractPacket {
* Retrieve unique entity ID to update.
* @return The current Entity ID
*/
public int getEntityId() {
public int getEntityID() {
return handle.getIntegers().read(0);
}
@ -52,7 +52,7 @@ public class WrapperPlayServerEntityMetadata extends AbstractPacket {
* Set unique entity ID to update.
* @param value - new value.
*/
public void setEntityId(int value) {
public void setEntityID(int value) {
handle.getIntegers().write(0, value);
}

View File

@ -27,7 +27,7 @@ import com.comphenix.protocol.injector.PacketConstructor;
import com.comphenix.protocol.reflect.IntEnum;
import com.gmail.filoghost.holographicdisplays.util.NMSVersion;
public class WrapperPlayServerSpawnEntity extends AbstractPacket {
public class WrapperPlayServerSpawnEntity extends AbstractPacket implements EntityRelatedPacketWrapper {
public static final PacketType TYPE = PacketType.Play.Server.SPAWN_ENTITY;
private static PacketConstructor entityConstructor;

View File

@ -28,7 +28,7 @@ import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.injector.PacketConstructor;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
public class WrapperPlayServerSpawnEntityLiving extends AbstractPacket {
public class WrapperPlayServerSpawnEntityLiving extends AbstractPacket implements EntityRelatedPacketWrapper {
public static final PacketType TYPE = PacketType.Play.Server.SPAWN_ENTITY_LIVING;
private static PacketConstructor entityConstructor;