progress on saving outposts/wepaons so far. not finished. probably going to change it to a different format

This commit is contained in:
NewGarbo 2016-02-11 17:08:05 +00:00
parent 67b12f3ae2
commit 90570c1185
23 changed files with 633 additions and 606 deletions

View File

@ -1,40 +0,0 @@
package mineplex.core.common.file;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import com.google.common.collect.Lists;
public class DataFileInStream
{
private Iterator<Byte> _iterator;
public DataFileInStream(String file) throws IOException
{
this(Lists.newArrayList(ArrayUtils.toObject(UtilFile.readAllBytes(file))));
}
public DataFileInStream(List<Byte> chunks)
{
_iterator = chunks.iterator();
}
public DataFileInStream(Byte[] chunks)
{
this(Lists.newArrayList(chunks));
}
public Byte read()
{
return _iterator.next();
}
public void dispose()
{
_iterator = null;
}
}

View File

@ -1,100 +0,0 @@
package mineplex.core.common.file;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import mineplex.core.common.file.chunks.DataChunk;
public class DataFileOutStream
{
private List<DataChunk<?>> _chunks;
public DataFileOutStream()
{
_chunks = new ArrayList<>();
}
public DataFileOutStream(List<DataChunk<?>> chunks)
{
_chunks = chunks;
}
public DataFileOutStream(DataChunk<?>[] chunks)
{
this(Lists.newArrayList(chunks));
}
public void add(DataChunk<?> chunk)
{
_chunks.add(chunk);
}
private byte[] ensureSize(byte[] bytes, int size)
{
byte[] newBytes = bytes;
if (bytes.length < size)
{
newBytes = Arrays.copyOf(bytes, size);
}
return newBytes;
}
public void writeToFile(String file) throws IOException
{
byte[] bytes = new byte[1];
for (DataChunk<?> chunk : _chunks)
{
int oldLength = bytes.length;
byte[] chunkBytes = chunk.toBytes();
bytes = ensureSize(bytes, chunkBytes.length + bytes.length);
for (int i = oldLength; i < chunkBytes.length; i++)
{
bytes[i - 1] = chunkBytes[(i - 1) - oldLength];
}
}
UtilFile.writeToFile(file, bytes);
}
public String writeBytesToString()
{
byte[] bytes = new byte[1];
for (DataChunk<?> chunk : _chunks)
{
int oldLength = bytes.length;
byte[] chunkBytes = chunk.toBytes();
bytes = ensureSize(bytes, chunkBytes.length + bytes.length);
for (int i = oldLength; i < chunkBytes.length; i++)
{
bytes[i - 1] = chunkBytes[(i - 1) - oldLength];
}
}
return new String(bytes);
}
public void release()
{
_chunks.clear();
}
public List<DataChunk<?>> getChunks()
{
return _chunks;
}
}

View File

@ -1,79 +0,0 @@
package mineplex.core.common.file;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
public class UtilFile
{
public static void writePlainFile(File file, String text) throws FileNotFoundException
{
PrintWriter writer = new PrintWriter(file);
writer.print(text);
writer.close();
}
/**
* Will read the specified file, and return the contents, or a null value,
* if an exception is thrown. Will handle all exceptions, and simply ignore
* them and return null. No stack trace printed or anything.
*/
public static String readIgnoreErrors(File file)
{
try
{
return readToStr(file);
}
catch (IOException exception)
{
return null;
}
}
public static String readToStr(File file) throws IOException
{
return new String(readAllBytes(file));
}
public static byte[] readAllBytes(File file) throws IOException
{
return Files.readAllBytes(Paths.get(file.toURI()));
}
public static void writePlainFile(String file, String text) throws FileNotFoundException
{
writePlainFile(new File(file), text);
}
/**
* Will read the specified file, and return the contents, or a null value,
* if an exception is thrown. Will handle all exceptions, and simply ignore
* them and return null. No stack trace printed or anything.
*/
public static String readIgnoreErrors(String file)
{
return readIgnoreErrors(new File(file));
}
public static String readToStr(String file) throws IOException
{
return readToStr(new File(file));
}
public static byte[] readAllBytes(String file) throws IOException
{
return readAllBytes(new File(file));
}
public static void writeToFile(String file, byte[] bytes) throws IOException
{
FileOutputStream stream = new FileOutputStream(new File(file));
stream.write(bytes);
stream.close();
}
}

