Merge branch 'master' into 'adventure-like-impl'

# Conflicts:
#   MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/util/crafting/CraftingRecipeDisplay.java
#   MMOItems-API/src/main/java/net/Indyuce/mmoitems/gui/ItemBrowser.java
#   MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/DisplayName.java
This commit is contained in:
Roch Blondiaux 2022-12-21 10:54:34 +00:00
commit 46fc12fee6
7 changed files with 110 additions and 79 deletions

View File

@ -111,18 +111,13 @@ public class CraftingRecipeDisplay extends ConfigItem {
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(ItemFlag.values());
meta.setDisplayName(MythicLib.plugin.parseColors(name.replace("#name#", (amount > 1 ? (ChatColor.WHITE + "" + amount + " x ") : "") + MMOUtils.getDisplayName(item))));
meta.setLore(lore.stream()
.map(s -> MythicLib.plugin.parseColors(s))
.collect(Collectors.toList()));
item.setItemMeta(meta);
NBTItem nbtItem = NBTItem.get(item);
// Name
nbtItem.setDisplayNameComponent(AdventureUtils.asComponent(name.replace("#name#", (amount > 1 ? (ChatColor.WHITE + "" + amount + " x ") : "") + MMOUtils.getDisplayName(item))));
// Lore
List<String> formattedLore = MythicLib.plugin.parseColors(lore);
nbtItem.setLoreComponents(formattedLore.stream()
.map(AdventureUtils::asComponent)
.toList());
return nbtItem.addTag(new ItemTag("recipeId", craftingRecipe.getId())).toItem();
return NBTItem.get(item).addTag(new ItemTag("recipeId", craftingRecipe.getId())).toItem();
}
}
}

View File

