PUSHING CODE BECAUSE MY HDD MIGHT BE BREAKING DOWN. THIS CODE IS UNTESTED PROBABLY DOESN'T WORK

This commit is contained in:
NewGarbo 2016-02-12 19:58:40 +00:00
parent 90570c1185
commit e6eb43ada8
12 changed files with 512 additions and 284 deletions

View File

@ -0,0 +1,80 @@
package mineplex.core.common;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
public class GsonLocation
{
private String _world;
private double _posX;
private double _posY;
private double _posZ;
private float _yaw;
private float _pitch;
public GsonLocation(Location location)
{
_world = location.getWorld().getName();
_posX = location.getX();
_posY = location.getY();
_posZ = location.getZ();
_yaw = location.getYaw();
_pitch = location.getPitch();
}
public GsonLocation(String world, double x, double y, double z)
{
this(Bukkit.getWorld(world), x, y, z, .0f, .0f);
}
public GsonLocation(String world, double x, double y, double z, float yaw, float pitch)
{
this(Bukkit.getWorld(world), x, y, z, yaw, pitch);
}
public GsonLocation(World world, double x, double y, double z, float yaw, float pitch)
{
_world = world.getName();
_posX = x;
_posY = y;
_posZ = z;
_yaw = yaw;
_pitch = pitch;
}
public GsonLocation(double x, double y, double z)
{
this(x, y, z, .0f, .0f);
}
public GsonLocation(double x, double y, double z, float yaw, float pitch)
{
this("world", x, y, z, yaw, pitch);
}
public Location bukkit()
{
return new Location(Bukkit.getWorld(_world), _posX, _posY, _posZ);
}
public String getWorld()
{
return _world;
}
public double getX()
{
return _posX;
}
public double getY()
{
return _posY;
}
public double getZ()
{
return _posZ;
}
}

View File

@ -1,43 +0,0 @@
package mineplex.core.common.gson;
import java.io.IOException;
import java.util.UUID;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
public class UUIDTypeAdapter extends TypeAdapter<UUID>
{
public UUID read(JsonReader reader) throws IOException
{
String uuid = null;
reader.beginObject();
read:
while (reader.hasNext())
{
switch (reader.nextName())
{
case "bytes":
{
uuid = reader.nextString();
break read;
}
}
}
reader.endObject();
return UUID.fromString(uuid);
}
public void write(JsonWriter writer, UUID uuid) throws IOException
{
writer.beginObject();
writer.name("bytes").value(uuid.toString());
writer.endObject();
}
}

View File

@ -64,6 +64,10 @@ public enum UpdateType
* Once every 8 seconds * Once every 8 seconds
*/ */
SEC_08(8000), SEC_08(8000),
/**
* Once every 5 seconds
*/
SEC_05(5000),
/** /**
* Once every 4 seconds * Once every 4 seconds
*/ */

View File

