Solved Help Checking for a inventory title

Discussion in 'Spigot Plugin Development' started by zackatack108, Jun 5, 2016.

  1. Hello spigot community, I'm having some issues with my plugin when I try to compare the title of a custom inventory that is opened.

    How it's supposed to work

    Inventory A:
    • Can open Inventory B
    Inventory B:
    • Can open Inventory C
    • Has a back option to Inventory A
    Inventory C:
    • Has a back option to Inventory B (I haven't coded this section yet)
    How it's working when I test it:

    Inventory A: Works as intended

    Inventory B: Works as intended

    Inventory C: When I click the back option it goes back to Inventory A (I haven't coded this section so it shouldn't be doing anything)


    Code (Text):
    public class PlayerUI implements Listener{
       
        private fireworkEditor fwEditor = new fireworkEditor();
       
        //Show Editor
        @EventHandler
        public void showEditor(InventoryClickEvent e){
           
            if (e.getInventory().getName().equals("Show Editor"));{
               
                Player player = (Player) e.getWhoClicked();
               
                if(e.getSlot()==0){
                    e.setCancelled(true);
                    e.getWhoClicked().sendMessage("You went back a page");
                }
                else if (e.getSlot()==4){
                    e.setCancelled(true);
                    fwEditor.fwEditorGUI(player);
                }
                else if (e.getSlot()==8){
                    e.setCancelled(true);
                    e.getWhoClicked().sendMessage("You went to the next page");
                }
            }
        }  
    }
    Code (Text):
    public class showEditor {

        public void showEditorGUI(CommandSender sender){
       
            //Previous Page
            ItemStack previousPage = new ItemStack(Material.WOOL, 1, (byte) 14);
            ItemMeta mPrevious = previousPage.getItemMeta();
            mPrevious.setDisplayName("Previous Page");
            previousPage.setItemMeta(mPrevious);
           
            //Create Firework
            ItemStack createFirework = new ItemStack(Material.FIREWORK);
            ItemMeta mFirework = createFirework.getItemMeta();
            mFirework.setDisplayName("Create Firework");
            createFirework.setItemMeta(mFirework);
           
            //Next Page
            ItemStack nextPage = new ItemStack(Material.WOOL, 1, (byte) 13);
            ItemMeta mNext = nextPage.getItemMeta();
            mNext.setDisplayName("Next Page");
            nextPage.setItemMeta(mNext);

            //Show Editor
            Inventory showEditor = Bukkit.createInventory(null, 54, ChatColor.DARK_GREEN + "Show Editor");
            showEditor.setItem(0, previousPage);
            showEditor.setItem(4, createFirework);
            showEditor.setItem(8, nextPage);
           
            ((Player) sender).openInventory(showEditor);
        }
    }
     

    Code (Text):
    public class FireworkUI implements Listener{
       
        private fireworkType type = new fireworkType();
        private showEditor showEdit = new showEditor();

        //Firework Editor
        @EventHandler
        public void fwEditorClick(InventoryClickEvent e){
           
            Inventory inv = e.getInventory();
               
            if(inv.getName().equalsIgnoreCase("Firework Editor"));
               
            Player player = (Player) e.getWhoClicked();
               
            if(e.getSlot()==10){
                e.setCancelled(true);
            }
            else if(e.getSlot()==11){
                e.setCancelled(true);
            }
            else if(e.getSlot()==12){
                e.setCancelled(true);
                type.fwType(player);
            }
            else if(e.getSlot()==13){
                e.setCancelled(true);
            }
            else if(e.getSlot()==14){
                e.setCancelled(true);
            }
            else if(e.getSlot()==15){
                e.setCancelled(true);
            }
            else if(e.getSlot()==16){
                e.setCancelled(true);
            }
            else if(e.getSlot()==22){
                e.setCancelled(true);
                showEdit.showEditorGUI(player);;
            }
        }
    }
     
    Code (Text):
    public class fireworkEditor {

        public void fwEditorGUI(Player player) {
           
            //Time
            ItemStack time = new ItemStack(Material.WATCH);
            ItemMeta mtime = time.getItemMeta();
            ArrayList<String> timelore = new ArrayList<String>();
            timelore.add(ChatColor.YELLOW + "Select the time in seconds the firework");
            timelore.add(ChatColor.YELLOW + "will launch from the beginning");
            mtime.setLore(timelore);
            mtime.setDisplayName(ChatColor.AQUA + "Time");
            time.setItemMeta(mtime);
           
            //Firework type
            ItemStack fwType = new ItemStack(Material.FIREWORK_CHARGE);
            ItemMeta mfwType = fwType.getItemMeta();
            mfwType.setDisplayName(ChatColor.AQUA + "Firework Type");
            ArrayList<String> typeLore = new ArrayList<String>();
            typeLore.add(ChatColor.YELLOW + "Select Firework Type");
            mfwType.setLore(typeLore);
            fwType.setItemMeta(mfwType);
           
            //Main Color
            ItemStack color = new ItemStack(Material.WOOL, 1, (byte) 15);
            ItemMeta mcolor = color.getItemMeta();
            mcolor.setDisplayName(ChatColor.AQUA + "Choose Base Color");
            color.setItemMeta(mcolor);
           
            //Fade Color
            ItemStack fadeColor = new ItemStack(Material.WOOL);
            ItemMeta mfColor = color.getItemMeta();
            mfColor.setDisplayName(ChatColor.AQUA + "Choose Fade Color");
            fadeColor.setItemMeta(mfColor);
           
           
            //Effect
            ItemStack effect = new ItemStack(Material.BLAZE_POWDER);
            ItemMeta meffect = color.getItemMeta();
            meffect.setDisplayName(ChatColor.AQUA + "Choose Firework Effect");
            effect.setItemMeta(meffect);
           
            //Delete
            ItemStack delete = new ItemStack(Material.TNT);
            ItemMeta mdelete = color.getItemMeta();
            mdelete.setDisplayName(ChatColor.AQUA + "Delete Firework");
            delete.setItemMeta(mdelete);
           
            //Back
            ItemStack back = new ItemStack(Material.ARROW);
            ItemMeta mback = color.getItemMeta();
            mback.setDisplayName(ChatColor.AQUA + "Back");
            back.setItemMeta(mback);
           
            //Direction
            ItemStack direction = new ItemStack(Material.COMPASS);
            ItemMeta mdirection = direction.getItemMeta();
            mdirection.setDisplayName(ChatColor.AQUA + "Choose Direction");
            direction.setItemMeta(mdirection);
           
            //Open firework editor type
            Inventory fwEditor = Bukkit.createInventory(null, 27, "Firework Editor");
           
            fwEditor.setItem(10, delete);
            fwEditor.setItem(11, time);
            fwEditor.setItem(12, fwType);
            fwEditor.setItem(13, color);
            fwEditor.setItem(14, fadeColor);
            fwEditor.setItem(15, effect);
            fwEditor.setItem(16, direction);
            fwEditor.setItem(22, back);
           
            player.openInventory(fwEditor);
           
        }
    }
     

    Code (Text):
    public class fireworkType {

        public void fwType(Player player){
           
            //Small Ball
            ItemStack smallBall = new ItemStack(Material.CLAY_BALL);
            ItemMeta mSmall = smallBall.getItemMeta();
            mSmall.setDisplayName(ChatColor.AQUA + "Small Ball");
            smallBall.setItemMeta(mSmall);
                           
            //Large Ball
            ItemStack largeBall = new ItemStack(Material.SNOW_BALL);
            ItemMeta mLarge = largeBall.getItemMeta();
            mLarge.setDisplayName(ChatColor.AQUA + "Large Ball");
            largeBall.setItemMeta(mLarge);
                           
            //Burst
            ItemStack burst = new ItemStack(Material.BLAZE_POWDER);
            ItemMeta mBurst = burst.getItemMeta();
            mBurst.setDisplayName(ChatColor.AQUA + "Burst");
            burst.setItemMeta(mBurst);
                           
            //Creeper
            ItemStack creeper = new ItemStack(Material.SKULL_ITEM, 1 , (byte) 4);
            ItemMeta mcreeper = creeper.getItemMeta();
            mcreeper.setDisplayName(ChatColor.AQUA + "Creeper");
            creeper.setItemMeta(mcreeper);
                           
            //Star
            ItemStack star = new ItemStack(Material.NETHER_STAR);
            ItemMeta mstar = star.getItemMeta();
            mstar.setDisplayName(ChatColor.AQUA + "Star");
            star.setItemMeta(mstar);
           
            //Back
            ItemStack back = new ItemStack(Material.ARROW);
            ItemMeta mback = back.getItemMeta();
            mback.setDisplayName(ChatColor.AQUA + "Back");
            back.setItemMeta(mback);
           
            //open firework type inventory
            Inventory fwType = Bukkit.createInventory(null, 27, ChatColor.DARK_GREEN + "Firework Type");
            fwType.setItem(11, smallBall);
            fwType.setItem(12, largeBall);
            fwType.setItem(13, burst);
            fwType.setItem(14, creeper);
            fwType.setItem(15, star);
            fwType.setItem(22, back);
           
            player.openInventory(fwType);
        }
    }
     

    So I'm pretty sure the issue I'm having is that the If statement that is looking for the inventory title isn't getting the title correctly. I've tried google for a solution but nothing has popped up. If anyone is able to help that would be great.
     
  2. Sw4p

    Supporter

    I dont know if this might be causing it but try changing
    Code (Text):
    Inventory showEditor = Bukkit.createInventory(null, 54, ChatColor.DARK_GREEN + "Show Editor");
    to
    Code (Text):
    Inventory showEditor = Bukkit.createInventory(null, 54,"Show Editor");
    And see if the color codes might be causing something.
     
    • Optimistic Optimistic x 1
  3. So I removed all of the color codes from the inventories but it doesn't seem to the color codes. Inventory C is still able to go to Inventory A which once again shouldn't happen.
     
  4. Sw4p

    Supporter

    I do not see your event handler for inventory C, am i just overlooking it?
     
  5. I haven't coded the event handle for Inventory C So basicly when I click the back option in Inventory C nothing should happen. What is happening is Inventory C is going back to Inventory A
     
  6. Sw4p

    Supporter

    Ok well create that handler and redirect it to the inventory you would like. I believe that will fix your issue.
     
  7. Okay it seems to be working now that I added the handler. I do have a question though. Why didn't it not do anything when I didn't have the handler. Why did it act like the other inventory that had a different name?
     
  8. Okay now I have another issue Inventory B isn't letting me go back to Inventory A now. I can only jump between Inventory B and C with the back options
     
  9. Sw4p

    Supporter

    Honestly i have no clue.

    It sounds like the server be be confused on which inventory is open to the player possibly? Can you try closing the inventory and then opening the next one. so
    Code (Text):
    Player.closeInv
    player.openInv(nextinv)
    maybe something like that might work smother.
     
  10. Okay so I tried closing and opening the new inventory but the issue still exist.
     
  11. Sw4p

    Supporter

    will you post your updated code? Preferably under spoilers like you did above :D
     
  12. Code A is still the same

    Code (Text):
    public class fireworkEditor {

        public void fwEditorGUI(Player player) {
           
            //Time
            ItemStack time = new ItemStack(Material.WATCH);
            ItemMeta mtime = time.getItemMeta();
            ArrayList<String> timelore = new ArrayList<String>();
            timelore.add(ChatColor.YELLOW + "Select the time in seconds the firework");
            timelore.add(ChatColor.YELLOW + "will launch from the beginning");
            mtime.setLore(timelore);
            mtime.setDisplayName(ChatColor.AQUA + "Time");
            time.setItemMeta(mtime);
           
            //Firework type
            ItemStack fwType = new ItemStack(Material.FIREWORK_CHARGE);
            ItemMeta mfwType = fwType.getItemMeta();
            mfwType.setDisplayName(ChatColor.AQUA + "Firework Type");
            ArrayList<String> typeLore = new ArrayList<String>();
            typeLore.add(ChatColor.YELLOW + "Select Firework Type");
            mfwType.setLore(typeLore);
            fwType.setItemMeta(mfwType);
           
            //Main Color
            ItemStack color = new ItemStack(Material.WOOL, 1, (byte) 15);
            ItemMeta mcolor = color.getItemMeta();
            mcolor.setDisplayName(ChatColor.AQUA + "Choose Base Color");
            color.setItemMeta(mcolor);
           
            //Fade Color
            ItemStack fadeColor = new ItemStack(Material.WOOL);
            ItemMeta mfColor = color.getItemMeta();
            mfColor.setDisplayName(ChatColor.AQUA + "Choose Fade Color");
            fadeColor.setItemMeta(mfColor);
           
           
            //Effect
            ItemStack effect = new ItemStack(Material.BLAZE_POWDER);
            ItemMeta meffect = color.getItemMeta();
            meffect.setDisplayName(ChatColor.AQUA + "Choose Firework Effect");
            effect.setItemMeta(meffect);
           
            //Delete
            ItemStack delete = new ItemStack(Material.TNT);
            ItemMeta mdelete = color.getItemMeta();
            mdelete.setDisplayName(ChatColor.AQUA + "Delete Firework");
            delete.setItemMeta(mdelete);
           
            //Back
            ItemStack back = new ItemStack(Material.ARROW);
            ItemMeta mback = color.getItemMeta();
            mback.setDisplayName(ChatColor.AQUA + "Back");
            back.setItemMeta(mback);
           
            //Direction
            ItemStack direction = new ItemStack(Material.COMPASS);
            ItemMeta mdirection = direction.getItemMeta();
            mdirection.setDisplayName(ChatColor.AQUA + "Choose Direction");
            direction.setItemMeta(mdirection);
           
            //Open firework editor type
            Inventory fwEditor = Bukkit.createInventory(null, 27, "Firework Editor");
           
            fwEditor.setItem(10, delete);
            fwEditor.setItem(11, time);
            fwEditor.setItem(12, fwType);
            fwEditor.setItem(13, color);
            fwEditor.setItem(14, fadeColor);
            fwEditor.setItem(15, effect);
            fwEditor.setItem(16, direction);
            fwEditor.setItem(22, back);
           
            player.closeInventory();
            player.openInventory(fwEditor);
           
        }  
    }
    Code (Text):
    public class FireworkUI implements Listener{
       
        private fireworkType type = new fireworkType();
        private showEditor showEdit = new showEditor();

        //Firework Editor
        @EventHandler
        public void fwEditorClick(InventoryClickEvent e){
           
            Inventory inv = e.getInventory();
               
            if(inv.getName().equalsIgnoreCase("Firework Editor"));
               
            Player player = (Player) e.getWhoClicked();
               
            if(e.getSlot()==10){
                e.setCancelled(true);
            }
            else if(e.getSlot()==11){
                e.setCancelled(true);
            }
            else if(e.getSlot()==12){
                e.setCancelled(true);
                type.fwType(player);
            }
            else if(e.getSlot()==13){
                e.setCancelled(true);
            }
            else if(e.getSlot()==14){
                e.setCancelled(true);
            }
            else if(e.getSlot()==15){
                e.setCancelled(true);
            }
            else if(e.getSlot()==16){
                e.setCancelled(true);
            }
            else if(e.getSlot()==22){
                e.setCancelled(true);
                showEdit.showEditorGUI(player);;
            }
        }
    }

    Code (Text):
    public class fireworkType {

        public void fwType(Player player){
           
            //Small Ball
            ItemStack smallBall = new ItemStack(Material.CLAY_BALL);
            ItemMeta mSmall = smallBall.getItemMeta();
            mSmall.setDisplayName(ChatColor.AQUA + "Small Ball");
            smallBall.setItemMeta(mSmall);
                           
            //Large Ball
            ItemStack largeBall = new ItemStack(Material.SNOW_BALL);
            ItemMeta mLarge = largeBall.getItemMeta();
            mLarge.setDisplayName(ChatColor.AQUA + "Large Ball");
            largeBall.setItemMeta(mLarge);
                           
            //Burst
            ItemStack burst = new ItemStack(Material.BLAZE_POWDER);
            ItemMeta mBurst = burst.getItemMeta();
            mBurst.setDisplayName(ChatColor.AQUA + "Burst");
            burst.setItemMeta(mBurst);
                           
            //Creeper
            ItemStack creeper = new ItemStack(Material.SKULL_ITEM, 1 , (byte) 4);
            ItemMeta mcreeper = creeper.getItemMeta();
            mcreeper.setDisplayName(ChatColor.AQUA + "Creeper");
            creeper.setItemMeta(mcreeper);
                           
            //Star
            ItemStack star = new ItemStack(Material.NETHER_STAR);
            ItemMeta mstar = star.getItemMeta();
            mstar.setDisplayName(ChatColor.AQUA + "Star");
            star.setItemMeta(mstar);
           
            //Back
            ItemStack back = new ItemStack(Material.ARROW);
            ItemMeta mback = back.getItemMeta();
            mback.setDisplayName(ChatColor.AQUA + "Back");
            back.setItemMeta(mback);
           
            //open firework type inventory
            Inventory fwType = Bukkit.createInventory(null, 27, "Firework Type");
            fwType.setItem(11, smallBall);
            fwType.setItem(12, largeBall);
            fwType.setItem(13, burst);
            fwType.setItem(14, creeper);
            fwType.setItem(15, star);
            fwType.setItem(22, back);
           
            player.closeInventory();
            player.openInventory(fwType);
        }
    }
    Code (Text):
    public class fwTypeUI implements Listener {
       
        private fireworkEditor editor = new fireworkEditor();

        //Firework Editor
        @EventHandler
        public void fwEditorClick(InventoryClickEvent e){
           
            Inventory inv = e.getInventory();
               
            if(inv.getName().equalsIgnoreCase("Show Editor"));
               
            Player player = (Player) e.getWhoClicked();
               
            if(e.getSlot()==11){
                e.setCancelled(true);
            }
            else if(e.getSlot()==12){
                e.setCancelled(true);
            }
            else if(e.getSlot()==13){
                e.setCancelled(true);
            }
            else if(e.getSlot()==14){
                e.setCancelled(true);
            }
            else if(e.getSlot()==15){
                e.setCancelled(true);
            }
            else if(e.getSlot()==22){
                e.setCancelled(true);
                editor.fwEditorGUI(player);
            }
        }

    }
     
  13. Sw4p

    Supporter

    First the Firework editor handler is looking for "Show Editor" should be your firework type inventory. and have you heard about Switch structures? It might clean up all those if/else's.

    Example:
    Code (Text):
    switch(e.getSlot()){
    case 11:
    case 12:
       <<Code here for slot 11 and 12>>
       break;
    }
     
  14. Okay I found out what the problem was I had an if statement that that didn't have the {} so it wasn't working as intended. Also I am working on changing the if else statements to the switch and case.
     
  15. Sw4p

    Supporter

    Glad you found your issue and the switch's will slim it down! If you have any more questions you know where to find me.