View File

@ -1,20 +0,0 @@
package mineplex.core.common.file.chunks;
import java.io.DataOutputStream;
import java.io.IOException;
public abstract class DataChunk<Type>
{
protected Type _value;
public DataChunk(Type value)
{
_value = value;
}
public abstract void WriteTo(DataOutputStream stream) throws IOException;
public abstract byte[] toBytes();
public abstract boolean IsValid(Type value);
}

View File

@ -1,35 +0,0 @@
package mineplex.core.common.file.chunks;
import java.io.DataOutputStream;
import java.io.IOException;
import mineplex.core.common.file.DataFileInStream;
public class DataChunkByte extends DataChunk<Byte>
{
public DataChunkByte(byte value)
{
super(Byte.valueOf(value));
}
public boolean IsValid(Byte value)
{
return value != null;
}
public void WriteTo(DataOutputStream stream) throws IOException
{
stream.write(toBytes());
}
public static Byte ReadFrom(DataFileInStream stream) throws IOException
{
return stream.read();
}
public byte[] toBytes()
{
return new byte[] { _value.byteValue() };
}
}

View File

@ -1,45 +0,0 @@
package mineplex.core.common.file.chunks;
import java.io.DataOutputStream;
import java.io.IOException;
import mineplex.core.common.file.DataFileInStream;
public class DataChunkInt extends DataChunk<Integer>
{
public DataChunkInt(int value)
{
super(Integer.valueOf(value));
}
public boolean IsValid(Integer value)
{
return value != null;
}
public void WriteTo(DataOutputStream stream) throws IOException
{
stream.write(toBytes());
}
public static Integer ReadFrom(DataFileInStream stream) throws IOException
{
int part1 = stream.read().byteValue();
int part2 = stream.read().byteValue();
int part3 = stream.read().byteValue();
int part4 = stream.read().byteValue();
return Integer.valueOf(part1 << 24 + part2 << 16 + part3 << 8 + part4);
}
public byte[] toBytes()
{
byte part1 = (byte) ((_value.intValue() >>> 24) & 0xFF);
byte part2 = (byte) ((_value.intValue() >>> 16) & 0xFF);
byte part3 = (byte) ((_value.intValue() >>> 8) & 0xFF);
byte part4 = (byte) (_value.intValue() & 0xFF);
return new byte[] { part1, part2, part3, part4 };
}
}

View File

@ -1,36 +0,0 @@
package mineplex.core.common.file.chunks;
import java.io.DataOutputStream;
import java.io.IOException;
import mineplex.core.common.file.DataFileInStream;
import mineplex.core.common.util.UtilEncode;
public class DataChunkLong extends DataChunk<Long>
{
public DataChunkLong(long value)
{
super(Long.valueOf(value));
}
public boolean IsValid(Long value)
{
return value != null;
}
public void WriteTo(DataOutputStream stream) throws IOException
{
stream.write(toBytes());
}
public static Long ReadFrom(DataFileInStream stream) throws IOException
{
return Long.valueOf(UtilEncode.bytesToLong(new byte[] { stream.read().byteValue(), stream.read().byteValue(), stream.read().byteValue(), stream.read().byteValue(), stream.read().byteValue(), stream.read().byteValue(), stream.read().byteValue(), stream.read().byteValue() }));
}
public byte[] toBytes()
{
return UtilEncode.longToBytes(_value.longValue());
}
}

View File

