add of treasure
Some checks failed
Build / build (push) Failing after 2s

This commit is contained in:
unurled 2024-04-08 17:03:56 +02:00
parent cd84847c14
commit 6af48441a4
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
4 changed files with 220 additions and 0 deletions

View file

@ -0,0 +1,65 @@
package me.unurled.sacredrealms.sr.components.treasure;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
public class Treasure {
/** Chest location */
private Location location;
/** key = place in chest value = itemstack placed in chest at key place */
private Map<Integer, ItemStack> items;
/** Permission to access this chest */
private String permission;
public Treasure() {}
public Treasure(Location location, Map<Integer, ItemStack> items, String permission) {
this.location = location;
this.items = items;
this.permission = permission;
}
/**
* Get the location of the treasure
*
* @return the location of the treasure
*/
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
/**
* Get the items in the chest
*
* @return the items
*/
public Map<Integer, ItemStack> getItems() {
return items;
}
public void setItems(HashMap<Integer, ItemStack> items) {
this.items = items;
}
/**
* Get the permission to open the chest
*
* @return the permission
*/
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
}

View file

@ -0,0 +1,75 @@
package me.unurled.sacredrealms.sr.components.treasure;
import static me.unurled.sacredrealms.sr.utils.Logger.error;
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 io.leangen.geantyref.TypeToken;
import java.lang.reflect.Type;
import java.util.Map;
import me.unurled.sacredrealms.sr.components.item.ItemStackDeserializer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
public class TreasureDeserializer implements JsonDeserializer<Treasure> {
/**
* 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 Treasure deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
Gson gson =
new GsonBuilder()
.registerTypeAdapter(ItemStack.class, new ItemStackDeserializer())
.create();
try {
String loc = gson.fromJson("location", String.class);
String[] parts = loc.split(",");
if (parts.length != 6) {
return null;
}
World world = Bukkit.getServer().getWorld(parts[0]);
double x = Double.parseDouble(parts[1]);
double y = Double.parseDouble(parts[2]);
double z = Double.parseDouble(parts[3]);
float pitch = Float.parseFloat(parts[4]);
float yaw = Float.parseFloat(parts[5]);
Location location = new Location(world, x, y, z, pitch, yaw);
String permission = gson.fromJson("permission", String.class);
String items = gson.fromJson("items", String.class);
Type hash = new TypeToken<Map<Integer, ItemStack>>() {}.getType();
Map<Integer, ItemStack> item = gson.fromJson(items, hash);
if (permission == null || item == null) {
return null;
}
return new Treasure(location, item, permission);
} catch (Exception e) {
error("Error deserializing treasure: " + e.getMessage());
}
return null;
}
}

View file

@ -0,0 +1,39 @@
package me.unurled.sacredrealms.sr.components.treasure;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.List;
import me.unurled.sacredrealms.sr.data.DataHandler;
import me.unurled.sacredrealms.sr.data.DataManager;
import me.unurled.sacredrealms.sr.managers.Manager;
public class TreasureManager extends Manager {
private final List<Treasure> treasures = new ArrayList<>();
/** Save the data */
@Override
public void saveData() {
// save all treasures
for (Treasure treasure : treasures) {
// serialize treasure
DataHandler dh = DataManager.getInstance(DataManager.class).getDataHandler();
Gson gson =
new GsonBuilder().registerTypeAdapter(Treasure.class, new TreasureSerializer()).create();
dh.set("treasures." + treasures.indexOf(treasure), gson.toJson(treasure));
}
}
/** Load the data */
@Override
public void loadData() {
// load all treasures
DataHandler dh = DataManager.getInstance(DataManager.class).getDataHandler();
Gson gson =
new GsonBuilder().registerTypeAdapter(Treasure.class, new TreasureDeserializer()).create();
for (String key : dh.getKeysAll("treasures")) {
// deserialize treasure
treasures.add(gson.fromJson(dh.get("treasures." + key), Treasure.class));
}
}
}

View file

@ -0,0 +1,41 @@
package me.unurled.sacredrealms.sr.components.treasure;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import me.unurled.sacredrealms.sr.components.item.ItemStackSerializer;
import org.bukkit.inventory.ItemStack;
public class TreasureSerializer implements JsonSerializer<Treasure> {
/**
* 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(Treasure src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
obj.add("location", new JsonPrimitive(src.getLocation().toString()));
obj.add("permission", new JsonPrimitive(src.getPermission()));
Gson gson =
new GsonBuilder().registerTypeAdapter(ItemStack.class, new ItemStackSerializer()).create();
obj.add("items", new JsonPrimitive(gson.toJson(src.getItems())));
return obj;
}
}