Event Problem

Discussion in 'Plugin Development' started by pigeontroll, Mar 16, 2014.

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

    pigeontroll

    So I have a problem with my Bukkit coding. I have a plugin where, if you click a compass, it will open up an inventory with wools that represent gamemodes, and if you click it, it will set your gamemode. Now, if I click anything in the quickbar/hotbar, it will be cancelled. However, I can use the number trick and move it into the inventory. For example, if I have a Compass in slot 1 (aka slot 0) I can put my cursor over another slot and press "1" and it will be moved there. Is there any way I can prevent that? Here's the code:
    Code:java
    1. package me.pigeontroll.gminv;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.DyeColor;
    8. import org.bukkit.GameMode;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.inventory.InventoryClickEvent;
    13. import org.bukkit.event.inventory.InventoryType.SlotType;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.material.Wool;
    18. import org.bukkit.plugin.Plugin;
    19.  
    20. public class GMInv implements Listener {
    21.  
    22. private Inventory inv;
    23. private ItemStack c, s, a;
    24.  
    25. public GMInv(Plugin p) {
    26. inv = Bukkit.getServer().createInventory(null, 9, "Gamemode Chooser");
    27.  
    28. c = createItem(DyeColor.GREEN, ChatColor.GREEN+""+ChatColor.BOLD+"Creative");
    29. s = createItem(DyeColor.YELLOW, ChatColor.YELLOW+""+ChatColor.BOLD+"Survival");
    30. a = createItem(DyeColor.RED, ChatColor.RED+""+ChatColor.BOLD+"Adventure");
    31.  
    32. inv.setItem(2, s);
    33. inv.setItem(4, c);
    34. inv.setItem(6, a);
    35.  
    36. Bukkit.getServer().getPluginManager().registerEvents(this, p);
    37. }
    38.  
    39. private ItemStack createItem(DyeColor dc, String name) {
    40. ItemStack i = new Wool(dc).toItemStack(1);
    41. ItemMeta im = i.getItemMeta();
    42. im.setDisplayName(name);
    43. im.setLore(Arrays.asList("Set your gamemode", "to " + name));
    44. i.setItemMeta(im);
    45. return i;
    46. }
    47.  
    48. public void show(Player p) {
    49. p.openInventory(inv);
    50. }
    51.  
    52. @EventHandler
    53. public void onInventoryClick(InventoryClickEvent e) {
    54. if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    55. if (e.getSlotType() == SlotType.OUTSIDE) return;
    56. if (e.getCurrentItem().getItemMeta() == null) return;
    57. if (e.getSlotType() == SlotType.QUICKBAR) {
    58. e.setCancelled(true);
    59. return;
    60. }
    61. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.stripColor("Creative"))) {
    62. e.setCancelled(true);
    63. e.getWhoClicked().closeInventory();
    64. e.getWhoClicked().setGameMode(GameMode.CREATIVE);
    65. }
    66. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.stripColor("Survival"))) {
    67. e.setCancelled(true);
    68. e.getWhoClicked().closeInventory();
    69. e.getWhoClicked().setGameMode(GameMode.SURVIVAL);
    70. }
    71. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.stripColor("Adventure"))) {
    72. e.setCancelled(true);
    73. e.getWhoClicked().closeInventory();
    74. e.getWhoClicked().setGameMode(GameMode.ADVENTURE);
    75. }
    76. else e.setCancelled(true);
    77.  
    78. }
    79. }

    PS When I try to click it after I move it, I get a stack-trace error.
     
  2. Offline

    amhokies

    You can't cast ItemMeta to ItemStack. If you simply want an ItemStack, just use e.getCurrentItem(), which returns an ItemStack instance.
     
  3. Offline

    pigeontroll

    amhokies Thanks! It works! Now I have another problem though D:
    I changed the title post.
     
  4. Offline

    FuZioN720

    pigeontroll what is the event error? can you post your console?
     
  5. Offline

    pigeontroll

    FuZioN720 I'm not sure, I already quit the server (As in close and restart computer), and I don't have the log. Do you think you can find the problem from here?
     
Thread Status:
Not open for further replies.

Share This Page