Search in sources :

Example 46 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project MyPet by xXKeyleXx.

the class ControlTarget method shouldStart.

@Override
public boolean shouldStart() {
    if (controlPathfinderGoal == null) {
        if (petEntity.getPathfinder().hasGoal("Control")) {
            controlPathfinderGoal = (Control) petEntity.getPathfinder().getGoal("Control");
        }
    }
    if (controlPathfinderGoal == null) {
        return false;
    }
    if (myPet.getDamage() <= 0 && myPet.getRangedDamage() <= 0) {
        return false;
    }
    if (controlPathfinderGoal.moveTo != null && petEntity.canMove()) {
        Behavior behaviorSkill = myPet.getSkills().get(Behavior.class);
        if (behaviorSkill.isActive()) {
            if (behaviorSkill.getBehavior() == BehaviorMode.Friendly) {
                return false;
            }
        }
        for (net.minecraft.world.entity.LivingEntity entityLiving : this.petEntity.level.getEntitiesOfClass(net.minecraft.world.entity.LivingEntity.class, this.petEntity.getBoundingBox().inflate(this.range, 4.0D, this.range))) {
            if (entityLiving != petEntity && !(entityLiving instanceof ArmorStand)) {
                if (entityLiving instanceof ServerPlayer) {
                    Player targetPlayer = (Player) entityLiving.getBukkitEntity();
                    if (myPet.getOwner().equals(targetPlayer)) {
                        continue;
                    } else if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), targetPlayer, true)) {
                        continue;
                    }
                } else if (entityLiving instanceof TamableAnimal) {
                    // Method: getOwner -> mojang mapping maps that to fx() even tho it still is getOwner.
                    Method getOwnerReflect = ReflectionUtil.getMethod(TamableAnimal.class, "getOwner");
                    TamableAnimal tameable = (TamableAnimal) entityLiving;
                    try {
                        if (tameable.isTame() && getOwnerReflect.invoke(tameable, null) != null) {
                            Player tameableOwner = (Player) ((net.minecraft.world.entity.player.Player) getOwnerReflect.invoke(tameable, null)).getBukkitEntity();
                            if (myPet.getOwner().equals(tameableOwner)) {
                                continue;
                            } else if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), tameableOwner, true)) {
                                continue;
                            }
                        }
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                } else if (entityLiving instanceof EntityMyPet) {
                    MyPet targetMyPet = ((EntityMyPet) entityLiving).getMyPet();
                    if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), targetMyPet.getOwner().getPlayer(), true)) {
                        continue;
                    }
                }
                if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), entityLiving.getBukkitEntity())) {
                    continue;
                }
                if (behaviorSkill != null) {
                    if (behaviorSkill.getBehavior() == BehaviorMode.Raid) {
                        if (entityLiving instanceof TamableAnimal) {
                            continue;
                        } else if (entityLiving instanceof EntityMyPet) {
                            continue;
                        } else if (entityLiving instanceof ServerPlayer) {
                            continue;
                        }
                    }
                }
                controlPathfinderGoal.stopControl();
                this.target = entityLiving;
                return true;
            }
        }
    }
    return false;
}
Also used : CraftPlayer(org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer) Player(org.bukkit.entity.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) TamableAnimal(net.minecraft.world.entity.TamableAnimal) MyPet(de.Keyle.MyPet.api.entity.MyPet) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) ServerPlayer(net.minecraft.server.level.ServerPlayer) Behavior(de.Keyle.MyPet.api.skill.skills.Behavior) Method(java.lang.reflect.Method)

Example 47 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project MyPet by xXKeyleXx.

the class BehaviorAggressiveTarget method shouldStart.

