Search in sources :

Example 6 with WorldGroup

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

use of de.Keyle.MyPet.api.WorldGroup 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 8 with WorldGroup

use of de.Keyle.MyPet.api.WorldGroup in project MyPet by xXKeyleXx.

the class MyPetSelectionGui method open.

public void open(final List<StoredMyPet> pets, int page, final RepositoryCallback<StoredMyPet> callback) {
    if (pets.size() > 0) {
        if (page < 1 || Math.ceil(pets.size() / 45.) < page) {
            page = 1;
        }
        final Map<Integer, StoredMyPet> petSlotList = new HashMap<>();
        WorldGroup wg = WorldGroup.getGroupByWorld(player.getPlayer().getWorld().getName());
        Iterator<StoredMyPet> iterator = pets.iterator();
        while (iterator.hasNext()) {
            StoredMyPet mypet = iterator.next();
            if (mypet.getWorldGroup().equals("") || !mypet.getWorldGroup().equals(wg.getName())) {
                iterator.remove();
            }
            if (player.hasMyPet() && player.getMyPet().getUUID().equals(mypet.getUUID())) {
                iterator.remove();
            }
        }
        final int previousPage = page == 1 ? (int) Math.ceil(pets.size() / 45.) : page - 1;
        final int nextPage = page == Math.ceil(pets.size() / 45.) ? 1 : page + 1;
        IconMenu menu = new IconMenu(title, new IconMenu.OptionClickEventHandler() {

            @Override
            public void onOptionClick(IconMenu.OptionClickEvent event) {
                if (event.getPosition() == 45) {
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            open(pets, previousPage, callback);
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                } else if (event.getPosition() == 53) {
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            open(pets, nextPage, callback);
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                } else if (event.getPosition() > 45) {
                    return;
                } else if (petSlotList.containsKey(event.getPosition())) {
                    StoredMyPet storedMyPet = petSlotList.get(event.getPosition());
                    if (storedMyPet != null && callback != null) {
                        callback.callback(storedMyPet);
                    }
                }
                event.setWillClose(true);
                event.setWillDestroy(true);
            }
        }, MyPetApi.getPlugin());
        int pagePets = pets.size() - (page - 1) * 45;
        for (int i = 0; i < pagePets && i <= 45; i++) {
            StoredMyPet mypet = pets.get(i + ((page - 1) * 45));
            List<String> lore = new ArrayList<>();
            lore.add(RESET + Translation.getString("Name.Hunger", player) + ": " + GOLD + Math.round(mypet.getSaturation()));
            if (mypet.getRespawnTime() > 0) {
                lore.add(RESET + Translation.getString("Name.Respawntime", player) + ": " + GOLD + mypet.getRespawnTime() + "sec");
            } else {
                lore.add(RESET + Translation.getString("Name.HP", player) + ": " + GOLD + String.format("%1.2f", mypet.getHealth()));
            }
            lore.add(RESET + Translation.getString("Name.Exp", player) + ": " + GOLD + String.format("%1.2f", mypet.getExp()));
            if (mypet.getInfo().containsKey("storage")) {
                TagCompound storage = mypet.getInfo().getAs("storage", TagCompound.class);
                if (storage.containsKey("level")) {
                    lore.add(RESET + Translation.getString("Name.Level", player) + ": " + GOLD + storage.getAs("level", TagInt.class).getIntData());
                }
            }
            lore.add(RESET + Translation.getString("Name.Type", player) + ": " + GOLD + Translation.getString("Name." + mypet.getPetType().name(), player));
            lore.add(RESET + Translation.getString("Name.Skilltree", player) + ": " + GOLD + (mypet.getSkilltree() != null ? mypet.getSkilltree().getDisplayName() : "-"));
            IconMenuItem icon = new IconMenuItem();
            icon.setTitle(RESET + mypet.getPetName());
            icon.addLore(lore);
            Optional<EggIconService> egg = MyPetApi.getServiceManager().getService(EggIconService.class);
            if (egg.isPresent()) {
                egg.get().updateIcon(mypet.getPetType(), icon);
            }
            int pos = menu.addOption(icon);
            petSlotList.put(pos, mypet);
        }
        if (previousPage != page) {
            menu.setOption(45, new IconMenuItem().setMaterial(Material.SIGN).setTitle("" + previousPage + " ≪≪"));
        }
        if (previousPage != page) {
            menu.setOption(53, new IconMenuItem().setMaterial(Material.SIGN).setTitle(ChatColor.BOLD + "≫≫ " + ChatColor.RESET + nextPage));
        }
        menu.open(player.getPlayer());
    }
}
Also used : IconMenu(de.Keyle.MyPet.api.gui.IconMenu) WorldGroup(de.Keyle.MyPet.api.WorldGroup) TagInt(de.keyle.knbt.TagInt) EggIconService(de.Keyle.MyPet.api.util.service.types.EggIconService) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) TagCompound(de.keyle.knbt.TagCompound) IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 9 with WorldGroup