@ -1,74 +0,0 @@
package mineplex.core.common.file.chunks;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
import mineplex.core.common.file.DataFileInStream;
import mineplex.core.common.util.UtilEncode;
public class DataChunkString extends DataChunk<String>
{
private static final String ENCODING = "UTF-8";
public DataChunkString(String value)
{
super(value);
}
public boolean IsValid(String value)
{
return value != null;
}
public void WriteTo(DataOutputStream stream) throws IOException
{
stream.write(toBytes());
}
public static String ReadFrom(DataFileInStream stream) throws IOException
{
int length = DataChunkInt.ReadFrom(stream).intValue();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; i++)
{
builder.append(DataChunkByte.ReadFrom(stream));
}
return null;
}
public byte[] toBytes()
{
try
{
/* four extra bytes reserved for the size of the string represented as an int */
byte[] bytes = new byte[4 + _value.getBytes(ENCODING).length];
byte[] intBytes = UtilEncode.intToBytes(_value.getBytes(ENCODING).length);
for (int i = 0; i < intBytes.length; i++)
{
bytes[i] = intBytes[i];
}
for (int i = 0; i < _value.getBytes(ENCODING).length; i++)
{
bytes[4 + i] = _value.getBytes(ENCODING)[i];
}
return bytes;
}
catch (Exception e)
{
return UtilEncode.intToBytes(0);
}
}
}

View File

@ -0,0 +1,43 @@
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

@ -1,116 +0,0 @@
package mineplex.core.common.util;
public class UtilEncode
{
public static byte[] intToBytes(int value)
{
byte part1 = (byte) ((value >>> 24) & 0xFF);
byte part2 = (byte) ((value >>> 16) & 0xFF);
byte part3 = (byte) ((value >>> 8) & 0xFF);
byte part4 = (byte) (value & 0xFF);
return new byte[] { part1, part2, part3, part4 };
}
public static byte[] booleanToBytes(boolean value)
{
return new byte[] { (byte) (value ? 1 : 0) };
}
public static byte[] doubleToBytes(double value)
{
return longToBytes(Double.doubleToLongBits(value));
}
public static byte[] floatToBytes(float value)
{
return intToBytes(Float.floatToIntBits(value));
}
public static byte[] charToBytes(char value)
{
byte part1 = (byte) ((value >>> 24) & 0xFF);
byte part2 = (byte) (value & 0xFF);
return new byte[] { part1, part2 };
}
public static byte[] shortToBytes(short value)
{
byte part1 = (byte) ((value >>> 24) & 0xFF);
byte part2 = (byte) (value & 0xFF);
return new byte[] { part1, part2 };
}
public static byte[] longToBytes(long value)
{
byte part1 = (byte) (value >>> 56);
byte part2 = (byte) (value >>> 48);
byte part3 = (byte) (value >>> 40);
byte part4 = (byte) (value >>> 32);
byte part5 = (byte) (value >>> 24);
byte part6 = (byte) (value >>> 16);
byte part7 = (byte) (value >>> 8);
byte part8 = (byte) (value);
return new byte[] { part1, part2, part3, part4, part5, part6, part7, part8 };
}
public static long bytesToLong(byte[] bytes)
{
long part1 = ((long) bytes[0]) << 56;
long part2 = (((long) bytes[1]) & 255) << 48;
long part3 = (((long) bytes[2]) & 255) << 40;
long part4 = (((long) bytes[3]) & 255) << 32;
long part5 = (((long) bytes[4]) & 255) << 24;
long part6 = (((long) bytes[5]) & 255) << 16;
long part7 = (((long) bytes[6]) & 255) << 8;
long part8 = (((long) bytes[7]) & 255);
return part1 + part2 + part3 + part4 + part5 + part6 + part7 + part8;
}
public static int bytesToInt(byte[] bytes)
{
int part1 = ((int) bytes[0]) << 24;
int part2 = ((int) bytes[1]) << 16;
int part3 = ((int) bytes[2]) << 8;
int part4 = ((int) bytes[3]);
return part1 + part2 + part3 + part4;
}
public static boolean bytesToBoolean(byte[] bytes)
{
return bytes[0] == 1;
}
// public static byte[] bytesToDouble(byte[] bytes)
// {
// return Double.longBitsToDouble();
// }
//
// public static byte[] floatToBytes(float value)
// {
// return intToBytes(Float.floatToIntBits(value));
// }
//
// public static byte[] charToBytes(char value)
// {
// byte part1 = (byte) ((value >>> 24) & 0xFF);
// byte part2 = (byte) (value & 0xFF);
//
// return new byte[] { part1, part2 };
// }
//
// public static byte[] shortToBytes(short value)
// {
// byte part1 = (byte) ((value >>> 24) & 0xFF);
// byte part2 = (byte) (value & 0xFF);
//
// return new byte[] { part1, part2 };
// }
}

