Search in sources :

Example 1 with EntityConverterService

use of de.Keyle.MyPet.api.util.service.types.EntityConverterService in project MyPet by xXKeyleXx.

the class EntityListener method on.

@EventHandler(priority = EventPriority.MONITOR)
public void on(final EntityDamageByEntityEvent event) {
    // noinspection ConstantConditions
    if (event.getEntity() == null) {
        // catch invalid events (i.e. EnchantmentAPI)
        return;
    }
    if (WorldGroup.getGroupByWorld(event.getEntity().getWorld()).isDisabled()) {
        return;
    }
    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().getName().equals("MyPet")) {
                            leashItem = (ItemStack) meta.value();
                            break;
                        }
                    }
                    if (leashItem == null) {
                        return;
                    }
                    projectile.removeMetadata("MyPetLeashItem", MyPetApi.getPlugin());
                }
                if (projectile.hasMetadata("MyPetLeashItemArrow")) {
                    metaList = projectile.getMetadata("MyPetLeashItemArrow");
                    for (MetadataValue meta : metaList) {
                        if (meta.getOwningPlugin().getName().equals("MyPet")) {
                            leashItemArrow = (ItemStack) meta.value();
                            break;
                        }
                    }
                    if (leashItemArrow == null) {
                        return;
                    }
                    projectile.removeMetadata("MyPetLeashItemArrow", MyPetApi.getPlugin());
                }
            } 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) && !justLeashed.contains(player.getUniqueId())) {
                LivingEntity leashTarget = (LivingEntity) event.getEntity();
                MyPetType petType = MyPetType.byEntityTypeName(leashTarget.getType().name());
                ConfigItem neededLeashItem = MyPetApi.getMyPetInfo().getLeashItem(petType);
                if (!Permissions.has(player, "MyPet.leash." + petType.name())) {
                    return;
                }
                boolean usedArrow = false;
                if (!neededLeashItem.compare(leashItem)) {
                    if (leashItemArrow == null || !neededLeashItem.compare(leashItemArrow)) {
                        return;
                    } else {
                        usedArrow = true;
                    }
                }
                for (LeashHook hook : MyPetApi.getPluginHookManager().getHooks(LeashHook.class)) {
                    if (!hook.canLeash(player, leashTarget)) {
                        return;
                    }
                }
                boolean willBeLeashed = true;
                for (Settings flagSettings : MyPetApi.getMyPetInfo().getLeashFlagSettings(petType)) {
                    String flagName = flagSettings.getName();
                    LeashFlag flag = MyPetApi.getLeashFlagManager().getLeashFlag(flagName);
                    if (flag == null) {
                        MyPetApi.getLogger().warning("\"" + flagName + "\" is not a valid leash requirement!");
                        continue;
                    }
                    MyPetPlayer myPetPlayer = null;
                    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
                        myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer(player);
                    }
                    if (!flag.check(player, leashTarget, event.getDamage(), flagSettings)) {
                        willBeLeashed = false;
                        if (myPetPlayer != null) {
                            if (myPetPlayer.isCaptureHelperActive()) {
                                String message = flag.getMissingMessage(player, leashTarget, event.getDamage(), flagSettings);
                                if (message != null) {
                                    myPetPlayer.sendMessage(LeashFlag.getMessagePrefix(false) + message, 10000);
                                }
                            }
                        }
                    } else {
                        if (myPetPlayer != null) {
                            if (myPetPlayer.isCaptureHelperActive()) {
                                String message = flag.getMissingMessage(player, leashTarget, event.getDamage(), flagSettings);
                                if (message != null) {
                                    myPetPlayer.sendMessage(LeashFlag.getMessagePrefix(true) + message, 10000);
                                }
                            }
                        }
                    }
                }
                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()));
                    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);
                    converter.ifPresent(service -> inactiveMyPet.setInfo(service.convertEntity(leashTarget)));
                    boolean remove = true;
                    for (LeashEntityHook hook : MyPetApi.getPluginHookManager().getHooks(LeashEntityHook.class)) {
                        if (!hook.prepare(leashTarget)) {
                            remove = false;
                        }
                    }
                    if (remove) {
                        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);
                    justLeashed.add(player.getUniqueId());
                    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);
                            myPet.ifPresent(MyPet::createEntity);
                            if (owner.isCaptureHelperActive()) {
                                owner.setCaptureHelperActive(false);
                                owner.sendMessage(Util.formatText(Translation.getString("Message.Command.CaptureHelper.Mode", owner), Translation.getString("Name.Disabled", owner)));
                            }
                            justLeashed.remove(player.getUniqueId());
                        }
                    });
                }
            }
        }
    }
}
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) MyPetSaveEvent(de.Keyle.MyPet.api.event.MyPetSaveEvent) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) Settings(de.Keyle.MyPet.api.util.configuration.settings.Settings) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) MetadataValue(org.bukkit.metadata.MetadataValue) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) LeashEntityHook(de.Keyle.MyPet.api.util.hooks.types.LeashEntityHook) WorldGroup(de.Keyle.MyPet.api.WorldGroup) MyPetCreateEvent(de.Keyle.MyPet.api.event.MyPetCreateEvent) LeashFlag(de.Keyle.MyPet.api.entity.leashing.LeashFlag) ItemStack(org.bukkit.inventory.ItemStack) LeashHook(de.Keyle.MyPet.api.util.hooks.types.LeashHook) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) EventHandler(org.bukkit.event.EventHandler)

