Search in sources :

Example 1 with MyPetTypeNotFoundException

use of de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException in project MyPet by xXKeyleXx.

the class CommandOptionCreate method onCommandOption.

@Override
public boolean onCommandOption(final CommandSender sender, String[] args) {
    if (args.length < 2) {
        return false;
    }
    String lang = MyPetApi.getPlatformHelper().getCommandSenderLanguage(sender);
    int forceOffset = 0;
    if (args[0].equalsIgnoreCase("-f")) {
        forceOffset = 1;
        if (args.length < 3) {
            return false;
        }
    }
    try {
        MyPetType myPetType = MyPetType.byName(args[1 + forceOffset]);
        if (MyPetApi.getMyPetInfo().isLeashableEntityType(EntityType.valueOf(myPetType.getBukkitName()))) {
            Player owner = Bukkit.getPlayer(args[forceOffset]);
            if (owner == null || !owner.isOnline()) {
                sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
                return true;
            }
            final MyPetPlayer newOwner;
            if (MyPetApi.getPlayerManager().isMyPetPlayer(owner)) {
                newOwner = MyPetApi.getPlayerManager().getMyPetPlayer(owner);
                if (newOwner.hasMyPet() && forceOffset == 1) {
                    MyPetApi.getMyPetManager().deactivateMyPet(newOwner, true);
                }
            } else {
                newOwner = MyPetApi.getPlayerManager().registerMyPetPlayer(owner);
            }
            final InactiveMyPet inactiveMyPet = new InactiveMyPet(newOwner);
            inactiveMyPet.setPetType(myPetType);
            inactiveMyPet.setPetName(Translation.getString("Name." + inactiveMyPet.getPetType().name(), inactiveMyPet.getOwner().getLanguage()));
            TagCompound compound = inactiveMyPet.getInfo();
            createInfo(myPetType, Arrays.copyOfRange(args, 2 + forceOffset, args.length), compound);
            final WorldGroup wg = WorldGroup.getGroupByWorld(owner.getWorld().getName());
            inactiveMyPet.setWorldGroup(wg.getName());
            Optional<RepositoryMyPetConverterService> converter = MyPetApi.getServiceManager().getService(RepositoryMyPetConverterService.class);
            if (converter.isPresent()) {
                converter.get().convert(inactiveMyPet);
            }
            MyPetCreateEvent createEvent = new MyPetCreateEvent(inactiveMyPet, MyPetCreateEvent.Source.AdminCommand);
            Bukkit.getServer().getPluginManager().callEvent(createEvent);
            MyPetSaveEvent saveEvent = new MyPetSaveEvent(inactiveMyPet);
            Bukkit.getServer().getPluginManager().callEvent(saveEvent);
            MyPetApi.getRepository().addMyPet(inactiveMyPet, new RepositoryCallback<Boolean>() {

                @Override
                public void callback(Boolean added) {
                    if (added) {
                        if (!newOwner.hasMyPet()) {
                            inactiveMyPet.getOwner().setMyPetForWorldGroup(wg, inactiveMyPet.getUUID());
                            MyPetApi.getRepository().updateMyPetPlayer(inactiveMyPet.getOwner(), null);
                            Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(inactiveMyPet);
                            if (myPet.isPresent()) {
                                myPet.get().createEntity();
                                sender.sendMessage(Translation.getString("Message.Command.Success", sender));
                            } else {
                                sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] Can't create MyPet for " + newOwner.getName() + ". Is this player online?");
                            }
                        } else {
                            sender.sendMessage(Translation.getString("Message.Command.Success", sender));
                        }
                    }
                }
            });
        }
    } catch (MyPetTypeNotFoundException e) {
        sender.sendMessage(Translation.getString("Message.Command.PetType.Unknown", lang));
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) Optional(com.google.common.base.Optional) WorldGroup(de.Keyle.MyPet.api.WorldGroup) TagCompound(de.keyle.knbt.TagCompound) MyPetSaveEvent(de.Keyle.MyPet.api.event.MyPetSaveEvent) MyPetCreateEvent(de.Keyle.MyPet.api.event.MyPetCreateEvent) RepositoryMyPetConverterService(de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) MyPetTypeNotFoundException(de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException)

Example 2 with MyPetTypeNotFoundException

use of de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException in project MyPet by xXKeyleXx.

the class CommandPetType method onCommand.

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
    if (args.length < 1) {
        return false;
    }
    String lang = "en";
    if (commandSender instanceof Player) {
        lang = MyPetApi.getPlatformHelper().getPlayerLanguage((Player) commandSender);
    }
    try {
        MyPetType myPetType = MyPetType.byName(args[0]);
        String leashFlagString = "";
        for (LeashFlag leashFlag : MyPetApi.getMyPetInfo().getLeashFlags(myPetType)) {
            leashFlagString += leashFlag.name() + ", ";
        }
        leashFlagString = leashFlagString.substring(0, leashFlagString.lastIndexOf(","));
        commandSender.sendMessage(Translation.getString("Name.LeashFlag", lang) + ": " + leashFlagString);
        String foodString = "";
        for (ConfigItem material : MyPetApi.getMyPetInfo().getFood(myPetType)) {
            foodString += WordUtils.capitalizeFully(MyPetApi.getPlatformHelper().getMaterialName(material.getItem().getTypeId()).replace("_", " ")) + ", ";
        }
        foodString = foodString.substring(0, foodString.lastIndexOf(","));
        commandSender.sendMessage(Translation.getString("Name.Food", lang) + ": " + foodString);
        commandSender.sendMessage(Translation.getString("Name.HP", lang) + ": " + MyPetApi.getMyPetInfo().getStartHP(myPetType));
    } catch (MyPetTypeNotFoundException e) {
        commandSender.sendMessage(Translation.getString("Message.Command.PetType.Unknown", lang));
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) LeashFlag(de.Keyle.MyPet.api.entity.LeashFlag) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) ConfigItem(de.Keyle.MyPet.api.util.ConfigItem) MyPetTypeNotFoundException(de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException)

Aggregations

MyPetType (de.Keyle.MyPet.api.entity.MyPetType)2 MyPetTypeNotFoundException (de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException)2 Player (org.bukkit.entity.Player)2 Optional (com.google.common.base.Optional)1 WorldGroup (de.Keyle.MyPet.api.WorldGroup)1 LeashFlag (de.Keyle.MyPet.api.entity.LeashFlag)1 MyPetCreateEvent (de.Keyle.MyPet.api.event.MyPetCreateEvent)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 RepositoryMyPetConverterService (de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService)1 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)1 TagCompound (de.keyle.knbt.TagCompound)1