Save and load items

This commit is contained in:
unurled 2024-03-15 11:52:08 +01:00
parent 627e533e9c
commit d12bf53972
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
8 changed files with 245 additions and 53 deletions

View file

@ -2,12 +2,19 @@ package me.unurled.sacredrealms.sr.components.item;
import static me.unurled.sacredrealms.sr.utils.Component.comp;
import static me.unurled.sacredrealms.sr.utils.Items.lore;
import static me.unurled.sacredrealms.sr.utils.Logger.error;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.HashMap;
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.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;
@ -38,7 +45,7 @@ import org.jetbrains.annotations.NotNull;
* */
public class Item {
@NotNull private final String ID;
@NotNull private String ID;
@NotNull private String name;
@ -50,12 +57,27 @@ public class Item {
@NotNull private Rarity rarity;
@NotNull private ItemType type;
@NotNull private HashMap<Attribute, Double> attributes;
@NotNull private HashMap<Enchantment, Integer> enchantments;
@NotNull private List<Ability> abilities;
public Item() {
this.ID = "";
this.name = "";
this.material = Material.AIR;
this.description = "";
this.rarity = Rarity.COMMON;
this.customModelData = 0;
this.abilities = List.of();
this.attributes = new HashMap<>();
this.enchantments = new HashMap<>();
this.type = ItemType.MISC;
}
public Item(@NotNull String ID, @NotNull String name, @NotNull Material material) {
this.ID = ID;
this.name = name;
@ -66,6 +88,7 @@ public class Item {
this.abilities = List.of();
this.attributes = new HashMap<>();
this.enchantments = new HashMap<>();
this.type = ItemType.MISC;
}
public @NotNull String getID() {
@ -189,24 +212,55 @@ public class Item {
@Override
public String toString() {
return "{"
+ "ID: "
+ "\"id\": \""
+ ID
+ ", name: "
+ "\", \"name\": \""
+ name
+ ", material: "
+ "\", \"material\": \""
+ material
+ ", description: "
+ "\", \"description\": \""
+ description
+ ", customModelData: "
+ "\", \"customModelData\": "
+ customModelData
+ ", rarity: "
+ "\", \"rarity\": \""
+ rarity
+ ", attributes: "
+ "\", \"type\": \""
+ type
+ "\", \"attributes\": \""
+ attributes
+ ", enchantments: "
+ "\", \"enchantments\": \""
+ enchantments
+ ", abilities: "
+ "\", \"abilities\": \""
+ abilities
+ "}";
+ "\"}";
}
public void fromString(String item) {
// extract every information from the string (it follows the same format as the toString method)
Gson gson =
new GsonBuilder().registerTypeAdapter(Ability.class, new AbilityDeserializer()).create();
try {
Map<String, Object> map =
gson.fromJson(item, new TypeToken<Map<String, Object>>() {}.getType());
this.ID = (String) map.get("id");
this.name = (String) map.get("name");
this.material = Material.valueOf((String) map.get("material"));
this.description = (String) map.get("description");
this.customModelData = ((Double) map.get("customModelData")).intValue();
this.rarity = Rarity.valueOf((String) map.get("rarity"));
this.type = ItemType.valueOf((String) map.get("type"));
Type attributeType = new TypeToken<HashMap<Attribute, Double>>() {}.getType();
this.attributes = gson.fromJson((String) map.get("attributes"), attributeType);
Type enchantmentType = new TypeToken<HashMap<Enchantment, Integer>>() {}.getType();
this.enchantments = gson.fromJson((String) map.get("enchantments"), enchantmentType);
Type abilityType = new TypeToken<List<Ability>>() {}.getType();
this.abilities = gson.fromJson((String) map.get("abilities"), abilityType);
} catch (Exception e) {
error("Failed to parse item from string: " + item + "\n" + e.getMessage());
}
}
}

View file

@ -1,6 +1,12 @@
package me.unurled.sacredrealms.sr.components.item;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.HashMap;
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;
@ -17,14 +23,41 @@ public class ItemManager extends Manager {
items = new HashMap<>();
}
/** Register all the items */
private void registerItems() {}
/** Load the manager */
@Override
public void load() {
super.load();
registerItems();
}
/** Save the data */
@Override
public void saveData() {
DataManager dm = DataManager.getInstance(DataManager.class);
DataHandler dh = dm.getDataHandler();
Gson gson =
new GsonBuilder().registerTypeAdapter(Item.class, new ItemStackSerializer()).create();
items.forEach(
(id, item) -> {
dh.set("sr.items." + id, gson.toJson(item));
});
}
/** Load the data */
@Override
public void loadData() {
DataManager dm = DataManager.getInstance(DataManager.class);
DataHandler dh = dm.getDataHandler();
Gson gson = new GsonBuilder().registerTypeAdapter(Item.class, new ItemDeserializer()).create();
dh.getKeysAll("sr.items")
.forEach(
key -> {
Item item = gson.fromJson(dh.get(key), Item.class);
items.put(item.getID(), item);
});
}
public void addItem(Item item) {

View file

@ -0,0 +1,8 @@
package me.unurled.sacredrealms.sr.components.item;
public enum ItemType {
WEAPON,
ARMOR,
CONSUMABLE,
MISC
}

View file

@ -1,41 +1,26 @@
package me.unurled.sacredrealms.sr.components.item.abilities;
/** Represents an ability that an item can have. TODO: Implement this class */
@SuppressWarnings("unused")
public class Ability {
public record Ability(
String name, String description, Integer cooldown, Integer manaCost, Integer damage) {
private final String name;
private final String description;
private final Integer cooldown;
private final Integer manaCost;
private final Integer damage;
public Ability(
String name, String description, Integer cooldown, Integer manaCost, Integer damage) {
this.name = name;
this.description = description;
this.cooldown = cooldown;
this.manaCost = manaCost;
this.damage = damage;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public Integer getCooldown() {
return cooldown;
}
public Integer getManaCost() {
return manaCost;
}
public Integer getDamage() {
return damage;
@Override
public String toString() {
return "Ability{"
+ "name='"
+ name
+ '\''
+ ", description='"
+ description
+ '\''
+ ", cooldown="
+ cooldown
+ ", manaCost="
+ manaCost
+ ", damage="
+ damage
+ '}';
}
}