Trimmed item name and price on signs
This commit is contained in:
@@ -3,6 +3,7 @@ package com.alttd.playershops.utils;
|
||||
import com.alttd.playershops.shop.PlayerShop;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
@@ -156,17 +157,49 @@ public class ShopUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Component itemNameComponent(ItemStack item) {
|
||||
Component component = Component.empty();
|
||||
public static Component trimmedItemNameComponent(ItemStack item) {
|
||||
if(item == null || item.getType().equals(Material.AIR))
|
||||
return Component.text("Nothing");
|
||||
boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName();
|
||||
if(dname) {
|
||||
component = component.append(item.getItemMeta().displayName());
|
||||
} else {
|
||||
component = component.append(Component.text(materialToName(item.getType()), NamedTextColor.WHITE));
|
||||
return materialToTrimmedName(item.getType());
|
||||
}
|
||||
|
||||
private static Component materialToTrimmedName(Material m) {
|
||||
if (m.equals(Material.TNT)) {
|
||||
return Component.text("TNT", NamedTextColor.WHITE);
|
||||
}
|
||||
String orig = m.name().toLowerCase();
|
||||
String[] splits = orig.split("_");
|
||||
StringBuilder sb = new StringBuilder(orig.length());
|
||||
int pos = 0;
|
||||
for (String split : splits) {
|
||||
sb.append(split);
|
||||
int loc = sb.lastIndexOf(split);
|
||||
char charLoc = sb.charAt(loc);
|
||||
if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") ||
|
||||
split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on")))
|
||||
sb.setCharAt(loc, Character.toUpperCase(charLoc));
|
||||
if (pos != splits.length - 1)
|
||||
sb.append(' ');
|
||||
++pos;
|
||||
}
|
||||
|
||||
if (sb.length() > 10)
|
||||
return Component.text(sb.substring(0, 8), NamedTextColor.WHITE).append(Component.text("!", NamedTextColor.RED));
|
||||
return Component.text(sb.toString(), NamedTextColor.WHITE);
|
||||
}
|
||||
|
||||
public static Component itemNameComponent(ItemStack item) {
|
||||
Component component;
|
||||
MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
if (item.getType().equals(Material.AIR))
|
||||
return miniMessage.deserialize("NONE");
|
||||
boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName();
|
||||
if (dname) {
|
||||
component = item.getItemMeta().displayName();
|
||||
} else {
|
||||
component = Component.text(materialToName(item.getType()), NamedTextColor.WHITE);
|
||||
}
|
||||
component = component.hoverEvent(item.asHoverEvent());
|
||||
return component;
|
||||
}
|
||||
|
||||
@@ -174,7 +207,7 @@ public class ShopUtil {
|
||||
if (m.equals(Material.TNT)) {
|
||||
return "TNT";
|
||||
}
|
||||
String orig = m.name().toLowerCase();
|
||||
String orig = m.toString().toLowerCase();
|
||||
String[] splits = orig.split("_");
|
||||
StringBuilder sb = new StringBuilder(orig.length());
|
||||
int pos = 0;
|
||||
|
||||
Reference in New Issue
Block a user