Solved My Shop Plugin repeats command increasingly

Discussion in 'Plugin Development' started by Caliburnz, Feb 18, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    Caliburnz

    So, I'm fairly new to the Bukkit API and was trying to make a GUI Shop plugin for my server. It works correctly kinda, when a player runs the command '/shop' it opens an Inventory for a player to buy and sell items that they click on. However, if they close the inventory after they are done, and go to use the command again later, it runs all the internal commands twice, thrice, etc. depending on the amount of times they use the /shop command. It only resets on server reloads/restarts. I've been staring at my code for hours and I'm not making any progress any suggestions on how to fix this? And yes, I know I don't check if they have the item to sell in their inventory, that's on my to-do list.

    Main Class:
    Code:
    package org.skyworld.skyshop;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.skyworld.skyshop.commands.ShopCommand;
    
    
    public class Main extends JavaPlugin implements Listener
    {
        @Override
        public void onEnable()
        {
            new ShopCommand(this);
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    }
    
    Command Class:
    Code:
    package org.skyworld.skyshop.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.skyworld.skyshop.Main;
    import org.skyworld.skyshop.Menu;
    
    public class ShopCommand implements CommandExecutor
    {
        private Main plugin;
        private String name;
    
        public ShopCommand(Main plugin)
        {
            this.plugin = plugin;
            plugin.getCommand("shop").setExecutor(this);
        }  
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if (!(sender instanceof Player))
                {
                    sender.sendMessage("Only players can use this command.");
                    return true;
                }
          
            Player p = (Player) sender;
          
            if (p.hasPermission("skyshop.shop"))
            {
                Menu shop = new Menu(plugin);
                shop.show(p);
                return true;
            }
            else
            {
                p.sendMessage("You do not have permission to use this command.");
            }
          
            return false;
        }
    }
    
    Menu Class:
    Code:
    package org.skyworld.skyshop;
    
    import java.math.BigDecimal;
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    
    import com.earth2me.essentials.api.Economy;
    import com.earth2me.essentials.api.NoLoanPermittedException;
    import com.earth2me.essentials.api.UserDoesNotExistException;
    
    public class Menu extends Economy implements Listener
    {
        private Main plugin;
        private Inventory shop;
        private Inventory cropShop;
        private Inventory matShop;
        private Inventory rankShop;
      
        private String[] seedLore = {"Buy 10,000", "- Gives slight bonus better to Ore Chances!"};
        private String[] wheatLore = {"Buy 100,000", "- Gives another slight bonus better to Ore Chances!", "- Gives access to the /feed command"};
        private String[] sugarLore = {"Buy 500,000", "- Same benefits as Wheat", "- Gives access to the /heal command", "- Gives access to the /fly command"};
      
        public Menu(Plugin p)
        {
            shop = Bukkit.getServer().createInventory(null, 9, "SkyShop");
            cropShop = Bukkit.getServer().createInventory(null, 27, "The Crop Shop");
            matShop = Bukkit.getServer().createInventory(null, 27, "The Material Shop");
            rankShop = Bukkit.getServer().createInventory(null, 9, "The Rank Shop");
          
            shop.setItem(2, createItem(Material.WHEAT_SEEDS, "The Crop Shop", "Sell your Crops here!"));
            shop.setItem(4, createItem(Material.IRON_INGOT, "The Material Shop", "Sell your Materials here!"));
            shop.setItem(6, createItem(Material.NAME_TAG, "The Rank Shop", "Buy a Rank with in-game Money!"));
          
            cropShop.setItem(0, createItem(Material.WHEAT_SEEDS, "Seeds", "Buy 16 -- Sell 8"));
            cropShop.setItem(1, createItem(Material.WHEAT, "Wheat", "Buy 24 -- Sell 12"));
            cropShop.setItem(2, createItem(Material.MELON_SEEDS, "Melon Seeds", "Buy 16 -- Sell 1"));
            cropShop.setItem(3, createItem(Material.MELON_SLICE, "Melon Slice", "Buy 24 -- Sell 1"));
            cropShop.setItem(4, createItem(Material.SUGAR_CANE, "Sugar Cane", "Buy 32 -- Sell 12"));
            cropShop.setItem(5, createItem(Material.CACTUS, "Cactus", "Buy 48 -- Sell 24"));
            cropShop.setItem(6, createItem(Material.PUMPKIN_SEEDS, "Pumpkin Seeds", "Buy 16 -- Sell 8"));
            cropShop.setItem(7, createItem(Material.PUMPKIN, "Pumpkin", "Buy 16 -- Sell 8"));
            cropShop.setItem(18, createItem(Material.OAK_SAPLING, "Oak Sapling", "Buy 32 -- Sell 16"));
            cropShop.setItem(19, createItem(Material.JUNGLE_SAPLING, "Jungle Sapling", "Buy 32 -- Sell 16"));
            cropShop.setItem(20, createItem(Material.BIRCH_SAPLING, "Birch Sapling", "Buy 32 -- Sell 16"));
            cropShop.setItem(21, createItem(Material.ACACIA_SAPLING, "Acacia Sapling", "Buy 32 -- Sell 16"));
            cropShop.setItem(22, createItem(Material.SPRUCE_SAPLING, "Spruce Sapling", "Buy 32 -- Sell 16"));
          
            matShop.setItem(2, createItem(Material.COAL, "Coal", "Buy 128 -- Sell 64"));
            matShop.setItem(3, createItem(Material.IRON_INGOT, "Iron Ingot", "Buy 256 -- Sell 128"));
            matShop.setItem(4, createItem(Material.GOLD_INGOT, "Gold Ingot", "Buy 512 -- Sell 256"));
            matShop.setItem(5, createItem(Material.DIAMOND, "Diamond", "Buy 1024 -- Sell 512"));
            matShop.setItem(6, createItem(Material.EMERALD, "Emerald", "Buy 2048 -- Sell 1024"));
          
            rankShop.setItem(0, createItem(Material.WHEAT_SEEDS, "Seed Rank", seedLore));
          
          
            Bukkit.getServer().getPluginManager().registerEvents(this, p);
        }
      
        private ItemStack createItem(Material mat, String name, String lore)
        {
                ItemStack i = new ItemStack(mat, 1);
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList(lore));
                i.setItemMeta(im);
                return i;
        }
      
        private ItemStack createItem(Material mat, String name, String[] lore)
        {
                ItemStack i = new ItemStack(mat, 1);
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList(lore));
                i.setItemMeta(im);
                return i;
        }
      
        public void show(Player p)
        {
            p.openInventory(shop);
        }
      
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event)
        {
            Player player = (Player) event.getWhoClicked();
          
            if (event.getCurrentItem().getItemMeta().getDisplayName().contains("The Crop Shop"))
            {
                event.setCancelled(true);
                player.openInventory(cropShop);  
            }
            if (event.getCurrentItem().getItemMeta().getDisplayName().contains("The Material Shop"))
            {
                event.setCancelled(true);
                player.openInventory(matShop);  
            }
            if (event.getCurrentItem().getItemMeta().getDisplayName().contains("The Rank Shop"))
            {
                event.setCancelled(true);
                player.openInventory(rankShop);  
            }
          
          
            if (event.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("seeds"))
            {
                if (event.isLeftClick())
                {
                        try
                        {
                            substract(player.getName(), new BigDecimal(16.0));
                            player.sendMessage("You paid $16");
                            event.setCancelled(true);
                            Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "give " + player.getName() + " wheat_seeds 1");
                        }
                        catch (NoLoanPermittedException e1)
                        {
                            player.sendMessage("Not enough money!");
                            event.setCancelled(true);
                        }
                        catch (UserDoesNotExistException e1) {}  
                }
                else if (event.isRightClick())
                {
                        try
                        {
                            player.sendMessage("You earned $8");                      
                            event.setCancelled(true);
                            Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(),
                                    "clear " + player.getName() + " wheat_seeds 1");
                            add(player.getName(), new BigDecimal(8));      
                          
                        }
                        catch (NoLoanPermittedException e1)
                        {
                            player.sendMessage("Not enough money!");
                            event.setCancelled(true);
                        }
                        catch (UserDoesNotExistException e1) {}      
                }
                return;
            }
            if (event.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("wheat"))
            {
                if (event.isLeftClick())
                {
                        try
                        {
                            substract(player.getName(), new BigDecimal(24));
                            player.sendMessage("You paid $24");                      
                            event.setCancelled(true);                      
                            Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "give " + player.getName() + " wheat 1");
                        }
                        catch (NoLoanPermittedException e1)
                        {
                            player.sendMessage("Not enough money!");
                            event.setCancelled(true);
                        }
                        catch (UserDoesNotExistException e1) {}      
                }
                else if (event.isRightClick())
                {
                        try
                        {
                            add(player.getName(), new BigDecimal(12));
                            player.sendMessage("You earned $12");                      
                            event.setCancelled(true);
                            Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "clear " + player.getName() + " wheat 1");                      
                        }
                        catch (NoLoanPermittedException e1)
                        {
                            player.sendMessage("Not enough money!");                  
                            event.setCancelled(true);
                        }
                        catch (UserDoesNotExistException e1) {}                  
                }
                return;
            }
        }
      
        @EventHandler
        public void onInventoryClose(InventoryCloseEvent event)
        {
          
        }
    }
    
    
     
    Last edited: Feb 18, 2019
  2. Offline

    mehboss

    I believe the problem is that you re-register your menu class event every single time the player does /shop.

    CHANGE:
    Code:
    public class ShopCommand implements CommandExecutor
    {
        private Main plugin;
        private String name;
    
        public ShopCommand(Main plugin)
        {
            this.plugin = plugin;
            plugin.getCommand("shop").setExecutor(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        { 
            if (!(sender instanceof Player))
                {
                    sender.sendMessage("Only players can use this command.");
                    return true;
                }
       
            Player p = (Player) sender;
       
            if (p.hasPermission("skyshop.shop"))
            {
                Menu shop = new Menu(plugin);
                shop.show(p);
                return true;
            }
            else
            {
                p.sendMessage("You do not have permission to use this command.");
            }
       
            return false;
        }
    }
    
    TO:
    Code:
    public class ShopCommand implements CommandExecutor
    {
        private Main plugin;
        private String name;
    
        Menu shop = null;
     
        public ShopCommand(Main plugin)
        {
            this.plugin = plugin;
            plugin.getCommand("shop").setExecutor(this);
            shop = new Menu(plugin);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        { 
            if (!(sender instanceof Player))
                {
                    sender.sendMessage("Only players can use this command.");
                    return true;
                }
       
            Player p = (Player) sender;
       
            if (p.hasPermission("skyshop.shop"))
            {
                shop.show(p);
                return true;
            }
            else
            {
                p.sendMessage("You do not have permission to use this command.");
            }
       
            return false;
        }
    }
    If your problem has been solved, mark your thread as solved. Thanks!
     
  3. Offline

    Caliburnz

    Yes! Thank you so much!

     
    mehboss likes this.
Thread Status:
Not open for further replies.

Share This Page