Search in sources :

Example 6 with Entity

use of net.minecraft.world.entity.Entity in project Denizen-For-Bukkit 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 : CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) Entity(net.minecraft.world.entity.Entity)

Example 7 with Entity

use of net.minecraft.world.entity.Entity in project Denizen-For-Bukkit 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_R1.entity.CraftEntity) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData)

Example 8 with Entity

use of net.minecraft.world.entity.Entity in project Denizen-For-Bukkit by DenizenScript.

the class EnchantmentHelperImpl method registerFakeEnchantment.

@Override
public org.bukkit.enchantments.Enchantment registerFakeEnchantment(EnchantmentScriptContainer.EnchantmentReference script) {
    try {
        EquipmentSlot[] slots = new EquipmentSlot[script.script.slots.size()];
        for (int i = 0; i < slots.length; i++) {
            slots[i] = EquipmentSlot.valueOf(script.script.slots.get(i).toUpperCase());
        }
        net.minecraft.world.item.enchantment.Enchantment nmsEnchant = new net.minecraft.world.item.enchantment.Enchantment(net.minecraft.world.item.enchantment.Enchantment.Rarity.valueOf(script.script.rarity), EnchantmentCategory.valueOf(script.script.category), slots) {

            @Override
            public int getMinLevel() {
                return script.script.minLevel;
            }

            @Override
            public int getMaxLevel() {
                return script.script.maxLevel;
            }

            @Override
            public int getMinCost(int level) {
                return script.script.getMinCost(level);
            }

            @Override
            public int getMaxCost(int level) {
                return script.script.getMaxCost(level);
            }

            @Override
            public int getDamageProtection(int level, DamageSource src) {
                return script.script.getDamageProtection(level, src.msgId, src.getEntity() == null ? null : src.getEntity().getBukkitEntity());
            }

            @Override
            public float getDamageBonus(int level, MobType type) {
                String typeName = "UNDEFINED";
                if (type == MobType.ARTHROPOD) {
                    typeName = "ARTHROPOD";
                } else if (type == MobType.ILLAGER) {
                    typeName = "ILLAGER";
                } else if (type == MobType.UNDEAD) {
                    typeName = "UNDEAD";
                } else if (type == MobType.WATER) {
                    typeName = "WATER";
                }
                return script.script.getDamageBonus(level, typeName);
            }

            @Override
            protected boolean checkCompatibility(net.minecraft.world.item.enchantment.Enchantment nmsEnchantment) {
                ResourceLocation nmsKey = Registry.ENCHANTMENT.getKey(nmsEnchantment);
                NamespacedKey bukkitKey = CraftNamespacedKey.fromMinecraft(nmsKey);
                org.bukkit.enchantments.Enchantment bukkitEnchant = CraftEnchantment.getByKey(bukkitKey);
                return script.script.isCompatible(bukkitEnchant);
            }

            @Override
            protected String getOrCreateDescriptionId() {
                return script.script.descriptionId;
            }

            @Override
            public String getDescriptionId() {
                return script.script.descriptionId;
            }

            @Override
            public Component getFullname(int level) {
                return Handler.componentToNMS(script.script.getFullName(level));
            }

            @Override
            public boolean canEnchant(net.minecraft.world.item.ItemStack var0) {
                return super.canEnchant(var0) && script.script.canEnchant(CraftItemStack.asBukkitCopy(var0));
            }

            @Override
            public void doPostAttack(LivingEntity attacker, Entity victim, int level) {
                script.script.doPostAttack(attacker.getBukkitEntity(), victim.getBukkitEntity(), level);
            }

            @Override
            public void doPostHurt(LivingEntity victim, Entity attacker, int level) {
                script.script.doPostHurt(victim.getBukkitEntity(), attacker.getBukkitEntity(), level);
            }

            @Override
            public boolean isTreasureOnly() {
                return script.script.isTreasureOnly;
            }

            @Override
            public boolean isCurse() {
                return script.script.isCurse;
            }

            @Override
            public boolean isTradeable() {
                return script.script.isTradable;
            }

            @Override
            public boolean isDiscoverable() {
                return script.script.isDiscoverable;
            }
        };
        String enchName = script.script.id.toUpperCase();
        Registry.register(Registry.ENCHANTMENT, "denizen:" + script.script.id, nmsEnchant);
        CraftEnchantment ench = new CraftEnchantment(nmsEnchant) {

            @Override
            public String getName() {
                return enchName;
            }
        };
        ENCHANTMENTS_BY_KEY.put(ench.getKey(), ench);
        ENCHANTMENTS_BY_NAME.put(enchName, ench);
        return ench;
    } catch (Throwable ex) {
        Debug.echoError("Failed to register enchantment " + script.script.id);
        Debug.echoError(ex);
        return null;
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) Entity(net.minecraft.world.entity.Entity) DamageSource(net.minecraft.world.damagesource.DamageSource) Enchantment(org.bukkit.enchantments.Enchantment) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) MobType(net.minecraft.world.entity.MobType) LivingEntity(net.minecraft.world.entity.LivingEntity) NamespacedKey(org.bukkit.NamespacedKey) CraftNamespacedKey(org.bukkit.craftbukkit.v1_17_R1.util.CraftNamespacedKey) ResourceLocation(net.minecraft.resources.ResourceLocation) Enchantment(org.bukkit.enchantments.Enchantment) CraftEnchantment(org.bukkit.craftbukkit.v1_17_R1.enchantments.CraftEnchantment) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) CraftEnchantment(org.bukkit.craftbukkit.v1_17_R1.enchantments.CraftEnchantment)

