mirror of
https://github.com/EngineHub/WorldGuard.git
synced 2024-12-18 07:07:43 +01:00
Merge pull request #313 from TheCutter/master
This commit is contained in:
commit
d827e01760
@ -66,12 +66,16 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
|
||||
String world1 = world;
|
||||
this.logger = logger;
|
||||
|
||||
PreparedStatement worldStmt = null;
|
||||
ResultSet worldResult = null;
|
||||
PreparedStatement insertWorldStatement = null;
|
||||
try {
|
||||
connect();
|
||||
|
||||
PreparedStatement verTest = null;
|
||||
try {
|
||||
// Test if the database is up to date, if not throw a critical error
|
||||
PreparedStatement verTest = this.conn.prepareStatement(
|
||||
verTest = this.conn.prepareStatement(
|
||||
"SELECT `world_id` FROM `region_cuboid` LIMIT 0,1;"
|
||||
);
|
||||
verTest.execute();
|
||||
@ -79,21 +83,23 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
|
||||
throw new InvalidTableFormatException(
|
||||
"region_storage_update_20110325.sql"
|
||||
);
|
||||
} finally {
|
||||
closeResource(verTest);
|
||||
}
|
||||
|
||||
PreparedStatement worldStmt = conn.prepareStatement(
|
||||
worldStmt = conn.prepareStatement(
|
||||
"SELECT `id` FROM " +
|
||||
"`world` " +
|
||||
"WHERE `name` = ? LIMIT 0,1"
|
||||
);
|
||||
|
||||
worldStmt.setString(1, world1);
|
||||
ResultSet worldResult = worldStmt.executeQuery();
|
||||
worldResult = worldStmt.executeQuery();
|
||||
|
||||
if (worldResult.first()) {
|
||||
this.worldDbId = worldResult.getInt("id");
|
||||
} else {
|
||||
PreparedStatement insertWorldStatement = this.conn.prepareStatement(
|
||||
insertWorldStatement = this.conn.prepareStatement(
|
||||
"INSERT INTO " +
|
||||
"`world` " +
|
||||
"(`id`, `name`) VALUES (null, ?)",
|
||||
@ -112,6 +118,10 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
|
||||
// We havn't connected to the databases, or there was an error
|
||||
// initialising the world record, so there is no point continuing
|
||||
return;
|
||||
} finally {
|
||||
closeResource(worldResult);
|
||||
closeResource(worldStmt);
|
||||
closeResource(insertWorldStatement);
|
||||
}
|
||||
|
||||
if (this.worldDbId <= 0) {
|
||||
@ -133,11 +143,11 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
|
||||
|
||||
private void connect() throws SQLException {
|
||||
if (conn != null) {
|
||||
// Make a dummy query to check the connnection is alive.
|
||||
// Check if the connection is still alive/valid.
|
||||
try {
|
||||
conn.prepareStatement("SELECT 1;").execute();
|
||||
conn.isValid(2);
|
||||
} catch (SQLException ex) {
|
||||
// Test if the dummy query failed because the connection is dead,
|
||||
// Test if validation failed because the connection is dead,
|
||||
// and if it is mark the connection as closed (the MySQL Driver
|
||||
// does not ensure that the connection is marked as closed unless
|
||||
// the close() method has been called.
|
||||
@ -153,8 +163,10 @@ private void connect() throws SQLException {
|
||||
|
||||
private void loadFlags(ProtectedRegion region) {
|
||||
// @TODO: Iterate _ONCE_
|
||||
PreparedStatement flagsStatement = null;
|
||||
ResultSet flagsResultSet = null;
|
||||
try {
|
||||
PreparedStatement flagsStatement = this.conn.prepareStatement(
|
||||
flagsStatement = this.conn.prepareStatement(
|
||||
"SELECT " +
|
||||
"`region_flag`.`flag`, " +
|
||||
"`region_flag`.`value` " +
|
||||
@ -164,7 +176,7 @@ private void loadFlags(ProtectedRegion region) {
|
||||
);
|
||||
|
||||
flagsStatement.setString(1, region.getId().toLowerCase());
|
||||
ResultSet flagsResultSet = flagsStatement.executeQuery();
|
||||
flagsResultSet = flagsStatement.executeQuery();
|
||||
|
||||
Map<String,Object> regionFlags = new HashMap<String,Object>();
|
||||
while (flagsResultSet.next()) {
|
||||
@ -186,6 +198,9 @@ private void loadFlags(ProtectedRegion region) {
|
||||
"Unable to load flags for region "
|
||||
+ region.getId().toLowerCase() + ": " + ex.getMessage()
|
||||
);
|
||||
} finally {
|
||||
closeResource(flagsResultSet);
|
||||
closeResource(flagsStatement);
|
||||
}
|
||||
}
|
||||
|
||||
@ -203,8 +218,10 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
|
||||
DefaultDomain owners = new DefaultDomain();
|
||||
DefaultDomain members = new DefaultDomain();
|
||||
|
||||
ResultSet userSet = null;
|
||||
PreparedStatement usersStatement = null;
|
||||
try {
|
||||
PreparedStatement usersStatement = this.conn.prepareStatement(
|
||||
usersStatement = this.conn.prepareStatement(
|
||||
"SELECT " +
|
||||
"`user`.`name`, " +
|
||||
"`region_players`.`owner` " +
|
||||
@ -217,7 +234,7 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
|
||||
);
|
||||
|
||||
usersStatement.setString(1, region.getId().toLowerCase());
|
||||
ResultSet userSet = usersStatement.executeQuery();
|
||||
userSet = usersStatement.executeQuery();
|
||||
while(userSet.next()) {
|
||||
if (userSet.getBoolean("owner")) {
|
||||
owners.addPlayer(userSet.getString("name"));
|
||||
@ -227,10 +244,15 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logger.warning("Unable to load users for region " + region.getId().toLowerCase() + ": " + ex.getMessage());
|
||||
} finally {
|
||||
closeResource(userSet);
|
||||
closeResource(usersStatement);
|
||||
}
|
||||
|
||||
PreparedStatement groupsStatement = null;
|
||||
ResultSet groupSet = null;
|
||||
try {
|
||||
PreparedStatement groupsStatement = this.conn.prepareStatement(
|
||||
groupsStatement = this.conn.prepareStatement(
|
||||
"SELECT " +
|
||||
"`group`.`name`, " +
|
||||
"`region_groups`.`owner` " +
|
||||
@ -243,7 +265,7 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
|
||||
);
|
||||
|
||||
groupsStatement.setString(1, region.getId().toLowerCase());
|
||||
ResultSet groupSet = groupsStatement.executeQuery();
|
||||
groupSet = groupsStatement.executeQuery();
|
||||
while(groupSet.next()) {
|
||||
if (groupSet.getBoolean("owner")) {
|
||||
owners.addGroup(groupSet.getString("name"));
|
||||
@ -253,6 +275,9 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logger.warning("Unable to load groups for region " + region.getId().toLowerCase() + ": " + ex.getMessage());
|
||||
} finally {
|
||||
closeResource(groupSet);
|
||||
closeResource(groupsStatement);
|
||||
}
|
||||
|
||||
region.setOwners(owners);
|
||||
@ -263,8 +288,10 @@ private void loadGlobal() {
|
||||
Map<String,ProtectedRegion> regions =
|
||||
new HashMap<String,ProtectedRegion>();
|
||||
|
||||
PreparedStatement globalRegionStatement = null;
|
||||
ResultSet globalResultSet = null;
|
||||
try {
|
||||
PreparedStatement globalRegionStatement = this.conn.prepareStatement(
|
||||
globalRegionStatement = this.conn.prepareStatement(
|
||||
"SELECT " +
|
||||
"`region`.`id`, " +
|
||||
"`region`.`priority`, " +
|
||||
@ -278,7 +305,7 @@ private void loadGlobal() {
|
||||
);
|
||||
|
||||
globalRegionStatement.setInt(1, this.worldDbId);
|
||||
ResultSet globalResultSet = globalRegionStatement.executeQuery();
|
||||
globalResultSet = globalRegionStatement.executeQuery();
|
||||
|
||||
while (globalResultSet.next()) {
|
||||
ProtectedRegion region = new GlobalProtectedRegion(globalResultSet.getString("id"));
|
||||
@ -303,6 +330,9 @@ private void loadGlobal() {
|
||||
logger.warning("\t\tCause: " + t.getMessage());
|
||||
t = t.getCause();
|
||||
}
|
||||
} finally {
|
||||
closeResource(globalResultSet);
|
||||
closeResource(globalRegionStatement);
|
||||
}
|
||||
|
||||
globalRegions = regions;
|
||||
@ -312,8 +342,10 @@ private void loadCuboid() {
|
||||
Map<String,ProtectedRegion> regions =
|
||||
new HashMap<String,ProtectedRegion>();
|
||||
|
||||
PreparedStatement cuboidRegionStatement = null;
|
||||
ResultSet cuboidResultSet = null;
|
||||
try {
|
||||
PreparedStatement cuboidRegionStatement = this.conn.prepareStatement(
|
||||
cuboidRegionStatement = this.conn.prepareStatement(
|
||||
"SELECT " +
|
||||
"`region_cuboid`.`min_z`, " +
|
||||
"`region_cuboid`.`min_y`, " +
|
||||
@ -335,7 +367,7 @@ private void loadCuboid() {
|
||||
);
|
||||
|
||||
cuboidRegionStatement.setInt(1, this.worldDbId);
|
||||
ResultSet cuboidResultSet = cuboidRegionStatement.executeQuery();
|
||||
cuboidResultSet = cuboidRegionStatement.executeQuery();
|
||||
|
||||
while (cuboidResultSet.next()) {
|
||||
Vector pt1 = new Vector(
|
||||
@ -378,6 +410,9 @@ private void loadCuboid() {
|
||||
logger.warning("\t\tCause: " + t.getMessage());
|
||||
t = t.getCause();
|
||||
}
|
||||
} finally {
|
||||
closeResource(cuboidResultSet);
|
||||
closeResource(cuboidRegionStatement);
|
||||
}
|
||||
|
||||
cuboidRegions = regions;
|
||||
@ -387,8 +422,11 @@ private void loadPoly2d() {
|
||||
Map<String,ProtectedRegion> regions =
|
||||
new HashMap<String,ProtectedRegion>();
|
||||
|
||||
PreparedStatement poly2dRegionStatement = null;
|
||||
ResultSet poly2dResultSet = null;
|
||||
PreparedStatement poly2dVectorStatement = null;
|
||||
try {
|
||||
PreparedStatement poly2dRegionStatement = this.conn.prepareStatement(
|
||||
poly2dRegionStatement = this.conn.prepareStatement(
|
||||
"SELECT " +
|
||||
"`region_poly2d`.`min_y`, " +
|
||||
"`region_poly2d`.`max_y`, " +
|
||||
@ -406,9 +444,9 @@ private void loadPoly2d() {
|
||||
);
|
||||
|
||||
poly2dRegionStatement.setInt(1, this.worldDbId);
|
||||
ResultSet poly2dResultSet = poly2dRegionStatement.executeQuery();
|
||||
poly2dResultSet = poly2dRegionStatement.executeQuery();
|
||||
|
||||
PreparedStatement poly2dVectorStatement = this.conn.prepareStatement(
|
||||
poly2dVectorStatement = this.conn.prepareStatement(
|
||||
"SELECT " +
|
||||
"`region_poly2d_point`.`x`, " +
|
||||
"`region_poly2d_point`.`z` " +
|
||||
@ -439,6 +477,8 @@ private void loadPoly2d() {
|
||||
continue;
|
||||
}
|
||||
|
||||
closeResource(poly2dVectorResultSet);
|
||||
|
||||
ProtectedRegion region = new ProtectedPolygonalRegion(id, points, minY, maxY);
|
||||
|
||||
region.setPriority(poly2dResultSet.getInt("priority"));
|
||||
@ -461,6 +501,10 @@ private void loadPoly2d() {
|
||||
logger.warning("\t\tCause: " + t.getMessage());
|
||||
t = t.getCause();
|
||||
}
|
||||
} finally {
|
||||
closeResource(poly2dResultSet);
|
||||
closeResource(poly2dRegionStatement);
|
||||
closeResource(poly2dVectorStatement);
|
||||
}
|
||||
|
||||
poly2dRegions = regions;
|
||||
@ -518,8 +562,11 @@ private Map<String,Integer> getUserIds(String... usernames) {
|
||||
|
||||
if (usernames.length < 1) return users;
|
||||
|
||||
ResultSet findUsersResults = null;
|
||||
PreparedStatement insertUserStatement = null;
|
||||
PreparedStatement findUsersStatement = null;
|
||||
try {
|
||||
PreparedStatement findUsersStatement = this.conn.prepareStatement(
|
||||
findUsersStatement = this.conn.prepareStatement(
|
||||
String.format(
|
||||
"SELECT " +
|
||||
"`user`.`id`, " +
|
||||
@ -532,13 +579,13 @@ private Map<String,Integer> getUserIds(String... usernames) {
|
||||
|
||||
RegionDBUtil.setValues(findUsersStatement, usernames);
|
||||
|
||||
ResultSet findUsersResults = findUsersStatement.executeQuery();
|
||||
findUsersResults = findUsersStatement.executeQuery();
|
||||
|
||||
while(findUsersResults.next()) {
|
||||
users.put(findUsersResults.getString("name"), findUsersResults.getInt("id"));
|
||||
}
|
||||
|
||||
PreparedStatement insertUserStatement = this.conn.prepareStatement(
|
||||
insertUserStatement = this.conn.prepareStatement(
|
||||
"INSERT INTO " +
|
||||
"`user` ( " +
|
||||
"`id`, " +
|
||||
@ -567,6 +614,10 @@ private Map<String,Integer> getUserIds(String... usernames) {
|
||||
logger.warning(t.getMessage());
|
||||
t = t.getCause();
|
||||
}
|
||||
} finally {
|
||||
closeResource(findUsersResults);
|
||||
closeResource(findUsersStatement);
|
||||
closeResource(insertUserStatement);
|
||||
}
|
||||
|
||||
return users;
|
||||
@ -582,8 +633,11 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
|
||||
|
||||
if (groupnames.length < 1) return groups;
|
||||
|
||||
PreparedStatement findGroupsStatement = null;
|
||||
ResultSet findGroupsResults = null;
|
||||
PreparedStatement insertGroupStatement = null;
|
||||
try {
|
||||
PreparedStatement findGroupsStatement = this.conn.prepareStatement(
|
||||
findGroupsStatement = this.conn.prepareStatement(
|
||||
String.format(
|
||||
"SELECT " +
|
||||
"`group`.`id`, " +
|
||||
@ -596,13 +650,13 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
|
||||
|
||||
RegionDBUtil.setValues(findGroupsStatement, groupnames);
|
||||
|
||||
ResultSet findGroupsResults = findGroupsStatement.executeQuery();
|
||||
findGroupsResults = findGroupsStatement.executeQuery();
|
||||
|
||||
while(findGroupsResults.next()) {
|
||||
groups.put(findGroupsResults.getString("name"), findGroupsResults.getInt("id"));
|
||||
}
|
||||
|
||||
PreparedStatement insertGroupStatement = this.conn.prepareStatement(
|
||||
insertGroupStatement = this.conn.prepareStatement(
|
||||
"INSERT INTO " +
|
||||
"`group` ( " +
|
||||
"`id`, " +
|
||||
@ -625,6 +679,10 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logger.warning("Could not get the database id for the groups " + groupnames.toString() + ex.getMessage());
|
||||
} finally {
|
||||
closeResource(findGroupsResults);
|
||||
closeResource(findGroupsStatement);
|
||||
closeResource(insertGroupStatement);
|
||||
}
|
||||
|
||||
return groups;
|
||||
@ -655,21 +713,26 @@ public void save() throws ProtectionDatabaseException {
|
||||
|
||||
List<String> regionsInDatabase = new ArrayList<String>();
|
||||
|
||||
PreparedStatement getAllRegionsStatement = null;
|
||||
ResultSet getAllRegionsResult = null;
|
||||
try {
|
||||
PreparedStatement getAllRegionsStatement = this.conn.prepareStatement(
|
||||
getAllRegionsStatement = this.conn.prepareStatement(
|
||||
"SELECT `region`.`id` FROM " +
|
||||
"`region` " +
|
||||
"WHERE `world_id` = ? "
|
||||
);
|
||||
|
||||
getAllRegionsStatement.setInt(1, this.worldDbId);
|
||||
ResultSet getAllRegionsResult = getAllRegionsStatement.executeQuery();
|
||||
getAllRegionsResult = getAllRegionsStatement.executeQuery();
|
||||
|
||||
while(getAllRegionsResult.next()) {
|
||||
regionsInDatabase.add(getAllRegionsResult.getString("id"));
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logger.warning("Could not get region list for save comparison: " + ex.getMessage());
|
||||
} finally {
|
||||
closeResource(getAllRegionsResult);
|
||||
closeResource(getAllRegionsStatement);
|
||||
}
|
||||
|
||||
for (Map.Entry<String, ProtectedRegion> entry : regions.entrySet()) {
|
||||
@ -707,10 +770,11 @@ public void save() throws ProtectionDatabaseException {
|
||||
}
|
||||
|
||||
for (Map.Entry<String, ProtectedRegion> entry : regions.entrySet()) {
|
||||
PreparedStatement setParentStatement = null;
|
||||
try {
|
||||
if (entry.getValue().getParent() == null) continue;
|
||||
|
||||
PreparedStatement setParentStatement = this.conn.prepareStatement(
|
||||
setParentStatement = this.conn.prepareStatement(
|
||||
"UPDATE `region` SET " +
|
||||
"`parent` = ? " +
|
||||
"WHERE `id` = ? AND `world_id` = " + this.worldDbId
|
||||
@ -723,12 +787,15 @@ public void save() throws ProtectionDatabaseException {
|
||||
} catch (SQLException ex) {
|
||||
logger.warning("Could not save region parents " + entry.getValue().getId().toLowerCase() + ": " + ex.getMessage());
|
||||
throw new ProtectionDatabaseException(ex);
|
||||
} finally {
|
||||
closeResource(setParentStatement);
|
||||
}
|
||||
}
|
||||
|
||||
for (String name : regionsInDatabase) {
|
||||
PreparedStatement removeRegion = null;
|
||||
try {
|
||||
PreparedStatement removeRegion = this.conn.prepareStatement(
|
||||
removeRegion = this.conn.prepareStatement(
|
||||
"DELETE FROM `region` WHERE `id` = ? "
|
||||
);
|
||||
|
||||
@ -736,13 +803,17 @@ public void save() throws ProtectionDatabaseException {
|
||||
removeRegion.execute();
|
||||
} catch (SQLException ex) {
|
||||
logger.warning("Could not remove region from database " + name + ": " + ex.getMessage());
|
||||
} finally {
|
||||
closeResource(removeRegion);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void updateFlags(ProtectedRegion region) throws SQLException {
|
||||
PreparedStatement clearCurrentFlagStatement = this.conn.prepareStatement(
|
||||
PreparedStatement clearCurrentFlagStatement = null;
|
||||
try {
|
||||
clearCurrentFlagStatement = this.conn.prepareStatement(
|
||||
"DELETE FROM `region_flag` " +
|
||||
"WHERE `region_id` = ? " +
|
||||
"AND `world_id` = " + this.worldDbId
|
||||
@ -756,7 +827,9 @@ private void updateFlags(ProtectedRegion region) throws SQLException {
|
||||
|
||||
Object flag = sqlMarshal(marshalFlag(entry.getKey(), entry.getValue()));
|
||||
|
||||
PreparedStatement insertFlagStatement = this.conn.prepareStatement(
|
||||
PreparedStatement insertFlagStatement = null;
|
||||
try {
|
||||
insertFlagStatement = this.conn.prepareStatement(
|
||||
"INSERT INTO `region_flag` ( " +
|
||||
"`id`, " +
|
||||
"`region_id`, " +
|
||||
@ -771,6 +844,12 @@ private void updateFlags(ProtectedRegion region) throws SQLException {
|
||||
insertFlagStatement.setObject(3, flag);
|
||||
|
||||
insertFlagStatement.execute();
|
||||
} finally {
|
||||
closeResource(insertFlagStatement);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
closeResource(clearCurrentFlagStatement);
|
||||
}
|
||||
}
|
||||
|
||||
@ -783,7 +862,13 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
|
||||
domain = region.getMembers();
|
||||
}
|
||||
|
||||
PreparedStatement deleteUsersForRegion = this.conn.prepareStatement(
|
||||
PreparedStatement deleteUsersForRegion = null;
|
||||
PreparedStatement insertUsersForRegion = null;
|
||||
PreparedStatement deleteGroupsForRegion = null;
|
||||
PreparedStatement insertGroupsForRegion = null;
|
||||
|
||||
try {
|
||||
deleteUsersForRegion = this.conn.prepareStatement(
|
||||
"DELETE FROM `region_players` " +
|
||||
"WHERE `region_id` = ? " +
|
||||
"AND `world_id` = " + this.worldDbId + " " +
|
||||
@ -794,7 +879,7 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
|
||||
deleteUsersForRegion.setBoolean(2, owners);
|
||||
deleteUsersForRegion.execute();
|
||||
|
||||
PreparedStatement insertUsersForRegion = this.conn.prepareStatement(
|
||||
insertUsersForRegion = this.conn.prepareStatement(
|
||||
"INSERT INTO `region_players` " +
|
||||
"(`region_id`, `world_id`, `user_id`, `owner`) " +
|
||||
"VALUES (?, " + this.worldDbId + ", ?, ?)"
|
||||
@ -810,7 +895,7 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
|
||||
insertUsersForRegion.execute();
|
||||
}
|
||||
|
||||
PreparedStatement deleteGroupsForRegion = this.conn.prepareStatement(
|
||||
deleteGroupsForRegion = this.conn.prepareStatement(
|
||||
"DELETE FROM `region_groups` " +
|
||||
"WHERE `region_id` = ? " +
|
||||
"AND `world_id` = " + this.worldDbId + " " +
|
||||
@ -821,7 +906,7 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
|
||||
deleteGroupsForRegion.setBoolean(2, owners);
|
||||
deleteGroupsForRegion.execute();
|
||||
|
||||
PreparedStatement insertGroupsForRegion = this.conn.prepareStatement(
|
||||
insertGroupsForRegion = this.conn.prepareStatement(
|
||||
"INSERT INTO `region_groups` " +
|
||||
"(`region_id`, `world_id`, `group_id`, `owner`) " +
|
||||
"VALUES (?, " + this.worldDbId + ", ?, ?)"
|
||||
@ -835,6 +920,12 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
|
||||
|
||||
insertGroupsForRegion.execute();
|
||||
}
|
||||
} finally {
|
||||
closeResource(deleteGroupsForRegion);
|
||||
closeResource(deleteUsersForRegion);
|
||||
closeResource(insertGroupsForRegion);
|
||||
closeResource(insertUsersForRegion);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -843,7 +934,9 @@ private <V> Object marshalFlag(Flag<V> flag, Object val) {
|
||||
}
|
||||
|
||||
private void insertRegion(ProtectedRegion region, String type) throws SQLException {
|
||||
PreparedStatement insertRegionStatement = this.conn.prepareStatement(
|
||||
PreparedStatement insertRegionStatement = null;
|
||||
try {
|
||||
insertRegionStatement = this.conn.prepareStatement(
|
||||
"INSERT INTO `region` (" +
|
||||
"`id`, " +
|
||||
"`world_id`, " +
|
||||
@ -859,6 +952,9 @@ private void insertRegion(ProtectedRegion region, String type) throws SQLExcepti
|
||||
insertRegionStatement.setInt(4, region.getPriority());
|
||||
|
||||
insertRegionStatement.execute();
|
||||
} finally {
|
||||
closeResource(insertRegionStatement);
|
||||
}
|
||||
|
||||
updateFlags(region);
|
||||
|
||||
@ -869,7 +965,9 @@ private void insertRegion(ProtectedRegion region, String type) throws SQLExcepti
|
||||
private void insertRegionCuboid(ProtectedCuboidRegion region) throws SQLException {
|
||||
insertRegion(region, "cuboid");
|
||||
|
||||
PreparedStatement insertCuboidRegionStatement = this.conn.prepareStatement(
|
||||
PreparedStatement insertCuboidRegionStatement = null;
|
||||
try {
|
||||
insertCuboidRegionStatement = this.conn.prepareStatement(
|
||||
"INSERT INTO `region_cuboid` (" +
|
||||
"`region_id`, " +
|
||||
"`world_id`, " +
|
||||
@ -894,12 +992,17 @@ private void insertRegionCuboid(ProtectedCuboidRegion region) throws SQLExceptio
|
||||
insertCuboidRegionStatement.setInt(7, max.getBlockX());
|
||||
|
||||
insertCuboidRegionStatement.execute();
|
||||
} finally {
|
||||
closeResource(insertCuboidRegionStatement);
|
||||
}
|
||||
}
|
||||
|
||||
private void insertRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException {
|
||||
insertRegion(region, "poly2d");
|
||||
|
||||
PreparedStatement insertPoly2dRegionStatement = this.conn.prepareStatement(
|
||||
PreparedStatement insertPoly2dRegionStatement = null;
|
||||
try {
|
||||
insertPoly2dRegionStatement = this.conn.prepareStatement(
|
||||
"INSERT INTO `region_poly2d` (" +
|
||||
"`region_id`, " +
|
||||
"`world_id`, " +
|
||||
@ -913,12 +1016,19 @@ private void insertRegionPoly2D(ProtectedPolygonalRegion region) throws SQLExcep
|
||||
insertPoly2dRegionStatement.setInt(3, region.getMinimumPoint().getBlockY());
|
||||
|
||||
insertPoly2dRegionStatement.execute();
|
||||
} finally {
|
||||
closeResource(insertPoly2dRegionStatement);
|
||||
}
|
||||
|
||||
updatePoly2dPoints(region);
|
||||
}
|
||||
|
||||
private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLException {
|
||||
PreparedStatement clearPoly2dPointsForRegionStatement = this.conn.prepareStatement(
|
||||
PreparedStatement clearPoly2dPointsForRegionStatement = null;
|
||||
PreparedStatement insertPoly2dPointStatement = null;
|
||||
|
||||
try {
|
||||
clearPoly2dPointsForRegionStatement = this.conn.prepareStatement(
|
||||
"DELETE FROM `region_poly2d_point` " +
|
||||
"WHERE `region_id` = ? " +
|
||||
"AND `world_id` = " + this.worldDbId
|
||||
@ -928,7 +1038,7 @@ private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLExcep
|
||||
|
||||
clearPoly2dPointsForRegionStatement.execute();
|
||||
|
||||
PreparedStatement insertPoly2dPointStatement = this.conn.prepareStatement(
|
||||
insertPoly2dPointStatement = this.conn.prepareStatement(
|
||||
"INSERT INTO `region_poly2d_point` (" +
|
||||
"`id`, " +
|
||||
"`region_id`, " +
|
||||
@ -946,6 +1056,10 @@ private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLExcep
|
||||
|
||||
insertPoly2dPointStatement.execute();
|
||||
}
|
||||
} finally {
|
||||
closeResource(clearPoly2dPointsForRegionStatement);
|
||||
closeResource(insertPoly2dPointStatement);
|
||||
}
|
||||
}
|
||||
|
||||
private void insertRegionGlobal(GlobalProtectedRegion region) throws SQLException {
|
||||
@ -953,7 +1067,9 @@ private void insertRegionGlobal(GlobalProtectedRegion region) throws SQLExceptio
|
||||
}
|
||||
|
||||
private void updateRegion(ProtectedRegion region, String type) throws SQLException {
|
||||
PreparedStatement updateRegionStatement = this.conn.prepareStatement(
|
||||
PreparedStatement updateRegionStatement = null;
|
||||
try {
|
||||
updateRegionStatement = this.conn.prepareStatement(
|
||||
"UPDATE `region` SET " +
|
||||
"`priority` = ? WHERE `id` = ? AND `world_id` = " + this.worldDbId
|
||||
);
|
||||
@ -962,6 +1078,9 @@ private void updateRegion(ProtectedRegion region, String type) throws SQLExcepti
|
||||
updateRegionStatement.setString(2, region.getId().toLowerCase());
|
||||
|
||||
updateRegionStatement.execute();
|
||||
} finally {
|
||||
closeResource(updateRegionStatement);
|
||||
}
|
||||
|
||||
updateFlags(region);
|
||||
|
||||
@ -972,7 +1091,9 @@ private void updateRegion(ProtectedRegion region, String type) throws SQLExcepti
|
||||
private void updateRegionCuboid(ProtectedCuboidRegion region) throws SQLException {
|
||||
updateRegion(region, "cuboid");
|
||||
|
||||
PreparedStatement updateCuboidRegionStatement = this.conn.prepareStatement(
|
||||
PreparedStatement updateCuboidRegionStatement = null;
|
||||
try {
|
||||
updateCuboidRegionStatement = this.conn.prepareStatement(
|
||||
"UPDATE `region_cuboid` SET " +
|
||||
"`min_z` = ?, " +
|
||||
"`min_y` = ?, " +
|
||||
@ -996,12 +1117,17 @@ private void updateRegionCuboid(ProtectedCuboidRegion region) throws SQLExceptio
|
||||
updateCuboidRegionStatement.setString(7, region.getId().toLowerCase());
|
||||
|
||||
updateCuboidRegionStatement.execute();
|
||||
} finally {
|
||||
closeResource(updateCuboidRegionStatement);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException {
|
||||
updateRegion(region, "poly2d");
|
||||
|
||||
PreparedStatement updatePoly2dRegionStatement = this.conn.prepareStatement(
|
||||
PreparedStatement updatePoly2dRegionStatement = null;
|
||||
try {
|
||||
updatePoly2dRegionStatement = this.conn.prepareStatement(
|
||||
"UPDATE `region_poly2d` SET " +
|
||||
"`max_y` = ?, " +
|
||||
"`min_y` = ? " +
|
||||
@ -1014,7 +1140,9 @@ private void updateRegionPoly2D(ProtectedPolygonalRegion region) throws SQLExcep
|
||||
updatePoly2dRegionStatement.setString(3, region.getId().toLowerCase());
|
||||
|
||||
updatePoly2dRegionStatement.execute();
|
||||
|
||||
} finally {
|
||||
closeResource(updatePoly2dRegionStatement);
|
||||
}
|
||||
updatePoly2dPoints(region);
|
||||
}
|
||||
|
||||
@ -1022,6 +1150,22 @@ private void updateRegionGlobal(GlobalProtectedRegion region) throws SQLExceptio
|
||||
updateRegion(region, "global");
|
||||
}
|
||||
|
||||
private void closeResource(ResultSet rs) {
|
||||
if (rs != null) {
|
||||
try {
|
||||
rs.close();
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
private void closeResource(Statement st) {
|
||||
if (st != null) {
|
||||
try {
|
||||
st.close();
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, ProtectedRegion> getRegions() {
|
||||
return regions;
|
||||
|
Loading…
Reference in New Issue
Block a user