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.

This commit is contained in:
Ty Sayers 2015-06-03 20:08:34 -04:00
parent 1fa919e954
commit e15a2ad123
10 changed files with 143 additions and 7 deletions

View File

@ -42,6 +42,7 @@ public class Clans extends JavaPlugin
private CoreClientManager _clientManager;
private DonationManager _donationManager;
@Override
public void onEnable()
{

View File

@ -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)

View File

@ -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<ClientClan> 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<ClientClan> 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<ClientClan> 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)
@ -225,6 +232,15 @@ public class ClansManager extends MiniClientPlugin<ClientClan> 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<String, ClanTerritory> getClaimMap()
{
return _claimMap;

View File

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

View File

@ -76,6 +76,7 @@ public class ClanRepository extends RepositoryBase
public Collection<ClanToken> retrieveClans()
{
System.out.println("Beginning to load clans from database...");
final NautHashMap<String, ClanToken> clans = new NautHashMap<String, ClanToken>();
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
@ -135,6 +138,8 @@ public class ClanRepository extends RepositoryBase
}, new ColumnVarChar("serverName", 100, _serverName));
System.out.println("2");
executeQuery(RETRIEVE_CLAN_ALLIANCE_INFO, new ResultSetCallable()
{
@Override
@ -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
@ -195,6 +202,7 @@ public class ClanRepository extends RepositoryBase
}, new ColumnVarChar("serverName", 100, _serverName));
System.out.println("Finished loading clans from database...");
return clans.values();
}

View File

@ -70,7 +70,7 @@ public class WeightSet<T>
{
roll -= weight.getWeight();
if (roll <= 0)
if (roll < 0)
{
return weight.getValue();
}

View File

@ -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<Location>(new Location(null, -200, 107, 0), new Location(null, 200, 107, 0));
_spawns = new WeightSet<Location>(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<Location> getSpawnLocations()
{
return _spawns.elements();
}
public boolean isInSpawn(Location location)
{
for(Location spawn : _spawns.elements())

View File

@ -14,6 +14,7 @@ public enum Region
EU,
ALL;
/**
* @return the geographical {@link Region} of the current running process.
*/

View File

@ -1,6 +1,5 @@
package mineplex.serverdata.data;
public interface Data
{
/**

View File

@ -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.