save player informations
This commit is contained in:
parent
31ba32933e
commit
5e34588e3b
6 changed files with 190 additions and 11 deletions
|
@ -0,0 +1,42 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class InventoryDeserializer implements JsonDeserializer<Inventory> {
|
||||
|
||||
@Override
|
||||
public Inventory deserialize(JsonElement jsonElement, Type type,
|
||||
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
Inventory inv = Bukkit.createInventory(null, InventoryType.PLAYER);
|
||||
// parse from a string, to a list of string
|
||||
String[] contents = jsonElement.getAsString().split(";");
|
||||
// iterate through the list of strings and create ItemStacks with deserialize()
|
||||
Gson gson =
|
||||
new GsonBuilder()
|
||||
.registerTypeAdapter(ItemStack.class, new ItemStackDeserializer())
|
||||
.create();
|
||||
for (int i = 0; i < contents.length; i++) {
|
||||
if (contents[i].equals("null")) {
|
||||
inv.setItem(i, null);
|
||||
} else {
|
||||
ItemStack item = gson.fromJson(contents[i], ItemStack.class);
|
||||
if (item != null)
|
||||
inv.setItem(i, item);
|
||||
}
|
||||
}
|
||||
return inv;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
public class InventorySerializer implements JsonSerializer<Inventory> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Inventory itemStacks, Type type,
|
||||
JsonSerializationContext jsonSerializationContext) {
|
||||
List<String> collect = Arrays.stream(itemStacks.getContents())
|
||||
.map(item -> item == null ? "null" : Arrays.toString(item.serializeAsBytes())).collect(Collectors.toList());
|
||||
|
||||
String s = String.join(";", collect);
|
||||
return new JsonPrimitive(s);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class ItemStackDeserializer implements JsonDeserializer<ItemStack> {
|
||||
|
||||
@Override
|
||||
public ItemStack deserialize(JsonElement jsonElement, Type type,
|
||||
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
Gson gson = new Gson();
|
||||
byte[] mm = gson.fromJson(jsonElement, byte[].class);
|
||||
if (mm != null) {
|
||||
return ItemStack.deserializeBytes(mm);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import java.lang.reflect.Type;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
public class PotionEffectDeserializer implements JsonDeserializer<PotionEffect> {
|
||||
|
||||
@Override
|
||||
public PotionEffect deserialize(JsonElement jsonElement, Type type,
|
||||
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
try {
|
||||
String pot = jsonElement.getAsString();
|
||||
pot = pot.replace("{", "").replace("}", "");
|
||||
String[] parts = pot.split(",");
|
||||
// find the potion effect type from the string part[0] is a key
|
||||
PotionEffectType pet = Registry.POTION_EFFECT_TYPE.get(new NamespacedKey("minecraft",
|
||||
parts[0]));
|
||||
int duration = Integer.parseInt(parts[1]);
|
||||
int amplifier = Integer.parseInt(parts[2]);
|
||||
boolean particles = Boolean.parseBoolean(parts[3]);
|
||||
|
||||
return new PotionEffect(pet, duration, amplifier, false, particles, false);
|
||||
} catch(Exception e) {
|
||||
error("Error deserializing potion effect: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
|
||||
public class PotionEffectSerializer implements JsonSerializer<PotionEffect> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(PotionEffect potionEffect, Type type,
|
||||
JsonSerializationContext jsonSerializationContext) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{");
|
||||
sb.append(potionEffect.getType().key());
|
||||
sb.append(",");
|
||||
sb.append(potionEffect.getDuration());
|
||||
sb.append(",");
|
||||
sb.append(potionEffect.getAmplifier());
|
||||
sb.append(",").append(potionEffect.hasParticles());
|
||||
sb.append("}");
|
||||
return new JsonPrimitive(sb.toString());
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue