Search in sources :

Example 46 with NbtTagCompound

use of com.ruinscraft.panilla.craftbukkit.v1_18_R2.nbt.NbtTagCompound in project Panilla by ds58.

the class PacketInspector method checkPacketPlayOutWindowItems.

@Override
public void checkPacketPlayOutWindowItems(Object _packet) throws NbtNotPermittedException {
    if (_packet instanceof PacketPlayOutWindowItems) {
        PacketPlayOutWindowItems packet = (PacketPlayOutWindowItems) _packet;
        try {
            Field windowIdField = PacketPlayOutWindowItems.class.getDeclaredField("a");
            windowIdField.setAccessible(true);
            int windowId = (int) windowIdField.get(packet);
            // check if window is not player inventory
            if (windowId != 0) {
                return;
            }
            Field itemStacksField = PacketPlayOutWindowItems.class.getDeclaredField("b");
            itemStacksField.setAccessible(true);
            List<ItemStack> itemStacks = (List<ItemStack>) itemStacksField.get(packet);
            for (ItemStack itemStack : itemStacks) {
                if (itemStack == null || !itemStack.hasTag()) {
                    continue;
                }
                NbtTagCompound tag = new NbtTagCompound(itemStack.getTag());
                String itemClass = itemStack.getClass().getSimpleName();
                String packetClass = packet.getClass().getSimpleName();
                // TODO: set slot?
                NbtChecks.checkPacketPlayOut(0, tag, itemClass, packetClass, panilla);
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : Field(java.lang.reflect.Field) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_16_R3.nbt.NbtTagCompound) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) List(java.util.List)

Example 47 with NbtTagCompound

use of com.ruinscraft.panilla.craftbukkit.v1_18_R2.nbt.NbtTagCompound in project Panilla by ds58.

the class InventoryCleaner method clean.

@Override
public void clean(IPanillaPlayer player) {
    CraftPlayer craftPlayer = (CraftPlayer) player.getHandle();
    Container container = craftPlayer.getHandle().activeContainer;
    for (int slot = 0; slot < container.slots.size(); slot++) {
        ItemStack itemStack = container.getSlot(slot).getItem();
        if (itemStack == null || !itemStack.hasTag()) {
            continue;
        }
        NBTTagCompound nmsTag = itemStack.getTag();
        INbtTagCompound tag = new NbtTagCompound(nmsTag);
        String itemName = itemStack.getItem().getName();
        if (nmsTag == null || itemName == null) {
            continue;
        }
        FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);
        if (FailedNbt.failsThreshold(failedNbt)) {
            container.getSlot(slot).getItem().setTag(null);
        } else if (FailedNbt.fails(failedNbt)) {
            nmsTag.remove(failedNbt.key);
            container.getSlot(slot).getItem().setTag(nmsTag);
        }
    }
}
Also used : Container(net.minecraft.server.v1_14_R1.Container) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_14_R1.nbt.NbtTagCompound) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) NBTTagCompound(net.minecraft.server.v1_14_R1.NBTTagCompound) CraftPlayer(org.bukkit.craftbukkit.v1_14_R1.entity.CraftPlayer) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) ItemStack(net.minecraft.server.v1_14_R1.ItemStack) FailedNbt(com.ruinscraft.panilla.api.exception.FailedNbt)

Example 48 with NbtTagCompound

use of com.ruinscraft.panilla.craftbukkit.v1_18_R2.nbt.NbtTagCompound in project Panilla by ds58.

the class PacketInspector method checkPacketPlayOutWindowItems.

