GUI Items Help

Discussion in 'Plugin Development' started by KrypticIce, Jan 19, 2015.

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

    KrypticIce

    Ive got a bug with my plugin i just cant fix the GUI works but when i click the things inside it dont spawn anything the 3 drinks are ment to speak 3 different potions but i cant get it to work any help? http://pastebin.com/hJWibF0B
     
  2. Offline

    Zandor300

    Change line 36 into:
    Code:
    public HashMap<Player, Entity> Menu = new HashMap<Player, Entity>();
    You can also just remove this line because it is never used...

    Never use Player in a HashMap but use the player name. This causes memory leaks.

    Change line 38 into:
    Code:
    public static void setItems() {
    Never use §5 for colors but use ChatColor.COLOR.
     
    Last edited by a moderator: Jan 19, 2015
  3. Offline

    KrypticIce

    Should it spawn items after this?

    Ok it didnt work i did it and it removed the 3 items???
     
  4. Offline

    Zandor300

    Just try
     
  5. Offline

    KrypticIce

    I already did when i changed it the code removed the items from the GUI they where there before i changed it.
     
  6. Offline

    Zandor300

    You need to call the setItems() method in your onEnable.
     
  7. Offline

    KrypticIce

    Then what it says create a method what do i put in the method? :(
     
  8. Offline

    Zandor300

    The method is already made if you've changed what i said.
     
  9. Offline

    KrypticIce

    It still wont give the items though the potions have returned but when you click them it does not give you any of the items set? this is the bit where i set the items.
    Code:
    case POTION:
         player.getInventory().setItem(1, new ItemStack(Material.POTION, 1, (byte)3 ));
            player.getInventory().setItem(1, new ItemStack(Material.POTION, 1, (byte)9 ));
            player.getInventory().setItem(1, new ItemStack(Material.POTION, 1, (byte)5 ));
            player.closeInventory();
            break;
    Code:
    package me.jake2967.Drinks;
    
    import java.util.HashMap;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    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.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class DrinksGUI extends JavaPlugin implements Listener
    {
         public void onEnable() {
             Bukkit.getServer().getLogger().info("EquinoxNetwork Drinks Enabled!");
             Bukkit.getServer().getPluginManager().registerEvents(this, this);
             setItems();
    }
    
    private void setItems() {
            // TODO Auto-generated method stub
         
        }
    
    public void onDisable() {
             Bukkit.getServer().getLogger().info("EquinoxNetwork Drinks Disabled!");
    }
             public static void setItems1() {
             }
    public static Inventory GUI = Bukkit.createInventory(null, 9, "§4§nDrinks");
    @SuppressWarnings({ })
    public HashMap<Player, Entity> Menu = new HashMap<Player, Entity>();
    
    static
    {
    
        ItemStack Coke = new ItemStack(Material.POTION, 1, (byte)2 );
        ItemMeta Cokemeta = Coke.getItemMeta();
        Cokemeta.setDisplayName(ChatColor.RED + "Cola");
        Coke.setItemMeta(Cokemeta);
        GUI.setItem(3, Coke);
     
        ItemStack Lemonade = new ItemStack(Material.POTION, 1, (byte)5 );
        ItemMeta Lemonademeta = Lemonade.getItemMeta();
        Lemonademeta.setDisplayName(ChatColor.YELLOW + "Lemonade");
        Lemonade.setItemMeta(Lemonademeta);
        GUI.setItem(4, Lemonade);
     
        ItemStack Vodka = new ItemStack(Material.POTION, 1, (byte)9 );
        ItemMeta Vodkameta = Vodka.getItemMeta();
        Vodkameta.setDisplayName(ChatColor.AQUA + "Vodka");
        Vodka.setItemMeta(Vodkameta);
        GUI.setItem(5, Vodka);
     
    
      }
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        ItemStack chest = new ItemStack(Material.CHEST, 1);
        ItemMeta chestmeta = chest.getItemMeta();
        chestmeta.setDisplayName(ChatColor.GOLD + "Drink selector");
        chest.setItemMeta(chestmeta);
        event.getPlayer().getInventory().addItem(new ItemStack(chest));
    
      }
    @EventHandler
    public void OnInventoryClick(InventoryClickEvent event) {
        if (!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Drinks"))
                  return;
        Player player = (Player) event.getWhoClicked();
        event.setCancelled(true);
     
        if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR || event.getCurrentItem().hasItemMeta()){
            player.closeInventory();
            return;
        }
        switch(event.getCurrentItem().getType()) {
    case POTION:
         player.getInventory().setItem(1, new ItemStack(Material.POTION, 1, (byte)3 ));
            player.getInventory().setItem(1, new ItemStack(Material.POTION, 1, (byte)9 ));
            player.getInventory().setItem(1, new ItemStack(Material.POTION, 1, (byte)5 ));
            player.closeInventory();
            break;
         
                default:
                    player.closeInventory();
                    break;
        }
    }
    
    
    
    @SuppressWarnings("unused")
    private static void setItem(int i, ItemStack lemonade) {
        // TODO Auto-generated method stub
     
    }
    
    
    @EventHandler
    public void onPlayerRespawn(PlayerRespawnEvent event)
    {
        ItemStack chest = new ItemStack(Material.CHEST, 1);
        ItemMeta chestmeta = chest.getItemMeta();
        chestmeta.setDisplayName(ChatColor.GOLD + "Drink selector");
        chest.setItemMeta(chestmeta);
        event.getPlayer().getInventory().addItem(new ItemStack(chest));
       
    }
    
          @EventHandler
          public void onPlayerInteract(PlayerInteractEvent e)
          {
           Action a = e.getAction();
           ItemStack is = e.getItem();
        
              if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
              return;
           
              if(is.getType() == Material.CHEST)
                  e.getPlayer().openInventory(GUI);
          }
    }
    
    
       
    
    
    
    
    
         
    
     
  10. Offline

    Zandor300

    ChipDev likes this.
  11. Offline

    KrypticIce

    Ok still doesnt give the player who clicks the Potion in the GUI the potion tho xD
     
  12. Offline

    Fuzzybear04

    What exactly is bad about using '§' signs for colours?

    You don't need to specify if the name is "Drinks", just use "inventoryName.getName()"
     
  13. Offline

    glasseater

    @Fuzzybear04 It's bad practice to use those when Bukkit gives you ChatColor.COLOR
     
  14. Offline

    Fuzzybear04

  15. Offline

    KrypticIce

    I use both i just need help fixing my error?
     
  16. Why never use § color codes!? Its working the same as the much longer ChatColor.COLOR
     
    KrypticIce likes this.
  17. Offline

    KrypticIce

    Ikr xD § works with ChatColor its easier to just type § When coding a big gui plugin sort of xD
     
  18. @FisheyLP @KrypticIce This has been discussed and debated on many threads. Long story short, if they changed the § then all the plugins would break apart from the ones using ChatColor.
     
  19. Offline

    xTigerRebornx

    @KrypticIce

    Code:
    if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR || event.getCurrentItem().hasItemMeta()){
            player.closeInventory();
            return;
        }
    Look at this code. You close the Inventory and return if the ItemStack has meta, making it never even reach that switch. Learn to debug your code. Simple debug along each step would instantly reveal where it gets stuck.

    @FisheyLP @Fuzzybear04
    What happens if Mojang decides to phase out the magic values for color codes (like they've done with numerical IDs)?
    Your code breaks, meaning compatibility is broken. However, chances are, whoever is implementing the Bukkit API for that version will have ChatColor fully supporting the new values. Its a bad practice simply because you are using hardcoded magic values when a simply better option is there in front of you.

    As for length, static imports are a thing. eliminates the lengthiness of ChatColor.COLOR.

    Edit: Ninja'd by @bwfcwalshy

    Edit 2: Readability is a factor too. "ChatColor.RED" is a clear representation of what is being used, "&c" is not (& used in place of the other symbol because I don't feel like c/p-ing)
     
  20. @xTigerRebornx
    I always use § instead of ChatColor.COLOR because:
    "MyString" + ChatColor.COLOR + "." <- ANNOYING
    "MyString§1." <- BEAUTIFUL

    :p
     
  21. Offline

    xTigerRebornx

    @FisheyLP As I said, static imports are a thing, and they are quite useful for this. Turns
    Code:
    String string = ChatColor.RED + "Red Text";
    into

    Code:
    String string = RED + "Red Text";
     
  22. Offline

    KrypticIce

    Anyone got any idea how to make &3 etc compatible with configs?
     
  23. Offline

    timtower Administrator Administrator Moderator

    @KrypticIce ChatColor.translateAlternateColorCode('&',plain string here);
     
    KrypticIce likes this.
  24. Offline

    Fuzzybear04

  25. Offline

    KrypticIce

    Thanks tim.
     
Thread Status:
Not open for further replies.

Share This Page