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>();
} }
/** /**
@ -145,7 +147,7 @@ public void load() {
config.removeProperty("auto-invincible-permission"); config.removeProperty("auto-invincible-permission");
usePlayerMove = config.getBoolean( usePlayerMove = config.getBoolean(
"use-player-move-event", true); "use-player-move-event", true);
hostKeys = new HashMap<String, String>(); hostKeys = new HashMap<String, String>();
Object hostKeysRaw = config.getProperty("host-keys"); Object hostKeysRaw = config.getProperty("host-keys");
if (hostKeysRaw == null || !(hostKeysRaw instanceof Map)) { if (hostKeysRaw == null || !(hostKeysRaw instanceof Map)) {
@ -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) {
@ -329,7 +329,7 @@ public void onPlayerChat(PlayerChatEvent event) {
public void onPlayerLogin(PlayerLoginEvent event) { public void onPlayerLogin(PlayerLoginEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
ConfigurationManager cfg = plugin.getGlobalStateManager(); ConfigurationManager cfg = plugin.getGlobalStateManager();
String hostKey = cfg.hostKeys.get(player.getName().toLowerCase()); String hostKey = cfg.hostKeys.get(player.getName().toLowerCase());
if (hostKey != null) { if (hostKey != null) {
String hostname = event.getHostname(); String hostname = event.getHostname();
@ -337,7 +337,7 @@ public void onPlayerLogin(PlayerLoginEvent event) {
if (colonIndex != -1) { if (colonIndex != -1) {
hostname = hostname.substring(0, colonIndex); hostname = hostname.substring(0, colonIndex);
} }
if (!hostname.equals(hostKey)) { if (!hostname.equals(hostKey)) {
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
"You did not join with the valid host key!"); "You did not join with the valid host key!");

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);
} }
} }

View File

@ -70,7 +70,7 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
try { try {
connect(); connect();
try { try {
// Test if the database is up to date, if not throw a critical error // Test if the database is up to date, if not throw a critical error
PreparedStatement verTest = this.conn.prepareStatement( PreparedStatement verTest = this.conn.prepareStatement(
@ -132,7 +132,7 @@ private void connect() throws SQLException {
} catch (SQLException ex) { } catch (SQLException ex) {
// Test if the dummy query failed because the connection is dead, // Test if the dummy query failed because the connection is dead,
// and if it is mark the connection as closed (the MySQL Driver // and if it is mark the connection as closed (the MySQL Driver
// does not ensure that the connection is marked as closed unless // does not ensure that the connection is marked as closed unless
// the close() method has been called. // the close() method has been called.
if ("08S01".equals(ex.getSQLState())) { if ("08S01".equals(ex.getSQLState())) {
conn.close(); conn.close();
@ -148,7 +148,7 @@ private void loadFlags(ProtectedRegion region) {
// @TODO: Iterate _ONCE_ // @TODO: Iterate _ONCE_
try { try {
PreparedStatement flagsStatement = this.conn.prepareStatement( PreparedStatement flagsStatement = this.conn.prepareStatement(
"SELECT " + "SELECT " +
"`region_flag`.`flag`, " + "`region_flag`.`flag`, " +
"`region_flag`.`value` " + "`region_flag`.`value` " +
"FROM `region_flag` " + "FROM `region_flag` " +
@ -463,7 +463,7 @@ public void load() throws ProtectionDatabaseException {
parentSets = new HashMap<ProtectedRegion,String>(); parentSets = new HashMap<ProtectedRegion,String>();
// We load the cuboid regions first, as this is likely to be the // We load the cuboid regions first, as this is likely to be the
// largest dataset. This should save time in regards to the putAll()s // largest dataset. This should save time in regards to the putAll()s
this.loadCuboid(); this.loadCuboid();
Map<String,ProtectedRegion> regions = this.cuboidRegions; Map<String,ProtectedRegion> regions = this.cuboidRegions;
this.cuboidRegions = null; this.cuboidRegions = null;
@ -625,10 +625,10 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
* b) If the region is not in the database, we insert it * b) If the region is not in the database, we insert it
* 3) We iterate over what remains of the in-database list and remove * 3) We iterate over what remains of the in-database list and remove
* them from the database * them from the database
* *
* TODO: Look at adding/removing/updating the database when the in * TODO: Look at adding/removing/updating the database when the in
* memory region is created/remove/updated * memory region is created/remove/updated
* *
* @see com.sk89q.worldguard.protection.databases.ProtectionDatabase#save() * @see com.sk89q.worldguard.protection.databases.ProtectionDatabase#save()
*/ */
public void save() throws ProtectionDatabaseException { public void save() throws ProtectionDatabaseException {