View File

@ -189,5 +189,93 @@ public class UtilMath
public static boolean isEven(int size)
{
return size % 2 == 0;
}
public static byte[] getBits(int value)
{
byte[] bits = new byte[32];
String bit = Long.toBinaryString(value);
while (bit.length() < 32)
{
bit = "0" + bit;
}
int index = 0;
for (char c : bit.toCharArray())
{
bits[index] = (byte) (c == '1' ? '1' : '0');
index++;
}
return bits;
}
public static byte[] getBits(long value)
{
byte[] bits = new byte[64];
String bit = Long.toBinaryString(value);
while (bit.length() < 64)
{
bit = "0" + bit;
}
int index = 0;
for (char c : bit.toCharArray())
{
bits[index] = (byte) (c == '1' ? '1' : '0');
index++;
}
return bits;
}
public static byte[] getBits(byte value)
{
byte[] bits = new byte[8];
String bit = Long.toBinaryString(value);
while (bit.length() < 8)
{
bit = "0" + bit;
}
int index = 0;
for (char c : bit.toCharArray())
{
bits[index] = (byte) (c == '1' ? '1' : '0');
index++;
}
return bits;
}
public static byte[] getBits(short value)
{
byte[] bits = new byte[16];
String bit = Long.toBinaryString(value);
while (bit.length() < 16)
{
bit = "0" + bit;
}
int index = 0;
for (char c : bit.toCharArray())
{
bits[index] = (byte) (c == '1' ? '1' : '0');
index++;
}
return bits;
}
}

View File

@ -0,0 +1,24 @@
package mineplex.game.clans.clans;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(stream);
dos.writeByte(91);
dos.flush();
stream.flush();
System.out.println(new String(stream.toByteArray()));
dos.close();
stream.close();
}
}

View File

