Search in sources :

Example 1 with LegacyEntityNbtNotPermittedException

use of com.ruinscraft.panilla.api.exception.LegacyEntityNbtNotPermittedException in project Panilla by ds58.

the class PacketInspector method checkPacketPlayOutSpawnEntity.

@Override
public void checkPacketPlayOutSpawnEntity(Object _packet) throws LegacyEntityNbtNotPermittedException {
    if (_packet instanceof PacketPlayOutSpawnEntity) {
        PacketPlayOutSpawnEntity packet = (PacketPlayOutSpawnEntity) _packet;
        try {
            Field idField = PacketPlayOutSpawnEntity.class.getDeclaredField("a");
            idField.setAccessible(true);
            int entityId = (int) idField.get(packet);
            Entity entity = getEntityById(entityId);
            if (entity != null) {
                if (entity instanceof EntityItem) {
                    EntityItem item = (EntityItem) entity;
                    if (item.getItemStack() == null) {
                        return;
                    }
                    if (!item.getItemStack().hasTag()) {
                        return;
                    }
                    INbtTagCompound tag = new NbtTagCompound(item.getItemStack().getTag());
                    String itemName = item.getItemStack().getItem().getName();
                    FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);
                    if (FailedNbt.fails(failedNbt)) {
                        throw new LegacyEntityNbtNotPermittedException(packet.getClass().getSimpleName(), false, failedNbt, entityId);
                    }
                }
            }
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_8_R3.nbt.NbtTagCompound) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) Field(java.lang.reflect.Field) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) FailedNbt(com.ruinscraft.panilla.api.exception.FailedNbt) LegacyEntityNbtNotPermittedException(com.ruinscraft.panilla.api.exception.LegacyEntityNbtNotPermittedException)

Aggregations

FailedNbt (com.ruinscraft.panilla.api.exception.FailedNbt)1 LegacyEntityNbtNotPermittedException (com.ruinscraft.panilla.api.exception.LegacyEntityNbtNotPermittedException)1 INbtTagCompound (com.ruinscraft.panilla.api.nbt.INbtTagCompound)1 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_8_R3.nbt.NbtTagCompound)1 Field (java.lang.reflect.Field)1