ItemStack not Spawning

Discussion in 'Plugin Development' started by xXDerMCFreakXx, Nov 22, 2018.

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

    xXDerMCFreakXx

    Hello again. Im trying to create items now inside a menu based on the command input. The player should specify which menu, which, slot, for what price it will be bought and sold in the arguments of the command. This is just a part of the whole code, but the rest isnt really important. Any help?
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         if (cmd.getName().equals("shop")) {
           Player player = (Player) sender;
           player.openInventory(menus[0]);
           return true;
         } else {
           if (cmd.getName().equals("shopadd")) {
             Player playr = (Player) sender;
             String Menu = args[0];
             String Slot = args[1];
             String Price = args[2];
             String SPrice = args[3];
             ItemStack tempItem;
             ItemMeta tempMeta;
             @SuppressWarnings("deprecation")
             ItemStack Item = playr.getItemInHand();
             ArrayList<String> lores = new ArrayList<String>();
             
             if (Menu.equals("Weapons")) {
               menus[5].addItem(tempItem = new ItemStack(Item.getType(), Integer.valueOf(Slot)));
               tempMeta = tempItem.getItemMeta();
               lores.add("Left Click to Buy");
               lores.add("Right Click to Sell");
               lores.add("Price: " + Price + "$");
               lores.add("Sell for: " + SPrice + "$");
               tempMeta.setLore(lores);
               tempItem.setItemMeta(tempMeta);
               lores.clear();
             }
     
    Last edited by a moderator: Nov 22, 2018
  2. @xXDerMCFreakXx

    You need to specify what doesn't work or what you would like to do.
     
  3. Offline

    xXDerMCFreakXx

    Well as i said im trying to spawn an item inside the inventory, called "Weapons". When i execute the command with all arguments correct and then check the shop, the wanted item isnt there.. Before i posted this thread, i made a debug message under "If (Menu.equals("Weapons"))". The message got displayed, but the item wasnt added.
     
  4. @xXDerMCFreakXx

    It looks like the only menu that will be shown to the player is menus[0] (when you execute /shop).
    And so far the only menu that you can change with the command /shopadd is menus[5]. (You hardcoded it like that right below the line where you check if the Menu equals "Weapons".
     
  5. Offline

    xXDerMCFreakXx

    yes, thats right. I made the same thing for every other menu but only included "weapons" in this thread. even if i try to add items to the "weapons" menu, it wont work. Here, let me show you my whole code:
    Code:
    package com.gmail.monimitev.shopplugin;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
       int keys = 30;
       FileConfiguration config;
       Inventory[] menus = new Inventory [7];
       ItemStack[] menuKeys = new ItemStack[keys];
       
       @Override
       public void onEnable() {
         
         // Instantiating Menus
         
         menus[0] = Bukkit.createInventory(null, 9, "Categories");
         menus[1] = Bukkit.createInventory(null, 54, "Raid");
         menus[2] = Bukkit.createInventory(null, 54, "Defense");
         menus[3] = Bukkit.createInventory(null, 54, "Blocks");
         menus[4] = Bukkit.createInventory(null, 54, "Minerals");
         menus[5] = Bukkit.createInventory(null, 54, "Potions");
         menus[6] = Bukkit.createInventory(null, 54, "Weapons");
         
         // Instantiating Main Keys
         
         menuKeys[0] = new ItemStack(Material.REDSTONE);
         ItemMeta backmet = menuKeys[0].getItemMeta();
         backmet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Back");
         menuKeys[0].setItemMeta(backmet);
         
         menuKeys[1] = new ItemStack(Material.IRON_CHESTPLATE);
         ItemMeta defmet = menuKeys[0].getItemMeta();
         defmet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Defense");
         menuKeys[1].setItemMeta(defmet);
         
         menuKeys[2] = new ItemStack(Material.COBBLESTONE);
         ItemMeta blomet = menuKeys[0].getItemMeta();
         blomet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Blocks");
         menuKeys[2].setItemMeta(blomet);
         
         menuKeys[3] = new ItemStack(Material.DIAMOND_BLOCK);
         ItemMeta minmet = menuKeys[0].getItemMeta();
         minmet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Minerals");
         menuKeys[3].setItemMeta(minmet);
         
         menuKeys[4] = new ItemStack(Material.POTION);
         ItemMeta potmet = menuKeys[0].getItemMeta();
         potmet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Potions");
         menuKeys[4].setItemMeta(potmet);
         
         menuKeys[5] = new ItemStack(Material.DIAMOND_SWORD);
         ItemMeta wepmet = menuKeys[0].getItemMeta();
         wepmet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Weapons");
         menuKeys[5].setItemMeta(wepmet);
         
         menuKeys[6] = new ItemStack(Material.TNT);
         ItemMeta raimet = menuKeys[0].getItemMeta();
         raimet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Raid");
         menuKeys[6].setItemMeta(raimet);
         
         // Positioning Keys
         
         menus[0].setItem(2, menuKeys[1]);
         menus[0].setItem(3, menuKeys[2]);
         menus[0].setItem(4, menuKeys[3]);
         menus[0].setItem(5, menuKeys[4]);
         menus[0].setItem(6, menuKeys[5]);
         menus[0].setItem(7, menuKeys[6]);
         menus[1].setItem(49, menuKeys[0]);
         menus[2].setItem(49, menuKeys[0]);
         menus[3].setItem(49, menuKeys[0]);
         menus[4].setItem(49, menuKeys[0]);
         menus[5].setItem(49, menuKeys[0]);
         menus[6].setItem(49, menuKeys[0]);
         
         Bukkit.getServer().getPluginManager().registerEvents(this, this);
       }
       
       public void onDisable() {
         
         
         
       }
       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         Player publicp = (Player) sender;
         if (cmd.getName().equals("shop")) {
           Player player = (Player) sender;
           player.openInventory(menus[0]);
           return true;
         } else {
           if (cmd.getName().equals("shopadd")) {
             Player playr = (Player) sender;
             String Menu = args[0];
             String Slot = args[1];
             String Price = args[2];
             String SPrice = args[3];
             ItemStack tempItem;
             ItemMeta tempMeta;
             @SuppressWarnings("deprecation")
             ItemStack Item = playr.getItemInHand();
             ArrayList<String> lores = new ArrayList<String>();
             
             if (Menu.equals("Weapons")) {
               menus[5].addItem(tempItem = new ItemStack(Item.getType(), Integer.valueOf(Slot)));
               tempMeta = tempItem.getItemMeta();
               lores.add("Left Click to Buy");
               lores.add("Right Click to Sell");
               lores.add("Price: " + Price + "$");
               lores.add("Sell for: " + SPrice + "$");
               tempMeta.setLore(lores);
               tempItem.setItemMeta(tempMeta);
               lores.clear();
               playr.sendMessage(ChatColor.GREEN + "Successfully added " + Item.getType() + " with price " + Price + " and sellprice " + SPrice + " in the menu " + Menu + ".");
             } else {
               if (Menu.equals("Potions")) {
                 menus[4].addItem(tempItem = new ItemStack(Item.getType(), Integer.valueOf(Slot)));
                 tempMeta = tempItem.getItemMeta();
                 lores.add("Left Click to Buy");
                 lores.add("Right Click to Sell");
                 lores.add("Price: " + Price + "$");
                 lores.add("Sell for: " + SPrice + "$");
                 tempMeta.setLore(lores);
                 tempItem.setItemMeta(tempMeta);
                 lores.clear();
                 playr.sendMessage(ChatColor.GREEN + "Successfully added " + Item.getType() + " with price " + Price + " and sellprice " + SPrice + " in the menu " + Menu + ".");
               } else {
                 if (Menu.equals("Defense")) {
                   menus[1].addItem(tempItem = new ItemStack(Item.getType(), Integer.valueOf(Slot)));
                   tempMeta = tempItem.getItemMeta();
                   lores.add("Left Click to Buy");
                   lores.add("Right Click to Sell");
                   lores.add("Price: " + Price + "$");
                   lores.add("Sell for: " + SPrice + "$");
                   tempMeta.setLore(lores);
                   tempItem.setItemMeta(tempMeta);
                   lores.clear();
                   playr.sendMessage(ChatColor.GREEN + "Successfully added " + Item.getType() + " with price " + Price + " and sellprice " + SPrice + " in the menu " + Menu + ".");
                 } else {
                   if (Menu.equals("Raid")) {
                     menus[6].addItem(tempItem = new ItemStack(Item.getType(), Integer.valueOf(Slot)));
                     tempMeta = tempItem.getItemMeta();
                     lores.add("Left Click to Buy");
                     lores.add("Right Click to Sell");
                     lores.add("Price: " + Price + "$");
                     lores.add("Sell for: " + SPrice + "$");
                     tempMeta.setLore(lores);
                     tempItem.setItemMeta(tempMeta);
                     lores.clear();
                     playr.sendMessage(ChatColor.GREEN + "Successfully added " + Item.getType() + " with price " + Price + " and sellprice " + SPrice + " in the menu " + Menu + ".");
                   } else {
                     if (Menu.equals("Blocks")) {
                       menus[2].addItem(tempItem = new ItemStack(Item.getType(), Integer.valueOf(Slot)));
                       tempMeta = tempItem.getItemMeta();
                       lores.add("Left Click to Buy");
                       lores.add("Right Click to Sell");
                       lores.add("Price: " + Price + "$");
                       lores.add("Sell for: " + SPrice + "$");
                       tempMeta.setLore(lores);
                       tempItem.setItemMeta(tempMeta);
                       lores.clear();
                       playr.sendMessage(ChatColor.GREEN + "Successfully added " + Item.getType() + " with price " + Price + " and sellprice " + SPrice + " in the menu " + Menu + ".");
                     } else {
                       if (Menu.equals("Minerals")) {
                         menus[3].addItem(tempItem = new ItemStack(Item.getType(), Integer.valueOf(Slot)));
                         tempMeta = tempItem.getItemMeta();
                         lores.add("Left Click to Buy");
                         lores.add("Right Click to Sell");
                         lores.add("Price: " + Price + "$");
                         lores.add("Sell for: " + SPrice + "$");
                         tempMeta.setLore(lores);
                         tempItem.setItemMeta(tempMeta);
                         lores.clear();
                         playr.sendMessage(ChatColor.GREEN + "Successfully added " + Item.getType() + " with price " + Price + " and sellprice " + SPrice + " in the menu " + Menu + ".");
                       } else {
                         publicp.sendMessage(ChatColor.RED + "An Error occured while trying to execute this command. Usage: /shopadd (Menu) (Price) (Sellprice) while holding an item in your hand.");
                       }
                     }
                   }
                 }
               }
             }
             return true;
           }
         }
         publicp.sendMessage(ChatColor.RED + "An Error occured while trying to execute this command. Usage: /shopadd (Menu) (Price) (Sellprice) while holding an item in your hand.");
         return false;
       }
       
    @EventHandler
    public void onInventoryClick (InventoryClickEvent event) {
       Player ply = (Player) event.getWhoClicked();
       Inventory currmenu = event.getInventory();
         ItemStack clickitem = event.getCurrentItem();
         if (clickitem.equals(menuKeys[6])) {
           ply.openInventory(menus[1]);
         } else {
           if (clickitem.equals(menuKeys[5])) {
             ply.openInventory(menus[6]);
           } else {
             if (clickitem.equals(menuKeys[1])) {
               ply.openInventory(menus[2]);
             } else {
               if (clickitem.equals(menuKeys[2])) {
                 ply.openInventory(menus[3]);
               } else {
                 if (clickitem.equals(menuKeys[3])) {
                   ply.openInventory(menus[4]);
                 } else {
                   if (clickitem.equals(menuKeys[4])) {
                     ply.openInventory(menus[5]);
                   } else {
                     if (clickitem.equals(menuKeys[0])) {
                       ply.openInventory(menus[0]);
                     }
                   }
                 }
               }
             }
           }
         }
       }
    }
     
  6. @xXDerMCFreakXx

    Code:
    menus[6] = Bukkit.createInventory(null, 54, "Weapons");
    Code:
    if (Menu.equals("Weapons")) {
               menus[5].addItem(tempItem = new ItemStack(Item.getType(), Integer.valueOf(Slot)));
    That looks really weird.

    Also, you should use sender.isOP() or sender.hasPermission(String) somewhere in your onCommand to make sure not every player can mess with the menu's like that.
     
  7. Offline

    The_Spaceman

    should 'menuKeys[0]' be 'menuKeys[3]'?
    and on all the other places where you create the ItemMeta for the items?
    and if not, it is most likely to be better... at least try it

    and what is the variable 'Menu' in like 'if (Menu.equals("Weapons")) {'
     
  8. Offline

    xXDerMCFreakXx

    knokko, thanks for spotting that mistake. Ill try to fix it right now and keep you updated

    The spaceman, it doesnt really matter which item i use as a meta template, it still works anyways. The variable menu in menu equals("weapons") is the first argument of the command, which menu the item should be located in.

    EDIT:

    thanks knokko, that was the problem
     
  9. @The_Spaceman

    Code:
    String Menu = args[0];
    It seems like he often uses the wrong numbers in menus[number] and menuKeys[number]
     
  10. Offline

    xXDerMCFreakXx

    Sorry i bother you guys this often, but im starting to get insane. Ive changed and added alot now, for example the database class. Its a pretty simple class. It just consists out of 5 arrays and a methode that allows another class to get the id for every array by searching for a name. The id works like this: everytime a new item was added to the database, the id gets added by one. This way you can get the index for the array Material, MenuID, Name, Price and Sell price just by 1 number. Adding an item works completely fine, but when i try to pick it up, it throws me a null exception. I debugged both codes multiple times and also tried different methods, but none of the worked. Originally, i used ArrayLists but they threw null errors at me too. I dont mind using 5 arrays with the size of 400, so this shouldnt be too much of a problem. Any help please? (sorry if my code is a bit messy)

    Database:

    Code:
    package com.gmail.monimitev.shopplugin;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Material;
    
    public class Database {
      
       //Initialise all Arrays
    
       public String[] name = new String[400];
       public Integer[] slot = new Integer[400];
       public Float[] price = new Float[400];
       public Float[] sprice = new Float[400];
       public Integer[] menu = new Integer[400];
       public Material[] material = new Material[400];
       public Integer numOfItems;
    
       public void checkChange(boolean changed) {
        
         //If Items existed before the load, get the current number of items (WIP, dont have the config yet)
        
         if (changed == false) {
           numOfItems = 0;
         } else {
    
         }
       }
    
       public void addItem(String Name, Integer Slot, Float Price, Float Sprice, Integer MenuID, Material MaterialofItem) {
        
         // Add Elements to arrays based on parameters
        
         name[numOfItems] = Name;
         slot[numOfItems] = Slot;
         price[numOfItems] = Price;
         sprice[numOfItems] = Sprice;
         menu[numOfItems] = MenuID;
         material[numOfItems] = MaterialofItem;
         numOfItems++;
       }
    
       public Integer getIDbyName(String Name) {
        
         // search for every element in the array name until it finds the searched name, then return the number of searches it needed
         // (this way it can give me the place of the item in the array)
        
         int numOfSearch = 0;
         for (String n : name) {
             if (n.equals(Name)) {
               return numOfSearch;
             } else {
               numOfSearch++;
             }
         }
         return numOfSearch;
       }
    
    }

    Main class:

    Code:
    package com.gmail.monimitev.shopplugin;
    
    import java.io.PrintWriter;
    
    import java.io.StringWriter;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Logger;
    
    import org.apache.commons.lang.WordUtils;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.ClickType;
    import org.bukkit.event.inventory.InventoryAction;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    // There are more imports, i just shortened it because its not important for the forums
    
    enum Menus {
    
       CATEGORIES(0), RAID(1), DEFENSE(2), BLOCKS(3), MINERALS(4), POTIONS(5), WEAPONS(6);
    
       private int menu;
    
       private Menus(int menu) {
         this.menu = menu;
       }
    
       public int getID() {
         return menu;
       }
    
    }
    
    enum MenuKeys {
    
       BACK(0), RAID(1), DEFENSE(2), BLOCKS(3), MINERALS(4), POTIONS(5), WEAPONS(6);
    
       private int menu;
    
       private MenuKeys(int menu) {
         this.menu = menu;
       }
    
       public int getID() {
         return menu;
       }
    
    }
    
    public class Main extends JavaPlugin implements Listener {
    
       Inventory[] menus = new Inventory[7];
       FileConfiguration config;
       ItemStack[] menuKeys = new ItemStack[7];
       Database shopItems = new Database();
       boolean hasChanged = false;
       boolean shopdisable = false;
    
       @Override
       public void onEnable() {
        
         // Im always putting in a try - catch clause so if something bad happens, a message gets displayed in the server
    
         try {
    
           // Checking if items have existed Prior the enabling (WIP)
    
          
           shopItems.checkChange(hasChanged);
          
           // Instantiating Menus
    
           menus[Menus.CATEGORIES.getID()] = Bukkit.createInventory(null, 9, "Categories");
           menus[Menus.RAID.getID()] = Bukkit.createInventory(null, 54, "Raid");
           menus[Menus.DEFENSE.getID()] = Bukkit.createInventory(null, 54, "Defense");
           menus[Menus.BLOCKS.getID()] = Bukkit.createInventory(null, 54, "Blocks");
           menus[Menus.MINERALS.getID()] = Bukkit.createInventory(null, 54, "Minerals");
           menus[Menus.POTIONS.getID()] = Bukkit.createInventory(null, 54, "Potions");
           menus[Menus.WEAPONS.getID()] = Bukkit.createInventory(null, 54, "Weapons");
    
           // Instantiating Main Keys
    
           menuKeys[MenuKeys.BACK.getID()] = new ItemStack(Material.REDSTONE);
           ItemMeta backmet = menuKeys[0].getItemMeta();
           backmet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Back");
           menuKeys[MenuKeys.BACK.getID()].setItemMeta(backmet);
    
           menuKeys[MenuKeys.DEFENSE.getID()] = new ItemStack(Material.IRON_CHESTPLATE);
           ItemMeta defmet = menuKeys[0].getItemMeta();
           defmet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Defense");
           menuKeys[MenuKeys.DEFENSE.getID()].setItemMeta(defmet);
    
           menuKeys[MenuKeys.BLOCKS.getID()] = new ItemStack(Material.COBBLESTONE);
           ItemMeta blomet = menuKeys[0].getItemMeta();
           blomet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Blocks");
           menuKeys[MenuKeys.BLOCKS.getID()].setItemMeta(blomet);
    
           menuKeys[MenuKeys.MINERALS.getID()] = new ItemStack(Material.DIAMOND_BLOCK);
           ItemMeta minmet = menuKeys[0].getItemMeta();
           minmet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Minerals");
           menuKeys[MenuKeys.MINERALS.getID()].setItemMeta(minmet);
    
           menuKeys[MenuKeys.POTIONS.getID()] = new ItemStack(Material.POTION);
           ItemMeta potmet = menuKeys[0].getItemMeta();
           potmet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Potions");
           menuKeys[MenuKeys.POTIONS.getID()].setItemMeta(potmet);
    
           menuKeys[MenuKeys.WEAPONS.getID()] = new ItemStack(Material.DIAMOND_SWORD);
           ItemMeta wepmet = menuKeys[0].getItemMeta();
           wepmet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Weapons");
           menuKeys[MenuKeys.WEAPONS.getID()].setItemMeta(wepmet);
    
           menuKeys[MenuKeys.RAID.getID()] = new ItemStack(Material.TNT);
           ItemMeta raimet = menuKeys[0].getItemMeta();
           raimet.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Raid");
           menuKeys[MenuKeys.RAID.getID()].setItemMeta(raimet);
    
           // Positioning Keys
    
           menus[Menus.CATEGORIES.getID()].setItem(2, menuKeys[MenuKeys.DEFENSE.getID()]);
           menus[Menus.CATEGORIES.getID()].setItem(3, menuKeys[MenuKeys.BLOCKS.getID()]);
           menus[Menus.CATEGORIES.getID()].setItem(4, menuKeys[MenuKeys.MINERALS.getID()]);
           menus[Menus.CATEGORIES.getID()].setItem(5, menuKeys[MenuKeys.RAID.getID()]);
           menus[Menus.CATEGORIES.getID()].setItem(6, menuKeys[MenuKeys.WEAPONS.getID()]);
           menus[Menus.CATEGORIES.getID()].setItem(7, menuKeys[MenuKeys.POTIONS.getID()]);
           menus[Menus.DEFENSE.getID()].setItem(49, menuKeys[MenuKeys.BACK.getID()]);
           menus[Menus.RAID.getID()].setItem(49, menuKeys[MenuKeys.BACK.getID()]);
           menus[Menus.BLOCKS.getID()].setItem(49, menuKeys[MenuKeys.BACK.getID()]);
           menus[Menus.WEAPONS.getID()].setItem(49, menuKeys[MenuKeys.BACK.getID()]);
           menus[Menus.POTIONS.getID()].setItem(49, menuKeys[MenuKeys.BACK.getID()]);
           menus[Menus.MINERALS.getID()].setItem(49, menuKeys[MenuKeys.BACK.getID()]);
          
           // Register events
    
           Bukkit.getServer().getPluginManager().registerEvents(this, this);
          
         } catch (Exception ohNo) {
           StringWriter writer = new StringWriter();
           ohNo.printStackTrace(new PrintWriter(writer, true));
           Bukkit.broadcastMessage(ChatColor.DARK_RED + writer.toString());
           Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD
               + "Error! An Exception was thrown by the plugin 'AtomicShop'. Please report this to the Dev immediately and disable it for now, otherwise corruption may occur.");
           Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "._. - xXDerMCFreakXx");
         }
       }
    
       public void onDisable() {
    
       }
    
       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         try {    
           Player publicp = (Player) sender;
           PlayerInventory publicpinv = publicp.getInventory();
           if (cmd.getName().equals("shop") && shopdisable == false) {
             Player player = (Player) sender;
             player.openInventory(menus[0]);
             return true;
           } else {
             if (publicp.isOp()) {
               try {
               if (cmd.getName().equals("shopadd") && publicpinv.getItemInMainHand() != null
                   && !args[3].isEmpty()) {
                 Player playr = (Player) sender;
                 String Menu = args[0];
                 String Slot = args[1];
                 String Price = args[2];
                 String SPrice = args[3];
                 ItemMeta tempMeta;
                 List<String> lores = new ArrayList<String>();
                 int menuindex = 0;
    
                 switch (Menu) {
    
                 case "Weapons":
                   menuindex = Menus.WEAPONS.getID();
                   break;
                 case "Minerals":
                   menuindex = Menus.MINERALS.getID();
                   break;
                 case "Blocks":
                   menuindex = Menus.BLOCKS.getID();
                   break;
                 case "Raid":
                   menuindex = Menus.RAID.getID();
                   break;
                 case "Potions":
                   menuindex = Menus.POTIONS.getID();
                   break;
                 case "Defense":
                   menuindex = Menus.DEFENSE.getID();
                   break;
                 default:
                   publicp.sendMessage(ChatColor.RED
                       + "An Error occured while trying to execute this command. Usage: /shopadd (Menu) (Slot) (Price) (Sellprice) while holding an item in your hand or /shopremove (Menu) (Slot) or /shopactive (true/false) Available Menus: 'Weapons', 'Blocks', 'Minerals', 'Raid', 'Defense' and 'Potions'");
                   return false;
    
                 }
                
                 // get the held item and assign a custom meta to it
    
                 ItemStack Item = playr.getInventory().getItemInMainHand();
                 tempMeta = Bukkit.getItemFactory().getItemMeta(Material.STONE);
                 String tempName = WordUtils.capitalize(Item.getType().name().replace("_", " ").toLowerCase());
                 tempMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + tempName);
                 lores.add(ChatColor.GRAY + "Left Click to Buy");
                 lores.add(ChatColor.GRAY + "Right Click to Sell");
                 lores.add(ChatColor.GRAY + "Price: " + ChatColor.GREEN + Price + "$");
                 lores.add(ChatColor.GRAY + "Sell for: " + SPrice + "$");
                 tempMeta.setLore(lores);
                 Item.setItemMeta(tempMeta);
                
                 // add the item to the menu and assign it into the database, then delete the item
                
                 menus[menuindex].setItem(Integer.valueOf(Slot), Item);
                 lores.clear();
                 playr.sendMessage(ChatColor.GREEN + "Successfully added " + Item.getType() + " with price "
                     + Price + " and sellprice " + SPrice + " in the menu " + Menu + ".");
                 shopItems.addItem(tempName, Integer.valueOf(Slot), Float.valueOf(Price), Float.valueOf(SPrice), Integer.valueOf(menuindex), Item.getType());
                 playr.getInventory().setItemInMainHand(null);
                 return true;
                
               } else {
                 if (cmd.equals("shopremove") && !args[1].isEmpty()) {
                   String Menu = args[0];
                   String Slot = args[1];
                   menus[Integer.valueOf(Menu)].clear(Integer.valueOf(Slot));
                   ;
                 } else {
                   if (cmd.equals("shopactive") && !args[1].isEmpty()) {
                    
                     // btw, /shopactive false/true doesnt work. any reason why?
                    
                     if (args[0].equals("true")) {
                       shopdisable = false;
                     } else {
                       if (args[0].equals("false")) {
                         shopdisable = true;
                       }
                     }
                   } else {
                   publicp.sendMessage(ChatColor.RED
                       + "An Error occured while trying to execute this command. Usage: /shopadd (Menu) (Slot) (Price) (Sellprice) while holding an item in your hand or /shopremove (Menu) (Slot) or /shopactive (true/false) Available Menus: 'Weapons', 'Blocks', 'Minerals', 'Raid', 'Defense' and 'Potions'");
                   return false;
                   }
                 }
               }
               }
               // here im sending a different message, because the AIOOB exception will only be thrown if the player didnt give enough (or too many) arguments
               catch (java.lang.ArrayIndexOutOfBoundsException e) {
                 publicp.sendMessage(ChatColor.RED
                     + "An Error occured while trying to execute this command. Usage: /shopadd (Menu) (Slot) (Price) (Sellprice) while holding an item in your hand or /shopremove (Menu) (Slot) or /shopactive (true/false) Available Menus: 'Weapons', 'Blocks', 'Minerals', 'Raid', 'Defense' and 'Potions'");
                 return false;
               }
             } else {
               publicp.sendMessage(ChatColor.RED + "Insufficient Permission");
               return false;
             }
           }
         } catch (Exception ohNo) {
           StringWriter writer = new StringWriter();
           ohNo.printStackTrace(new PrintWriter(writer, true));
           Bukkit.broadcastMessage(ChatColor.DARK_RED + writer.toString());
           Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD
               + "Error! An Exception was thrown by the plugin 'AtomicShop'. Please report this to the Dev immediately and disable it for now, otherwise corruption may occur.");
           Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "._. - xXDerMCFreakXx");
         }
         return false;
       }
    
       @SuppressWarnings("deprecation")
       @EventHandler
       public void onInventoryClick(InventoryClickEvent event) {
         ItemStack clickitem;
         Player ply = (Player) event.getWhoClicked();
         Inventory inv;
         if (!event.getCurrentItem().equals(null)) {
           clickitem = event.getCurrentItem();
         } else {
           return;
         }
        
         // if the clicked item is a menukey, open the right inventory
        
         if (clickitem.equals(menuKeys[MenuKeys.BLOCKS.getID()])) {
           ply.openInventory(menus[Menus.BLOCKS.getID()]);
         } else {
           if (clickitem.equals(menuKeys[MenuKeys.WEAPONS.getID()])) {
             ply.openInventory(menus[Menus.WEAPONS.getID()]);
           } else {
             if (clickitem.equals(menuKeys[MenuKeys.POTIONS.getID()])) {
               ply.openInventory(menus[Menus.POTIONS.getID()]);
             } else {
               if (clickitem.equals(menuKeys[MenuKeys.MINERALS.getID()])) {
                 ply.openInventory(menus[Menus.MINERALS.getID()]);
               } else {
                 if (clickitem.equals(menuKeys[MenuKeys.DEFENSE.getID()])) {
                   ply.openInventory(menus[Menus.DEFENSE.getID()]);
                 } else {
                   if (clickitem.equals(menuKeys[MenuKeys.RAID.getID()])) {
                     ply.openInventory(menus[Menus.RAID.getID()]);
                   } else {
                     if (clickitem.equals(menuKeys[MenuKeys.BACK.getID()])) {
                       ply.openInventory(menus[Menus.CATEGORIES.getID()]);
                     } else {
                      
                       //if used inventory is a menu inventory and the item isnt air, do...
                      
                       if (event.getInventory().getTitle().equals("Minerals")
                           || event.getInventory().getTitle().equals("Blocks")
                           || event.getInventory().getTitle().equals("Defense")
                           || event.getInventory().getTitle().equals("Weapons")
                           || event.getInventory().getTitle().equals("Raid")
                           || event.getInventory().getTitle().equals("Potions")) {
                         if (clickitem.getItemMeta() != null) {
                           try {
                             RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
                             Economy econ = economyProvider.getProvider();;
                             ItemMeta tempmeta2 = clickitem.getItemMeta();
                             String tempname2 = "";
                             tempname2 = tempmeta2.getDisplayName();
                             // now it shouldve gotten the name and should now search the id of the item by the name. this is the line that throws the null exception.
                             Integer ItemID = shopItems.getIDbyName(tempname2);
                             List<String> lorecheck = clickitem.getItemMeta().getLore();
                             if (event.getClick().equals(ClickType.RIGHT)
                                 && clickitem.hasItemMeta() && lorecheck.get(0).equals(ChatColor.GRAY + "Left Click to Buy")) {
                               float sprice = shopItems.sprice[ItemID];
                               if (ply.getInventory().contains(shopItems.material[ItemID])) {
                                 ply.getInventory().addItem(
                                     new ItemStack(shopItems.material[ItemID]));
                                 econ.depositPlayer(ply, sprice);
                                 ply.sendMessage(ChatColor.GREEN
                                     + "You have successfully sold and Item for " + sprice
                                     + "$!");
                               } else {
                                 inv = event.getInventory();
                                 ply.closeInventory();
                                 ply.sendMessage(ChatColor.RED
                                     + "You do not have the Item you want to sell!");
                               }
                             } else {
                               if (event.getClick().equals(ClickType.LEFT)
                                 && clickitem.hasItemMeta()) {
                               float price = shopItems.price[ItemID];
                               if (econ.getBalance(ply) >= price) {
                                 econ.withdrawPlayer(ply, price);
                                 ply.getInventory()
                                     .removeItem(new ItemStack(shopItems.material[ItemID],
                                         ply.getInventory().firstEmpty()));
                                 ply.sendMessage(ChatColor.GREEN
                                     + "You have successfully bought an Item for " + price
                                     + "$!");
                               } else {
                                 double missingFunds = econ.getBalance(ply) - price;
                                 ply.closeInventory();
                                 ply.sendMessage(ChatColor.RED
                                     + "You do not have enough Money! You need "
                                     + Math.abs(missingFunds)
                                     + "$ more Money to buy this Item.");
                               }
                             }
                             }
                             lorecheck.clear();
                             //if the player clicked a menu item, bring it back
                             ItemStack tempItem = event.getCurrentItem();
                             event.setCursor(null);
                             event.setCurrentItem(tempItem);
                           } catch (Exception ohNo) {
                             StringWriter writer = new StringWriter();
                             ohNo.printStackTrace(new PrintWriter(writer, true));
                             Bukkit.broadcastMessage(ChatColor.DARK_RED + writer.toString());
                             Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD
                                 + "Error! An Exception was thrown by the plugin 'AtomicShop'. Please report this to the Dev immediately and disable it for now, otherwise corruption may occur.");
                             Bukkit.broadcastMessage(
                                 ChatColor.LIGHT_PURPLE + "._. - xXDerMCFreakXx");
                           }
                         } else {
                           ply.closeInventory();
                           ply.openInventory(event.getInventory());
                           return;
                         }
                       } else {
                         return;
                       }
                     }
                   }
                 }
               }
             }
           }
         }
       }
    }
     
    Last edited by a moderator: Nov 25, 2018
  11. @xXDerMCFreakXx

    First of all, you should avoid using Integer and Float. Use int and float instead.

    The actual problem seems to be that DataBase.getIDByName throws a NullPointerException. I think I know why:
    You are iterating over all names with the line for (String n : name)
    The problem is that this will iterate over all strings in name. name is a String[] with length 400. When you add the first item, name[0] will get a value. When you add the second item, name[1] will get a value. However, name[2] until name[399] will not have a value and thus they will have a value of null.
    Now get back to for (String n : name):
    n will be null for every element in name that doesn't have a value.
    In the next line, namely
    if (n.equals(Name)) {
    you will try to use the equals method of n, but n is null, so you try to use the equals method of null, which throws the NullPointerException.
     
  12. Offline

    xXDerMCFreakXx

    first of all, thank you for replying. Second of all, i have an idea. I will set every value with a placeholder value that should be overwritten as soon as possible. However, and this is why i wasnt including this in the first place, shouldnt for (String n : name) break the operation and return the value as soon as the name was found? i mean it shouldnt even reach name[1] if the value was saved in name[0] and n matches Name, right?
     
  13. @xXDerMCFreakXx

    Now that you mention it, the null values indeed should not be reached if the name that is requested was already added. So, indeed, the method should returns before it comes so far.
    So I was probably wrong. I think you will need to tell us on what line the NullPointerException occured (just copy the line)
     
Thread Status:
Not open for further replies.

Share This Page