@ -1,8 +1,7 @@
package mineplex.game.clans.clans.siege; package mineplex.game.clans.clans.siege;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List; import java.util.Map;
import java.util.UUID;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
@ -18,10 +17,11 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import mineplex.core.MiniPlugin; import mineplex.core.MiniPlugin;
import mineplex.core.common.gson.UUIDTypeAdapter;
import mineplex.core.common.util.F; import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilInv; import mineplex.core.common.util.UtilInv;
import mineplex.core.common.util.UtilPlayer; 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.ClanInfo;
import mineplex.game.clans.clans.ClansBlacklist; import mineplex.game.clans.clans.ClansBlacklist;
import mineplex.game.clans.clans.ClansManager; import mineplex.game.clans.clans.ClansManager;
@ -31,7 +31,7 @@ import mineplex.game.clans.clans.siege.outpost.OutpostManager;
import mineplex.game.clans.clans.siege.weapon.Cannon; import mineplex.game.clans.clans.siege.weapon.Cannon;
import mineplex.game.clans.clans.siege.weapon.Catapult; import mineplex.game.clans.clans.siege.weapon.Catapult;
import mineplex.game.clans.clans.siege.weapon.SiegeWeapon; import mineplex.game.clans.clans.siege.weapon.SiegeWeapon;
import mineplex.game.clans.clans.siege.weapon.serialization.TokenInfo; import mineplex.game.clans.clans.siege.weapon.serialization.SiegeWeaponToken;
import mineplex.game.clans.core.repository.ClanTerritory; import mineplex.game.clans.core.repository.ClanTerritory;
import mineplex.game.clans.spawn.Spawn; import mineplex.game.clans.spawn.Spawn;
@ -43,7 +43,7 @@ public class SiegeManager extends MiniPlugin
public static SiegeManager Instance; public static SiegeManager Instance;
public List<SiegeWeapon> LiveSiegeWeapons = new ArrayList<>(); public Map<String, SiegeWeapon> LiveSiegeWeapons = new HashMap<>();
private Gson _gson; private Gson _gson;
@ -51,7 +51,7 @@ public class SiegeManager extends MiniPlugin
{ {
super("Siege", plugin); super("Siege", plugin);
_gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); _gson = new Gson();
_clans = clans; _clans = clans;
@ -66,45 +66,17 @@ public class SiegeManager extends MiniPlugin
Instance = this; Instance = this;
} }
/** @EventHandler
* data clusters: public void onUpdate(UpdateEvent event)
* STRING : { {
* int(4BYTE) length; if (event.getType() == UpdateType.SLOWER)
* byte[] bytes; {
* } _outpostManager.loadExistingOutposts();
*
* LOCATION : { loadExistingWeapons();
* STRING world; }
* int(4BYTE) x; }
* int(4BYTE) y;
* int(4BYTE) z;
* }
*
* ITEM : {
* STRING material;
* byte data;
* int amount;
* }
*
* INVENTORY : {
* int numOfEntries;
* ITEM[] item;
* }
*
* data format:
* Outpost {
* STRING clanOwner;
* int weaponType;
* STRING outpostId;
* LOCATION location;
* INVENTORY inventory;
* MAP<String, UUID> comprisedOf;
* double health;
* double yaw;
* String rider;
* long lastFired;
* }
*/
private void loadExistingWeapons() private void loadExistingWeapons()
{ {
try try
@ -121,14 +93,55 @@ public class SiegeManager extends MiniPlugin
continue; continue;
} }
ArmorStand stand = (ArmorStand) entity;
String data = entity.getMetadata("$SERIALIZED_SIEGE_WEAPON_DATA$").get(0).asString(); String data = entity.getMetadata("$SERIALIZED_SIEGE_WEAPON_DATA$").get(0).asString();
String type = entity.getMetadata("$TOKEN_INFO$").get(0).asString();
SiegeWeapon weapon = _gson.fromJson(data, _gson.fromJson(type, TokenInfo.class).Type); 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);
}
} }
} }
@ -266,6 +279,8 @@ public class SiegeManager extends MiniPlugin
{ {
outpost.addWeapon(cannon); outpost.addWeapon(cannon);
} }
LiveSiegeWeapons.put(cannon.getId(), cannon);
} }
public boolean trySpawnCatapult(Player player, Location location) public boolean trySpawnCatapult(Player player, Location location)
@ -327,11 +342,13 @@ public class SiegeManager extends MiniPlugin
{ {
outpost.addWeapon(catapult); outpost.addWeapon(catapult);
} }
LiveSiegeWeapons.put(catapult.getId(), catapult);
} }
public void dead(SiegeWeapon weapon) public void dead(SiegeWeapon weapon)
{ {
LiveSiegeWeapons.remove(weapon); LiveSiegeWeapons.remove(weapon.getId());
} }
public OutpostManager getOutpostManager() public OutpostManager getOutpostManager()
@ -339,4 +356,14 @@ public class SiegeManager extends MiniPlugin
return _outpostManager; return _outpostManager;
} }
public Gson getGson()
{
return _gson;
}
public ClansManager getClansManager()
{
return _clans;
}
} }

View File

