Cancel Event

Discussion in 'Plugin Development' started by 17xDillz1997, Sep 19, 2014.

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

    17xDillz1997

    Sort of new to coding. If you look, you'll realise my plugins are pretty simple.

    Here I need some help for the GUI to have an inventory but no one can take out anything.
    Someone help?
    It's a kit plugin where you preview what's in it.
    I have two classes: One called KitPreview and one called KitPreviews xD

    Here is KitPreview:
    Code:java
    1. package me.dillz.preview;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.block.Sign;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.block.SignChangeEvent;
    12. import org.bukkit.event.inventory.InventoryClickEvent;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16.  
    17. public class KitPreview
    18. implements Listener
    19. {
    20. @EventHandler(priority=EventPriority.MONITOR)
    21. public void onCreateSign(SignChangeEvent e)
    22. {
    23. if ((e.isCancelled()) && (!e.getPlayer().isOp())) {
    24. return;
    25. }
    26. String[] lines = e.getLines();
    27. if (lines[0] == null) {
    28. return;
    29. }
    30. if (!lines[0].equalsIgnoreCase("[PreviewKit]")) {
    31. return;
    32. }
    33. if (!e.getPlayer().isOp())
    34. {
    35. e.getPlayer().sendMessage(ChatColor.BOLD + "You do not have permission to do this");
    36. e.setCancelled(true);
    37. return;
    38. }
    39. e.setCancelled(false);
    40. e.setLine(0, ChatColor.BOLD + "[PreviewKit]");
    41. e.setLine(1, e.getLine(1));
    42. e.setLine(2, ChatColor.BLUE + "Right Click");
    43. e.setLine(3, ChatColor.BLUE + "To See Kit!");
    44. }
    45.  
    46. @EventHandler
    47. public void onClickSign(PlayerInteractEvent e)
    48. {
    49. if ((e.getAction() != Action.RIGHT_CLICK_BLOCK) && (e.getAction() != Action.RIGHT_CLICK_BLOCK)) {
    50. return;
    51. }
    52. if ((e.getClickedBlock().getState() instanceof Sign))
    53. {
    54. Sign s = (Sign)e.getClickedBlock().getState();
    55. if (s.getLine(0).contentEquals(ChatColor.BOLD + "[PreviewKit]"))
    56. {
    57. String kit = s.getLine(1);
    58. Player p = e.getPlayer();
    59. Inventory vInv = Bukkit.createInventory(null, 36, ChatColor.BLUE + "Preview Kit: " + ChatColor.DARK_AQUA + kit);
    60. ItemStack[] i = p.getInventory().getContents();
    61. p.getInventory().clear();
    62. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "kit " + kit + " " + e.getPlayer().getName());
    63. try
    64. {
    65. Thread.sleep(50L);
    66. }
    67. catch (InterruptedException ex) {}
    68. ItemStack[] ii = p.getInventory().getContents();
    69. p.getInventory().clear();
    70. if (i != null) {
    71. for (int iii = 0; iii < i.length; iii++) {
    72. if (i[iii] != null) {
    73. p.getInventory().setItem(iii, i[iii]);
    74. }
    75. }
    76. }
    77. if (ii != null) {
    78. for (int iii = 0; iii < ii.length; iii++) {
    79. if (ii[iii] != null) {
    80. vInv.addItem(new ItemStack[] { ii[iii] });
    81. }
    82. }
    83. }
    84. p.openInventory(vInv);
    85. e.setCancelled(true);
    86. }
    87. }
    88. }
    89.  
    90. @SuppressWarnings("deprecation")
    91. @EventHandler
    92. public void onClickItem(InventoryClickEvent e)
    93. {
    94. if (e.getInventory().getTitle().startsWith(ChatColor.DARK_PURPLE + "PreviewKit:"))
    95. {
    96. e.setCursor(null);
    97. e.getWhoClicked().closeInventory();
    98. e.getWhoClicked().openInventory(e.getInventory());
    99. e.setCancelled(true);
    100. }
    101. }
    102.  
    103. public void previewKit(String kitName, Player player)
    104. {
    105. String kit = kitName;
    106. Player p = player;
    107. Inventory vInv = Bukkit.createInventory(null, 36, ChatColor.DARK_PURPLE + "Preview Kit: " + ChatColor.BOLD + kit);
    108. ItemStack[] i = p.getInventory().getContents();
    109. p.getInventory().clear();
    110. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "kit " + kit + " " + p.getName());
    111. try
    112. {
    113. Thread.sleep(50L);
    114. }
    115. catch (InterruptedException ex) {}
    116. ItemStack[] ii = p.getInventory().getContents();
    117. p.getInventory().clear();
    118. if (i != null) {
    119. for (int iii = 0; iii < i.length; iii++) {
    120. if (i[iii] != null) {
    121. p.getInventory().setItem(iii, i[iii]);
    122. }
    123. }
    124. }
    125. if (ii != null) {
    126. for (int iii = 0; iii < ii.length; iii++) {
    127. if (ii[iii] != null) {
    128. vInv.addItem(new ItemStack[] { ii[iii] });
    129. }
    130. }
    131. }
    132. p.openInventory(vInv);
    133. }
    134. }
    135.  

    Here is KitPreviews:
    Code:java
    1. package me.dillz.preview;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.EventPriority;
    12. import org.bukkit.event.HandlerList;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class KitPreviews
    19. extends JavaPlugin
    20. implements Listener
    21. {
    22. KitPreview tp;
    23. public static final String TAG = ChatColor.BOLD + "[PreviewKit]" + ChatColor.GOLD;
    24. @SuppressWarnings("unused")
    25. private static final Random RANDOM_GENERATOR = new Random();
    26.  
    27. public void onEnable()
    28. {
    29. this.tp = new KitPreview();
    30. Bukkit.getPluginManager().registerEvents(this.tp, this);
    31. Bukkit.getPluginManager().registerEvents(this, this);
    32. }
    33.  
    34. public void onDisable()
    35. {
    36. HandlerList.unregisterAll(this.tp);
    37. }
    38.  
    39. public boolean onCommand(CommandSender s, Command c, String a, String[] args)
    40. {
    41. if (c.getName().equalsIgnoreCase("PreviewKit"))
    42. {
    43. if (args.length < 1)
    44. {
    45. s.sendMessage(TAG + " Usage: /previewkit (kit)");
    46. return true;
    47. }
    48. String kit = args[0];
    49. if (!(s instanceof Player))
    50. {
    51. s.sendMessage(TAG + " Console cannot do this");
    52. return true;
    53. }
    54. Player p = (Player)s;
    55. this.tp.previewKit(kit, p);
    56. }
    57. return true;
    58. }
    59.  
    60. @EventHandler
    61. public void onCommandPreprocess(PlayerCommandPreprocessEvent e)
    62. {
    63. if (e.getMessage().toLowerCase().startsWith("/previewkit"))
    64. {
    65. e.getPlayer().sendMessage(TAG + " Dillz's PreviewKit Plugin!");
    66. e.setCancelled(true);
    67. }
    68. }
    69.  
    70. @EventHandler(priority=EventPriority.HIGHEST)
    71. public void onPlayerJoin(PlayerJoinEvent e)
    72. {
    73. e.getPlayer().sendMessage(TAG + " This server uses PreviewKit!");
    74. }
    75. }
    76.  


    Any help appreciated. Just edit the code instead of telling me if you can :p
     
  2. Offline

    ChipDev

    Just use event.setCancelled(true); to PlayerInteractEvent in the inventory.
    Not going to spoon feed :l
     
  3. Offline

    17xDillz1997

    It didn't work :c
    Btw did you mean e.setCancelled(true);
     
    ChipDev likes this.
  4. Offline

    mythbusterma

    17xDillz1997

    He meant Cancellable#setCancelled(true), but that's besides the point. I think you should really reconsider what you're doing, you sleep the thread (something which, in Bukkit programming, you should almost never do), you modify/cancel an event when you set the prioirty to Monitor, which violates the contract of EventHandlers, you opened an inventory in a handler for an inventory event, which is specifically recommended against in the JavaDocs, and you use horrible names like "i" and "ii" for variables that aren't throwaways. Learn Java, then try again.
     
  5. Offline

    ChipDev

    Awesome, on my team of using e, not event ;D
     
  6. Offline

    Peter25715


    Event. or e.set...
    Great going for the one who helped :)
    and now pls switch for 'Solved' :)

    ChipDev, You will see "Prefix" on the title, Click on it and look for Solved.
    Cause people will come for nothing here, And also when the people see that this is solved they will come over here and take some info and help from people ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
    ChipDev likes this.
Thread Status:
Not open for further replies.

Share This Page