Search in sources :

Example 11 with IconMenuItem

use of de.Keyle.MyPet.api.gui.IconMenuItem in project MyPet by xXKeyleXx.

the class CommandChooseSkilltree method onCommand.

public boolean onCommand(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 (MyPetApi.getMyPetManager().hasActiveMyPet(player)) {
        final MyPet myPet = MyPetApi.getMyPetManager().getMyPet(player);
        final MyPetPlayer myPetOwner = myPet.getOwner();
        if (Configuration.Skilltree.AUTOMATIC_SKILLTREE_ASSIGNMENT && !myPet.getOwner().isMyPetAdmin()) {
            myPet.autoAssignSkilltree();
            sender.sendMessage(Translation.getString("Message.Command.ChooseSkilltree.AutomaticSkilltreeAssignment", myPet.getOwner().getLanguage()));
        } else if (myPet.getSkilltree() != null && Configuration.Skilltree.CHOOSE_SKILLTREE_ONLY_ONCE && !myPet.getOwner().isMyPetAdmin()) {
            sender.sendMessage(Util.formatText(Translation.getString("Message.Command.ChooseSkilltree.OnlyOnce", myPet.getOwner().getLanguage()), myPet.getPetName()));
        } else if (SkillTreeMobType.hasMobType(myPet.getPetType())) {
            SkillTreeMobType skillTreeMobType = SkillTreeMobType.byPetType(myPet.getPetType());
            if (args.length >= 1) {
                String skilltreeName = "";
                for (String arg : args) {
                    skilltreeName += arg + " ";
                }
                skilltreeName = skilltreeName.substring(0, skilltreeName.length() - 1);
                if (skillTreeMobType.hasSkillTree(skilltreeName)) {
                    SkillTree skillTree = skillTreeMobType.getSkillTree(skilltreeName);
                    if (Permissions.hasLegacy(player, "MyPet.skilltree.", skillTree.getPermission())) {
                        int requiredLevel = skillTree.getRequiredLevel();
                        if (requiredLevel > 1 && myPet.getExperience().getLevel() < requiredLevel) {
                            myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Skilltree.RequiresLevel.Message", player), myPet.getPetName(), requiredLevel));
                        } else if (myPet.setSkilltree(skillTree)) {
                            sender.sendMessage(Util.formatText(Translation.getString("Message.Skilltree.SwitchedTo", player), skillTree.getName()));
                            if (!myPet.getOwner().isMyPetAdmin() || Configuration.Skilltree.SWITCH_FEE_ADMIN) {
                                double switchPenalty = Configuration.Skilltree.SWITCH_FEE_FIXED;
                                switchPenalty += myPet.getExperience().getExp() * Configuration.Skilltree.SWITCH_FEE_PERCENT / 100.;
                                if (requiredLevel > 1) {
                                    double minExp = myPet.getExperience().getExpByLevel(requiredLevel);
                                    switchPenalty = myPet.getExp() - switchPenalty < minExp ? myPet.getExp() - minExp : switchPenalty;
                                }
                                myPet.getExperience().removeExp(switchPenalty);
                            }
                        } else {
                            sender.sendMessage(Translation.getString("Message.Skilltree.NotSwitched", player));
                        }
                    } else {
                        sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Skilltree.CantFindSkilltree", player), skilltreeName));
                    }
                } else {
                    sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Skilltree.CantFindSkilltree", player), skilltreeName));
                }
            } else {
                List<SkillTree> availableSkilltrees = new ArrayList<>();
                for (SkillTree skillTree : skillTreeMobType.getSkillTrees()) {
                    if (Permissions.hasLegacy(player, "MyPet.skilltree.", skillTree.getPermission())) {
                        availableSkilltrees.add(skillTree);
                    }
                }
                if (availableSkilltrees.size() == 0) {
                    sender.sendMessage(Util.formatText(Translation.getString("Message.Command.ChooseSkilltree.NoneAvailable", player), myPet.getPetName()));
                    return true;
                }
                final Map<Integer, SkillTree> skilltreeSlotMap = new HashMap<>();
                IconMenu menu = new IconMenu(Util.cutString(Util.formatText(Translation.getString("Message.Skilltree.Available", myPetOwner), myPet.getPetName()), 32), new IconMenu.OptionClickEventHandler() {

                    @Override
                    public void onOptionClick(IconMenu.OptionClickEvent event) {
                        if (myPet != myPetOwner.getMyPet()) {
                            event.setWillClose(true);
                            event.setWillDestroy(true);
                            return;
                        }
                        if (skilltreeSlotMap.containsKey(event.getPosition())) {
                            SkillTree selecedSkilltree = skilltreeSlotMap.get(event.getPosition());
                            if (selecedSkilltree != null) {
                                int requiredLevel = selecedSkilltree.getRequiredLevel();
                                if (requiredLevel > 1 && myPet.getExperience().getLevel() < requiredLevel) {
                                    myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Skilltree.RequiresLevel.Message", myPetOwner), myPet.getPetName(), requiredLevel));
                                } else if (myPet.setSkilltree(selecedSkilltree)) {
                                    myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Skilltree.SwitchedTo", myPetOwner), selecedSkilltree.getName()));
                                    if (!myPet.getOwner().isMyPetAdmin() || Configuration.Skilltree.SWITCH_FEE_ADMIN) {
                                        double switchPenalty = Configuration.Skilltree.SWITCH_FEE_FIXED;
                                        switchPenalty += myPet.getExperience().getExp() * Configuration.Skilltree.SWITCH_FEE_PERCENT / 100.;
                                        if (requiredLevel > 1) {
                                            double minExp = myPet.getExperience().getExpByLevel(requiredLevel);
                                            switchPenalty = myPet.getExp() - switchPenalty < minExp ? myPet.getExp() - minExp : switchPenalty;
                                        }
                                        myPet.getExperience().removeExp(switchPenalty);
                                    }
                                } else {
                                    myPet.getOwner().sendMessage(Translation.getString("Message.Skilltree.NotSwitched", myPetOwner));
                                }
                            }
                        }
                        event.setWillClose(true);
                        event.setWillDestroy(true);
                    }
                }, MyPetApi.getPlugin());
                for (int i = 0; i < availableSkilltrees.size(); i++) {
                    SkillTree addedSkilltree = availableSkilltrees.get(i);
                    TagCompound tag = addedSkilltree.getIconItem();
                    IconMenuItem option = IconMenuItem.fromTagCompund(tag);
                    option.setTitle(ChatColor.RESET + "❱❱❱  " + ChatColor.DARK_GREEN + Colorizer.setColors(addedSkilltree.getDisplayName()) + ChatColor.RESET + "  ❰❰❰");
                    boolean selectable = false;
                    int requiredLevel = addedSkilltree.getRequiredLevel();
                    if (requiredLevel > 1) {
                        selectable = myPet.getExperience().getLevel() >= addedSkilltree.getRequiredLevel();
                    }
                    List<String> description = new ArrayList<>();
                    if (requiredLevel > 1) {
                        String reqLevelMessage = ChatColor.RESET + "▶▶▶  ";
                        if (selectable) {
                            reqLevelMessage += ChatColor.GREEN;
                        } else {
                            reqLevelMessage += ChatColor.DARK_RED;
                        }
                        reqLevelMessage += Util.formatText(Translation.getString("Message.Skilltree.RequiresLevel.Item", myPetOwner), requiredLevel) + ChatColor.RESET + "  ◀◀◀";
                        description.add(reqLevelMessage);
                    }
                    for (String line : addedSkilltree.getDescription()) {
                        description.add(Colorizer.setColors(line));
                    }
                    option.addLore(description);
                    menu.setOption(i, option);
                    skilltreeSlotMap.put(i, addedSkilltree);
                }
                menu.open(player);
            }
        }
    } 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) IconMenu(de.Keyle.MyPet.api.gui.IconMenu) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) HashMap(java.util.HashMap) MyPet(de.Keyle.MyPet.api.entity.MyPet) ArrayList(java.util.ArrayList) TagCompound(de.keyle.knbt.TagCompound) SkillTree(de.Keyle.MyPet.api.skill.skilltree.SkillTree) SkillTreeMobType(de.Keyle.MyPet.api.skill.skilltree.SkillTreeMobType) IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem)

