From e15a2ad12332a9229104aa62ae63687ec81cde46 Mon Sep 17 00:00:00 2001 From: Ty Sayers Date: Wed, 3 Jun 2015 20:08:34 -0400 Subject: [PATCH] Add ClansRegions to handle the initialization of default server-side Clans representing special territories such as Spawn and Fields. Additionally, claim land with appropriate distribution according to the respective Clan. Fix bug with WeightSet generation not properly handling low amounts of uniformly likely elements. Fix bug with ClanInfo not properly handling creation of clans and initializing the date created field. Fix bug with Spawn region not properly protecting from claim and PvP settings. --- .../src/mineplex/game/clans/Clans.java | 1 + .../mineplex/game/clans/clans/ClanInfo.java | 4 +- .../game/clans/clans/ClansManager.java | 18 ++- .../clans/clans/regions/ClansRegions.java | 104 ++++++++++++++++++ .../clans/repository/ClanRepository.java | 10 +- .../clans/items/generation/WeightSet.java | 2 +- .../src/mineplex/game/clans/spawn/Spawn.java | 8 +- .../src/mineplex/serverdata/Region.java | 1 + .../src/mineplex/serverdata/data/Data.java | 1 - .../serverdata/servers/ServerRepository.java | 1 + 10 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/regions/ClansRegions.java diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Clans.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Clans.java index b27796e68..fbd57fc59 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Clans.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Clans.java @@ -42,6 +42,7 @@ public class Clans extends JavaPlugin private CoreClientManager _clientManager; private DonationManager _donationManager; + @Override public void onEnable() { diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClanInfo.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClanInfo.java index 0dda88280..fc266e6ac 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClanInfo.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClanInfo.java @@ -36,7 +36,7 @@ public class ClanInfo private boolean _admin = false; - private Timestamp _dateCreated = null; + private Timestamp _dateCreated; private Timestamp _lastOnline = null; // Loaded from Client @@ -74,7 +74,7 @@ public class ClanInfo _energy = token.Energy; _admin = token.Admin; - _dateCreated = token.DateCreated; + _dateCreated = (token.DateCreated != null) ? token.DateCreated : new Timestamp(System.currentTimeMillis()); _lastOnline = token.LastOnline; for (ClanMemberToken memberToken : token.Members) diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java index 65eb96461..40759c6c4 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java @@ -38,6 +38,7 @@ import mineplex.game.clans.clans.commands.ClansAllyChatCommand; import mineplex.game.clans.clans.commands.ClansChatCommand; import mineplex.game.clans.clans.commands.ClansCommand; import mineplex.game.clans.clans.commands.ServerTimeCommand; +import mineplex.game.clans.clans.regions.ClansRegions; import mineplex.game.clans.clans.repository.ClanTerritory; import mineplex.game.clans.clans.repository.tokens.ClanMemberToken; import mineplex.game.clans.clans.repository.tokens.ClanTerritoryToken; @@ -77,6 +78,7 @@ public class ClansManager extends MiniClientPlugin implements IRelat private ClansAdmin _clanAdmin; private ClansGame _clanGame; private ClansBlocks _clanBlocks; + private ClansRegions _clanRegions; private BlockRestore _blockRestore; private Teleport _teleport; private ConditionManager _condition; @@ -167,6 +169,10 @@ public class ClansManager extends MiniClientPlugin implements IRelat for (ClanTerritoryToken territoryToken : token.Territories) _claimMap.put(territoryToken.Chunk, new ClanTerritory(territoryToken)); } + + // Initialize default region factions and territory (spawn/fields/borderlands) + _clanRegions = new ClansRegions(this); + _clanRegions.initializeRegions(); } @Override @@ -199,7 +205,8 @@ public class ClansManager extends MiniClientPlugin implements IRelat int z = Math.abs(location.getBlockZ()); return (x > FIELD_RADIUS || z > FIELD_RADIUS) - && (x <= CLAIMABLE_RADIUS && z <= CLAIMABLE_RADIUS); + && (x <= CLAIMABLE_RADIUS && z <= CLAIMABLE_RADIUS) + && !Spawn.getInstance().isInSpawn(location); } public static boolean isFields(Location location) @@ -224,6 +231,15 @@ public class ClansManager extends MiniClientPlugin implements IRelat { return _clanMap.get(clan); } + + /** + * @param clanName + * @return true, if a Clan with matching {@code clanName} exists, false otherwise. + */ + public boolean clanExists(String clanName) + { + return getClan(clanName) != null; + } public NautHashMap getClaimMap() { diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/regions/ClansRegions.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/regions/ClansRegions.java new file mode 100644 index 000000000..0d82fd183 --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/regions/ClansRegions.java @@ -0,0 +1,104 @@ +package mineplex.game.clans.clans.regions; + +import java.util.Set; + +import org.bukkit.Bukkit; +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.World; + +import mineplex.core.common.util.UtilWorld; +import mineplex.game.clans.clans.ClanInfo; +import mineplex.game.clans.clans.ClansDataAccessLayer; +import mineplex.game.clans.clans.ClansManager; +import mineplex.game.clans.spawn.Spawn; + +public class ClansRegions +{ + public final static String DEFAULT_WORLD_NAME = "world"; + public final static int SPAWN_RADIUS = 2; // Radius of spawn claim area (measured in chunks) + public final static int FIELDS_RADIUS = 8; // Radius of fields claim area (measured in chunks) + public final static int BORDERLANDS_RADIUS = 75; // Radius of borderlands claim area (measured in chunks) + + private ClansManager _manager; + private World _world; + + public ClansRegions(ClansManager manager, String worldName) + { + _manager = manager; + _world = Bukkit.getWorld(worldName); + } + + public ClansRegions(ClansManager manager) + { + this(manager, DEFAULT_WORLD_NAME); + } + + public void initializeRegions() + { + Location worldCenter = new Location(_world, 0, 0, 0); + + // Initialize Spawn faction and claims + Set spawns = Spawn.getInstance().getSpawnLocations(); + Location[] spawnsArray = spawns.toArray(new Location[spawns.size()]); + claimArea("Spawn", SPAWN_RADIUS, 0, true, spawnsArray); + + // Initialize Fields and Borderlands factions and claims + claimArea("Fields", FIELDS_RADIUS, 0, false, worldCenter); + claimArea("Borderlands", BORDERLANDS_RADIUS, 50, false, worldCenter); + } + + /** + * Initializes a server-side clan and claims area according to the + * location and radius properties passed in. + * @param clanName - the name of the clan to create/claim territory for + * @param location - the center location to begin the claim + * @param chunkRadius - the radius (in chunks) to claim + * @param claimOffset - the initial offset in claim (creating a 'hole' with chunk offset radius) + * @param safe - whether the chunk claimed is considered a 'safe' (pvp-free) region. + */ + private void claimArea(String clanName, int chunkRadius, int claimOffset, boolean safe, Location... locations) + { + ClanInfo clan = _manager.getClan(clanName); + + if (clan == null) + { + for (Location location : locations) + { + clan = _manager.getClanDataAccess().create("ClansRegions", clanName, true); + claimArea(clan, location, chunkRadius, claimOffset, safe); + log(String.format("Initialized %s faction territory and creation!", clanName)); + } + } + } + + private void claimArea(ClanInfo clan, Location location, int chunkRadius, int claimOffset, boolean safe) + { + int chunkX = location.getChunk().getX(); + int chunkZ = location.getChunk().getZ(); + + for (int xOffset = -chunkRadius; xOffset <= chunkRadius; xOffset++) + { + for (int zOffset = -chunkRadius; zOffset <= chunkRadius; zOffset++) + { + int x = chunkX + xOffset; + int z = chunkZ + zOffset; + Chunk chunk = location.getWorld().getChunkAt(x, z); + String chunkStr = UtilWorld.chunkToStr(chunk); + + if ((Math.abs(xOffset) < claimOffset && Math.abs(zOffset) < claimOffset) + || _manager.getClaimMap().containsKey(chunkStr)) + { + continue; + } + + _manager.getClanDataAccess().claim(clan.getName(), chunkStr, "ClansRegions", safe); + } + } + } + + private void log(String message) + { + _manager.log(message); + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/ClanRepository.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/ClanRepository.java index e22160e3c..b90f306a5 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/ClanRepository.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/ClanRepository.java @@ -76,6 +76,7 @@ public class ClanRepository extends RepositoryBase public Collection retrieveClans() { + System.out.println("Beginning to load clans from database..."); final NautHashMap clans = new NautHashMap(); executeQuery(RETRIEVE_START_CLAN_INFO, new ResultSetCallable() @@ -112,6 +113,8 @@ public class ClanRepository extends RepositoryBase }, new ColumnVarChar("serverName", 100, _serverName)); + System.out.println("1"); + executeQuery(RETRIEVE_CLAN_MEMBER_INFO, new ResultSetCallable() { @Override @@ -134,6 +137,8 @@ public class ClanRepository extends RepositoryBase } }, new ColumnVarChar("serverName", 100, _serverName)); + + System.out.println("2"); executeQuery(RETRIEVE_CLAN_ALLIANCE_INFO, new ResultSetCallable() { @@ -157,6 +162,8 @@ public class ClanRepository extends RepositoryBase }, new ColumnVarChar("serverName", 100, _serverName)); + System.out.println("3"); + executeQuery(RETRIEVE_CLAN_ENEMY_INFO, new ResultSetCallable() { @Override @@ -194,7 +201,8 @@ public class ClanRepository extends RepositoryBase } }, new ColumnVarChar("serverName", 100, _serverName)); - + + System.out.println("Finished loading clans from database..."); return clans.values(); } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/items/generation/WeightSet.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/items/generation/WeightSet.java index 68ee5f5ab..46c829c81 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/items/generation/WeightSet.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/items/generation/WeightSet.java @@ -70,7 +70,7 @@ public class WeightSet { roll -= weight.getWeight(); - if (roll <= 0) + if (roll < 0) { return weight.getValue(); } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/spawn/Spawn.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/spawn/Spawn.java index 8c0c0aea9..a542da1e5 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/spawn/Spawn.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/spawn/Spawn.java @@ -1,6 +1,7 @@ package mineplex.game.clans.spawn; import java.util.Random; +import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -35,7 +36,7 @@ public class Spawn extends MiniPlugin super("Clan Spawn Zones", plugin); _instance = this; - _spawns = new WeightSet(new Location(null, -200, 107, 0), new Location(null, 200, 107, 0)); + _spawns = new WeightSet(new Location(getSpawnWorld(), -200, 85, 0), new Location(getSpawnWorld(), 200, 71, 0)); } @EventHandler @@ -86,6 +87,11 @@ public class Spawn extends MiniPlugin return spawn; } + public Set getSpawnLocations() + { + return _spawns.elements(); + } + public boolean isInSpawn(Location location) { for(Location spawn : _spawns.elements()) diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Region.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Region.java index 5bc846b37..957c7a479 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Region.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Region.java @@ -14,6 +14,7 @@ public enum Region EU, ALL; + /** * @return the geographical {@link Region} of the current running process. */ diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/Data.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/Data.java index 34bb2ddda..d0e0c943b 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/Data.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/Data.java @@ -1,6 +1,5 @@ package mineplex.serverdata.data; - public interface Data { /** diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerRepository.java index 37b119766..51f5ad8b9 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerRepository.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerRepository.java @@ -17,6 +17,7 @@ import mineplex.serverdata.data.ServerGroup; public interface ServerRepository { + /** * @return a newly instanced snapshot {@link Collection} of all currently active * {@link MinecraftServer}s in the repository.