a few changes.
This commit is contained in:
parent
3b7c87f2b6
commit
e8ec7fb852
@ -11,10 +11,11 @@ import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.massivecraft.mcore4.adapter.InventoryAdapter;
|
||||
import com.massivecraft.mcore4.adapter.ItemStackAdapter;
|
||||
import com.massivecraft.mcore4.adapter.MongoURIAdapter;
|
||||
import com.massivecraft.mcore4.cmd.Cmd;
|
||||
import com.massivecraft.mcore4.gson.InventoryTypeAdapter;
|
||||
import com.massivecraft.mcore4.gson.ItemStackAdapter;
|
||||
import com.massivecraft.mcore4.gson.MongoURIAdapter;
|
||||
import com.massivecraft.mcore4.lib.gson.Gson;
|
||||
import com.massivecraft.mcore4.lib.gson.GsonBuilder;
|
||||
import com.massivecraft.mcore4.lib.mongodb.MongoURI;
|
||||
import com.massivecraft.mcore4.persist.One;
|
||||
@ -30,6 +31,7 @@ public class MCore extends JavaPlugin
|
||||
// -------------------------------------------- //
|
||||
// PERSIST
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static Map<Object, Persist> persistInstances = new HashMap<Object, Persist>();
|
||||
public static Map<Object, Persist> getPersistInstances() { return persistInstances; }
|
||||
public static Persist getPersist(Object owner) { return persistInstances.get(owner); }
|
||||
@ -84,8 +86,7 @@ public class MCore extends JavaPlugin
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static Random random = new Random();
|
||||
|
||||
|
||||
public static Gson gson = getGsonBuilder().create();
|
||||
|
||||
@Override
|
||||
public void onDisable()
|
||||
@ -117,7 +118,7 @@ public class MCore extends JavaPlugin
|
||||
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
|
||||
.registerTypeAdapter(MongoURI.class, MongoURIAdapter.get())
|
||||
.registerTypeAdapter(ItemStack.class, new ItemStackAdapter())
|
||||
.registerTypeAdapter(Inventory.class, new InventoryTypeAdapter());
|
||||
.registerTypeAdapter(Inventory.class, new InventoryAdapter());
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.massivecraft.mcore4.gson;
|
||||
package com.massivecraft.mcore4.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
@ -14,8 +14,9 @@ import com.massivecraft.mcore4.lib.gson.JsonParseException;
|
||||
import com.massivecraft.mcore4.lib.gson.JsonPrimitive;
|
||||
import com.massivecraft.mcore4.lib.gson.JsonSerializationContext;
|
||||
import com.massivecraft.mcore4.lib.gson.JsonSerializer;
|
||||
import com.massivecraft.mcore4.lib.mongodb.BasicDBObject;
|
||||
|
||||
public class InventoryTypeAdapter implements JsonDeserializer<Inventory>, JsonSerializer<Inventory>
|
||||
public class InventoryAdapter implements JsonDeserializer<Inventory>, JsonSerializer<Inventory>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// FIELD NAME CONSTANTS
|
||||
@ -30,20 +31,20 @@ public class InventoryTypeAdapter implements JsonDeserializer<Inventory>, JsonSe
|
||||
@Override
|
||||
public JsonElement serialize(Inventory src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
return serialize(src);
|
||||
return toJson(src);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Inventory deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
return deserialize(json);
|
||||
return fromJson(json);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// STATIC LOGIC
|
||||
// JSON
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static JsonElement serialize(Inventory src)
|
||||
public static JsonElement toJson(Inventory src)
|
||||
{
|
||||
JsonObject jsonInventory = new JsonObject();
|
||||
ItemStack[] itemStacks = src.getContents();
|
||||
@ -52,7 +53,7 @@ public class InventoryTypeAdapter implements JsonDeserializer<Inventory>, JsonSe
|
||||
for (int i = 0; i < itemStacks.length; i++)
|
||||
{
|
||||
ItemStack itemStack = itemStacks[i];
|
||||
JsonObject jsonItemStack = ItemStackAdapter.serialize(itemStack);
|
||||
JsonObject jsonItemStack = ItemStackAdapter.toJson(itemStack);
|
||||
if (jsonItemStack == null) continue;
|
||||
jsonInventory.add(String.valueOf(i), jsonItemStack);
|
||||
}
|
||||
@ -60,7 +61,7 @@ public class InventoryTypeAdapter implements JsonDeserializer<Inventory>, JsonSe
|
||||
return jsonInventory;
|
||||
}
|
||||
|
||||
public static Inventory deserialize(JsonElement json)
|
||||
public static Inventory fromJson(JsonElement json)
|
||||
{
|
||||
if ( ! json.isJsonObject()) return null;
|
||||
JsonObject jsonInventory = json.getAsJsonObject();
|
||||
@ -75,7 +76,49 @@ public class InventoryTypeAdapter implements JsonDeserializer<Inventory>, JsonSe
|
||||
// Fetch the jsonItemStack or mark it as empty and continue
|
||||
String stackIdx = String.valueOf(i);
|
||||
JsonElement jsonItemStack = jsonInventory.get(stackIdx);
|
||||
ItemStack itemStack = ItemStackAdapter.deserialize(jsonItemStack);
|
||||
ItemStack itemStack = ItemStackAdapter.fromJson(jsonItemStack);
|
||||
itemStacks[i] = itemStack;
|
||||
}
|
||||
|
||||
Inventory ret = new CraftInventoryCustom(null, size, "items");
|
||||
ret.setContents(itemStacks);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// BSON
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static BasicDBObject toBson(Inventory src)
|
||||
{
|
||||
BasicDBObject bsonInventory = new BasicDBObject();
|
||||
ItemStack[] itemStacks = src.getContents();
|
||||
bsonInventory.put(SIZE, itemStacks.length);
|
||||
|
||||
for (int i = 0; i < itemStacks.length; i++)
|
||||
{
|
||||
ItemStack itemStack = itemStacks[i];
|
||||
BasicDBObject bsonItemStack = ItemStackAdapter.toBson(itemStack);
|
||||
if (bsonItemStack == null) continue;
|
||||
bsonInventory.put(String.valueOf(i), bsonItemStack);
|
||||
}
|
||||
|
||||
return bsonInventory;
|
||||
}
|
||||
|
||||
public static Inventory fromBson(BasicDBObject bsonInventory)
|
||||
{
|
||||
if ( ! bsonInventory.containsField(SIZE)) return null;
|
||||
int size = bsonInventory.getInt(SIZE);
|
||||
|
||||
ItemStack[] itemStacks = new ItemStack[size];
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
// Fetch the jsonItemStack or mark it as empty and continue
|
||||
String stackIdx = String.valueOf(i);
|
||||
BasicDBObject bsonItemStack = (BasicDBObject) bsonInventory.get(stackIdx);
|
||||
ItemStack itemStack = ItemStackAdapter.fromBson(bsonItemStack);
|
||||
itemStacks[i] = itemStack;
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.massivecraft.mcore4.gson;
|
||||
package com.massivecraft.mcore4.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map.Entry;
|
||||
@ -13,6 +13,7 @@ import com.massivecraft.mcore4.lib.gson.JsonObject;
|
||||
import com.massivecraft.mcore4.lib.gson.JsonParseException;
|
||||
import com.massivecraft.mcore4.lib.gson.JsonSerializationContext;
|
||||
import com.massivecraft.mcore4.lib.gson.JsonSerializer;
|
||||
import com.massivecraft.mcore4.lib.mongodb.BasicDBObject;
|
||||
|
||||
public class ItemStackAdapter implements JsonDeserializer<ItemStack>, JsonSerializer<ItemStack>
|
||||
{
|
||||
@ -32,20 +33,20 @@ public class ItemStackAdapter implements JsonDeserializer<ItemStack>, JsonSerial
|
||||
@Override
|
||||
public JsonElement serialize(ItemStack itemStack, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
return serialize(itemStack);
|
||||
return toJson(itemStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
return deserialize(json);
|
||||
return fromJson(json);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// STATIC LOGIC
|
||||
// JSON
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static JsonObject serialize(ItemStack itemStack)
|
||||
public static JsonObject toJson(ItemStack itemStack)
|
||||
{
|
||||
if (itemStack == null || itemStack.getTypeId() == 0 || itemStack.getAmount() == 0)
|
||||
{
|
||||
@ -76,7 +77,7 @@ public class ItemStackAdapter implements JsonDeserializer<ItemStack>, JsonSerial
|
||||
return jsonItemStack;
|
||||
}
|
||||
|
||||
public static ItemStack deserialize(JsonElement json)
|
||||
public static ItemStack fromJson(JsonElement json)
|
||||
{
|
||||
if (json == null || ! json.isJsonObject()) return null;
|
||||
|
||||
@ -119,4 +120,83 @@ public class ItemStackAdapter implements JsonDeserializer<ItemStack>, JsonSerial
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// BSON
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static BasicDBObject toBson(ItemStack itemStack)
|
||||
{
|
||||
if (itemStack == null || itemStack.getTypeId() == 0 || itemStack.getAmount() == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
BasicDBObject bsonItemStack = new BasicDBObject();
|
||||
|
||||
bsonItemStack.put(TYPE, itemStack.getTypeId());
|
||||
|
||||
if (itemStack.getAmount() != 1)
|
||||
{
|
||||
bsonItemStack.put(AMOUNT, itemStack.getAmount());
|
||||
}
|
||||
if (itemStack.getDurability() != 0) // Durability is a weird name since it is the amount of damage.
|
||||
{
|
||||
bsonItemStack.put(DAMAGE, itemStack.getDurability());
|
||||
}
|
||||
if (itemStack.getEnchantments().size() > 0)
|
||||
{
|
||||
BasicDBObject bsonEnchantments = new BasicDBObject();
|
||||
for (Entry<Enchantment, Integer> entry : itemStack.getEnchantments().entrySet())
|
||||
{
|
||||
bsonEnchantments.put(String.valueOf(entry.getKey().getId()), entry.getValue());
|
||||
}
|
||||
bsonItemStack.put(ENCHANTMENTS, bsonEnchantments);
|
||||
}
|
||||
|
||||
return bsonItemStack;
|
||||
}
|
||||
|
||||
public static ItemStack fromBson(BasicDBObject bsonItemStack)
|
||||
{
|
||||
if (bsonItemStack == null) return null;
|
||||
|
||||
// Populate values
|
||||
int type = 0;
|
||||
int amount = 1;
|
||||
short damage = 0;
|
||||
|
||||
if (bsonItemStack.containsField(TYPE))
|
||||
{
|
||||
type = bsonItemStack.getInt(TYPE);
|
||||
}
|
||||
|
||||
if (bsonItemStack.containsField(AMOUNT))
|
||||
{
|
||||
amount = bsonItemStack.getInt(AMOUNT);
|
||||
}
|
||||
|
||||
if (bsonItemStack.containsField(DAMAGE))
|
||||
{
|
||||
damage = (short) bsonItemStack.getInt(DAMAGE);
|
||||
}
|
||||
|
||||
// Create Non enchanted stack
|
||||
ItemStack stack = new ItemStack(type, amount, damage);
|
||||
|
||||
// Add enchantments if there are any
|
||||
if (bsonItemStack.containsField(ENCHANTMENTS))
|
||||
{
|
||||
BasicDBObject bsonEnchantments = (BasicDBObject) bsonItemStack.get(ENCHANTMENTS);
|
||||
|
||||
for (Entry<String, Object> enchantmentEntry: bsonEnchantments.entrySet())
|
||||
{
|
||||
int enchantmentId = Integer.valueOf(enchantmentEntry.getKey());
|
||||
Integer enchantmentLevel = (Integer) enchantmentEntry.getValue();
|
||||
stack.addUnsafeEnchantment(Enchantment.getById(enchantmentId), enchantmentLevel);
|
||||
}
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.massivecraft.mcore4.gson;
|
||||
package com.massivecraft.mcore4.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
@ -1,76 +0,0 @@
|
||||
package com.massivecraft.mcore4.mongodb;
|
||||
|
||||
import org.bukkit.craftbukkit.inventory.CraftInventoryCustom;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.massivecraft.mcore4.lib.mongodb.BasicDBObject;
|
||||
|
||||
public class InventoryTypeAdapter
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// FIELD NAME CONSTANTS
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static final String SIZE = "size";
|
||||
|
||||
// -------------------------------------------- //
|
||||
// STATIC LOGIC
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static BasicDBObject serialize(Inventory src)
|
||||
{
|
||||
BasicDBObject bsonInventory = new BasicDBObject();
|
||||
ItemStack[] itemStacks = src.getContents();
|
||||
bsonInventory.put(SIZE, itemStacks.length);
|
||||
|
||||
for (int i = 0; i < itemStacks.length; i++)
|
||||
{
|
||||
ItemStack itemStack = itemStacks[i];
|
||||
BasicDBObject bsonItemStack = ItemStackAdapter.serialize(itemStack);
|
||||
if (bsonItemStack == null) continue;
|
||||
bsonInventory.put(String.valueOf(i), bsonItemStack);
|
||||
}
|
||||
|
||||
return bsonInventory;
|
||||
}
|
||||
|
||||
public static Inventory deserialize(BasicDBObject bsonInventory)
|
||||
{
|
||||
if ( ! bsonInventory.containsField(SIZE)) return null;
|
||||
int size = bsonInventory.getInt(SIZE);
|
||||
|
||||
ItemStack[] itemStacks = new ItemStack[size];
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
// Fetch the jsonItemStack or mark it as empty and continue
|
||||
String stackIdx = String.valueOf(i);
|
||||
BasicDBObject bsonItemStack = (BasicDBObject) bsonInventory.get(stackIdx);
|
||||
ItemStack itemStack = ItemStackAdapter.deserialize(bsonItemStack);
|
||||
itemStacks[i] = itemStack;
|
||||
}
|
||||
|
||||
Inventory ret = new CraftInventoryCustom(null, size, "items");
|
||||
ret.setContents(itemStacks);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// UTIL
|
||||
// -------------------------------------------- //
|
||||
|
||||
// This utility is nice to have in many cases :)
|
||||
public static boolean isInventoryEmpty(Inventory inv)
|
||||
{
|
||||
if (inv == null) return true;
|
||||
for (ItemStack stack : inv.getContents())
|
||||
{
|
||||
if (stack == null) continue;
|
||||
if (stack.getAmount() == 0) continue;
|
||||
if (stack.getTypeId() == 0) continue;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,99 +0,0 @@
|
||||
package com.massivecraft.mcore4.mongodb;
|
||||
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.massivecraft.mcore4.lib.mongodb.BasicDBObject;
|
||||
|
||||
public class ItemStackAdapter
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// FIELD NAME CONSTANTS
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static final String TYPE = "type";
|
||||
public static final String AMOUNT = "amount";
|
||||
public static final String DAMAGE = "damage";
|
||||
public static final String ENCHANTMENTS = "enchantments";
|
||||
|
||||
// -------------------------------------------- //
|
||||
// STATIC LOGIC
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static BasicDBObject serialize(ItemStack itemStack)
|
||||
{
|
||||
if (itemStack == null || itemStack.getTypeId() == 0 || itemStack.getAmount() == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
BasicDBObject bsonItemStack = new BasicDBObject();
|
||||
|
||||
bsonItemStack.put(TYPE, itemStack.getTypeId());
|
||||
|
||||
if (itemStack.getAmount() != 1)
|
||||
{
|
||||
bsonItemStack.put(AMOUNT, itemStack.getAmount());
|
||||
}
|
||||
if (itemStack.getDurability() != 0) // Durability is a weird name since it is the amount of damage.
|
||||
{
|
||||
bsonItemStack.put(DAMAGE, itemStack.getDurability());
|
||||
}
|
||||
if (itemStack.getEnchantments().size() > 0)
|
||||
{
|
||||
BasicDBObject bsonEnchantments = new BasicDBObject();
|
||||
for (Entry<Enchantment, Integer> entry : itemStack.getEnchantments().entrySet())
|
||||
{
|
||||
bsonEnchantments.put(String.valueOf(entry.getKey().getId()), entry.getValue());
|
||||
}
|
||||
bsonItemStack.put(ENCHANTMENTS, bsonEnchantments);
|
||||
}
|
||||
|
||||
return bsonItemStack;
|
||||
}
|
||||
|
||||
public static ItemStack deserialize(BasicDBObject bsonItemStack)
|
||||
{
|
||||
if (bsonItemStack == null) return null;
|
||||
|
||||
// Populate values
|
||||
int type = 0;
|
||||
int amount = 1;
|
||||
short damage = 0;
|
||||
|
||||
if (bsonItemStack.containsField(TYPE))
|
||||
{
|
||||
type = bsonItemStack.getInt(TYPE);
|
||||
}
|
||||
|
||||
if (bsonItemStack.containsField(AMOUNT))
|
||||
{
|
||||
amount = bsonItemStack.getInt(AMOUNT);
|
||||
}
|
||||
|
||||
if (bsonItemStack.containsField(DAMAGE))
|
||||
{
|
||||
damage = (short) bsonItemStack.getInt(DAMAGE);
|
||||
}
|
||||
|
||||
// Create Non enchanted stack
|
||||
ItemStack stack = new ItemStack(type, amount, damage);
|
||||
|
||||
// Add enchantments if there are any
|
||||
if (bsonItemStack.containsField(ENCHANTMENTS))
|
||||
{
|
||||
BasicDBObject bsonEnchantments = (BasicDBObject) bsonItemStack.get(ENCHANTMENTS);
|
||||
|
||||
for (Entry<String, Object> enchantmentEntry: bsonEnchantments.entrySet())
|
||||
{
|
||||
int enchantmentId = Integer.valueOf(enchantmentEntry.getKey());
|
||||
Integer enchantmentLevel = (Integer) enchantmentEntry.getValue();
|
||||
stack.addUnsafeEnchantment(Enchantment.getById(enchantmentId), enchantmentLevel);
|
||||
}
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
}
|
@ -1,13 +1,5 @@
|
||||
package com.massivecraft.mcore4.persist;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@ -81,53 +73,6 @@ public class Persist
|
||||
// UTILS
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static void write(File file, String content) throws IOException
|
||||
{
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8"));
|
||||
out.write(content);
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static String read(File file) throws IOException
|
||||
{
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
|
||||
String ret = new String(new byte[0], "UTF-8");
|
||||
|
||||
String line;
|
||||
while ((line = in.readLine()) != null)
|
||||
{
|
||||
ret += line;
|
||||
}
|
||||
|
||||
in.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static boolean writeCatch(File file, String content)
|
||||
{
|
||||
try
|
||||
{
|
||||
write(file, content);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String readCatch(File file)
|
||||
{
|
||||
try
|
||||
{
|
||||
return read(file);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> ArrayList<T> uglySQL(Collection<T> items, Predictate<T> where, Comparator<T> orderby, Integer limit, Integer offset)
|
||||
{
|
||||
ArrayList<T> ret = new ArrayList<T>(items.size());
|
||||
|
@ -15,6 +15,7 @@ import com.massivecraft.mcore4.Predictate;
|
||||
import com.massivecraft.mcore4.lib.gson.Gson;
|
||||
import com.massivecraft.mcore4.persist.IClassManager;
|
||||
import com.massivecraft.mcore4.persist.Persist;
|
||||
import com.massivecraft.mcore4.util.DiscUtil;
|
||||
|
||||
public abstract class GsonClassManager<T> implements IClassManager<T>
|
||||
{
|
||||
@ -259,7 +260,7 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
|
||||
{
|
||||
String json = this.getGson().toJson(entity);
|
||||
File file = this.fileFromId(id);
|
||||
return Persist.writeCatch(file, json);
|
||||
return DiscUtil.writeCatch(file, json);
|
||||
}
|
||||
this.removeFile(id);
|
||||
// TODO: Remove if loaded??
|
||||
@ -307,7 +308,7 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
|
||||
if (entity != null) return entity;
|
||||
if ( ! this.containsId(id)) return null;
|
||||
File file = this.fileFromId(id);
|
||||
String json = Persist.readCatch(file);
|
||||
String json = DiscUtil.readCatch(file);
|
||||
if (json == null) return null;
|
||||
entity = this.getGson().fromJson(json, this.getManagedClass());
|
||||
this.attach(entity, id, true);
|
||||
|
@ -4,33 +4,48 @@ import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class DiscUtil
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// CONSTANTS
|
||||
// -------------------------------------------- //
|
||||
|
||||
private final static String UTF8 = "UTF-8";
|
||||
|
||||
// -------------------------------------------- //
|
||||
// BYTE
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static byte[] readBytes(File file) throws IOException
|
||||
{
|
||||
return Files.readAllBytes(file.toPath());
|
||||
}
|
||||
|
||||
public static void writeBytes(File file, byte[] bytes) throws IOException
|
||||
{
|
||||
Files.write(file.toPath(), bytes);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// STRING
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static void write(File file, String content) throws IOException
|
||||
{
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8"));
|
||||
out.write(content);
|
||||
out.close();
|
||||
writeBytes(file, utf8(content));
|
||||
}
|
||||
|
||||
public static String read(File file) throws IOException
|
||||
{
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
|
||||
String ret = new String(new byte[0], "UTF-8");
|
||||
|
||||
String line;
|
||||
while ((line = in.readLine()) != null)
|
||||
{
|
||||
ret += line+"\n";
|
||||
}
|
||||
|
||||
in.close();
|
||||
|
||||
if (ret.length() == 0) return ret;
|
||||
return ret.substring(0, ret.length()-1);
|
||||
return utf8(readBytes(file));
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// CATCH
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static boolean writeCatch(File file, String content)
|
||||
{
|
||||
try
|
||||
@ -56,6 +71,10 @@ public class DiscUtil
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// DOWNLOAD
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static boolean downloadUrl(String urlstring, File file)
|
||||
{
|
||||
try
|
||||
@ -78,6 +97,10 @@ public class DiscUtil
|
||||
return downloadUrl(urlstring, new File(filename));
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// FILE DELETION
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static boolean deleteRecursive(File path) throws FileNotFoundException
|
||||
{
|
||||
if ( ! path.exists()) throw new FileNotFoundException(path.getAbsolutePath());
|
||||
@ -92,4 +115,34 @@ public class DiscUtil
|
||||
return ret && path.delete();
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// UTF8 ENCODE AND DECODE
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static byte[] utf8(String string)
|
||||
{
|
||||
try
|
||||
{
|
||||
return string.getBytes(UTF8);
|
||||
}
|
||||
catch (UnsupportedEncodingException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String utf8(byte[] bytes)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new String(bytes, UTF8);
|
||||
}
|
||||
catch (UnsupportedEncodingException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ public class PlayerUtil
|
||||
eplayer.netServerHandler.sendPacket(new Packet8UpdateHealth(eplayer.getHealth(), eplayer.getFoodData().a(), eplayer.getFoodData().e()));
|
||||
}
|
||||
|
||||
// TODO: Is there synchronization/parallelism risks here?
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Player getPlayerExact(String exactPlayerName)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user