@ -1,14 +1,17 @@
package mineplex.game.clans.clans.siege.outpost; package mineplex.game.clans.clans.siege.outpost;
import java.io.ByteArrayOutputStream; import java.io.File;
import java.io.DataOutputStream; import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.UUID; import java.util.UUID;
import java.util.Map.Entry;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
@ -16,6 +19,7 @@ import org.bukkit.Sound;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Arrow; import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.FallingBlock; import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -64,6 +68,7 @@ import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.ClansBlacklist; import mineplex.game.clans.clans.ClansBlacklist;
import mineplex.game.clans.clans.siege.events.SiegeWeaponExplodeEvent; import mineplex.game.clans.clans.siege.events.SiegeWeaponExplodeEvent;
import mineplex.game.clans.clans.siege.weapon.SiegeWeapon; 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 mineplex.game.clans.core.repository.ClanTerritory;
import net.minecraft.server.v1_8_R3.AxisAlignedBB; import net.minecraft.server.v1_8_R3.AxisAlignedBB;
@ -127,34 +132,34 @@ public class Outpost implements Listener
private ArmorStand _dataEntity; private ArmorStand _dataEntity;
public Outpost(OutpostManager outpostManager, String id, Location origin, ClanInfo ownerClan, OutpostType outpostType, double health, long spawnTime, ClanInfo againstClan, long siegeDeclaredTime, OutpostState outpostState) public Outpost(OutpostManager outpostManager, OutpostToken token)
{ {
_host = outpostManager; _host = outpostManager;
_health = health; _health = token.Health;
_siegeDeclaredTime = siegeDeclaredTime; _siegeDeclaredTime = token.SiegeDeclaredTime;
_against = againstClan; _against = outpostManager.getClansManager().getClan(token.AgainstClan);
_id = id; _id = token.Id;
_siegeWeaponDistance = outpostType._size + 27.5; _siegeWeaponDistance = token.Type._size + 27.5;
_owner = ownerClan; _owner = outpostManager.getClansManager().getClan(token.OwnerClan);
_startCorner = origin.clone().subtract(outpostType._size, 1.1, outpostType._size); _startCorner = token.Origin.clone().subtract(token.Type._size, 1.1, token.Type._size);
_endCorner = origin.clone().add(outpostType._size + .9, outpostType._ySize - 1, outpostType._size + .9); _endCorner = token.Origin.clone().add(token.Type._size + .9, token.Type._ySize - 1, token.Type._size + .9);
_weapons = new ArrayList<>(); _weapons = new ArrayList<>();
_forceFieldStart = _startCorner.clone().subtract(4, 0, 4); _forceFieldStart = _startCorner.clone().subtract(4, 0, 4);
_forceFieldEnd = _endCorner.clone().add(4.5, 0, 4.5); _forceFieldEnd = _endCorner.clone().add(4.5, 0, 4.5);
_origin = origin; _origin = token.Origin;
_type = outpostType; _type = token.Type;
_spawnTime = spawnTime; _spawnTime = token.TimeSpawned;
_core = _type.getCoreLocation(_origin); _core = _type.getCoreLocation(_origin);
@ -164,15 +169,23 @@ public class Outpost implements Listener
_preHologram.start(); _preHologram.start();
_preHologram2.start(); _preHologram2.start();
_dataEntity = origin.getWorld().spawn(origin, ArmorStand.class); _state = token.OutpostState;
_state = outpostState; for (Entity entity : token.Origin.getWorld().getEntities())
{
if (entity.getUniqueId().equals(token.DataEntityUUID))
{
_dataEntity = (ArmorStand) entity;
}
}
} }
public Outpost(OutpostManager host, ClanInfo clan, Location location, OutpostType type) public Outpost(OutpostManager host, ClanInfo clan, Location location, OutpostType type)
{ {
_host = host; _host = host;
location.add(.5, 0, .5);
_id = Integer.toString(UtilMath.random.nextInt(13333337)); _id = Integer.toString(UtilMath.random.nextInt(13333337));
_siegeWeaponDistance = type._size + 27.5; _siegeWeaponDistance = type._size + 27.5;
@ -201,18 +214,49 @@ public class Outpost implements Listener
_preHologram.start(); _preHologram.start();
_preHologram2.start(); _preHologram2.start();
_dataEntity = location.getWorld().spawn(location, ArmorStand.class); _dataEntity = location.getWorld().spawn(location.subtract(0, 5, 0), ArmorStand.class);
_dataEntity.setVisible(false);
_dataEntity.setGravity(false);
_state = OutpostState.AWAITING; _state = OutpostState.AWAITING;
} }
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() private void updateData()
{ {
try try
{ {
String data = ""; 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)); _dataEntity.setMetadata("$SERIALIZED_OUTPOST_DATA$", new FixedMetadataValue(_host.getPlugin(), data));
} }
@ -222,6 +266,15 @@ public class Outpost implements Listener
} }
} }
@EventHandler
public void saveData(UpdateEvent event)
{
if (event.getType() == UpdateType.SEC_05)
{
updateData();
}
}
private void cleanup() private void cleanup()
{ {
_blocks = null; _blocks = null;
@ -237,6 +290,8 @@ public class Outpost implements Listener
_weapons.forEach(SiegeWeapon::kill); _weapons.forEach(SiegeWeapon::kill);
_weapons.clear(); _weapons.clear();
_dataEntity.remove();
_host.queueForRemoval(_owner.getName()); _host.queueForRemoval(_owner.getName());
} }

