Search in sources :

Example 1 with PetShop

use of de.Keyle.MyPet.util.shop.PetShop in project MyPet by xXKeyleXx.

the class CommandShop method onCommand.

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (!MyPetApi.getHookHelper().isEconomyEnabled()) {
        sender.sendMessage(Translation.getString("Message.No.Economy", sender));
        return true;
    }
    Player player = null;
    if (sender instanceof ConsoleCommandSender && args.length < 2) {
        sender.sendMessage("You can't use this command from server console!");
        return true;
    }
    if (sender instanceof Player) {
        if (WorldGroup.getGroupByWorld(((Player) sender).getWorld()).isDisabled()) {
            sender.sendMessage(Translation.getString("Message.No.AllowedHere", sender));
            return true;
        }
    }
    if (args.length > 1) {
        if (!(sender instanceof Player) || Permissions.has((Player) sender, "MyPet.admin")) {
            player = Bukkit.getPlayer(args[1]);
            if (player == null || !player.isOnline()) {
                sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", sender));
                return true;
            }
        }
    }
    if (player == null) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("You can't use this command from server console!");
            return true;
        }
        player = (Player) sender;
    }
    final Optional<ShopManager> shopManager = MyPetApi.getServiceManager().getService(ShopManager.class);
    if (shopManager.isPresent()) {
        if (args.length > 0) {
            String shop = args[0];
            if (Permissions.has(player, "MyPet.shop.access." + shop) || Permissions.has(player, "MyPet.admin")) {
                shopManager.get().open(shop, player);
            } else {
                player.sendMessage(Translation.getString("Message.No.Allowed", player));
            }
        } else {
            String shop = shopManager.get().getDefaultShopName();
            if (shop != null) {
                if (Permissions.has(player, "MyPet.shop.access." + shop) || Permissions.has(player, "MyPet.admin")) {
                    shopManager.get().open(player);
                    return true;
                }
            } else {
                final List<String> availableShops = getAvailablePetShops(player);
                if (availableShops != null && availableShops.size() > 0) {
                    final Player finalPlayer = player;
                    Map<Integer, String> shops = new HashMap<>();
                    IconMenu menu = new IconMenu(Translation.getString("Message.Shop.Available", player), event -> {
                        String shopname = shops.get(event.getPosition());
                        if (shopname != null) {
                            final String finalShopname = shopname;
                            new BukkitRunnable() {

                                @Override
                                public void run() {
                                    shopManager.get().open(finalShopname, finalPlayer);
                                }
                            }.runTaskLater(MyPetApi.getPlugin(), 5L);
                            event.setWillClose(true);
                            event.setWillDestroy(true);
                        }
                    }, MyPetApi.getPlugin());
                    ItemDatabase itemDatabase = MyPetApi.getServiceManager().getService(ItemDatabase.class).get();
                    for (String shopname : availableShops) {
                        PetShop s = shopManager.get().getShop(shopname);
                        IconMenuItem icon = new IconMenuItem();
                        icon.setTitle(RESET + Colorizer.setColors(s.getDisplayName()));
                        SkilltreeIcon si = s.getIcon();
                        MaterialHolder material = itemDatabase.getByID(si.getMaterial());
                        if (material == null) {
                            material = itemDatabase.getByID("chest");
                        }
                        icon.setMaterial(material.getMaterial()).setGlowing(si.isGlowing());
                        if (material.isLegacy()) {
                            icon.setData(material.getLegacyId().getData());
                        }
                        if (Util.isBetween(0, 53, s.getPosition())) {
                            menu.setOption(s.getPosition(), icon);
                            shops.put(s.getPosition(), s.getName());
                        } else {
                            shops.put(menu.addOption(icon), s.getName());
                        }
                    }
                    menu.open(player);
                    return true;
                }
            }
            player.sendMessage(Translation.getString("Message.No.Allowed", player));
        }
    }
    return true;
}
Also used : PetShop(de.Keyle.MyPet.util.shop.PetShop) Player(org.bukkit.entity.Player) IconMenu(de.Keyle.MyPet.api.gui.IconMenu) ItemDatabase(de.Keyle.MyPet.api.util.inventory.material.ItemDatabase) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MaterialHolder(de.Keyle.MyPet.api.util.inventory.material.MaterialHolder) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) SkilltreeIcon(de.Keyle.MyPet.api.skill.skilltree.SkilltreeIcon) ShopManager(de.Keyle.MyPet.util.shop.ShopManager) IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem)

Aggregations

IconMenu (de.Keyle.MyPet.api.gui.IconMenu)1 IconMenuItem (de.Keyle.MyPet.api.gui.IconMenuItem)1 SkilltreeIcon (de.Keyle.MyPet.api.skill.skilltree.SkilltreeIcon)1 ItemDatabase (de.Keyle.MyPet.api.util.inventory.material.ItemDatabase)1 MaterialHolder (de.Keyle.MyPet.api.util.inventory.material.MaterialHolder)1 PetShop (de.Keyle.MyPet.util.shop.PetShop)1 ShopManager (de.Keyle.MyPet.util.shop.ShopManager)1 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)1 Player (org.bukkit.entity.Player)1 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)1