Example 9 with Entity

use of net.minecraft.world.entity.Entity in project Denizen-For-Bukkit 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 : CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) Entity(net.minecraft.world.entity.Entity) 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_17_R1.inventory.CraftItemStack)

Example 10 with Entity

use of net.minecraft.world.entity.Entity in project Denizen-For-Bukkit by DenizenScript.

the class DenizenNetworkManagerImpl method tryProcessVelocityPacketForAttach.

public void tryProcessVelocityPacketForAttach(ClientboundSetEntityMotionPacket packet, Entity e) throws IllegalAccessException {
    EntityAttachmentHelper.EntityAttachedToMap attList = EntityAttachmentHelper.toEntityToData.get(e.getUUID());
    if (attList != null) {
        for (EntityAttachmentHelper.PlayerAttachMap attMap : attList.attachedToMap.values()) {
            EntityAttachmentHelper.AttachmentData att = attMap.getAttachment(player.getUUID());
            if (attMap.attached.isValid() && att != null) {
                ClientboundSetEntityMotionPacket pNew = new ClientboundSetEntityMotionPacket(copyPacket(packet));
                ENTITY_ID_PACKVELENT.setInt(pNew, att.attached.getBukkitEntity().getEntityId());
                if (NMSHandler.debugPackets) {
                    Debug.log("Attach Velocity Packet: " + pNew.getClass().getCanonicalName() + " for " + att.attached.getUUID() + " sent to " + player.getName());
                }
                oldManager.send(pNew);
            }
        }
    }
    if (e.passengers != null && !e.passengers.isEmpty()) {
        for (Entity ent : e.passengers) {
            tryProcessVelocityPacketForAttach(packet, ent);
        }
    }
}
Also used : CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) Entity(net.minecraft.world.entity.Entity) EntityAttachmentHelper(com.denizenscript.denizen.utilities.entity.EntityAttachmentHelper)

Aggregations

Entity (net.minecraft.world.entity.Entity)22 CraftEntity (org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity)9 CraftEntity (org.bukkit.craftbukkit.v1_18_R1.entity.CraftEntity)9 FakeEntity (com.denizenscript.denizen.utilities.entity.FakeEntity)8 LivingEntity (org.bukkit.entity.LivingEntity)8 EntityAttachmentHelper (com.denizenscript.denizen.utilities.entity.EntityAttachmentHelper)6 SynchedEntityData (net.minecraft.network.syncher.SynchedEntityData)4 LivingEntity (net.minecraft.world.entity.LivingEntity)4 Vector (org.bukkit.util.Vector)4 EquipmentSlot (net.minecraft.world.entity.EquipmentSlot)3 ItemStack (net.minecraft.world.item.ItemStack)3 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)2 FakeEquipCommand (com.denizenscript.denizen.scripts.commands.entity.FakeEquipCommand)2 DisguiseCommand (com.denizenscript.denizen.scripts.commands.player.DisguiseCommand)2 Pair (com.mojang.datafixers.util.Pair)2 BlockPos (net.minecraft.core.BlockPos)2 NonNullList (net.minecraft.core.NonNullList)2 SectionPos (net.minecraft.core.SectionPos)2 ResourceLocation (net.minecraft.resources.ResourceLocation)2 DamageSource (net.minecraft.world.damagesource.DamageSource)2