Getting Region ID WorldGuard API

Discussion in 'Plugin Development' started by Colby l, Jan 26, 2013.

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

    Colby l

    I'm trying to remove a region through WorldGuard's API, and when I use the code below, I get the error message about concurrent exceptions.

    here's my code:
    Code:java
    1. package com.ahellhound.autodelete;
    2.  
    3.  
    4.  
    5. import java.text.SimpleDateFormat;
    6. import java.util.Date;
    7. import java.util.Map;
    8. import java.util.logging.Level;
    9. import java.util.logging.Logger;
    10. import org.bukkit.World;
    11.  
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.ChatColor;
    14. import org.bukkit.Location;
    15. import org.bukkit.World;
    16. import org.bukkit.command.CommandExecutor;
    17. import org.bukkit.configuration.ConfigurationSection;
    18. import org.bukkit.entity.Player;
    19. import org.bukkit.event.EventHandler;
    20. import org.bukkit.event.Listener;
    21. import org.bukkit.event.player.PlayerInteractEvent;
    22. import org.bukkit.event.player.PlayerLoginEvent;
    23. import org.bukkit.plugin.Plugin;
    24. import org.bukkit.plugin.java.JavaPlugin;
    25.  
    26. import com.sk89q.worldguard.bukkit.WGBukkit;
    27. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    28. import com.sk89q.worldguard.protection.managers.RegionManager;
    29. import com.sk89q.worldguard.protection.regions.ProtectedRegion;
    30.  
    31.  
    32. public class Main extends JavaPlugin implements CommandExecutor,Listener {
    33.  
    34. Logger log = Logger.getLogger("Minecraft");
    35.  
    36.  
    37. @Override
    38. public void onEnable() {
    39.  
    40. getWorldGuard();
    41. // this.getConfig().options().copyDefaults(true);
    42. //this.saveConfig();
    43. //reloadConfig();
    44. getServer().getPluginManager().registerEvents(this, this);
    45. log.info("[" + getDescription().getName() + "] " + getDescription().getName() + " version " + getDescription().getVersion() + " is now enabled.");
    46. log.info("[" + getDescription().getName() + "]" + " Made By AhellHound");
    47.  
    48.  
    49.  
    50.  
    51. }
    52.  
    53. private WorldGuardPlugin getWorldGuard() {
    54. Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
    55.  
    56. // WorldGuard may not be loaded
    57. if (plugin == null || !(plugin instanceof WorldGuardPlugin)) {
    58.  
    59.  
    60. this.getLogger().log(Level.SEVERE, "[AutoDelete]" + ChatColor.RED + "You Must Have WorldGuard In Order For This Plugin To Work!");
    61.  
    62. }
    63.  
    64. return (WorldGuardPlugin) plugin;
    65. }
    66.  
    67. @EventHandler
    68. public void normalLogin(PlayerLoginEvent event) {
    69.  
    70.  
    71.  
    72. }
    73.  
    74. @EventHandler
    75. public void TestEvent(PlayerInteractEvent event) {
    76. //Variables
    77. Player p = event.getPlayer();
    78. long time = new Date().getTime();
    79. long integerValue = time;
    80. //long pTime = pt.getLong(key);
    81. int Mili = 3600000;
    82. double InactAmountMili;
    83.  
    84. Bukkit.broadcastMessage("Time " + time);
    85. Bukkit.broadcastMessage("integerValue " + integerValue);
    86.  
    87. //Saves player time to file
    88. if (p.hasPermission("autodelete.check"))
    89. {
    90.  
    91. getConfig().set("PlayerTimes." + p.getName(), integerValue);
    92. saveConfig();
    93.  
    94.  
    95. //Date
    96. double cit = getConfig().getDouble("InactiveAmount");
    97. InactAmountMili = (cit * Mili);
    98. Bukkit.broadcastMessage("InactAmountMili " + InactAmountMili);
    99. Bukkit.broadcastMessage("cit " + cit);
    100.  
    101.  
    102.  
    103. //Reads Values
    104. ConfigurationSection pt = getConfig().getConfigurationSection("PlayerTimes");
    105.  
    106. Location loc = p.getLocation();
    107.  
    108. RegionManager rm = WGBukkit.getRegionManager(loc.getWorld());
    109.  
    110. Map<String, ProtectedRegion> regionMap = WGBukkit.getRegionManager(loc.getWorld()).getRegions();
    111. //ConfigurationSection pt1 = getConfig().getConfigurationSection("PlayerTimes");
    112.  
    113.  
    114.  
    115. if(pt == null)
    116. {
    117. pt = getConfig().createSection("PlayerTimes");
    118. }
    119.  
    120.  
    121. for(String key : pt.getKeys(false))
    122.  
    123. {
    124.  
    125.  
    126.  
    127. if ((pt.getLong(key) + InactAmountMili) < (long)time)
    128. {
    129. //key = Player Name
    130. for(ProtectedRegion region : regionMap.values()) {
    131. if(region.isOwner(key)) {
    132.  
    133. Bukkit.broadcastMessage("key + InactAmountMili " + (pt.getLong(key) + InactAmountMili));
    134. String regId = region.getId();
    135. // String regReg = rm.getRegion(region);
    136. rm.removeRegion(regId);
    137.  
    138. //String regId = region.getId();
    139. //rm.removeRegion(regId);
    140.  
    141. //do stuff with region here
    142. }
    143. }
    144.  
    145.  
    146. }
    147.  
    148.  
    149. }
    150.  
    151. }
    152.  
    153.  
    154. }
    155.  
    156.  
    157.  
    158. }


    Thanks for the help,
    AhellHound

    Also, I tried to find a removeRegion in RegionManager, but I couldn't find it in javadocs

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. Offline

    dkabot

    'Cannot make a static reference to a non-static method'
    Just saying RegionManager calls the region manager statically, hence the issue. You need to run the method off an INSTANCE of RegionManager. Try doing it with getWorldGuard().getRegionManager() instead of RegionManager
     
  3. Offline

    Colby l

    Hey Dkabot!

    So I got an error, with no exceptions in eclipse.

    Here's the code:

    Code:java
    1.  
    2. package com.ahellhound.autodelete;
    3.  
    4.  
    5.  
    6. import java.text.SimpleDateFormat;
    7. import java.util.Date;
    8. import java.util.Map;
    9. import java.util.logging.Level;
    10. import java.util.logging.Logger;
    11. import org.bukkit.World;
    12.  
    13. import org.bukkit.Bukkit;
    14. import org.bukkit.ChatColor;
    15. import org.bukkit.Location;
    16. import org.bukkit.World;
    17. import org.bukkit.command.CommandExecutor;
    18. import org.bukkit.configuration.ConfigurationSection;
    19. import org.bukkit.entity.Player;
    20. import org.bukkit.event.EventHandler;
    21. import org.bukkit.event.Listener;
    22. import org.bukkit.event.player.PlayerInteractEvent;
    23. import org.bukkit.event.player.PlayerLoginEvent;
    24. import org.bukkit.plugin.Plugin;
    25. import org.bukkit.plugin.java.JavaPlugin;
    26.  
    27. import com.sk89q.worldguard.bukkit.WGBukkit;
    28. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    29. import com.sk89q.worldguard.protection.managers.RegionManager;
    30. import com.sk89q.worldguard.protection.regions.ProtectedRegion;
    31.  
    32.  
    33. public class Main extends JavaPlugin implements CommandExecutor,Listener {
    34.  
    35. Logger log = Logger.getLogger("Minecraft");
    36.  
    37.  
    38. @Override
    39. public void onEnable() {
    40.  
    41. getWorldGuard();
    42. // this.getConfig().options().copyDefaults(true);
    43. //this.saveConfig();
    44. //reloadConfig();
    45. getServer().getPluginManager().registerEvents(this, this);
    46. log.info("[" + getDescription().getName() + "] " + getDescription().getName() + " version " + getDescription().getVersion() + " is now enabled.");
    47. log.info("[" + getDescription().getName() + "]" + " Made By AhellHound");
    48.  
    49.  
    50.  
    51.  
    52. }
    53.  
    54. private WorldGuardPlugin getWorldGuard() {
    55. Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
    56.  
    57. // WorldGuard may not be loaded
    58. if (plugin == null || !(plugin instanceof WorldGuardPlugin)) {
    59.  
    60.  
    61. this.getLogger().log(Level.SEVERE, "[AutoDelete]" + ChatColor.RED + "You Must Have WorldGuard In Order For This Plugin To Work!");
    62.  
    63. }
    64.  
    65. return (WorldGuardPlugin) plugin;
    66. }
    67.  
    68. @EventHandler
    69. public void normalLogin(PlayerLoginEvent event) {
    70.  
    71.  
    72.  
    73. }
    74.  
    75. @EventHandler
    76. public void TestEvent(PlayerInteractEvent event) {
    77. //Variables
    78. Player p = event.getPlayer();
    79. long time = new Date().getTime();
    80. int integerValue = (int) time;
    81. //long pTime = pt.getLong(key);
    82. int Mili = 3600000;
    83. int InactAmountMili;
    84.  
    85.  
    86. //Saves player time to file
    87. if (p.hasPermission("autodelete.check"))
    88. {
    89.  
    90. getConfig().set("PlayerTimes." + p.getName(), integerValue);
    91. saveConfig();
    92.  
    93.  
    94. //Date
    95. int cit = getConfig().getInt("InactiveAmount");
    96. InactAmountMili = cit * Mili;
    97.  
    98.  
    99.  
    100.  
    101. //Reads Values
    102. ConfigurationSection pt = getConfig().getConfigurationSection("PlayerTimes");
    103.  
    104.  
    105.  
    106. Location loc = p.getLocation();
    107. RegionManager rm = getWorldGuard().getRegionManager(loc.getWorld());
    108. Map<String, ProtectedRegion> regionMap = getWorldGuard().getRegionManager(loc.getWorld()).getRegions();
    109.  
    110.  
    111.  
    112. if(pt == null)
    113. {
    114. pt = getConfig().createSection("PlayerTimes");
    115. }
    116.  
    117. for(String key : pt.getKeys(false))
    118.  
    119. {
    120.  
    121.  
    122.  
    123. if ((pt.getLong(key) + InactAmountMili) < (int)time)
    124. {
    125. //key = Player Name
    126. for(ProtectedRegion region : regionMap.values()) {
    127. if(region.isOwner(key)) {
    128.  
    129.  
    130. //ProtectedRegion regId = RegionManager.getRegionExact(region);
    131. String regId = region.getId();
    132. rm.removeRegion(regId);
    133.  
    134. //do stuff with region here
    135. }
    136. }
    137.  
    138.  
    139. }
    140.  
    141.  
    142. }
    143.  
    144. }
    145.  
    146.  
    147. }
    148.  
    149.  
    150.  
    151. }
    152.  


    Here's the error:
    Code:
    org.bukkit.event.EventException
    ...
    Caused by: java.util.ConcurrentModificationException
    I can't seem to find any good examples on Google.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  4. Offline

    dkabot

    ConvurrentModificationException means multiple things are trying to change something at the same time which does not work.
    I'm not good enough to say specifically which it is, so all I can suggest is trial and error.
     
  5. Offline

    Colby l

    Hmm, I've had little luck so far, if anyone else can bring in their input, that'd be awesome, thanks!

    Well my plugin may cease production, as I've tried to also send the /region remove command through console (and I tried to use getPlayer to seem like a player is sending it) with no luck.

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

Share This Page