save item when modified

This commit is contained in:
unurled 2024-05-25 15:49:56 +02:00
parent d52fc0d9eb
commit 0edce64ab3
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
3 changed files with 91 additions and 50 deletions

View file

@ -55,87 +55,101 @@ public class ItemCommand implements TabExecutor {
return;
}
boolean modified = false;
switch (args[2].toLowerCase()) {
case "name" -> modifyName(sender, args, p, item);
case MATERIAL -> modifyMaterial(sender, args, p, item);
case ENCHANTMENTS -> modifyEnchantments(args, p);
case ATTRIBUTES -> modifyAttributes(sender, args, p, item);
case ABILITIES -> modifyAbilities(sender, args, p);
case CUSTOMMODELDATA -> modifyModelData(sender, args, p, item);
case RARITY -> modifyRarity(sender, args, p, item);
case "type" -> modifyType(sender, args, p, item);
case "description" -> modifyDescription(sender, args, p, item);
default -> p.sendMessage(USAGE);
case "name" -> modified = modifyName(sender, args, p, item);
case MATERIAL -> modified = modifyMaterial(sender, args, p, item);
case ENCHANTMENTS -> modified = modifyEnchantments(args, p);
case ATTRIBUTES -> modified = modifyAttributes(sender, args, p, item);
case ABILITIES -> modified = modifyAbilities(sender, args, p);
case CUSTOMMODELDATA -> modified = modifyModelData(sender, args, p, item);
case RARITY -> modified = modifyRarity(sender, args, p, item);
case "type" -> modified = modifyType(sender, args, p, item);
case "description" -> modified = modifyDescription(sender, args, p, item);
default -> {
p.sendMessage(USAGE);
return;
}
}
// save the item
if (item != null && modified) {
im.saveItem(item);
}
}
private static void modifyDescription(
private static boolean modifyDescription(
@NotNull CommandSender sender, @NotNull String @NotNull [] args, Player p, Item item) {
if (args.length < 4) {
p.sendMessage("Usage: /item modify <id> description <value>");
return;
return false;
}
String description = String.join(" ", Arrays.copyOfRange(args, 3, args.length));
item.setDescription(description);
sender.sendMessage(comp("<green>Description set."));
return true;
}
private static void modifyType(
private static boolean modifyType(
@NotNull CommandSender sender, @NotNull String @NotNull [] args, Player p, Item item) {
if (args.length < 4) {
p.sendMessage("Usage: /item modify <id> type <value>");
return;
return false;
}
ItemType type;
try {
type = ItemType.valueOf(args[3]);
} catch (Exception e) {
sender.sendMessage(comp("<red>Type not found."));
return;
return false;
}
item.setType(type);
sender.sendMessage(comp("<green>Type set."));
return true;
}
private static void modifyRarity(
private static boolean modifyRarity(
@NotNull CommandSender sender, @NotNull String @NotNull [] args, Player p, Item item) {
if (args.length < 4) {
p.sendMessage("Usage: /item modify <id> rarity <value>");
return;
return false;
}
Rarity rarity;
try {
rarity = Rarity.valueOf(args[3]);
} catch (Exception e) {
sender.sendMessage(comp("<red>Rarity not found."));
return;
return false;
}
item.setRarity(rarity);
sender.sendMessage(comp("<green>Rarity set."));
return true;
}
private static void modifyModelData(
private static boolean modifyModelData(
@NotNull CommandSender sender, @NotNull String @NotNull [] args, Player p, Item item) {
if (args.length < 4) {
p.sendMessage("Usage: /item modify <id> custommodeldata <value>");
return;
return false;
}
int customModelData;
try {
customModelData = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
sender.sendMessage(comp("<red>Invalid value."));
return;
return false;
}
item.setCustomModelData(customModelData);
sender.sendMessage(comp("<green>Custom model data set."));
return true;
}
private static void modifyAbilities(
private static boolean modifyAbilities(
@NotNull CommandSender sender, @NotNull String @NotNull [] args, Player p) {
if (args.length < 5) {
p.sendMessage("Usage: /item modify <id> abilities add <ability>");
return;
return false;
}
if (args[3].equalsIgnoreCase("add")) {
p.sendMessage("Abilities not working for now.");
@ -144,69 +158,73 @@ public class ItemCommand implements TabExecutor {
sender.sendMessage("Abilities not working for now.");
// TODO: remove ability
}
return true;
}
private static void modifyName(
private static boolean modifyName(
@NotNull CommandSender sender, @NotNull String @NotNull [] args, Player p, Item item) {
if (args.length < 4) {
p.sendMessage("Usage: /item modify <id> name <value>");
return;
return false;
}
String name = String.join(" ", Arrays.copyOfRange(args, 3, args.length));
item.setName(name);
sender.sendMessage(comp("<green>Item name set."));
// TODO: update item in player inventory
return true;
}
private static void modifyMaterial(
private static boolean modifyMaterial(
@NotNull CommandSender sender, @NotNull String @NotNull [] args, Player p, Item item) {
if (args.length < 4) {
p.sendMessage("Usage: /item modify <id> material <value>");
return;
return false;
}
Material material = Material.matchMaterial(args[3]);
if (material == null) {
sender.sendMessage(comp("<red>Material not found."));
return;
return false;
}
item.setMaterial(material);
sender.sendMessage(comp("<green>Item material set."));
return true;
}
private static void modifyEnchantments(@NotNull String @NotNull [] args, Player p) {
private static boolean modifyEnchantments(@NotNull String @NotNull [] args, Player p) {
if (args.length < 4) {
p.sendMessage("Usage: /item modify <id> enchantments <add|remove> [enchantment]");
return;
return false;
}
if (args[3].equalsIgnoreCase("add")) {
if (args.length < 5) {
p.sendMessage("Usage: /item modify <id> enchantments add <enchantment>");
return;
return false;
}
// TODO: add enchantment
p.sendMessage("Enchantments not working for now.");
return;
return false;
}
if (args[3].equalsIgnoreCase(REMOVE)) {
if (args.length < 5) {
p.sendMessage("Usage: /item modify <id> enchantments remove <enchantment>");
return;
return false;
}
// TODO: remove enchantment
p.sendMessage("Enchantments not working for now.");
}
return true;
}
private static void modifyAttributes(
private static boolean modifyAttributes(
@NotNull CommandSender sender, @NotNull String @NotNull [] args, Player p, Item item) {
if (args.length < 4) {
p.sendMessage("Usage: /item modify <id> attributes <add|remove> [attribute] [value]");
return;
return false;
}
if (args[3].equalsIgnoreCase("add")) {
if (args.length < 6) {
p.sendMessage("Usage: /item modify <id> attributes add <attribute> <value>");
return;
return false;
}
Attribute attribute = null;
try {
@ -214,23 +232,23 @@ public class ItemCommand implements TabExecutor {
} catch (Exception e) {
sender.sendMessage(comp("<red>Attribute not found."));
error("Error on attribute affectation on item: " + args[1] + "\n" + e);
return;
return false;
}
double value;
try {
value = Double.parseDouble(args[5]);
} catch (NumberFormatException e) {
sender.sendMessage(comp("<red>Invalid value."));
return;
return false;
}
item.addAttribute(attribute, value);
sender.sendMessage(comp("<green>Attribute added."));
return;
return false;
}
if (args[3].equalsIgnoreCase(REMOVE)) {
if (args.length < 5) {
p.sendMessage("Usage: /item modify <id> attributes remove <attribute>");
return;
return false;
}
Attribute attribute = null;
try {
@ -238,11 +256,12 @@ public class ItemCommand implements TabExecutor {
} catch (Exception e) {
sender.sendMessage(comp("<red>Attribute not found."));
error("Error on attribute affectation on item: " + args[1] + "\n" + e);
return;
return false;
}
item.removeAttribute(attribute);
sender.sendMessage(comp("<green>Attribute removed."));
}
return true;
}
private static void give(
@ -346,7 +365,13 @@ public class ItemCommand implements TabExecutor {
} else if (args[2].equalsIgnoreCase("type")) {
return Arrays.stream(ItemType.values()).map(Enum::name).toList();
} else if (args[2].equalsIgnoreCase(MATERIAL)) {
return Arrays.stream(Material.values()).map(Enum::name).toList();
return Arrays.stream(Material.values())
.map(
name -> {
if (name.name().startsWith(args[3].toUpperCase())) return name.name();
return null;
})
.toList();
} else if (args[2].equalsIgnoreCase(ENCHANTMENTS)) {
return List.of("add", REMOVE);
} else if (args[2].equalsIgnoreCase(ATTRIBUTES)) {
@ -354,9 +379,9 @@ public class ItemCommand implements TabExecutor {
} else if (args[2].equalsIgnoreCase(ABILITIES)) {
return List.of("add", REMOVE);
} else if (args[2].equalsIgnoreCase(CUSTOMMODELDATA)) {
try {
try {
Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
} catch (NumberFormatException e) {
return List.of("1");
}
}
@ -476,7 +501,7 @@ public class ItemCommand implements TabExecutor {
|| args[0].equalsIgnoreCase("give")
|| args[0].equalsIgnoreCase(MODIFY))) {
ItemManager im = Manager.getInstance(ItemManager.class);
return im.getItemIDs();
return im.getItemIDs();
}
List<String> completions = argsLengthThree(args);

View file

@ -4,12 +4,14 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.HashMap;
import java.util.List;
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.managers.Manager;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
public class ItemManager extends Manager {
public static final NamespacedKey ID = new NamespacedKey("sr", "id");
@ -27,8 +29,7 @@ public class ItemManager extends Manager {
DataManager dm = Manager.getInstance(DataManager.class);
DataHandler dh = dm.getDataHandler();
Gson gson =
new GsonBuilder().registerTypeAdapter(Item.class, new ItemStackSerializer()).create();
Gson gson = new GsonBuilder().registerTypeAdapter(Item.class, new ItemSerializer()).create();
items.forEach((id, item) -> dh.set("sr.items." + id, gson.toJson(item)));
}
@ -39,6 +40,11 @@ public class ItemManager extends Manager {
DataManager dm = Manager.getInstance(DataManager.class);
DataHandler dh = dm.getDataHandler();
if (dh == null) {
SR.getInstance().getLogger().severe("DataHandler is null in ItemManager.loadData()!");
return;
}
Gson gson = new GsonBuilder().registerTypeAdapter(Item.class, new ItemDeserializer()).create();
dh.getKeysAll("sr.items")
@ -49,6 +55,15 @@ public class ItemManager extends Manager {
});
}
public void saveItem(@NotNull Item item) {
DataManager dm = Manager.getInstance(DataManager.class);
DataHandler dh = dm.getDataHandler();
Gson gson = new GsonBuilder().registerTypeAdapter(Item.class, new ItemSerializer()).create();
dh.set("sr.items." + item.getId(), gson.toJson(item));
}
public void addItem(Item item) {
items.put(item.getId(), item);
}

View file

@ -15,6 +15,7 @@ import me.unurled.sacredrealms.sr.components.player.PlayerManager;
import me.unurled.sacredrealms.sr.components.treasure.TreasureManager;
import me.unurled.sacredrealms.sr.data.DataManager;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Managers {
@ -29,15 +30,15 @@ public class Managers {
List.of(
DataManager.class,
PlayerManager.class,
ItemManager.class,
CombatManager.class,
CommandManager.class,
ItemManager.class,
EntityManager.class,
ClientBuildManager.class,
TreasureManager.class));
}
private void register(List<Class<? extends Manager>> clazz) {
private void register(@NotNull List<Class<? extends Manager>> clazz) {
int i = 0;
for (Class<? extends Manager> c : clazz) {
Bukkit.getScheduler()
@ -53,7 +54,7 @@ public class Managers {
error("Failed to register manager: " + c.getSimpleName());
}
},
5L * i);
10L * i);
i++;
}
}