Initial commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package net.crankedpvp.storageutils;
|
||||
|
||||
import com.google.gson.FieldNamingPolicy;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import lombok.Getter;
|
||||
import net.crankedpvp.storageutils.utils.*;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
public class StorageUtils extends JavaPlugin {
|
||||
|
||||
@Getter
|
||||
private static StorageUtils instance;
|
||||
private final Type listType = new TypeToken<List<ItemStack>>(){}.getType();
|
||||
|
||||
private final Gson gson = new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.serializeNulls()
|
||||
.registerTypeAdapter(ItemStack.class, new ItemStackSerializer()).setLenient()
|
||||
.registerTypeAdapter(ItemStack.class, new ItemStackDeserializer()).setLenient()
|
||||
.registerTypeAdapter(ItemStack[].class, new ItemStackArraySerializer()).setLenient()
|
||||
.registerTypeAdapter(ItemStack[].class, new ItemStackArrayDeserializer()).setLenient()
|
||||
.registerTypeAdapter(Location.class, new LocationSerializer()).setLenient()
|
||||
.registerTypeAdapter(Location.class, new LocationDeserializer()).setLenient()
|
||||
.registerTypeAdapter(listType, new ItemStackArrayListSerializer()).setLenient()
|
||||
.registerTypeAdapter(listType, new ItemStackArrayListDeserializer()).setLenient()
|
||||
.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY)
|
||||
.create();
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
instance = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package net.crankedpvp.storageutils.struct;
|
||||
|
||||
public interface NewKeyToSave<T> {
|
||||
|
||||
T getKey();
|
||||
|
||||
void setKey(T key);
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package net.crankedpvp.storageutils.struct;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class NewSaveLoadManager<K, V extends NewKeyToSave<K>> {
|
||||
|
||||
private final File dataDirectory;
|
||||
private final JavaPlugin plugin;
|
||||
private final Gson gson;
|
||||
private final Class<V> clazz;
|
||||
@Getter
|
||||
private final Map<K, V> cache;
|
||||
|
||||
protected NewSaveLoadManager(JavaPlugin plugin, Gson gson, String dataDirectory, Class<V> clazz) {
|
||||
this.plugin = plugin;
|
||||
this.gson = gson;
|
||||
this.clazz = clazz;
|
||||
this.dataDirectory = new File(plugin.getDataFolder(), dataDirectory);
|
||||
this.cache = new HashMap<>();
|
||||
}
|
||||
|
||||
|
||||
public void loadAll() {
|
||||
if (!dataDirectory.exists()) {
|
||||
dataDirectory.mkdirs();
|
||||
}
|
||||
|
||||
File[] validKitFiles = dataDirectory.listFiles((dir,name) -> name.endsWith(".json"));
|
||||
|
||||
if (validKitFiles == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (File file : validKitFiles) {
|
||||
this.load(file);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveAll() {
|
||||
if (!dataDirectory.exists()) {
|
||||
dataDirectory.mkdirs();
|
||||
}
|
||||
|
||||
for (Map.Entry<K, V> entry : cache.entrySet()) {
|
||||
this.save(this.serialize(entry.getKey()), entry.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void purge() {
|
||||
if (!dataDirectory.exists()) {
|
||||
dataDirectory.mkdirs();
|
||||
}
|
||||
|
||||
for (File file : Objects.requireNonNull(dataDirectory.listFiles())) {
|
||||
K deserialize = this.deserialize(file.getName().substring(0,file.getName().length()-5));
|
||||
if (!this.containsKey(deserialize)) {
|
||||
file.deleteOnExit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void load(String dataDirectory, String key) {
|
||||
this.load(new File(this.plugin.getDataFolder() + dataDirectory, key + ".json"));
|
||||
}
|
||||
|
||||
public void load(File file) {
|
||||
String fileName = file.getName();
|
||||
int fileNameLength = fileName.length();
|
||||
String key = fileName.substring(0,fileNameLength-5);
|
||||
|
||||
try (FileReader reader = new FileReader(file)) {
|
||||
V valueFromJson = this.gson.fromJson(reader, clazz);
|
||||
if (valueFromJson == null) {
|
||||
return;
|
||||
}
|
||||
valueFromJson.setKey(this.deserialize(key));
|
||||
this.cache.put(this.deserialize(key), valueFromJson);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void save(String key, V v) {
|
||||
File file = new File(dataDirectory,key + ".json");
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
String jsonString = this.gson.toJson(v);
|
||||
try (FileWriter writer = new FileWriter(file)) {
|
||||
writer.write(jsonString);
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public V getFromCache(K key) {
|
||||
return this.cache.get(key);
|
||||
}
|
||||
|
||||
public void addToCache(V v) {
|
||||
this.cache.put(v.getKey(), v);
|
||||
}
|
||||
|
||||
public void removeFromCache(V v) {
|
||||
this.cache.remove(v.getKey());
|
||||
}
|
||||
|
||||
public void removeFromCache(K key) {
|
||||
this.cache.remove(key);
|
||||
}
|
||||
|
||||
public boolean containsKey(K key) {
|
||||
return this.cache.containsKey(key);
|
||||
}
|
||||
|
||||
public Set<K> getCacheKeys() {
|
||||
return this.cache.keySet();
|
||||
}
|
||||
|
||||
public Collection<V> getCacheValues() {
|
||||
return this.cache.values();
|
||||
}
|
||||
|
||||
public abstract String serialize(K key);
|
||||
|
||||
public abstract K deserialize(String key);
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package net.crankedpvp.storageutils.utils;
|
||||
|
||||
import com.google.gson.*;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ItemStackArrayDeserializer implements JsonDeserializer<ItemStack[]> {
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public ItemStack[] deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
JsonArray jsonArray = ((JsonArray) jsonElement);
|
||||
List<ItemStack> items = new ArrayList<>();
|
||||
|
||||
for (JsonElement element : jsonArray) {
|
||||
try {
|
||||
items.add(jsonDeserializationContext.deserialize(element.getAsJsonPrimitive(), ItemStack.class));
|
||||
} catch (IllegalStateException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return items.toArray(new ItemStack[] {});
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package net.crankedpvp.storageutils.utils;
|
||||
|
||||
import com.google.gson.*;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ItemStackArrayListDeserializer implements JsonDeserializer<List<ItemStack>> {
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public List<ItemStack> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
JsonArray jsonArray = ((JsonArray) jsonElement);
|
||||
List<ItemStack> items = new ArrayList<>();
|
||||
|
||||
for (JsonElement element : jsonArray) {
|
||||
try {
|
||||
items.add(jsonDeserializationContext.deserialize(element.getAsJsonPrimitive(), ItemStack.class));
|
||||
} catch (IllegalStateException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return items;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package net.crankedpvp.storageutils.utils;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
|
||||
public class ItemStackArrayListSerializer implements JsonSerializer<List<ItemStack>> {
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public JsonElement serialize(List<ItemStack> itemStacks, Type type, JsonSerializationContext jsonSerializationContext) {
|
||||
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
|
||||
for (ItemStack itemStack : itemStacks) {
|
||||
jsonArray.add(jsonSerializationContext.serialize(itemStack, ItemStack.class));
|
||||
}
|
||||
|
||||
return jsonArray;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package net.crankedpvp.storageutils.utils;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class ItemStackArraySerializer implements JsonSerializer<ItemStack[]> {
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public JsonElement serialize(ItemStack[] itemStacks, Type type, JsonSerializationContext jsonSerializationContext) {
|
||||
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
|
||||
for (ItemStack itemStack : itemStacks) {
|
||||
jsonArray.add(jsonSerializationContext.serialize(itemStack, ItemStack.class));
|
||||
}
|
||||
|
||||
return jsonArray;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package net.crankedpvp.storageutils.utils;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.io.BukkitObjectInputStream;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class ItemStackDeserializer implements JsonDeserializer<ItemStack> {
|
||||
|
||||
public static ItemStack deserialize(String base64) throws IOException, ClassNotFoundException {
|
||||
byte[] data = Base64Coder.decodeLines(base64);
|
||||
ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
|
||||
BukkitObjectInputStream in = new BukkitObjectInputStream(bytesIn);
|
||||
ItemStack itemStack = (ItemStack) in.readObject();
|
||||
in.close();
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public ItemStack deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
return deserialize(jsonElement.getAsJsonPrimitive().getAsString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package net.crankedpvp.storageutils.utils;
|
||||
|
||||
import com.google.gson.*;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.io.BukkitObjectOutputStream;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class ItemStackSerializer implements JsonSerializer<ItemStack> {
|
||||
|
||||
public static String serialize(ItemStack itemStack) throws IOException {
|
||||
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
|
||||
BukkitObjectOutputStream out = new BukkitObjectOutputStream(bytesOut);
|
||||
out.writeObject(itemStack);
|
||||
out.flush();
|
||||
out.close();
|
||||
return Base64Coder.encodeLines(bytesOut.toByteArray());
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public JsonElement serialize(ItemStack itemStack, Type type, JsonSerializationContext jsonSerializationContext) {
|
||||
String serialized = serialize(itemStack);
|
||||
JsonObject jsonElement = (JsonObject) JsonParser.parseString("{ \"item\": \"" + serialized + "\" }");
|
||||
return jsonElement.getAsJsonPrimitive("item");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package net.crankedpvp.storageutils.utils;
|
||||
|
||||
public interface KeyToSave {
|
||||
|
||||
String getKey();
|
||||
|
||||
void setKey(String key);
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package net.crankedpvp.storageutils.utils;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class LocationDeserializer implements JsonDeserializer<Location> {
|
||||
|
||||
public static Location deserialize(String s) {
|
||||
if (s == null || s.trim().equals("")) {
|
||||
return null;
|
||||
}
|
||||
final String[] parts = s.split(":");
|
||||
if (parts.length == 4) {
|
||||
final World w = Bukkit.getServer().getWorld(parts[0]);
|
||||
final int x = Integer.parseInt(parts[1]);
|
||||
final int y = Integer.parseInt(parts[2]);
|
||||
final int z = Integer.parseInt(parts[3]);
|
||||
return new Location(w, x, y, z);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public Location deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
return deserialize(jsonElement.getAsJsonPrimitive().getAsString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package net.crankedpvp.storageutils.utils;
|
||||
|
||||
import com.google.gson.*;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.io.BukkitObjectOutputStream;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class LocationSerializer implements JsonSerializer<Location> {
|
||||
|
||||
public static String serialize(Location obj) {
|
||||
if (obj == null) {
|
||||
return "";
|
||||
}
|
||||
return obj.getWorld().getName() + ":" + obj.getBlockX() + ":" + obj.getBlockY() + ":" + obj.getBlockZ();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public JsonElement serialize(Location location, Type type, JsonSerializationContext jsonSerializationContext) {
|
||||
String serialized = serialize(location);
|
||||
JsonObject jsonElement = (JsonObject) JsonParser.parseString("{ \"location\": \"" + serialized + "\" }");
|
||||
return jsonElement.getAsJsonPrimitive("location");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package net.crankedpvp.storageutils.utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class SaveLoadManager<T extends KeyToSave> {
|
||||
|
||||
private final File dataDirectory;
|
||||
private final Gson gson;
|
||||
private final Class<T> clazz;
|
||||
private final Map<String, T> cache;
|
||||
|
||||
protected SaveLoadManager(JavaPlugin plugin, Gson gson, String dataDirectory, Class<T> clazz) {
|
||||
this.gson = gson;
|
||||
this.clazz = clazz;
|
||||
this.dataDirectory = new File(plugin.getDataFolder(), dataDirectory);
|
||||
this.cache = new HashMap<>();
|
||||
}
|
||||
|
||||
|
||||
public void loadAll() {
|
||||
if (!dataDirectory.exists()) {
|
||||
dataDirectory.mkdirs();
|
||||
}
|
||||
|
||||
File[] validKitFiles = dataDirectory.listFiles((dir,name) -> name.endsWith(".json"));
|
||||
|
||||
if (validKitFiles == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (File file : validKitFiles) {
|
||||
this.load(file);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveAll() {
|
||||
if (!dataDirectory.exists()) {
|
||||
dataDirectory.mkdirs();
|
||||
}
|
||||
|
||||
for (Map.Entry<String, T> entry : cache.entrySet()) {
|
||||
this.save(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void load(File file) {
|
||||
String fileName = file.getName();
|
||||
int fileNameLength = fileName.length();
|
||||
String key = fileName.substring(0,fileNameLength-5);
|
||||
|
||||
try (FileReader reader = new FileReader(file)) {
|
||||
T t = this.gson.fromJson(reader, clazz);
|
||||
if (t == null) {
|
||||
return;
|
||||
}
|
||||
t.setKey(key);
|
||||
cache.put(key, t);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void save(String key, T t) {
|
||||
File file = new File(dataDirectory,key + ".json");
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
String jsonString = this.gson.toJson(t);
|
||||
try (FileWriter writer = new FileWriter(file)) {
|
||||
writer.write(jsonString);
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public T getFromCache(String key) {
|
||||
return this.cache.get(key);
|
||||
}
|
||||
|
||||
public void addToCache(T t) {
|
||||
this.cache.put(t.getKey(), t);
|
||||
}
|
||||
|
||||
public void removeFromCache(T t) {
|
||||
this.cache.remove(t.getKey());
|
||||
}
|
||||
|
||||
public void removeFromCache(String key) {
|
||||
this.cache.remove(key);
|
||||
}
|
||||
|
||||
public boolean containsKey(String key) {
|
||||
return this.cache.containsKey(key);
|
||||
}
|
||||
|
||||
public Set<String> getCacheKeys() {
|
||||
return this.cache.keySet();
|
||||
}
|
||||
|
||||
public Collection<T> getCacheValues() {
|
||||
return this.cache.values();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
name: StorageUtil
|
||||
main: net.crankedpvp.storageutils.StorageUtils
|
||||
version: ${git.total.commit.count}.0
|
||||
description: "§fCommit ID: §a${git.commit.id.abbrev}"
|
||||
website: https://crankedpvp.net/
|
||||
Reference in New Issue
Block a user