Skip to content

Commit

Permalink
adventure support for item name and lore
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Oct 13, 2024
1 parent 66148e2 commit 80a3576
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 63 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ dependencies {
compileOnly("com.github.MilkBowl:VaultAPI:1.7") { isTransitive = false }

shadowImpl("de.tr7zw:item-nbt-api:2.13.2")
shadowImpl("net.kyori:adventure-api:4.17.0")
shadowImpl("net.kyori:adventure-platform-bukkit:4.3.4")
shadowImpl("net.kyori:adventure-text-minimessage:4.17.0")

shadow(project(":paper"))
for (proj in rootProject.project(":nms").subprojects) {
Expand Down Expand Up @@ -132,6 +135,7 @@ tasks {
configurations = listOf(project.configurations.shadow.get())
mapOf(
"de.tr7zw.changeme.nbtapi" to "nbtapi",
"net.kyori" to "kyori",
).forEach { (original, target) ->
relocate(original, "think.rpgitems.utils.$target")
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/think/rpgitems/RPGItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import de.tr7zw.changeme.nbtapi.NBT;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand Down Expand Up @@ -74,6 +75,10 @@ public static boolean isPaper() {

public static Logger logger;
public static RPGItems plugin;
private static BukkitAudiences adventure;
public static BukkitAudiences adv() {
return adventure;
}

List<Plugin> managedPlugins = new ArrayList<>();
public Configuration cfg;
Expand Down
84 changes: 37 additions & 47 deletions src/main/java/think/rpgitems/item/RPGItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@
import think.rpgitems.power.trigger.BaseTriggers;
import think.rpgitems.power.trigger.Trigger;
import think.rpgitems.support.MythicSupport;
import think.rpgitems.utils.ColorHelper;
import think.rpgitems.utils.ISubItemTagContainer;
import think.rpgitems.utils.MaterialUtils;
import think.rpgitems.utils.MessageType;
import think.rpgitems.utils.*;
import think.rpgitems.utils.nyaacore.Pair;
import think.rpgitems.utils.nyaacore.utils.ItemStackUtils;
import think.rpgitems.utils.nyaacore.utils.ItemTagUtils;
Expand All @@ -78,6 +75,7 @@
import static org.bukkit.Material.*;
import static org.bukkit.attribute.AttributeModifier.Operation.ADD_NUMBER;
import static think.rpgitems.RPGItems.forceWrap;
import static think.rpgitems.utils.ColorHelper.send;
import static think.rpgitems.utils.nyaacore.utils.ReflectionUtils.getPropVal;
import static think.rpgitems.utils.nyaacore.utils.ReflectionUtils.setPropVal;

Expand Down Expand Up @@ -133,7 +131,6 @@ public class RPGItem implements RPGBaseHolder {
@Getter @Setter private boolean hasPermission;
@Setter private String permission;
private String displayName;
private String displayNameColored;
@Getter @Setter private String factor;
@Getter @Setter Map<String, FactorModifier> factorModifiers = new HashMap<>();
@Getter private int damageMin = 0;
Expand Down Expand Up @@ -632,7 +629,7 @@ public void save(ConfigurationSection s) {

s.set("haspermission", isHasPermission());
s.set("permission", getPermission());
s.set("display", getDisplayNameRaw().replaceAll("§", "&"));
s.set("display", getDisplayName());
s.set("factor", getFactor());

for (FactorModifier modifier : factorModifiers.values()) {
Expand Down Expand Up @@ -883,16 +880,7 @@ public void updateItem(@Nullable Player player, ItemStack item, boolean loreOnly
LoreUpdateEvent loreUpdateEvent = new LoreUpdateEvent(this, player, item, oldLore, lore);
Bukkit.getPluginManager().callEvent(loreUpdateEvent);
item = loreUpdateEvent.item;
meta.setLore(loreUpdateEvent.newLore);

//quality prefix
String qualityPrefix = plugin.cfg.qualityPrefixes.get(getQuality());
if (qualityPrefix != null) {
if (meta.hasDisplayName() && !meta.getDisplayName().startsWith(qualityPrefix)) {
String displayName = meta.getDisplayName();
meta.setDisplayName(qualityPrefix + displayName);
}
}
ItemUtils.setItemLore(item, loreUpdateEvent.newLore);

if (armour > 0 || armourProjectile > 0 || !armourExpression.isEmpty()) {
String m = item.getType().name().toUpperCase();
Expand Down Expand Up @@ -920,31 +908,33 @@ public void updateItem(@Nullable Player player, ItemStack item, boolean loreOnly
if (loreOnly) {
rpgitemsTagContainer.commit();
item.setItemMeta(meta);
return;
}

meta.setUnbreakable(isCustomItemModel() || hasMarker(Unbreakable.class));
meta.removeItemFlags(meta.getItemFlags().toArray(new ItemFlag[0]));
} else {
meta.setUnbreakable(hasMarker(Unbreakable.class));
meta.removeItemFlags(meta.getItemFlags().toArray(new ItemFlag[0]));

meta.setCustomModelData(itemUpdateEvent.getCustomModelData());
for (ItemFlag flag : itemUpdateEvent.getItemFlags()) {
meta.addItemFlags(flag);
}
if (getEnchantMode() == EnchantMode.DISALLOW) {
Set<Enchantment> enchs = meta.getEnchants().keySet();
for (Enchantment e : enchs) {
meta.removeEnchant(e);
meta.setCustomModelData(itemUpdateEvent.getCustomModelData());
for (ItemFlag flag : itemUpdateEvent.getItemFlags()) {
meta.addItemFlags(flag);
}
}
Map<Enchantment, Integer> enchantMap = getEnchantMap();
if (enchantMap != null) {
for (Entry<Enchantment, Integer> e : enchantMap.entrySet()) {
meta.addEnchant(e.getKey(), Math.max(meta.getEnchantLevel(e.getKey()), e.getValue()), true);
if (getEnchantMode() == EnchantMode.DISALLOW) {
Set<Enchantment> enchs = meta.getEnchants().keySet();
for (Enchantment e : enchs) {
meta.removeEnchant(e);
}
}
Map<Enchantment, Integer> enchantMap = getEnchantMap();
if (enchantMap != null) {
for (Entry<Enchantment, Integer> e : enchantMap.entrySet()) {
meta.addEnchant(e.getKey(), Math.max(meta.getEnchantLevel(e.getKey()), e.getValue()), true);
}
}
checkAndMakeUnique(rpgitemsTagContainer);
rpgitemsTagContainer.commit();
item.setItemMeta(refreshAttributeModifiers(meta));
}
checkAndMakeUnique(rpgitemsTagContainer);
rpgitemsTagContainer.commit();
item.setItemMeta(refreshAttributeModifiers(meta));
ItemUtils.setItemLore(item, loreUpdateEvent.newLore);

if (loreOnly) return;

ItemTagUtils.setInt(item, NBT_UID, uid);
if (RPGItems.plugin.cfg.itemStackUuid) {
Expand Down Expand Up @@ -1568,6 +1558,11 @@ private String getDamageText(double dmg, double dmgMultiple, String path) {
return null;
}

public String getQualityPrefix() {
String qualityPrefix = plugin.cfg.qualityPrefixes.get(getQuality());
return qualityPrefix == null ? "" : qualityPrefix;
}

@Deprecated
public ItemStack toItemStack() {
return toItemStack(null);
Expand All @@ -1582,8 +1577,8 @@ public ItemStack toItemStack(Player player) {
ItemPDC.set(rpgitemsTagContainer, TAG_STACK_ID, UUID.randomUUID());
}
rpgitemsTagContainer.commit();
meta.setDisplayName(getDisplayName());
rStack.setItemMeta(meta);
ItemUtils.setItemDisplayName(rStack, getQualityPrefix() + getDisplayName());

updateItem(player, rStack, false);
return rStack;
Expand All @@ -1604,8 +1599,8 @@ public void toModel(@Nullable Player player, ItemStack itemStack) {
ItemTagUtils.setBoolean(itemStack, NBT_IS_MODEL, true);

meta.commit();
itemMeta.setDisplayName(getDisplayName());
itemStack.setItemMeta(itemMeta);
ItemUtils.setItemDisplayName(itemStack, getQualityPrefix() + getDisplayName());
}

public void unModel(ItemStack itemStack, Player owner) {
Expand All @@ -1620,14 +1615,14 @@ public void unModel(ItemStack itemStack, Player owner) {
}
meta.remove(TAG_IS_MODEL);
meta.commit();
itemMeta.setDisplayName(getDisplayName());
itemStack.setItemMeta(itemMeta);
ItemUtils.setItemDisplayName(itemStack, getQualityPrefix() + getDisplayName());
}

public Event.Result checkPermission(Player p, boolean showWarn) {
if (isHasPermission() && !p.hasPermission(getPermission())) {
if (showWarn)
p.sendMessage(I18n.getInstance(p.getLocale()).format("message.error.permission", getDisplayName()));
send(p, I18n.getInstance(p.getLocale()).format("message.error.permission", getDisplayName()));
return Event.Result.DENY;
}
return Event.Result.ALLOW;
Expand Down Expand Up @@ -2249,18 +2244,13 @@ public double getCriticalAntiRate(LivingEntity entity) {
return rate.get();
}


public String getDisplayNameRaw() {
return displayName;
}
@Override
public String getDisplayName() {
return displayNameColored;
return displayName;
}
@Override
public void setDisplayName(String displayName) {
this.displayName = displayName;
this.displayNameColored = ColorHelper.parseColor(displayName);
}

public void setDurabilityBound(int min, int max) {
Expand Down
29 changes: 13 additions & 16 deletions src/main/java/think/rpgitems/item/RPGStone.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import think.rpgitems.power.trigger.BaseTriggers;
import think.rpgitems.utils.ColorHelper;
import think.rpgitems.utils.ISubItemTagContainer;
import think.rpgitems.utils.ItemUtils;
import think.rpgitems.utils.MaterialUtils;
import think.rpgitems.utils.nyaacore.utils.ItemTagUtils;
import think.rpgitems.utils.pdc.ItemPDC;
Expand Down Expand Up @@ -50,7 +51,6 @@ public class RPGStone implements RPGBaseHolder {
@Getter private final String name;

private String displayName;
private String displayNameColored;
@Setter @Getter private List<String> description = new ArrayList<>();
@Setter @Getter private List<String> extraDescription = new ArrayList<>();
@Getter @Setter private boolean customItemModel;
Expand Down Expand Up @@ -188,7 +188,7 @@ public void save(ConfigurationSection s) {
s.set("name", getName());
s.set("uid", getUid());

s.set("display", getDisplayNameRaw().replaceAll("§", "&"));
s.set("display", getDisplayName());

ArrayList<String> descriptionConv = new ArrayList<>(getDescription());
descriptionConv.replaceAll(s1 -> s1.replaceAll("§", "&"));
Expand Down Expand Up @@ -341,21 +341,22 @@ public void updateItem(ItemStack item, boolean loreOnly) {
((LeatherArmorMeta) meta).setColor(Color.fromRGB(getDataValue()));
}

meta.setDisplayName(getDisplayName());
meta.setLore(new ArrayList<>(getDescription()));

if (loreOnly) {
rpgitemsTagContainer.commit();
item.setItemMeta(meta);
if (!useCustomTrigger()) ItemTagUtils.remove(item, NBT_POWER_STONE_TRIGGER);
return;
} else {
if (isCustomItemModel()) {
meta.setCustomModelData(getCustomModelData());
}
rpgitemsTagContainer.commit();
item.setItemMeta(meta);
}

if (isCustomItemModel()) {
meta.setCustomModelData(getCustomModelData());
}
rpgitemsTagContainer.commit();
item.setItemMeta(meta);
ItemUtils.setItemDisplayName(item, getDisplayName());
ItemUtils.setItemLore(item, new ArrayList<>(getDescription()));

if (loreOnly) return;

ItemTagUtils.setInt(item, NBT_POWER_STONE_UID, uid);
if (RPGItems.plugin.cfg.itemStackUuid) {
Expand Down Expand Up @@ -389,17 +390,13 @@ public void give(Player player, int count) {
}
}

public String getDisplayNameRaw() {
return displayName;
}
@Override
public String getDisplayName() {
return displayNameColored;
return displayName;
}
@Override
public void setDisplayName(String displayName) {
this.displayName = displayName;
this.displayNameColored = ColorHelper.parseColor(displayName);
}
@Override
public void addDescription(String str) {
Expand Down
Loading

0 comments on commit 80a3576

Please sign in to comment.