Example 12 with IconMenuItem

use of de.Keyle.MyPet.api.gui.IconMenuItem in project MyPet by xXKeyleXx.

the class IconMenuInventory method open.

@Override
public void open(IconMenu menu, HumanEntity player) {
    size = menu.getSize();
    minecraftInventory = new CustomInventory(size, menu.getTitle());
    for (int slot = 0; slot < size; slot++) {
        IconMenuItem menuItem = menu.getOption(slot);
        if (menuItem != null) {
            ItemStack item = createItemStack(menuItem);
            minecraftInventory.setItem(slot, item);
        }
    }
    player.openInventory(minecraftInventory.getBukkitInventory());
}
Also used : IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) CraftItemStack(org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack) ItemStack(net.minecraft.server.v1_10_R1.ItemStack) CustomInventory(de.Keyle.MyPet.compat.v1_10_R1.util.inventory.CustomInventory)

Example 13 with IconMenuItem

use of de.Keyle.MyPet.api.gui.IconMenuItem in project MyPet by xXKeyleXx.

the class IconMenuInventory method update.

@Override
public void update(IconMenu menu) {
    for (int slot = 0; slot < size; slot++) {
        IconMenuItem menuItem = menu.getOption(slot);
        if (menuItem != null) {
            ItemStack item = createItemStack(menuItem);
            minecraftInventory.setItem(slot, item);
        } else {
            minecraftInventory.setItem(slot, null);
        }
    }
}
Also used : IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) CraftItemStack(org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack) ItemStack(net.minecraft.server.v1_10_R1.ItemStack)

