add of treasure and code moving around

This commit is contained in:
unurled 2024-04-08 17:03:37 +02:00
parent e250eca841
commit cd84847c14
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
18 changed files with 19 additions and 30 deletions

View file

@ -0,0 +1,64 @@
package me.unurled.sacredrealms.sr.components.clientbuild;
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.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
public class ClientBuildDeserializer implements JsonDeserializer<ClientBuild> {
/**
* Gson invokes this call-back method during deserialization when it encounters a field of the
* specified type.
*
* <p>In the implementation of this call-back method, you should consider invoking {@link
* JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any
* non-trivial field of the returned object. However, you should never invoke it on the same type
* passing {@code json} since that will cause an infinite loop (Gson will call your call-back
* method again).
*
* @param json The Json data being deserialized
* @param typeOfT The type of the Object to deserialize to
* @param context
* @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
* @throws JsonParseException if json is not in the expected format of {@code typeofT}
*/
@Override
public ClientBuild deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
String[] data = json.getAsString().split(";");
ClientBuild build = new ClientBuild(data[0]);
for (String block : data[1].split(",")) {
String[] spl = block.replace("{", "").replace("}", "").split("\\|");
World world = Bukkit.getWorld(spl[0]);
if (world == null) {
error("World " + spl[0] + " does not exist");
return build;
}
Location loc;
BlockData bData;
try {
loc =
new Location(
world,
Integer.parseInt(spl[1]),
Integer.parseInt(spl[2]),
Integer.parseInt(spl[3]));
bData = Bukkit.createBlockData(spl[4]);
} catch (NumberFormatException e) {
error("Invalid block data: " + block);
continue;
}
build.addBlock(loc, bData);
}
return build;
}
}

View file

@ -15,8 +15,6 @@ import me.unurled.sacredrealms.sr.SR;
import me.unurled.sacredrealms.sr.commands.admin.ClientBuildCommand;
import me.unurled.sacredrealms.sr.data.DataHandler;
import me.unurled.sacredrealms.sr.data.DataManager;
import me.unurled.sacredrealms.sr.data.gson.ClientBuildDeserializer;
import me.unurled.sacredrealms.sr.data.gson.ClientBuildSerializer;
import me.unurled.sacredrealms.sr.managers.Manager;
import org.bukkit.Bukkit;
import org.bukkit.Location;

View file

@ -0,0 +1,49 @@
package me.unurled.sacredrealms.sr.components.clientbuild;
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.Map.Entry;
import org.bukkit.Location;
import org.bukkit.block.data.BlockData;
public class ClientBuildSerializer implements JsonSerializer<ClientBuild> {
/**
* Gson invokes this call-back method during serialization when it encounters a field of the
* specified type.
*
* <p>In the implementation of this call-back method, you should consider invoking {@link
* JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any
* non-trivial field of the {@code src} object. However, you should never invoke it on the {@code
* src} object itself since that will cause an infinite loop (Gson will call your call-back method
* again).
*
* @param src the object that needs to be converted to Json.
* @param typeOfSrc the actual type (fully genericized version) of the source object.
* @param context
* @return a JsonElement corresponding to the specified object.
*/
@Override
public JsonElement serialize(ClientBuild src, Type typeOfSrc, JsonSerializationContext context) {
StringBuilder s = new StringBuilder();
s.append(src.getName());
s.append(";");
for (Entry<Location, BlockData> entry : src.getBlocks().entrySet()) {
s.append("{");
s.append(entry.getKey().getWorld().getName());
s.append("|");
s.append(entry.getKey().getBlockX());
s.append("|");
s.append(entry.getKey().getBlockY());
s.append("|");
s.append(entry.getKey().getBlockZ());
s.append("|");
s.append(entry.getValue().getAsString());
s.append("},");
}
return new JsonPrimitive(s.toString());
}
}

View file

@ -8,11 +8,11 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import me.unurled.sacredrealms.sr.components.item.ItemStackDeserializer;
import me.unurled.sacredrealms.sr.components.item.ItemStackSerializer;
import me.unurled.sacredrealms.sr.components.player.PotionEffectDeserializer;
import me.unurled.sacredrealms.sr.data.DataHandler;
import me.unurled.sacredrealms.sr.data.DataManager;
import me.unurled.sacredrealms.sr.data.gson.ItemStackDeserializer;
import me.unurled.sacredrealms.sr.data.gson.ItemStackSerializer;
import me.unurled.sacredrealms.sr.data.gson.PotionEffectDeserializer;
import me.unurled.sacredrealms.sr.managers.Manager;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;

View file

