use of org.bukkit.craftbukkit.v1_18_R2.entity in project Denizen by DenizenScript.
the class EntityHelperImpl method follow.
@Override
public void follow(final Entity target, final Entity follower, final double speed, final double lead, final double maxRange, final boolean allowWander, final boolean teleport) {
if (target == null || follower == null) {
return;
}
final net.minecraft.world.entity.Entity nmsEntityFollower = ((CraftEntity) follower).getHandle();
if (!(nmsEntityFollower instanceof Mob)) {
return;
}
final Mob nmsFollower = (Mob) nmsEntityFollower;
final PathNavigation followerNavigation = nmsFollower.getNavigation();
UUID uuid = follower.getUniqueId();
if (followTasks.containsKey(uuid)) {
followTasks.get(uuid).cancel();
}
final int locationNearInt = (int) Math.floor(lead);
final boolean hasMax = maxRange > lead;
followTasks.put(follower.getUniqueId(), new BukkitRunnable() {
private boolean inRadius = false;
public void run() {
if (!target.isValid() || !follower.isValid()) {
this.cancel();
}
followerNavigation.setSpeedModifier(2D);
Location targetLocation = target.getLocation();
Path path;
if (hasMax && !Utilities.checkLocation(targetLocation, follower.getLocation(), maxRange) && !target.isDead() && target.isOnGround()) {
if (!inRadius) {
if (teleport) {
follower.teleport(Utilities.getWalkableLocationNear(targetLocation, locationNearInt));
} else {
cancel();
}
} else {
inRadius = false;
path = followerNavigation.createPath(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ(), 0);
if (path != null) {
followerNavigation.moveTo(path, 1D);
followerNavigation.setSpeedModifier(2D);
}
}
} else if (!inRadius && !Utilities.checkLocation(targetLocation, follower.getLocation(), lead)) {
path = followerNavigation.createPath(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ(), 0);
if (path != null) {
followerNavigation.moveTo(path, 1D);
followerNavigation.setSpeedModifier(2D);
}
} else {
inRadius = true;
}
if (inRadius && !allowWander) {
followerNavigation.stop();
}
nmsFollower.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(speed);
}
}.runTaskTimer(NMSHandler.getJavaPlugin(), 0, 10));
}
use of org.bukkit.craftbukkit.v1_18_R2.entity in project Denizen by DenizenScript.
the class EntityHelperImpl method sendShowPacket.
@Override
public void sendShowPacket(Player pl, Entity entity) {
if (entity instanceof Player) {
pl.showPlayer(Denizen.getInstance(), (Player) entity);
return;
}
CraftPlayer craftPlayer = (CraftPlayer) pl;
ServerPlayer entityPlayer = craftPlayer.getHandle();
if (entityPlayer.connection != null && !craftPlayer.equals(entity)) {
ChunkMap tracker = ((ServerLevel) craftPlayer.getHandle().level).getChunkProvider().chunkMap;
net.minecraft.world.entity.Entity other = ((CraftEntity) entity).getHandle();
ChunkMap.TrackedEntity entry = tracker.G.get(other.getId());
if (entry != null) {
entry.removePlayer(entityPlayer);
entry.updatePlayer(entityPlayer);
}
}
}
use of org.bukkit.craftbukkit.v1_18_R2.entity in project Denizen 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;
}
use of org.bukkit.craftbukkit.v1_18_R2.entity in project Denizen by DenizenScript.
the class DenizenNetworkManagerImpl method processDisguiseForPacket.
public boolean processDisguiseForPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
if (DisguiseCommand.disguises.isEmpty() || antiDuplicate) {
return false;
}
try {
int ider = -1;
if (packet instanceof ClientboundSetEntityDataPacket) {
ider = ((ClientboundSetEntityDataPacket) packet).getId();
}
if (packet instanceof ClientboundUpdateAttributesPacket) {
ider = ((ClientboundUpdateAttributesPacket) packet).getEntityId();
}
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();
}
if (ider != -1) {
Entity e = player.getLevel().getEntity(ider);
if (e == null) {
return false;
}
HashMap<UUID, DisguiseCommand.TrackedDisguise> playerMap = DisguiseCommand.disguises.get(e.getUUID());
if (playerMap == null) {
return false;
}
DisguiseCommand.TrackedDisguise disguise = playerMap.get(player.getUUID());
if (disguise == null) {
disguise = playerMap.get(null);
if (disguise == null) {
return false;
}
}
if (!disguise.isActive) {
return false;
}
if (NMSHandler.debugPackets) {
doPacketOutput("DISGUISED packet " + packet.getClass().getName() + " for entity " + ider + " to player " + player.getScoreboardName());
}
if (packet instanceof ClientboundSetEntityDataPacket) {
ClientboundSetEntityDataPacket metadataPacket = (ClientboundSetEntityDataPacket) packet;
if (e.getId() == player.getId()) {
if (!disguise.shouldFake) {
return false;
}
List<SynchedEntityData.DataItem<?>> data = metadataPacket.getUnpackedData();
for (SynchedEntityData.DataItem item : data) {
EntityDataAccessor<?> watcherObject = item.getAccessor();
int watcherId = watcherObject.getId();
if (watcherId == 0) {
// Entity flags
ClientboundSetEntityDataPacket altPacket = new ClientboundSetEntityDataPacket(copyPacket(metadataPacket));
data = new ArrayList<>(data);
ENTITY_METADATA_LIST.set(altPacket, data);
data.remove(item);
byte flags = (byte) item.getValue();
// Invisible flag
flags |= 0x20;
data.add(new SynchedEntityData.DataItem(watcherObject, flags));
ClientboundSetEntityDataPacket updatedPacket = getModifiedMetadataFor(altPacket);
oldManager.send(updatedPacket == null ? altPacket : updatedPacket, genericfuturelistener);
return true;
}
}
} else {
ClientboundSetEntityDataPacket altPacket = new ClientboundSetEntityDataPacket(e.getId(), ((CraftEntity) disguise.toOthers.entity.entity).getHandle().getEntityData(), true);
oldManager.send(altPacket, genericfuturelistener);
return true;
}
return false;
}
if (packet instanceof ClientboundUpdateAttributesPacket) {
FakeEntity fake = ider == player.getId() ? disguise.fakeToSelf : disguise.toOthers;
if (fake == null) {
return false;
}
if (fake.entity.entity instanceof LivingEntity) {
return false;
}
// Non-living don't have attributes
return true;
}
antiDuplicate = true;
disguise.sendTo(Collections.singletonList(new PlayerTag(player.getBukkitEntity())));
antiDuplicate = false;
return true;
}
} catch (Throwable ex) {
antiDuplicate = false;
Debug.echoError(ex);
}
return false;
}
use of org.bukkit.craftbukkit.v1_18_R2.entity in project Denizen 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) {
doPacketOutput("Attach Velocity Packet: " + pNew.getClass().getCanonicalName() + " for " + att.attached.getUUID() + " sent to " + player.getScoreboardName());
}
oldManager.send(pNew);
}
}
}
if (e.passengers != null && !e.passengers.isEmpty()) {
for (Entity ent : e.passengers) {
tryProcessVelocityPacketForAttach(packet, ent);
}
}
}
Aggregations