Fixed elements

This commit is contained in:
Indyuce 2022-08-17 19:35:18 +02:00
parent d716fddbd4
commit 13dec22e5f
6 changed files with 108 additions and 21 deletions

View File

@ -38,7 +38,6 @@ import net.Indyuce.mmoitems.comp.rpg.McMMOHook;
import net.Indyuce.mmoitems.comp.rpg.RPGHandler;
import net.Indyuce.mmoitems.gui.PluginInventory;
import net.Indyuce.mmoitems.gui.edition.recipe.RecipeBrowserGUI;
import net.Indyuce.mmoitems.gui.listener.GuiListener;
import net.Indyuce.mmoitems.listener.*;
import net.Indyuce.mmoitems.manager.*;
import org.apache.commons.lang.Validate;
@ -79,6 +78,7 @@ public class MMOItems extends JavaPlugin {
private final PlayerInventoryHandler inventory = new PlayerInventoryHandler();
private final List<StringInputParser> stringInputParsers = new ArrayList<>();
private final List<EnchantPlugin<? extends Enchantment>> enchantPlugins = new ArrayList<>();
private final StatManager statManager = new StatManager();
private DropTableManager dropTableManager;
private WorldGenManager worldGenManager;
@ -86,7 +86,6 @@ public class MMOItems extends JavaPlugin {
private ConfigManager configManager;
private BlockManager blockManager;
private TierManager tierManager;
private StatManager statManager;
private SetManager setManager;
private PlaceholderParser placeholderParser = new DefaultPlaceholderParser();
@ -119,11 +118,7 @@ public class MMOItems extends JavaPlugin {
saveDefaultConfig();
configManager = new ConfigManager();
/*
* Stat manager must be initialized before MMOCore compatibility
* initializes so that MMOCore can register its stats
*/
statManager = new StatManager();
statManager.load();
typeManager.reload();
templateManager.preloadTemplates();
@ -187,6 +182,7 @@ public class MMOItems extends JavaPlugin {
dropTableManager = new DropTableManager();
worldGenManager = new WorldGenManager();
blockManager = new BlockManager();
MMOItems.plugin.getStats().reload(false);
if (Bukkit.getPluginManager().getPlugin("Vault") != null) vaultSupport = new VaultSupport();

View File

@ -64,6 +64,7 @@ public class ReloadCommandTreeNode extends CommandTreeNode {
MMOItems.plugin.getLayouts().reload();
MMOItems.plugin.getFormats().reload();
MMOItems.plugin.getTemplates().reload();
MMOItems.plugin.getStats().reload(true);
sender.sendMessage(MMOItems.plugin.getPrefix() + MMOItems.plugin.getName() + " "
+ MMOItems.plugin.getDescription().getVersion() + " reloaded.");
sender.sendMessage(MMOItems.plugin.getPrefix() + "- " + ChatColor.RED

View File

@ -1,9 +1,12 @@
package net.Indyuce.mmoitems.manager;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.element.Element;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.stat.type.*;
import net.Indyuce.mmoitems.util.ElementStatType;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
@ -27,16 +30,37 @@ public class StatManager {
* Load default stats using java reflection, get all public static final
* fields in the ItemStat and register them as stat instances
*/
public StatManager() {
public void load() {
for (Field field : ItemStats.class.getFields())
try {
if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()) && field.get(null) instanceof ItemStat)
register((ItemStat) field.get(null));
} catch (IllegalArgumentException | IllegalAccessException exception) {
MMOItems.plugin.getLogger().log(Level.WARNING, "Couldn't register stat called " + field.getName());
MMOItems.plugin.getLogger().log(Level.WARNING, "Couldn't register stat called '" + field.getName() + "': " + exception.getMessage());
}
}
/**
* @see FictiveNumericStat
* @deprecated
*/
@Deprecated
public void reload(boolean cleanFirst) {
// Clean fictive numeric stats before
if (cleanFirst)
for (Iterator<DoubleStat> ite = numeric.iterator(); ite.hasNext(); ) {
DoubleStat stat = ite.next();
if (stat instanceof FictiveNumericStat)
ite.remove();
}
// Register elemental stats
for (ElementStatType type : ElementStatType.values())
for (Element element : MythicLib.plugin.getElements().getAll())
numeric.add(new FictiveNumericStat(element, type));
}
public Collection<ItemStat> getAll() {
return stats.values();
}

View File

@ -132,7 +132,7 @@ public class Elements extends ItemStat<RandomElementListData, ElementListData> i
// Create Array
ArrayList<ItemTag> ret = new ArrayList<>();
for (Pair<Element, ElementStatType> pair : data.getKeys())
ret.add(new ItemTag("MMOITEMS_" + pair, data.getStat(pair.getKey(), pair.getValue())));
ret.add(new ItemTag("MMOITEMS_" + pair.getValue().getConcatenatedTagPath(pair.getKey()), data.getStat(pair.getKey(), pair.getValue())));
// Thats it
return ret;
@ -145,7 +145,7 @@ public class Elements extends ItemStat<RandomElementListData, ElementListData> i
ArrayList<ItemTag> relevantTags = new ArrayList<>();
for (Element element : Element.values())
for (ElementStatType statType : ElementStatType.values()) {
final String path = "MMOITEMS_" + element.getName() + "_" + statType.name();
final String path = "MMOITEMS_" + statType.getConcatenatedTagPath(element);
if (mmoitem.getNBT().hasTag(path))
relevantTags.add(ItemTag.getTagAtPath(path, mmoitem.getNBT(), SupportedNBTTagValues.DOUBLE));
}
@ -165,7 +165,6 @@ public class Elements extends ItemStat<RandomElementListData, ElementListData> i
// Create new
ElementListData elements = new ElementListData();
boolean success = false;
// Try to find every existing element
for (Element element : Element.values())
@ -176,10 +175,7 @@ public class Elements extends ItemStat<RandomElementListData, ElementListData> i
elements.setStat(element, statType, (double) tag.getValue());
}
if (success) {
return elements;
}
return null;
return elements.isEmpty() ? null : elements;
}
@Override

View File

@ -69,9 +69,8 @@ public class ShieldPatternStat extends ItemStat<ShieldPatternData, ShieldPattern
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull ShieldPatternData data) {
BlockStateMeta meta = (BlockStateMeta) item.getMeta();
Banner banner = (Banner) meta.getBlockState();
ShieldPatternData pattern = (ShieldPatternData) data;
banner.setBaseColor(pattern.getBaseColor());
banner.setPatterns(pattern.getPatterns());
banner.setBaseColor(data.getBaseColor());
banner.setPatterns(data.getPatterns());
((BlockStateMeta) item.getMeta()).setBlockState(banner);
item.getMeta().addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
}

View File

@ -0,0 +1,71 @@
package net.Indyuce.mmoitems.stat.type;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.element.Element;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.util.ElementStatType;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* Used for elements otherwise MMOItems doesn't
* register elemental stats inside of MythicLib
* <p>
* Workaround that will be removed when adding stat categories
* in order to give more clarity to the item editor.
*
* @deprecated Definitely not a perfect implementation
*/
@Deprecated
public class FictiveNumericStat extends DoubleStat implements InternalStat {
public FictiveNumericStat(Element el, ElementStatType type) {
super(type.getConcatenatedTagPath(el), Material.BARRIER, "Fictive Stat", new String[0]);
}
@Override
public NumericStatFormula whenInitialized(Object object) {
throw new RuntimeException("Fictive item stat");
}
@Override
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull DoubleData data) {
throw new RuntimeException("Fictive item stat");
}
@NotNull
@Override
public ArrayList<ItemTag> getAppliedNBT(@NotNull DoubleData data) {
throw new RuntimeException("Fictive item stat");
}
@Override
public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) {
throw new RuntimeException("Fictive item stat");
}
@Override
public void whenLoaded(@NotNull ReadMMOItem mmoitem) {
throw new RuntimeException("Fictive item stat");
}
@Nullable
@Override
public DoubleData getLoadedNBT(@NotNull ArrayList<ItemTag> storedTags) {
throw new RuntimeException("Fictive item stat");
}
@Override
public void whenDisplayed(List<String> lore, Optional<NumericStatFormula> statData) {
throw new RuntimeException("Fictive item stat");
}
}