add of treasure and code moving around
This commit is contained in:
parent
e250eca841
commit
cd84847c14
18 changed files with 19 additions and 30 deletions
|
@ -0,0 +1,43 @@
|
|||
package me.unurled.sacredrealms.sr.components.player;
|
||||
|
||||
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 me.unurled.sacredrealms.sr.components.item.ItemStackDeserializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class InventoryDeserializer implements JsonDeserializer<Inventory> {
|
||||
|
||||
@Override
|
||||
public Inventory deserialize(
|
||||
@NotNull 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,26 @@
|
|||
package me.unurled.sacredrealms.sr.components.player;
|
||||
|
||||
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.List;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class InventorySerializer implements JsonSerializer<Inventory> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(
|
||||
@NotNull Inventory itemStacks, Type type, JsonSerializationContext jsonSerializationContext) {
|
||||
List<String> collect =
|
||||
Arrays.stream(itemStacks.getContents())
|
||||
.map(item -> item == null ? "null" : Arrays.toString(item.serializeAsBytes()))
|
||||
.toList();
|
||||
|
||||
String s = String.join(";", collect);
|
||||
return new JsonPrimitive(s);
|
||||
}
|
||||
}
|
|
@ -11,10 +11,6 @@ import java.util.UUID;
|
|||
import me.unurled.sacredrealms.sr.SR;
|
||||
import me.unurled.sacredrealms.sr.data.DataHandler;
|
||||
import me.unurled.sacredrealms.sr.data.DataManager;
|
||||
import me.unurled.sacredrealms.sr.data.gson.InventoryDeserializer;
|
||||
import me.unurled.sacredrealms.sr.data.gson.InventorySerializer;
|
||||
import me.unurled.sacredrealms.sr.data.gson.PotionEffectDeserializer;
|
||||
import me.unurled.sacredrealms.sr.data.gson.PotionEffectSerializer;
|
||||
import me.unurled.sacredrealms.sr.events.player.PlayerLevelUpEvent;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
import me.unurled.sacredrealms.sr.utils.Items;
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package me.unurled.sacredrealms.sr.components.player;
|
||||
|
||||
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.components.player;
|
||||
|
||||
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) {
|
||||
String sb =
|
||||
"{"
|
||||
+ potionEffect.getType().key()
|
||||
+ ","
|
||||
+ potionEffect.getDuration()
|
||||
+ ","
|
||||
+ potionEffect.getAmplifier()
|
||||
+ ","
|
||||
+ potionEffect.hasParticles()
|
||||
+ "}";
|
||||
return new JsonPrimitive(sb);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue