Update for async player chat stupidity

This commit is contained in:
zml2008 2012-08-04 19:17:52 -07:00
parent ba5be6071f
commit 684214ff6a
5 changed files with 46 additions and 28 deletions

View File

@ -47,7 +47,7 @@
<dependency> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId> <artifactId>bukkit</artifactId>
<version>1.2.5-R4.1-SNAPSHOT</version> <version>1.3.1-R0.1-SNAPSHOT</version>
</dependency> </dependency>
<!-- CommandBook --> <!-- CommandBook -->

View File

@ -24,6 +24,8 @@
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.sk89q.commandbook.CommandBook; import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.GodComponent; import com.sk89q.commandbook.GodComponent;
@ -73,7 +75,7 @@ public class ConfigurationManager {
/** /**
* Holds configurations for different worlds. * Holds configurations for different worlds.
*/ */
private Map<String, WorldConfiguration> worlds; private ConcurrentMap<String, WorldConfiguration> worlds;
/** /**
* The global configuration for use when loading worlds * The global configuration for use when loading worlds
@ -115,7 +117,7 @@ public class ConfigurationManager {
*/ */
public ConfigurationManager(WorldGuardPlugin plugin) { public ConfigurationManager(WorldGuardPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
this.worlds = new HashMap<String, WorldConfiguration>(); this.worlds = new ConcurrentHashMap<String, WorldConfiguration>();
} }
/** /**
@ -193,10 +195,14 @@ public void unload() {
public WorldConfiguration get(World world) { public WorldConfiguration get(World world) {
String worldName = world.getName(); String worldName = world.getName();
WorldConfiguration config = worlds.get(worldName); WorldConfiguration config = worlds.get(worldName);
WorldConfiguration newConfig = null;
if (config == null) { while (config == null) {
config = new WorldConfiguration(plugin, worldName, this.config); if (newConfig == null) {
worlds.put(worldName, config); newConfig = new WorldConfiguration(plugin, worldName, this.config);
}
worlds.putIfAbsent(world.getName(), newConfig);
config = worlds.get(world.getName());
} }
return config; return config;

View File

@ -37,10 +37,10 @@
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerBedEnterEvent; import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent; import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerGameModeChangeEvent; import org.bukkit.event.player.PlayerGameModeChangeEvent;
@ -304,7 +304,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerChat(PlayerChatEvent event) { public void onPlayerChat(AsyncPlayerChatEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
WorldConfiguration wcfg = plugin.getGlobalStateManager().get(player.getWorld()); WorldConfiguration wcfg = plugin.getGlobalStateManager().get(player.getWorld());
if (wcfg.useRegions) { if (wcfg.useRegions) {

View File

@ -23,6 +23,7 @@
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.Location; import org.bukkit.Location;
@ -65,7 +66,7 @@ public class GlobalRegionManager {
/** /**
* Map of managers per-world. * Map of managers per-world.
*/ */
private HashMap<String, RegionManager> managers; private ConcurrentHashMap<String, RegionManager> managers;
/** /**
* Stores the list of modification dates for the world files. This allows * Stores the list of modification dates for the world files. This allows
@ -81,7 +82,7 @@ public class GlobalRegionManager {
public GlobalRegionManager(WorldGuardPlugin plugin) { public GlobalRegionManager(WorldGuardPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
config = plugin.getGlobalStateManager(); config = plugin.getGlobalStateManager();
managers = new HashMap<String, RegionManager>(); managers = new ConcurrentHashMap<String, RegionManager>();
lastModified = new HashMap<String, Long>(); lastModified = new HashMap<String, Long>();
} }
@ -125,19 +126,27 @@ public void unloadAll() {
lastModified.clear(); lastModified.clear();
} }
public RegionManager load(World world) {
RegionManager manager = create(world);
managers.put(world.getName(), manager);
return manager;
}
/** /**
* Load region information for a world. * Load region information for a world.
* *
* @param world The world to load a RegionManager for * @param world The world to load a RegionManager for
* @return the loaded RegionManager * @return the loaded RegionManager
*/ */
public RegionManager load(World world) { public RegionManager create(World world) {
String name = world.getName(); String name = world.getName();
boolean sql = config.useSqlDatabase;
String sqlDsn = config.sqlDsn;
ProtectionDatabase database; ProtectionDatabase database;
File file = null; File file = null;
try { try {
if (!config.useSqlDatabase) { if (!sql) {
file = getPath(name); file = getPath(name);
database = new YAMLDatabase(file, plugin.getLogger()); database = new YAMLDatabase(file, plugin.getLogger());
@ -149,8 +158,6 @@ public RegionManager load(World world) {
// Create a manager // Create a manager
RegionManager manager = new FlatRegionManager(database); RegionManager manager = new FlatRegionManager(database);
managers.put(name, manager);
manager.load(); manager.load();
plugin.getLogger().info(manager.getRegions().size() plugin.getLogger().info(manager.getRegions().size()
@ -159,7 +166,7 @@ public RegionManager load(World world) {
return manager; return manager;
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {
String logStr = "Failed to load regions from "; String logStr = "Failed to load regions from ";
if (config.useSqlDatabase) { if (sql) {
logStr += "SQL Database <" + config.sqlDsn + "> "; logStr += "SQL Database <" + config.sqlDsn + "> ";
} else { } else {
logStr += "file \"" + file + "\" "; logStr += "file \"" + file + "\" ";
@ -224,9 +231,14 @@ public void reloadChanged() {
*/ */
public RegionManager get(World world) { public RegionManager get(World world) {
RegionManager manager = managers.get(world.getName()); RegionManager manager = managers.get(world.getName());
RegionManager newManager = null;
if (manager == null) { while (manager == null) {
manager = load(world); if (newManager == null) {
newManager = create(world);
}
managers.putIfAbsent(world.getName(), newManager);
manager = managers.get(world.getName());
} }
return manager; return manager;
@ -353,7 +365,7 @@ public boolean allows(StateFlag flag, Location loc, LocalPlayer player) {
return true; return true;
} }
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = get(world);
return mgr.getApplicableRegions(toVector(loc)).allows(flag, player); return mgr.getApplicableRegions(toVector(loc)).allows(flag, player);
} }
} }