From e25accfba4a7205b5ea3325f316ee4db13be8cb6 Mon Sep 17 00:00:00 2001 From: NewGarbo Date: Mon, 15 Feb 2016 11:24:29 +0000 Subject: [PATCH] outpost saving and loadi9ng --- .../core/database/RepositoryBase.java | 5 +- .../clans/core/repository/ClanRepository.java | 5 + .../game/clans/clans/ClansManager.java | 8 +- .../game/clans/clans/ClansUtility.java | 13 + .../game/clans/clans/siege/SiegeManager.java | 113 ++----- .../CommandSiegeSupplies.java | 4 +- .../clans/clans/siege/outpost/Outpost.java | 287 ++++++++---------- .../clans/siege/outpost/OutpostBlock.java | 57 ---- .../clans/siege/outpost/OutpostBuilder.java | 18 -- .../clans/siege/outpost/OutpostManager.java | 166 +++++++--- .../clans/siege/outpost/OutpostState.java | 38 ++- .../clans/siege/outpost/OutpostType.java | 31 +- .../siege/outpost/build/OutpostBlock.java | 82 +++++ .../{ => build}/OutpostBlockBanner.java | 8 +- .../siege/repository/OutpostRepository.java | 152 ++++++++++ .../repository/SiegeWeaponRepository.java | 124 ++++++++ .../tokens}/OutpostToken.java | 16 +- .../repository/tokens/SiegeWeaponToken.java | 22 ++ .../game/clans/clans/siege/weapon/Cannon.java | 14 +- .../clans/clans/siege/weapon/Catapult.java | 14 +- .../clans/clans/siege/weapon/SiegeWeapon.java | 121 ++------ .../serialization/SiegeWeaponToken.java | 23 -- .../siege/weapon/serialization/TokenInfo.java | 6 - .../game/clans/gameplay/Gameplay.java | 7 +- Plugins/database-config.dat | 4 + Plugins/redis-config.dat | 4 + 26 files changed, 800 insertions(+), 542 deletions(-) rename Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/{outpost => commands}/CommandSiegeSupplies.java (86%) delete mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBlock.java delete mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBuilder.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/build/OutpostBlock.java rename Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/{ => build}/OutpostBlockBanner.java (82%) create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/OutpostRepository.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/SiegeWeaponRepository.java rename Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/{weapon/serialization => repository/tokens}/OutpostToken.java (54%) create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/tokens/SiegeWeaponToken.java delete mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/SiegeWeaponToken.java delete mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/TokenInfo.java create mode 100644 Plugins/database-config.dat create mode 100644 Plugins/redis-config.dat diff --git a/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java index 55acc83ed..2df578f9b 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java @@ -146,7 +146,10 @@ public abstract class RepositoryBase implements Listener try (ResultSet resultSet = statement.executeQuery()) { - callable.processResultSet(resultSet); + if (callable != null) + { + callable.processResultSet(resultSet); + } } } catch (SQLException exception) diff --git a/Plugins/Mineplex.Game.Clans.Core/src/mineplex/game/clans/core/repository/ClanRepository.java b/Plugins/Mineplex.Game.Clans.Core/src/mineplex/game/clans/core/repository/ClanRepository.java index 70f9d082b..061306b40 100644 --- a/Plugins/Mineplex.Game.Clans.Core/src/mineplex/game/clans/core/repository/ClanRepository.java +++ b/Plugins/Mineplex.Game.Clans.Core/src/mineplex/game/clans/core/repository/ClanRepository.java @@ -560,6 +560,11 @@ public class ClanRepository extends RepositoryBase // new ColumnInt("clanScore", clanScore), new ColumnInt("otherClanScore", otherClanScore), new ColumnInt("clanKills", clanKills), // new ColumnInt("otherClanKills", otherClanKills), new ColumnInt("clanId", clanId), new ColumnInt("otherClanId", otherClanId)); // } + + public int getServerId() + { + return _serverId; + } public boolean updateClanGenerator(int clanId, String generator, int generatorStock) { 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 682afca84..81891c6b7 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 @@ -256,7 +256,6 @@ public class ClansManager extends MiniClientPluginimplements IRelati _itemMapManager = new ItemMapManager(this, _worldEvent); new TntGeneratorManager(plugin, this); new SupplyDropManager(plugin, this); - new SiegeManager(plugin, this); _explosion = new Explosion(plugin, blockRestore); _warPointEvasion = new WarPointEvasion(plugin); @@ -383,6 +382,8 @@ public class ClansManager extends MiniClientPluginimplements IRelati } Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "Replay|Restrict"); + + new SiegeManager(plugin, this); } @Override @@ -1174,4 +1175,9 @@ public class ClansManager extends MiniClientPluginimplements IRelati return null; } + + public int getServerId() + { + return _clanDataAccess.getRepository().getServerId(); + } } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansUtility.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansUtility.java index 4e466128c..a57f0470e 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansUtility.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansUtility.java @@ -206,6 +206,19 @@ public class ClansUtility return Clans.getClan(clan); } + public ClanInfo getClanById(int id) + { + for (ClanInfo clan : Clans.getClanMap().values()) + { + if (clan.getId() == id) + { + return clan; + } + } + + return null; + } + public ClanInfo getClanByPlayer(Player player) { return getClanByUUID(player.getUniqueId()); diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/SiegeManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/SiegeManager.java index 1e9621533..bd4b3a8e0 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/SiegeManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/SiegeManager.java @@ -4,8 +4,6 @@ import java.util.HashMap; import java.util.Map; import org.bukkit.Location; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.event.EventHandler; @@ -14,26 +12,24 @@ import org.bukkit.event.entity.ProjectileHitEvent; import org.bukkit.plugin.java.JavaPlugin; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import mineplex.core.MiniPlugin; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilInv; +import mineplex.core.common.util.UtilMath; import mineplex.core.common.util.UtilPlayer; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import mineplex.game.clans.clans.ClanInfo; import mineplex.game.clans.clans.ClansBlacklist; import mineplex.game.clans.clans.ClansManager; -import mineplex.game.clans.clans.siege.outpost.CommandSiegeSupplies; +import mineplex.game.clans.clans.siege.commands.CommandSiegeSupplies; import mineplex.game.clans.clans.siege.outpost.Outpost; import mineplex.game.clans.clans.siege.outpost.OutpostManager; import mineplex.game.clans.clans.siege.weapon.Cannon; import mineplex.game.clans.clans.siege.weapon.Catapult; import mineplex.game.clans.clans.siege.weapon.SiegeWeapon; -import mineplex.game.clans.clans.siege.weapon.serialization.SiegeWeaponToken; import mineplex.game.clans.core.repository.ClanTerritory; -import mineplex.game.clans.spawn.Spawn; public class SiegeManager extends MiniPlugin { @@ -43,7 +39,7 @@ public class SiegeManager extends MiniPlugin public static SiegeManager Instance; - public Map LiveSiegeWeapons = new HashMap<>(); + public Map LiveSiegeWeapons = new HashMap<>(); private Gson _gson; @@ -57,98 +53,32 @@ public class SiegeManager extends MiniPlugin _outpostManager = new OutpostManager(clans, this); - _outpostManager.loadExistingOutposts(); - - loadExistingWeapons(); - addCommand(new CommandSiegeSupplies(_outpostManager)); Instance = this; + + _outpostManager.loadOutposts(); + loadSiegeWeapons(); } @EventHandler - public void onUpdate(UpdateEvent event) + public void save(UpdateEvent event) { if (event.getType() == UpdateType.SLOWER) { - _outpostManager.loadExistingOutposts(); - - loadExistingWeapons(); + _outpostManager.saveOutposts(); + saveSiegeWeapons(); } } - private void loadExistingWeapons() + private void loadSiegeWeapons() { - try - { - for (Entity entity : Spawn.getSpawnWorld().getEntities()) - { - if (!(entity instanceof ArmorStand)) - { - continue; - } - - if (!entity.hasMetadata("$SERIALIZED_SIEGE_WEAPON_DATA$") ||!entity.hasMetadata("$TOKEN_INFO$")) - { - continue; - } - - String data = entity.getMetadata("$SERIALIZED_SIEGE_WEAPON_DATA$").get(0).asString(); - - SiegeWeaponToken token = _gson.fromJson(data, SiegeWeaponToken.class); - - if (LiveSiegeWeapons.containsKey(token.Id)) - { - continue; - } - - if (_clans.getClan(token.OwnerClan) == null) - { - for (Entity other : Spawn.getSpawnWorld().getEntities()) - { - if (token.ComprisedOf.values().contains(other.getUniqueId())) - { - other.remove(); - } - else if (other.getUniqueId().equals(token.DataEntityUUID)) - { - other.remove(); - } - } - - continue; - } - - SiegeWeapon weapon = null; - - switch (token.WeaponType) - { - case 1: - { - weapon = new Catapult(this, token); - break; - } - - case 2: - { - weapon = new Cannon(this, token); - break; - } - } - - LiveSiegeWeapons.put(token.Id, weapon); - - if (_outpostManager.Get(token.OutpostId) != null) - { - _outpostManager.Get(token.OutpostId).addWeapon(weapon); - } - - } - } - catch (Exception e) - { - e.printStackTrace(); - } + + } + + private void saveSiegeWeapons() + { + } @EventHandler @@ -280,7 +210,7 @@ public class SiegeManager extends MiniPlugin outpost.addWeapon(cannon); } - LiveSiegeWeapons.put(cannon.getId(), cannon); + LiveSiegeWeapons.put(cannon.getUniqueId(), cannon); } public boolean trySpawnCatapult(Player player, Location location) @@ -343,12 +273,12 @@ public class SiegeManager extends MiniPlugin outpost.addWeapon(catapult); } - LiveSiegeWeapons.put(catapult.getId(), catapult); + LiveSiegeWeapons.put(catapult.getUniqueId(), catapult); } public void dead(SiegeWeapon weapon) { - LiveSiegeWeapons.remove(weapon.getId()); + LiveSiegeWeapons.remove(weapon.getUniqueId()); } public OutpostManager getOutpostManager() @@ -366,4 +296,9 @@ public class SiegeManager extends MiniPlugin return _clans; } + public int randomId() + { + return Math.abs(UtilMath.random.nextInt()); + } + } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/CommandSiegeSupplies.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/commands/CommandSiegeSupplies.java similarity index 86% rename from Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/CommandSiegeSupplies.java rename to Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/commands/CommandSiegeSupplies.java index 4de674758..3e9f4cae1 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/CommandSiegeSupplies.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/commands/CommandSiegeSupplies.java @@ -1,4 +1,4 @@ -package mineplex.game.clans.clans.siege.outpost; +package mineplex.game.clans.clans.siege.commands; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -7,6 +7,8 @@ import mineplex.core.command.CommandBase; import mineplex.core.common.Rank; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilPlayer; +import mineplex.game.clans.clans.siege.outpost.Outpost; +import mineplex.game.clans.clans.siege.outpost.OutpostManager; import mineplex.game.clans.clans.siege.weapon.Cannon; import mineplex.game.clans.clans.siege.weapon.Catapult; diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/Outpost.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/Outpost.java index 41269da61..fed50eb70 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/Outpost.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/Outpost.java @@ -1,25 +1,17 @@ package mineplex.game.clans.clans.siege.outpost; -import java.io.File; -import java.io.FileWriter; -import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; -import java.util.Random; -import java.util.UUID; -import java.util.Map.Entry; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.block.Chest; -import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Arrow; -import org.bukkit.entity.Entity; import org.bukkit.entity.FallingBlock; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -49,6 +41,7 @@ import mineplex.core.common.util.UtilBlock; import mineplex.core.common.util.UtilColor; import mineplex.core.common.util.UtilEvent; import mineplex.core.common.util.UtilEvent.ActionType; +import mineplex.core.common.util.UtilInv; import mineplex.core.common.util.UtilItem; import mineplex.core.common.util.UtilMath; import mineplex.core.common.util.UtilParticle; @@ -67,8 +60,9 @@ import mineplex.core.updater.event.UpdateEvent; import mineplex.game.clans.clans.ClanInfo; import mineplex.game.clans.clans.ClansBlacklist; import mineplex.game.clans.clans.siege.events.SiegeWeaponExplodeEvent; +import mineplex.game.clans.clans.siege.outpost.build.OutpostBlock; +import mineplex.game.clans.clans.siege.repository.tokens.OutpostToken; import mineplex.game.clans.clans.siege.weapon.SiegeWeapon; -import mineplex.game.clans.clans.siege.weapon.serialization.OutpostToken; import mineplex.game.clans.core.repository.ClanTerritory; import net.minecraft.server.v1_8_R3.AxisAlignedBB; @@ -81,11 +75,11 @@ public class Outpost implements Listener public static final long PREP_TIME = 2 * 60 * 1000; - private OutpostManager _host; + private OutpostManager _outpostManager; - private final String _id; + private final int _uniqueId; - private ClanInfo _owner; + private ClanInfo _ownerClan; private Location _startCorner; private Location _origin; @@ -120,32 +114,32 @@ public class Outpost implements Listener UtilColor.rgb(147, 212, 17), UtilColor.rgb(150, 199, 51))); - private long _spawnTime; + private long _timeSpawned; - public double _maxHealth = 4500; - public double _health = _maxHealth; + public int _maxHealth = 4500; + public int _health = _maxHealth; - public ClanInfo _against; + public ClanInfo _againstClan; private long _siegeDeclaredTime = -1; private Arrow _declarationArrow; - private ArmorStand _dataEntity; - public Outpost(OutpostManager outpostManager, OutpostToken token) { - _host = outpostManager; + _outpostManager = outpostManager; - _health = token.Health; + System.out.println(token.OutpostState + ", " + token.Type); + + setHealth(token.Health); _siegeDeclaredTime = token.SiegeDeclaredTime; - _against = outpostManager.getClansManager().getClan(token.AgainstClan); + _againstClan = token.AgainstClan; - _id = token.Id; + _uniqueId = token.UniqueId; _siegeWeaponDistance = token.Type._size + 27.5; - _owner = outpostManager.getClansManager().getClan(token.OwnerClan); + _ownerClan = token.OwnerClan; _startCorner = token.Origin.clone().subtract(token.Type._size, 1.1, token.Type._size); _endCorner = token.Origin.clone().add(token.Type._size + .9, token.Type._ySize - 1, token.Type._size + .9); @@ -155,42 +149,43 @@ public class Outpost implements Listener _forceFieldStart = _startCorner.clone().subtract(4, 0, 4); _forceFieldEnd = _endCorner.clone().add(4.5, 0, 4.5); - _origin = token.Origin; + _origin = token.Origin.clone(); _type = token.Type; - _spawnTime = token.TimeSpawned; + _timeSpawned = token.TimeSpawned; _core = _type.getCoreLocation(_origin); - _preHologram = new Hologram(_owner.Clans.getHologramManager(), _origin.clone().add(0.5, 2.3, 0.5), F.elem(_owner.getName()) + C.cWhite + "'s Outpost block (Right-Click to activate)"); - _preHologram2 = new Hologram(_owner.Clans.getHologramManager(), _origin.clone().add(0.5, 3, 0.5), "Despawning: " + UtilText.getProgress(null, 0, null, true)); + _preHologram = new Hologram(_ownerClan.Clans.getHologramManager(), _origin.clone().add(0.5, 2.3, 0.5), F.elem(_ownerClan.getName()) + C.cWhite + "'s Outpost block (Right-Click to activate)"); + _preHologram2 = new Hologram(_ownerClan.Clans.getHologramManager(), _origin.clone().add(0.5, 3, 0.5), "Despawning: " + UtilText.getProgress(null, 0, null, true)); - _preHologram.start(); - _preHologram2.start(); + if (token.OutpostState == OutpostState.AWAITING) + { + _preHologram.start(); + _preHologram2.start(); + } + + _blocks = _type.createBuildQueue(_origin, _ownerClan.Clans); _state = token.OutpostState; - for (Entity entity : token.Origin.getWorld().getEntities()) - { - if (entity.getUniqueId().equals(token.DataEntityUUID)) - { - _dataEntity = (ArmorStand) entity; - } - } + _circleStages = new LoopIterator(circleAround(new Vector(0., 0., 0.), 40, .6d)); + + List reverse = circleAround(new Vector(0., 0., 0.), 40, .6d); + Collections.reverse(reverse); + _reverseCircleStages = new LoopIterator(reverse); } - public Outpost(OutpostManager host, ClanInfo clan, Location location, OutpostType type) + public Outpost(OutpostManager outpostManager, ClanInfo clan, Location location, OutpostType type) { - _host = host; + _outpostManager = outpostManager; - location.add(.5, 0, .5); - - _id = Integer.toString(UtilMath.random.nextInt(13333337)); + _uniqueId = outpostManager.getSiegeManager().randomId(); _siegeWeaponDistance = type._size + 27.5; - _owner = clan; + _ownerClan = clan; _startCorner = location.clone().subtract(type._size, 1.1, type._size); _endCorner = location.clone().add(type._size + .9, type._ySize - 1, type._size + .9); @@ -204,77 +199,29 @@ public class Outpost implements Listener _type = type; - _spawnTime = System.currentTimeMillis(); + _timeSpawned = System.currentTimeMillis(); _core = _type.getCoreLocation(_origin); - _preHologram = new Hologram(_owner.Clans.getHologramManager(), _origin.clone().add(0.5, 2.3, 0.5), F.elem(_owner.getName()) + C.cWhite + "'s Outpost block (Right-Click to activate)"); - _preHologram2 = new Hologram(_owner.Clans.getHologramManager(), _origin.clone().add(0.5, 3, 0.5), "Despawning: " + UtilText.getProgress(null, 0, null, true)); + _preHologram = new Hologram(_ownerClan.Clans.getHologramManager(), _origin.clone().add(0.5, 2.3, 0.5), F.elem(_ownerClan.getName()) + C.cWhite + "'s Outpost block (Right-Click to activate)"); + _preHologram2 = new Hologram(_ownerClan.Clans.getHologramManager(), _origin.clone().add(0.5, 3, 0.5), "Despawning: " + UtilText.getProgress(null, 0, null, true)); _preHologram.start(); _preHologram2.start(); - _dataEntity = location.getWorld().spawn(location.subtract(0, 5, 0), ArmorStand.class); - _dataEntity.setVisible(false); - _dataEntity.setGravity(false); - _state = OutpostState.AWAITING; + + _outpostManager.getRepository().insertOutpost(toToken()); + + _circleStages = new LoopIterator(circleAround(new Vector(0., 0., 0.), 40, .6d)); + + List reverse = circleAround(new Vector(0., 0., 0.), 40, .6d); + Collections.reverse(reverse); + _reverseCircleStages = new LoopIterator(reverse); + + UtilServer.registerEvents(this); } - public OutpostToken tokenize() - { - OutpostToken token = new OutpostToken(); - - token.Id = _id; - - token.Origin = _origin; - - token.Type = _type; - - token.OwnerClan = _owner.getName(); - - token.Health = _health; - - token.TimeSpawned = _spawnTime; - - token.AgainstClan = _against != null ? _against.getName() : null; - - token.DataEntityUUID = _dataEntity.getUniqueId(); - - token.SiegeDeclaredTime = _siegeDeclaredTime; - - token.OutpostState = _state; - - return token; - } - - private void updateData() - { - try - { - String data = _host.getSiegeManager().getGson().toJson(tokenize()); - - PrintWriter writer = new PrintWriter(new FileWriter(new File("H:\\" + new Random().nextInt()))); - writer.print(data); - writer.close(); - - _dataEntity.setMetadata("$SERIALIZED_OUTPOST_DATA$", new FixedMetadataValue(_host.getPlugin(), data)); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - - @EventHandler - public void saveData(UpdateEvent event) - { - if (event.getType() == UpdateType.SEC_05) - { - updateData(); - } - } - private void cleanup() { _blocks = null; @@ -290,9 +237,7 @@ public class Outpost implements Listener _weapons.forEach(SiegeWeapon::kill); _weapons.clear(); - _dataEntity.remove(); - - _host.queueForRemoval(_owner.getName()); + _outpostManager.queueForRemoval(_ownerClan.getName()); } @EventHandler @@ -313,7 +258,7 @@ public class Outpost implements Listener return; } - if (!_owner.equals(_owner.Clans.getClanUtility().getClanByPlayer(event.getPlayer()))) + if (!_ownerClan.equals(_ownerClan.Clans.getClanUtility().getClanByPlayer(event.getPlayer()))) { UtilPlayer.message(event.getPlayer(), F.main("Clans", "This is not yours to activate!")); return; @@ -337,13 +282,12 @@ public class Outpost implements Listener return; } - event.setCancelled(true); Player player = (Player) event.getEntity(); Arrow arrow = player.shootArrow(); - if (_owner.isMember(player)) + if (_ownerClan.isMember(player)) { for (int slot = 0; slot < player.getInventory().getSize(); slot++) { @@ -356,13 +300,13 @@ public class Outpost implements Listener if (item.isSimilar(SIEGE_DECLARATION_ARROW)) { - if (_against == null && !arrow.hasMetadata("OutpostData")) + if (_againstClan == null && !arrow.hasMetadata("OutpostData")) { - arrow.setMetadata("OutpostData", new FixedMetadataValue(_host.getPlugin(), _owner.getName() + ";" + player.getName())); + arrow.setMetadata("OutpostData", new FixedMetadataValue(_outpostManager.getPlugin(), _ownerClan.getName() + ";" + player.getName())); _declarationArrow = (Arrow) arrow; + player.getInventory().setItem(slot, UtilInv.decrement(item)); + break; } - - player.getInventory().setItem(slot, null); } } } @@ -373,12 +317,12 @@ public class Outpost implements Listener { if (event.getBlock().getLocation().equals(_core) && getState() == OutpostState.LIVE) { - UtilPlayer.message(event.getPlayer(), F.main("Clans", "You have destroyed " + F.elem(_owner.getName()) + "'s Outpost!")); + UtilPlayer.message(event.getPlayer(), F.main("Clans", "You have destroyed " + F.elem(_ownerClan.getName()) + "'s Outpost!")); _core.getBlock().setType(Material.AIR); - _owner.inform("Your Outpost has been destroyed!", null); - UtilTextMiddle.display("Siege", "Your Outpost has been destroyed", 20, 100, 20, _owner.getOnlinePlayersArray()); + _ownerClan.inform("Your Outpost has been destroyed!", null); + UtilTextMiddle.display("Siege", "Your Outpost has been destroyed", 20, 100, 20, _ownerClan.getOnlinePlayersArray()); if (getState() == OutpostState.AWAITING) { @@ -400,7 +344,7 @@ public class Outpost implements Listener { _origin.getBlock().setType(Material.AIR); _origin.getWorld().dropItem(_origin, OUTPOST_ITEM); - _owner.inform("Your Outpost block has been destroyed.", null); + _ownerClan.inform("Your Outpost block has been destroyed.", null); cleanup(); event.setCancelled(true); return; @@ -436,14 +380,14 @@ public class Outpost implements Listener UtilParticle.PlayParticle(ParticleType.MOB_SPELL, _declarationArrow.getLocation(), new Vector(color.getRed(), color.getGreen(), color.getBlue()), 1f, 0, ViewDist.MAX); } - if (_against != null && getTimeToSiege() > 0) + if (_againstClan != null && getTimeToSiege() > 150) { - _against.getOnlinePlayers().forEach(this::informTimeToSiege); - _owner.getOnlinePlayers().forEach(this::informTimeToSiege); + _againstClan.getOnlinePlayers().forEach(this::informTimeToSiege); + _ownerClan.getOnlinePlayers().forEach(this::informTimeToSiege); } - else if (_against != null) + else if (_againstClan != null) { - UtilTextBottom.display("Enemy Outpost Health: " + UtilText.colorPercentage(getHealthPercentage()) + "%", _against.getOnlinePlayersArray()); + UtilTextBottom.display("Enemy Outpost Health: " + UtilText.colorPercentage(getHealthPercentage()) + "%", _againstClan.getOnlinePlayersArray()); } if (_state == OutpostState.AWAITING) @@ -451,7 +395,7 @@ public class Outpost implements Listener if (getLifetime() > 60000) { _origin.getBlock().setType(Material.AIR); - _owner.inform("You have lost your Outpost block, as no one activated it fast enough!", null); + _ownerClan.inform("You have lost your Outpost block, as no one activated it fast enough!", null); cleanup(); return; } @@ -482,11 +426,11 @@ public class Outpost implements Listener for (OutpostBlock block : _blocks.values()) { - if (block._id == Material.CHEST.getId()) + if (block.getId() == Material.CHEST.getId()) { for (int slot = 0; slot < 8; slot++) { - ((Chest) block._loc.getBlock().getState()).getInventory().setItem(slot, SIEGE_DECLARATION_ARROW); + ((Chest) block.getLocation().getBlock().getState()).getInventory().setItem(slot, SIEGE_DECLARATION_ARROW); } break; @@ -554,7 +498,7 @@ public class Outpost implements Listener } UtilServer.getPlayersCollection().stream() - .filter(player -> !_owner.isMember(player)) + .filter(player -> !_ownerClan.isMember(player)) .filter(player -> UtilAlg.inBoundingBox(player.getLocation(), _forceFieldStart, _forceFieldEnd)) .forEach(player -> { UtilAction.velocity(player, UtilAlg.getTrajectory2d(_core, player.getLocation()), .4, true, 0.8, 0, 1.1, true); @@ -573,26 +517,20 @@ public class Outpost implements Listener _preHologram = null; _state = OutpostState.CONSTRUCTING; - _blocks = new LinkedHashMap<>(_buildQueue = _type.createBuildQueue(_origin, _owner.Clans)); + _blocks = new LinkedHashMap<>(_buildQueue = _type.createBuildQueue(_origin, _ownerClan.Clans)); - _owner.inform("Siege", "Your Outpost is now being constructed.", null); - - _circleStages = new LoopIterator(circleAround(new Vector(0., 0., 0.), 40, .6d)); - - List reverse = circleAround(new Vector(0., 0., 0.), 40, .6d); - Collections.reverse(reverse); - _reverseCircleStages = new LoopIterator(reverse); + _ownerClan.inform("Siege", "Your Outpost is now being constructed.", null); //Inform nearby Clans for (int chunkX = -3; chunkX < 3; chunkX++) { for (int chunkZ = -3; chunkZ < 3; chunkZ++) { - ClanTerritory territory = _owner.Clans.getClanUtility().getClaim(_origin.getWorld().getChunkAt(_origin.getChunk().getX() + chunkX, _origin.getChunk().getZ() + chunkZ)); + ClanTerritory territory = _ownerClan.Clans.getClanUtility().getClaim(_origin.getWorld().getChunkAt(_origin.getChunk().getX() + chunkX, _origin.getChunk().getZ() + chunkZ)); if (territory != null && ClansBlacklist.isValidClanName(territory.Owner)) { - ClanInfo clan = _owner.Clans.getClanUtility().getClanByClanName(territory.Owner); + ClanInfo clan = _ownerClan.Clans.getClanUtility().getClanByClanName(territory.Owner); clan.inform("A siege has begun near your territory!", null); UtilTextMiddle.display("Siege", "A Siege has been declared on your Clan!", 20, 100, 20, clan.getOnlinePlayersArray()); @@ -623,15 +561,15 @@ public class Outpost implements Listener NonFinalInteger wait = new NonFinalInteger(0); _blocks.values().stream().filter(block -> UtilMath.random.nextBoolean()).filter(block -> UtilMath.random.nextBoolean()).limit(20).forEach(block -> - _host.runSyncLater(() -> { - UtilParticle.PlayParticle(ParticleType.HUGE_EXPLOSION, block._loc, new Vector(0,0,0), 1f, 1, ViewDist.MAX); - _origin.getWorld().playSound(block._loc, Sound.EXPLODE, 1.0f, 1.0f); + _outpostManager.runSyncLater(() -> { + UtilParticle.PlayParticle(ParticleType.HUGE_EXPLOSION, block.getLocation(), new Vector(0,0,0), 1f, 1, ViewDist.MAX); + _origin.getWorld().playSound(block.getLocation(), Sound.EXPLODE, 1.0f, 1.0f); }, wait.add(4 + UtilMath.random.nextInt(4)).get()) ); - _host.runSyncLater(() -> { + _outpostManager.runSyncLater(() -> { _blocks.values().forEach(block -> { - Material mat = Material.getMaterial(block._id); + Material mat = Material.getMaterial(block.getId()); if (UtilItem.isTranslucent(mat)) { @@ -639,13 +577,13 @@ public class Outpost implements Listener return; } - FallingBlock fall = block._loc.getWorld().spawnFallingBlock(block._loc, block._id, block._data); + FallingBlock fall = block.getLocation().getWorld().spawnFallingBlock(block.getLocation(), block.getId(), block.getData()); fall.setDropItem(false); Vector vec = UtilAlg.getTrajectory(fall.getLocation(), getExactMiddle()); UtilAction.velocity(fall, vec, 1, false, 0, 0.6, 10, false); - fall.setMetadata("ClansOutpost", new FixedMetadataValue(_owner.Clans.getPlugin(), _owner.getName())); + fall.setMetadata("ClansOutpost", new FixedMetadataValue(_ownerClan.Clans.getPlugin(), _ownerClan.getName())); block.restore(); }); @@ -653,7 +591,7 @@ public class Outpost implements Listener cleanup(); }, wait.get() + 5L); - _owner.inform("Your Clan's Outpost has been destroyed.", null); + _ownerClan.inform("Your Clan's Outpost has been destroyed.", null); } @EventHandler @@ -672,12 +610,12 @@ public class Outpost implements Listener public ClanInfo getOwner() { - return _owner; + return _ownerClan; } public long getLifetime() { - return System.currentTimeMillis() - _spawnTime; + return System.currentTimeMillis() - _timeSpawned; } public AxisAlignedBB getBounds() @@ -705,20 +643,20 @@ public class Outpost implements Listener public final int getHealthPercentage() { - return ((int) ((_health / _maxHealth) * 100.d)); + return ((int) ((((double) _health) / ((double) _maxHealth)) * 100.d)); } - public final double getHealth() + public final int getHealth() { return _health; } public final String getDisplayHealth() { - return UtilText.getProgress(null, _health / _maxHealth, null, false, 12); + return UtilText.getProgress(null, ((double) _health) / ((double) _maxHealth), null, false, 12); } - public final void setHealth(double health) + public final void setHealth(int health) { _health = UtilMath.clamp(health, 0, _maxHealth); @@ -728,51 +666,51 @@ public class Outpost implements Listener } } - public final void removeHealth(double health) + public final void removeHealth(int health) { setHealth(_health - health); } - public final void addHealth(double health) + public final void addHealth(int health) { setHealth(_health + health); } public ClanInfo getAgainst() { - return _against; + return _againstClan; } public void declareOn(Player declarer, ClanInfo against) { - if (_against != null) + if (_againstClan != null) { - UtilPlayer.message(declarer, F.main("Clans", "Your Clan has already declared Siege against " + F.elem(_against.getName()) + ".")); + UtilPlayer.message(declarer, F.main("Clans", "Your Clan has already declared Siege against " + F.elem(_againstClan.getName()) + ".")); } else { - if (_host.isInSiege(against)) + if (_outpostManager.isInSiege(against)) { UtilPlayer.message(declarer, F.main("Clans", F.elem(against.getName()) + " is already in a Siege.")); return; } - _against = against; + _againstClan = against; _siegeDeclaredTime = System.currentTimeMillis(); - _owner.inform(F.main("Clans", "Your Clan has declared a Siege against " + F.elem(_against.getName()) + "."), null); - _owner.inform("Siege", "Your Clan has declared a Siege", null); - _owner.inform(F.main("Clans", F.time(UtilTime.MakeStr(PREP_TIME)) + " to Siege."), null); + _ownerClan.inform(F.main("Clans", "Your Clan has declared a Siege against " + F.elem(_againstClan.getName()) + "."), null); + _ownerClan.inform("Siege", "Your Clan has declared a Siege", null); + _ownerClan.inform(F.main("Clans", F.time(UtilTime.MakeStr(PREP_TIME)) + " to Siege."), null); - _against.inform(F.main("Clans", F.elem(_owner.getName()) + " has declared a Siege against your Clan."), null); - _against.inform(F.main("Clans", F.time(UtilTime.MakeStr(PREP_TIME)) + " to Siege."), null); - _against.inform("Siege", F.elem(_owner.getName()) + " has declared a Siege", null); + _againstClan.inform(F.main("Clans", F.elem(_ownerClan.getName()) + " has declared a Siege against your Clan."), null); + _againstClan.inform(F.main("Clans", F.time(UtilTime.MakeStr(PREP_TIME)) + " to Siege."), null); + _againstClan.inform("Siege", F.elem(_ownerClan.getName()) + " has declared a Siege", null); } } public void informTimeToSiege(Player player) { - UtilTextBottom.display("Time to Siege: " + F.time(UtilTime.MakeStr(getTimeToSiege())), player); + UtilTextBottom.display(C.Bold + "Time to Siege: " + F.time(UtilTime.MakeStr(getTimeToSiege())), player); } public long getTimeSiegeDeclared() @@ -800,8 +738,25 @@ public class Outpost implements Listener _weapons.add(weapon); } - public String getId() + public int getUniqueId() { - return _id; + return _uniqueId; + } + + public OutpostToken toToken() + { + OutpostToken token = new OutpostToken(); + + token.UniqueId = _uniqueId; + token.Origin = _origin; + token.Type = _type; + token.OwnerClan = _ownerClan; + token.Health = _health; + token.TimeSpawned = _timeSpawned; + token.AgainstClan = _againstClan; + token.SiegeDeclaredTime = _siegeDeclaredTime; + token.OutpostState = _state; + + return token; } } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBlock.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBlock.java deleted file mode 100644 index f9dde3d53..000000000 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBlock.java +++ /dev/null @@ -1,57 +0,0 @@ -package mineplex.game.clans.clans.siege.outpost; - -import java.util.Map; - -import org.bukkit.Effect; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.BlockState; - -import mineplex.core.common.util.UtilWorld; - -public class OutpostBlock -{ - protected Location _loc; - protected int _id; - protected byte _data; - - protected int _originalId; - protected byte _originalData; - - public OutpostBlock(Map blocks, Location loc, int id, byte data) - { - _loc = loc; - _id = id; - _data = data; - - String locStr = UtilWorld.locToStr(loc); - - if (blocks.containsKey(locStr)) - { - _originalId = blocks.get(locStr)._originalId; - _originalData = blocks.get(locStr)._originalData; - } - else - { - _originalId = _loc.getBlock().getTypeId(); - _originalData = _loc.getBlock().getData(); - } - } - - public void set() - { - _loc.getBlock().setTypeIdAndData(_id, _data, false); - if (_id != 0) - { - _loc.getWorld().playEffect(_loc, Effect.STEP_SOUND, Material.getMaterial(_id), 10); - } - } - - public void restore() - { - BlockState state = _loc.getBlock().getState(); - state.setTypeId(_originalId); - state.setRawData(_originalData); - state.update(true, false); - } -} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBuilder.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBuilder.java deleted file mode 100644 index 89ac4d68e..000000000 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBuilder.java +++ /dev/null @@ -1,18 +0,0 @@ -package mineplex.game.clans.clans.siege.outpost; - -import java.util.List; - -import org.bukkit.Location; - -public class OutpostBuilder -{ - public OutpostBuilder(Location pos, String string, List wallLocations, Outpost outpost) - { - } - - public void tick() - { - - } - -} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostManager.java index d032a9b6b..e365b380d 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostManager.java @@ -1,26 +1,26 @@ package mineplex.game.clans.clans.siege.outpost; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; +import java.util.Stack; import org.bukkit.Chunk; import org.bukkit.Location; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.inventory.ItemStack; import com.google.common.collect.Lists; import mineplex.core.MiniPlugin; import mineplex.core.common.util.C; import mineplex.core.common.util.F; +import mineplex.core.common.util.NautHashMap; import mineplex.core.common.util.UtilAlg; import mineplex.core.common.util.UtilItem; import mineplex.core.common.util.UtilMath; @@ -35,21 +35,25 @@ import mineplex.game.clans.clans.ClansManager; import mineplex.game.clans.clans.event.PlayerClaimTerritoryEvent; import mineplex.game.clans.clans.siege.SiegeManager; import mineplex.game.clans.clans.siege.events.SiegeWeaponExplodeEvent; -import mineplex.game.clans.clans.siege.weapon.serialization.OutpostToken; +import mineplex.game.clans.clans.siege.repository.OutpostRepository; +import mineplex.game.clans.clans.siege.repository.tokens.OutpostToken; import mineplex.game.clans.core.repository.ClanTerritory; -import mineplex.game.clans.spawn.Spawn; public class OutpostManager extends MiniPlugin { private ClansManager _clansManager; - private Map _outposts = new HashMap<>(); - private Map _idToOutpost = new HashMap<>(); + private NautHashMap _outposts = new NautHashMap<>(); + private NautHashMap _idToOutpost = new NautHashMap<>(); private List _removalQueue; private SiegeManager _siegeManager; + private OutpostRepository _repository; + + private Stack _addQueue = new Stack<>(); + public OutpostManager(ClansManager clansManager, SiegeManager siegeManager) { super("Outpost Manager", clansManager.getPlugin()); @@ -58,45 +62,17 @@ public class OutpostManager extends MiniPlugin _clansManager = clansManager; + _repository= new OutpostRepository(clansManager.getPlugin(), this); + _removalQueue = new ArrayList<>(); } - public void loadExistingOutposts() - { - try - { - for (Entity entity : Spawn.getSpawnWorld().getEntities()) - { - if (!(entity instanceof ArmorStand)) - { - continue; - } - - if (!entity.hasMetadata("$SERIALIZED_OUTPOST_DATA$")) - { - continue; - } - - String data = entity.getMetadata("$SERIALIZED_OUTPOST_DATA$").get(0).asString(); - - Outpost outpost = new Outpost(this, _siegeManager.getGson().fromJson(data, OutpostToken.class)); - - _outposts.put(outpost.getOwner().getName(), outpost); - _idToOutpost.put(outpost.getId(), outpost); - } - } - catch (Exception e) - { - e.printStackTrace(); - } - } - @EventHandler(priority = EventPriority.LOWEST) public void onPlaceBlock(BlockPlaceEvent event) { if (event.getItemInHand().isSimilar(Outpost.OUTPOST_ITEM)) { - if (spawnOutpost(event.getPlayer(), event.getBlock().getLocation(), OutpostType.MK3)) + if (spawnOutpost(event.getPlayer(), event.getBlock().getLocation(), OutpostType.MK_III)) { event.getBlock().setData((byte) Outpost.OUTPOST_BLOCK_DATA); } @@ -217,9 +193,7 @@ public class OutpostManager extends MiniPlugin } _outposts.put(clan.getName(), new Outpost(this, clan, location, type)); - _idToOutpost.put(_outposts.get(clan.getName()).getId(), _outposts.get(clan.getName())); - - _plugin.getServer().getPluginManager().registerEvents(_outposts.get(clan.getName()), _plugin); + _idToOutpost.put(_outposts.get(clan.getName()).getUniqueId(), _outposts.get(clan.getName())); return true; } @@ -250,6 +224,13 @@ public class OutpostManager extends MiniPlugin @EventHandler public void update(UpdateEvent event) { + if (!_addQueue.isEmpty()) + { + Outpost outpost = _addQueue.pop(); + + _outposts.put(outpost.getOwner().getName(), outpost); + } + for (Outpost outpost : _outposts.values()) { if (outpost.getState() != OutpostState.DEAD) @@ -265,7 +246,7 @@ public class OutpostManager extends MiniPlugin { if (!_removalQueue.isEmpty()) { - HandlerList.unregisterAll(_outposts.remove(_removalQueue.remove(0))); + HandlerList.unregisterAll(_idToOutpost.remove(Integer.valueOf(_outposts.remove(_removalQueue.remove(0)).getUniqueId()))); } } @@ -283,6 +264,11 @@ public class OutpostManager extends MiniPlugin public Outpost Get(ClanInfo clan) { + if (clan == null) + { + return null; + } + return _outposts.get(clan.getName().toLowerCase()); } @@ -301,6 +287,34 @@ public class OutpostManager extends MiniPlugin return Lists.newArrayList(_outposts.values()); } + @EventHandler + public void onQuit(PlayerQuitEvent event) + { + Player player = (Player) event.getPlayer(); + ClanInfo clan = _clansManager.getClan(player); + + if (Get(clan) != null) + { + return; + } + + for (int slot = 0; slot < player.getInventory().getSize(); slot++) + { + ItemStack item = player.getInventory().getItem(slot); + + if (item == null) + { + continue; + } + + if (item.isSimilar(Outpost.SIEGE_DECLARATION_ARROW)) + { + player.getInventory().setItem(slot, null); + break; + } + } + } + @EventHandler public void onSiegeWeaponExplode(SiegeWeaponExplodeEvent event) { @@ -366,13 +380,75 @@ public class OutpostManager extends MiniPlugin return false; } - public Outpost Get(String outpostId) + public Outpost Get(int outpostId) { - return _idToOutpost.get(outpostId); + return _idToOutpost.get(Integer.valueOf(outpostId)); } public ClansManager getClansManager() { return _clansManager; } + + public void loadOutposts() + { + System.out.println("[OUTPOSTS] LOADING OUTPOSTS FROM DATABASE"); + + _repository.getOutpostsByServer(_clansManager.getServerId(), tokens -> { + for (OutpostToken token : tokens) + { + if ((System.currentTimeMillis() - token.TimeSpawned) > Outpost.MAX_LIFETIME) + { + System.out.println("[OUTPOSTS] SKIPPING & REMOVING OUTPOST [" + token.UniqueId + "] BECAUSE OF OLD AGE"); + + _repository.deleteOutpost(token.UniqueId); + + continue; + } + + Outpost outpost = new Outpost(this, token); + + System.out.println("[OUTPOSTS] INITIALIZED OUTPOST FROM DATABASE SAVE"); + + _addQueue.push(outpost); + } + }); + } + + public void saveOutposts() + { + final Stack queue = new Stack<>(); + + for (final Outpost outpost : _outposts.values()) + { + final OutpostToken token = outpost.toToken(); + + queue.push(() -> { + _repository.updateOutpost(token); + }); + } + + runAsync(() -> { + while (!queue.isEmpty()) + { + queue.pop().run(); + } + }); + + runAsync(() -> + _repository.getOutpostsByServer(_clansManager.getServerId(), tokens -> + tokens.forEach(token -> { + if (!_idToOutpost.containsKey(Integer.valueOf(token.UniqueId))) + { + _repository.deleteOutpost(token.UniqueId); + } + }) + ) + ); + } + + public OutpostRepository getRepository() + { + return _repository; + } } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostState.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostState.java index ffa3162ec..8d37671fa 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostState.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostState.java @@ -2,9 +2,37 @@ package mineplex.game.clans.clans.siege.outpost; public enum OutpostState { - AWAITING, - CONSTRUCTING, - LIVE, - DESTRUCTING, - DEAD; + /** + * this much space between them so that there is some room for any potential new states. + */ + AWAITING(10), + CONSTRUCTING(20), + LIVE(30), + DESTRUCTING(40), + DEAD(50); + + private byte _id; + + OutpostState(int id) + { + _id = (byte) id; + } + + public static OutpostState ById(byte id) + { + for (OutpostState state : values()) + { + if (state._id == id) + { + return state; + } + } + + return null; + } + + public byte getId() + { + return _id; + } } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostType.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostType.java index 73d9c63a5..134e6b000 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostType.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostType.java @@ -14,11 +14,13 @@ import mineplex.core.common.block.schematic.Schematic; import mineplex.core.common.block.schematic.UtilSchematic; import mineplex.core.common.util.UtilWorld; import mineplex.game.clans.clans.ClansManager; +import mineplex.game.clans.clans.siege.outpost.build.OutpostBlock; +import mineplex.game.clans.clans.siege.outpost.build.OutpostBlockBanner; import net.minecraft.server.v1_8_R3.NBTTagCompound; public enum OutpostType { - ORIGINAL_CLANS(3, 6) { + MK_I(1, 3, 6) { public LinkedHashMap createBuildQueue(Location location, ClansManager clans) { LinkedHashMap build = new LinkedHashMap<>(); @@ -229,7 +231,7 @@ public enum OutpostType return location.clone().subtract(0, 1, 0); } }, - MK2(5, 25) { + MK_II(2, 5, 25) { public LinkedHashMap createBuildQueue(Location location, ClansManager clans) { try @@ -289,7 +291,7 @@ public enum OutpostType return location.clone().subtract(0, 1, 0); } }, - MK3(5, 25) { + MK_III(3, 5, 25) { public LinkedHashMap createBuildQueue(Location location, ClansManager clans) { try @@ -358,10 +360,18 @@ public enum OutpostType protected int _size; protected int _ySize; - OutpostType(int size, int ySize) + private int _id; + + OutpostType(int id, int size, int ySize) { _size = size; _ySize = ySize; + _id = id; + } + + public int getId() + { + return _id; } public abstract LinkedHashMap createBuildQueue(Location location, ClansManager clans); @@ -369,4 +379,17 @@ public enum OutpostType public abstract Location getCoreLocation(Location location); public abstract List getWallLocations(Location location); + + public static OutpostType ById(byte id) + { + for (OutpostType type : values()) + { + if (type._id == id) + { + return type; + } + } + + return null; + } } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/build/OutpostBlock.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/build/OutpostBlock.java new file mode 100644 index 000000000..c0f2f490e --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/build/OutpostBlock.java @@ -0,0 +1,82 @@ +package mineplex.game.clans.clans.siege.outpost.build; + +import java.util.Map; + +import org.bukkit.Effect; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.BlockState; + +import mineplex.core.common.util.UtilWorld; + +public class OutpostBlock +{ + private Location _location; + private int _id; + private byte _data; + + private int _originalId; + private byte _originalData; + + public OutpostBlock(Map blocks, Location loc, int id, byte data) + { + _location = loc; + _id = id; + _data = data; + + String locStr = UtilWorld.locToStr(loc); + + if (blocks.containsKey(locStr)) + { + _originalId = blocks.get(locStr)._originalId; + _originalData = blocks.get(locStr)._originalData; + } + else + { + _originalId = _location.getBlock().getTypeId(); + _originalData = _location.getBlock().getData(); + } + } + + public void set() + { + _location.getBlock().setTypeIdAndData(_id, _data, false); + if (_id != 0) + { + _location.getWorld().playEffect(_location, Effect.STEP_SOUND, Material.getMaterial(_id), 10); + } + } + + public void restore() + { + BlockState state = _location.getBlock().getState(); + state.setTypeId(_originalId); + state.setRawData(_originalData); + state.update(true, false); + } + + public int getId() + { + return _id; + } + + public byte getData() + { + return _data; + } + + public int getOriginalId() + { + return _originalId; + } + + public int getOriginalData() + { + return _originalData; + } + + public Location getLocation() + { + return _location; + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBlockBanner.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/build/OutpostBlockBanner.java similarity index 82% rename from Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBlockBanner.java rename to Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/build/OutpostBlockBanner.java index fe537ed63..02ec95e86 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/OutpostBlockBanner.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/outpost/build/OutpostBlockBanner.java @@ -1,4 +1,4 @@ -package mineplex.game.clans.clans.siege.outpost; +package mineplex.game.clans.clans.siege.outpost.build; import java.util.ArrayList; import java.util.Arrays; @@ -10,6 +10,10 @@ import org.bukkit.Location; import org.bukkit.block.Banner; import org.bukkit.block.banner.Pattern; +/** + * Special block for Mk III Outpost + * + */ public class OutpostBlockBanner extends OutpostBlock { private DyeColor _baseColor; @@ -27,7 +31,7 @@ public class OutpostBlockBanner extends OutpostBlock { super.set(); - Banner banner = (Banner) _loc.getBlock().getState(); + Banner banner = (Banner) getLocation().getBlock().getState(); banner.setBaseColor(_baseColor); banner.setPatterns(_patterns); diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/OutpostRepository.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/OutpostRepository.java new file mode 100644 index 000000000..d6bcb600a --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/OutpostRepository.java @@ -0,0 +1,152 @@ +package mineplex.game.clans.clans.siege.repository; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.List; + +import org.bukkit.plugin.java.JavaPlugin; + +import com.google.common.collect.Lists; + +import mineplex.core.common.util.Callback; +import mineplex.core.common.util.UtilWorld; +import mineplex.core.database.DBPool; +import mineplex.core.database.RepositoryBase; +import mineplex.core.database.column.ColumnByte; +import mineplex.core.database.column.ColumnInt; +import mineplex.core.database.column.ColumnTimestamp; +import mineplex.core.database.column.ColumnVarChar; +import mineplex.game.clans.clans.ClanInfo; +import mineplex.game.clans.clans.siege.outpost.OutpostManager; +import mineplex.game.clans.clans.siege.outpost.OutpostState; +import mineplex.game.clans.clans.siege.outpost.OutpostType; +import mineplex.game.clans.clans.siege.repository.tokens.OutpostToken; + +public class OutpostRepository extends RepositoryBase +{ + private OutpostManager _manager; + + private static final String CREATE = "CREATE TABLE IF NOT EXISTS clansOutposts (uniqueId INT NOT NULL," + + "serverId INT NOT NULL," + + "origin VARCHAR(30)," + + "outpostType TINYINT NOT NULL," + + "ownerClan INT NOT NULL," + + "health INT NOT NULL," + + "timeSpawned LONG," + + "againstClan INT," + + "siegeDeclaredTime LONG," + + "outpostState TINYINT NOT NULL);"; + + private static final String GET_OUTPOST_BY_ID = "SELECT * FROM clansOutposts WHERE uniqueId=?;"; + private static final String GET_OUTPOST_BY_CLAN = "SELECT * FROM clansOutposts WHERE ownerClan=?;"; + private static final String GET_OUTPOSTS_BY_SERVER = "SELECT * FROM clansOutposts WHERE serverId=?;"; + + private static final String UPDATE_OUTPOST = "UPDATE clansOutposts SET health=?,againstClan=?,siegeDeclaredTime=?,outpostState=? WHERE uniqueId=?;"; + private static final String INSERT_OUTPOST = "INSERT INTO clansOutposts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + + private static final String DELETE_OUTPOST = "DELETE FROM clansOutposts WHERE uniqueId=?;"; + + public OutpostRepository(JavaPlugin plugin, OutpostManager manager) + { + super(plugin, DBPool.getAccount()); + + _manager = manager; + } + + public void deleteOutpost(final int uniqueId) + { + executeUpdate(DELETE_OUTPOST, new ColumnInt("uniqueId", uniqueId)); + } + + public void getOutpostById(final int uniqueId, final Callback callback) + { + executeQuery(GET_OUTPOST_BY_ID, resultSet -> { + OutpostToken token = new OutpostToken(); + + resultSet.next(); + + load(token, resultSet); + + callback.run(token); + }, new ColumnInt("uniqueId", uniqueId)); + } + + public void getOutpostByClan(final ClanInfo clan, final Callback callback) + { + executeQuery(GET_OUTPOST_BY_CLAN, resultSet -> { + resultSet.next(); + + OutpostToken token = new OutpostToken(); + + load(token, resultSet); + + callback.run(token); + }, new ColumnInt("ownerClan", clan.getId())); + } + + public void getOutpostsByServer(final int serverId, final Callback> callback) + { + executeQuery(GET_OUTPOSTS_BY_SERVER, resultSet -> { + List tokens = Lists.newArrayList(); + + while (resultSet.next()) + { + OutpostToken token = new OutpostToken(); + load(token, resultSet); + tokens.add(token); + } + + callback.run(tokens); + }, new ColumnInt("serverId", serverId)); + } + + private void load(OutpostToken token, ResultSet columns) throws SQLException + { + token.UniqueId = columns.getInt("uniqueId"); + token.Origin = UtilWorld.strToLoc(columns.getString("origin")); + token.Type = OutpostType.ById(columns.getByte("outpostType")); + token.OwnerClan = _manager.getClansManager().getClanUtility().getClanById(columns.getInt("ownerClan")); + token.Health = columns.getShort("health"); + token.TimeSpawned = columns.getTimestamp("timeSpawned").getTime(); + token.AgainstClan = _manager.getClansManager().getClanUtility().getClanById(columns.getInt("againstClan")); + token.SiegeDeclaredTime = columns.getTimestamp("siegeDeclaredTime").getTime(); + token.OutpostState = OutpostState.ById(columns.getByte("outpostState")); + } + + @Override + protected void initialize() + { + executeUpdate(CREATE); + } + + @Override + protected void update() + { + } + + public void updateOutpost(OutpostToken token) + { + executeUpdate(UPDATE_OUTPOST, + new ColumnInt("health", token.Health), + new ColumnInt("againstClan", token.AgainstClan == null ? 0 : token.AgainstClan.getId()), + new ColumnTimestamp("siegeDeclaredTime", new Timestamp(token.SiegeDeclaredTime)), + new ColumnByte("outpostState", Byte.valueOf(token.OutpostState.getId())), + new ColumnInt("uniqueId", token.UniqueId)); + } + + public void insertOutpost(OutpostToken token) + { + executeUpdate(INSERT_OUTPOST, + new ColumnInt("uniqueId", token.UniqueId), + new ColumnInt("serverId", _manager.getClansManager().getServerId()), + new ColumnVarChar("origin", 30, UtilWorld.locToStr(token.Origin)), + new ColumnInt("outpostType", token.Type.getId()), + new ColumnInt("ownerClan", token.OwnerClan.getId()), + new ColumnInt("health", token.Health), + new ColumnTimestamp("timeSpawned", new Timestamp(token.TimeSpawned)), + new ColumnInt("againstClan", token.AgainstClan == null ? 0 : token.AgainstClan.getId()), + new ColumnTimestamp("siegeDeclaredTime", new Timestamp(token.SiegeDeclaredTime)), + new ColumnByte("outpostState", Byte.valueOf(token.OutpostState.getId()))); + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/SiegeWeaponRepository.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/SiegeWeaponRepository.java new file mode 100644 index 000000000..4a325efac --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/SiegeWeaponRepository.java @@ -0,0 +1,124 @@ +package mineplex.game.clans.clans.siege.repository; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +import org.bukkit.plugin.java.JavaPlugin; + +import com.google.common.collect.Lists; + +import mineplex.core.common.util.Callback; +import mineplex.core.common.util.UtilWorld; +import mineplex.core.database.DBPool; +import mineplex.core.database.RepositoryBase; +import mineplex.core.database.column.ColumnInt; +import mineplex.game.clans.clans.ClanInfo; +import mineplex.game.clans.clans.siege.SiegeManager; +import mineplex.game.clans.clans.siege.outpost.Outpost; +import mineplex.game.clans.clans.siege.repository.tokens.SiegeWeaponToken; + +public class SiegeWeaponRepository extends RepositoryBase +{ + private SiegeManager _manager; + + private static final String CREATE = "CREATE TABLE IF NOT EXISTS clansSiegeWeapons (uniqueId INT NOT NULL," + + "serverId INT NOT NULL," + + "location VARCHAR(30)," + + "ownerClan INT NOT NULL," + + "outpostId INT," + + "weaponType TINYINT NOT NULL," + + "health int NOT NULL," + + "yaw int NOT NULL," + + "lastFired LONG);"; + + private static final String GET_WEAPON_BY_ID = "SELECT * FROM clansSiegeWeapons WHERE uniqueId=?;"; + private static final String GET_WEAPONS_BY_CLAN = "SELECT * FROM clansSiegeWeapons WHERE ownerClan=?;"; + private static final String GET_WEAPONS_BY_OUTPOST = "SELECT * FROM clansSiegeWeapons WHERE outpostId=?;"; + + private static final String DELETE_WEAPON = "DELETE FROM clansSiegeWeapons WHERE uniqueId=?;"; + + public SiegeWeaponRepository(JavaPlugin plugin, SiegeManager manager) + { + super(plugin, DBPool.getAccount()); + + _manager = manager; + } + + public void deleteWeapon(final int uniqueId) + { + executeUpdate(DELETE_WEAPON, new ColumnInt("uniqueId", uniqueId)); + } + + public void getWeaponById(final int uniqueId, final Callback callback) + { + executeQuery(GET_WEAPON_BY_ID, resultSet -> { + SiegeWeaponToken token = new SiegeWeaponToken(); + + resultSet.next(); + + load(token, resultSet); + + callback.run(token); + }, new ColumnInt("uniqueId", uniqueId)); + } + + public void getWeaponsByClan(final ClanInfo clan, final Callback> callback) + { + executeQuery(GET_WEAPONS_BY_CLAN, resultSet -> { + List tokens = Lists.newArrayList(); + + while (resultSet.next()) + { + SiegeWeaponToken token = new SiegeWeaponToken(); + + load(token, resultSet); + + tokens.add(token); + } + + callback.run(tokens); + }, new ColumnInt("ownerClan", clan.getId())); + } + + public void getWeaponsByOutpost(final Outpost outpost, final Callback> callback) + { + executeQuery(GET_WEAPONS_BY_OUTPOST, resultSet -> { + List tokens = Lists.newArrayList(); + + while (resultSet.next()) + { + SiegeWeaponToken token = new SiegeWeaponToken(); + + load(token, resultSet); + + tokens.add(token); + } + + callback.run(tokens); + }, new ColumnInt("outpostId", outpost.getUniqueId())); + } + + private void load(SiegeWeaponToken token, ResultSet columns) throws SQLException + { + token.Id = columns.getInt("uniqueId"); + token.Location = UtilWorld.strToLoc(columns.getString("location")); + token.OwnerClan = _manager.getClansManager().getClanUtility().getClanById(columns.getInt("ownerClan")); + token.OutpostId = columns.getInt("outpostId"); + token.WeaponType = columns.getByte("weaponType"); + token.Health = columns.getShort("health"); + token.Yaw = columns.getShort("yaw"); + token.LastFired = columns.getTimestamp("lastFired").getTime(); + } + + @Override + protected void initialize() + { + executeUpdate(CREATE); + } + + @Override + protected void update() + { + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/OutpostToken.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/tokens/OutpostToken.java similarity index 54% rename from Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/OutpostToken.java rename to Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/tokens/OutpostToken.java index 2810ad924..1bd6c1912 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/OutpostToken.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/tokens/OutpostToken.java @@ -1,24 +1,20 @@ -package mineplex.game.clans.clans.siege.weapon.serialization; - -import java.util.UUID; +package mineplex.game.clans.clans.siege.repository.tokens; import org.bukkit.Location; -import mineplex.core.common.GsonLocation; import mineplex.game.clans.clans.ClanInfo; import mineplex.game.clans.clans.siege.outpost.OutpostState; import mineplex.game.clans.clans.siege.outpost.OutpostType; public class OutpostToken { - public String Id; - public GsonLocation Origin; + public int UniqueId; + public Location Origin; public OutpostType Type; - public String OwnerClan; - public double Health; + public ClanInfo OwnerClan; + public int Health; public long TimeSpawned; - public String AgainstClan; + public ClanInfo AgainstClan; public long SiegeDeclaredTime; public OutpostState OutpostState; - public UUID DataEntityUUID; } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/tokens/SiegeWeaponToken.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/tokens/SiegeWeaponToken.java new file mode 100644 index 000000000..b07edf267 --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/repository/tokens/SiegeWeaponToken.java @@ -0,0 +1,22 @@ +package mineplex.game.clans.clans.siege.repository.tokens; + +import java.util.Map; +import java.util.UUID; + +import org.bukkit.Location; + +import mineplex.game.clans.clans.ClanInfo; + +public class SiegeWeaponToken +{ + public int Id; + public ClanInfo OwnerClan; + public byte WeaponType; + public int OutpostId; + public Location Location; + public Map ComprisedOf; + public int Health; + public int Yaw; + public UUID Rider; + public long LastFired; +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/Cannon.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/Cannon.java index 15557fc9e..e760c2d2a 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/Cannon.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/Cannon.java @@ -28,9 +28,9 @@ import mineplex.core.itemstack.ItemBuilder; import mineplex.game.clans.clans.ClanInfo; import mineplex.game.clans.clans.siege.SiegeManager; import mineplex.game.clans.clans.siege.outpost.Outpost; +import mineplex.game.clans.clans.siege.repository.tokens.SiegeWeaponToken; import mineplex.game.clans.clans.siege.weapon.projectile.ProjectileAttributes; import mineplex.game.clans.clans.siege.weapon.projectile.WeaponProjectile; -import mineplex.game.clans.clans.siege.weapon.serialization.SiegeWeaponToken; import mineplex.game.clans.clans.siege.weapon.util.AccessRule; import mineplex.game.clans.clans.siege.weapon.util.AccessType; import mineplex.game.clans.clans.siege.weapon.util.WeaponStateInfo; @@ -41,7 +41,7 @@ public class Cannon extends SiegeWeapon public Cannon(SiegeManager siegeManager, SiegeWeaponToken token) { - super(1400.d, "Cannon", token, siegeManager.getClansManager(), siegeManager); + super(1400, "Cannon", token, siegeManager.getClansManager(), siegeManager); setBoundingBox(1); @@ -67,7 +67,7 @@ public class Cannon extends SiegeWeapon return false; } - if (!_owner.isMember(player)) + if (!_ownerClan.isMember(player)) { UtilPlayer.message(player, F.main("Clans", "This cannon is not owned by your Clan.")); return false; @@ -91,7 +91,7 @@ public class Cannon extends SiegeWeapon enableInventory(UtilServer.getServer().createInventory(null, InventoryType.DISPENSER, C.cDAquaB + _name), new AccessRule(AccessType.RCLICK_BB, player -> player.equals(getRider()))); setRideable(new AccessRule(AccessType.RCLICK_BB, player -> { - if (!_owner.isMember(player)) + if (!_ownerClan.isMember(player)) { UtilPlayer.message(player, F.main("Clans", "This cannon is not owned by your Clan.")); return false; @@ -109,7 +109,7 @@ public class Cannon extends SiegeWeapon public Cannon(Location location, ClanInfo clan, SiegeManager siegeManager, Outpost outpost) { - super(2, location, 1400.d, "Cannon", clan, clan.Clans, siegeManager, outpost); + super(2, location, 1400, "Cannon", clan, clan.Clans, siegeManager, outpost); setBoundingBox(1); @@ -137,7 +137,7 @@ public class Cannon extends SiegeWeapon return false; } - if (!_owner.isMember(player)) + if (!_ownerClan.isMember(player)) { UtilPlayer.message(player, F.main("Clans", "This cannon is not owned by your Clan.")); return false; @@ -161,7 +161,7 @@ public class Cannon extends SiegeWeapon enableInventory(UtilServer.getServer().createInventory(null, InventoryType.DISPENSER, C.cDAquaB + _name), new AccessRule(AccessType.RCLICK_BB, player -> player.equals(getRider()))); setRideable(new AccessRule(AccessType.RCLICK_BB, player -> { - if (!_owner.isMember(player)) + if (!_ownerClan.isMember(player)) { UtilPlayer.message(player, F.main("Clans", "This cannon is not owned by your Clan.")); return false; diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/Catapult.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/Catapult.java index fdbbba338..d4e70a40e 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/Catapult.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/Catapult.java @@ -24,9 +24,9 @@ import mineplex.core.itemstack.ItemBuilder; import mineplex.game.clans.clans.ClanInfo; import mineplex.game.clans.clans.siege.SiegeManager; import mineplex.game.clans.clans.siege.outpost.Outpost; +import mineplex.game.clans.clans.siege.repository.tokens.SiegeWeaponToken; import mineplex.game.clans.clans.siege.weapon.projectile.ProjectileAttributes; import mineplex.game.clans.clans.siege.weapon.projectile.WeaponProjectile; -import mineplex.game.clans.clans.siege.weapon.serialization.SiegeWeaponToken; import mineplex.game.clans.clans.siege.weapon.util.AccessRule; import mineplex.game.clans.clans.siege.weapon.util.AccessType; import mineplex.game.clans.clans.siege.weapon.util.WeaponStateInfo; @@ -37,7 +37,7 @@ public class Catapult extends SiegeWeapon public Catapult(SiegeManager siegeManager, SiegeWeaponToken token) { - super(1400.d, "Catapult", token, siegeManager.getClansManager(), siegeManager); + super(1400, "Catapult", token, siegeManager.getClansManager(), siegeManager); setBoundingBox(3, 0); @@ -66,7 +66,7 @@ public class Catapult extends SiegeWeapon return false; } - if (!_owner.isMember(player)) + if (!_ownerClan.isMember(player)) { UtilPlayer.message(player, F.main("Clans", "This Catapult is not owned by your Clan.")); return false; @@ -90,7 +90,7 @@ public class Catapult extends SiegeWeapon enableInventory(UtilServer.getServer().createInventory(null, InventoryType.DISPENSER, C.cDAquaB + _name), new AccessRule(AccessType.RCLICK_BB, player -> player.equals(getRider()))); setRideable(new AccessRule(AccessType.RCLICK_BB, player -> { - if (!_owner.isMember(player)) + if (!_ownerClan.isMember(player)) { UtilPlayer.message(player, F.main("Clans", "This Catapult is not owned by your Clan.")); return false; @@ -108,7 +108,7 @@ public class Catapult extends SiegeWeapon public Catapult(Location location, ClanInfo clan, SiegeManager siegeManager, Outpost outpost) { - super(1, location, 2500.d, "Catapult", clan, clan.Clans, siegeManager, outpost); + super(1, location, 2500, "Catapult", clan, clan.Clans, siegeManager, outpost); setBoundingBox(3, 0); @@ -139,7 +139,7 @@ public class Catapult extends SiegeWeapon return false; } - if (!_owner.isMember(player)) + if (!_ownerClan.isMember(player)) { UtilPlayer.message(player, F.main("Clans", "This Catapult is not owned by your Clan.")); return false; @@ -163,7 +163,7 @@ public class Catapult extends SiegeWeapon enableInventory(UtilServer.getServer().createInventory(null, InventoryType.DISPENSER, C.cDAquaB + _name), new AccessRule(AccessType.RCLICK_BB, player -> player.equals(getRider()))); setRideable(new AccessRule(AccessType.RCLICK_BB, player -> { - if (!_owner.isMember(player)) + if (!_ownerClan.isMember(player)) { UtilPlayer.message(player, F.main("Clans", "This Catapult is not owned by your Clan.")); return false; diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/SiegeWeapon.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/SiegeWeapon.java index e7ca2fd56..5955b5287 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/SiegeWeapon.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/SiegeWeapon.java @@ -1,6 +1,5 @@ package mineplex.game.clans.clans.siege.weapon; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -28,7 +27,6 @@ import org.bukkit.event.player.PlayerInteractAtEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.util.EulerAngle; import org.spigotmc.event.entity.EntityDismountEvent; @@ -52,10 +50,9 @@ import mineplex.game.clans.clans.ClansManager; import mineplex.game.clans.clans.siege.SiegeManager; import mineplex.game.clans.clans.siege.events.SiegeWeaponExplodeEvent; import mineplex.game.clans.clans.siege.outpost.Outpost; -import mineplex.game.clans.clans.siege.outpost.OutpostManager; +import mineplex.game.clans.clans.siege.repository.tokens.SiegeWeaponToken; import mineplex.game.clans.clans.siege.weapon.projectile.ProjectileAttributes; import mineplex.game.clans.clans.siege.weapon.projectile.WeaponProjectile; -import mineplex.game.clans.clans.siege.weapon.serialization.SiegeWeaponToken; import mineplex.game.clans.clans.siege.weapon.util.AccessRule; import mineplex.game.clans.clans.siege.weapon.util.AccessType; import mineplex.game.clans.clans.siege.weapon.util.BarrierCollisionBox; @@ -66,17 +63,17 @@ public abstract class SiegeWeapon implements Listener protected ClansManager _clans; protected SiegeManager _siegeManager; - protected String _id; + protected int _uniqueId; /** * THIS MAY BE NULL IF NOT PLACED IN OUTPOST */ protected Outpost _outpost; - protected ClanInfo _owner; + protected ClanInfo _ownerClan; protected final String _name; - protected final double _maxHealth; - protected double _health; + protected final int _maxHealth; + protected int _health; protected Inventory _inventory; protected AccessRule _inventoryAccess; @@ -134,20 +131,18 @@ public abstract class SiegeWeapon implements Listener protected final int _weaponTypeIdentifier; - private ArmorStand _dataEntity; - - public SiegeWeapon(double maxHealth, String name, SiegeWeaponToken token, ClansManager clansManager, SiegeManager siegeManager) + public SiegeWeapon(int maxHealth, String name, SiegeWeaponToken token, ClansManager clansManager, SiegeManager siegeManager) { _weaponTypeIdentifier = token.WeaponType; - _id = token.Id; + _uniqueId = token.Id; _outpost = siegeManager.getOutpostManager().Get(token.OutpostId); _siegeManager = siegeManager; _location = token.Location; _name = name; _health = _maxHealth = maxHealth; - _owner = clansManager.getClan(token.OwnerClan); + _ownerClan = token.OwnerClan; _comprisedOf = Lists.newArrayList(); _registeredStates = Maps.newHashMap(); @@ -156,7 +151,7 @@ public abstract class SiegeWeapon implements Listener _infoHologram = new Hologram(ClansManager.getInstance().getHologramManager(), _location.clone().add(.5, 3, .5), _name + " Health", getDisplayHealth()); _infoHologram.start(); - UtilServer.getPluginManager().registerEvents(this, clansManager.getPlugin()); + UtilServer.registerEvents(this); _clans = clansManager; @@ -170,14 +165,8 @@ public abstract class SiegeWeapon implements Listener _entityMapping.put(entry.getKey(), entity); } } - - if (entity.getUniqueId().equals(token.DataEntityUUID)) - { - _dataEntity = (ArmorStand) entity; - } } - _yaw = token.Yaw; _rider = Bukkit.getPlayer(token.Rider); _lastFired = token.LastFired; @@ -185,9 +174,9 @@ public abstract class SiegeWeapon implements Listener _health = token.Health; } - public SiegeWeapon(int typeId, Location location, double maxHealth, String name, ClanInfo owner, ClansManager clansManager, SiegeManager siegeManager, Outpost outpost) + public SiegeWeapon(int typeId, Location location, int maxHealth, String name, ClanInfo owner, ClansManager clansManager, SiegeManager siegeManager, Outpost outpost) { - _id = Integer.toString(UtilMath.random.nextInt(13333337)); + _uniqueId = siegeManager.randomId(); _weaponTypeIdentifier = typeId; _outpost = outpost; @@ -195,7 +184,7 @@ public abstract class SiegeWeapon implements Listener _location = location; _name = name; _health = _maxHealth = maxHealth; - _owner = owner; + _ownerClan = owner; _comprisedOf = Lists.newArrayList(); _registeredStates = Maps.newHashMap(); @@ -207,70 +196,6 @@ public abstract class SiegeWeapon implements Listener UtilServer.getPluginManager().registerEvents(this, clansManager.getPlugin()); _clans = clansManager; - - _dataEntity = location.getWorld().spawn(location.subtract(0, 5, 0), ArmorStand.class); - _dataEntity.setVisible(false); - _dataEntity.setGravity(false); - } - - public SiegeWeaponToken tokenize() - { - SiegeWeaponToken token = new SiegeWeaponToken(); - - Map comprisedOf = new HashMap<>(); - - for (Entry entry : _entityMapping.entrySet()) - { - comprisedOf.put(entry.getKey(), entry.getValue().getUniqueId()); - } - - - token.ComprisedOf = comprisedOf; - - token.OwnerClan = _owner.getName(); - - token.WeaponType = _weaponTypeIdentifier; - - token.OutpostId = _outpost.getId(); - - token.Id = _id; - - token.Location = _location; - - token.Health = _health; - - token.Yaw = _yaw; - - token.DataEntityUUID = _dataEntity.getUniqueId(); - - token.Rider = _rider.getUniqueId(); - - token.LastFired = _lastFired; - - return token; - } - - private void updateData() - { - try - { - String data = _siegeManager.getGson().toJson(tokenize()); - - _dataEntity.setMetadata("$SERIALIZED_SIEGE_WEAPON_DATA$", new FixedMetadataValue(_siegeManager.getPlugin(), data)); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - - @EventHandler - public void saveData(UpdateEvent event) - { - if (event.getType() == UpdateType.SEC_05) - { - updateData(); - } } public int getBaseDamage() @@ -280,7 +205,7 @@ public abstract class SiegeWeapon implements Listener public ClanInfo getOwner() { - return _owner; + return _ownerClan; } public ClansManager getClans() @@ -294,7 +219,7 @@ public abstract class SiegeWeapon implements Listener removeHealth(health); - UtilTextMiddle.display(UtilText.possesive(_owner.getName(), _name), C.cRed + "-" + health, 5, 15, 5); + UtilTextMiddle.display(UtilText.possesive(_ownerClan.getName(), _name), C.cRed + "-" + health, 5, 15, 5); } protected abstract double[] getProjectileVelocity(); @@ -497,9 +422,9 @@ public abstract class SiegeWeapon implements Listener return player.equals(getRider()); } - public String getId() + public int getUniqueId() { - return _id; + return _uniqueId; } protected void setRideable(AccessRule accessRule) @@ -520,8 +445,6 @@ public abstract class SiegeWeapon implements Listener _comprisedOf.clear(); _infoHologram.stop(); - _dataEntity.remove(); - _siegeManager.dead(this); _alive = false; @@ -624,7 +547,7 @@ public abstract class SiegeWeapon implements Listener return; } - if (!_owner.isMember(player)) + if (!_ownerClan.isMember(player)) { damage(player); } @@ -696,17 +619,17 @@ public abstract class SiegeWeapon implements Listener // Health Management - public final double getHealth() + public final int getHealth() { return _health; } public final String getDisplayHealth() { - return UtilText.getProgress(null, _health / _maxHealth, null, false, 12); + return UtilText.getProgress(null, ((double) _health) / ((double) _maxHealth), null, false, 12); } - public final void setHealth(double health) + public final void setHealth(int health) { _health = UtilMath.clamp(health, 0, _maxHealth); @@ -718,12 +641,12 @@ public abstract class SiegeWeapon implements Listener } } - public final void removeHealth(double health) + public final void removeHealth(int health) { setHealth(_health - health); } - public final void addHealth(double health) + public final void addHealth(int health) { setHealth(_health + health); } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/SiegeWeaponToken.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/SiegeWeaponToken.java deleted file mode 100644 index e6a941a27..000000000 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/SiegeWeaponToken.java +++ /dev/null @@ -1,23 +0,0 @@ -package mineplex.game.clans.clans.siege.weapon.serialization; - -import java.util.Map; -import java.util.UUID; - -import org.bukkit.Location; - -import mineplex.core.common.GsonLocation; - -public class SiegeWeaponToken -{ - public String OwnerClan; - public int WeaponType; - public String OutpostId; - public GsonLocation Location; - public Map ComprisedOf; - public double Health; - public double Yaw; - public UUID Rider; - public String Id; - public long LastFired; - public UUID DataEntityUUID; -} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/TokenInfo.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/TokenInfo.java deleted file mode 100644 index 2bab907e6..000000000 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/siege/weapon/serialization/TokenInfo.java +++ /dev/null @@ -1,6 +0,0 @@ -package mineplex.game.clans.clans.siege.weapon.serialization; - -public class TokenInfo -{ - public Class Type; -} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/gameplay/Gameplay.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/gameplay/Gameplay.java index ac061de02..0d6daad86 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/gameplay/Gameplay.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/gameplay/Gameplay.java @@ -154,9 +154,14 @@ public class Gameplay extends MiniPlugin } } - @EventHandler(priority = EventPriority.LOWEST) + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onBowShoot(EntityShootBowEvent event) { + if (event.isCancelled()) + { + return; + } + if (event.getEntity() instanceof Player) { Player player = (Player) event.getEntity(); diff --git a/Plugins/database-config.dat b/Plugins/database-config.dat new file mode 100644 index 000000000..97bb8b2f7 --- /dev/null +++ b/Plugins/database-config.dat @@ -0,0 +1,4 @@ +ACCOUNT 10.33.53.17/Account MilitantPaladin ga.Usarathehe6up +QUEUE 10.33.53.17/Queue MilitantPaladin ga.Usarathehe6up +MINEPLEX 10.33.53.17/Mineplex MilitantPaladin ga.Usarathehe6up +STATS_MINEPLEX 10.33.53.17/Mineplex MilitantPaladin ga.Usarathehe6up diff --git a/Plugins/redis-config.dat b/Plugins/redis-config.dat new file mode 100644 index 000000000..ff7e4defb --- /dev/null +++ b/Plugins/redis-config.dat @@ -0,0 +1,4 @@ +10.33.53.17 6379 master ServerStatus +10.33.53.17 6378 slave ServerStatus +10.33.53.17 6379 master +10.33.53.17 6378 slave \ No newline at end of file