From c20d2c9516a04927b45686f301e0ea75050549e6 Mon Sep 17 00:00:00 2001 From: Ty Sayers Date: Wed, 26 Aug 2015 17:37:31 -0700 Subject: [PATCH] Fix bug with economy gold additions/reductions not properly working and causing shops to fail. Fix bug with region resetting command and streamline the claiming of server-based faction chunks for Spawn, Shops, Fields and Borderlands. --- .../repository/DonationRepository.java | 33 +---------- .../src/mineplex/game/clans/Farming.java | 3 +- .../mineplex/game/clans/clans/ClanInfo.java | 3 +- .../clans/clans/ClansDataAccessLayer.java | 41 +++++++++++++- .../clans/clans/regions/ClansRegions.java | 55 +++++++++++++------ .../clans/repository/ClanRepository.java | 47 ++++++++++++++++ .../game/clans/economy/GoldManager.java | 7 ++- 7 files changed, 136 insertions(+), 53 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java index 414f2aad7..83bfa6c25 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java @@ -193,39 +193,12 @@ public class DonationRepository extends RepositoryBase { public void run() { - try ( - Connection connection = getConnection(); - 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(); - } + boolean success = executeUpdate(UPDATE_ACCOUNT_GOLD, new ColumnInt("gold", gold), new ColumnInt("id", accountId)) > 0; + callback.run(success); } }), "Error updating player gold amount in DonationRepository : "); } - + @Override protected void initialize() { diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Farming.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Farming.java index 8abcc527d..f7145451e 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Farming.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Farming.java @@ -7,6 +7,7 @@ import mineplex.core.common.util.UtilPlayer; import org.bukkit.Material; import org.bukkit.event.EventHandler; +import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.plugin.java.JavaPlugin; @@ -18,7 +19,7 @@ public class Farming extends MiniPlugin } @EventHandler - public void BlockBreak(BlockPlaceEvent event) + public void BlockBreak(BlockBreakEvent event) { if (event.isCancelled()) return; 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 1c6d1a67b..9e721cdf8 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 @@ -551,8 +551,7 @@ public class ClanInfo public int getEnergyPurchasable() { - int diff = getEnergyMax() - getEnergy(); - return diff > 0 ? diff : 0; + return Math.max(getEnergyMax() - getEnergy(), 0); } public List getOnlinePlayers() diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansDataAccessLayer.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansDataAccessLayer.java index a6ec809db..36a5ce169 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansDataAccessLayer.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansDataAccessLayer.java @@ -388,6 +388,45 @@ public class ClansDataAccessLayer _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") public boolean claim(String name, final String chunk, String player, final boolean safe) { @@ -436,7 +475,7 @@ public class ClansDataAccessLayer } //Log - _manager.log("Added Claim for [" + name + "] at [" + chunk + "] by [" + player + "]."); + //_manager.log("Added Claim for [" + name + "] at [" + chunk + "] by [" + player + "]."); return true; } 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 index c2dc59644..2db1d2926 100644 --- 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 @@ -1,6 +1,7 @@ package mineplex.game.clans.clans.regions; import java.util.ArrayList; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; @@ -8,6 +9,7 @@ import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -56,21 +58,35 @@ public class ClansRegions extends MiniPlugin } public void initializeRegions() - { - initializeRegions(false); - } - - public void initializeRegions(boolean overrideCreation) { Location worldCenter = new Location(_world, 0, 0, 0); // Initialize Spawn faction and claims - claimArea("Spawn", SPAWN_RADIUS, 0, true, overrideCreation, new Location[]{Spawn.getNorthSpawn(), Spawn.getSouthSpawn()}); - claimArea("Shops", SHOP_RADIUS, 0, true, overrideCreation, new Location[]{Spawn.getEastShop(), Spawn.getWestShop()}); - + claimArea("Spawn", SPAWN_RADIUS, 0, true, new Location[]{Spawn.getNorthSpawn(), Spawn.getSouthSpawn()}); + claimArea("Shops", SHOP_RADIUS, 0, true, new Location[]{Spawn.getEastShop(), Spawn.getWestShop()}); + // Initialize Fields and Borderlands factions and claims - claimArea("Fields", FIELDS_RADIUS, 0, false, overrideCreation, worldCenter); - claimArea("Borderlands", BORDERLANDS_RADIUS, 32, false, overrideCreation, worldCenter); + claimArea("Fields", FIELDS_RADIUS, 0, false, 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() @@ -79,13 +95,13 @@ public class ClansRegions extends MiniPlugin clearClaims("Shops"); clearClaims("Fields"); clearClaims("Borderlands"); - initializeRegions(true); + initializeRegions(); } private void clearClaims(String name) { - System.out.println("Clearing claims for " + name + "!"); ClanInfo clan = _manager.getClan(name); + System.out.println("Clearing claims for " + name + " with clan id " + clan.getId() + "!"); 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 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, - boolean overrideCreation, final Location... locations) + private void claimArea(final String clanName, final int chunkRadius, final int claimOffset, final boolean safe, final Location... locations) { final ClanInfo clan = _manager.getClan(clanName); @@ -127,6 +142,8 @@ public class ClansRegions extends MiniPlugin claimArea(data, location, chunkRadius, claimOffset, safe); log(String.format("Initialized %s faction territory and creation!", clanName)); } + + debugClan(clanName); } else { @@ -136,7 +153,7 @@ public class ClansRegions extends MiniPlugin } }); } - else if (overrideCreation) + else { for (Location location : locations) { @@ -149,6 +166,7 @@ public class ClansRegions extends MiniPlugin { int chunkX = location.getChunk().getX(); int chunkZ = location.getChunk().getZ(); + Set chunks = new HashSet(); for (int xOffset = -chunkRadius; xOffset <= chunkRadius; xOffset++) { @@ -165,9 +183,14 @@ public class ClansRegions extends MiniPlugin 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) 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 8ccaabbf0..566e32b52 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 @@ -1,7 +1,10 @@ package mineplex.game.clans.clans.repository; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.sql.Timestamp; import java.util.Collection; import java.util.UUID; @@ -13,6 +16,7 @@ import mineplex.core.common.util.NautHashMap; import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; +import mineplex.core.database.column.Column; import mineplex.core.database.column.ColumnBoolean; import mineplex.core.database.column.ColumnInt; 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.ClanTerritoryToken; import mineplex.game.clans.clans.repository.tokens.ClanToken; + import org.jooq.DSLContext; 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; } + + 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) { diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/economy/GoldManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/economy/GoldManager.java index f28d0a278..f536769fd 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/economy/GoldManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/economy/GoldManager.java @@ -174,17 +174,18 @@ public class GoldManager extends MiniPlugin public void addGold(Player player, int amount) { if (amount >= 0) - _donationManager.RewardGoldLater("GoldManager", player, amount); + _donationManager.rewardGold(null, "GoldManager", player, amount); } public void deductGold(Callback 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) { - int value = token.getGoldValue(); + int value = token.getGoldValue(); addGold(player, value); notify(player, String.format("You have cashed in a gold token worth %dg!", value)); }