Search in sources :

Example 26 with org.bukkit.craftbukkit.v1_17_R1.entity

use of org.bukkit.craftbukkit.v1_17_R1.entity in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method getDamageTo.

@Override
public double getDamageTo(LivingEntity attacker, Entity target) {
    MobType monsterType;
    if (target instanceof LivingEntity) {
        monsterType = ((CraftLivingEntity) target).getHandle().getMobType();
    } else {
        monsterType = MobType.UNDEFINED;
    }
    double damage = 0;
    AttributeInstance attrib = attacker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
    if (attrib != null) {
        damage = attrib.getValue();
    }
    if (attacker.getEquipment() != null && attacker.getEquipment().getItemInMainHand() != null) {
        damage += EnchantmentHelper.getDamageBonus(CraftItemStack.asNMSCopy(attacker.getEquipment().getItemInMainHand()), monsterType);
    }
    if (damage <= 0) {
        return 0;
    }
    if (target != null) {
        DamageSource source;
        if (attacker instanceof Player) {
            source = DamageSource.playerAttack(((CraftPlayer) attacker).getHandle());
        } else {
            source = DamageSource.mobAttack(((CraftLivingEntity) attacker).getHandle());
        }
        net.minecraft.world.entity.Entity nmsTarget = ((CraftEntity) target).getHandle();
        if (nmsTarget.isInvulnerableTo(source)) {
            return 0;
        }
        if (!(nmsTarget instanceof net.minecraft.world.entity.LivingEntity)) {
            return damage;
        }
        net.minecraft.world.entity.LivingEntity livingTarget = (net.minecraft.world.entity.LivingEntity) nmsTarget;
        damage = CombatRules.getDamageAfterAbsorb((float) damage, (float) livingTarget.getArmorValue(), (float) livingTarget.getAttributeValue(Attributes.ARMOR_TOUGHNESS));
        int enchantDamageModifier = EnchantmentHelper.getDamageProtection(livingTarget.getArmorSlots(), source);
        if (enchantDamageModifier > 0) {
            damage = CombatRules.getDamageAfterMagicAbsorb((float) damage, (float) enchantDamageModifier);
        }
    }
    return damage;
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) DamageSource(net.minecraft.world.damagesource.DamageSource) LivingEntity(org.bukkit.entity.LivingEntity) AttributeInstance(org.bukkit.attribute.AttributeInstance) org.bukkit.entity(org.bukkit.entity) org.bukkit.craftbukkit.v1_18_R1.entity(org.bukkit.craftbukkit.v1_18_R1.entity) net.minecraft.world.entity(net.minecraft.world.entity)

Example 27 with org.bukkit.craftbukkit.v1_17_R1.entity

use of org.bukkit.craftbukkit.v1_17_R1.entity in project Denizen-For-Bukkit by DenizenScript.

the class DenizenNetworkManagerImpl method tryProcessMovePacketForAttach.

public void tryProcessMovePacketForAttach(ClientboundMoveEntityPacket 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) {
                ClientboundMoveEntityPacket pNew;
                int newId = att.attached.getBukkitEntity().getEntityId();
                if (packet instanceof ClientboundMoveEntityPacket.Pos) {
                    pNew = new ClientboundMoveEntityPacket.Pos(newId, packet.getXa(), packet.getYa(), packet.getZa(), packet.isOnGround());
                } else if (packet instanceof ClientboundMoveEntityPacket.Rot) {
                    pNew = new ClientboundMoveEntityPacket.Rot(newId, packet.getyRot(), packet.getxRot(), packet.isOnGround());
                } else if (packet instanceof ClientboundMoveEntityPacket.PosRot) {
                    pNew = new ClientboundMoveEntityPacket.PosRot(newId, packet.getXa(), packet.getYa(), packet.getZa(), packet.getyRot(), packet.getxRot(), packet.isOnGround());
                } else {
                    if (Debug.verbose) {
                        Debug.echoError("Impossible move-entity packet class: " + packet.getClass().getCanonicalName());
                    }
                    return;
                }
                if (att.positionalOffset != null && (packet instanceof ClientboundMoveEntityPacket.Pos || packet instanceof ClientboundMoveEntityPacket.PosRot)) {
                    boolean isRotate = packet instanceof ClientboundMoveEntityPacket.PosRot;
                    byte yaw, pitch;
                    if (att.noRotate) {
                        Entity attachedEntity = ((CraftEntity) att.attached.getBukkitEntity()).getHandle();
                        yaw = EntityAttachmentHelper.compressAngle(attachedEntity.getYRot());
                        pitch = EntityAttachmentHelper.compressAngle(attachedEntity.getXRot());
                    } else if (isRotate) {
                        yaw = packet.getyRot();
                        pitch = packet.getxRot();
                    } else {
                        yaw = EntityAttachmentHelper.compressAngle(e.getYRot());
                        pitch = EntityAttachmentHelper.compressAngle(e.getXRot());
                    }
                    if (att.noPitch) {
                        Entity attachedEntity = ((CraftEntity) att.attached.getBukkitEntity()).getHandle();
                        pitch = EntityAttachmentHelper.compressAngle(attachedEntity.getXRot());
                    }
                    byte newYaw = yaw;
                    if (isRotate) {
                        newYaw = EntityAttachmentHelper.adaptedCompressedAngle(newYaw, att.positionalOffset.getYaw());
                        pitch = EntityAttachmentHelper.adaptedCompressedAngle(pitch, att.positionalOffset.getPitch());
                    }
                    Vector goalPosition = att.fixedForOffset(new Vector(e.getX(), e.getY(), e.getZ()), e.getYRot(), e.getXRot());
                    Vector oldPos = att.visiblePositions.get(player.getUUID());
                    boolean forceTele = false;
                    if (oldPos == null) {
                        oldPos = att.attached.getLocation().toVector();
                        forceTele = true;
                    }
                    Vector moveNeeded = goalPosition.clone().subtract(oldPos);
                    att.visiblePositions.put(player.getUUID(), goalPosition.clone());
                    int offX = (int) (moveNeeded.getX() * (32 * 128));
                    int offY = (int) (moveNeeded.getY() * (32 * 128));
                    int offZ = (int) (moveNeeded.getZ() * (32 * 128));
                    if (forceTele || offX < Short.MIN_VALUE || offX > Short.MAX_VALUE || offY < Short.MIN_VALUE || offY > Short.MAX_VALUE || offZ < Short.MIN_VALUE || offZ > Short.MAX_VALUE) {
                        ClientboundTeleportEntityPacket newTeleportPacket = new ClientboundTeleportEntityPacket(e);
                        ENTITY_ID_PACKTELENT.setInt(newTeleportPacket, att.attached.getBukkitEntity().getEntityId());
                        POS_X_PACKTELENT.setDouble(newTeleportPacket, goalPosition.getX());
                        POS_Y_PACKTELENT.setDouble(newTeleportPacket, goalPosition.getY());
                        POS_Z_PACKTELENT.setDouble(newTeleportPacket, goalPosition.getZ());
                        YAW_PACKTELENT.setByte(newTeleportPacket, newYaw);
                        PITCH_PACKTELENT.setByte(newTeleportPacket, pitch);
                        if (NMSHandler.debugPackets) {
                            Debug.log("Attach Move-Tele Packet: " + newTeleportPacket.getClass().getCanonicalName() + " for " + att.attached.getUUID() + " sent to " + player.getName() + " with original yaw " + yaw + " adapted to " + newYaw);
                        }
                        oldManager.send(newTeleportPacket);
                    } else {
                        POS_X_PACKENT.setShort(pNew, (short) Mth.clamp(offX, Short.MIN_VALUE, Short.MAX_VALUE));
                        POS_Y_PACKENT.setShort(pNew, (short) Mth.clamp(offY, Short.MIN_VALUE, Short.MAX_VALUE));
                        POS_Z_PACKENT.setShort(pNew, (short) Mth.clamp(offZ, Short.MIN_VALUE, Short.MAX_VALUE));
                        if (isRotate) {
                            YAW_PACKENT.setByte(pNew, yaw);
                            PITCH_PACKENT.setByte(pNew, pitch);
                        }
                        if (NMSHandler.debugPackets) {
                            Debug.log("Attach Move Packet: " + pNew.getClass().getCanonicalName() + " for " + att.attached.getUUID() + " sent to " + player.getName() + " with original yaw " + yaw + " adapted to " + newYaw);
                        }
                        oldManager.send(pNew);
                    }
                } else {
                    if (NMSHandler.debugPackets) {
                        Debug.log("Attach Replica-Move 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) {
            tryProcessMovePacketForAttach(packet, ent);
        }
    }
}
Also used : CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) Entity(net.minecraft.world.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) SectionPos(net.minecraft.core.SectionPos) BlockPos(net.minecraft.core.BlockPos) EntityAttachmentHelper(com.denizenscript.denizen.utilities.entity.EntityAttachmentHelper) Vector(org.bukkit.util.Vector)

Example 28 with org.bukkit.craftbukkit.v1_17_R1.entity

use of org.bukkit.craftbukkit.v1_17_R1.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 : CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) Entity(net.minecraft.world.entity.Entity) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData)

Example 29 with org.bukkit.craftbukkit.v1_17_R1.entity

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

Example 30 with org.bukkit.craftbukkit.v1_17_R1.entity

use of org.bukkit.craftbukkit.v1_17_R1.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)

Aggregations

SkinnableEntity (net.citizensnpcs.npc.skin.SkinnableEntity)41 LivingEntity (org.bukkit.entity.LivingEntity)39 org.bukkit.entity (org.bukkit.entity)18 net.minecraft.world.entity (net.minecraft.world.entity)16 Entity (net.minecraft.server.v1_12_R1.Entity)13 Entity (com.google.datastore.v1.Entity)12 Entity (net.minecraft.server.v1_11_R1.Entity)12 Entity (net.minecraft.server.v1_8_R3.Entity)11 CraftEntity (org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity)11 PathEntity (net.minecraft.server.v1_11_R1.PathEntity)9 PathEntity (net.minecraft.server.v1_8_R3.PathEntity)9 Entity (net.minecraft.world.entity.Entity)9 CraftEntity (org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity)9 CraftEntity (org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity)9 NPCHolder (net.citizensnpcs.npc.ai.NPCHolder)8 ServerPlayer (net.minecraft.server.level.ServerPlayer)8 Entity (net.minecraft.server.v1_10_R1.Entity)8 PathEntity (net.minecraft.server.v1_12_R1.PathEntity)8 Mob (net.minecraft.world.entity.Mob)8 CraftEntity (org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity)8