Item Updater has been updated and should now function correctly. Be warned this build is moslty untested and you could encounter some issue. Please report them on our gitlab page. MMOLib 1.3.4+ Required.

This commit is contained in:
Ethan 2020-09-15 21:14:00 -04:00
parent 7781ca0033
commit 25f738f80c
6 changed files with 179 additions and 166 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.Indyuce</groupId>
<artifactId>MMOItems</artifactId>
<version>6.1.2</version>
<version>6.1.3</version>
<name>MMOItems</name>
<description>A great item solution for your RPG server.</description>

View File

@ -1,21 +1,19 @@
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.manager.UpdaterManager.KeepOption;
import net.mmogroup.mmolib.api.item.NBTItem;
public class UpdaterData {
// TODO change this to MMOItemTemplate
private final Type type;
private final String id;
/*
* two UUIDs can be found : one on the itemStack in the nbttags, and one in
@ -23,27 +21,28 @@ public class UpdaterData {
* they don't match, the item needs to be updated. UUID not final because it
* must be changed
*/
private final MMOItemTemplate template;
private UUID uuid;
private final Set<KeepOption> options = new HashSet<>();
public UpdaterData(Type type, String id, ConfigurationSection config) {
this(type, id, UUID.fromString(config.getString("uuid")));
public UpdaterData(MMOItemTemplate template, ConfigurationSection config) {
this(template, UUID.fromString(config.getString("uuid")));
for (KeepOption option : KeepOption.values())
if (config.getBoolean(option.getPath()))
options.add(option);
}
public UpdaterData(Type type, String id, UUID uuid, KeepOption... options) {
public UpdaterData(MMOItemTemplate template, UUID uuid, KeepOption... options) {
this.template = template;
this.uuid = uuid;
this.type = type;
this.id = id;
this.options.addAll(Arrays.asList(options));
}
public UpdaterData(Type type, String id, UUID uuid, boolean enableAllOptions) {
this(type, id, uuid);
public UpdaterData(MMOItemTemplate template, UUID uuid, boolean enableAllOptions) {
this(template, uuid);
if (enableAllOptions)
options.addAll(Arrays.asList(KeepOption.values()));
@ -57,15 +56,15 @@ public class UpdaterData {
}
public String getPath() {
return type.getId() + "." + id;
return template.getType().getId() + "." + template.getId();
}
public Type getType() {
return type;
return template.getType();
}
public String getId() {
return id;
return template.getId();
}
public UUID getUniqueId() {

View File

@ -1,19 +1,18 @@
package net.Indyuce.mmoitems.command;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.util.message.Message;
import net.Indyuce.mmoitems.gui.edition.ItemUpdaterEdition;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.item.NBTItem;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class UpdateItemCommand implements CommandExecutor {
@Override
@ -36,7 +35,7 @@ public class UpdateItemCommand implements CommandExecutor {
return true;
}
ItemStack newItem = MMOItems.plugin.getUpdater().getUpdated(item.getItem());
ItemStack newItem = MMOItems.plugin.getUpdater().getUpdated(item.getItem(), player);
if (newItem == null || newItem.getType() == Material.AIR) {
sender.sendMessage(ChatColor.RED + "Could not update your item.");
return true;

View File

@ -1,16 +1,5 @@
package net.Indyuce.mmoitems.gui.edition;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.UpdaterData;
@ -18,6 +7,16 @@ import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.manager.UpdaterManager.KeepOption;
import net.mmogroup.mmolib.api.util.AltChar;
import net.mmogroup.mmolib.version.VersionMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.List;
public class ItemUpdaterEdition extends EditionInventory {
private static final int[] slots = { 19, 20, 21, 28, 29, 30, 37, 38, 39 };
@ -38,22 +37,33 @@ public class ItemUpdaterEdition extends EditionInventory {
UpdaterData did = MMOItems.plugin.getUpdater().getData(template);
ItemStack disable = VersionMaterial.RED_STAINED_GLASS_PANE.toItem();
ItemStack disable = VersionMaterial.RED_DYE.toItem();
ItemMeta disableMeta = disable.getItemMeta();
disableMeta.setDisplayName(ChatColor.GREEN + "Disable");
disableMeta.setDisplayName(ChatColor.RED + "Disable");
List<String> disableLore = new ArrayList<String>();
disableLore.add(ChatColor.GRAY + "Your item will not be dynamically updated.");
disableLore.add(ChatColor.RED + "Your item will not be dynamically updated.");
disableLore.add("");
disableLore.add(ChatColor.YELLOW + AltChar.listDash + " Click to disable the item updater.");
disableMeta.setLore(disableLore);
disable.setItemMeta(disableMeta);
ItemStack enable = VersionMaterial.LIME_DYE.toItem();
ItemMeta enableMeta = enable.getItemMeta();
enableMeta.setDisplayName(ChatColor.GREEN + "Enable");
List<String> enableLore = new ArrayList<String>();
enableLore.add(ChatColor.GREEN + "Your item will be dynamically updated.");
enableLore.add("");
enableLore.add(ChatColor.YELLOW + AltChar.listDash + " Click to enable the item updater.");
enableMeta.setLore(enableLore);
enable.setItemMeta(enableMeta);
int n = 0;
for (KeepOption option : KeepOption.values())
inv.setItem(slots[n++],
getBooleanItem(MMOUtils.caseOnWords(option.name().substring(5).toLowerCase()), did.hasOption(option), option.getLore()));
inv.setItem(32, disable);
inv.setItem(23, enable);
inv.setItem(4, getCachedItem());
return inv;
@ -87,13 +97,19 @@ public class ItemUpdaterEdition extends EditionInventory {
return;
}
if (item.getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Disable")) {
if (item.getItemMeta().getDisplayName().equals(ChatColor.RED + "Disable")) {
MMOItems.plugin.getUpdater().disable(template);
player.closeInventory();
player.sendMessage(ChatColor.YELLOW + "Successfully disabled the item updater for '" + template.getId() + "'.");
return;
}
if (item.getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Enable")) {
player.closeInventory();
player.sendMessage(ChatColor.YELLOW + "Successfully enabled the item updater for '" + template.getId() + "'.");
return;
}
/*
* find clicked option based on item display name. no need to use
* NBTTags

View File

@ -129,7 +129,7 @@ public class PluginUpdateManager {
new String[] { "Enables the item updater for every item.", "&cNot recommended unless you know what you are doing." }, sender -> {
for (Type type : MMOItems.plugin.getTypes().getAll())
for (String id : type.getConfigFile().getConfig().getKeys(false))
MMOItems.plugin.getUpdater().enable(new UpdaterData(type, id, UUID.randomUUID(), true));
MMOItems.plugin.getUpdater().enable(new UpdaterData(MMOItems.plugin.getTemplates().getTemplate(type, id), UUID.randomUUID(), true));
}));
register(new PluginUpdate(4,

View File

@ -1,11 +1,18 @@
package net.Indyuce.mmoitems.manager;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.UpdaterData;
import net.Indyuce.mmoitems.api.item.ItemReference;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.mmoitem.VolatileMMOItem;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.api.util.TemplateMap;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.item.NBTItem;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
@ -15,15 +22,14 @@ import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.UpdaterData;
import net.Indyuce.mmoitems.api.item.ItemReference;
import net.Indyuce.mmoitems.api.util.TemplateMap;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.item.NBTItem;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
public class UpdaterManager implements Listener {
private final TemplateMap<UpdaterData> data = new TemplateMap<>();
@ -34,10 +40,10 @@ public class UpdaterManager implements Listener {
try {
Type type = MMOItems.plugin.getTypes().getOrThrow(typeFormat);
for (String id : config.getConfigurationSection(typeFormat).getKeys(false))
enable(new UpdaterData(type, id, config.getConfigurationSection(typeFormat + "." + id)));
enable(new UpdaterData(MMOItems.plugin.getTemplates().getTemplate(type, id), config.getConfigurationSection(typeFormat + "." + id)));
} catch (IllegalArgumentException exception) {
MMOItems.plugin.getLogger().log(Level.WARNING,
"An issue occured while trying to load dynamic updater data: " + exception.getMessage());
"An issue occurred while trying to load dynamic updater data: " + exception.getMessage());
}
}
@ -54,11 +60,12 @@ public class UpdaterManager implements Listener {
}
public void enable(ItemReference template) {
this.data.setValue(template.getType(), template.getId(), new UpdaterData(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) {
this.data.setValue(type, id, new UpdaterData(type, id, UUID.randomUUID()));
this.data.setValue(type, id, new UpdaterData(MMOItems.plugin.getTemplates().getTemplate(type, id), UUID.randomUUID()));
}
public void enable(UpdaterData data) {
@ -86,7 +93,7 @@ public class UpdaterManager implements Listener {
if (item == null || item.getType() == Material.AIR)
return;
ItemStack newItem = getUpdated(item);
ItemStack newItem = getUpdated(item, (Player) event.getWhoClicked());
if (!newItem.equals(item))
event.setCurrentItem(newItem);
}
@ -98,119 +105,112 @@ public class UpdaterManager implements Listener {
public void updateOnJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
player.getEquipment().setHelmet(getUpdated(player.getEquipment().getHelmet()));
player.getEquipment().setChestplate(getUpdated(player.getEquipment().getChestplate()));
player.getEquipment().setLeggings(getUpdated(player.getEquipment().getLeggings()));
player.getEquipment().setBoots(getUpdated(player.getEquipment().getBoots()));
player.getEquipment().setHelmet(getUpdated(player.getEquipment().getHelmet(), player));
player.getEquipment().setChestplate(getUpdated(player.getEquipment().getChestplate(), player));
player.getEquipment().setLeggings(getUpdated(player.getEquipment().getLeggings(), player));
player.getEquipment().setBoots(getUpdated(player.getEquipment().getBoots(), player));
for (int j = 0; j < 9; j++)
player.getInventory().setItem(j, getUpdated(player.getInventory().getItem(j)));
player.getEquipment().setItemInOffHand(getUpdated(player.getEquipment().getItemInOffHand()));
player.getInventory().setItem(j, getUpdated(player.getInventory().getItem(j), player));
player.getEquipment().setItemInOffHand(getUpdated(player.getEquipment().getItemInOffHand(), player));
}
public ItemStack getUpdated(ItemStack item) {
return getUpdated(MMOLib.plugin.getVersion().getWrapper().getNBTItem(item));
public ItemStack getUpdated(ItemStack item, Player target) {
if (item == null) {
return null;
}
if (item.getType() == Material.AIR)
return item;
return getUpdated(MMOLib.plugin.getVersion().getWrapper().getNBTItem(item), target);
}
public ItemStack getUpdated(NBTItem item) {
public ItemStack getUpdated(NBTItem item, Player target) {
/*
* If the item type is null, then it is not an mmoitem and it does not
* need to be updated
*/
Type type = item.getType();
if (type == null)
* If the item type is null, then it is not an mmoitem and it does not
* need to be updated
*/
if (item.getType() == null)
return item.getItem();
return item.getItem();
if (!data.hasValue(item.getType(), item.getString("MMOITEMS_ITEM_ID")))
return item.getItem();
// String id = item.getString("MMOITEMS_ITEM_ID");
// String path = type.getId() + "." + id;
// if (!data.containsKey(path))
// 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.
// */
// UpdaterData did = data.get(path);
// if (did.matches(item))
// return item.getItem();
//
// // (funny bug fix) CLONE THE MMOITEM
// MMOItem newItemMMO = MMOItems.plugin.getItems().getMMOItem(type,
// id).clone();
//
// /*
// * apply older gem stones, using a light MMOItem so the item does not
// * calculate every stat data from the older item.
// */
// MMOItem itemMMO = new VolatileMMOItem(item);
// if (did.hasOption(KeepOption.KEEP_GEMS) &&
// itemMMO.hasData(ItemStat.GEM_SOCKETS))
// newItemMMO.setData(ItemStat.GEM_SOCKETS,
// itemMMO.getData(ItemStat.GEM_SOCKETS));
//
// if (did.hasOption(KeepOption.KEEP_SOULBOUND) &&
// itemMMO.hasData(ItemStat.SOULBOUND))
// newItemMMO.setData(ItemStat.SOULBOUND,
// itemMMO.getData(ItemStat.SOULBOUND));
//
// // if (did.hasOption(KeepOption.KEEP_SKIN) && itemMMO.hasData(stat))
//
// // apply amount
// ItemStack newItem = newItemMMO.newBuilder().build();
// newItem.setAmount(item.getItem().getAmount());
//
// ItemMeta newItemMeta = newItem.getItemMeta();
// List<String> lore = newItemMeta.getLore();
//
// /*
// * 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
// * removed!
// */
// if (did.hasOption(KeepOption.KEEP_ENCHANTS))
// item.getItem().getItemMeta().getEnchants().forEach((enchant, level)
// -> newItemMeta.addEnchant(enchant, level, true));
//
// /*
// * keepLore is used to save enchants from custom enchants plugins that
// * only use lore to save enchant data
// */
// if (did.hasOption(KeepOption.KEEP_LORE)) {
// int n = 0;
// for (String s : item.getItem().getItemMeta().getLore()) {
// if (!s.startsWith(ChatColor.GRAY + ""))
// break;
// lore.add(n++, s);
// }
// }
//
// /*
// * keep durability can be used for tools to save their durability so
// * users do not get extra durability when the item is updated
// */
// VersionWrapper wrapper = MMOLib.plugin.getVersion().getWrapper();
// if (did.hasOption(KeepOption.KEEP_DURABILITY) &&
// wrapper.isDamageable(item.getItem()) &&
// wrapper.isDamageable(newItem))
// wrapper.applyDurability(newItem, newItemMeta,
// wrapper.getDurability(item.getItem(), item.getItem().getItemMeta()));
//
// /*
// * 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())
// newItemMeta.setDisplayName(item.getItem().getItemMeta().getDisplayName());
//
// newItemMeta.setLore(lore);
// newItem.setItemMeta(newItemMeta);
// return newItem;
/*
* 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.
*/
UpdaterData did = data.getValue(item.getType(), item.getString("MMOITEMS_ITEM_ID"));
if (did.matches(item))
return item.getItem();
MMOItemTemplate template = MMOItems.plugin.getTemplates().getTemplate(item.getType(), item.getString("MMOITEMS_ITEM_ID"));
MMOItem newMMOItem = template.newBuilder(PlayerData.get(target).getRPG()).build();
/*
* apply older gem stones, using a light MMOItem so the item does not
* calculate every stat data from the older item.
*/
MMOItem volatileItem = new VolatileMMOItem(item);
if (did.hasOption(KeepOption.KEEP_GEMS) && volatileItem.hasData(ItemStat.GEM_SOCKETS))
newMMOItem.replaceData(ItemStat.GEM_SOCKETS, volatileItem.getData(ItemStat.GEM_SOCKETS));
if (did.hasOption(KeepOption.KEEP_SOULBOUND) && volatileItem.hasData(ItemStat.SOULBOUND))
newMMOItem.replaceData(ItemStat.SOULBOUND, volatileItem.getData(ItemStat.SOULBOUND));
// if (did.hasOption(KeepOption.KEEP_SKIN) && itemMMO.hasData(stat))
// apply amount
ItemStack newItem = newMMOItem.newBuilder().build();
newItem.setAmount(item.getItem().getAmount());
ItemMeta newItemMeta = newItem.getItemMeta();
List<String> lore = newItemMeta.getLore();
/*
* 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
* removed!
*/
if (did.hasOption(KeepOption.KEEP_ENCHANTS))
item.getItem().getItemMeta().getEnchants().forEach((enchant, level) -> newItemMeta.addEnchant(enchant, level, true));
/*
* keepLore is used to save enchants from custom enchants plugins that
* only use lore to save enchant data
*/
if (did.hasOption(KeepOption.KEEP_LORE)) {
int n = 0;
for (String s : item.getItem().getItemMeta().getLore()) {
if (!s.startsWith(ChatColor.GRAY + ""))
break;
lore.add(n++, s);
}
}
/*
* 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) {
((Damageable) newItemMeta).setDamage(((Damageable) item.getItem().getItemMeta()).getDamage());
}
/*
* 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())
newItemMeta.setDisplayName(item.getItem().getItemMeta().getDisplayName());
newItemMeta.setLore(lore);
newItem.setItemMeta(newItemMeta);
return newItem;
}
public enum KeepOption {
@ -222,7 +222,6 @@ public class UpdaterManager implements Listener {
KEEP_GEMS("The item keeps its empty gem", "sockets and applied gems."),
KEEP_SOULBOUND("The item keeps its soulbound data."),
// KEEP_SKIN("Keep the item applied skins."),
;
private final List<String> lore;