fixed lore updates

This commit is contained in:
Indyuce 2021-12-20 01:02:53 +01:00
parent 164fd9c5c8
commit f2bb73e2b7
7 changed files with 12 additions and 319 deletions

View File

@ -140,8 +140,8 @@
<dependency> <dependency>
<groupId>io.lumine</groupId> <groupId>io.lumine</groupId>
<artifactId>MythicLib</artifactId> <artifactId>MythicLib-dist</artifactId>
<version>1.1.6</version> <version>1.2</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>

View File

@ -1,104 +0,0 @@
package net.Indyuce.mmoitems.api;
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 io.lumine.mythic.lib.api.item.NBTItem;
public class UpdaterData {
/**
* 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<>();
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(MMOItemTemplate template, UUID uuid, KeepOption... options) {
this.id = template.getId();
this.type = template.getType();
this.uuid = uuid;
this.options.addAll(Arrays.asList(options));
}
public UpdaterData(MMOItemTemplate template, UUID uuid, boolean enableAllOptions) {
this(template, uuid);
if (enableAllOptions)
options.addAll(Arrays.asList(KeepOption.values()));
}
public void save(ConfigurationSection config) {
for (KeepOption option : KeepOption.values())
if (options.contains(option))
config.set(option.getPath(), true);
config.set("uuid", uuid.toString());
}
public String getPath() {
return type.getId() + "." + id;
}
public Type getType() {
return type;
}
public String getId() {
return id;
}
public UUID getUniqueId() {
return uuid;
}
/*
* used everytime a change is applied to one item. the database uuid is
* randomized so that any item with different UUIDs have to be dynamically
* updated
*/
public void setUniqueId(UUID uuid) {
Validate.notNull(uuid, "UUID cannot be null");
this.uuid = uuid;
}
public boolean matches(NBTItem item) {
return uuid.toString().equals(item.getString("MMOITEMS_ITEM_UUID"));
}
public boolean hasOption(KeepOption option) {
return options.contains(option);
}
public void addOption(KeepOption option) {
options.add(option);
}
public void removeOption(KeepOption option) {
options.remove(option);
}
}

View File

