!Templates are now loaded when MI loads for extra compatibility

This commit is contained in:
Indyuce 2020-09-20 12:51:15 +02:00
parent 1dba8b2241
commit 239e3c9eff
6 changed files with 187 additions and 116 deletions

View File

@ -1,5 +1,21 @@
package net.Indyuce.mmoitems;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.annotation.Nullable;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.ItemTier;
import net.Indyuce.mmoitems.api.SoulboundInfo;
@ -9,13 +25,21 @@ import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.command.MMOItemsCommandTreeRoot;
import net.Indyuce.mmoitems.command.UpdateItemCommand;
import net.Indyuce.mmoitems.command.completion.UpdateItemCompletion;
import net.Indyuce.mmoitems.comp.*;
import net.Indyuce.mmoitems.comp.AdvancedEnchantmentsHook;
import net.Indyuce.mmoitems.comp.MMOItemsMetrics;
import net.Indyuce.mmoitems.comp.MMOItemsRewardTypes;
import net.Indyuce.mmoitems.comp.RealDualWieldHook;
import net.Indyuce.mmoitems.comp.WorldEditSupport;
import net.Indyuce.mmoitems.comp.eco.VaultSupport;
import net.Indyuce.mmoitems.comp.flags.DefaultFlags;
import net.Indyuce.mmoitems.comp.flags.FlagPlugin;
import net.Indyuce.mmoitems.comp.flags.ResidenceFlags;
import net.Indyuce.mmoitems.comp.flags.WorldGuardFlags;
import net.Indyuce.mmoitems.comp.holograms.*;
import net.Indyuce.mmoitems.comp.holograms.CMIPlugin;
import net.Indyuce.mmoitems.comp.holograms.HologramSupport;
import net.Indyuce.mmoitems.comp.holograms.HologramsPlugin;
import net.Indyuce.mmoitems.comp.holograms.HolographicDisplaysPlugin;
import net.Indyuce.mmoitems.comp.holograms.TrHologramPlugin;
import net.Indyuce.mmoitems.comp.inventory.DefaultPlayerInventory;
import net.Indyuce.mmoitems.comp.inventory.OrnamentPlayerInventory;
import net.Indyuce.mmoitems.comp.inventory.PlayerInventory;
@ -34,24 +58,34 @@ import net.Indyuce.mmoitems.comp.rpg.DefaultHook;
import net.Indyuce.mmoitems.comp.rpg.RPGHandler;
import net.Indyuce.mmoitems.gui.PluginInventory;
import net.Indyuce.mmoitems.gui.listener.GuiListener;
import net.Indyuce.mmoitems.listener.*;
import net.Indyuce.mmoitems.manager.*;
import net.Indyuce.mmoitems.listener.CraftingListener;
import net.Indyuce.mmoitems.listener.CustomBlockListener;
import net.Indyuce.mmoitems.listener.CustomSoundListener;
import net.Indyuce.mmoitems.listener.DisableInteractions;
import net.Indyuce.mmoitems.listener.DurabilityListener;
import net.Indyuce.mmoitems.listener.ElementListener;
import net.Indyuce.mmoitems.listener.ItemUse;
import net.Indyuce.mmoitems.listener.PlayerListener;
import net.Indyuce.mmoitems.manager.AbilityManager;
import net.Indyuce.mmoitems.manager.BlockManager;
import net.Indyuce.mmoitems.manager.ConfigManager;
import net.Indyuce.mmoitems.manager.CraftingManager;
import net.Indyuce.mmoitems.manager.DropTableManager;
import net.Indyuce.mmoitems.manager.EntityManager;
import net.Indyuce.mmoitems.manager.ItemManager;
import net.Indyuce.mmoitems.manager.LayoutManager;
import net.Indyuce.mmoitems.manager.PluginUpdateManager;
import net.Indyuce.mmoitems.manager.RecipeManager;
import net.Indyuce.mmoitems.manager.SetManager;
import net.Indyuce.mmoitems.manager.StatManager;
import net.Indyuce.mmoitems.manager.TemplateManager;
import net.Indyuce.mmoitems.manager.TierManager;
import net.Indyuce.mmoitems.manager.TypeManager;
import net.Indyuce.mmoitems.manager.UpdaterManager;
import net.Indyuce.mmoitems.manager.UpgradeManager;
import net.Indyuce.mmoitems.manager.WorldGenManager;
import net.mmogroup.mmolib.api.player.MMOPlayerData;
import net.mmogroup.mmolib.version.SpigotPlugin;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import javax.annotation.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
public class MMOItems extends JavaPlugin {
public static MMOItems plugin;
@ -102,17 +136,17 @@ public class MMOItems extends JavaPlugin {
getLogger().log(Level.WARNING, "Could not initialize support with WorldEdit 7: " + exception.getMessage());
}
/*
* stat manager must be initialized before MMOCore compatibility
* initializes so that MMOCore can register its stats
*/
saveDefaultConfig();
/*
* Stat manager must be initialized before MMOCore compatibility
* initializes so that MMOCore and other plugins can register their
* stats. After types are loaded, templates are loaded so that other
* plugins when enabling can load MI template references
*/
statManager = new StatManager();
typeManager.reload();
templateManager.loadCompatibility(); // explained why in method
templateManager.preloadTemplates();
if (Bukkit.getPluginManager().getPlugin("MMOCore") != null)
new MMOCoreMMOLoader();
@ -137,14 +171,17 @@ public class MMOItems extends JavaPlugin {
getLogger().log(Level.INFO, "Hooked onto MMOInventory");
}
findRpgPlugin();
templateManager.reload();
/*
* After tiers, sets and upgrade templates are loaded, MI template data
* CAN be fully loaded
*/
tierManager = new TierManager();
setManager = new SetManager();
upgradeManager = new UpgradeManager();
templateManager.postloadTemplates();
dropTableManager = new DropTableManager();
dynamicUpdater = new UpdaterManager();
worldGenManager = new WorldGenManager();

View File

@ -1,31 +1,31 @@
package net.Indyuce.mmoitems.api;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.manager.UpdaterManager.KeepOption;
import net.mmogroup.mmolib.api.item.NBTItem;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.manager.UpdaterManager.KeepOption;
import net.mmogroup.mmolib.api.item.NBTItem;
public class UpdaterData {
// TODO change this to MMOItemTemplate
/*
* two UUIDs can be found : one on the itemStack in the nbttags, and one in
* the UpdaterData instance. if the two match, the item is up to date. if
* they don't match, the item needs to be updated. UUID not final because it
* must be changed
/**
* The item reference
*/
private final Type type;
private final String id;
/**
* Two UUIDs can be found: one on the itemStack in the NBTTags, and one in
* the UpdaterData instance. If the UUIDs match, the item is up to date. If
* they don't match, the item needs to be updated. UUID not final because it
* is updated everytime the item is edited using the editor GUI
*/
private UUID uuid;
private final Set<KeepOption> options = new HashSet<>();

View File

@ -47,7 +47,7 @@ public class StatEdition implements Edition {
inv.getPlayer().sendMessage(ChatColor.YELLOW + "" + ChatColor.STRIKETHROUGH + "-----------------------------------------------------");
for (String line : message)
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + line);
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + ChatColor.translateAlternateColorCodes('&', line));
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Type 'cancel' to abort editing.");
/*

View File

@ -8,6 +8,8 @@ import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.mmogroup.mmolib.api.util.PostLoadObject;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
@ -15,7 +17,7 @@ import javax.annotation.Nullable;
import java.util.*;
import java.util.logging.Level;
public class MMOItemTemplate implements ItemReference {
public class MMOItemTemplate extends PostLoadObject implements ItemReference {
private final Type type;
private final String id;
@ -36,6 +38,8 @@ public class MMOItemTemplate implements ItemReference {
* different item types share the same ID
*/
public MMOItemTemplate(Type type, String id) {
super(null);
this.type = type;
this.id = id;
}
@ -49,11 +53,15 @@ public class MMOItemTemplate implements ItemReference {
* The config file read to load the template
*/
public MMOItemTemplate(Type type, ConfigurationSection config) {
super(config);
Validate.notNull(config, "Could not load template config");
this.type = type;
this.id = config.getName().toUpperCase().replace("-", "_").replace(" ", "_");
}
@Override
protected void whenPostLoaded(ConfigurationSection config) {
if (config.contains("option"))
for (TemplateOption option : TemplateOption.values())
if (config.getBoolean("option." + option.name().toLowerCase().replace("_", "-")))

View File

@ -184,6 +184,57 @@ public class TemplateManager {
return (int) found;
}
/**
* Templates must be loaded whenever MMOItems enables so that other plugins
* like MMOCore can load template references in drop items or other objects.
* Template data is only loaded when MMOItems enables, once sets, tiers..
* are initialized
*/
public void preloadTemplates() {
for (Type type : MMOItems.plugin.getTypes().getAll()) {
FileConfiguration config = type.getConfigFile().getConfig();
for (String key : config.getKeys(false))
try {
registerTemplate(new MMOItemTemplate(type, config.getConfigurationSection(key)));
} catch (IllegalArgumentException exception) {
MMOItems.plugin.getLogger().log(Level.INFO, "Could not preload item template '" + key + "': " + exception.getMessage());
}
}
}
/**
* Loads item generator modifiers and post load item templates.
*/
public void postloadTemplates() {
MMOItems.plugin.getLogger().log(Level.INFO, "Loading template modifiers, please wait..");
for (File file : new File(MMOItems.plugin.getDataFolder() + "/modifiers").listFiles()) {
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
for (String key : config.getKeys(false))
try {
TemplateModifier modifier = new TemplateModifier(config.getConfigurationSection(key));
modifiers.put(modifier.getId(), modifier);
} catch (IllegalArgumentException exception) {
MMOItems.plugin.getLogger().log(Level.INFO, "Could not load template modifier '" + key + "': " + exception.getMessage());
}
}
MMOItems.plugin.getLogger().log(Level.INFO, "Loading item templates, please wait..");
templates.forEach(template -> {
try {
template.postLoad();
} catch (IllegalArgumentException exception) {
MMOItems.plugin.getLogger().log(Level.INFO, "Could not load item template '" + template.getId() + "': " + exception.getMessage());
}
});
}
/**
* Reloads the item templates. This is the method used to reload the manager
* when the server is already running. It clears all the maps and loads
* everything again. Template references in other plugins like MMOCore must
* be refreshed afterwards.
*/
public void reload() {
templates.clear();
modifiers.clear();
@ -211,21 +262,5 @@ public class TemplateManager {
}
}
}
// this loads dummy items for on load so
// plugins that enable before mmoitems that use
// items (mmocore) don't error out and need
// a reload
public void loadCompatibility() {
templates.clear();
for (Type type : MMOItems.plugin.getTypes().getAll()) {
FileConfiguration config = type.getConfigFile().getConfig();
for (String key : config.getKeys(false))
try {
registerTemplate(new MMOItemTemplate(type, key));
} catch (IllegalArgumentException ignored) {
}
}
}
}

View File

@ -62,8 +62,8 @@ public class UpdaterManager implements Listener {
}
public void enable(ItemReference template) {
this.data.setValue(template.getType(), template.getId(), new UpdaterData(MMOItems.plugin.getTemplates()
.getTemplate(template.getType(), template.getId()), UUID.randomUUID()));
this.data.setValue(template.getType(), template.getId(),
new UpdaterData(MMOItems.plugin.getTemplates().getTemplate(template.getType(), template.getId()), UUID.randomUUID()));
}
public void enable(Type type, String id) {
@ -129,13 +129,9 @@ public class UpdaterManager implements Listener {
*/
Type type = item.getType();
if (type == null)
if (type == null || !data.hasValue(type, item.getString("MMOITEMS_ITEM_ID")))
return item.getItem();
if (!data.hasValue(type, item.getString("MMOITEMS_ITEM_ID")))
return item.getItem();
/*
* check the internal UUID of the item, if it does not make the one
* stored in the item updater data then the item is outdated.
@ -169,8 +165,7 @@ public class UpdaterManager implements Listener {
/*
* add old enchants to the item. warning - if enabled the item will
* remember of ANY enchant on the old item, even the enchants that
were
* remember of ANY enchant on the old item, even the enchants that were
* removed!
*/
if (did.hasOption(KeepOption.KEEP_ENCHANTS))
@ -193,15 +188,11 @@ public class UpdaterManager implements Listener {
* keep durability can be used for tools to save their durability so
* users do not get extra durability when the item is updated
*/
;
if (did.hasOption(KeepOption.KEEP_DURABILITY) && item.getItem().getItemMeta() instanceof Damageable && newItemMeta instanceof Damageable) {
if (did.hasOption(KeepOption.KEEP_DURABILITY) && item.getItem().getItemMeta() instanceof Damageable && newItemMeta instanceof Damageable)
((Damageable) newItemMeta).setDamage(((Damageable) item.getItem().getItemMeta()).getDamage());
}
/*
* keep name so players who renamed the item in the anvil does not
have
* keep name so players who renamed the item in the anvil does not have
* to rename it again
*/
if (did.hasOption(KeepOption.KEEP_NAME) && item.getItem().getItemMeta().hasDisplayName())