Example 2 with EntityConverterService

use of de.Keyle.MyPet.api.util.service.types.EntityConverterService in project MyPet by xXKeyleXx.

the class CommandRelease method onCommand.

public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
    if (sender instanceof Player) {
        Player petOwner = (Player) sender;
        if (WorldGroup.getGroupByWorld(petOwner.getWorld()).isDisabled()) {
            petOwner.sendMessage(Translation.getString("Message.No.AllowedHere", petOwner));
            return true;
        }
        if (MyPetApi.getMyPetManager().hasActiveMyPet(petOwner)) {
            MyPet myPet = MyPetApi.getMyPetManager().getMyPet(petOwner);
            if (!Permissions.has(petOwner, "MyPet.command.release")) {
                return true;
            }
            if (myPet.getStatus() == PetState.Despawned) {
                sender.sendMessage(Util.formatText(Translation.getString("Message.Call.First", petOwner), myPet.getPetName()));
                return true;
            } else if (myPet.getStatus() == PetState.Dead) {
                sender.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Respawn.In", petOwner), myPet.getPetName(), myPet.getRespawnTime()));
                return true;
            }
            String name = "";
            if (args.length > 0) {
                for (String arg : args) {
                    if (!name.equals("")) {
                        name += " ";
                    }
                    name += arg;
                }
            }
            if (ChatColor.stripColor(myPet.getPetName()).trim().equalsIgnoreCase(name.trim())) {
                MyPetRemoveEvent removeEvent = new MyPetRemoveEvent(myPet, MyPetRemoveEvent.Source.Release);
                Bukkit.getServer().getPluginManager().callEvent(removeEvent);
                if (!MyPetApi.getMyPetInfo().getRemoveAfterRelease(myPet.getPetType())) {
                    LivingEntity normalEntity = (LivingEntity) myPet.getLocation().get().getWorld().spawnEntity(myPet.getLocation().get(), EntityType.valueOf(myPet.getPetType().getBukkitName()));
                    Optional<EntityConverterService> converter = MyPetApi.getServiceManager().getService(EntityConverterService.class);
                    try {
                        converter.ifPresent(entityConverterService -> entityConverterService.convertEntity(myPet, normalEntity));
                    } catch (Exception e) {
                        normalEntity.remove();
                        return true;
                    }
                }
                if (myPet.getSkills().isActive(Backpack.class)) {
                    myPet.getSkills().get(Backpack.class).getInventory().dropContentAt(myPet.getLocation().get());
                }
                if (myPet instanceof MyPetEquipment) {
                    ((MyPetEquipment) myPet).dropEquipment();
                }
                myPet.removePet();
                myPet.getOwner().setMyPetForWorldGroup(WorldGroup.getGroupByWorld(petOwner.getWorld().getName()), null);
                sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Release.Success", petOwner), myPet.getPetName()));
                MyPetApi.getMyPetManager().deactivateMyPet(myPet.getOwner(), false);
                MyPetApi.getRepository().removeMyPet(myPet.getUUID(), null);
                return true;
            } else {
                FancyMessage message = new FancyMessage(Translation.getString("Message.Command.Release.Confirm", petOwner) + " ");
                List<String> lore = new ArrayList<>();
                lore.add(RESET + Translation.getString("Name.Hunger", petOwner) + ": " + GOLD + Math.round(myPet.getSaturation()));
                if (myPet.getRespawnTime() > 0) {
                    lore.add(RESET + Translation.getString("Name.Respawntime", petOwner) + ": " + GOLD + myPet.getRespawnTime() + "sec");
                } else {
                    lore.add(RESET + Translation.getString("Name.HP", petOwner) + ": " + GOLD + String.format("%1.2f", myPet.getHealth()));
                }
                lore.add(RESET + Translation.getString("Name.Exp", petOwner) + ": " + GOLD + String.format("%1.2f", myPet.getExp()));
                lore.add(RESET + Translation.getString("Name.Type", petOwner) + ": " + GOLD + Translation.getString("Name." + myPet.getPetType().name(), petOwner));
                lore.add(RESET + Translation.getString("Name.Skilltree", petOwner) + ": " + GOLD + (myPet.getSkilltree() != null ? Colorizer.setColors(myPet.getSkilltree().getDisplayName()) : "-"));
                message.then(myPet.getPetName()).color(ChatColor.AQUA).command("/petrelease " + ChatColor.stripColor(myPet.getPetName())).itemTooltip(new ItemTooltip().addLore(lore).setTitle(myPet.getPetName()));
                MyPetApi.getPlatformHelper().sendMessageRaw((Player) sender, message.toJSONString());
                return true;
            }
        } else {
            sender.sendMessage(Translation.getString("Message.No.HasPet", petOwner));
        }
        return true;
    }
    sender.sendMessage("You can't use this command from server console!");
    return false;
}
Also used : EntityConverterService(de.Keyle.MyPet.api.util.service.types.EntityConverterService) Player(org.bukkit.entity.Player) MyPet(de.Keyle.MyPet.api.entity.MyPet) ArrayList(java.util.ArrayList) FancyMessage(de.Keyle.MyPet.api.util.chat.FancyMessage) LivingEntity(org.bukkit.entity.LivingEntity) MyPetRemoveEvent(de.Keyle.MyPet.api.event.MyPetRemoveEvent) ItemTooltip(de.Keyle.MyPet.api.util.chat.parts.ItemTooltip) MyPetEquipment(de.Keyle.MyPet.api.entity.MyPetEquipment)