@ -16,6 +16,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -50,18 +51,10 @@ public class Consumable extends UseItem {
return false; return false;
} }
/**
* @deprecated Use {@link Consumable#useOnPlayer()}
*/
@Deprecated
public boolean useWithoutItem() {
return useOnPlayer() == ConsumableConsumeResult.CONSUME;
}
/** /**
* @return If the item should be consumed * @return If the item should be consumed
*/ */
public ConsumableConsumeResult useOnPlayer() { public ConsumableConsumeResult useOnPlayer(EquipmentSlot handUsed) {
NBTItem nbtItem = getNBTItem(); NBTItem nbtItem = getNBTItem();
// Inedible stat cancels this operation from the beginning // Inedible stat cancels this operation from the beginning
@ -104,24 +97,15 @@ public class Consumable extends UseItem {
ItemStack oldItem = nbtItem.getItem(); ItemStack oldItem = nbtItem.getItem();
if (oldItem.getAmount() > 1) { if (oldItem.getAmount() > 1) {
newItem.setAmount(1); newItem.setAmount(1);
player.getInventory().setItem(handUsed, newItem);
if (player.getInventory().getItemInMainHand().equals(oldItem))
player.getInventory().setItemInMainHand(newItem);
else if (player.getInventory().getItemInOffHand().equals(oldItem))
player.getInventory().setItemInOffHand(newItem);
oldItem.setAmount(oldItem.getAmount() - 1); oldItem.setAmount(oldItem.getAmount() - 1);
new SmartGive(player).give(oldItem); new SmartGive(player).give(oldItem);
/** /**
* Player just holding one item * Player just holding one item
*/ */
} else { } else
if (player.getInventory().getItemInMainHand().equals(oldItem)) player.getInventory().setItem(handUsed, newItem);
player.getInventory().setItemInMainHand(newItem);
else if (player.getInventory().getItemInOffHand().equals(oldItem))
player.getInventory().setItemInOffHand(newItem);
}
return ConsumableConsumeResult.NOT_CONSUME; return ConsumableConsumeResult.NOT_CONSUME;
} }

View File

@ -254,7 +254,7 @@ public class ItemStackBuilder {
if (meta.hasLore()) { if (meta.hasLore()) {
List<Component> componentLore = new LinkedList<>(); List<Component> componentLore = new LinkedList<>();
meta.getLore().forEach(line -> componentLore.add(LegacyComponent.parse(line))); meta.getLore().forEach(line -> componentLore.add(LegacyComponent.simpleParse(line)));
nbtItem.setLoreComponents(componentLore); nbtItem.setLoreComponents(componentLore);
} }

View File

@ -179,8 +179,8 @@ public class LoreBuilder {
*/ */
for (int j = 0; j < lore.size(); ) { for (int j = 0; j < lore.size(); ) {
// Apply color codes and replace bar prefixes // Replace bar prefixes
String str = MythicLib.plugin.parseColors(lore.get(j).replace("{bar}", "").replace("{sbar}", "")); String str = lore.get(j).replace("{bar}", "").replace("{sbar}", "");
// Need to break down the line into multiple // Need to break down the line into multiple
if (str.contains("\\n")) { if (str.contains("\\n")) {

View File

@ -85,7 +85,7 @@ public class ItemUse implements Listener {
event.setCancelled(true); event.setCancelled(true);
useItem.getPlayerData().getMMOPlayerData().triggerSkills(player.isSneaking() ? TriggerType.SHIFT_RIGHT_CLICK : TriggerType.RIGHT_CLICK, null); useItem.getPlayerData().getMMOPlayerData().triggerSkills(player.isSneaking() ? TriggerType.SHIFT_RIGHT_CLICK : TriggerType.RIGHT_CLICK, null);
Consumable.ConsumableConsumeResult result = ((Consumable) useItem).useOnPlayer(); Consumable.ConsumableConsumeResult result = ((Consumable) useItem).useOnPlayer(event.getHand());
if (result == Consumable.ConsumableConsumeResult.CANCEL) if (result == Consumable.ConsumableConsumeResult.CANCEL)
return; return;
@ -323,7 +323,7 @@ public class ItemUse implements Listener {
return; return;
} }
Consumable.ConsumableConsumeResult result = ((Consumable) useItem).useOnPlayer(); Consumable.ConsumableConsumeResult result = ((Consumable) useItem).useOnPlayer(event.getItem().equals(player.getInventory().getItemInMainHand()) ? org.bukkit.inventory.EquipmentSlot.HAND : org.bukkit.inventory.EquipmentSlot.OFF_HAND);
// No effects are applied and not consumed // No effects are applied and not consumed
if (result == Consumable.ConsumableConsumeResult.CANCEL) { if (result == Consumable.ConsumableConsumeResult.CANCEL) {

View File

@ -1,187 +0,0 @@
package net.Indyuce.mmoitems.manager;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.api.util.LegacyComponent;
import io.lumine.mythic.utils.adventure.text.Component;
import io.lumine.mythic.utils.adventure.text.format.NamedTextColor;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.api.player.PlayerData;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
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 java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class UpdaterManager implements Listener {
/*public UpdaterManager() {
FileConfiguration config = new ConfigFile("/dynamic", "updater").getConfig();
for (String typeFormat : config.getKeys(false))
try {
Type type = MMOItems.plugin.getTypes().getOrThrow(typeFormat);
for (String id : config.getConfigurationSection(typeFormat).getKeys(false)) {
MMOItemTemplate template = MMOItems.plugin.getTemplates().getTemplateOrThrow(type, id);
//enable(new UpdaterData(template, config.getConfigurationSection(typeFormat + "." + id)));
}
} catch (IllegalArgumentException exception) {
MMOItems.plugin.getLogger().log(Level.WARNING,
"An issue occurred while trying to load dynamic updater data: " + exception.getMessage());
}
}*/
/**
* Updates inventory item when an item is clicked in a player's inventory
*/
@EventHandler
public void updateOnClick(InventoryClickEvent event) {
ItemStack item = event.getCurrentItem();
if (item == null || item.getType() == Material.AIR) return;
ItemStack newItem = getUpdated(item, (Player) event.getWhoClicked());
if (!newItem.equals(item)) event.setCurrentItem(newItem);
}
/**
* Updates a player inventory when joining
*/
@EventHandler(priority = EventPriority.HIGH)
public void updateOnJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
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));
player.getEquipment().setItemInOffHand(getUpdated(player.getEquipment().getItemInOffHand(), player));
}
public ItemStack getUpdated(ItemStack item, Player target) {
return getUpdated(MythicLib.plugin.getVersion().getWrapper().getNBTItem(item), target);
}
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 = Type.get(item.getType());
if (type == null) 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.getValue(type, item.getString("MMOITEMS_ITEM_ID"));
if (did.matches(item))
return item.getItem();
*/
MMOItemTemplate template = MMOItems.plugin.getTemplates().getTemplate(type, 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(ItemStats.GEM_SOCKETS))
newMMOItem.replaceData(ItemStats.GEM_SOCKETS, volatileItem.getData(ItemStats.GEM_SOCKETS));
if (did.hasOption(KeepOption.KEEP_SOULBOUND) && volatileItem.hasData(ItemStats.SOULBOUND))
newMMOItem.replaceData(ItemStats.SOULBOUND, volatileItem.getData(ItemStats.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();
NBTItem nbtItem = NBTItem.get(newItem);
List<Component> lore = new LinkedList<>();
newItemMeta.getLore().forEach(line -> lore.add(LegacyComponent.parse(line)));
/*
* 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 (Component component : nbtItem.getLoreComponents()) {
if (component.color() != NamedTextColor.GRAY) break;
lore.add(n++, component);
}
//}
/*
* 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());
newItem.setItemMeta(newItemMeta);
/*
* 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())
nbtItem.setDisplayNameComponent(LegacyComponent.parse(item.getItem().getItemMeta().getDisplayName()));
nbtItem.setLoreComponents(lore);
return nbtItem.toItem();
}
public enum KeepOption {
KEEP_LORE("Any lore line starting with '&7' will be", "kept when updating your item.", "", "This option is supposed to keep", "the item custom enchants.", ChatColor.RED + "May not support every enchant plugin."),
KEEP_ENCHANTS("The item keeps its old enchantments."),
KEEP_DURABILITY("The item keeps its durability.", "Don't use this option if you", "are using texture-by-durability!"),
KEEP_NAME("The item keeps its display name."),
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;
KeepOption(String... lore) {
this.lore = Arrays.asList(lore);
}
public List<String> getLore() {
return lore;
}
public String getPath() {
return name().toLowerCase().replace("_", "-").substring(5);
}
}
}