Merge remote-tracking branch 'origin/clans-beta' into clans-beta

This commit is contained in:
Shaun Bennett 2015-08-26 18:35:20 -07:00
commit a5400b0fef
7 changed files with 136 additions and 53 deletions

View File

@ -193,39 +193,12 @@ public class DonationRepository extends RepositoryBase
{ {
public void run() public void run()
{ {
try ( boolean success = executeUpdate(UPDATE_ACCOUNT_GOLD, new ColumnInt("gold", gold), new ColumnInt("id", accountId)) > 0;
Connection connection = getConnection(); callback.run(success);
CallableStatement statement = connection.prepareCall("{call accountGoldChange(?, ?, ?)}");
)
{
statement.setInt(1, accountId);
statement.setInt(2, gold);
statement.registerOutParameter(3, Types.BOOLEAN);
Boolean hasResults = statement.execute();
if (hasResults)
{
ResultSet resultSet = statement.getResultSet();
while (resultSet.next())
{
callback.run(resultSet.getBoolean(1));
}
}
}
catch (SQLException exception)
{
exception.printStackTrace();
}
catch (Exception exception)
{
exception.printStackTrace();
}
} }
}), "Error updating player gold amount in DonationRepository : "); }), "Error updating player gold amount in DonationRepository : ");
} }
@Override @Override
protected void initialize() protected void initialize()
{ {

View File

@ -7,6 +7,7 @@ import mineplex.core.common.util.UtilPlayer;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -18,7 +19,7 @@ public class Farming extends MiniPlugin
} }
@EventHandler @EventHandler
public void BlockBreak(BlockPlaceEvent event) public void BlockBreak(BlockBreakEvent event)
{ {
if (event.isCancelled()) if (event.isCancelled())
return; return;

View File

@ -551,8 +551,7 @@ public class ClanInfo
public int getEnergyPurchasable() public int getEnergyPurchasable()
{ {
int diff = getEnergyMax() - getEnergy(); return Math.max(getEnergyMax() - getEnergy(), 0);
return diff > 0 ? diff : 0;
} }
public List<Player> getOnlinePlayers() public List<Player> getOnlinePlayers()

View File

@ -388,6 +388,45 @@ public class ClansDataAccessLayer
_manager.log("Added Neutral between [" + cA.getName() + "] and [" + cB.getName() + "] by [" + player + "]."); _manager.log("Added Neutral between [" + cA.getName() + "] and [" + cB.getName() + "] by [" + player + "].");
} }
public boolean claimAll(final String name, final String player, final boolean safe, final String... chunks)
{
if (!_manager.getClanMap().containsKey(name))
return false;
final ClanInfo clan = _manager.getClanMap().get(name);
//Unclaim
for (String chunk : chunks)
{
if (_manager.getClaimMap().containsKey(chunk))
{
unclaim(chunk, player, false);
}
//Memory
ClanTerritory claim = new ClanTerritory();
claim.Owner = name;
claim.Safe = safe;
clan.getClaimSet().add(chunk);
_manager.getClaimMap().put(chunk, claim);
}
//Save
runAsync(new Runnable()
{
@Override
public void run()
{
_repository.addTerritoryClaims(clan.getId(), safe, chunks);
//Log
_manager.log("Successfully added [" + chunks.length + "] Claims for [" + name + "] by [" + player + "].");
}
});
return true;
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public boolean claim(String name, final String chunk, String player, final boolean safe) public boolean claim(String name, final String chunk, String player, final boolean safe)
{ {
@ -436,7 +475,7 @@ public class ClansDataAccessLayer
} }
//Log //Log
_manager.log("Added Claim for [" + name + "] at [" + chunk + "] by [" + player + "]."); //_manager.log("Added Claim for [" + name + "] at [" + chunk + "] by [" + player + "].");
return true; return true;
} }

View File

