loads of progress
This commit is contained in:
parent
b308d1759f
commit
67b12f3ae2
@ -0,0 +1,40 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
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);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
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() };
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
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 };
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -7,7 +7,7 @@ import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FileUtil
|
||||
public class FileUtil
|
||||
{
|
||||
public static void DeleteFolder(File folder)
|
||||
{
|
||||
|
@ -0,0 +1,116 @@
|
||||
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 };
|
||||
// }
|
||||
|
||||
}
|
@ -1,310 +0,0 @@
|
||||
package mineplex.core.common.util;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
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 writeDataFile(File file, DataFileChunk... chunks) throws IOException
|
||||
{
|
||||
DataOutputStream stream = new DataOutputStream(new FileOutputStream(file));
|
||||
for (DataFileChunk chunk : chunks)
|
||||
{
|
||||
chunk.writeTo(stream);
|
||||
}
|
||||
stream.close();
|
||||
}
|
||||
|
||||
public static DataFileReader beginReading(String file) throws FileNotFoundException
|
||||
{
|
||||
return beginReading(new File(file));
|
||||
}
|
||||
|
||||
public static DataFileReader beginReading(File file) throws FileNotFoundException
|
||||
{
|
||||
return new DataFileReader(file);
|
||||
}
|
||||
|
||||
public static void writeDataFile(String file, DataFileChunk... chunks) throws IOException
|
||||
{
|
||||
writeDataFile(new File(file), chunks);
|
||||
}
|
||||
|
||||
public static class DataFileChunk
|
||||
{
|
||||
private ChunkType _type;
|
||||
private Object _value;
|
||||
|
||||
public DataFileChunk(ChunkType type, Object value)
|
||||
{
|
||||
if (type == null)
|
||||
{
|
||||
throw new RuntimeException("ChunkType can NOT be null.");
|
||||
}
|
||||
|
||||
_type = type;
|
||||
|
||||
if (!_type.isValid(value))
|
||||
{
|
||||
throw new RuntimeException("Invalid value provided for the specified ChunkType.");
|
||||
}
|
||||
|
||||
_value = value;
|
||||
}
|
||||
|
||||
public void writeTo(DataOutputStream stream) throws IOException
|
||||
{
|
||||
_type.writeTo(stream, _value);
|
||||
}
|
||||
}
|
||||
|
||||
public static enum ChunkType
|
||||
{
|
||||
STRING(new ChunkImpl()
|
||||
{
|
||||
public void writeTo(DataOutputStream stream, Object value) throws IOException
|
||||
{
|
||||
String str = (String) value;
|
||||
|
||||
INTEGER.writeTo(stream, str.length());
|
||||
for (char b : str.toCharArray())
|
||||
{
|
||||
CHAR.writeTo(stream, b);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isValid(Object value)
|
||||
{
|
||||
return value.getClass().equals(String.class);
|
||||
}
|
||||
|
||||
public Object readFrom(DataInputStream stream) throws IOException
|
||||
{
|
||||
int length = (int) INTEGER.readFrom(stream);
|
||||
|
||||
StringBuilder string = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
string.append(CHAR.readFrom(stream));
|
||||
}
|
||||
|
||||
return string.toString();
|
||||
}
|
||||
}),
|
||||
DOUBLE(new ChunkImpl()
|
||||
{
|
||||
public void writeTo(DataOutputStream stream, Object value) throws IOException
|
||||
{
|
||||
double number = (double) value;
|
||||
|
||||
stream.writeDouble(number);
|
||||
}
|
||||
|
||||
public boolean isValid(Object value)
|
||||
{
|
||||
return value.getClass().equals(Double.class);
|
||||
}
|
||||
|
||||
public Object readFrom(DataInputStream stream) throws IOException
|
||||
{
|
||||
return stream.readDouble();
|
||||
}
|
||||
}),
|
||||
INTEGER(new ChunkImpl()
|
||||
{
|
||||
public void writeTo(DataOutputStream stream, Object value) throws IOException
|
||||
{
|
||||
int number = (int) value;
|
||||
|
||||
stream.writeInt(number);
|
||||
}
|
||||
|
||||
public boolean isValid(Object value)
|
||||
{
|
||||
return value.getClass().equals(Integer.class);
|
||||
}
|
||||
|
||||
public Object readFrom(DataInputStream stream) throws IOException
|
||||
{
|
||||
return stream.readInt();
|
||||
}
|
||||
}),
|
||||
BYTE(new ChunkImpl()
|
||||
{
|
||||
public void writeTo(DataOutputStream stream, Object value) throws IOException
|
||||
{
|
||||
byte number = (byte) value;
|
||||
|
||||
stream.writeByte(number);
|
||||
}
|
||||
|
||||
public boolean isValid(Object value)
|
||||
{
|
||||
return value.getClass().equals(Byte.class);
|
||||
}
|
||||
|
||||
public Object readFrom(DataInputStream stream) throws IOException
|
||||
{
|
||||
return stream.readByte();
|
||||
}
|
||||
}),
|
||||
CHAR(new ChunkImpl()
|
||||
{
|
||||
public void writeTo(DataOutputStream stream, Object value) throws IOException
|
||||
{
|
||||
char number = (char) value;
|
||||
|
||||
stream.writeChar(number);
|
||||
}
|
||||
|
||||
public boolean isValid(Object value)
|
||||
{
|
||||
return value.getClass().equals(Character.class);
|
||||
}
|
||||
|
||||
public Object readFrom(DataInputStream stream) throws IOException
|
||||
{
|
||||
return stream.readChar();
|
||||
}
|
||||
}),
|
||||
LONG(new ChunkImpl()
|
||||
{
|
||||
public void writeTo(DataOutputStream stream, Object value) throws IOException
|
||||
{
|
||||
long number = (long) value;
|
||||
|
||||
stream.writeLong(number);
|
||||
}
|
||||
|
||||
public boolean isValid(Object value)
|
||||
{
|
||||
return value.getClass().equals(Long.class);
|
||||
}
|
||||
|
||||
public Object readFrom(DataInputStream stream) throws IOException
|
||||
{
|
||||
return stream.readLong();
|
||||
}
|
||||
});
|
||||
|
||||
private ChunkImpl _impl;
|
||||
|
||||
ChunkType(ChunkImpl impl)
|
||||
{
|
||||
_impl = impl;
|
||||
}
|
||||
|
||||
protected void writeTo(DataOutputStream stream, Object value) throws IOException
|
||||
{
|
||||
_impl.writeTo(stream, value);
|
||||
}
|
||||
|
||||
protected boolean isValid(Object value)
|
||||
{
|
||||
return value != null && _impl.isValid(value);
|
||||
}
|
||||
|
||||
public Object readFrom(DataInputStream stream) throws IOException
|
||||
{
|
||||
return _impl.readFrom(stream);
|
||||
}
|
||||
}
|
||||
|
||||
public static class DataFileReader
|
||||
{
|
||||
private DataInputStream _stream;
|
||||
|
||||
public DataFileReader(File file) throws FileNotFoundException
|
||||
{
|
||||
_stream = new DataInputStream(new FileInputStream(file));
|
||||
}
|
||||
|
||||
public Object readChunk(ChunkType type) throws IOException
|
||||
{
|
||||
return type.readFrom(_stream);
|
||||
}
|
||||
|
||||
public void close() throws IOException
|
||||
{
|
||||
_stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected interface ChunkImpl
|
||||
{
|
||||
void writeTo(DataOutputStream stream, Object value) throws IOException;
|
||||
|
||||
Object readFrom(DataInputStream stream) throws IOException;
|
||||
|
||||
boolean isValid(Object value);
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,7 @@ import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
@ -59,14 +60,13 @@ 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.weapon.Cannon;
|
||||
import mineplex.game.clans.clans.siege.weapon.SiegeWeapon;
|
||||
import mineplex.game.clans.core.repository.ClanTerritory;
|
||||
import net.minecraft.server.v1_8_R3.AxisAlignedBB;
|
||||
|
||||
public class Outpost implements Listener
|
||||
{
|
||||
protected static final long MAX_LIFETIME = 5 * 60 * 1000; // 30 minutes
|
||||
protected static final long MAX_LIFETIME = 30 * 60 * 1000; // 30 minutes
|
||||
public static final ItemStack OUTPOST_ITEM = new ItemBuilder(Material.NETHERRACK, 1).setRawTitle(C.cBlue + "Outpost").setLore(C.cWhite + "It appears to be a contraption of some sort!").build();
|
||||
public static final ItemStack SIEGE_DECLARATION_ARROW = new ItemBuilder(Material.ARROW, 1).setRawTitle(C.cAquaB + "Siege Declaration Arrow").setLore(UtilText.splitLineToArray(C.cWhite + "Fire a bow with this arrow at a rival Clan's base to declare Siege on them. Note that you are required to do this before being able to place Siege Weapons in your Outpost.", LineFormat.LORE)).build();
|
||||
public static final byte OUTPOST_BLOCK_DATA = (byte) 137;
|
||||
@ -75,15 +75,14 @@ public class Outpost implements Listener
|
||||
|
||||
private OutpostManager _host;
|
||||
|
||||
private UUID _uuid = UUID.randomUUID();
|
||||
|
||||
private ClanInfo _owner;
|
||||
|
||||
private Location _startCorner;
|
||||
private Location _origin;
|
||||
private Location _endCorner;
|
||||
|
||||
private Location _siegeAreaStart;
|
||||
private Location _siegeAreaEnd;
|
||||
|
||||
private Location _forceFieldStart;
|
||||
private Location _forceFieldEnd;
|
||||
|
||||
@ -103,10 +102,6 @@ public class Outpost implements Listener
|
||||
private LoopIterator<Vector> _circleStages;
|
||||
private LoopIterator<Vector> _reverseCircleStages;
|
||||
|
||||
private List<OutpostBuilder> _builders;
|
||||
|
||||
private List<OutpostBuilder> _removalQueue;
|
||||
|
||||
private double _siegeWeaponDistance;
|
||||
|
||||
private ColorFader _fader = new ColorFader(30, UtilColor.hexToRgb(0x00A296), UtilColor.hexToRgb(0x29E6B6));
|
||||
@ -138,16 +133,11 @@ public class Outpost implements Listener
|
||||
_startCorner = location.clone().subtract(type._size, 1.1, type._size);
|
||||
_endCorner = location.clone().add(type._size + .9, type._ySize - 1, type._size + .9);
|
||||
|
||||
_builders = new ArrayList<>();
|
||||
_removalQueue = new ArrayList<>();
|
||||
_weapons = new ArrayList<>();
|
||||
|
||||
_forceFieldStart = _startCorner.clone().subtract(3, 0, 3);
|
||||
_forceFieldEnd = _endCorner.clone().add(3, 0, 3);
|
||||
|
||||
_siegeAreaStart = _startCorner.clone().subtract(7, 0, 7);
|
||||
_siegeAreaEnd = _endCorner.clone().add(7, 0, 7);
|
||||
|
||||
_origin = location.clone();
|
||||
|
||||
_type = type;
|
||||
@ -243,7 +233,6 @@ public class Outpost implements Listener
|
||||
if (_against == null && !event.getProjectile().hasMetadata("OutpostData"))
|
||||
{
|
||||
event.getProjectile().setMetadata("OutpostData", new FixedMetadataValue(_host.getPlugin(), _owner.getName() + ";" + player.getName()));
|
||||
event.getProjectile().setMetadata("BypassFiring", new FixedMetadataValue(_host.getPlugin(), "x"));
|
||||
_declarationArrow = (Arrow) event.getProjectile();
|
||||
}
|
||||
|
||||
@ -433,19 +422,6 @@ public class Outpost implements Listener
|
||||
@EventHandler
|
||||
public void forcefield(UpdateEvent event)
|
||||
{
|
||||
if (!_removalQueue.isEmpty())
|
||||
{
|
||||
_builders.remove(_removalQueue.remove(0));
|
||||
}
|
||||
|
||||
if (event.getType() == UpdateType.TICK && getState() == OutpostState.CONSTRUCTING)
|
||||
{
|
||||
for (OutpostBuilder builder : _builders)
|
||||
{
|
||||
builder.tick();
|
||||
}
|
||||
}
|
||||
|
||||
if (event.getType() != UpdateType.FAST || getState() != OutpostState.CONSTRUCTING)
|
||||
{
|
||||
return;
|
||||
@ -473,12 +449,6 @@ public class Outpost implements Listener
|
||||
_state = OutpostState.CONSTRUCTING;
|
||||
_blocks = new LinkedHashMap<>(_buildQueue = _type.createBuildQueue(_origin, _owner.Clans));
|
||||
|
||||
{
|
||||
Location pos = UtilBlock.getHighest(_startCorner.getWorld(), _startCorner.clone().subtract(1, 0, 1).getBlockX(), _startCorner.clone().subtract(1, 0, 1).getBlockZ()).getLocation();
|
||||
|
||||
_builders.add(new OutpostBuilder(pos, "Peasant", _type.getWallLocations(_origin), this));
|
||||
}
|
||||
|
||||
_owner.inform("Siege", "Your Outpost is now being constructed.", null);
|
||||
|
||||
_circleStages = new LoopIterator<Vector>(circleAround(new Vector(0., 0., 0.), 40, .6d));
|
||||
@ -605,11 +575,6 @@ public class Outpost implements Listener
|
||||
{
|
||||
return _state;
|
||||
}
|
||||
|
||||
public void queueForRemoval(OutpostBuilder builder)
|
||||
{
|
||||
_removalQueue.add(builder);
|
||||
}
|
||||
|
||||
// AREA IN WHICH CANNONS AND OTHER SIEGE WEAPONS CAN BE PLACED
|
||||
public boolean isInSiegeArea(Location location)
|
||||
@ -670,6 +635,7 @@ public class Outpost implements Listener
|
||||
if (_host.isInSiege(against))
|
||||
{
|
||||
UtilPlayer.message(declarer, F.main("Clans", F.elem(against.getName()) + " is already in a Siege."));
|
||||
return;
|
||||
}
|
||||
|
||||
_against = against;
|
||||
|
@ -186,31 +186,6 @@ public class OutpostManager extends MiniPlugin
|
||||
}
|
||||
}
|
||||
|
||||
for (int x = -type._size; x < type._size; x++)
|
||||
{
|
||||
for (int y = -1; y < type._ySize; y++)
|
||||
{
|
||||
for (int z = -type._size; z < type._size; z++)
|
||||
{
|
||||
if (UtilBlock.airFoliage(location.clone().add(x, -1, z).getBlock()))
|
||||
{
|
||||
Block block = location.clone().add(x, -1, z).getBlock();
|
||||
|
||||
while (block.getType() == Material.AIR)
|
||||
{
|
||||
if (block.getLocation().getY() <= 30)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
_clansManager.getBlockRestore().add(block, Material.DIRT.getId(), (byte) 0, Outpost.MAX_LIFETIME);
|
||||
block = block.getRelative(BlockFace.DOWN);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_outposts.put(clan.getName(), new Outpost(this, clan, location, type));
|
||||
|
||||
_plugin.getServer().getPluginManager().registerEvents(_outposts.get(clan.getName()), _plugin);
|
||||
|
@ -154,7 +154,7 @@ public class Gameplay extends MiniPlugin
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onBowShoot(EntityShootBowEvent event)
|
||||
{
|
||||
if (event.getEntity() instanceof Player)
|
||||
@ -164,7 +164,7 @@ public class Gameplay extends MiniPlugin
|
||||
|
||||
if (!playerClass.IsGameClass(ClassType.Assassin, ClassType.Ranger))
|
||||
{
|
||||
if (event.getProjectile().hasMetadata("BypassFiring"))
|
||||
if (event.getProjectile().hasMetadata("OutpostData"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
@ -13,11 +13,11 @@ 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.UtilFile;
|
||||
import mineplex.core.common.util.UtilFile.ChunkType;
|
||||
import mineplex.core.common.util.UtilFile.DataFileReader;
|
||||
import mineplex.core.common.util.UtilPlayer;
|
||||
import mineplex.core.common.util.UtilServer;
|
||||
import mineplex.core.common.util.UtilTextMiddle;
|
||||
@ -77,10 +77,10 @@ public class SafeLog extends MiniPlugin
|
||||
{
|
||||
try
|
||||
{
|
||||
DataFileReader reader = UtilFile.beginReading(_clansManager.UserDataDir + String.format("DEATH_%s.dat", player.getUniqueId().toString()));
|
||||
DataFileInStream stream = new DataFileInStream(_clansManager.UserDataDir + String.format("DEATH_%s.dat", player.getUniqueId().toString()));
|
||||
|
||||
final long time = (long) reader.readChunk(ChunkType.LONG);
|
||||
final String killerName = (String) reader.readChunk(ChunkType.STRING);
|
||||
final long time = DataChunkLong.ReadFrom(stream).longValue();
|
||||
final String killerName = DataChunkString.ReadFrom(stream);
|
||||
|
||||
UtilServer.getServer().getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable()
|
||||
{
|
||||
@ -91,7 +91,8 @@ public class SafeLog extends MiniPlugin
|
||||
UtilTextMiddle.display("Offline Death", "Log out in a safer place next time!", 15, 80, 40, player);
|
||||
}
|
||||
}, 15);
|
||||
reader.close();
|
||||
|
||||
stream.dispose();
|
||||
|
||||
deathFile.delete();
|
||||
}
|
||||
|
@ -15,12 +15,11 @@ 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;
|
||||
import mineplex.core.common.util.UtilFile;
|
||||
import mineplex.core.common.util.UtilFile.ChunkType;
|
||||
import mineplex.core.common.util.UtilFile.DataFileChunk;
|
||||
import mineplex.core.common.util.UtilPlayer;
|
||||
import mineplex.core.common.util.UtilServer;
|
||||
import mineplex.core.common.util.UtilTime;
|
||||
@ -28,7 +27,6 @@ import mineplex.core.disguise.DisguiseManager;
|
||||
import mineplex.core.disguise.disguises.DisguisePlayer;
|
||||
import mineplex.core.hologram.Hologram;
|
||||
import mineplex.core.hologram.HologramManager;
|
||||
import mineplex.game.clans.Clans;
|
||||
import mineplex.game.clans.clans.ClansManager;
|
||||
|
||||
public class CombatLogNPC
|
||||
@ -101,7 +99,11 @@ public class CombatLogNPC
|
||||
|
||||
try
|
||||
{
|
||||
UtilFile.writeDataFile(new File(_userDataPath + String.format("DEATH_%s.dat", _playerInfo.getPlayerUuid())), new DataFileChunk(ChunkType.LONG, System.currentTimeMillis()), new DataFileChunk(ChunkType.STRING, killerName));
|
||||
DataFileOutStream stream = new DataFileOutStream();
|
||||
|
||||
stream.writeToFile(_userDataPath + String.format("DEATH_%s.dat", _playerInfo.getPlayerUuid()));
|
||||
|
||||
stream.release();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user