Search in sources :

Example 11 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class NbtRepository method getMyPets.

@Override
public void getMyPets(final MyPetPlayer owner, final RepositoryCallback<List<StoredMyPet>> callback) {
    if (callback != null) {
        List<StoredMyPet> petList = new ArrayList<>();
        for (UUID petUUID : petPlayerMultiMap.get(owner.getInternalUUID())) {
            if (petTags.containsKey(petUUID)) {
                InactiveMyPet myPet = new InactiveMyPet(owner);
                myPet.load(petTags.get(petUUID));
                List<RepositoryMyPetConverterService> converters = MyPetApi.getServiceManager().getServices(RepositoryMyPetConverterService.class);
                for (RepositoryMyPetConverterService converter : converters) {
                    converter.convert(myPet);
                }
                petList.add(myPet);
            }
        }
        callback.run(petList);
    }
}
Also used : RepositoryMyPetConverterService(de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 12 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class StorageTrait method onRightClick.

@EventHandler
public void onRightClick(final NPCRightClickEvent npcEvent) {
    if (this.npc != npcEvent.getNPC()) {
        return;
    }
    final Player player = npcEvent.getClicker();
    if (!Permissions.has(player, "MyPet.npc.storage")) {
        player.sendMessage(Translation.getString("Message.No.Allowed", player));
        return;
    }
    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
        final MyPetPlayer myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer(player);
        assert myPetPlayer != null;
        if (myPetPlayer.hasMyPet()) {
            final NPC npc = this.npc;
            MyPetApi.getRepository().getMyPets(myPetPlayer, new RepositoryCallback<List<StoredMyPet>>() {

                @Override
                public void callback(List<StoredMyPet> pets) {
                    WorldGroup wg = WorldGroup.getGroupByWorld(myPetPlayer.getPlayer().getWorld().getName());
                    int inactivePetCount = 0;
                    UUID activePetUUID = myPetPlayer.getMyPet().getUUID();
                    for (StoredMyPet mypet : pets) {
                        if (activePetUUID.equals(mypet.getUUID()) || (!mypet.getWorldGroup().equals("") && !mypet.getWorldGroup().equals(wg.getName()))) {
                            continue;
                        }
                        inactivePetCount++;
                    }
                    int maxPetCount = 0;
                    if (!Permissions.has(player, "MyPet.admin")) {
                        for (int i = Misc.MAX_STORED_PET_COUNT; i > 0; i--) {
                            if (Permissions.hasLegacy(player, "MyPet.petstorage.limit.", i)) {
                                maxPetCount = i;
                                break;
                            }
                        }
                    } else {
                        maxPetCount = Misc.MAX_STORED_PET_COUNT;
                    }
                    if (inactivePetCount == 0 && maxPetCount == 0) {
                        player.sendMessage(Translation.getString("Message.No.Allowed", player));
                        return;
                    }
                    if (inactivePetCount >= maxPetCount) {
                        String stats = "(" + inactivePetCount + "/" + maxPetCount + ")";
                        final MyPetSelectionGui gui = new MyPetSelectionGui(myPetPlayer, stats + " " + Translation.getString("Message.Npc.SwitchTitle", player));
                        gui.open(pets, new RepositoryCallback<StoredMyPet>() {

                            @Override
                            public void callback(StoredMyPet storedMyPet) {
                                MyPetApi.getMyPetManager().deactivateMyPet(myPetPlayer, true);
                                Optional<MyPet> activePet = MyPetApi.getMyPetManager().activateMyPet(storedMyPet);
                                if (activePet.isPresent() && myPetPlayer.isOnline()) {
                                    Player p = myPetPlayer.getPlayer();
                                    myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Npc.ChosenPet", player), activePet.get().getPetName()));
                                    WorldGroup wg = WorldGroup.getGroupByWorld(p.getWorld().getName());
                                    myPetPlayer.setMyPetForWorldGroup(wg, activePet.get().getUUID());
                                    switch(activePet.get().createEntity()) {
                                        case Canceled:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", player), activePet.get().getPetName()));
                                            break;
                                        case NoSpace:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", player), activePet.get().getPetName()));
                                            break;
                                        case NotAllowed:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", player), activePet.get().getPetName()));
                                            break;
                                        case Dead:
                                            if (de.Keyle.MyPet.api.Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                                                myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", myPetPlayer), activePet.get().getPetName()));
                                            } else {
                                                myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead.Respawn", myPetPlayer), activePet.get().getPetName(), activePet.get().getRespawnTime()));
                                            }
                                            break;
                                        case Spectator:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Spectator", myPetPlayer), activePet.get().getPetName()));
                                            break;
                                    }
                                }
                            }
                        });
                    } else {
                        IconMenu menu = new IconMenu(Translation.getString("Message.Npc.HandOverTitle", myPetPlayer), event -> {
                            if (!myPetPlayer.hasMyPet()) {
                                return;
                            }
                            if (event.getPosition() == 3) {
                                boolean store = true;
                                double costs = calculateStorageCosts(myPetPlayer.getMyPet());
                                if (MyPetApi.getHookHelper().isEconomyEnabled() && costs > 0 && npc.hasTrait(WalletTrait.class)) {
                                    WalletTrait walletTrait = npc.getTrait(WalletTrait.class);
                                    if (!MyPetApi.getHookHelper().getEconomy().canPay(myPetPlayer, costs)) {
                                        player.sendMessage(Util.formatText(Translation.getString("Message.No.Money", myPetPlayer), myPetPlayer.getMyPet().getPetName(), npcEvent.getNPC().getName()));
                                        store = false;
                                    }
                                    if (MyPetApi.getHookHelper().getEconomy().pay(myPetPlayer, costs)) {
                                        walletTrait.deposit(costs);
                                    } else {
                                        store = false;
                                    }
                                }
                                if (store) {
                                    StoredMyPet storedMyPet = myPetPlayer.getMyPet();
                                    if (MyPetApi.getMyPetManager().deactivateMyPet(myPetPlayer, true)) {
                                        // remove pet from world groups
                                        String wg1 = myPetPlayer.getWorldGroupForMyPet(storedMyPet.getUUID());
                                        myPetPlayer.setMyPetForWorldGroup(wg1, null);
                                        MyPetApi.getRepository().updateMyPetPlayer(myPetPlayer, null);
                                        player.sendMessage(Util.formatText(Translation.getString("Message.Npc.HandOver", myPetPlayer), storedMyPet.getPetName(), npcEvent.getNPC().getName()));
                                    }
                                }
                            }
                            event.setWillClose(true);
                            event.setWillDestroy(true);
                        }, MyPetApi.getPlugin());
                        String[] lore;
                        double storageCosts = calculateStorageCosts(myPetPlayer.getMyPet());
                        if (MyPetApi.getPluginHookManager().isHookActive(VaultHook.class) && npc.hasTrait(WalletTrait.class) && storageCosts > 0) {
                            lore = new String[3];
                            lore[1] = "";
                            lore[2] = RESET + Translation.getString("Name.Costs", myPetPlayer) + ": " + (MyPetApi.getHookHelper().getEconomy().canPay(myPetPlayer, storageCosts) ? GREEN : RED) + storageCosts + DARK_GREEN + " " + MyPetApi.getHookHelper().getEconomy().currencyNameSingular();
                        } else {
                            lore = new String[1];
                        }
                        lore[0] = RESET + Util.formatText(Translation.getString("Message.Npc.YesHandOver", myPetPlayer), myPetPlayer.getMyPet().getPetName());
                        menu.setOption(3, new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "WOOL", "GREEN_WOOL")).setData(5).setTitle(GREEN + Translation.getString("Name.Yes", myPetPlayer)).setLore(lore));
                        menu.setOption(5, new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "WOOL", "RED_WOOL")).setData(14).setTitle(RED + Translation.getString("Name.No", myPetPlayer)).setLore(RESET + Util.formatText(Translation.getString("Message.Npc.NoHandOver", myPetPlayer), myPetPlayer.getMyPet().getPetName())));
                        menu.open(player);
                    }
                }
            });
        } else {
            MyPetApi.getRepository().getMyPets(myPetPlayer, new RepositoryCallback<List<StoredMyPet>>() {

                @Override
                public void callback(List<StoredMyPet> pets) {
                    if (pets.size() > 0) {
                        int maxPetCount = 0;
                        if (!Permissions.has(player, "MyPet.admin")) {
                            for (int i = Misc.MAX_STORED_PET_COUNT; i > 0; i--) {
                                if (Permissions.hasLegacy(player, "MyPet.petstorage.limit.", i)) {
                                    maxPetCount = i;
                                    break;
                                }
                            }
                        } else {
                            maxPetCount = Misc.MAX_STORED_PET_COUNT;
                        }
                        String stats = "(" + pets.size() + "/" + maxPetCount + ")";
                        MyPetSelectionGui gui = new MyPetSelectionGui(myPetPlayer, Translation.getString("Message.Npc.TakeTitle", myPetPlayer) + " " + stats);
                        gui.open(pets, new RepositoryCallback<StoredMyPet>() {

                            @Override
                            public void callback(StoredMyPet storedMyPet) {
                                Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(storedMyPet);
                                if (myPet.isPresent()) {
                                    Player player = myPetPlayer.getPlayer();
                                    myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Npc.ChosenPet", myPetPlayer), myPet.get().getPetName()));
                                    WorldGroup wg = WorldGroup.getGroupByWorld(player.getWorld().getName());
                                    myPetPlayer.setMyPetForWorldGroup(wg, myPet.get().getUUID());
                                    MyPetApi.getRepository().updateMyPetPlayer(myPetPlayer, null);
                                    switch(myPet.get().createEntity()) {
                                        case Canceled:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", myPetPlayer), myPet.get().getPetName()));
                                            break;
                                        case NoSpace:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", myPetPlayer), myPet.get().getPetName()));
                                            break;
                                        case NotAllowed:
                                            myPetPlayer.sendMessage(Translation.getString("Message.No.AllowedHere", myPetPlayer).replace("%petname%", myPet.get().getPetName()));
                                            break;
                                        case Dead:
                                            if (de.Keyle.MyPet.api.Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                                                myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", myPetPlayer), myPet.get().getPetName()));
                                            } else {
                                                myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead.Respawn", myPetPlayer), myPet.get().getPetName(), myPet.get().getRespawnTime()));
                                            }
                                            break;
                                    }
                                }
                            }
                        });
                    } else {
                        myPetPlayer.sendMessage(Translation.getString("Message.No.HasPet", myPetPlayer), 5000);
                    }
                }
            });
        }
        return;
    }
    player.sendMessage(Translation.getString("Message.No.HasPet", player));
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) EnumSelector(de.Keyle.MyPet.api.util.EnumSelector) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) Player(org.bukkit.entity.Player) WorldGroup(de.Keyle.MyPet.api.WorldGroup) NPC(net.citizensnpcs.api.npc.NPC) EventHandler(org.bukkit.event.EventHandler) Misc(de.Keyle.MyPet.api.Configuration.Misc) IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) NPCRightClickEvent(net.citizensnpcs.api.event.NPCRightClickEvent) Translation(de.Keyle.MyPet.api.util.locale.Translation) Trait(net.citizensnpcs.api.trait.Trait) Material(org.bukkit.Material) MyPet(de.Keyle.MyPet.api.entity.MyPet) Util(de.Keyle.MyPet.api.Util) IconMenu(de.Keyle.MyPet.api.gui.IconMenu) MyPetApi(de.Keyle.MyPet.MyPetApi) UUID(java.util.UUID) VaultHook(de.Keyle.MyPet.util.hooks.VaultHook) CitizensHook(de.Keyle.MyPet.util.hooks.CitizensHook) List(java.util.List) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) MyPetSelectionGui(de.Keyle.MyPet.gui.selectionmenu.MyPetSelectionGui) Permissions(de.Keyle.MyPet.api.player.Permissions) Optional(java.util.Optional) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) ChatColor(org.bukkit.ChatColor) Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) IconMenu(de.Keyle.MyPet.api.gui.IconMenu) 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) VaultHook(de.Keyle.MyPet.util.hooks.VaultHook) MyPetSelectionGui(de.Keyle.MyPet.gui.selectionmenu.MyPetSelectionGui) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) List(java.util.List) IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) UUID(java.util.UUID) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) EventHandler(org.bukkit.event.EventHandler)

