From 0ded0879da2ac797975c10ac721f642513225131 Mon Sep 17 00:00:00 2001 From: Roch Blonndiaux Date: Mon, 20 Feb 2023 17:10:25 +0100 Subject: [PATCH] New interfaces --- .../java/net/Indyuce/mmoitems/api/Type.java | 1 + .../mmoitems/api/item/type/MMOItemType.java | 85 +++++++++ .../api/item/type/MMOPredefinedType.java | 165 ++++++++++++++++++ .../api/item/type/set/MMOTypeSet.java | 12 ++ 4 files changed, 263 insertions(+) create mode 100644 MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/MMOItemType.java create mode 100644 MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/MMOPredefinedType.java create mode 100644 MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/set/MMOTypeSet.java diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java index 36375eb0..50ccc572 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java @@ -21,6 +21,7 @@ import java.util.Objects; @SuppressWarnings("unused") public class Type { + // Slashing public static final Type SWORD = new Type(TypeSet.SLASHING, "SWORD", true, ModifierSource.MELEE_WEAPON); diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/MMOItemType.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/MMOItemType.java new file mode 100644 index 00000000..dd26b584 --- /dev/null +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/MMOItemType.java @@ -0,0 +1,85 @@ +package net.Indyuce.mmoitems.api.item.type; + +import io.lumine.mythic.lib.api.item.NBTItem; +import io.lumine.mythic.lib.player.modifier.ModifierSource; +import net.Indyuce.mmoitems.MMOItems; +import net.Indyuce.mmoitems.api.ConfigFile; +import net.Indyuce.mmoitems.api.Type; +import net.Indyuce.mmoitems.api.item.type.set.MMOTypeSet; +import net.Indyuce.mmoitems.api.item.util.identify.UnidentifiedItem; +import net.Indyuce.mmoitems.stat.type.ItemStat; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +import javax.annotation.Nullable; +import java.util.List; + +/** + * mmoitems + * 20/02/2023 + * + * @author Roch Blondiaux (Kiwix). + */ +public interface MMOItemType { + + @NotNull + String getId(); + + @NotNull + String getName(); + + boolean isWeapon(); + + ModifierSource getModifierSource(); + + ItemStack toItemStack(); + + boolean isFourGUIMode(); + + String getLoreFormat(); + + List> getAvailableStats(); + + ConfigFile getConfiguration(); + + UnidentifiedItem getUnidentifiedItem(); + + void load(ConfigurationSection config); + + /** + * Used in command executors and completions for easier manipulation + * + * @param id The type id + * @return If a registered type with this ID could be found + */ + static boolean isValid(@Nullable String id) { + return id != null && MMOItems.plugin.getTypes().has(id.toUpperCase().replace("-", "_").replace(" ", "_")); + } + + /** + * Reads an ItemStack in hopes for finding its MMOItem Type. + * + * @param item The item to retrieve the type from + * @return The type of the item, if it has a type. + */ + @Nullable + @Contract("null -> null") + static MMOTypeSet get(@Nullable ItemStack item) { + return get(NBTItem.get(item).getType()); + } + + /** + * Used in command executors and completions for easier manipulation + * + * @param id The type id + * @return The type or null if it couldn't be found + */ + @Nullable + @Contract("null -> null") + static MMOTypeSet get(@Nullable String id) { + String format = id.toUpperCase().replace("-", "_").replace(" ", "_"); + return MMOItems.plugin.getTypes().has(format) ? MMOItems.plugin.getTypes().get(format) : null; + } +} diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/MMOPredefinedType.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/MMOPredefinedType.java new file mode 100644 index 00000000..9ffc6976 --- /dev/null +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/MMOPredefinedType.java @@ -0,0 +1,165 @@ +package net.Indyuce.mmoitems.api.item.type; + +import io.lumine.mythic.lib.MythicLib; +import io.lumine.mythic.lib.player.modifier.ModifierSource; +import net.Indyuce.mmoitems.api.ConfigFile; +import net.Indyuce.mmoitems.api.TypeSet; +import net.Indyuce.mmoitems.api.item.type.set.MMOTypeSet; +import net.Indyuce.mmoitems.api.item.util.identify.UnidentifiedItem; +import net.Indyuce.mmoitems.manager.TypeManager; +import net.Indyuce.mmoitems.stat.type.ItemStat; +import org.apache.commons.lang.Validate; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * mmoitems + * 20/02/2023 + * + * @author Roch Blondiaux (Kiwix). + */ +public class MMOPredefinedType implements MMOItemType { + + private final String id; + private final MMOTypeSet set; + private final ModifierSource modifierSource; + private final boolean weapon; + + // Configurable + private String name; + + @Nullable + private String loreFormat; + + /** + * Used to display the item in the item explorer and in the item recipes + * list in the advanced workbench. can also be edited using the config + * files. + */ + private ItemStack item; + private UnidentifiedItem unidentifiedTemplate; + + /** + * List of stats which can be applied onto an item which has this type. This + * improves performance when generating an item by a significant amount. + */ + private final List> stats = new ArrayList<>(); + + public MMOPredefinedType(String id, boolean weapon, ModifierSource modSource) { + this.id = id.toUpperCase().replace("-", "_").replace(" ", "_"); + this.modifierSource = modSource; + this.weapon = weapon; + this.loreFormat = null; + } + + public MMOPredefinedType(@NotNull TypeManager manager, @NotNull ConfigurationSection config) { + id = config.getName().toUpperCase().replace("-", "_").replace(" ", "_"); + + parent = manager.get(config.getString("parent", "").toUpperCase().replace("-", "_").replace(" ", "_")); + + set = (parent != null ? parent.set : TypeSet.EXTRA); + weapon = (parent != null && parent.weapon); + modifierSource = (parent != null ? parent.modifierSource : ModifierSource.OTHER); + this.loreFormat = config.getString("LoreFormat", (parent != null ? parent.loreFormat : null)); + } + + @NotNull + @Override + public String getId() { + return this.id; + } + + @NotNull + @Override + public String getName() { + return this.name; + } + + @Override + public boolean isWeapon() { + return this.weapon; + } + + @Override + public ModifierSource getModifierSource() { + return this.modifierSource; + } + + @Override + public ItemStack toItemStack() { + return this.item.clone(); + } + + @Override + public boolean isFourGUIMode() { + + } + + @Override + public String getLoreFormat() { + return this.loreFormat; + } + + @Override + public List> getAvailableStats() { + return this.stats; + } + + @Override + public ConfigFile getConfiguration() { + return new ConfigFile("/item", this.id.toLowerCase()); + } + + @Override + public UnidentifiedItem getUnidentifiedItem() { + return this.unidentifiedTemplate; + } + + @Override + public void load(ConfigurationSection config) { + Validate.notNull(config, String.format("Could not find config for %s", getId())); + + name = config.getString("name", name); + item = read(config.getString("display", item == null ? Material.STONE.toString() : item.getType().toString())); + + (unidentifiedTemplate = new UnidentifiedItem(this)).update(config.getConfigurationSection("unident-item")); + + // Getting overridden? + loreFormat = config.getString("LoreFormat", (parent != null ? parent.loreFormat : loreFormat)); + } + + private ItemStack read(String data) { + Validate.notNull(data, "Input must not be null"); + + String[] split = data.split(":"); + Material material = Material.valueOf(split[0]); + return split.length > 1 ? MythicLib.plugin.getVersion().getWrapper().textureItem(material, Integer.parseInt(split[1])) : new ItemStack(material); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MMOTypeSet type = (MMOTypeSet) o; + return id.equals(type.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Type{" + + "id='" + id + '\'' + + '}'; + } +} diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/set/MMOTypeSet.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/set/MMOTypeSet.java new file mode 100644 index 00000000..9389893d --- /dev/null +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/type/set/MMOTypeSet.java @@ -0,0 +1,12 @@ +package net.Indyuce.mmoitems.api.item.type.set; + +/** + * mmoitems + * 20/02/2023 + * + * @author Roch Blondiaux (Kiwix). + */ +public interface MMOTypeSet { + + +}