Search in sources :

Example 86 with org.bukkit.craftbukkit.v1_18_R2.entity

use of org.bukkit.craftbukkit.v1_18_R2.entity in project Denizen by DenizenScript.

the class DenizenNetworkManagerImpl method getModifiedMetadataFor.

public ClientboundSetEntityDataPacket getModifiedMetadataFor(ClientboundSetEntityDataPacket metadataPacket) {
    if (!RenameCommand.hasAnyDynamicRenames() && SneakCommand.forceSetSneak.isEmpty()) {
        return null;
    }
    try {
        int eid = metadataPacket.getId();
        Entity ent = player.level.getEntity(eid);
        if (ent == null) {
            // If it doesn't exist on-server, it's definitely not relevant, so move on
            return null;
        }
        String nameToApply = RenameCommand.getCustomNameFor(ent.getUUID(), player.getBukkitEntity(), false);
        Boolean forceSneak = SneakCommand.shouldSneak(ent.getUUID(), player.getUUID());
        if (nameToApply == null && forceSneak == null) {
            return null;
        }
        List<SynchedEntityData.DataItem<?>> data = new ArrayList<>(metadataPacket.getUnpackedData());
        boolean any = false;
        for (int i = 0; i < data.size(); i++) {
            SynchedEntityData.DataItem<?> item = data.get(i);
            EntityDataAccessor<?> watcherObject = item.getAccessor();
            int watcherId = watcherObject.getId();
            if (watcherId == 0 && forceSneak != null) {
                // 0: Entity flags
                byte val = (Byte) item.getValue();
                if (forceSneak) {
                    // 8: Crouching
                    val |= 0x02;
                } else {
                    val &= ~0x02;
                }
                data.set(i, new SynchedEntityData.DataItem(watcherObject, val));
                any = true;
            } else if (watcherId == 2 && nameToApply != null) {
                // 2: Custom name metadata
                Optional<Component> name = Optional.of(Handler.componentToNMS(FormattedTextHelper.parse(nameToApply, ChatColor.WHITE)));
                data.set(i, new SynchedEntityData.DataItem(watcherObject, name));
                any = true;
            } else if (watcherId == 3 && nameToApply != null) {
                // 3: custom name visible metadata
                data.set(i, new SynchedEntityData.DataItem(watcherObject, true));
                any = true;
            }
        }
        if (!any) {
            return null;
        }
        ClientboundSetEntityDataPacket altPacket = new ClientboundSetEntityDataPacket(copyPacket(metadataPacket));
        ENTITY_METADATA_LIST.set(altPacket, data);
        return altPacket;
    } catch (Throwable ex) {
        Debug.echoError(ex);
        return null;
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) Entity(net.minecraft.world.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_18_R2.entity.CraftEntity) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData)

Example 87 with org.bukkit.craftbukkit.v1_18_R2.entity

use of org.bukkit.craftbukkit.v1_18_R2.entity in project Denizen by DenizenScript.

the class DenizenNetworkManagerImpl method processHiddenEntitiesForPacket.