View File

@ -1,14 +1,10 @@
package mineplex.game.clans.clans.siege.outpost; package mineplex.game.clans.clans.siege.outpost;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
@ -39,6 +35,7 @@ import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.clans.event.PlayerClaimTerritoryEvent; import mineplex.game.clans.clans.event.PlayerClaimTerritoryEvent;
import mineplex.game.clans.clans.siege.SiegeManager; import mineplex.game.clans.clans.siege.SiegeManager;
import mineplex.game.clans.clans.siege.events.SiegeWeaponExplodeEvent; import mineplex.game.clans.clans.siege.events.SiegeWeaponExplodeEvent;
import mineplex.game.clans.clans.siege.weapon.serialization.OutpostToken;
import mineplex.game.clans.core.repository.ClanTerritory; import mineplex.game.clans.core.repository.ClanTerritory;
import mineplex.game.clans.spawn.Spawn; import mineplex.game.clans.spawn.Spawn;
@ -80,158 +77,12 @@ public class OutpostManager extends MiniPlugin
continue; continue;
} }
ArmorStand stand = (ArmorStand) entity;
String data = entity.getMetadata("$SERIALIZED_OUTPOST_DATA$").get(0).asString(); String data = entity.getMetadata("$SERIALIZED_OUTPOST_DATA$").get(0).asString();
ByteArrayInputStream stream = new ByteArrayInputStream(data.getBytes("UTF-8")); Outpost outpost = new Outpost(this, _siegeManager.getGson().fromJson(data, OutpostToken.class));
DataInputStream dis = new DataInputStream(stream);
String id; _outposts.put(outpost.getOwner().getName(), outpost);
Location origin; _idToOutpost.put(outpost.getId(), outpost);
OutpostType outpostType;
ClanInfo ownerClan;
double health;
long spawnTime;
ClanInfo againstClan;
long siegeDeclaredTime;
OutpostState outpostState;
// Read UUID
{
int uuidLength = dis.readInt();
StringBuilder clanName = new StringBuilder();
for (int i = 0; i < uuidLength; i++)
{
clanName.append((char) dis.readByte());
}
id = clanName.toString();
}
// Read origin location
{
int worldLength = dis.readInt();
StringBuilder worldName = new StringBuilder();
for (int i = 0; i < worldLength; i++)
{
worldName.append((char) dis.readByte());
}
String world = worldName.toString();
int x = dis.readInt();
int y = dis.readInt();
int z = dis.readInt();
origin = new Location(Bukkit.getWorld(world), x, y, z);
}
// Read outpost type
{
int typeLength = dis.readInt();
StringBuilder type = new StringBuilder();
for (int i = 0; i < typeLength; i++)
{
type.append((char) dis.readByte());
}
outpostType = OutpostType.valueOf(type.toString());
}
// Read owner clan
{
int clanNameLength = dis.readInt();
StringBuilder clanName = new StringBuilder();
for (int i = 0; i < clanNameLength; i++)
{
clanName.append((char) dis.readByte());
}
ClanInfo clan = _clansManager.getClanUtility().getClanByClanName(clanName.toString());
if (clan == null)
{
System.out.println("OUTPOSTMANAGER: Tried to load Outpost (ID: " + id + ") but did not find owner clan.");
return;
}
ownerClan = clan;
}
// Read health
{
health = dis.readDouble();
}
// Read spawn time
{
spawnTime = dis.readLong();
}
// Read "against" clan
{
int clanNameLength = dis.readInt();
if (clanNameLength == 0)
{
againstClan = null;
}
else
{
StringBuilder clanName = new StringBuilder();
for (int i = 0; i < clanNameLength; i++)
{
clanName.append((char) dis.readByte());
}
ClanInfo clan = _clansManager.getClanUtility().getClanByClanName(clanName.toString());
if (clan == null)
{
System.out.println("OUTPOSTMANAGER: Tried to load Outpost (ID: " + id + ") but did not find against clan.");
return;
}
againstClan = clan;
}
}
// Read siegeDeclaredTime
{
siegeDeclaredTime = dis.readLong();
}
// Read outpost state
{
int stateLength = dis.readInt();
StringBuilder state = new StringBuilder();
for (int i = 0; i < stateLength; i++)
{
state.append((char) dis.readByte());
}
outpostState = OutpostState.valueOf(state.toString());
}
dis.close();
stream.close();
Outpost outpost = new Outpost(this, id, origin, ownerClan, outpostType, health, spawnTime, againstClan, siegeDeclaredTime, outpostState);
_outposts.put(ownerClan.getName(), outpost);
_idToOutpost.put(id, outpost);
} }
} }
catch (Exception e) catch (Exception e)
@ -514,4 +365,14 @@ public class OutpostManager extends MiniPlugin
return false; return false;
} }
public Outpost Get(String outpostId)
{
return _idToOutpost.get(outpostId);
}
public ClansManager getClansManager()
{
return _clansManager;
}
} }

