Search in sources :

Example 11 with MyPetPlayer

use of de.Keyle.MyPet.api.player.MyPetPlayer in project MyPet by xXKeyleXx.

the class CommandSwitch method onCommand.

public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
    if (!(sender instanceof Player)) {
        sender.sendMessage("You can't use this command from server console!");
        return true;
    }
    Player player = (Player) sender;
    if (!Permissions.hasLegacy(player, "MyPet.command.switch")) {
        player.sendMessage(Translation.getString("Message.No.Allowed", player));
        return true;
    }
    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
        final MyPetPlayer owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
        MyPetApi.getRepository().getMyPets(owner, new RepositoryCallback<List<StoredMyPet>>() {

            @Override
            public void callback(List<StoredMyPet> pets) {
                if (pets.size() - (owner.hasMyPet() ? 1 : 0) == 0) {
                    owner.sendMessage(Translation.getString("Message.Command.Switch.NoStoredPets", owner));
                    return;
                }
                if (owner.isOnline()) {
                    String worldGroup = WorldGroup.getGroupByWorld(owner.getPlayer().getWorld().getName()).getName();
                    int inactivePetCount = getInactivePetCount(pets, worldGroup);
                    int maxPetCount = getMaxPetCount(owner.getPlayer());
                    String title;
                    if (owner.hasMyPet()) {
                        inactivePetCount--;
                        title = Translation.getString("Message.Npc.SwitchTitle", owner);
                    } else {
                        title = Translation.getString("Message.SelectMyPet", owner);
                    }
                    String stats = "(" + inactivePetCount + "/" + maxPetCount + ")";
                    final MyPetSelectionGui gui = new MyPetSelectionGui(owner, title + " " + stats);
                    gui.open(pets, new RepositoryCallback<StoredMyPet>() {

                        @Override
                        public void callback(StoredMyPet storedMyPet) {
                            Optional<MyPet> activePet = MyPetApi.getMyPetManager().activateMyPet(storedMyPet);
                            if (activePet.isPresent() && owner.isOnline()) {
                                Player player = owner.getPlayer();
                                activePet.get().getOwner().sendMessage(Util.formatText(Translation.getString("Message.Npc.ChosenPet", owner), activePet.get().getPetName()));
                                WorldGroup wg = WorldGroup.getGroupByWorld(player.getWorld().getName());
                                owner.setMyPetForWorldGroup(wg, activePet.get().getUUID());
                                switch(activePet.get().createEntity()) {
                                    case Canceled:
                                        owner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", owner), activePet.get().getPetName()));
                                        break;
                                    case NoSpace:
                                        owner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", owner), activePet.get().getPetName()));
                                        break;
                                    case NotAllowed:
                                        owner.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", owner), activePet.get().getPetName()));
                                        break;
                                    case Dead:
                                        owner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Respawn.In", owner), activePet.get().getPetName(), activePet.get().getRespawnTime()));
                                        break;
                                    case Spectator:
                                        sender.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Spectator", owner), activePet.get().getPetName()));
                                        break;
                                }
                            }
                        }
                    });
                }
            }
        });
    } else {
        sender.sendMessage(Translation.getString("Message.No.HasPet", player));
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) WorldGroup(de.Keyle.MyPet.api.WorldGroup) MyPetSelectionGui(de.Keyle.MyPet.gui.selectionmenu.MyPetSelectionGui) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) ArrayList(java.util.ArrayList) List(java.util.List) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 12 with MyPetPlayer

use of de.Keyle.MyPet.api.player.MyPetPlayer in project MyPet by xXKeyleXx.

the class CommandOptionClone method onCommandOption.