Aggregations

EntityConverterService (de.Keyle.MyPet.api.util.service.types.EntityConverterService)2 WorldGroup (de.Keyle.MyPet.api.WorldGroup)1 MyPet (de.Keyle.MyPet.api.entity.MyPet)1 MyPetBukkitEntity (de.Keyle.MyPet.api.entity.MyPetBukkitEntity)1 MyPetEquipment (de.Keyle.MyPet.api.entity.MyPetEquipment)1 MyPetType (de.Keyle.MyPet.api.entity.MyPetType)1 LeashFlag (de.Keyle.MyPet.api.entity.leashing.LeashFlag)1 MyPetCreateEvent (de.Keyle.MyPet.api.event.MyPetCreateEvent)1 MyPetRemoveEvent (de.Keyle.MyPet.api.event.MyPetRemoveEvent)1 MyPetSaveEvent (de.Keyle.MyPet.api.event.MyPetSaveEvent)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 ConfigItem (de.Keyle.MyPet.api.util.ConfigItem)1 FancyMessage (de.Keyle.MyPet.api.util.chat.FancyMessage)1 ItemTooltip (de.Keyle.MyPet.api.util.chat.parts.ItemTooltip)1 Settings (de.Keyle.MyPet.api.util.configuration.settings.Settings)1 LeashEntityHook (de.Keyle.MyPet.api.util.hooks.types.LeashEntityHook)1 LeashHook (de.Keyle.MyPet.api.util.hooks.types.LeashHook)1 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)1 ArrayList (java.util.ArrayList)1 LivingEntity (org.bukkit.entity.LivingEntity)1