@Override
public boolean shouldStart() {
    Behavior behaviorSkill = myPet.getSkills().get(Behavior.class);
    if (!behaviorSkill.isActive() || behaviorSkill.getBehavior() != BehaviorMode.Aggressive) {
        return false;
    }
    if (myPet.getDamage() <= 0 && myPet.getRangedDamage() <= 0) {
        return false;
    }
    if (!petEntity.canMove()) {
        return false;
    }
    if (petEntity.hasTarget()) {
        return false;
    }
    for (net.minecraft.world.entity.LivingEntity entityLiving : this.petEntity.level.getEntitiesOfClass(net.minecraft.world.entity.LivingEntity.class, this.petOwnerEntity.getBoundingBox().inflate(range, range, range))) {
        if (entityLiving != petEntity && !(entityLiving instanceof ArmorStand) && entityLiving.isAlive() && petEntity.distanceToSqr(entityLiving) <= 91) {
            if (entityLiving instanceof ServerPlayer) {
                Player targetPlayer = (Player) entityLiving.getBukkitEntity();
                if (myPet.getOwner().equals(targetPlayer)) {
                    continue;
                }
                if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), targetPlayer, true)) {
                    continue;
                }
            } else if (entityLiving instanceof EntityMyPet) {
                MyPet targetMyPet = ((EntityMyPet) entityLiving).getMyPet();
                if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), targetMyPet.getOwner().getPlayer(), true)) {
                    continue;
                }
            } else if (entityLiving instanceof TamableAnimal) {
                // Method: getOwner -> mojang mapping maps that to fx() even tho it still is getOwner.
                Method getOwnerReflect = ReflectionUtil.getMethod(TamableAnimal.class, "getOwner");
                TamableAnimal tameable = (TamableAnimal) entityLiving;
                try {
                    if (tameable.isTame() && getOwnerReflect.invoke(tameable, null) != null) {
                        Player tameableOwner = (Player) ((net.minecraft.world.entity.player.Player) getOwnerReflect.invoke(tameable, null)).getBukkitEntity();
                        if (myPet.getOwner().equals(tameableOwner)) {
                            continue;
                        } else if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), tameableOwner, true)) {
                            continue;
                        }
                    }
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            } else if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), entityLiving.getBukkitEntity())) {
                continue;
            }
            this.target = entityLiving;
            return true;
        }
    }
    return false;
}
Also used : CraftPlayer(org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer) Player(org.bukkit.entity.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) TamableAnimal(net.minecraft.world.entity.TamableAnimal) MyPet(de.Keyle.MyPet.api.entity.MyPet) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) ServerPlayer(net.minecraft.server.level.ServerPlayer) Behavior(de.Keyle.MyPet.api.skill.skills.Behavior)

Example 48 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project MyPet by xXKeyleXx.

the class RangedAttack method shouldStart.

@Override
public boolean shouldStart() {
    if (myPet.getRangedDamage() <= 0) {
        return false;
    }
    if (!entityMyPet.canMove()) {
        return false;
    }
    if (!entityMyPet.hasTarget()) {
        return false;
    }
    LivingEntity target = ((CraftLivingEntity) this.entityMyPet.getMyPetTarget()).getHandle();
    if (target instanceof ArmorStand) {
        return false;
    }
    double meleeDamage = myPet.getDamage();
    if (meleeDamage > 0 && this.entityMyPet.distanceToSqr(target.getX(), target.getBoundingBox().minY, target.getZ()) < 4) {
        Ranged rangedSkill = myPet.getSkills().get(Ranged.class);
        if (meleeDamage > rangedSkill.getDamage().getValue().doubleValue()) {
            return false;
        }
    }
    Behavior behaviorSkill = myPet.getSkills().get(Behavior.class);
    if (behaviorSkill != null && behaviorSkill.isActive()) {
        if (behaviorSkill.getBehavior() == Behavior.BehaviorMode.Friendly) {
            return false;
        }
        if (behaviorSkill.getBehavior() == Behavior.BehaviorMode.Raid) {
            if (target instanceof TamableAnimal && ((TamableAnimal) target).isTame()) {
                return false;
            }
            if (target instanceof EntityMyPet) {
                return false;
            }
            if (target instanceof ServerPlayer) {
                return false;
            }
        }
    }
    this.target = target;
    return true;
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity) Ranged(de.Keyle.MyPet.api.skill.skills.Ranged) EntityMyPet(de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) TamableAnimal(net.minecraft.world.entity.TamableAnimal) ServerPlayer(net.minecraft.server.level.ServerPlayer) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity) Behavior(de.Keyle.MyPet.api.skill.skills.Behavior)

Example 49 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project MyPet by xXKeyleXx.

the class EntityMyPig method handlePlayerInteraction.