Example 13 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class PetShop method open.

public void open(final Player player) {
    if (!MyPetApi.getHookHelper().isEconomyEnabled()) {
        player.sendMessage(Translation.getString("Message.No.Economy", player));
        return;
    }
    VaultHook economyHook = (VaultHook) MyPetApi.getHookHelper().getEconomy();
    IconMenu shop = new IconMenu(Colorizer.setColors(displayName), event -> {
        if (pets.containsKey(event.getPosition())) {
            final ShopMyPet pet = pets.get(event.getPosition());
            if (pet != null) {
                final Player p = event.getPlayer();
                final MyPetPlayer owner;
                if (MyPetApi.getPlayerManager().isMyPetPlayer(p)) {
                    owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
                    if (owner.hasMyPet() && !Permissions.has(owner, "MyPet.shop.storage")) {
                        p.sendMessage(Translation.getString("Message.Command.Trade.Receiver.HasPet", player));
                        return;
                    }
                } else {
                    owner = null;
                }
                final BukkitRunnable confirmRunner = new BukkitRunnable() {

                    @Override
                    public void run() {
                        IconMenu menu = new IconMenu(Util.formatText(Translation.getString("Message.Shop.Confirm.Title", player), pet.getPetName(), economyHook.getEconomy().format(pet.getPrice())), event1 -> {
                            if (event1.getPosition() == 3) {
                                if (pet.getPrice() > 0) {
                                    if (economyHook.canPay(p.getUniqueId(), pet.getPrice())) {
                                        if (economyHook.getEconomy().withdrawPlayer(p, pet.getPrice()).transactionSuccess()) {
                                            switch(wallet) {
                                                case Bank:
                                                    economyHook.getEconomy().bankDeposit(walletOwner, pet.getPrice());
                                                    break;
                                                case Player:
                                                    economyHook.getEconomy().depositPlayer(Bukkit.getOfflinePlayer(UUID.fromString(walletOwner)), pet.getPrice());
                                                case Private:
                                                    depositPrivate(pet.getPrice());
                                                    break;
                                            }
                                        } else {
                                            p.sendMessage(Translation.getString("Message.No.Money", player));
                                            return;
                                        }
                                    } else {
                                        p.sendMessage(Translation.getString("Message.Shop.NoMoney", player));
                                        return;
                                    }
                                }
                                final MyPetPlayer petOwner;
                                if (owner == null) {
                                    petOwner = MyPetApi.getPlayerManager().registerMyPetPlayer(player);
                                } else {
                                    petOwner = owner;
                                }
                                pet.setOwner(petOwner);
                                final StoredMyPet clonedPet = MyPetApi.getMyPetManager().getInactiveMyPetFromMyPet(pet);
                                clonedPet.setOwner(petOwner);
                                clonedPet.setWorldGroup(WorldGroup.getGroupByWorld(player.getWorld().getName()).getName());
                                clonedPet.setUUID(null);
                                MyPetApi.getRepository().addMyPet(clonedPet, new RepositoryCallback<Boolean>() {

                                    @Override
                                    public void callback(Boolean value) {
                                        p.sendMessage(Util.formatText(Translation.getString("Message.Shop.Success", player), clonedPet.getPetName(), economyHook.getEconomy().format(pet.getPrice())));
                                        MyPetCreateEvent createEvent = new MyPetCreateEvent(clonedPet, MyPetCreateEvent.Source.PetShop);
                                        Bukkit.getServer().getPluginManager().callEvent(createEvent);
                                        if (petOwner.hasMyPet()) {
                                            p.sendMessage(Util.formatText(Translation.getString("Message.Shop.SuccessStorage", player), clonedPet.getPetName()));
                                        } else {
                                            petOwner.setMyPetForWorldGroup(WorldGroup.getGroupByWorld(player.getWorld().getName()), clonedPet.getUUID());
                                            MyPetApi.getRepository().updateMyPetPlayer(petOwner, null);
                                            MyPet activePet = MyPetApi.getMyPetManager().activateMyPet(clonedPet).get();
                                            activePet.createEntity();
                                        }
                                    }
                                });
                            }
                            event1.setWillClose(true);
                            event1.setWillDestroy(true);
                        }, MyPetApi.getPlugin());
                        IconMenuItem icon = new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "WOOL", "LIME_WOOL")).setData(5).setTitle(ChatColor.GREEN + Translation.getString("Name.Yes", player)).setLore(ChatColor.RESET + Util.formatText(Translation.getString("Message.Shop.Confirm.Yes", player), pet.getPetName(), economyHook.getEconomy().format(pet.getPrice())));
                        if (owner != null && owner.hasMyPet()) {
                            icon.addLoreLine("").addLoreLine(Translation.getString("Message.Shop.Confirm.SendStorage", player));
                        }
                        menu.setOption(3, icon);
                        menu.setOption(5, new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "WOOL", "RED_WOOL")).setData(14).setTitle(ChatColor.RED + Translation.getString("Name.No", player)).setLore(ChatColor.RESET + Util.formatText(Translation.getString("Message.Shop.Confirm.No", player), pet.getPetName(), economyHook.getEconomy().format(pet.getPrice()))));
                        menu.open(player);
                    }
                };
                if (owner != null && owner.hasMyPet()) {
                    MyPetApi.getRepository().getMyPets(owner, new RepositoryCallback<List<StoredMyPet>>() {

                        @Override
                        public void callback(List<StoredMyPet> value) {
                            int petCount = getInactivePetCount(value, WorldGroup.getGroupByWorld(player.getWorld().getName()).getName()) - 1;
                            int limit = getMaxPetCount(p);
                            if (petCount >= limit) {
                                p.sendMessage(Util.formatText(Translation.getString("Message.Command.Switch.Limit", player), limit));
                                return;
                            }
                            confirmRunner.runTaskLater(MyPetApi.getPlugin(), 5L);
                        }
                    });
                } else {
                    confirmRunner.runTaskLater(MyPetApi.getPlugin(), 5L);
                }
            }
        }
    }, MyPetApi.getPlugin());
    double balance = economyHook.getBalance(player);
    for (int pos : pets.keySet()) {
        ShopMyPet pet = pets.get(pos);
        IconMenuItem icon = pet.getIcon();
        ChatColor canPay = balance >= pet.getPrice() ? ChatColor.GREEN : ChatColor.RED;
        icon.addLoreLine(ChatColor.RESET + "" + ChatColor.BLUE + Translation.getString("Name.Price", player) + ": " + canPay + economyHook.getEconomy().format(pet.getPrice()), 0);
        shop.setOption(pos, icon);
    }
    shop.open(player);
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) IconMenu(de.Keyle.MyPet.api.gui.IconMenu) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Material(org.bukkit.Material) VaultHook(de.Keyle.MyPet.util.hooks.VaultHook) MyPetCreateEvent(de.Keyle.MyPet.api.event.MyPetCreateEvent) IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) ChatColor(org.bukkit.ChatColor) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 14 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet 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, 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()));
            }
            boolean levelFound = false;
            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());
                    levelFound = true;
                }
            }
            if (!levelFound) {
                lore.add(RESET + Translation.getString("Name.Exp", player) + ": " + GOLD + String.format("%1.2f", mypet.getExp()));
            }
            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 + Colorizer.setColors(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);
            egg.ifPresent(service -> service.updateIcon(mypet.getPetType(), icon));
            int pos = menu.addOption(icon);
            petSlotList.put(pos, mypet);
        }
        if (previousPage != page) {
            menu.setOption(45, new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "SIGN", "OAK_SIGN")).setTitle("" + previousPage + " ≪≪"));
        }
        if (previousPage != page) {
            menu.setOption(53, new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "SIGN", "OAK_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) Material(org.bukkit.Material) TagCompound(de.keyle.knbt.TagCompound) IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 15 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class PlayerListener method onMyPetPlayerChangeWorld.

@EventHandler
public void onMyPetPlayerChangeWorld(final PlayerChangedWorldEvent event) {
    if (!event.getPlayer().isOnline()) {
        return;
    }
    final WorldGroup toGroup = WorldGroup.getGroupByWorld(event.getPlayer().getWorld().getName());
    if (MyPetApi.getPlayerManager().isMyPetPlayer(event.getPlayer())) {
        final MyPetPlayer myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer(event.getPlayer());
        final WorldGroup fromGroup = WorldGroup.getGroupByWorld(event.getFrom().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) {
                                if (Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                                    myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", runMyPet.getOwner()), runMyPet.getPetName()));
                                } else {
                                    myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead.Respawn", runMyPet.getOwner()), 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 (toGroup.isDisabled()) {
                return;
            }
            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

StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)23 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)15 MyPet (de.Keyle.MyPet.api.entity.MyPet)10 Player (org.bukkit.entity.Player)9 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)8 WorldGroup (de.Keyle.MyPet.api.WorldGroup)7 IOException (java.io.IOException)7 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)7 List (java.util.List)6 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)5 Skilltree (de.Keyle.MyPet.api.skill.skilltree.Skilltree)5 UUID (java.util.UUID)5 IconMenu (de.Keyle.MyPet.api.gui.IconMenu)4 IconMenuItem (de.Keyle.MyPet.api.gui.IconMenuItem)4 RepositoryMyPetConverterService (de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService)4 TagCompound (de.keyle.knbt.TagCompound)4 Material (org.bukkit.Material)4 Repository (de.Keyle.MyPet.api.repository.Repository)3 FancyMessage (de.Keyle.MyPet.api.util.chat.FancyMessage)3 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)3