@ -1,6 +1,7 @@
package mineplex.game.clans.clans.regions; package mineplex.game.clans.clans.regions;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -8,6 +9,7 @@ import java.util.Set;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -56,21 +58,35 @@ public class ClansRegions extends MiniPlugin
} }
public void initializeRegions() public void initializeRegions()
{
initializeRegions(false);
}
public void initializeRegions(boolean overrideCreation)
{ {
Location worldCenter = new Location(_world, 0, 0, 0); Location worldCenter = new Location(_world, 0, 0, 0);
// Initialize Spawn faction and claims // Initialize Spawn faction and claims
claimArea("Spawn", SPAWN_RADIUS, 0, true, overrideCreation, new Location[]{Spawn.getNorthSpawn(), Spawn.getSouthSpawn()}); claimArea("Spawn", SPAWN_RADIUS, 0, true, new Location[]{Spawn.getNorthSpawn(), Spawn.getSouthSpawn()});
claimArea("Shops", SHOP_RADIUS, 0, true, overrideCreation, new Location[]{Spawn.getEastShop(), Spawn.getWestShop()}); claimArea("Shops", SHOP_RADIUS, 0, true, new Location[]{Spawn.getEastShop(), Spawn.getWestShop()});
// Initialize Fields and Borderlands factions and claims // Initialize Fields and Borderlands factions and claims
claimArea("Fields", FIELDS_RADIUS, 0, false, overrideCreation, worldCenter); claimArea("Fields", FIELDS_RADIUS, 0, false, worldCenter);
claimArea("Borderlands", BORDERLANDS_RADIUS, 32, false, overrideCreation, worldCenter); claimArea("Borderlands", BORDERLANDS_RADIUS, 32, false, worldCenter);
//debugClan("Spawn");
//debugClan("Shops");
//debugClan("Fields");
//debugClan("Borderlands");
}
public void debugClan(String clanName)
{
ClanInfo clan = _manager.getClan(clanName);
if (clan != null)
{
System.out.println("Clan " + clanName + " has " + clan.getClaimSet().size() + " claims!");
}
else
{
System.out.println("NO CLAN FOUND BY NAME " + clanName);
}
} }
public void resetRegions() public void resetRegions()
@ -79,13 +95,13 @@ public class ClansRegions extends MiniPlugin
clearClaims("Shops"); clearClaims("Shops");
clearClaims("Fields"); clearClaims("Fields");
clearClaims("Borderlands"); clearClaims("Borderlands");
initializeRegions(true); initializeRegions();
} }
private void clearClaims(String name) private void clearClaims(String name)
{ {
System.out.println("Clearing claims for " + name + "!");
ClanInfo clan = _manager.getClan(name); ClanInfo clan = _manager.getClan(name);
System.out.println("Clearing claims for " + name + " with clan id " + clan.getId() + "!");
if (clan != null) if (clan != null)
{ {
@ -108,8 +124,7 @@ public class ClansRegions extends MiniPlugin
* @param claimOffset - the initial offset in claim (creating a 'hole' with chunk offset radius) * @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. * @param safe - whether the chunk claimed is considered a 'safe' (pvp-free) region.
*/ */
private void claimArea(final String clanName, final int chunkRadius, final int claimOffset, final boolean safe, private void claimArea(final String clanName, final int chunkRadius, final int claimOffset, final boolean safe, final Location... locations)
boolean overrideCreation, final Location... locations)
{ {
final ClanInfo clan = _manager.getClan(clanName); final ClanInfo clan = _manager.getClan(clanName);
@ -127,6 +142,8 @@ public class ClansRegions extends MiniPlugin
claimArea(data, location, chunkRadius, claimOffset, safe); claimArea(data, location, chunkRadius, claimOffset, safe);
log(String.format("Initialized %s faction territory and creation!", clanName)); log(String.format("Initialized %s faction territory and creation!", clanName));
} }
debugClan(clanName);
} }
else else
{ {
@ -136,7 +153,7 @@ public class ClansRegions extends MiniPlugin
} }
}); });
} }
else if (overrideCreation) else
{ {
for (Location location : locations) for (Location location : locations)
{ {
@ -149,6 +166,7 @@ public class ClansRegions extends MiniPlugin
{ {
int chunkX = location.getChunk().getX(); int chunkX = location.getChunk().getX();
int chunkZ = location.getChunk().getZ(); int chunkZ = location.getChunk().getZ();
Set<String> chunks = new HashSet<String>();
for (int xOffset = -chunkRadius; xOffset <= chunkRadius; xOffset++) for (int xOffset = -chunkRadius; xOffset <= chunkRadius; xOffset++)
{ {
@ -165,9 +183,14 @@ public class ClansRegions extends MiniPlugin
continue; continue;
} }
_manager.getClanDataAccess().claim(clan.getName(), chunkStr, "ClansRegions", safe); chunks.add(chunkStr);
} }
} }
if (chunks.size() > 0)
{
_manager.getClanDataAccess().claimAll(clan.getName(), "ClansRegions", safe, chunks.toArray(new String[0]));
}
} }
private void sendBorder(Player player) private void sendBorder(Player player)

