Search in sources :

Example 1 with PlayerLeashEntityHook

use of de.Keyle.MyPet.api.util.hooks.types.PlayerLeashEntityHook in project MyPet by xXKeyleXx.

the class EntityListener method on.

@EventHandler(priority = EventPriority.MONITOR)
public void on(final EntityDamageByEntityEvent event) {
    if (!event.getEntity().isDead() && !(event.getEntity() instanceof MyPetBukkitEntity)) {
        if (MyPetApi.getMyPetInfo().isLeashableEntityType(event.getEntity().getType())) {
            ItemStack leashItem = null;
            ItemStack leashItemArrow = null;
            Player player;
            if (Configuration.Misc.ALLOW_RANGED_LEASHING && event.getDamager() instanceof Projectile) {
                Projectile projectile = (Projectile) event.getDamager();
                if (!(projectile.getShooter() instanceof Player)) {
                    return;
                }
                player = (Player) projectile.getShooter();
                List<MetadataValue> metaList;
                if (projectile.hasMetadata("MyPetLeashItem")) {
                    metaList = projectile.getMetadata("MyPetLeashItem");
                    for (MetadataValue meta : metaList) {
                        if (meta.getOwningPlugin() == MyPetApi.getPlugin()) {
                            leashItem = (ItemStack) meta.value();
                            break;
                        }
                    }
                    if (leashItem == null) {
                        return;
                    }
                }
                if (projectile.hasMetadata("")) {
                    metaList = projectile.getMetadata("MyPetLeashItemArrow");
                    for (MetadataValue meta : metaList) {
                        if (meta.getOwningPlugin() == MyPetApi.getPlugin()) {
                            leashItemArrow = (ItemStack) meta.value();
                            break;
                        }
                    }
                    if (leashItemArrow == null) {
                        return;
                    }
                }
            } else if (event.getDamager() instanceof Player) {
                player = (Player) event.getDamager();
                if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
                    leashItem = player.getEquipment().getItemInMainHand();
                } else {
                    leashItem = player.getItemInHand();
                }
            } else {
                return;
            }
            if (!MyPetApi.getMyPetManager().hasActiveMyPet(player)) {
                LivingEntity leashTarget = (LivingEntity) event.getEntity();
                MyPetType petType = MyPetType.byEntityTypeName(leashTarget.getType().name());
                ConfigItem neededLeashItem = MyPetApi.getMyPetInfo().getLeashItem(petType);
                if (!Permissions.hasLegacy(player, "MyPet.leash.", petType.name())) {
                    return;
                }
                boolean usedArrow = false;
                if (!neededLeashItem.compare(leashItem)) {
                    if (leashItemArrow == null || !neededLeashItem.compare(leashItemArrow)) {
                        return;
                    } else {
                        usedArrow = true;
                    }
                }
                for (PlayerLeashEntityHook hook : MyPetApi.getPluginHookManager().getHooks(PlayerLeashEntityHook.class)) {
                    if (!hook.canLeash(player, leashTarget)) {
                        return;
                    }
                }
                boolean willBeLeashed = true;
                flagLoop: for (LeashFlag flag : MyPetApi.getMyPetInfo().getLeashFlags(petType)) {
                    switch(flag) {
                        case Adult:
                            if (leashTarget instanceof Ageable) {
                                willBeLeashed = ((Ageable) leashTarget).isAdult();
                            } else if (leashTarget instanceof Zombie) {
                                willBeLeashed = !((Zombie) leashTarget).isBaby();
                            }
                            break;
                        case Baby:
                            if (leashTarget instanceof Ageable) {
                                willBeLeashed = !((Ageable) leashTarget).isAdult();
                            } else if (leashTarget instanceof Zombie) {
                                willBeLeashed = ((Zombie) leashTarget).isBaby();
                            }
                            break;
                        case LowHp:
                            willBeLeashed = ((leashTarget.getHealth() - event.getDamage()) * 100) / leashTarget.getMaxHealth() <= 10;
                            break;
                        case UserCreated:
                            if (leashTarget instanceof IronGolem) {
                                willBeLeashed = ((IronGolem) leashTarget).isPlayerCreated();
                            }
                            break;
                        case Wild:
                            if (leashTarget instanceof IronGolem) {
                                willBeLeashed = !((IronGolem) leashTarget).isPlayerCreated();
                            } else if (leashTarget instanceof Tameable) {
                                willBeLeashed = !((Tameable) leashTarget).isTamed();
                            } else if (leashTarget instanceof Horse) {
                                willBeLeashed = !((Horse) leashTarget).isTamed();
                            }
                            break;
                        case Tamed:
                            if (leashTarget instanceof Tameable) {
                                willBeLeashed = ((Tameable) leashTarget).isTamed() && ((Tameable) leashTarget).getOwner() == player;
                            } else if (leashTarget instanceof Horse) {
                                willBeLeashed = ((Horse) leashTarget).isTamed() && ((Horse) leashTarget).getOwner() == player;
                            }
                            break;
                        case CanBreed:
                            if (leashTarget instanceof Ageable) {
                                willBeLeashed = ((Ageable) leashTarget).canBreed();
                            }
                            break;
                        case Angry:
                            if (leashTarget instanceof Wolf) {
                                willBeLeashed = ((Wolf) leashTarget).isAngry();
                            }
                            break;
                        case Impossible:
                            willBeLeashed = false;
                            break flagLoop;
                        case None:
                            willBeLeashed = true;
                            break flagLoop;
                    }
                    if (!willBeLeashed) {
                        break;
                    }
                }
                if (willBeLeashed) {
                    event.setCancelled(true);
                    final MyPetPlayer owner;
                    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
                        owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
                    } else {
                        owner = MyPetApi.getPlayerManager().registerMyPetPlayer(player);
                    }
                    final InactiveMyPet inactiveMyPet = new InactiveMyPet(owner);
                    inactiveMyPet.setPetType(petType);
                    inactiveMyPet.setPetName(Translation.getString("Name." + petType.name(), inactiveMyPet.getOwner().getLanguage()));
                    WorldGroup worldGroup = WorldGroup.getGroupByWorld(player.getWorld().getName());
                    inactiveMyPet.setWorldGroup(worldGroup.getName());
                    inactiveMyPet.getOwner().setMyPetForWorldGroup(worldGroup, inactiveMyPet.getUUID());
                    /*
                        if(leashTarget.getCustomName() != null)
                        {
                            inactiveMyPet.setPetName(leashTarget.getCustomName());
                        }
                        */
                    Optional<EntityConverterService> converter = MyPetApi.getServiceManager().getService(EntityConverterService.class);
                    if (converter.isPresent()) {
                        inactiveMyPet.setInfo(converter.get().convertEntity(leashTarget));
                    }
                    leashTarget.remove();
                    if (!usedArrow) {
                        if (Configuration.Misc.CONSUME_LEASH_ITEM && player.getGameMode() != GameMode.CREATIVE && leashItem != null) {
                            if (leashItem.getAmount() > 1) {
                                leashItem.setAmount(leashItem.getAmount() - 1);
                            } else {
                                if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
                                    player.getEquipment().setItemInMainHand(null);
                                } else {
                                    player.setItemInHand(null);
                                }
                            }
                        }
                    }
                    MyPetCreateEvent createEvent = new MyPetCreateEvent(inactiveMyPet, MyPetCreateEvent.Source.Leash);
                    Bukkit.getServer().getPluginManager().callEvent(createEvent);
                    MyPetSaveEvent saveEvent = new MyPetSaveEvent(inactiveMyPet);
                    Bukkit.getServer().getPluginManager().callEvent(saveEvent);
                    MyPetApi.getPlugin().getRepository().addMyPet(inactiveMyPet, new RepositoryCallback<Boolean>() {

                        @Override
                        public void callback(Boolean value) {
                            owner.sendMessage(Translation.getString("Message.Leash.Add", owner));
                            Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(inactiveMyPet);
                            if (myPet.isPresent()) {
                                myPet.get().createEntity();
                            }
                        }
                    });
                }
            }
        }
    }
}
Also used : EntityConverterService(de.Keyle.MyPet.api.util.service.types.EntityConverterService) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) ConfigItem(de.Keyle.MyPet.api.util.ConfigItem) PlayerLeashEntityHook(de.Keyle.MyPet.api.util.hooks.types.PlayerLeashEntityHook) MetadataValue(org.bukkit.metadata.MetadataValue) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) Optional(com.google.common.base.Optional) WorldGroup(de.Keyle.MyPet.api.WorldGroup) EntityMyPetProjectile(de.Keyle.MyPet.api.skill.skills.ranged.EntityMyPetProjectile) CraftMyPetProjectile(de.Keyle.MyPet.api.skill.skills.ranged.CraftMyPetProjectile) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Optional (com.google.common.base.Optional)1 WorldGroup (de.Keyle.MyPet.api.WorldGroup)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 CraftMyPetProjectile (de.Keyle.MyPet.api.skill.skills.ranged.CraftMyPetProjectile)1 EntityMyPetProjectile (de.Keyle.MyPet.api.skill.skills.ranged.EntityMyPetProjectile)1 ConfigItem (de.Keyle.MyPet.api.util.ConfigItem)1 PlayerLeashEntityHook (de.Keyle.MyPet.api.util.hooks.types.PlayerLeashEntityHook)1 EntityConverterService (de.Keyle.MyPet.api.util.service.types.EntityConverterService)1 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)1 EventHandler (org.bukkit.event.EventHandler)1 ItemStack (org.bukkit.inventory.ItemStack)1 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)1 MetadataValue (org.bukkit.metadata.MetadataValue)1