@Override
public void checkPacketPlayOutWindowItems(Object _packet) throws NbtNotPermittedException {
    if (_packet instanceof PacketPlayOutWindowItems) {
        PacketPlayOutWindowItems packet = (PacketPlayOutWindowItems) _packet;
        try {
            Field windowIdField = PacketPlayOutWindowItems.class.getDeclaredField("a");
            windowIdField.setAccessible(true);
            int windowId = (int) windowIdField.get(packet);
            // check if window is not player inventory
            if (windowId != 0) {
                return;
            }
            Field itemStacksField = PacketPlayOutWindowItems.class.getDeclaredField("b");
            itemStacksField.setAccessible(true);
            List<ItemStack> itemStacks = (List<ItemStack>) itemStacksField.get(packet);
            for (ItemStack itemStack : itemStacks) {
                if (itemStack == null || !itemStack.hasTag()) {
                    continue;
                }
                NbtTagCompound tag = new NbtTagCompound(itemStack.getTag());
                String itemClass = itemStack.getClass().getSimpleName();
                String packetClass = packet.getClass().getSimpleName();
                // TODO: set slot?
                NbtChecks.checkPacketPlayOut(0, tag, itemClass, packetClass, panilla);
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : Field(java.lang.reflect.Field) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_14_R1.nbt.NbtTagCompound) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) List(java.util.List)

Example 49 with NbtTagCompound

use of com.ruinscraft.panilla.craftbukkit.v1_18_R2.nbt.NbtTagCompound in project Panilla by ds58.

the class InventoryCleaner method clean.

@Override
public void clean(IPanillaPlayer player) {
    CraftPlayer craftPlayer = (CraftPlayer) player.getHandle();
    Container container = craftPlayer.getHandle().activeContainer;
    for (int slot = 0; slot < container.slots.size(); slot++) {
        ItemStack itemStack = container.getSlot(slot).getItem();
        if (itemStack == null || !itemStack.hasTag()) {
            continue;
        }
        NBTTagCompound nmsTag = itemStack.getTag();
        INbtTagCompound tag = new NbtTagCompound(nmsTag);
        String itemName = itemStack.getItem().getName();
        if (nmsTag == null || itemName == null) {
            continue;
        }
        FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);
        if (FailedNbt.failsThreshold(failedNbt)) {
            container.getSlot(slot).getItem().setTag(null);
        } else if (FailedNbt.fails(failedNbt)) {
            nmsTag.remove(failedNbt.key);
            container.getSlot(slot).getItem().setTag(nmsTag);
        }
    }
}
Also used : Container(net.minecraft.server.v1_15_R1.Container) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_15_R1.nbt.NbtTagCompound) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) NBTTagCompound(net.minecraft.server.v1_15_R1.NBTTagCompound) CraftPlayer(org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) ItemStack(net.minecraft.server.v1_15_R1.ItemStack) FailedNbt(com.ruinscraft.panilla.api.exception.FailedNbt)

Example 50 with NbtTagCompound

use of com.ruinscraft.panilla.craftbukkit.v1_18_R2.nbt.NbtTagCompound in project Panilla by ds58.

the class PacketInspector method checkPacketPlayOutSpawnEntity.

@Override
public void checkPacketPlayOutSpawnEntity(Object _packet) throws EntityNbtNotPermittedException {
    if (_packet instanceof PacketPlayOutSpawnEntity) {
        PacketPlayOutSpawnEntity packet = (PacketPlayOutSpawnEntity) _packet;
        try {
            Field typeField = PacketPlayOutSpawnEntity.class.getDeclaredField("b");
            typeField.setAccessible(true);
            UUID entityId = (UUID) typeField.get(packet);
            org.bukkit.entity.Entity bukkitEntity = org.bukkit.Bukkit.getEntity(entityId);
            CraftEntity craftEntity = (CraftEntity) bukkitEntity;
            if (craftEntity == null) {
                return;
            }
            Entity entity = craftEntity.getHandle();
            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 EntityNbtNotPermittedException(packet.getClass().getSimpleName(), false, failedNbt, entityId, bukkitEntity.getWorld().getName());
                    }
                }
            }
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : CraftEntity(org.bukkit.craftbukkit.v1_15_R1.entity.CraftEntity) EntityNbtNotPermittedException(com.ruinscraft.panilla.api.exception.EntityNbtNotPermittedException) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_15_R1.nbt.NbtTagCompound) CraftEntity(org.bukkit.craftbukkit.v1_15_R1.entity.CraftEntity) Field(java.lang.reflect.Field) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) UUID(java.util.UUID) FailedNbt(com.ruinscraft.panilla.api.exception.FailedNbt)

Aggregations

INbtTagCompound (com.ruinscraft.panilla.api.nbt.INbtTagCompound)55 Field (java.lang.reflect.Field)34 FailedNbt (com.ruinscraft.panilla.api.exception.FailedNbt)22 ItemStack (net.minecraft.world.item.ItemStack)12 List (java.util.List)11 EntityNbtNotPermittedException (com.ruinscraft.panilla.api.exception.EntityNbtNotPermittedException)10 UUID (java.util.UUID)10 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_12_R1.nbt.NbtTagCompound)5 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_13_R2.nbt.NbtTagCompound)5 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_14_R1.nbt.NbtTagCompound)5 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_15_R1.nbt.NbtTagCompound)5 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_16_R1.nbt.NbtTagCompound)5 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_16_R2.nbt.NbtTagCompound)5 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_16_R3.nbt.NbtTagCompound)5 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_17_R1.nbt.NbtTagCompound)5 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_18_R1.nbt.NbtTagCompound)5 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_18_R2.nbt.NbtTagCompound)5 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_8_R3.nbt.NbtTagCompound)5 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 PacketPlayInSetCreativeSlot (net.minecraft.network.protocol.game.PacketPlayInSetCreativeSlot)3