From 8f1da698d2f4b1ce23a2effd2217c33d831a31d5 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Sat, 29 Aug 2015 14:37:35 -0700 Subject: [PATCH] TNT generator storage in mysql (needs testing still) --- .../clans/clans/ClansDataAccessLayer.java | 41 ++++++++++++++++++- .../clans/repository/ClanRepository.java | 8 ++++ .../tntGenerator/TntGeneratorManager.java | 30 +++++++++++--- 3 files changed, 73 insertions(+), 6 deletions(-) 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 d8b33aca2..ebfb9ed7a 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 @@ -21,6 +21,7 @@ import mineplex.game.clans.clans.repository.ClanTerritory; import mineplex.game.clans.clans.repository.tokens.ClanEnemyToken; import mineplex.game.clans.clans.repository.tokens.ClanToken; import mineplex.game.clans.clans.scoreboard.ClansScoreboardManager; +import mineplex.game.clans.clans.tntGenerator.TntGenerator; public class ClansDataAccessLayer { @@ -430,7 +431,7 @@ public class ClansDataAccessLayer @Override public void run() { - _repository.addTerritoryClaims(clan.getId(), safe, chunks); + _repository.addTerritoryClaims(clan.getId(), safe, chunks); //Log _manager.log("Successfully added [" + chunks.length + "] Claims for [" + name + "] by [" + player + "]."); @@ -611,6 +612,44 @@ public class ClansDataAccessLayer //Log _manager.log("Updated Enemy Data for [" + clan.getName() + ", " + otherClan.getName() + "]"); } + + public void updateGenerator(final ClanInfo clanInfo, final Callback callback) + { + TntGenerator generator = clanInfo.getGenerator(); + final String location; + final int generatorStock; + + if (generator != null) + { + location = UtilWorld.blockToStr(generator.getBlock()); + generatorStock = generator.getStock(); + } + else + { + location = ""; + generatorStock = 0; + } + + runAsync(new Runnable() + { + @Override + public void run() + { + final boolean ran = _repository.updateClanGenerator(clanInfo.getId(), location, generatorStock); + runSync(new Runnable() + { + @Override + public void run() + { + if (callback != null) + { + callback.run(ran); + } + } + }); + } + }); + } public void updateEnergy(ClanInfo clan) { 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 566e32b52..2696addd0 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 @@ -22,6 +22,7 @@ import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnTimestamp; import mineplex.core.database.column.ColumnVarChar; import mineplex.database.tables.records.ClansRecord; +import mineplex.game.clans.clans.ClanInfo; import mineplex.game.clans.clans.repository.tokens.ClanAllianceToken; import mineplex.game.clans.clans.repository.tokens.ClanEnemyToken; import mineplex.game.clans.clans.repository.tokens.ClanMemberToken; @@ -66,6 +67,7 @@ public class ClanRepository extends RepositoryBase private static String UPDATE_CLAN_ALLIANCE = "UPDATE clanAlliances SET trusted = ? WHERE clanId = ? AND otherClanId = ?;"; private static String UPDATE_CLAN_ENEMY = "UPDATE clanEnemies SET clanScore = ?, otherClanScore = ?, clanKills = ?, otherClanKills = ? WHERE clanId = ? AND otherClanId = ?;"; private static String UPDATE_CLAN_TERRITORY = "UPDATE clanTerritory SET safe = ? WHERE serverId = ? AND chunk = ?;"; + private static String UPDATE_CLAN_GENERATOR = "UPDATE clans SET generator = ?, generatorCount = ? WHERE id = ?;"; private static String GET_CLAN_SERVER = "SELECT id FROM clanServer WHERE clanServer.serverName = ?"; private static String ADD_CLAN_SERVER = "INSERT INTO clanServer (serverName) VALUES (?);"; @@ -414,6 +416,12 @@ public class ClanRepository extends RepositoryBase new ColumnInt("otherClanKills", otherClanKills), new ColumnInt("clanId", clanId), new ColumnInt("otherClanId", otherClanId)); } + public boolean updateClanGenerator(int clanId, String generator, int generatorStock) + { + return executeUpdate(UPDATE_CLAN_GENERATOR, new ColumnVarChar("generator", 140, generator), new ColumnInt("generatorStock", generatorStock), new ColumnInt("clanId", clanId)) + > 0; + } + public void updateTerritoryClaim(String chunk, boolean safe) { executeUpdate(UPDATE_CLAN_TERRITORY, new ColumnBoolean("safe", safe), new ColumnInt("serverId", _serverId), new ColumnVarChar("chunk", 100, chunk)); diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/tntGenerator/TntGeneratorManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/tntGenerator/TntGeneratorManager.java index a9c62633c..b5ef3b8f6 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/tntGenerator/TntGeneratorManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/tntGenerator/TntGeneratorManager.java @@ -17,6 +17,7 @@ import net.minecraft.server.v1_7_R4.TileEntityBrewingStand; import mineplex.core.MiniPlugin; import mineplex.core.common.util.C; +import mineplex.core.common.util.Callback; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilPlayer; import mineplex.core.common.util.UtilTextMiddle; @@ -59,6 +60,7 @@ public class TntGeneratorManager extends MiniPlugin if (generator.getBlock().getType() != GENERATOR_MATERIAL) { clanInfo.setGenerator(null); + _clansManager.getClanDataAccess().updateGenerator(clanInfo, null); continue; } @@ -69,6 +71,7 @@ public class TntGeneratorManager extends MiniPlugin _clansManager.messageClan(clanInfo, F.main("Clans", "Your " + F.elem("TNT Generator") + " has been destroyed due to the land no longer being claimed")); destroyGenerator(generator); clanInfo.setGenerator(null); + _clansManager.getClanDataAccess().updateGenerator(clanInfo, null); continue; } @@ -85,6 +88,7 @@ public class TntGeneratorManager extends MiniPlugin generator.setStock(generator.getStock() + 1); generator.setTicks(0); updateBrewingStand(generator); + _clansManager.getClanDataAccess().updateGenerator(clanInfo, null); } generator.incrementTicks(); @@ -129,6 +133,7 @@ public class TntGeneratorManager extends MiniPlugin TntGenerator generator = new TntGenerator(event.getBlock()); clan.setGenerator(generator); + _clansManager.getClanDataAccess().updateGenerator(clan, null); _clansManager.messageClan(clan, F.main("Clans", F.name(event.getPlayer().getName()) + " placed a " + F.elem("TNT Generator") + " at " + F.elem(UtilWorld.blockToStrClean(event.getBlock())))); } @@ -154,21 +159,36 @@ public class TntGeneratorManager extends MiniPlugin } @EventHandler - public void onInteract(PlayerInteractEvent event) + public void onInteract(final PlayerInteractEvent event) { if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock() != null && event.getClickedBlock().getType() == GENERATOR_MATERIAL) { ClanInfo clan = getGenerator(event.getClickedBlock()); if (clan != null) { - TntGenerator generator = clan.getGenerator(); - int stock = generator.getStock(); + final TntGenerator generator = clan.getGenerator(); + final int stock = generator.getStock(); if (stock > 0) { generator.setStock(stock - 1); - updateBrewingStand(generator); - dropItem(event.getClickedBlock(), new ItemStack(Material.TNT)); + _clansManager.getClanDataAccess().updateGenerator(clan, new Callback() + { + @Override + public void run(Boolean data) + { + if (data) + { + updateBrewingStand(generator); + dropItem(event.getClickedBlock(), new ItemStack(Material.TNT)); + } + else + { + // failed + generator.setStock(stock); + } + } + }); } else {