View File

@ -1,7 +1,10 @@
package mineplex.game.clans.clans.repository; package mineplex.game.clans.clans.repository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Collection; import java.util.Collection;
import java.util.UUID; import java.util.UUID;
@ -13,6 +16,7 @@ import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.DBPool; import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase; import mineplex.core.database.RepositoryBase;
import mineplex.core.database.ResultSetCallable; import mineplex.core.database.ResultSetCallable;
import mineplex.core.database.column.Column;
import mineplex.core.database.column.ColumnBoolean; import mineplex.core.database.column.ColumnBoolean;
import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnInt;
import mineplex.core.database.column.ColumnTimestamp; import mineplex.core.database.column.ColumnTimestamp;
@ -23,6 +27,7 @@ import mineplex.game.clans.clans.repository.tokens.ClanEnemyToken;
import mineplex.game.clans.clans.repository.tokens.ClanMemberToken; import mineplex.game.clans.clans.repository.tokens.ClanMemberToken;
import mineplex.game.clans.clans.repository.tokens.ClanTerritoryToken; import mineplex.game.clans.clans.repository.tokens.ClanTerritoryToken;
import mineplex.game.clans.clans.repository.tokens.ClanToken; import mineplex.game.clans.clans.repository.tokens.ClanToken;
import org.jooq.DSLContext; import org.jooq.DSLContext;
import static mineplex.database.Tables.*; import static mineplex.database.Tables.*;
@ -337,6 +342,48 @@ public class ClanRepository extends RepositoryBase
{ {
return executeUpdate(ADD_CLAN_TERRITORY, new ColumnInt("clanId", clanId), new ColumnInt("serverId", _serverId), new ColumnVarChar("chunk", 100, chunk), new ColumnBoolean("safe", safe)) == 1; return executeUpdate(ADD_CLAN_TERRITORY, new ColumnInt("clanId", clanId), new ColumnInt("serverId", _serverId), new ColumnVarChar("chunk", 100, chunk), new ColumnBoolean("safe", safe)) == 1;
} }
public boolean addTerritoryClaims(int clanId, boolean safe, String... chunks)
{
int affectedRows = 0;
int size = chunks.length;
String query = "INSERT INTO clanTerritory (clanId, serverId, chunk, safe) VALUES";
for (int i = 0; i < size; i++)
{
query += " (?, ?, ?, ?)";
if (i < (size - 1))
query += ",";
}
query += ";";
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS))
{
Column<?> clanIdCol = new ColumnInt("clanId", clanId);
Column<?> serverIdCol = new ColumnInt("serverId", _serverId);
Column<?> safeCol = new ColumnBoolean("safe", safe);
int i = 0;
for (String chunk : chunks)
{
Column<?> chunkCol = new ColumnVarChar("chunk", 100, chunk);
clanIdCol.setValue(preparedStatement, i + 1);
serverIdCol.setValue(preparedStatement, i + 2);
chunkCol.setValue(preparedStatement, i + 3);
safeCol.setValue(preparedStatement, i + 4);
i += 4;
}
affectedRows = preparedStatement.executeUpdate();
}
catch (Exception exception)
{
exception.printStackTrace();
}
return affectedRows == size;
}
public void addEnemy(int clanId, int otherClanId) public void addEnemy(int clanId, int otherClanId)
{ {

View File

@ -174,17 +174,18 @@ public class GoldManager extends MiniPlugin
public void addGold(Player player, int amount) public void addGold(Player player, int amount)
{ {
if (amount >= 0) if (amount >= 0)
_donationManager.RewardGoldLater("GoldManager", player, amount); _donationManager.rewardGold(null, "GoldManager", player, amount);
} }
public void deductGold(Callback<Boolean> resultCallback, Player player, int amount) public void deductGold(Callback<Boolean> resultCallback, Player player, int amount)
{ {
_donationManager.rewardGold(resultCallback, "GoldManager", player, -amount); if (amount >= 0)
_donationManager.rewardGold(resultCallback, "GoldManager", player, -amount);
} }
public void cashIn(Player player, GoldToken token) public void cashIn(Player player, GoldToken token)
{ {
int value = token.getGoldValue(); int value = token.getGoldValue();
addGold(player, value); addGold(player, value);
notify(player, String.format("You have cashed in a gold token worth %dg!", value)); notify(player, String.format("You have cashed in a gold token worth %dg!", value));
} }