@ -132,7 +132,7 @@ public class PlayerData {
/**
* @return If the player hands are full i.e if the player is holding
* two items in their hands, one being two handed
* two items in their hands, one being two handed
*/
public boolean isEncumbered() {
@ -443,8 +443,7 @@ public class PlayerData {
/**
* Called when the corresponding MMOPlayerData has already been initialized.
*/
public static void load(@NotNull UUID player) {
public static PlayerData load(@NotNull UUID player) {
/*
* Double check they are online, for some reason even if this is fired
* from the join event the player can be offline if they left in the
@ -454,7 +453,7 @@ public class PlayerData {
PlayerData playerData = new PlayerData(MMOPlayerData.get(player));
data.put(player, playerData);
playerData.updateInventory();
return;
return playerData;
}
/*
@ -463,6 +462,7 @@ public class PlayerData {
*/
PlayerData playerData = data.get(player);
playerData.rpgPlayer = MMOItems.plugin.getRPG().getInfo(playerData);
return playerData;
}
public static Collection<PlayerData> getLoaded() {

View File

@ -5,7 +5,6 @@ import io.lumine.mythic.lib.command.api.CommandTreeNode;
import io.lumine.mythic.lib.command.api.Parameter;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.util.MMOUtils;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
@ -14,6 +13,7 @@ import net.Indyuce.mmoitems.api.util.RandomAmount;
import net.Indyuce.mmoitems.api.util.message.Message;
import net.Indyuce.mmoitems.command.MMOItemsCommandTreeRoot;
import net.Indyuce.mmoitems.stat.data.SoulboundData;
import net.Indyuce.mmoitems.util.MMOUtils;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -60,7 +60,7 @@ public class GiveCommandTreeNode extends CommandTreeNode {
double unidentify = args.length > 5 ? Double.parseDouble(args[5]) / 100 : 0;
double drop = args.length > 6 ? Double.parseDouble(args[6]) / 100 : 1;
double soulbound = args.length > 7 ? Double.parseDouble(args[7]) / 100 : 0;
boolean silent = args.length > 8 ? args[8].equalsIgnoreCase("silent") || args[8].equalsIgnoreCase("s") : false;
boolean silent = args.length > 8 && (args[8].equalsIgnoreCase("silent") || args[8].equalsIgnoreCase("s"));
// roll drop chance
if (random.nextDouble() > drop)
@ -83,13 +83,14 @@ public class GiveCommandTreeNode extends CommandTreeNode {
item.setAmount(amount.getRandomAmount());
// message
if (!silent) {
Message.RECEIVED_ITEM.format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(item), "#amount#",
(item.getAmount() > 1 ? " x" + item.getAmount() : "")).send(target);
if (!sender.equals(target))
sender.sendMessage(MMOItems.plugin.getPrefix() + ChatColor.YELLOW + "Successfully gave " + ChatColor.GOLD
+ MMOUtils.getDisplayName(item) + (item.getAmount() > 1 ? " x" + item.getAmount() : "") + ChatColor.YELLOW + " to "
+ ChatColor.GOLD + target.getName() + ChatColor.YELLOW + ".");
if (!silent)
Message.RECEIVED_ITEM.format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(item), "#amount#",
(item.getAmount() > 1 ? " x" + item.getAmount() : "")).send(target);
}
// item
new SmartGive(target).give(item);

View File

@ -8,6 +8,8 @@ import io.lumine.mythic.lib.api.util.ui.SilentNumbers;
import io.lumine.mythic.lib.util.AdventureUtils;
import io.lumine.mythic.lib.version.VersionMaterial;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.util.ColorUtils;
import net.Indyuce.mmoitems.util.MMOUtils;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.edition.NewItemEdition;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
@ -132,7 +134,7 @@ public class ItemBrowser extends PluginInventory {
* Displays all the items of the chosen Type
* ------------------------------
*/
Inventory inv = Bukkit.createInventory(this, 54, (deleteMode ? ("Delete Mode: ") : ("Item Explorer: ")) + type.getName());
Inventory inv = Bukkit.createInventory(this, 54, (deleteMode ? ("Delete Mode: ") : ("Item Explorer: ")) + ColorUtils.stripColors(type.getName()));
/*
* Build cool Item Stacks for buttons and sh

View File

@ -14,6 +14,7 @@ import net.Indyuce.mmoitems.stat.type.GemStoneStat;
import net.Indyuce.mmoitems.stat.type.NameData;
import net.Indyuce.mmoitems.stat.type.StatHistory;
import net.Indyuce.mmoitems.stat.type.StringStat;
import net.Indyuce.mmoitems.util.ColorUtils;
import org.bukkit.ChatColor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -21,6 +22,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
public class DisplayName extends StringStat implements GemStoneStat {
public DisplayName() {
super("NAME", VersionMaterial.OAK_SIGN.toMaterial(), "Display Name", new String[]{"The item display name."},
new String[]{"all"});
@ -28,14 +30,13 @@ public class DisplayName extends StringStat implements GemStoneStat {
@Override
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringData data) {
final AdventureParser parser = MythicLib.plugin.getAdventureParser();
// Bake
String format = data.toString();
final ItemTier tier = item.getMMOItem().getTier();
ItemTier tier = item.getMMOItem().getTier();
format = format.replace("<tier-name>", tier != null ? parser.stripColors(tier.getUnparsedName()) : "")
.replace("<tier-color>", tier != null ? parser.lastColor(tier.getUnparsedName(), true) : "&f")
.replace("<tier-color-cleaned>", tier != null ? parser.lastColor(tier.getUnparsedName(), false) : "");
// Bake
String format = data.toString()
.replace("<tier-name>", tier != null ? ColorUtils.stripColors(tier.getName()) : "")
.replace("<tier-color>", tier != null ? ColorUtils.getLastColors(tier.getName()) : "&f")
.replace("<tier-color-cleaned>", tier != null ? ColorUtils.stripDecoration(ColorUtils.getLastColors(tier.getName())) : "&f");
// Is this upgradable?
format = cropUpgrade(format);
@ -54,69 +55,66 @@ public class DisplayName extends StringStat implements GemStoneStat {
@NotNull
String cropUpgrade(@NotNull String format) {
String suffix = MMOItems.plugin.getConfig().getString("item-upgrading.name-suffix", " &8(&e+#lvl#&8)");
if (suffix == null || suffix.isEmpty()) {
if (suffix == null || suffix.isEmpty())
return format;
}
//MMOItems.getConsole().sendMessage("Level " + upgradeLevel);
//MMOItems.getConsole().sendMessage("Format " + format);
if (suffix != null) {
// Crop lvl
int lvlOFFSET = suffix.indexOf("#lvl#");
if (lvlOFFSET < 0) {
return format;
}
String sB4 = suffix.substring(0, lvlOFFSET);
String aFt = suffix.substring(lvlOFFSET + "#lvl#".length());
String sB4_alt = sB4.replace("+", "-");
String aFt_alt = aFt.replace("+", "-");
// Crop lvl
int lvlOFFSET = suffix.indexOf("#lvl#");
if (lvlOFFSET < 0)
return format;
String sB4 = suffix.substring(0, lvlOFFSET);
String aFt = suffix.substring(lvlOFFSET + "#lvl#".length());
String sB4_alt = sB4.replace("+", "-");
String aFt_alt = aFt.replace("+", "-");
// Remove it
if (format.contains(sB4)) {
// Remove it
if (format.contains(sB4)) {
// Get offsets
int sB4_offset = format.indexOf(sB4);
int aFt_offset = format.lastIndexOf(aFt);
// Get offsets
int sB4_offset = format.indexOf(sB4);
int aFt_offset = format.lastIndexOf(aFt);
// No after = to completion
if (aFt_offset < 0) {
aFt_offset = format.length();
} else {
aFt_offset += aFt.length();
}
// Remove that
String beforePrefix = format.substring(0, sB4_offset);
String afterPrefix = format.substring(aFt_offset);
// Replace
format = beforePrefix + afterPrefix;
// No after = to completion
if (aFt_offset < 0) {
aFt_offset = format.length();
} else {
aFt_offset += aFt.length();
}
// Remove it
if (format.contains(sB4_alt)) {
// Remove that
String beforePrefix = format.substring(0, sB4_offset);
String afterPrefix = format.substring(aFt_offset);
// Get offsets
int sB4_offset = format.indexOf(sB4_alt);
int aFt_offset = format.lastIndexOf(aFt_alt);
// Replace
format = beforePrefix + afterPrefix;
}
// No after = to completion
if (aFt_offset < 0) {
aFt_offset = format.length();
} else {
aFt_offset += aFt_alt.length();
}
// Remove it
if (format.contains(sB4_alt)) {
// Remove that
String beforePrefix = format.substring(0, sB4_offset);
String afterPrefix = format.substring(aFt_offset);
// Get offsets
int sB4_offset = format.indexOf(sB4_alt);
int aFt_offset = format.lastIndexOf(aFt_alt);
// Replace
format = beforePrefix + afterPrefix;
// No after = to completion
if (aFt_offset < 0) {
aFt_offset = format.length();
} else {
aFt_offset += aFt_alt.length();
}
// Remove that
String beforePrefix = format.substring(0, sB4_offset);
String afterPrefix = format.substring(aFt_offset);
// Replace
format = beforePrefix + afterPrefix;
}
/*/ Bake old indices for removal
ArrayList<String> oldSuffixii = new ArrayList<>(); boolean negativity = false;
if (upgradeLevel < 0) { upgradeLevel = -upgradeLevel; negativity = true; }
@ -135,8 +133,7 @@ public class DisplayName extends StringStat implements GemStoneStat {
//MMOItems.getConsole().sendMessage("Edited " + format);
} //*/
//MMOItems.getConsole().sendMessage("Final " + format);
}
//MMOItems.getConsole().sendMessage("Final " + format);
return format;
}
@ -148,7 +145,6 @@ public class DisplayName extends StringStat implements GemStoneStat {
String actSuffix = levelPrefix(suffix, lvl);
return format + actSuffix;
}
return format;
}

View File

@ -0,0 +1,38 @@
package net.Indyuce.mmoitems.util;
import org.bukkit.ChatColor;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* mmoitems
* 09/11/2022
*
* @author Roch Blondiaux (Kiwix).
*/
public class ColorUtils {
private static final Pattern STRIP_DECORATION_PATTERN = Pattern.compile("(?i)" + '§' + "[K-O]");
private static final Pattern COLOR_TAG_PATTERN = Pattern.compile("(?i)<.*>");
private static final Pattern START_COLOR_TAG_PATTERN = Pattern.compile("(?i)<[^/]*>");
private static final Pattern MINI_MSG_DECORATION_PATTERN = Pattern.compile("(?i)(<|</)(bold|italic|underlined|strikethrough|obfuscated|b|em|i|u|st|obf).*>");
public static @NotNull String stripDecoration(@NotNull String input) {
return "%s%s".formatted(ChatColor.RESET, MINI_MSG_DECORATION_PATTERN.matcher(STRIP_DECORATION_PATTERN.matcher(input).replaceAll("")).replaceAll(""))
.replace('§', '&');
}
public static @NotNull String stripColors(@NotNull String input) {
return ChatColor.stripColor(COLOR_TAG_PATTERN.matcher(input).replaceAll(""));
}
public static @NotNull String getLastColors(@NotNull String input) {
Matcher matcher = START_COLOR_TAG_PATTERN.matcher(input);
String lastMatch = null;
while (matcher.find())
lastMatch = matcher.group();
return lastMatch == null ? ChatColor.getLastColors(input) : lastMatch;
}
}

View File

@ -31,13 +31,13 @@ import java.util.*;
public class PlayerListener implements Listener {
private final Map<Player, List<ItemStack>> deathItems = new HashMap<>();
@EventHandler(priority = EventPriority.NORMAL)
@EventHandler(priority = EventPriority.LOWEST)
public void loadPlayerData(PlayerJoinEvent event) {
MMOItems.plugin.getRecipes().refreshRecipeBook(event.getPlayer());
PlayerData.load(event.getPlayer());
}
@EventHandler(priority = EventPriority.HIGH)
@EventHandler(priority = EventPriority.HIGHEST)
public void savePlayerData(PlayerQuitEvent event) {
PlayerData.get(event.getPlayer()).save(true);
}
@ -48,7 +48,6 @@ public class PlayerListener implements Listener {
@SuppressWarnings("InstanceofIncompatibleInterface")
@EventHandler(priority = EventPriority.MONITOR)
public void onDeathForUpgradeLoss(@NotNull PlayerDeathEvent event) {
// No
if (event instanceof Cancellable) { if (((Cancellable) event).isCancelled()) { return; } }