Example 14 with IconMenuItem

use of de.Keyle.MyPet.api.gui.IconMenuItem in project MyPet by xXKeyleXx.

the class IconMenuInventory method open.

@Override
public void open(IconMenu menu, HumanEntity player) {
    size = menu.getSize();
    minecraftInventory = new CustomInventory(size, menu.getTitle());
    for (int slot = 0; slot < size; slot++) {
        IconMenuItem menuItem = menu.getOption(slot);
        if (menuItem != null) {
            ItemStack item = createItemStack(menuItem);
            minecraftInventory.setItem(slot, item);
        }
    }
    player.openInventory(minecraftInventory.getBukkitInventory());
}
Also used : IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) CraftItemStack(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack) ItemStack(net.minecraft.server.v1_12_R1.ItemStack) CustomInventory(de.Keyle.MyPet.compat.v1_12_R1.util.inventory.CustomInventory)

Example 15 with IconMenuItem

use of de.Keyle.MyPet.api.gui.IconMenuItem 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)

Aggregations

IconMenuItem (de.Keyle.MyPet.api.gui.IconMenuItem)21 IconMenu (de.Keyle.MyPet.api.gui.IconMenu)3 TagCompound (de.keyle.knbt.TagCompound)2 ItemStack (net.minecraft.server.v1_10_R1.ItemStack)2 ItemStack (net.minecraft.server.v1_11_R1.ItemStack)2 ItemStack (net.minecraft.server.v1_12_R1.ItemStack)2 ItemStack (net.minecraft.server.v1_7_R4.ItemStack)2 ItemStack (net.minecraft.server.v1_8_R1.ItemStack)2 ItemStack (net.minecraft.server.v1_8_R2.ItemStack)2 ItemStack (net.minecraft.server.v1_8_R3.ItemStack)2 ItemStack (net.minecraft.server.v1_9_R1.ItemStack)2 ItemStack (net.minecraft.server.v1_9_R2.ItemStack)2 CraftItemStack (org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack)2 CraftItemStack (org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack)2 CraftItemStack (org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack)2 CraftItemStack (org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack)2 CraftItemStack (org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack)2 CraftItemStack (org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack)2 CraftItemStack (org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack)2 CraftItemStack (org.bukkit.craftbukkit.v1_9_R1.inventory.CraftItemStack)2