Item not going away

Discussion in 'Plugin Development' started by PizzaPixel, Mar 25, 2014.

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

    PizzaPixel

    Im trying to do when an item is clicked in the inventory the item you have in hand dissapears heres my code
    Code:java
    1. package me.firstminigame;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.inventory.InventoryClickEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.inventory.Inventory;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.plugin.Plugin;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16. import org.bukkit.scoreboard.Scoreboard;
    17. import org.bukkit.scoreboard.ScoreboardManager;
    18. import org.bukkit.scoreboard.Team;
    19.  
    20. public class CTO extends JavaPlugin implements Listener {
    21. public static CTO plugin;
    22.  
    23.  
    24. ScoreboardManager sbManager;
    25. Scoreboard sBoard;
    26.  
    27. Team Red;
    28. Team Blue;
    29. Team Yellow;
    30. Team Green;
    31.  
    32. public void onEnable() {
    33. getServer().getPluginManager().registerEvents(this, this);
    34.  
    35.  
    36. sbManager = Bukkit.getScoreboardManager();
    37. sBoard = sbManager.getNewScoreboard();
    38.  
    39. Red = sBoard.registerNewTeam("Red");
    40. Blue = sBoard.registerNewTeam("Blue");
    41. Yellow = sBoard.registerNewTeam("Yellow");
    42. Green = sBoard.registerNewTeam("Green");
    43.  
    44. }
    45.  
    46. public static Inventory myInventory = Bukkit.createInventory(null, 9, "Choose your team");
    47.  
    48.  
    49.  
    50.  
    51. static {
    52. myInventory.setItem(0, new ItemStack(Material.REDSTONE_BLOCK, 1));
    53. myInventory.setItem(1, new ItemStack(Material.LAPIS_BLOCK, 1));
    54. myInventory.setItem(2, new ItemStack (Material.GOLD_BLOCK, 1));
    55. myInventory.setItem(3, new ItemStack (Material.EMERALD_BLOCK, 1));
    56. }
    57.  
    58.  
    59.  
    60. @EventHandler
    61. public void onPlayerInteract(PlayerInteractEvent event) {
    62. Player player = event.getPlayer();
    63. Material mat = player.getItemInHand().getType();
    64. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    65. if(mat == Material.IRON_BLOCK){ player.openInventory(myInventory);
    66.  
    67. }
    68. }
    69. }
    70.  
    71. @EventHandler
    72. public void onPlayerInteract1(PlayerInteractEvent event) {
    73. Player player = event.getPlayer();
    74. Material mat = player.getItemInHand().getType();
    75. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    76. if(mat == Material.REDSTONE_BLOCK){ player.openInventory(myInventory);
    77.  
    78. }
    79. }
    80. }
    81.  
    82. @EventHandler
    83. public void onPlayerInteract2(PlayerInteractEvent event) {
    84. Player player = event.getPlayer();
    85. Material mat = player.getItemInHand().getType();
    86. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    87. if(mat == Material.LAPIS_BLOCK){ player.openInventory(myInventory);
    88.  
    89. }
    90. }
    91. }
    92.  
    93. @EventHandler
    94. public void onPlayerInteract3(PlayerInteractEvent event) {
    95. Player player = event.getPlayer();
    96. Material mat = player.getItemInHand().getType();
    97. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    98. if(mat == Material.GOLD_BLOCK){ player.openInventory(myInventory);
    99.  
    100. }
    101. }
    102. }
    103.  
    104. @EventHandler
    105. public void onPlayerInteract4(PlayerInteractEvent event) {
    106. Player player = event.getPlayer();
    107. Material mat = player.getItemInHand().getType();
    108. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    109. if(mat == Material.EMERALD_BLOCK){ player.openInventory(myInventory);
    110.  
    111. }
    112. }
    113. }
    114.  
    115. @SuppressWarnings("deprecation")
    116. @EventHandler
    117. public void onInventoryClick(InventoryClickEvent event) {
    118. final Player player = (Player) event.getWhoClicked();
    119. ItemStack clicked = event.getCurrentItem();
    120. Inventory inventory = event.getInventory();
    121. if (inventory.getName().equals(myInventory.getName())) {
    122. if (clicked.getType() == Material.REDSTONE_BLOCK) {
    123.  
    124. event.setCancelled(true);
    125. player.closeInventory();
    126. Red.addPlayer(player);
    127. player.sendMessage("You are now in the Red Team");
    128. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CTO.getInstance(), new Runnable() {
    129. public void run() {
    130. player.setItemInHand(null);
    131. }
    132. }, 3);
    133. player.updateInventory();
    134. }
    135. }
    136. }
    137.  
    138.  
    139. @SuppressWarnings("deprecation")
    140. @EventHandler
    141. public void onInventoryClick1(InventoryClickEvent event) {
    142. final Player player = (Player) event.getWhoClicked();
    143. ItemStack clicked = event.getCurrentItem();
    144. Inventory inventory = event.getInventory();
    145. if (inventory.getName().equals(myInventory.getName())) {
    146. if (clicked.getType() == Material.LAPIS_BLOCK) {
    147. event.setCancelled(true);
    148. player.closeInventory();
    149. Blue.addPlayer(player);
    150. player.sendMessage("You are now in the Blue Team");
    151. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CTO.getInstance(), new Runnable() {
    152. public void run() {
    153. player.setItemInHand(null);
    154. }
    155. }, 3);
    156. player.updateInventory();
    157. }
    158. }
    159. }
    160. private static Plugin getInstance() {
    161. // TODO Auto-generated method stub
    162. return null;
    163. }
    164.  
    165. @SuppressWarnings("deprecation")
    166. @EventHandler
    167. public void onInventoryClick2(InventoryClickEvent event) {
    168. final Player player = (Player) event.getWhoClicked();
    169. ItemStack clicked = event.getCurrentItem();
    170. Inventory inventory = event.getInventory();
    171. if (inventory.getName().equals(myInventory.getName())) {
    172. if (clicked.getType() == Material.GOLD_BLOCK) {
    173. event.setCancelled(true);
    174. player.closeInventory();
    175. Yellow.addPlayer(player);
    176. player.sendMessage("You are now in the Yellow Team");
    177. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CTO.getInstance(), new Runnable() {
    178. public void run() {
    179. player.setItemInHand(null);
    180. }
    181. }, 3);
    182. player.updateInventory();
    183. }
    184. }
    185. }
    186.  
    187. @SuppressWarnings("deprecation")
    188. @EventHandler
    189. public void onInventoryClick3(InventoryClickEvent event) {
    190. final Player player = (Player) event.getWhoClicked();
    191. ItemStack clicked = event.getCurrentItem();
    192. Inventory inventory = event.getInventory();
    193. if (inventory.getName().equals(myInventory.getName())) {
    194. if (clicked.getType() == Material.EMERALD_BLOCK) {
    195. event.setCancelled(true);
    196. player.closeInventory();
    197. Green.addPlayer(player);
    198. player.sendMessage("You are now in the Green Team");
    199. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CTO.getInstance(), new Runnable() {
    200. public void run() {
    201. player.setItemInHand(null);
    202. }
    203. }, 3);
    204. player.updateInventory();
    205. }
    206. }}}
    207.  
    208.  
    209.  
    210.  
    211.  


    But when i click the items in the Inventory the item you have in hand doesnt dissapear
     
  2. Offline

    Onlineids

    Well first off you didnt have to make events for each blocks you couldve done or ( || )
    And try putting player.updateInventory() inside your runnable
     
  3. Offline

    PizzaPixel

  4. Offline

    Onlineids

    Any errors? If not toss in some debug messages and tell me how far the code gets.
     
  5. Offline

    PizzaPixel

  6. Offline

    Onlineids

    Toss a couple debug messages in then and tell me when it stops
     
  7. Offline

    PizzaPixel

    Onlineids I can't do that now I'm not home
     
  8. Offline

    Onlineids

    Then how did u test what I said before -_-
     
  9. Offline

    PizzaPixel

    Onlineids I left 10 minutes ago

    The debug messages all worked :( Onlineids

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    ean521

    It looks like you DEFINED run() but never CALLED it. I've never used Runnable before, though, so I may be wrong.
     
  11. Offline

    PizzaPixel

  12. Offline

    ean521

  13. Offline

    PizzaPixel

    ean521 No i called the runnable

    LOL

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  14. Offline

    ean521

    PizzaPixel

    Okay then, I just wasn't sure you did, as I never saw it in the code.


    EDIT: Where DO you call it? :confused:
     
  15. Offline

    PizzaPixel

    ean521 Its fine

    SkillSam Not working

    Anyone? :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  16. Offline

    JBoss925

    Well just use the inventory click event to get the player, get the inventory and get the clicked item and set it to air.
    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent e){
    3. if(e.getAction() == InventoryAction.PICKUP_ALL || e.getAction() == InventoryAction.PICKUP_ONE){
    4. Player p = (Player) e.getWhoClicked();
    5. if(p.getItemOnCursor().getType() == Material.LAPIS_BLOCK){
    6. p.getItemOnCursor().setType(Material.AIR);
    7. //Do other stuff here with your plugin.
    8. }
    9. if(p.getItemOnCursor().getType() == Material.REDSTONE_BLOCK){
    10. p.getItemOnCursor().setType(Material.AIR);
    11. //Do other stuff here with your plugin.
    12.  
    13. //Etc etc etc...
    14. }
    15. }
    16. }
     
  17. Offline

    PizzaPixel

    JBoss925 You dont understand what im doing :(
     
  18. Offline

    JBoss925

    I have no idea what you're doing. Please explain and maybe I can help.
     
  19. Offline

    Mattkx4

    Then please.. enlighten us.
     
  20. Offline

    PizzaPixel

    Mattkx4 Look, Thats not part of my plugin. Your telling me to do a completely different plugin.
     
Thread Status:
Not open for further replies.

Share This Page