@ -14,8 +14,8 @@ import java.util.List;
import java.util.Map;
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
import me.unurled.sacredrealms.sr.components.item.abilities.Ability;
import me.unurled.sacredrealms.sr.components.item.abilities.AbilityDeserializer;
import me.unurled.sacredrealms.sr.components.item.enchantments.Enchantment;
import me.unurled.sacredrealms.sr.data.gson.AbilityDeserializer;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

View file

@ -0,0 +1,34 @@
package me.unurled.sacredrealms.sr.components.item;
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;
public class ItemDeserializer implements JsonDeserializer<Item> {
/**
* Gson invokes this call-back method during deserialization when it encounters a field of the
* specified type.
*
* <p>In the implementation of this call-back method, you should consider invoking {@link
* JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any
* non-trivial field of the returned object. However, you should never invoke it on the same type
* passing {@code json} since that will cause an infinite loop (Gson will call your call-back
* method again).
*
* @param json The Json data being deserialized
* @param typeOfT The type of the Object to deserialize to
* @param context
* @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
* @throws JsonParseException if json is not in the expected format of {@code typeofT}
*/
@Override
public Item deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
Item item = new Item();
item.fromString(json.getAsString());
return item;
}
}

View file

@ -6,8 +6,6 @@ import java.util.HashMap;
import java.util.List;
import me.unurled.sacredrealms.sr.data.DataHandler;
import me.unurled.sacredrealms.sr.data.DataManager;
import me.unurled.sacredrealms.sr.data.gson.ItemDeserializer;
import me.unurled.sacredrealms.sr.data.gson.ItemStackSerializer;
import me.unurled.sacredrealms.sr.managers.Manager;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;

View file

@ -0,0 +1,30 @@
package me.unurled.sacredrealms.sr.components.item;
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;
public class ItemSerializer implements JsonSerializer<Item> {
/**
* Gson invokes this call-back method during serialization when it encounters a field of the
* specified type.
*
* <p>In the implementation of this call-back method, you should consider invoking {@link
* JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any
* non-trivial field of the {@code src} object. However, you should never invoke it on the {@code
* src} object itself since that will cause an infinite loop (Gson will call your call-back method
* again).
*
* @param src the object that needs to be converted to Json.
* @param typeOfSrc the actual type (fully genericized version) of the source object.
* @param context
* @return a JsonElement corresponding to the specified object.
*/
@Override
public JsonElement serialize(Item src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
}

View file

@ -0,0 +1,24 @@
package me.unurled.sacredrealms.sr.components.item;
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 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;
}
}

View file

@ -0,0 +1,32 @@
package me.unurled.sacredrealms.sr.components.item;
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 org.bukkit.inventory.ItemStack;
public class ItemStackSerializer implements JsonSerializer<ItemStack> {
/**
* Gson invokes this call-back method during serialization when it encounters a field of the
* specified type.
*
* <p>In the implementation of this call-back method, you should consider invoking {@link
* JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any
* non-trivial field of the {@code src} object. However, you should never invoke it on the {@code
* src} object itself since that will cause an infinite loop (Gson will call your call-back method
* again).
*
* @param src the object that needs to be converted to Json.
* @param typeOfSrc the actual type (fully genericized version) of the source object.
* @param context
* @return a JsonElement corresponding to the specified object.
*/
@Override
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(Arrays.toString(src.serializeAsBytes()));
}
}

View file

@ -0,0 +1,45 @@
package me.unurled.sacredrealms.sr.components.item.abilities;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
public class AbilityDeserializer implements JsonDeserializer<Ability> {
/**
* Gson invokes this call-back method during deserialization when it encounters a field of the
* specified type.
*
* <p>In the implementation of this call-back method, you should consider invoking {@link
* JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any
* non-trivial field of the returned object. However, you should never invoke it on the same type
* passing {@code json} since that will cause an infinite loop (Gson will call your call-back
* method again).
*
* @param json The Json data being deserialized
* @param typeOfT The type of the Object to deserialize to
* @param context
* @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
* @throws JsonParseException if json is not in the expected format of {@code typeofT}
*/
@Override
public Ability deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
try {
JsonObject jsonObject = json.getAsJsonObject();
String name = jsonObject.get("name").getAsString();
String description = jsonObject.get("description").getAsString();
Integer cooldown = jsonObject.get("cooldown").getAsInt();
Integer manaCost = jsonObject.get("manaCost").getAsInt();
Integer damage = jsonObject.get("damage").getAsInt();
return new Ability(name, description, cooldown, manaCost, damage);
} catch (Exception e) {
throw new JsonParseException("Error deserializing Ability: " + e.getMessage());
}
}
}

View file

@ -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;
}
}

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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);
}
}