public boolean processHiddenEntitiesForPacket(Packet<?> packet) {
    if (!HideEntitiesHelper.hasAnyHides()) {
        return false;
    }
    try {
        int ider = -1;
        Entity e = null;
        if (packet instanceof ClientboundAddPlayerPacket) {
            ider = ((ClientboundAddPlayerPacket) packet).getEntityId();
        } else if (packet instanceof ClientboundAddEntityPacket) {
            ider = ((ClientboundAddEntityPacket) packet).getId();
        } else if (packet instanceof ClientboundAddMobPacket) {
            ider = ((ClientboundAddMobPacket) packet).getId();
        } else if (packet instanceof ClientboundAddPaintingPacket) {
            ider = ((ClientboundAddPaintingPacket) packet).getId();
        } else if (packet instanceof ClientboundAddExperienceOrbPacket) {
            ider = ((ClientboundAddExperienceOrbPacket) packet).getId();
        } else if (packet instanceof ClientboundMoveEntityPacket) {
            e = ((ClientboundMoveEntityPacket) packet).getEntity(player.getLevel());
        } else if (packet instanceof ClientboundSetEntityDataPacket) {
            ider = ((ClientboundSetEntityDataPacket) packet).getId();
        } else if (packet instanceof ClientboundSetEntityMotionPacket) {
            ider = ((ClientboundSetEntityMotionPacket) packet).getId();
        } else if (packet instanceof ClientboundTeleportEntityPacket) {
            ider = ((ClientboundTeleportEntityPacket) packet).getId();
        }
        if (e == null && ider != -1) {
            e = player.getLevel().getEntity(ider);
        }
        if (e != null) {
            if (isHidden(e)) {
                return true;
            }
            if (packet instanceof ClientboundAddPlayerPacket || packet instanceof ClientboundAddEntityPacket || packet instanceof ClientboundAddMobPacket || packet instanceof ClientboundAddPaintingPacket || packet instanceof ClientboundAddExperienceOrbPacket) {
                processFakePlayerSpawn(e);
            }
        }
    } catch (Exception ex) {
        Debug.echoError(ex);
    }
    return false;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) Entity(net.minecraft.world.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_18_R2.entity.CraftEntity)

Example 88 with org.bukkit.craftbukkit.v1_18_R2.entity

use of org.bukkit.craftbukkit.v1_18_R2.entity in project Denizen by DenizenScript.

the class DenizenNetworkManagerImpl method processAttachToForPacket.

public boolean processAttachToForPacket(Packet<?> packet) {
    if (EntityAttachmentHelper.toEntityToData.isEmpty()) {
        return false;
    }
    try {
        if (packet instanceof ClientboundMoveEntityPacket) {
            Entity e = ((ClientboundMoveEntityPacket) packet).getEntity(player.getLevel());
            if (e == null) {
                return false;
            }
            if (!e.isPassenger()) {
                tryProcessMovePacketForAttach((ClientboundMoveEntityPacket) packet, e);
            }
            return EntityAttachmentHelper.denyOriginalPacketSend(player.getUUID(), e.getUUID());
        } else if (packet instanceof ClientboundSetEntityMotionPacket) {
            int ider = ((ClientboundSetEntityMotionPacket) packet).getId();
            Entity e = player.getLevel().getEntity(ider);
            if (e == null) {
                return false;
            }
            tryProcessVelocityPacketForAttach((ClientboundSetEntityMotionPacket) packet, e);
            return EntityAttachmentHelper.denyOriginalPacketSend(player.getUUID(), e.getUUID());
        } else if (packet instanceof ClientboundTeleportEntityPacket) {
            int ider = ((ClientboundTeleportEntityPacket) packet).getId();
            Entity e = player.getLevel().getEntity(ider);
            if (e == null) {
                return false;
            }
            tryProcessTeleportPacketForAttach((ClientboundTeleportEntityPacket) packet, e, VECTOR_ZERO);
            return EntityAttachmentHelper.denyOriginalPacketSend(player.getUUID(), e.getUUID());
        }
    } catch (Exception ex) {
        Debug.echoError(ex);
    }
    return false;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) Entity(net.minecraft.world.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_18_R2.entity.CraftEntity)

Example 89 with org.bukkit.craftbukkit.v1_18_R2.entity

use of org.bukkit.craftbukkit.v1_18_R2.entity in project Denizen by DenizenScript.

the class DenizenNetworkManagerImpl method processEquipmentForPacket.

public boolean processEquipmentForPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
    if (FakeEquipCommand.overrides.isEmpty()) {
        return false;
    }
    try {
        if (packet instanceof ClientboundSetEquipmentPacket) {
            int eid = ((ClientboundSetEquipmentPacket) packet).getEntity();
            Entity ent = player.level.getEntity(eid);
            if (ent == null) {
                return false;
            }
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(ent.getUUID(), player.getBukkitEntity());
            if (override == null) {
                return false;
            }
            List<Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack>> equipment = new ArrayList<>(((ClientboundSetEquipmentPacket) packet).getSlots());
            ClientboundSetEquipmentPacket newPacket = new ClientboundSetEquipmentPacket(eid, equipment);
            for (int i = 0; i < equipment.size(); i++) {
                Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack> pair = equipment.get(i);
                ItemStack use = pair.getSecond();
                switch(pair.getFirst()) {
                    case MAINHAND:
                        use = override.hand == null ? use : CraftItemStack.asNMSCopy(override.hand.getItemStack());
                        break;
                    case OFFHAND:
                        use = override.offhand == null ? use : CraftItemStack.asNMSCopy(override.offhand.getItemStack());
                        break;
                    case CHEST:
                        use = override.chest == null ? use : CraftItemStack.asNMSCopy(override.chest.getItemStack());
                        break;
                    case HEAD:
                        use = override.head == null ? use : CraftItemStack.asNMSCopy(override.head.getItemStack());
                        break;
                    case LEGS:
                        use = override.legs == null ? use : CraftItemStack.asNMSCopy(override.legs.getItemStack());
                        break;
                    case FEET:
                        use = override.boots == null ? use : CraftItemStack.asNMSCopy(override.boots.getItemStack());
                        break;
                }
                equipment.set(i, new Pair<>(pair.getFirst(), use));
            }
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundEntityEventPacket) {
            Entity ent = ((ClientboundEntityEventPacket) packet).getEntity(player.level);
            if (!(ent instanceof net.minecraft.world.entity.LivingEntity)) {
                return false;
            }
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(ent.getUUID(), player.getBukkitEntity());
            if (override == null || (override.hand == null && override.offhand == null)) {
                return false;
            }
            if (((ClientboundEntityEventPacket) packet).getEventId() != (byte) 55) {
                return false;
            }
            List<Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack>> equipment = new ArrayList<>();
            ItemStack hand = override.hand != null ? CraftItemStack.asNMSCopy(override.hand.getItemStack()) : ((net.minecraft.world.entity.LivingEntity) ent).getMainHandItem();
            ItemStack offhand = override.offhand != null ? CraftItemStack.asNMSCopy(override.offhand.getItemStack()) : ((net.minecraft.world.entity.LivingEntity) ent).getOffhandItem();
            equipment.add(new Pair<>(net.minecraft.world.entity.EquipmentSlot.MAINHAND, hand));
            equipment.add(new Pair<>(net.minecraft.world.entity.EquipmentSlot.OFFHAND, offhand));
            ClientboundSetEquipmentPacket newPacket = new ClientboundSetEquipmentPacket(ent.getId(), equipment);
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundContainerSetContentPacket) {
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(player.getUUID(), player.getBukkitEntity());
            if (override == null) {
                return false;
            }
            int window = ((ClientboundContainerSetContentPacket) packet).getContainerId();
            if (window != 0) {
                return false;
            }
            NonNullList<ItemStack> items = (NonNullList<ItemStack>) ((ClientboundContainerSetContentPacket) packet).getItems();
            if (override.head != null) {
                items.set(5, CraftItemStack.asNMSCopy(override.head.getItemStack()));
            }
            if (override.chest != null) {
                items.set(6, CraftItemStack.asNMSCopy(override.chest.getItemStack()));
            }
            if (override.legs != null) {
                items.set(7, CraftItemStack.asNMSCopy(override.legs.getItemStack()));
            }
            if (override.boots != null) {
                items.set(8, CraftItemStack.asNMSCopy(override.boots.getItemStack()));
            }
            if (override.offhand != null) {
                items.set(45, CraftItemStack.asNMSCopy(override.offhand.getItemStack()));
            }
            if (override.hand != null) {
                items.set(player.getInventory().selected + 36, CraftItemStack.asNMSCopy(override.hand.getItemStack()));
            }
            ClientboundContainerSetContentPacket newPacket = new ClientboundContainerSetContentPacket(window, ((ClientboundContainerSetContentPacket) packet).getStateId(), items, ((ClientboundContainerSetContentPacket) packet).getCarriedItem());
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundContainerSetSlotPacket) {
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(player.getUUID(), player.getBukkitEntity());
            if (override == null) {
                return false;
            }
            int window = ((ClientboundContainerSetSlotPacket) packet).getContainerId();
            if (window != 0) {
                return false;
            }
            int slot = ((ClientboundContainerSetSlotPacket) packet).getSlot();
            org.bukkit.inventory.ItemStack item = null;
            if (slot == 5 && override.head != null) {
                item = override.head.getItemStack();
            } else if (slot == 6 && override.chest != null) {
                item = override.chest.getItemStack();
            } else if (slot == 7 && override.legs != null) {
                item = override.legs.getItemStack();
            } else if (slot == 8 && override.boots != null) {
                item = override.boots.getItemStack();
            } else if (slot == 45 && override.offhand != null) {
                item = override.offhand.getItemStack();
            } else if (slot == player.getInventory().selected + 36 && override.hand != null) {
                item = override.hand.getItemStack();
            }
            if (item == null) {
                return false;
            }
            ClientboundContainerSetSlotPacket newPacket = new ClientboundContainerSetSlotPacket(window, ((ClientboundContainerSetSlotPacket) packet).getStateId(), slot, CraftItemStack.asNMSCopy(item));
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        }
    } catch (Throwable ex) {
        Debug.echoError(ex);
    }
    return false;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) Entity(net.minecraft.world.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_18_R2.entity.CraftEntity) LivingEntity(org.bukkit.entity.LivingEntity) FakeEquipCommand(com.denizenscript.denizen.scripts.commands.entity.FakeEquipCommand) NonNullList(net.minecraft.core.NonNullList) Pair(com.mojang.datafixers.util.Pair) NonNullList(net.minecraft.core.NonNullList) ItemStack(net.minecraft.world.item.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack)

Example 90 with org.bukkit.craftbukkit.v1_18_R2.entity

use of org.bukkit.craftbukkit.v1_18_R2.entity in project Denizen by DenizenScript.

the class CustomEntityHelperImpl method spawnItemProjectile.

@Override
public ItemProjectile spawnItemProjectile(Location location, ItemStack itemStack) {
    CraftWorld world = (CraftWorld) location.getWorld();
    EntityItemProjectileImpl entity = new EntityItemProjectileImpl(world.getHandle(), location, CraftItemStack.asNMSCopy(itemStack));
    world.getHandle().addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM);
    return entity.getBukkitEntity();
}
Also used : EntityItemProjectileImpl(com.denizenscript.denizen.nms.v1_18.impl.entities.EntityItemProjectileImpl) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld)

Aggregations

LivingEntity (org.bukkit.entity.LivingEntity)114 Entity (org.hypertrace.entity.data.service.v1.Entity)110 SkinnableEntity (net.citizensnpcs.npc.skin.SkinnableEntity)102 Test (org.junit.jupiter.api.Test)95 CraftEntity (org.bukkit.craftbukkit.v1_18_R2.entity.CraftEntity)53 Entity (net.minecraft.world.entity.Entity)42 Location (org.bukkit.Location)41 ArrayList (java.util.ArrayList)39 Entity (com.google.datastore.v1.Entity)33 net.minecraft.world.entity (net.minecraft.world.entity)33 org.bukkit.entity (org.bukkit.entity)32 EnrichedEntity (org.hypertrace.entity.data.service.v1.EnrichedEntity)32 Player (org.bukkit.entity.Player)29 ServerPlayer (net.minecraft.server.level.ServerPlayer)28 LivingEntity (net.minecraft.world.entity.LivingEntity)28 CraftWorld (org.bukkit.craftbukkit.v1_18_R2.CraftWorld)28 CraftPlayer (org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer)27 Event (org.hypertrace.core.datamodel.Event)27 Mob (net.minecraft.world.entity.Mob)25 NPCHolder (net.citizensnpcs.npc.ai.NPCHolder)23