Skip to content

Commit

Permalink
fix fonts in inventory titles
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 6, 2022
1 parent 97a40d3 commit 8b7f1bd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
Expand Up @@ -30,6 +30,17 @@ public Inventory createInventory(InventoryHolder holder, InventoryType type, Str
return Bukkit.getServer().createInventory(holder, type, PaperModule.parseFormattedText(title, ChatColor.BLACK));
}

@Override
public String parseComponent(Object input, ChatColor baseColor) {
if (input == null) {
return null;
}
if (input instanceof Component) {
return PaperModule.stringifyComponent((Component) input, baseColor);
}
return super.parseComponent(input, baseColor);
}

@Override
public String getTitle(Inventory inventory) {
// TODO: Paper lacks an inventory.getTitle? 0.o
Expand Down
@@ -1,6 +1,8 @@
package com.denizenscript.denizen.utilities;

import com.denizenscript.denizen.nms.NMSHandler;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Sign;
Expand All @@ -22,6 +24,24 @@ public Inventory createInventory(InventoryHolder holder, InventoryType type, Str
return Bukkit.getServer().createInventory(holder, type, title);
}

public String parseComponent(Object input, ChatColor baseColor) {
if (input == null) {
return null;
}
if (input instanceof String) {
return (String) input;
}
else if (input instanceof BaseComponent[]) {
return FormattedTextHelper.stringify((BaseComponent[]) input, baseColor);
}
else if (input instanceof BaseComponent) {
return FormattedTextHelper.stringify((BaseComponent) input);
}
else {
return input.toString();
}
}

public String getTitle(Inventory inventory) {
return NMSHandler.getInstance().getTitle(inventory);
}
Expand Down
Expand Up @@ -109,22 +109,25 @@ public static String stringify(BaseComponent component) {
StringBuilder builder = new StringBuilder(128);
ChatColor color = component.getColorRaw();
if (color != null) {
builder.append(color.toString());
builder.append(color);
}
if (boolNotNull(component.isBoldRaw())) {
builder.append(ChatColor.BOLD.toString());
builder.append(ChatColor.BOLD);
}
if (boolNotNull(component.isItalicRaw())) {
builder.append(ChatColor.ITALIC.toString());
builder.append(ChatColor.ITALIC);
}
if (boolNotNull(component.isStrikethroughRaw())) {
builder.append(ChatColor.STRIKETHROUGH.toString());
builder.append(ChatColor.STRIKETHROUGH);
}
if (boolNotNull(component.isUnderlinedRaw())) {
builder.append(ChatColor.UNDERLINE.toString());
builder.append(ChatColor.UNDERLINE);
}
if (boolNotNull(component.isObfuscatedRaw())) {
builder.append(ChatColor.MAGIC.toString());
builder.append(ChatColor.MAGIC);
}
if (component.getFontRaw() != null) {
builder.append(ChatColor.COLOR_CHAR).append("[font=").append(component.getFontRaw()).append("]");
}
boolean hasInsertion = component.getInsertion() != null;
if (hasInsertion) {
Expand Down
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.nms.v1_18;

import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.*;
import com.denizenscript.denizen.nms.v1_18.helpers.*;
Expand All @@ -10,6 +11,7 @@
import com.denizenscript.denizen.nms.v1_18.impl.jnbt.CompoundTagImpl;
import com.denizenscript.denizen.nms.util.jnbt.Tag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.AdvancedTextImpl;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.google.common.collect.Iterables;
import com.mojang.authlib.GameProfile;
Expand Down Expand Up @@ -59,6 +61,7 @@
import org.bukkit.persistence.PersistentDataContainer;
import org.spigotmc.AsyncCatcher;

import java.lang.invoke.MethodHandle;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -178,9 +181,22 @@ public int getPort() {
return ((CraftServer) Bukkit.getServer()).getServer().getPort();
}

public static MethodHandle PAPER_INVENTORY_TITLE_GETTER;

@Override
public String getTitle(Inventory inventory) {
Container nms = ((CraftInventory) inventory).getInventory();
if (inventory instanceof CraftInventoryCustom && Denizen.supportsPaper) {
try {
if (PAPER_INVENTORY_TITLE_GETTER == null) {
PAPER_INVENTORY_TITLE_GETTER = ReflectionHelper.getMethodHandle(nms.getClass(), "title");
}
return AdvancedTextImpl.instance.parseComponent(PAPER_INVENTORY_TITLE_GETTER.invoke(nms), ChatColor.BLACK);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
if (nms instanceof Nameable) {
return CraftChatMessage.fromComponent(((Nameable) nms).getDisplayName());
}
Expand Down

0 comments on commit 8b7f1bd

Please sign in to comment.