View File

@ -1,9 +1,13 @@
package mineplex.game.clans.clans.siege.weapon; package mineplex.game.clans.clans.siege.weapon;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Slime; import org.bukkit.entity.Slime;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -26,6 +30,7 @@ import mineplex.game.clans.clans.siege.SiegeManager;
import mineplex.game.clans.clans.siege.outpost.Outpost; import mineplex.game.clans.clans.siege.outpost.Outpost;
import mineplex.game.clans.clans.siege.weapon.projectile.ProjectileAttributes; 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.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.AccessRule;
import mineplex.game.clans.clans.siege.weapon.util.AccessType; import mineplex.game.clans.clans.siege.weapon.util.AccessType;
import mineplex.game.clans.clans.siege.weapon.util.WeaponStateInfo; import mineplex.game.clans.clans.siege.weapon.util.WeaponStateInfo;
@ -34,11 +39,77 @@ public class Cannon extends SiegeWeapon
{ {
public static final ItemStack CANNON_ITEM = new ItemBuilder(Material.IRON_BLOCK, 1).setRawTitle(C.cBlue + "Cannon").setLore(C.cWhite + "BOOM BABY!").build(); public static final ItemStack CANNON_ITEM = new ItemBuilder(Material.IRON_BLOCK, 1).setRawTitle(C.cBlue + "Cannon").setLore(C.cWhite + "BOOM BABY!").build();
public Cannon(SiegeManager siegeManager, SiegeWeaponToken token)
{
super(1400.d, "Cannon", token, siegeManager.getClansManager(), siegeManager);
setBoundingBox(1);
setStateInfo("Unloaded", new WeaponStateInfo(Material.SPONGE, (byte) 1));
setStateInfo("Loaded", new WeaponStateInfo(Material.SPONGE, (byte) 0));
setFirepowerType(Material.SULPHUR);
setAmmunitionType(Material.TNT);
setFirepowerSlots(Integer.valueOf(1), Integer.valueOf(3), Integer.valueOf(5), Integer.valueOf(7));
setMaximumFirepowerPerSlot(3);
setAmmunitionSlot(4);
setMaximumAmmunitionPerSlot(1);
_baseDamage = 650;
setProjectileAttributes(new ProjectileAttributes().setPrimedTnt().setDoCrater().craterSize(3).craterChanceOfAir(2.d));
setFireRule(new AccessRule(AccessType.LCLICK_BB, player -> {
if (!isRiding(player))
{
return false;
}
if (!_owner.isMember(player))
{
UtilPlayer.message(player, F.main("Clans", "This cannon is not owned by your Clan."));
return false;
}
if (!canBeFired())
{
UtilPlayer.message(player, F.main("Clans", "Cannon is not loaded correctly."));
return false;
}
if (System.currentTimeMillis() - _lastFired < 20000)
{
UtilPlayer.message(player, F.main("Clans", "Cannon is cooling down (" + F.time(UtilTime.MakeStr(20000 - (System.currentTimeMillis() - _lastFired))) + ")"));
return false;
}
return true;
}));
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))
{
UtilPlayer.message(player, F.main("Clans", "This cannon is not owned by your Clan."));
return false;
}
if (getRider() != null && !getRider().equals(player))
{
UtilPlayer.message(player, F.main("Clans", "Someone is already riding this cannon."));
return false;
}
return !player.equals(getRider());
}));
}
public Cannon(Location location, ClanInfo clan, SiegeManager siegeManager, Outpost outpost) public Cannon(Location location, ClanInfo clan, SiegeManager siegeManager, Outpost outpost)
{ {
super(location, 1400.d, "Cannon", clan, clan.Clans, siegeManager, outpost); super(2, location, 1400.d, "Cannon", clan, clan.Clans, siegeManager, outpost);
location.add(.5, 0, .5);
setBoundingBox(1); setBoundingBox(1);

View File

@ -26,6 +26,7 @@ import mineplex.game.clans.clans.siege.SiegeManager;
import mineplex.game.clans.clans.siege.outpost.Outpost; import mineplex.game.clans.clans.siege.outpost.Outpost;
import mineplex.game.clans.clans.siege.weapon.projectile.ProjectileAttributes; 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.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.AccessRule;
import mineplex.game.clans.clans.siege.weapon.util.AccessType; import mineplex.game.clans.clans.siege.weapon.util.AccessType;
import mineplex.game.clans.clans.siege.weapon.util.WeaponStateInfo; import mineplex.game.clans.clans.siege.weapon.util.WeaponStateInfo;
@ -34,9 +35,80 @@ public class Catapult extends SiegeWeapon
{ {
public static final ItemStack CATAPULT_ITEM = new ItemBuilder(Material.GOLD_BLOCK, 1).setRawTitle(C.cBlue + "Catapult").setLore(C.cWhite + "AWHHH YEAH!!!").build(); public static final ItemStack CATAPULT_ITEM = new ItemBuilder(Material.GOLD_BLOCK, 1).setRawTitle(C.cBlue + "Catapult").setLore(C.cWhite + "AWHHH YEAH!!!").build();
public Catapult(SiegeManager siegeManager, SiegeWeaponToken token)
{
super(1400.d, "Catapult", token, siegeManager.getClansManager(), siegeManager);
setBoundingBox(3, 0);
setStateInfo("Unloaded", new WeaponStateInfo(Material.PACKED_ICE, (byte) 0));
setStateInfo("Loaded", new WeaponStateInfo(Material.ENDER_PORTAL_FRAME, (byte) 0));
setStateInfo("Fired", new WeaponStateInfo(Material.LAPIS_BLOCK, (byte) 0));
setFirepowerType(Material.FIREBALL);
setAmmunitionType(Material.COBBLESTONE);
setFirepowerSlots(Integer.valueOf(1), Integer.valueOf(3), Integer.valueOf(5), Integer.valueOf(7));
setMaximumFirepowerPerSlot(3);
setAmmunitionSlot(4);
setMaximumAmmunitionPerSlot(1);
setProjectileAttributes(new ProjectileAttributes().setFallingBlock().setFallingBlockType(Material.TNT).setDoCrater().craterSize(3).craterChanceOfAir(1.3d));
_baseDamage = 550;
_rotSpeed = 60.0f;
setFireRule(new AccessRule(AccessType.LCLICK_BB, player -> {
if (!isRiding(player))
{
return false;
}
if (!_owner.isMember(player))
{
UtilPlayer.message(player, F.main("Clans", "This Catapult is not owned by your Clan."));
return false;
}
if (!canBeFired())
{
UtilPlayer.message(player, F.main("Clans", "Catapult is not loaded correctly."));
return false;
}
if (System.currentTimeMillis() - _lastFired < 20000)
{
UtilPlayer.message(player, F.main("Clans", "Catapult is cooling down (" + F.time(UtilTime.MakeStr(20000 - (System.currentTimeMillis() - _lastFired))) + ")"));
return false;
}
return true;
}));
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))
{
UtilPlayer.message(player, F.main("Clans", "This Catapult is not owned by your Clan."));
return false;
}
if (getRider() != null && !getRider().equals(player))
{
UtilPlayer.message(player, F.main("Clans", "Someone is already riding this Catapult."));
return false;
}
return !player.equals(getRider());
}));
}
public Catapult(Location location, ClanInfo clan, SiegeManager siegeManager, Outpost outpost) public Catapult(Location location, ClanInfo clan, SiegeManager siegeManager, Outpost outpost)
{ {
super(location, 2500.d, "Catapult", clan, clan.Clans, siegeManager, outpost); super(1, location, 2500.d, "Catapult", clan, clan.Clans, siegeManager, outpost);
setBoundingBox(3, 0); setBoundingBox(3, 0);

View File

@ -7,6 +7,7 @@ import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -27,6 +28,7 @@ import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.EulerAngle; import org.bukkit.util.EulerAngle;
import org.spigotmc.event.entity.EntityDismountEvent; import org.spigotmc.event.entity.EntityDismountEvent;
@ -50,6 +52,7 @@ import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.clans.siege.SiegeManager; import mineplex.game.clans.clans.siege.SiegeManager;
import mineplex.game.clans.clans.siege.events.SiegeWeaponExplodeEvent; import mineplex.game.clans.clans.siege.events.SiegeWeaponExplodeEvent;
import mineplex.game.clans.clans.siege.outpost.Outpost; import mineplex.game.clans.clans.siege.outpost.Outpost;
import mineplex.game.clans.clans.siege.outpost.OutpostManager;
import mineplex.game.clans.clans.siege.weapon.projectile.ProjectileAttributes; 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.projectile.WeaponProjectile;
import mineplex.game.clans.clans.siege.weapon.serialization.SiegeWeaponToken; import mineplex.game.clans.clans.siege.weapon.serialization.SiegeWeaponToken;
@ -63,6 +66,8 @@ public abstract class SiegeWeapon implements Listener
protected ClansManager _clans; protected ClansManager _clans;
protected SiegeManager _siegeManager; protected SiegeManager _siegeManager;
protected String _id;
/** /**
* THIS MAY BE NULL IF NOT PLACED IN OUTPOST * THIS MAY BE NULL IF NOT PLACED IN OUTPOST
*/ */
@ -87,7 +92,7 @@ public abstract class SiegeWeapon implements Listener
protected final List<Entity> _comprisedOf; protected final List<Entity> _comprisedOf;
// Friendly-name mapping to index in _comprisedOf for easier management of entities. // Friendly-name mapping to index in _comprisedOf for easier management of entities.
private final Map<String, Entity> _entityMapping; protected final Map<String, Entity> _entityMapping;
private boolean _isRideable; private boolean _isRideable;
private AccessRule _mountAccess; private AccessRule _mountAccess;
@ -129,8 +134,60 @@ public abstract class SiegeWeapon implements Listener
protected final int _weaponTypeIdentifier; protected final int _weaponTypeIdentifier;
private ArmorStand _dataEntity;
public SiegeWeapon(double maxHealth, String name, SiegeWeaponToken token, ClansManager clansManager, SiegeManager siegeManager)
{
_weaponTypeIdentifier = token.WeaponType;
_id = token.Id;
_outpost = siegeManager.getOutpostManager().Get(token.OutpostId);
_siegeManager = siegeManager;
_location = token.Location;
_name = name;
_health = _maxHealth = maxHealth;
_owner = clansManager.getClan(token.OwnerClan);
_comprisedOf = Lists.newArrayList();
_registeredStates = Maps.newHashMap();
_entityMapping = Maps.newHashMap();
_infoHologram = new Hologram(ClansManager.getInstance().getHologramManager(), _location.clone().add(.5, 3, .5), _name + " Health", getDisplayHealth());
_infoHologram.start();
UtilServer.getPluginManager().registerEvents(this, clansManager.getPlugin());
_clans = clansManager;
for (Entity entity : token.Location.getWorld().getEntities())
{
for (Entry<String, UUID> entry : token.ComprisedOf.entrySet())
{
if (entity.getUniqueId().equals(entry.getValue()))
{
_comprisedOf.add(entity);
_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;
_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, double maxHealth, String name, ClanInfo owner, ClansManager clansManager, SiegeManager siegeManager, Outpost outpost)
{ {
_id = Integer.toString(UtilMath.random.nextInt(13333337));
_weaponTypeIdentifier = typeId; _weaponTypeIdentifier = typeId;
_outpost = outpost; _outpost = outpost;
@ -150,6 +207,10 @@ public abstract class SiegeWeapon implements Listener
UtilServer.getPluginManager().registerEvents(this, clansManager.getPlugin()); UtilServer.getPluginManager().registerEvents(this, clansManager.getPlugin());
_clans = clansManager; _clans = clansManager;
_dataEntity = location.getWorld().spawn(location.subtract(0, 5, 0), ArmorStand.class);
_dataEntity.setVisible(false);
_dataEntity.setGravity(false);
} }
public SiegeWeaponToken tokenize() public SiegeWeaponToken tokenize()
@ -166,11 +227,13 @@ public abstract class SiegeWeapon implements Listener
token.ComprisedOf = comprisedOf; token.ComprisedOf = comprisedOf;
token.OwnerClan = _owner; token.OwnerClan = _owner.getName();
token.WeaponType = _weaponTypeIdentifier; token.WeaponType = _weaponTypeIdentifier;
token.OutpostId = _outpost == null ? null : _outpost.getId(); token.OutpostId = _outpost.getId();
token.Id = _id;
token.Location = _location; token.Location = _location;
@ -178,13 +241,38 @@ public abstract class SiegeWeapon implements Listener
token.Yaw = _yaw; token.Yaw = _yaw;
token.Rider = _rider; token.DataEntityUUID = _dataEntity.getUniqueId();
token.Rider = _rider.getUniqueId();
token.LastFired = _lastFired; token.LastFired = _lastFired;
return token; 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() public int getBaseDamage()
{ {
return _baseDamage; return _baseDamage;
@ -409,6 +497,11 @@ public abstract class SiegeWeapon implements Listener
return player.equals(getRider()); return player.equals(getRider());
} }
public String getId()
{
return _id;
}
protected void setRideable(AccessRule accessRule) protected void setRideable(AccessRule accessRule)
{ {
_isRideable = true; _isRideable = true;
@ -427,6 +520,8 @@ public abstract class SiegeWeapon implements Listener
_comprisedOf.clear(); _comprisedOf.clear();
_infoHologram.stop(); _infoHologram.stop();
_dataEntity.remove();
_siegeManager.dead(this); _siegeManager.dead(this);
_alive = false; _alive = false;

View File

@ -1,19 +1,24 @@
package mineplex.game.clans.clans.siege.weapon.serialization; package mineplex.game.clans.clans.siege.weapon.serialization;
import java.util.UUID;
import org.bukkit.Location; 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.OutpostState;
import mineplex.game.clans.clans.siege.outpost.OutpostType; import mineplex.game.clans.clans.siege.outpost.OutpostType;
public class OutpostToken public class OutpostToken
{ {
public String Id; public String Id;
public Location Origin; public GsonLocation Origin;
public OutpostType Type; public OutpostType Type;
public String OwnerClan; public String OwnerClan;
public double Health; public double Health;
public long TimeSpawned; public long TimeSpawned;
public String againstClan; public String AgainstClan;
public long siegeDeclaredTime; public long SiegeDeclaredTime;
public OutpostState outpostState; public OutpostState OutpostState;
public UUID DataEntityUUID;
} }

View File

@ -4,19 +4,20 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player;
import mineplex.game.clans.clans.ClanInfo; import mineplex.core.common.GsonLocation;
public class SiegeWeaponToken public class SiegeWeaponToken
{ {
public ClanInfo OwnerClan; public String OwnerClan;
public int WeaponType; public int WeaponType;
public String OutpostId; public String OutpostId;
public Location Location; public GsonLocation Location;
public Map<String, UUID> ComprisedOf; public Map<String, UUID> ComprisedOf;
public double Health; public double Health;
public double Yaw; public double Yaw;
public Player Rider; public UUID Rider;
public String Id;
public long LastFired; public long LastFired;
public UUID DataEntityUUID;
} }