Search in sources :

Example 1 with MyPetRemoveEvent

use of de.Keyle.MyPet.api.event.MyPetRemoveEvent 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 (MyPetApi.getMyPetManager().hasActiveMyPet(petOwner)) {
            MyPet myPet = MyPetApi.getMyPetManager().getMyPet(petOwner);
            if (!Permissions.hasLegacy(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 (myPet.getSkills().isSkillActive(Inventory.class)) {
                    myPet.getSkills().getSkill(Inventory.class).get().getInventory().dropContentAt(myPet.getLocation().get());
                }
                if (myPet instanceof MyPetEquipment) {
                    ((MyPetEquipment) myPet).dropEquipment();
                }
                if (!Configuration.Misc.REMOVE_PETS_AFTER_RELEASE) {
                    LivingEntity normalEntity = (LivingEntity) myPet.getLocation().get().getWorld().spawnEntity(myPet.getLocation().get(), EntityType.valueOf(myPet.getPetType().getBukkitName()));
                    Optional<EntityConverterService> converter = MyPetApi.getServiceManager().getService(EntityConverterService.class);
                    if (converter.isPresent()) {
                        converter.get().convertEntity(myPet, normalEntity);
                    }
                }
                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 ? myPet.getSkilltree().getDisplayName() : "-"));
                message.then(myPet.getPetName()).color(ChatColor.AQUA).command("/petrelease " + ChatColor.stripColor(myPet.getPetName())).itemTooltip(new ItemTooltip().setMaterial(Material.MONSTER_EGG).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.fanciful.FancyMessage) LivingEntity(org.bukkit.entity.LivingEntity) MyPetRemoveEvent(de.Keyle.MyPet.api.event.MyPetRemoveEvent) ItemTooltip(de.keyle.fanciful.ItemTooltip) MyPetEquipment(de.Keyle.MyPet.api.entity.MyPetEquipment) Inventory(de.Keyle.MyPet.skill.skills.Inventory)

Example 2 with MyPetRemoveEvent

use of de.Keyle.MyPet.api.event.MyPetRemoveEvent in project MyPet by xXKeyleXx.

the class CommandOptionRemove method onCommandOption.

@Override
public boolean onCommandOption(CommandSender sender, String[] args) {
    String lang = MyPetApi.getPlatformHelper().getCommandSenderLanguage(sender);
    if (args.length >= 1) {
        Player player = Bukkit.getPlayer(args[0]);
        if (player == null || !player.isOnline()) {
            sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
            return true;
        }
        if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
            MyPetPlayer petOwner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
            if (petOwner.hasMyPet()) {
                MyPet myPet = petOwner.getMyPet();
                MyPetRemoveEvent removeEvent = new MyPetRemoveEvent(myPet, MyPetRemoveEvent.Source.AdminCommand);
                Bukkit.getServer().getPluginManager().callEvent(removeEvent);
                myPet.getOwner().setMyPetForWorldGroup(WorldGroup.getGroupByWorld(player.getWorld().getName()), null);
                MyPetApi.getMyPetManager().deactivateMyPet(myPet.getOwner(), false);
                MyPetApi.getRepository().removeMyPet(myPet.getUUID(), null);
                sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] You removed the MyPet of: " + ChatColor.YELLOW + petOwner.getName());
            }
        }
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetRemoveEvent(de.Keyle.MyPet.api.event.MyPetRemoveEvent) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPet(de.Keyle.MyPet.api.entity.MyPet)

Aggregations

MyPet (de.Keyle.MyPet.api.entity.MyPet)2 MyPetRemoveEvent (de.Keyle.MyPet.api.event.MyPetRemoveEvent)2 Player (org.bukkit.entity.Player)2 MyPetEquipment (de.Keyle.MyPet.api.entity.MyPetEquipment)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 EntityConverterService (de.Keyle.MyPet.api.util.service.types.EntityConverterService)1 Inventory (de.Keyle.MyPet.skill.skills.Inventory)1 FancyMessage (de.keyle.fanciful.FancyMessage)1 ItemTooltip (de.keyle.fanciful.ItemTooltip)1 ArrayList (java.util.ArrayList)1 LivingEntity (org.bukkit.entity.LivingEntity)1