@Override
public InteractionResult handlePlayerInteraction(final net.minecraft.world.entity.player.Player entityhuman, InteractionHand enumhand, final ItemStack itemStack) {
    if (enumhand == InteractionHand.OFF_HAND) {
        if (itemStack != null) {
            if (itemStack.getItem() == Items.LEAD) {
                ((ServerLevel) this.level).getChunkSource().broadcastAndSend(this, new ClientboundSetEntityLinkPacket(this, null));
                entityhuman.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
                new BukkitRunnable() {

                    @Override
                    public void run() {
                        if (entityhuman instanceof ServerPlayer) {
                            // TODO
                            entityhuman.setItemInHand(InteractionHand.OFF_HAND, itemStack);
                            Player p = (Player) entityhuman.getBukkitEntity();
                            if (!p.isOnline()) {
                                p.saveData();
                            }
                        }
                    }
                }.runTaskLater(MyPetApi.getPlugin(), 5);
            }
        }
        return InteractionResult.CONSUME;
    }
    if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
        return InteractionResult.CONSUME;
    }
    if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) {
        if (itemStack.getItem() == Items.SADDLE && !getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
            getMyPet().setSaddle(CraftItemStack.asBukkitCopy(itemStack));
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            return InteractionResult.CONSUME;
        } else if (itemStack.getItem() == Items.SHEARS && getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
            ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), CraftItemStack.asNMSCopy(getMyPet().getSaddle()));
            entityitem.pickupDelay = 10;
            entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
            this.level.addFreshEntity(entityitem);
            makeSound("entity.sheep.shear", 1.0F, 1.0F);
            getMyPet().setSaddle(null);
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                try {
                    itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> entityhuman1.broadcastBreakEvent(enumhand));
                } catch (Error e) {
                    // TODO REMOVE
                    itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> {
                        try {
                            CompatManager.ENTITY_LIVING_broadcastItemBreak.invoke(entityhuman1, enumhand);
                        } catch (IllegalAccessException | InvocationTargetException ex) {
                            ex.printStackTrace();
                        }
                    });
                }
            }
            return InteractionResult.CONSUME;
        } else if (Configuration.MyPet.Pig.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            getMyPet().setBaby(false);
            return InteractionResult.CONSUME;
        }
    }
    return InteractionResult.PASS;
}
Also used : MyPet(de.Keyle.MyPet.api.entity.MyPet) EntityMyPet(de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet) ClientboundSetEntityLinkPacket(net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket) Items(net.minecraft.world.item.Items) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetApi(de.Keyle.MyPet.MyPetApi) MyPig(de.Keyle.MyPet.api.entity.types.MyPig) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) CompatManager(de.Keyle.MyPet.compat.v1_18_R1.CompatManager) EntityDataAccessor(net.minecraft.network.syncher.EntityDataAccessor) InteractionResult(net.minecraft.world.InteractionResult) Player(org.bukkit.entity.Player) ServerLevel(net.minecraft.server.level.ServerLevel) InvocationTargetException(java.lang.reflect.InvocationTargetException) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) ServerPlayer(net.minecraft.server.level.ServerPlayer) Configuration(de.Keyle.MyPet.api.Configuration) ItemEntity(net.minecraft.world.entity.item.ItemEntity) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) Player(org.bukkit.entity.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemEntity(net.minecraft.world.entity.item.ItemEntity) ClientboundSetEntityLinkPacket(net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket) ServerPlayer(net.minecraft.server.level.ServerPlayer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 50 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class PaintingData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(Painting.class).create(Keys.ART_TYPE).get(h -> (ArtType) h.motive).setAnd((h, v) -> {
        if (!h.level.isClientSide) {
            final Motive oldArt = h.motive;
            h.motive = (Motive) v;
            ((HangingEntityAccessor) h).invoker$setDirection(h.getDirection());
            if (!h.survives()) {
                h.motive = oldArt;
                ((HangingEntityAccessor) h).invoker$setDirection(h.getDirection());
                return false;
            }
            final ChunkMapAccessor chunkManager = (ChunkMapAccessor) ((ServerLevel) h.level).getChunkSource().chunkMap;
            final ChunkMap_TrackedEntityAccessor paintingTracker = chunkManager.accessor$entityMap().get(h.getId());
            if (paintingTracker == null) {
                return true;
            }
            final List<ServerPlayer> players = new ArrayList<>();
            for (final ServerPlayer player : paintingTracker.accessor$seenBy()) {
                final ClientboundRemoveEntitiesPacket packet = new ClientboundRemoveEntitiesPacket(h.getId());
                player.connection.send(packet);
                players.add(player);
            }
            for (final ServerPlayer player : players) {
                SpongeCommon.serverScheduler().submit(Task.builder().plugin(Launch.instance().commonPlugin()).delay(new SpongeTicks(SpongeGameConfigs.getForWorld(h.level).get().entity.painting.respawnDelay)).execute(() -> {
                    final ClientboundAddPaintingPacket packet = new ClientboundAddPaintingPacket(h);
                    player.connection.send(packet);
                }).build());
            }
            return true;
        }
        return true;
    });
}
Also used : ChunkMap_TrackedEntityAccessor(org.spongepowered.common.accessor.server.level.ChunkMap_TrackedEntityAccessor) ClientboundRemoveEntitiesPacket(net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket) Painting(net.minecraft.world.entity.decoration.Painting) Launch(org.spongepowered.common.launch.Launch) ChunkMapAccessor(org.spongepowered.common.accessor.server.level.ChunkMapAccessor) SpongeGameConfigs(org.spongepowered.common.config.SpongeGameConfigs) SpongeCommon(org.spongepowered.common.SpongeCommon) ArtType(org.spongepowered.api.data.type.ArtType) ServerLevel(net.minecraft.server.level.ServerLevel) ArrayList(java.util.ArrayList) HangingEntityAccessor(org.spongepowered.common.accessor.world.entity.decoration.HangingEntityAccessor) ServerPlayer(net.minecraft.server.level.ServerPlayer) Keys(org.spongepowered.api.data.Keys) List(java.util.List) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) Task(org.spongepowered.api.scheduler.Task) SpongeTicks(org.spongepowered.common.util.SpongeTicks) Motive(net.minecraft.world.entity.decoration.Motive) ClientboundAddPaintingPacket(net.minecraft.network.protocol.game.ClientboundAddPaintingPacket) ServerLevel(net.minecraft.server.level.ServerLevel) ClientboundRemoveEntitiesPacket(net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket) ClientboundAddPaintingPacket(net.minecraft.network.protocol.game.ClientboundAddPaintingPacket) ChunkMapAccessor(org.spongepowered.common.accessor.server.level.ChunkMapAccessor) ArrayList(java.util.ArrayList) Painting(net.minecraft.world.entity.decoration.Painting) HangingEntityAccessor(org.spongepowered.common.accessor.world.entity.decoration.HangingEntityAccessor) Motive(net.minecraft.world.entity.decoration.Motive) ArtType(org.spongepowered.api.data.type.ArtType) ChunkMap_TrackedEntityAccessor(org.spongepowered.common.accessor.server.level.ChunkMap_TrackedEntityAccessor) ServerPlayer(net.minecraft.server.level.ServerPlayer) SpongeTicks(org.spongepowered.common.util.SpongeTicks)

