custom item recode

This commit is contained in:
Indyuce 2022-02-27 13:16:52 +01:00
parent e27980f18d
commit fff24aa077
17 changed files with 255 additions and 245 deletions

View File

@ -17,7 +17,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.util.item.CurrencyItem;
import net.Indyuce.mmocore.util.item.CurrencyItemBuilder;
import io.lumine.mythic.lib.api.util.SmartGive;
public class Withdraw implements Listener {
@ -101,9 +101,9 @@ public class Withdraw implements Listener {
SmartGive smart = new SmartGive(player);
if (note > 0)
smart.give(new CurrencyItem("NOTE", note).build());
smart.give(new CurrencyItemBuilder("NOTE", note).build());
ItemStack coinsItem = new CurrencyItem("GOLD_COIN", 1).build();
ItemStack coinsItem = new CurrencyItemBuilder("GOLD_COIN", 1).build();
coinsItem.setAmount(coins);
smart.give(coinsItem);
}

View File

@ -1,144 +0,0 @@
package net.Indyuce.mmocore.api.util.item;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import net.Indyuce.mmocore.MMOCore;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.version.VersionMaterial;
public class ConfigItem {
private final String name, id, texture;
private final ItemStack item;
private final List<String> lore;
private final int damage, modeldata;
private boolean unbreakable;
private final Map<String, String> placeholders = new HashMap<>();
public ConfigItem(ConfigurationSection config) {
id = config.getName();
name = config.getString("name");
lore = config.getStringList("lore");
item = new ItemStack(Material.valueOf(config.getString("item")));
Validate.notNull(name, "Name cannot be null");
Validate.notNull(lore, "Lore can be empty but not null");
/*
* extra options
*/
damage = config.getInt("damage");
texture = config.getString("texture");
modeldata = config.getInt("custom-model-data");
}
public ConfigItem(String id) {
this(MMOCore.plugin.configItems.get(id));
}
public ConfigItem(ConfigItem cache) {
this.id = cache.id;
name = cache.name;
lore = cache.lore;
item = cache.item;
damage = cache.damage;
texture = cache.texture;
modeldata = cache.modeldata;
unbreakable = cache.unbreakable;
}
public ItemStack getItem(int amount) {
ItemStack item = this.item.clone();
item.setAmount(amount);
return item;
}
public List<String> getLore() {
return lore;
}
public String getName() {
return name;
}
public int getModelData() {
return modeldata;
}
public String getId() {
return id;
}
public ConfigItem setUnbreakable() {
unbreakable = true;
return this;
}
public ConfigItem addPlaceholders(String... placeholders) {
for (int j = 0; j < placeholders.length - 1; j += 2)
this.placeholders.put(placeholders[j], placeholders[j + 1]);
return this;
}
public ItemStack build() {
return build(1);
}
public ItemStack build(int amount) {
ItemStack item = getItem(amount);
ItemMeta meta = item.getItemMeta();
if (meta instanceof Damageable)
((Damageable) meta).setDamage(damage);
if (MythicLib.plugin.getVersion().isStrictlyHigher(1, 13))
meta.setCustomModelData(modeldata);
if (item.getType() == VersionMaterial.PLAYER_HEAD.toMaterial() && texture != null)
try {
Field profileField = meta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
GameProfile profile = new GameProfile(UUID.randomUUID(), null);
profile.getProperties().put("textures", new Property("textures", texture));
profileField.set(meta, profile);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException exception) {
MMOCore.log(Level.WARNING, "Could not load config item texture of " + id);
}
meta.addItemFlags(ItemFlag.values());
meta.setDisplayName(format(name));
List<String> lore = new ArrayList<>();
getLore().forEach(line -> lore.add(format(line)));
meta.setLore(lore);
item.setItemMeta(meta);
return unbreakable ? NBTItem.get(item).addTag(new ItemTag("Unbreakable", true)).toItem() : item;
}
protected String format(String string) {
for (String placeholder : placeholders.keySet())
if (string.contains("{" + placeholder + "}"))
string = string.replace("{" + placeholder + "}", "" + placeholders.get(placeholder));
return MythicLib.plugin.parseColors(string);
}
}

View File

@ -1,45 +0,0 @@
package net.Indyuce.mmocore.api.util.item;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.NBTItem;
public class CurrencyItem extends ConfigItem {
private final int worth, amount;
public CurrencyItem(String key, int worth) {
this(key, worth, 1);
}
public CurrencyItem(String key, int worth, int amount) {
super(key);
addPlaceholders("worth", "" + (this.worth = worth));
this.amount = amount;
}
@Override
public ItemStack build() {
ItemStack item = getItem(amount);
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(ItemFlag.values());
meta.setDisplayName(format(getName()));
List<String> lore = new ArrayList<>();
getLore().forEach(line -> lore.add(format(line)));
meta.setLore(lore);
if(MythicLib.plugin.getVersion().isStrictlyHigher(1, 13))
meta.setCustomModelData(getModelData());
item.setItemMeta(meta);
return NBTItem.get(item).addTag(new ItemTag("RpgWorth", worth)).toItem();
}
}

View File

@ -1,17 +0,0 @@
package net.Indyuce.mmocore.api.util.item;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import io.lumine.mythic.lib.MythicLib;
public class NamedItemStack extends ItemStack {
public NamedItemStack(Material material, String name) {
super(material);
ItemMeta meta = getItemMeta();
meta.setDisplayName(MythicLib.plugin.parseColors(name));
setItemMeta(meta);
}
}

View File

@ -3,7 +3,7 @@ package net.Indyuce.mmocore.command.rpg;
import io.lumine.mythic.lib.api.util.SmartGive;
import io.lumine.mythic.lib.command.api.CommandTreeNode;
import io.lumine.mythic.lib.command.api.Parameter;
import net.Indyuce.mmocore.api.util.item.CurrencyItemBuilder;
import net.Indyuce.mmocore.util.item.CurrencyItemBuilder;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

View File

@ -7,7 +7,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import net.Indyuce.mmocore.api.util.item.CurrencyItemBuilder;
import net.Indyuce.mmocore.util.item.CurrencyItemBuilder;
import io.lumine.mythic.lib.api.util.SmartGive;
import io.lumine.mythic.lib.command.api.CommandTreeNode;
import io.lumine.mythic.lib.command.api.Parameter;

View File

@ -9,7 +9,7 @@ import io.lumine.xikage.mythicmobs.drops.IMultiDrop;
import io.lumine.xikage.mythicmobs.drops.LootBag;
import io.lumine.xikage.mythicmobs.drops.droppables.ItemDrop;
import io.lumine.xikage.mythicmobs.io.MythicLineConfig;
import net.Indyuce.mmocore.api.util.item.CurrencyItem;
import net.Indyuce.mmocore.util.item.CurrencyItemBuilder;
public class CurrencyItemDrop extends Drop implements IMultiDrop {
private final String key;
@ -30,7 +30,7 @@ public class CurrencyItemDrop extends Drop implements IMultiDrop {
@Override
public LootBag get(DropMetadata metadata) {
LootBag loot = new LootBag(metadata);
loot.add(new ItemDrop(this.getLine(), (MythicLineConfig) this.getConfig(), new BukkitItemStack(new CurrencyItem(key, random(minw, maxw)).build())));
loot.add(new ItemDrop(this.getLine(), (MythicLineConfig) this.getConfig(), new BukkitItemStack(new CurrencyItemBuilder(key, random(minw, maxw)).build())));
return loot;
}

View File

@ -2,6 +2,7 @@ package net.Indyuce.mmocore.comp.mythicmobs.load;
import java.util.Random;
import net.Indyuce.mmocore.util.item.SimpleItemBuilder;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
@ -13,8 +14,7 @@ import io.lumine.xikage.mythicmobs.drops.LootBag;
import io.lumine.xikage.mythicmobs.drops.droppables.ItemDrop;
import io.lumine.xikage.mythicmobs.io.MythicLineConfig;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.api.util.item.ConfigItem;
import net.Indyuce.mmocore.api.util.item.CurrencyItem;
import net.Indyuce.mmocore.util.item.CurrencyItemBuilder;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.NBTItem;
@ -35,7 +35,7 @@ public class GoldPouchDrop extends Drop implements IMultiDrop {
@Override
public LootBag get(DropMetadata metadata) {
LootBag loot = new LootBag(metadata);
NBTItem nbt = NBTItem.get(new ConfigItem("MOB_GOLD_POUCH").build());
NBTItem nbt = NBTItem.get(new SimpleItemBuilder("MOB_GOLD_POUCH").build());
ItemStack[] content = new ItemStack[18];
int money = random.nextInt(max - min + 1) + min;
@ -45,11 +45,11 @@ public class GoldPouchDrop extends Drop implements IMultiDrop {
money -= a;
if (a < 30 && random.nextDouble() < .3) {
content[getAvailableSlot(content)] = new CurrencyItem("GOLD_COIN", 1, a).build();
content[getAvailableSlot(content)] = setAmount(new CurrencyItemBuilder("GOLD_COIN", 1).build(), a);
continue;
}
content[getAvailableSlot(content)] = new CurrencyItem("NOTE", a, 1).build();
content[getAvailableSlot(content)] = new CurrencyItemBuilder("NOTE", a).build();
}
nbt.addTag(new ItemTag("RpgPouchSize", 18), new ItemTag("RpgPouchMob", true), new ItemTag("RpgPouchInventory", MMOCoreUtils.toBase64(content)));
@ -57,6 +57,11 @@ public class GoldPouchDrop extends Drop implements IMultiDrop {
return loot;
}
private ItemStack setAmount(ItemStack item, int amount) {
item.setAmount(amount);
return item;
}
private int getAvailableSlot(ItemStack[] content) {
int slot;
while (content[slot = random.nextInt(content.length)] != null)

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmocore.gui.eco;
import net.Indyuce.mmocore.util.item.SimpleItemBuilder;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -13,7 +14,6 @@ import org.bukkit.scheduler.BukkitRunnable;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.api.util.item.ConfigItem;
import net.Indyuce.mmocore.gui.api.PluginInventory;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.api.util.SmartGive;
@ -30,7 +30,7 @@ public class DepositMenu extends PluginInventory {
public Inventory getInventory() {
Inventory inv = Bukkit.createInventory(this, 27, "Deposit");
inv.setItem(26, depositItem = new ConfigItem("DEPOSIT_ITEM").addPlaceholders("worth", "0").build());
inv.setItem(26, depositItem = new SimpleItemBuilder("DEPOSIT_ITEM").addPlaceholders("worth", "0").build());
new BukkitRunnable() {
@ -115,6 +115,6 @@ public class DepositMenu extends PluginInventory {
private void updateDeposit(Inventory inv) {
deposit = MMOCoreUtils.getWorth(inv.getContents());
inv.setItem(26, depositItem = new ConfigItem("DEPOSIT_ITEM").addPlaceholders("worth", "" + deposit).build());
inv.setItem(26, depositItem = new SimpleItemBuilder("DEPOSIT_ITEM").addPlaceholders("worth", "" + deposit).build());
}
}

View File

@ -7,7 +7,7 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.scheduler.BukkitRunnable;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.util.math.particle.ChestParticleEffect;
import net.Indyuce.mmocore.loot.chest.particle.ChestParticleEffect;
public class TierEffect {
private final ChestParticleEffect type;

View File

@ -1,16 +1,19 @@
package net.Indyuce.mmocore.loot.droptable.dropitem;
import net.Indyuce.mmocore.loot.LootBuilder;
import net.Indyuce.mmocore.api.util.item.CurrencyItem;
import io.lumine.mythic.lib.api.MMOLineConfig;
import net.Indyuce.mmocore.util.item.CurrencyItemBuilder;
import net.Indyuce.mmocore.loot.LootBuilder;
import org.bukkit.inventory.ItemStack;
public class GoldDropItem extends DropItem {
public GoldDropItem(MMOLineConfig config) {
super(config);
}
public GoldDropItem(MMOLineConfig config) {
super(config);
}
@Override
public void collect(LootBuilder builder) {
builder.addLoot(new CurrencyItem("GOLD_COIN", 1, rollAmount()).build());
}
@Override
public void collect(LootBuilder builder) {
ItemStack item = new CurrencyItemBuilder("GOLD_COIN", 1).build();
item.setAmount(rollAmount());
builder.addLoot(item);
}
}

View File

@ -1,23 +1,26 @@
package net.Indyuce.mmocore.loot.droptable.dropitem;
import net.Indyuce.mmocore.loot.LootBuilder;
import net.Indyuce.mmocore.api.util.item.CurrencyItem;
import io.lumine.mythic.lib.api.MMOLineConfig;
import net.Indyuce.mmocore.util.item.CurrencyItemBuilder;
import net.Indyuce.mmocore.loot.LootBuilder;
import org.bukkit.inventory.ItemStack;
public class NoteDropItem extends DropItem {
private final int min, max;
private final int min, max;
public NoteDropItem(MMOLineConfig config) {
super(config);
public NoteDropItem(MMOLineConfig config) {
super(config);
config.validate("max", "min");
config.validate("max", "min");
min = (int) config.getDouble("min");
max = (int) config.getDouble("max");
}
min = (int) config.getDouble("min");
max = (int) config.getDouble("max");
}
@Override
public void collect(LootBuilder builder) {
builder.addLoot(new CurrencyItem("NOTE", random.nextInt(max - min + 1) + min, rollAmount()).build());
}
@Override
public void collect(LootBuilder builder) {
ItemStack item = new CurrencyItemBuilder("NOTE", random.nextInt(max - min + 1) + min).build();
item.setAmount(rollAmount());
builder.addLoot(item);
}
}

View File

@ -7,7 +7,7 @@ import java.util.logging.Level;
import org.bukkit.configuration.file.FileConfiguration;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.util.item.ConfigItem;
import net.Indyuce.mmocore.util.item.ConfigItem;
public class ConfigItemManager {
private final Map<String, ConfigItem> map = new HashMap<>();

View File

@ -0,0 +1,88 @@
package net.Indyuce.mmocore.util.item;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.version.VersionMaterial;
import net.Indyuce.mmocore.MMOCore;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.util.*;
import java.util.logging.Level;
public abstract class AbstractItemBuilder {
private final ConfigItem configItem;
private final Map<String, String> placeholders = new HashMap<>();
public AbstractItemBuilder(@NotNull ConfigItem configItem) {
this.configItem = Objects.requireNonNull(configItem, "Config item cannot be null");
}
public AbstractItemBuilder(String key) {
this(MMOCore.plugin.configItems.get(key));
}
public ConfigItem getItem() {
return configItem;
}
public AbstractItemBuilder addPlaceholders(String... placeholders) {
for (int j = 0; j < placeholders.length - 1; j += 2)
this.placeholders.put(placeholders[j], placeholders[j + 1]);
return this;
}
public ItemStack build() {
ItemStack item = new ItemStack(configItem.getMaterial());
ItemMeta meta = item.getItemMeta();
if (item.getType() == VersionMaterial.PLAYER_HEAD.toMaterial() && configItem.getTexture() != null)
try {
Field profileField = meta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
GameProfile profile = new GameProfile(UUID.randomUUID(), null);
profile.getProperties().put("textures", new Property("textures", configItem.getTexture()));
profileField.set(meta, profile);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException exception) {
MMOCore.log(Level.WARNING, "Could not load texture of config item called '" + configItem.getId() + "'");
}
meta.addItemFlags(ItemFlag.values());
meta.setDisplayName(applyPlaceholders(configItem.getName()));
if (configItem.isUnbreakable())
meta.setUnbreakable(true);
if (meta instanceof Damageable)
((Damageable) meta).setDamage(configItem.getDamage());
meta.setCustomModelData(configItem.getModelData());
List<String> lore = new ArrayList<>();
configItem.getLore().forEach(line -> lore.add(applyPlaceholders(line)));
meta.setLore(lore);
whenBuildingMeta(item, meta);
item.setItemMeta(meta);
NBTItem nbtItem = NBTItem.get(item);
nbtItem.addTag(new ItemTag("MMOCoreItemId", configItem.getId()));
whenBuildingNBT(nbtItem);
return nbtItem.toItem();
}
public abstract void whenBuildingMeta(ItemStack item, ItemMeta meta);
public abstract void whenBuildingNBT(NBTItem nbtItem);
public String applyPlaceholders(String string) {
for (String placeholder : placeholders.keySet())
if (string.contains("{" + placeholder + "}"))
string = string.replace("{" + placeholder + "}", "" + placeholders.get(placeholder));
return MythicLib.plugin.parseColors(string);
}
}

View File

@ -0,0 +1,65 @@
package net.Indyuce.mmocore.util.item;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import javax.annotation.Nullable;
import java.util.List;
public class ConfigItem {
private final String name, id, texture;
private final Material material;
private final List<String> lore;
private final int damage, modeldata;
private final boolean unbreakable;
public ConfigItem(ConfigurationSection config) {
id = config.getName();
name = config.getString("name");
lore = config.getStringList("lore");
material = Material.valueOf(config.getString("item"));
Validate.notNull(name, "Name cannot be null");
Validate.notNull(lore, "Lore can be empty but not null");
// Extra options
damage = config.getInt("damage");
texture = config.getString("texture");
modeldata = config.getInt("custom-model-data");
unbreakable = config.getBoolean("unbreakable");
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public Material getMaterial() {
return material;
}
public int getDamage() {
return damage;
}
@Nullable
public String getTexture() {
return texture;
}
public boolean isUnbreakable() {
return unbreakable;
}
public List<String> getLore() {
return lore;
}
public int getModelData() {
return modeldata;
}
}

View File

@ -0,0 +1,26 @@
package net.Indyuce.mmocore.util.item;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.NBTItem;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class CurrencyItemBuilder extends AbstractItemBuilder {
private final int worth;
public CurrencyItemBuilder(String key, int worth) {
super(key);
addPlaceholders("worth", String.valueOf(this.worth = worth));
}
@Override
public void whenBuildingMeta(ItemStack item, ItemMeta meta) {
// Nothing
}
@Override
public void whenBuildingNBT(NBTItem nbtItem) {
nbtItem.addTag(new ItemTag("RpgWorth", worth));
}
}

View File

@ -0,0 +1,26 @@
package net.Indyuce.mmocore.util.item;
import io.lumine.mythic.lib.api.item.NBTItem;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
public class SimpleItemBuilder extends AbstractItemBuilder {
public SimpleItemBuilder(@NotNull ConfigItem configItem) {
super(configItem);
}
public SimpleItemBuilder(@NotNull String key) {
super(key);
}
@Override
public void whenBuildingMeta(ItemStack item, ItemMeta meta) {
// Nothing
}
@Override
public void whenBuildingNBT(NBTItem nbtItem) {
// Nothing
}
}