@Override
public boolean onCommandOption(CommandSender sender, String[] args) {
    if (args.length < 2) {
        return false;
    }
    String lang = MyPetApi.getPlatformHelper().getCommandSenderLanguage(sender);
    Player oldOwner = Bukkit.getPlayer(args[0]);
    if (oldOwner == null || !oldOwner.isOnline()) {
        sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
        return true;
    }
    final Player newOwner = Bukkit.getPlayer(args[1]);
    if (newOwner == null || !newOwner.isOnline()) {
        sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
        return true;
    }
    if (!MyPetApi.getPlayerManager().isMyPetPlayer(oldOwner)) {
        sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Util.formatText(Translation.getString("Message.No.UserHavePet", lang), oldOwner.getName()));
        return true;
    }
    MyPetPlayer oldPetOwner = MyPetApi.getPlayerManager().getMyPetPlayer(oldOwner);
    if (!oldPetOwner.hasMyPet()) {
        sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Util.formatText(Translation.getString("Message.No.UserHavePet", lang), oldOwner.getName()));
        return true;
    }
    final MyPetPlayer newPetOwner;
    if (MyPetApi.getPlayerManager().isMyPetPlayer(newOwner)) {
        newPetOwner = MyPetApi.getPlayerManager().getMyPetPlayer(newOwner);
    } else {
        newPetOwner = MyPetApi.getPlayerManager().registerMyPetPlayer(newOwner);
    }
    if (newPetOwner.hasMyPet()) {
        sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + newOwner.getName() + " has already an active MyPet!");
        return true;
    }
    MyPet oldPet = oldPetOwner.getMyPet();
    final InactiveMyPet newPet = new InactiveMyPet(newPetOwner);
    newPet.setPetName(oldPet.getPetName());
    newPet.setExp(oldPet.getExperience().getExp());
    newPet.setHealth(oldPet.getHealth());
    newPet.setHungerValue(oldPet.getHungerValue());
    newPet.setRespawnTime(oldPet.getRespawnTime());
    newPet.setInfo(oldPet.getInfo());
    newPet.setPetType(oldPet.getPetType());
    newPet.setSkilltree(oldPet.getSkilltree());
    newPet.setWorldGroup(oldPet.getWorldGroup());
    newPet.setSkills(oldPet.getSkillInfo());
    MyPetSaveEvent event = new MyPetSaveEvent(newPet);
    Bukkit.getServer().getPluginManager().callEvent(event);
    MyPetApi.getRepository().addMyPet(newPet, new RepositoryCallback<Boolean>() {

        @Override
        public void callback(Boolean value) {
            Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(newPet);
            if (myPet.isPresent()) {
                WorldGroup worldGroup = WorldGroup.getGroupByWorld(newPet.getOwner().getPlayer().getWorld().getName());
                newPet.setWorldGroup(worldGroup.getName());
                newPet.getOwner().setMyPetForWorldGroup(worldGroup, newPet.getUUID());
                MyPetApi.getRepository().updateMyPetPlayer(newPetOwner, null);
                newOwner.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] MyPet owned by " + newPetOwner.getName() + " successfully cloned!");
            }
        }
    });
    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) MyPet(de.Keyle.MyPet.api.entity.MyPet) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) WorldGroup(de.Keyle.MyPet.api.WorldGroup) MyPetSaveEvent(de.Keyle.MyPet.api.event.MyPetSaveEvent)

Example 13 with MyPetPlayer

use of de.Keyle.MyPet.api.player.MyPetPlayer 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 14 with MyPetPlayer

use of de.Keyle.MyPet.api.player.MyPetPlayer in project MyPet by xXKeyleXx.

the class Timer method startTimer.

public static void startTimer() {
    stopTimer();
    timerIDs.add(Bukkit.getScheduler().scheduleSyncRepeatingTask(MyPetApi.getPlugin(), new Runnable() {

        public void run() {
            for (MyPet myPet : MyPetApi.getMyPetManager().getAllActiveMyPets()) {
                myPet.schedule();
            }
        }
    }, 0L, 20L));
    timerIDs.add(Bukkit.getScheduler().scheduleSyncRepeatingTask(MyPetApi.getPlugin(), new Runnable() {

        public void run() {
            for (Scheduler task : tasksToSchedule) {
                task.schedule();
            }
        }
    }, 5L, 20L));
    timerIDs.add(Bukkit.getScheduler().scheduleSyncRepeatingTask(MyPetApi.getPlugin(), new Runnable() {

        public void run() {
            for (MyPetPlayer player : MyPetApi.getPlayerManager().getMyPetPlayers()) {
                player.schedule();
            }
        }
    }, 10L, 20L));
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPet(de.Keyle.MyPet.api.entity.MyPet)

Example 15 with MyPetPlayer

use of de.Keyle.MyPet.api.player.MyPetPlayer in project MyPet by xXKeyleXx.

the class PlayerManager method registerMyPetPlayer.

public MyPetPlayer registerMyPetPlayer(Player player) {
    MyPetPlayer myPetPlayer = createMyPetPlayer(player);
    MyPetApi.getRepository().addMyPetPlayer(myPetPlayer, null);
    setOnline(myPetPlayer);
    return myPetPlayer;
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer)

Aggregations

MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)46 EventHandler (org.bukkit.event.EventHandler)19 MyPet (de.Keyle.MyPet.api.entity.MyPet)17 Player (org.bukkit.entity.Player)16 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)14 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)8 WorldGroup (de.Keyle.MyPet.api.WorldGroup)7 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)6 TagCompound (de.keyle.knbt.TagCompound)5 List (java.util.List)5 UUID (java.util.UUID)4 Optional (com.google.common.base.Optional)3 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)3 SkillTreeMobType (de.Keyle.MyPet.api.skill.skilltree.SkillTreeMobType)3 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)3 MyPetPlayerImpl (de.Keyle.MyPet.util.player.MyPetPlayerImpl)3 FancyMessage (de.keyle.fanciful.FancyMessage)3 IOException (java.io.IOException)3 MyPetSaveEvent (de.Keyle.MyPet.api.event.MyPetSaveEvent)2 ConfigItem (de.Keyle.MyPet.api.util.ConfigItem)2