Solved Could not pass event BlockPlaceEvent

Discussion in 'Plugin Development' started by XXLuigiMario, Aug 25, 2014.

Thread Status:
Not open for further replies.
  1. I'm having an issue with my event, this is my error:

    Code:
    25.08 23:14:50 [Multicraft] Skipped 2034 lines due to rate limit (30/s)
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_67]
    25.08 23:14:48 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_67]
    25.08 23:14:48 [Server] INFO at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source) ~[?:?]
    25.08 23:14:48 [Server] INFO at me.XXLuigiMario.ProtectionStonesFix.ProtectionStonesFix.onBlockPlace(ProtectionStonesFix.java:87) ~[?:?]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_67]
    25.08 23:14:48 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_67]
    25.08 23:14:48 [Server] INFO at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source) ~[?:?]
    25.08 23:14:48 [Server] INFO at me.XXLuigiMario.ProtectionStonesFix.ProtectionStonesFix.onBlockPlace(ProtectionStonesFix.java:87) ~[?:?]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_67]
    25.08 23:14:48 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_67]
    25.08 23:14:48 [Server] INFO at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source) ~[?:?]
    25.08 23:14:48 [Server] INFO at me.XXLuigiMario.ProtectionStonesFix.ProtectionStonesFix.onBlockPlace(ProtectionStonesFix.java:87) ~[?:?]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[craftbukkit_179_beta.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    25.08 23:14:48 [Server] INFO org.bukkit.event.EventException
    25.08 23:14:48 [Server] ERROR Could not pass event BlockPlaceEvent to ProtectionStonesFix v1.0
    
    my code:

    --- Line 76 ---
    Code:java
    1. @EventHandler(priority = EventPriority.LOW)
    2. public void onBlockPlace(BlockPlaceEvent e) {
    3. Player player = e.getPlayer();
    4. Block block = e.getBlock();
    5. if (canBuild(player, block)) {
    6. if (block.getType() == Material.CLAY || block.getType() == Material.COAL_BLOCK || block.getType() == Material.OBSIDIAN || block.getType() == Material.BEDROCK) {
    7. ItemStack hand = player.getItemInHand();
    8. if (!(hand.getItemMeta().getLore().get(1).equalsIgnoreCase("Protection Area"))) {
    9. e.setCancelled(true);
    10. BlockPlaceEvent event = new BlockPlaceEvent(block, null, block, hand, player, true);
    11. Bukkit.getPluginManager().callEvent(event);
    12. Integer amount = hand.getAmount();
    13. hand.setAmount(amount--);
    14. player.setItemInHand(hand);
    15. }
    16. }
    17. }
    18. }

    --- Line 95 ---

    How can I fix it?
     
  2. Offline

    JBoss925

    You're creating an infinite loop.

    BlockPlaceEvent calls a BlockPlaceEvent that then calls a BlockPlaceEvent that then calls a BlockPlaceEvent and so on.

    I don't understand why you're calling a new BlockPlaceEvent though.
     
  3. Thought so.
    But I need to cancel it now, or else another plugin gets the event, and I don't want that.
     
  4. Offline

    JBoss925

    Change your priority to highest. Then it'll be passed through the other event handlers first and then yours so yours will be the most effective.
     
  5. Ya, the thing is the plugin also using Highest...
     
  6. Offline

    JBoss925

    Two things I can think of:

    1. In your onEnable(), unregister the listener of the class you don't want to get the event.

    or if you still want the other class to get the event:

    2. What you can do is create a custom event exactly the same as the BlockPlaceEvent but with a different name. Then listen for the BlockPlaceEvent and then when a BlockPlaceEvent occurs, trigger your custom event. Now have a different method listening for the custom event and do stuff there.
     
  7. I want the event to happen but only if
    Code:java
    1. if (block.getType() == Material.CLAY || block.getType() == Material.COAL_BLOCK || block.getType() == Material.OBSIDIAN || block.getType() == Material.BEDROCK) {
    is not true.
     
  8. Offline

    JBoss925

    Well then check that ^ and if it is not true, register the listener for the class. If it is, unregister the listener for the class.
     
  9. How do I save a listener to register it again later?
     
  10. Offline

    JBoss925

    You don't? It's just pluginManager.registerEvents(this, new ListenerClassThatYouWantToRegister());
     
  11. lol fail nvm
     
  12. Offline

    JBoss925

    Haha it's ok. Tell me if you need anything else!:p
     
  13. Thank you :D

    I just tested it and it's not working :/
    My code:
    Code:java
    1. @EventHandler(priority = EventPriority.LOW)
    2. public void onBlockPlace(BlockPlaceEvent e) {
    3. Player player = e.getPlayer();
    4. Block block = e.getBlock();
    5. if (canBuild(player, block)) {
    6. getProtectionStones();
    7. if (ProtectionStones.blocksList.contains(block.getType())) {
    8. if (player.getItemInHand().getItemMeta().getLore().get(1).equalsIgnoreCase("Protection Area")) {
    9. Bukkit.getPluginManager().registerEvents(this, getProtectionStones());
    10. }else{
    11. BlockPlaceEvent.getHandlerList().unregister(getProtectionStones());
    12. }
    13. }
    14. }
    15. }

    The other server's listener runs anyway.

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

    JBoss925

    Hmmmm…can I see full code?
     

  15. Code:java
    1. package me.XXLuigiMario.ProtectionStonesFix;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.logging.Level;
    6. import java.util.regex.Matcher;
    7. import java.util.regex.Pattern;
    8.  
    9. import net.milkbowl.vault.economy.Economy;
    10.  
    11. import org.bukkit.Bukkit;
    12. import org.bukkit.ChatColor;
    13. import org.bukkit.Material;
    14. import org.bukkit.block.Block;
    15. import org.bukkit.block.BlockState;
    16. import org.bukkit.block.Sign;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.EventHandler;
    19. import org.bukkit.event.EventPriority;
    20. import org.bukkit.event.Listener;
    21. import org.bukkit.event.block.Action;
    22. import org.bukkit.event.block.BlockBreakEvent;
    23. import org.bukkit.event.block.BlockPlaceEvent;
    24. import org.bukkit.event.block.SignChangeEvent;
    25. import org.bukkit.event.player.PlayerInteractEvent;
    26. import org.bukkit.inventory.ItemStack;
    27. import org.bukkit.inventory.meta.ItemMeta;
    28. import org.bukkit.plugin.Plugin;
    29. import org.bukkit.plugin.PluginDescriptionFile;
    30. import org.bukkit.plugin.RegisteredServiceProvider;
    31. import org.bukkit.plugin.java.JavaPlugin;
    32.  
    33. import com.axeldios.ProtectionStones.ProtectionStones;
    34. import com.axeldios.ProtectionStones.ProtectionStonesBlockListener;
    35. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    36.  
    37. public class ProtectionStonesFix extends JavaPlugin implements Listener {
    38.  
    39. PluginDescriptionFile pdfFile = getDescription();
    40. public static Economy econ = null;
    41.  
    42. public void onDisable() {
    43. getLogger().info(pdfFile.getName() + " " + pdfFile.getVersion() + " by XXLuigiMario has been disabled.");
    44. }
    45.  
    46. public void onEnable() {
    47. getServer().getPluginManager().registerEvents(this, this);
    48. getLogger().info(pdfFile.getName() + " " + pdfFile.getVersion() + " by XXLuigiMario has been enabled.");
    49. if (!setupEconomy()) {
    50. getLogger().log(Level.SEVERE, "Missing dependency! (Vault)");
    51. getServer().getPluginManager().disablePlugin(this);
    52. return;
    53. }
    54. getWorldGuard();
    55. }
    56.  
    57. private WorldGuardPlugin getWorldGuard() {
    58. Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
    59. if (plugin == null || !(plugin instanceof WorldGuardPlugin)) {
    60. getLogger().log(Level.SEVERE, "Missing dependency! (WorldGuard)");
    61. Bukkit.getPluginManager().disablePlugin(this);
    62. return null;
    63. }
    64. return (WorldGuardPlugin) plugin;
    65. }
    66.  
    67. private ProtectionStones getProtectionStones() {
    68. Plugin plugin = getServer().getPluginManager().getPlugin("ProtectionStones");
    69. if (plugin == null || !(plugin instanceof ProtectionStones)) {
    70. getLogger().log(Level.SEVERE, "Missing dependency! (ProtectionStones)");
    71. Bukkit.getPluginManager().disablePlugin(this);
    72. return null;
    73. }
    74. return (ProtectionStones) plugin;
    75. }
    76.  
    77. private boolean setupEconomy() {
    78. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    79. return false;
    80. }
    81. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    82. if (rsp == null) {
    83. return false;
    84. }
    85. econ = rsp.getProvider();
    86. return econ != null;
    87. }
    88.  
    89. @EventHandler(priority = EventPriority.LOW)
    90. public void onBlockPlace(BlockPlaceEvent e) {
    91. Player player = e.getPlayer();
    92. Block block = e.getBlock();
    93. if (canBuild(player, block)) {
    94. getProtectionStones();
    95. if (ProtectionStones.blocksList.contains(block.getType())) {
    96. if (player.getItemInHand().getItemMeta().getLore().get(1).equalsIgnoreCase("Protection Area")) {
    97. Bukkit.getPluginManager().registerEvents(this, getProtectionStones());
    98. }else{
    99. BlockPlaceEvent.getHandlerList().unregister(getProtectionStones());
    100. }
    101. }
    102. }
    103. }
    104.  
    105. @EventHandler(priority = EventPriority.LOW)
    106. public void onBlockBreak(BlockBreakEvent e) {
    107. Player player = e.getPlayer();
    108. Block block = e.getBlock();
    109. if (canBuild(player, block)) {
    110. if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) {
    111. BlockState bs = block.getState();
    112. if (bs instanceof Sign) {
    113. Sign sign = (Sign) bs;
    114. if (sign.getLine(0).equalsIgnoreCase(signFormat())) {
    115. if (!(player.hasPermission("protectionstones.shop.destroy"))){
    116. noPerms(player);
    117. }else{
    118. player.sendMessage(ChatColor.DARK_RED + "Shop sign destroyed.");
    119. }
    120. }
    121. }
    122. }
    123. }
    124. }
    125.  
    126. @SuppressWarnings("deprecation")
    127. @EventHandler(priority = EventPriority.MONITOR)
    128. public void onPlayerUse(PlayerInteractEvent e) {
    129. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    130. Player player = e.getPlayer();
    131. Block block = e.getClickedBlock();
    132. if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) {
    133. if (!player.hasPermission("protectionstones.shop.use")) {
    134. noPerms(player);
    135. return;
    136. }
    137. Sign sign = (Sign) e.getClickedBlock().getState();
    138. if (!sign.getLine(0).equalsIgnoreCase(signFormat())) {
    139. return;
    140. }
    141. AreaSize size = parseSize(sign.getLine(1));
    142. if (size == null) {
    143. return;
    144. }
    145. double price;
    146. try {
    147. price = Double.parseDouble(sign.getLine(2).replace("$", ""));
    148. } catch (NumberFormatException ex) {
    149. return;
    150. }
    151. Material material = Material.getMaterial(sign.getLine(3).toUpperCase());
    152. if (material == null || !material.isBlock() || material.hasGravity()) {
    153. return;
    154. }
    155. if (econ.getBalance(player) < price) {
    156. player.sendMessage(ChatColor.DARK_RED + "You do not have enough money.");
    157. return;
    158. }
    159. econ.withdrawPlayer(player, price);
    160. List<String> lore = new ArrayList<String>();
    161. lore.add(ChatColor.DARK_GRAY + size.getString());
    162. lore.add(ChatColor.GREEN + "Protection Area");
    163. ItemStack pb = new ItemStack(material);
    164. ItemMeta im = pb.getItemMeta();
    165. im.setLore(lore);
    166. pb.setItemMeta(im);
    167. player.getInventory().addItem(pb);
    168. player.updateInventory();
    169. player.sendMessage(ChatColor.GREEN + "Bought a " + size.getString() + " protection block for $ " + price);
    170. }
    171. }
    172. }
    173.  
    174. @EventHandler(priority = EventPriority.LOW)
    175. public void onSignEdit(SignChangeEvent e) {
    176. Player player = e.getPlayer();
    177. if (e.getLine(0).equalsIgnoreCase("[protectshop]")) {
    178. if (!player.hasPermission("protectionstones.shop.create")) {
    179. e.setCancelled(true);
    180. noPerms(player);
    181. return;
    182. }
    183. Material material = Material.getMaterial(e.getLine(3).toUpperCase());
    184. AreaSize size = parseSize(e.getLine(1));
    185. if (size == null) {
    186. e.setLine(1, ChatColor.STRIKETHROUGH + e.getLine(1));
    187. player.sendMessage(ChatColor.DARK_RED + "Invalid area size!");
    188. return;
    189. }
    190. double price;
    191. try {
    192. price = Double.parseDouble(e.getLine(2).replace("$", ""));
    193. } catch (NumberFormatException ex) {
    194. e.setLine(2, ChatColor.STRIKETHROUGH + e.getLine(2));
    195. player.sendMessage(ChatColor.DARK_RED + "Invalid price!");
    196. return;
    197. }
    198. if (material == null) {
    199. player.sendMessage(ChatColor.DARK_RED + "Invalid material!");
    200. } else if (!material.isBlock()) {
    201. player.sendMessage(ChatColor.DARK_RED + "The material can not be an item!");
    202. } else if (material.hasGravity()) {
    203. player.sendMessage(ChatColor.DARK_RED + "The material can not have gravity!");
    204. } else {
    205. getProtectionStones();
    206. if (!ProtectionStones.blocksList.contains(material)){
    207. player.sendMessage(ChatColor.DARK_RED + "That material is not in the config!");
    208. } else {
    209. e.setLine(0, signFormat());
    210. e.setLine(1, size.getString());
    211. e.setLine(2, "$ " + price);
    212. e.setLine(3, material.name());
    213. player.sendMessage(ChatColor.GREEN + "Shop sign created!");
    214. return;
    215. }
    216. }
    217. e.setLine(3, ChatColor.STRIKETHROUGH + e.getLine(3));
    218. }
    219. }
    220.  
    221. private String signFormat() {
    222. return ChatColor.GREEN + "[ProtectShop]";
    223. }
    224.  
    225. private void noPerms(Player player) {
    226. player.sendMessage(ChatColor.DARK_RED + "You don not have permission to do that!");
    227. }
    228.  
    229. private AreaSize parseSize(String line) {
    230. AreaSize size = new AreaSize();
    231. if (!line.matches("(.*)\\s*(\\d+)\\s*x\\s*(\\d+)\\s*x\\s*(\\d+)(.*)")) {
    232. return null;
    233. }
    234. Pattern p = Pattern.compile("-?\\d+");
    235. Matcher m = p.matcher(line);
    236. m.find();
    237. size.length = Integer.parseInt(m.group());
    238. m.find();
    239. size.height = Integer.parseInt(m.group());
    240. m.find();
    241. size.width = Integer.parseInt(m.group());
    242. return size;
    243. }
    244.  
    245. private class AreaSize {
    246. int length;
    247. int height;
    248. int width;
    249. private String getString(){
    250. return "" + this.length + " x " + this.height + " x " + this.width;
    251. }
    252. }
    253.  
    254. private boolean canBuild(Player player, Block block) {
    255. if (getWorldGuard().canBuild(player, block)) {
    256. return true;
    257. } else {
    258. return false;
    259. }
    260. }
    261. }
    262.  
     
  16. Offline

    JBoss925

    Try this and tell if it works and/or what is prints out.
    I changed unregister to unregisterAll.

    Code:java
    1. @EventHandler(priority = EventPriority.LOW)
    2. public void onBlockPlace(BlockPlaceEvent e) {
    3. Player player = e.getPlayer();
    4. Block block = e.getBlock();
    5. if (canBuild(player, block)) {
    6. Bukkit.broadcastMessage("1");
    7. getProtectionStones();
    8. if (ProtectionStones.blocksList.contains(block.getType())) {
    9. Bukkit.broadcastMessage("2");
    10. if (player.getItemInHand().getItemMeta().getLore().get(1).equalsIgnoreCase("Protection Area")) {
    11. Bukkit.broadcastMessage("3");
    12. Bukkit.getPluginManager().registerEvents(this, getProtectionStones());
    13. Bukkit.broadcastMessage("4");
    14. }else{
    15. BlockPlaceEvent.getHandlerList().unregisterAll(getProtectionStones());
    16. Bukkit.broadcastMessage("5");
    17. }
    18. }
    19. }
    20. }
     
  17. I didn't do that, I looked at my code, and found out what was wrong, here's my code:
    Unregistering works, but re-registering doesn't, once it's unregistered it won't register again.
    Code:java
    1. @EventHandler(priority = EventPriority.LOW)
    2. public void onBlockPlace(BlockPlaceEvent e) {
    3. Player player = e.getPlayer();
    4. Block block = e.getBlock();
    5. Material type = block.getType();
    6. if (canBuild(player, block)) {
    7. getProtectionStones();
    8. if (type == Material.CLAY || type == Material.COAL_BLOCK || type == Material.OBSIDIAN || type == Material.BEDROCK) {
    9. if (player.getItemInHand().getItemMeta().getLore().get(1).equalsIgnoreCase("Protection Area")) {
    10. Bukkit.getPluginManager().registerEvents(this, getProtectionStones());
    11. }else{
    12. BlockPlaceEvent.getHandlerList().unregister(getProtectionStones());
    13. }
    14. }
    15. }
    16. }
     
  18. Offline

    JBoss925

    Well, I suppose the conflicting plugin had its priority like that for a reason. If you can't alter the source code then there's nothing I can really do except suggest what I previously said:

     
  19. I think I found my way out, I decompiled the plugin and I saw there's a variable I can modify to make the event not trigger, i'll post the code here when done for anyone that could be interested.
     
  20. Offline

    Rocoty

    Can you go back and tell us what you really want to achieve. I feel like this isn't getting anywhere...
     
  21. The plugin ProtectionStones does not check if the block you're placing is in fact a protection block, so if you for example say that obsidian is a protection block, you can simply go find somewhere in a cave an obsidian block, mine it, and place it everywhere you want and the plugin will treat it as a Protection Block, but in my plugin I'm checking if the item has the lore of "&aProtection Area", and if it does have it, It'll let ProtectionStones get the event, if it doesnt (a normal block) it'll not let ProtectionStones get it.
    I'm checking for that lore because with my plugin you can also make sign shops to sell those blocks, and when sold, it'll apply to the first line of the lore the radius and to the second one "Protection Area" and you'll get a block with a lore, something like this:

    8 x 8 x 8
    Protection Area
     
  22. Offline

    Rocoty

    Why don't you set the priority to monitor then? And ignoreCancelled to true
     
  23. Because the plugin has the priority to HIGHEST, and it'll fire before mine, as MONITOR is the last priority that's called.
    Once my priority is fired his event will already have protected the area.

    This is ProtectionBlock's BlockPlaceEvent:
    Code:java
    1. @EventHandler(priority=EventPriority.HIGHEST, ignoreCancelled=true)
    2. public void onBlockPlace(BlockPlaceEvent event)
    3. {
    4. Player player = event.getPlayer();
    5. World world = player.getWorld();
    6. PluginManager pm = Bukkit.getServer().getPluginManager();
    7. if (pm.getPlugin("WorldGuard") != null) {
    8. this.worldGuard = ((WorldGuardPlugin)Bukkit.getServer().getPluginManager().getPlugin("WorldGuard"));
    9. }
    10. Block pb = event.getBlock();
    11.  
    12. int size = 0;
    13. String blockName = "";
    14. int end = ProtectionStones.blocks;
    15. boolean done = false;
    16. for (int i = 0; !done; i++) {
    17. if (Material.getMaterial(ProtectionStones.blockType[i]) == pb.getType())
    18. {
    19. size = ProtectionStones.blockSize[i];
    20. blockName = ProtectionStones.blockType[i];
    21. done = true;
    22. }
    23. else if (i > end)
    24. {
    25. done = true;
    26. }
    27. }
    28. RegionManager regionManager = this.worldGuard.getRegionManager(world);
    29. if (size != 0)
    30. {
    31. int t = ProtectionStones.toggleList.indexOf(" " + player.getName() + " ");
    32. if (t != -1)
    33. {
    34. event.setCancelled(false);
    35. return;
    36. }
    37. int w = ProtectionStones.exclusionList.indexOf(" " + world.getName().toLowerCase() + " ");
    38. if (w != -1)
    39. {
    40. if (ProtectionStones.exclusionPlacement)
    41. {
    42. event.setCancelled(false);
    43. }
    44. else
    45. {
    46. player.sendMessage(ChatColor.RED + "No ProtectionStones can be placed in this world.");
    47. player.updateInventory();
    48. event.setCancelled(true);
    49. }
    50. return;
    51. }
    52. if (ProtectionStones.regionLimit == 0)
    53. {
    54. player.sendMessage(ChatColor.RED + "No ProtectionStones can be placed at this time.");
    55. event.setCancelled(true);
    56. return;
    57. }
    58. if (ProtectionStones.regionLimit == -2)
    59. {
    60. int g = 0;
    61. int theGroupLimit = 0;
    62. String theGroupBlocks = "";
    63. String theGroup = "";
    64. for (g = 1; g < 10; g++)
    65. {
    66. theGroup = "protectionstones.group" + g;
    67. if (player.hasPermission(theGroup))
    68. {
    69. theGroupLimit = ProtectionStones.groupLimit[g];
    70. theGroupBlocks = ProtectionStones.groupBlocks[g];
    71. }
    72. }
    73. LocalPlayer playerName = null;
    74. if (theGroupLimit != -1) {
    75. if (theGroupLimit > 0)
    76. {
    77. int count = -1;
    78. try
    79. {
    80. playerName = this.worldGuard.wrapPlayer(player);
    81. count = regionManager.getRegionCountOfPlayer(playerName);
    82. }
    83. catch (Exception localException) {}
    84. if (count >= theGroupLimit)
    85. {
    86. player.sendMessage(ChatColor.RED + "You have reached your limit on ProtectionStones you can place at this time.");
    87. event.setCancelled(true);
    88. }
    89. }
    90. else
    91. {
    92. player.sendMessage(ChatColor.RED + "You cannot place any ProtectionStones.");
    93. event.setCancelled(true);
    94. return;
    95. }
    96. }
    97. if (theGroupBlocks.indexOf(blockName) == -1)
    98. {
    99. player.sendMessage(ChatColor.RED + "You cannot place that ProtectionStones type.");
    100. event.setCancelled(true);
    101. }
    102. }
    103. else if (ProtectionStones.regionLimit != -1)
    104. {
    105. LocalPlayer playerName = null;
    106. int count = -1;
    107. try
    108. {
    109. playerName = this.worldGuard.wrapPlayer(player);
    110. count = regionManager.getRegionCountOfPlayer(playerName);
    111. }
    112. catch (Exception localException1) {}
    113. if (count >= ProtectionStones.regionLimit)
    114. {
    115. player.sendMessage(ChatColor.RED + "You have reached your limit on ProtectionStones you can place at this time.");
    116. event.setCancelled(true);
    117. return;
    118. }
    119. }
    120. if (size > 0) {
    121. if (this.worldGuard.canBuild(player, pb.getLocation()))
    122. {
    123. if (player.hasPermission("protectionstones.create"))
    124. {
    125. double x = pb.getLocation().getX();
    126. double y = pb.getLocation().getY();
    127. double z = pb.getLocation().getZ();
    128. Vector v1 = null;
    129. Vector v2 = null;
    130. if (ProtectionStones.skyBedrock)
    131. {
    132. int mapHeight = world.getMaxHeight() - 1;
    133. v1 = new Vector(x - size, 0.0D, z - size);
    134. v2 = new Vector(x + size, mapHeight, z + size);
    135. }
    136. else
    137. {
    138. v1 = new Vector(x - size, y - size, z - size);
    139. v2 = new Vector(x + size, y + size, z + size);
    140. }
    141. BlockVector min = v1.toBlockVector();
    142. BlockVector max = v2.toBlockVector();
    143. String psx = Double.toString(x);
    144. String psy = Double.toString(y);
    145. String psz = Double.toString(z);
    146. String id = "ps" + psx.substring(0, psx.indexOf(".")) + "x" + psy.substring(0, psy.indexOf(".")) + "y" + psz.substring(0, psz.indexOf(".")) + "z";
    147. ProtectedRegion region = new ProtectedCuboidRegion(id, min, max);
    148. String playerName = player.getName();
    149. region.getOwners().addPlayer(playerName);
    150. regionManager.addRegion(region);
    151.  
    152.  
    153. LocalPlayer thePlayer = this.worldGuard.wrapPlayer(player);
    154. boolean overLap = regionManager.overlapsUnownedRegion(region, thePlayer);
    155. boolean override = false;
    156. if (overLap)
    157. {
    158. if (ProtectionStones.priorityOverride)
    159. {
    160. Vector v = new Vector(x, y, z);
    161. List<String> idList = regionManager.getApplicableRegionsIDs(v);
    162. for (String currentID : idList) {
    163. if (!currentID.equalsIgnoreCase(id))
    164. {
    165. int priority = regionManager.getRegion(currentID).getPriority();
    166. if (priority == -1) {
    167. override = true;
    168. }
    169. }
    170. }
    171. }
    172. if (!override)
    173. {
    174. player.sendMessage(ChatColor.RED + "You can't protect that area, it overlaps another region.");
    175. regionManager.removeRegion(id);
    176. saveRegionCheck(world);
    177.  
    178.  
    179.  
    180.  
    181.  
    182.  
    183.  
    184. player.updateInventory();
    185. event.setCancelled(true);
    186. return;
    187. }
    188. }
    189. HashMap newFlags = new HashMap();
    190. for (int i = 0; i < DefaultFlag.flagsList.length; i++) {
    191. for (int j = 0; j <= ProtectionStones.flags; j++) {
    192. if (DefaultFlag.flagsList[i].getName().equalsIgnoreCase(ProtectionStones.flagName[j])) {
    193. if (ProtectionStones.flagSetting[j] != null)
    194. {
    195. Object newValue = ProtectionStones.getFlagValue(DefaultFlag.flagsList[i], ProtectionStones.flagSetting[j]);
    196. if ((DefaultFlag.flagsList[i].getName().equalsIgnoreCase("greeting")) || (DefaultFlag.flagsList[i].getName().equalsIgnoreCase("farewell"))) {
    197. newValue = newValue.toString().replaceAll("%player%", player.getName());
    198. }
    199. newFlags.put(DefaultFlag.flagsList[i], newValue);
    200. }
    201. else
    202. {
    203. newFlags.put(DefaultFlag.flagsList[i], null);
    204. }
    205. }
    206. }
    207. }
    208. region.setFlags(newFlags);
    209. region.setPriority(ProtectionStones.priorityDefault);
    210. saveRegionCheck(world);
    211.  
    212.  
    213.  
    214.  
    215.  
    216.  
    217.  
    218. player.sendMessage(ChatColor.YELLOW + "This area is now protected.");
    219. if (ProtectionStones.autoHide)
    220. {
    221. pb.setTypeId(0);
    222. ItemStack ore = player.getItemInHand();
    223. ore.setAmount(ore.getAmount() - 1);
    224. player.setItemInHand(ore.getAmount() == 0 ? null : ore);
    225. }
    226. }
    227. else
    228. {
    229. player.sendMessage(ChatColor.RED + "You don't have permission to place a ProtectionStone.");
    230. event.setCancelled(true);
    231. }
    232. }
    233. else
    234. {
    235. player.sendMessage(ChatColor.RED + "You can't protect that area.");
    236. event.setCancelled(true);
    237. }
    238. }
    239. }
    240. }[/i][/i][/i][/i][/i][/i][/i][/i][/i]

    Right now I'm trying to modify the toggleList variable adding the name of the player when the block hasn't got a lore, and removing it when it does, ProtectionStones uses a very bad (from my point of view) way to save the name of the players that disable the placement of protection blocks.
    I'm trying to use this:
    Code:java
    1. @EventHandler(priority = EventPriority.LOW)
    2. public void onBlockPlace(BlockPlaceEvent e) {
    3. Player player = e.getPlayer();
    4. Block block = e.getBlock();
    5. Material type = block.getType();
    6. if (canBuild(player, block)) {
    7. if (type == Material.CLAY || type == Material.COAL_BLOCK || type == Material.OBSIDIAN || type == Material.BEDROCK) {
    8. String playerName = " " + player.getName() + " ";
    9. int w = ProtectionStones.toggleList.indexOf(playerName);
    10. if (player.getItemInHand().getItemMeta().getLore().get(1).equalsIgnoreCase(ChatColor.GREEN + "Protection Area")) {
    11. if (!(w == -1)) {
    12. Bukkit.broadcastMessage(ChatColor.GREEN + "ON");
    13. ProtectionStones.toggleList += playerName;
    14. }
    15. }else{
    16. if (w == -1) {
    17. Bukkit.broadcastMessage(ChatColor.DARK_RED + "OFF");
    18. ProtectionStones.toggleList = ProtectionStones.toggleList.replace(playerName, "");
    19. }
    20. }
    21. }
    22. }
    23. }

    But it doesn't work, I've debugged the code with brodcasts but the ON broadcast never fires, idk why :/


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

    Rocoty

    Alright...what if you unregister ProtectionStones' listener. Then you extend that class and override the event handler method in question. Inside the overridden method body, check for your condition and if it is met just call the superclass counterpart for that method. It's a bit hacky but it may just do what you want
     
  25. I'll try.


    Code:java
    1. public class ProtectionStonesCheck extends ProtectionStonesBlockListener {
    2. @EventHandler(priority=EventPriority.HIGHEST, ignoreCancelled=true)
    3. public void onBlockPlace(BlockPlaceEvent e) {
    4. Player player = e.getPlayer();
    5. Block block = e.getBlock();
    6. Material type = block.getType();
    7. if (canBuild(player, block)) {
    8. if (type == Material.CLAY || type == Material.COAL_BLOCK || type == Material.OBSIDIAN || type == Material.BEDROCK) {
    9. if (player.getItemInHand().getItemMeta().getLore().get(1).equalsIgnoreCase("Protection Area")) {
    10.  
    11. }else{
    12.  
    13. }
    14. }
    15. }
    16. }
    17. }

    So in the check, I need to put Bukkit.getPluginManager().callEvent();
    But If I call another BlockPlaceEvent wouldn't that create a loop like in the first post?

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

    Rocoty

    No don't call another event. Call super.onBlockPlace(e); But make sure you are actually overriding the method (add the @Override annotation for clarity) Make sure to register the listener as well.
     
  27. Offline

    xize

    XXLuigiMario

    well that depends on the logic you try to implement.

    for example you got a if check with material types if you use callEvent with that same info again then it does again the same check and calls again so it fires a new BlockPlaceEvent that would create a infinitife call what potentionally could cause a stackoverflow, but if you do for example put in a other location or a other block or whatever it won't unless it still goes through the same material check (its not recommend to use a event call in the same event, and also BlockPlaceEvent could have some triggery in combination with nocheat etc when a event get custom called but the range is to far)

    anyways could you maybe post your full stacktrace the saved one from your harddrive?, multicraft is known to not show a full stacktrace ;)
     
  28. [​IMG]

    Doesn't work :(

    I debugged it like this:

    Code:java
    1. @EventHandler(priority = EventPriority.LOW)
    2. public void onBlockPlace(BlockPlaceEvent e) {
    3. Player player = e.getPlayer();
    4. Block block = e.getBlock();
    5. Material type = block.getType();
    6. if (canBuild(player, block)) {
    7. Bukkit.broadcastMessage("I got here. 1.");
    8. if (type == Material.CLAY || type == Material.COAL_BLOCK || type == Material.OBSIDIAN || type == Material.BEDROCK) {
    9. Bukkit.broadcastMessage("I got here. 2.");
    10. if (player.getItemInHand().getItemMeta().getLore().contains("Protection Area")) {
    11. Bukkit.broadcastMessage("I got here. 3.");
    12. }else{
    13. Bukkit.broadcastMessage("Nope.avi");
    14. }
    15. }
    16. }
    17. }


    But if the item has the correct lore it fires "Nope.avi" and if it doesn't, it only gets to the "I got here. 2." :confused:

    I've tried with .equalsIgnoreCase and with .contains.

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

    Rocoty

    You must have gotten an error. There is no way neither "I got here. 3." nor "Nope.avi" will broadcast when "I got here. 2." does broadcast, unless an error occurred. Check your console. It may well be a NullPointerException, as not all items have lore.
     
  30. No NPE :(
     
Thread Status:
Not open for further replies.

Share This Page