use of de.Keyle.MyPet.api.WorldGroup in project MyPet by xXKeyleXx.

the class PlayerListener method onMyPetPlayerChangeWorld.

@EventHandler
public void onMyPetPlayerChangeWorld(final PlayerChangedWorldEvent event) {
    if (!event.getPlayer().isOnline()) {
        return;
    }
    if (MyPetApi.getPlayerManager().isMyPetPlayer(event.getPlayer())) {
        final MyPetPlayer myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer(event.getPlayer());
        final WorldGroup fromGroup = WorldGroup.getGroupByWorld(event.getFrom().getName());
        final WorldGroup toGroup = WorldGroup.getGroupByWorld(event.getPlayer().getWorld().getName());
        final MyPet myPet = myPetPlayer.hasMyPet() ? myPetPlayer.getMyPet() : null;
        final BukkitRunnable callPet = new BukkitRunnable() {

            public void run() {
                if (myPetPlayer.isOnline() && myPetPlayer.hasMyPet()) {
                    MyPet runMyPet = myPetPlayer.getMyPet();
                    switch(runMyPet.createEntity()) {
                        case Canceled:
                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", myPetPlayer), runMyPet.getPetName()));
                            break;
                        case NoSpace:
                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", myPetPlayer), runMyPet.getPetName()));
                            break;
                        case NotAllowed:
                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", myPetPlayer), runMyPet.getPetName()));
                            break;
                        case Dead:
                            if (runMyPet != myPet) {
                                myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", myPetPlayer), runMyPet.getPetName(), runMyPet.getRespawnTime()));
                            }
                            break;
                        case Flying:
                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Flying", myPetPlayer), runMyPet.getPetName()));
                            break;
                        case Success:
                            if (runMyPet != myPet) {
                                myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Command.Call.Success", myPetPlayer), runMyPet.getPetName()));
                            }
                            break;
                    }
                }
            }
        };
        if (fromGroup != toGroup) {
            final boolean hadMyPetInFromWorld = MyPetApi.getMyPetManager().deactivateMyPet(myPetPlayer, true);
            if (myPetPlayer.hasMyPetInWorldGroup(toGroup)) {
                final UUID groupMyPetUUID = myPetPlayer.getMyPetForWorldGroup(toGroup);
                MyPetApi.getRepository().getMyPets(myPetPlayer, new RepositoryCallback<List<StoredMyPet>>() {

                    @Override
                    public void callback(List<StoredMyPet> pets) {
                        for (StoredMyPet myPet : pets) {
                            if (myPet.getUUID().equals(groupMyPetUUID)) {
                                MyPetApi.getMyPetManager().activateMyPet(myPet);
                                break;
                            }
                        }
                        if (myPetPlayer.hasMyPet()) {
                            if (myPetPlayer.getMyPet().wantsToRespawn()) {
                                callPet.runTaskLater(MyPetApi.getPlugin(), 20L);
                            }
                        } else {
                            myPetPlayer.setMyPetForWorldGroup(toGroup, null);
                        }
                    }
                });
            } else if (hadMyPetInFromWorld) {
                myPetPlayer.getPlayer().sendMessage(Translation.getString("Message.MultiWorld.NoActivePetInThisWorld", myPetPlayer));
            }
        } else if (myPet != null) {
            if (myPet.wantsToRespawn()) {
                callPet.runTaskLater(MyPetApi.getPlugin(), 20L);
            }
        }
    }
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) WorldGroup(de.Keyle.MyPet.api.WorldGroup) MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) List(java.util.List) UUID(java.util.UUID) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) EventHandler(org.bukkit.event.EventHandler)

Aggregations

WorldGroup (de.Keyle.MyPet.api.WorldGroup)9 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)7 MyPet (de.Keyle.MyPet.api.entity.MyPet)5 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)5 Player (org.bukkit.entity.Player)4 EventHandler (org.bukkit.event.EventHandler)4 Optional (com.google.common.base.Optional)3 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)3 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)3 List (java.util.List)3 UUID (java.util.UUID)3 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)3 MyPetSaveEvent (de.Keyle.MyPet.api.event.MyPetSaveEvent)2 TagCompound (de.keyle.knbt.TagCompound)2 MyPetType (de.Keyle.MyPet.api.entity.MyPetType)1 MyPetCreateEvent (de.Keyle.MyPet.api.event.MyPetCreateEvent)1 MyPetPlayerJoinEvent (de.Keyle.MyPet.api.event.MyPetPlayerJoinEvent)1 MyPetTypeNotFoundException (de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException)1 IconMenu (de.Keyle.MyPet.api.gui.IconMenu)1 IconMenuItem (de.Keyle.MyPet.api.gui.IconMenuItem)1