Aggregations

ServerPlayer (net.minecraft.server.level.ServerPlayer)71 TamableAnimal (net.minecraft.world.entity.TamableAnimal)12 Behavior (de.Keyle.MyPet.api.skill.skills.Behavior)11 ServerLevel (net.minecraft.server.level.ServerLevel)11 ArmorStand (net.minecraft.world.entity.decoration.ArmorStand)11 ItemStack (net.minecraft.world.item.ItemStack)10 CraftPlayer (org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer)10 Player (org.bukkit.entity.Player)10 MyPet (de.Keyle.MyPet.api.entity.MyPet)8 EntityMyPet (de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet)8 CraftPlayer (org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer)8 Player (net.minecraft.world.entity.player.Player)7 EntityMyPet (de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet)6 ArrayList (java.util.ArrayList)6 ClientboundRemoveEntitiesPacket (net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket)6 ChunkMap (net.minecraft.server.level.ChunkMap)6 ServerGamePacketListenerImpl (net.minecraft.server.network.ServerGamePacketListenerImpl)6 LivingEntity (net.minecraft.world.entity.LivingEntity)6 ItemEntity (net.minecraft.world.entity.item.ItemEntity)6 PlayerEntityTagMessage (com.wuest.prefab.proxy.messages.PlayerEntityTagMessage)5