@ -2,8 +2,11 @@ package mineplex.game.clans.clans.siege;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
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;
@ -11,7 +14,11 @@ import org.bukkit.event.block.BlockPlaceEvent;
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.gson.UUIDTypeAdapter;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilInv;
import mineplex.core.common.util.UtilPlayer;
@ -24,7 +31,9 @@ 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.TokenInfo;
import mineplex.game.clans.core.repository.ClanTerritory;
import mineplex.game.clans.spawn.Spawn;
public class SiegeManager extends MiniPlugin
{
@ -36,19 +45,99 @@ public class SiegeManager extends MiniPlugin
public List<SiegeWeapon> LiveSiegeWeapons = new ArrayList<>();
private Gson _gson;
public SiegeManager(JavaPlugin plugin, ClansManager clans)
{
super("Siege", plugin);
_gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
_clans = clans;
_outpostManager = new OutpostManager(clans, this);
_outpostManager.loadExistingOutposts();
loadExistingWeapons();
addCommand(new CommandSiegeSupplies(_outpostManager));
Instance = this;
}
/**
* data clusters:
* STRING : {
* int(4BYTE) length;
* byte[] bytes;
* }
*
* LOCATION : {
* 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()
{
try
{
for (Entity entity : Spawn.getSpawnWorld().getEntities())
{
if (!(entity instanceof ArmorStand))
{
continue;
}
if (!entity.hasMetadata("$SERIALIZED_SIEGE_WEAPON_DATA$") ||!entity.hasMetadata("$TOKEN_INFO$"))
{
continue;
}
ArmorStand stand = (ArmorStand) entity;
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);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
@EventHandler
public void projectileHit(ProjectileHitEvent event)
{

View File

@ -1,5 +1,7 @@
package mineplex.game.clans.clans.siege.outpost;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
@ -12,6 +14,7 @@ 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.FallingBlock;
import org.bukkit.entity.Player;
@ -75,7 +78,7 @@ public class Outpost implements Listener
private OutpostManager _host;
private UUID _uuid = UUID.randomUUID();
private final String _id;
private ClanInfo _owner;
@ -122,10 +125,56 @@ public class Outpost implements Listener
private long _siegeDeclaredTime = -1;
private Arrow _declarationArrow;
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)
{
_host = outpostManager;
_health = health;
_siegeDeclaredTime = siegeDeclaredTime;
_against = againstClan;
_id = id;
_siegeWeaponDistance = outpostType._size + 27.5;
_owner = ownerClan;
_startCorner = origin.clone().subtract(outpostType._size, 1.1, outpostType._size);
_endCorner = origin.clone().add(outpostType._size + .9, outpostType._ySize - 1, outpostType._size + .9);
_weapons = new ArrayList<>();
_forceFieldStart = _startCorner.clone().subtract(4, 0, 4);
_forceFieldEnd = _endCorner.clone().add(4.5, 0, 4.5);
_origin = origin;
_type = outpostType;
_spawnTime = spawnTime;
_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.start();
_preHologram2.start();
_dataEntity = origin.getWorld().spawn(origin, ArmorStand.class);
_state = outpostState;
}
public Outpost(OutpostManager host, ClanInfo clan, Location location, OutpostType type)
{
_host = host;
_id = Integer.toString(UtilMath.random.nextInt(13333337));
_siegeWeaponDistance = type._size + 27.5;
_owner = clan;
@ -135,8 +184,8 @@ public class Outpost implements Listener
_weapons = new ArrayList<>();
_forceFieldStart = _startCorner.clone().subtract(3, 0, 3);
_forceFieldEnd = _endCorner.clone().add(3, 0, 3);
_forceFieldStart = _startCorner.clone().subtract(4, 0, 4);
_forceFieldEnd = _endCorner.clone().add(4.5, 0, 4.5);
_origin = location.clone();
@ -152,9 +201,27 @@ public class Outpost implements Listener
_preHologram.start();
_preHologram2.start();
_dataEntity = location.getWorld().spawn(location, ArmorStand.class);
_state = OutpostState.AWAITING;
}
private void updateData()
{
try
{
String data = "";
_dataEntity.setMetadata("$SERIALIZED_OUTPOST_DATA$", new FixedMetadataValue(_host.getPlugin(), data));
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void cleanup()
{
_blocks = null;
@ -210,12 +277,16 @@ public class Outpost implements Listener
@EventHandler(priority = EventPriority.HIGHEST)
public void fireBow(EntityShootBowEvent event)
{
if (!(event.getEntity() instanceof Player))
if (!(event.getEntity() instanceof Player) || !(event.getProjectile() instanceof Arrow))
{
return;
}
event.setCancelled(true);
Player player = (Player) event.getEntity();
Arrow arrow = player.shootArrow();
if (_owner.isMember(player))
{
@ -230,10 +301,10 @@ public class Outpost implements Listener
if (item.isSimilar(SIEGE_DECLARATION_ARROW))
{
if (_against == null && !event.getProjectile().hasMetadata("OutpostData"))
if (_against == null && !arrow.hasMetadata("OutpostData"))
{
event.getProjectile().setMetadata("OutpostData", new FixedMetadataValue(_host.getPlugin(), _owner.getName() + ";" + player.getName()));
_declarationArrow = (Arrow) event.getProjectile();
arrow.setMetadata("OutpostData", new FixedMetadataValue(_host.getPlugin(), _owner.getName() + ";" + player.getName()));
_declarationArrow = (Arrow) arrow;
}
player.getInventory().setItem(slot, null);
@ -301,6 +372,15 @@ public class Outpost implements Listener
protected void update()
{
if (_declarationArrow != null)
{
_declarationArrow.setFireTicks(20);
RGBData color = _arrowColors.next();
UtilParticle.PlayParticle(ParticleType.MOB_SPELL, _declarationArrow.getLocation(), new Vector(color.getRed(), color.getGreen(), color.getBlue()), 1f, 0, ViewDist.MAX);
}
if (_against != null && getTimeToSiege() > 0)
{
_against.getOnlinePlayers().forEach(this::informTimeToSiege);
@ -321,15 +401,6 @@ public class Outpost implements Listener
return;
}
if (_declarationArrow != null)
{
_declarationArrow.setFireTicks(20);
RGBData color = _arrowColors.next();
UtilParticle.PlayParticle(ParticleType.MOB_SPELL, _declarationArrow.getLocation(), new Vector(color.getRed(), color.getGreen(), color.getBlue()), 1f, 0, ViewDist.MAX);
}
_preHologram2.setText(UtilText.getProgress(null, UtilMath.clamp(getLifetime(), 0., 60000.) / 60000., null, true));
RGBData color = UtilColor.RgbLightBlue;
@ -490,13 +561,6 @@ public class Outpost implements Listener
return list;
}
public void instakill()
{
_blocks.values().forEach(OutpostBlock::restore);
cleanup();
}
public void kill()
{
_state = OutpostState.DESTRUCTING;
@ -663,7 +727,7 @@ public class Outpost implements Listener
public long getTimeToSiege()
{
return System.currentTimeMillis() - getTimeSiegeDeclared();
return PREP_TIME - (System.currentTimeMillis() - getTimeSiegeDeclared());
}
public void setDeclarationArrow(Arrow arrow)
@ -680,4 +744,9 @@ public class Outpost implements Listener
{
_weapons.add(weapon);
}
public String getId()
{
return _id;
}
}

View File

@ -1,15 +1,18 @@
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.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
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;
@ -20,11 +23,9 @@ import org.bukkit.event.entity.EntityChangeBlockEvent;
import com.google.common.collect.Lists;
import mineplex.core.MiniPlugin;
import mineplex.core.common.events.ServerShutdownEvent;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilItem;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilPlayer;
@ -39,12 +40,14 @@ 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.core.repository.ClanTerritory;
import mineplex.game.clans.spawn.Spawn;
public class OutpostManager extends MiniPlugin
{
private ClansManager _clansManager;
private Map<String, Outpost> _outposts = new HashMap<>();
private Map<String, Outpost> _idToOutpost = new HashMap<>();
private List<String> _removalQueue;
@ -61,6 +64,182 @@ public class OutpostManager extends MiniPlugin
_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;
}
ArmorStand stand = (ArmorStand) entity;
String data = entity.getMetadata("$SERIALIZED_OUTPOST_DATA$").get(0).asString();
ByteArrayInputStream stream = new ByteArrayInputStream(data.getBytes("UTF-8"));
DataInputStream dis = new DataInputStream(stream);
String id;
Location origin;
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)
{
e.printStackTrace();
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlaceBlock(BlockPlaceEvent event)
{
@ -187,6 +366,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);
@ -202,15 +382,6 @@ public class OutpostManager extends MiniPlugin
}
}
@EventHandler
public void onServerShutdown(ServerShutdownEvent event)
{
for (Outpost outpost : _outposts.values())
{
outpost.instakill();
}
}
@EventHandler
public void onClaim(PlayerClaimTerritoryEvent event)
{

View File

@ -136,7 +136,7 @@ public class Cannon extends SiegeWeapon
armorStand.setVisible(false);
armorStand.setGravity(false);
armorStand.setPassenger(getEntity("Filler"));
armorStand.setPassenger(getEntity("Filler_1"));
addEntity(armorStand, "WEAPON");
}

View File

@ -1,7 +1,10 @@
package mineplex.game.clans.clans.siege.weapon;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
@ -49,6 +52,7 @@ import mineplex.game.clans.clans.siege.events.SiegeWeaponExplodeEvent;
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.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;
@ -123,8 +127,12 @@ public abstract class SiegeWeapon implements Listener
protected boolean _alive = true;
public SiegeWeapon(Location location, double maxHealth, String name, ClanInfo owner, ClansManager clansManager, SiegeManager siegeManager, Outpost outpost)
protected final int _weaponTypeIdentifier;
public SiegeWeapon(int typeId, Location location, double maxHealth, String name, ClanInfo owner, ClansManager clansManager, SiegeManager siegeManager, Outpost outpost)
{
_weaponTypeIdentifier = typeId;
_outpost = outpost;
_siegeManager = siegeManager;
_location = location;
@ -144,6 +152,39 @@ public abstract class SiegeWeapon implements Listener
_clans = clansManager;
}
public SiegeWeaponToken tokenize()
{
SiegeWeaponToken token = new SiegeWeaponToken();
Map<String, UUID> comprisedOf = new HashMap<>();
for (Entry<String, Entity> entry : _entityMapping.entrySet())
{
comprisedOf.put(entry.getKey(), entry.getValue().getUniqueId());
}
token.ComprisedOf = comprisedOf;
token.OwnerClan = _owner;
token.WeaponType = _weaponTypeIdentifier;
token.OutpostId = _outpost == null ? null : _outpost.getId();
token.Location = _location;
token.Health = _health;
token.Yaw = _yaw;
token.Rider = _rider;
token.LastFired = _lastFired;
return token;
}
public int getBaseDamage()
{
return _baseDamage;

View File

@ -0,0 +1,19 @@
package mineplex.game.clans.clans.siege.weapon.serialization;
import org.bukkit.Location;
import mineplex.game.clans.clans.siege.outpost.OutpostState;
import mineplex.game.clans.clans.siege.outpost.OutpostType;
public class OutpostToken
{
public String Id;
public Location Origin;
public OutpostType Type;
public String OwnerClan;
public double Health;
public long TimeSpawned;
public String againstClan;
public long siegeDeclaredTime;
public OutpostState outpostState;
}

View File

@ -0,0 +1,22 @@
package mineplex.game.clans.clans.siege.weapon.serialization;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import mineplex.game.clans.clans.ClanInfo;
public class SiegeWeaponToken
{
public ClanInfo OwnerClan;
public int WeaponType;
public String OutpostId;
public Location Location;
public Map<String, UUID> ComprisedOf;
public double Health;
public double Yaw;
public Player Rider;
public long LastFired;
}

View File

@ -0,0 +1,6 @@
package mineplex.game.clans.clans.siege.weapon.serialization;
public class TokenInfo
{
public Class<?> Type;
}

View File

@ -164,11 +164,6 @@ public class Gameplay extends MiniPlugin
if (!playerClass.IsGameClass(ClassType.Assassin, ClassType.Ranger))
{
if (event.getProjectile().hasMetadata("OutpostData"))
{
return;
}
notify(player, "You cannot use bows without the proper class!");
event.setCancelled(true);
}

View File

@ -1,6 +1,8 @@
package mineplex.game.clans.gameplay.safelog;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
@ -13,9 +15,6 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin;
import mineplex.core.common.file.DataFileInStream;
import mineplex.core.common.file.chunks.DataChunkLong;
import mineplex.core.common.file.chunks.DataChunkString;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
@ -77,10 +76,18 @@ public class SafeLog extends MiniPlugin
{
try
{
DataFileInStream stream = new DataFileInStream(_clansManager.UserDataDir + String.format("DEATH_%s.dat", player.getUniqueId().toString()));
DataInputStream stream = new DataInputStream(new FileInputStream(deathFile));
final long time = DataChunkLong.ReadFrom(stream).longValue();
final String killerName = DataChunkString.ReadFrom(stream);
final long time = stream.readLong();
final int length = stream.readInt();
final StringBuilder killerName = new StringBuilder();
for (int i = 0; i < length; i++)
{
killerName.append((char) stream.readByte());
}
stream.close();
UtilServer.getServer().getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable()
{
@ -92,8 +99,6 @@ public class SafeLog extends MiniPlugin
}
}, 15);
stream.dispose();
deathFile.delete();
}
catch (Exception e)

View File

@ -1,6 +1,8 @@
package mineplex.game.clans.gameplay.safelog.npc;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
@ -15,8 +17,6 @@ import org.bukkit.entity.Player;
import org.bukkit.entity.Skeleton;
import org.bukkit.metadata.FixedMetadataValue;
import mineplex.core.common.file.DataFileOutStream;
import mineplex.core.common.file.UtilFile;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilEnt;
@ -90,20 +90,20 @@ public class CombatLogNPC
{
killerName = ((CraftPlayer) killer).getName();
}
else if (killer != null)
else
{
killerName = UtilEnt.getName(killer);
}
System.out.println(killerName);
try
{
DataFileOutStream stream = new DataFileOutStream();
DataOutputStream stream = new DataOutputStream(new FileOutputStream(_userDataPath + String.format("DEATH_%s.dat", _playerInfo.getPlayerUuid())));
stream.writeToFile(_userDataPath + String.format("DEATH_%s.dat", _playerInfo.getPlayerUuid()));
stream.writeLong(System.currentTimeMillis());
stream.writeInt(killerName.length());
stream.writeBytes(killerName);
stream.release();
stream.close();
}
catch (IOException e)
{