Search in sources :

Example 1 with MyPetEquipment

use of de.Keyle.MyPet.api.entity.MyPetEquipment 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

MyPet (de.Keyle.MyPet.api.entity.MyPet)1 MyPetEquipment (de.Keyle.MyPet.api.entity.MyPetEquipment)1 MyPetRemoveEvent (de.Keyle.MyPet.api.event.MyPetRemoveEvent)1 FancyMessage (de.Keyle.MyPet.api.util.chat.FancyMessage)1 ItemTooltip (de.Keyle.MyPet.api.util.chat.parts.ItemTooltip)1 EntityConverterService (de.Keyle.MyPet.api.util.service.types.EntityConverterService)1 ArrayList (java.util.ArrayList)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Player (org.bukkit.entity.Player)1