Skip to content

Commit 68fed2f

Browse files
BillyGalbreathgranny
authored andcommitted
Allow anvil colors
1 parent a10fb30 commit 68fed2f

4 files changed

Lines changed: 69 additions & 83 deletions

File tree

patches/server/0079-Allow-anvil-colors.patch

Lines changed: 0 additions & 83 deletions
This file was deleted.

purpur-api/src/main/java/org/purpurmc/purpur/util/permissions/PurpurPermissions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public static Permission registerPermissions() {
3535

3636
DefaultPermissions.registerPermission(PREFIX + "inventory_totem", "Allows the user to use totem of undying anywhere in their inventory", PermissionDefault.FALSE, purpur);
3737

38+
Permission anvil = DefaultPermissions.registerPermission(PREFIX + "anvil", "Allows the user to use all anvil color and format abilities", PermissionDefault.FALSE, purpur);
39+
DefaultPermissions.registerPermission(PREFIX + "anvil.color", "Allows the user to use color codes in an anvil", PermissionDefault.FALSE, anvil);
40+
DefaultPermissions.registerPermission(PREFIX + "anvil.minimessage", "Allows the user to use minimessage tags in an anvil", PermissionDefault.FALSE, anvil);
41+
DefaultPermissions.registerPermission(PREFIX + "anvil.remove_italics", "Allows the user to remove italics in an anvil", PermissionDefault.FALSE, anvil);
42+
DefaultPermissions.registerPermission(PREFIX + "anvil.format", "Allows the user to use format codes in an anvil", PermissionDefault.FALSE, anvil);
43+
anvil.recalculatePermissibles();
44+
3845
Permission book = DefaultPermissions.registerPermission(PREFIX + "book", "Allows the user to use color codes on books", PermissionDefault.FALSE, purpur);
3946
DefaultPermissions.registerPermission(PREFIX + "book.color.edit", "Allows the user to use color codes on books when editing", PermissionDefault.FALSE, book);
4047
DefaultPermissions.registerPermission(PREFIX + "book.color.sign", "Allows the user to use color codes on books when signing", PermissionDefault.FALSE, book);

purpur-server/minecraft-patches/sources/net/minecraft/world/inventory/AnvilMenu.java.patch

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,61 @@
7373
i++;
7474
}
7575
}
76+
@@ -236,6 +_,54 @@
77+
if (!this.itemName.equals(item.getHoverName().getString())) {
78+
i1 = 1;
79+
i += i1;
80+
+ // Purpur start - Allow anvil colors
81+
+ if (this.player != null) {
82+
+ org.bukkit.craftbukkit.entity.CraftHumanEntity player = this.player.getBukkitEntity();
83+
+ String name = this.itemName;
84+
+ boolean removeItalics = false;
85+
+ if (player.hasPermission("purpur.anvil.remove_italics")) {
86+
+ if (name.startsWith("&r")) {
87+
+ name = name.substring(2);
88+
+ removeItalics = true;
89+
+ } else if (name.startsWith("<r>")) {
90+
+ name = name.substring(3);
91+
+ removeItalics = true;
92+
+ } else if (name.startsWith("<reset>")) {
93+
+ name = name.substring(7);
94+
+ removeItalics = true;
95+
+ }
96+
+ }
97+
+ if (this.player.level().purpurConfig.anvilAllowColors) {
98+
+ if (player.hasPermission("purpur.anvil.color")) {
99+
+ java.util.regex.Matcher matcher = java.util.regex.Pattern.compile("(?i)&([0-9a-fr])").matcher(name);
100+
+ while (matcher.find()) {
101+
+ String match = matcher.group(1);
102+
+ name = name.replace("&" + match, "\u00a7" + match.toLowerCase(java.util.Locale.ROOT));
103+
+ }
104+
+ //name = name.replaceAll("(?i)&([0-9a-fr])", "\u00a7$1");
105+
+ }
106+
+ if (player.hasPermission("purpur.anvil.format")) {
107+
+ java.util.regex.Matcher matcher = java.util.regex.Pattern.compile("(?i)&([k-or])").matcher(name);
108+
+ while (matcher.find()) {
109+
+ String match = matcher.group(1);
110+
+ name = name.replace("&" + match, "\u00a7" + match.toLowerCase(java.util.Locale.ROOT));
111+
+ }
112+
+ //name = name.replaceAll("(?i)&([l-or])", "\u00a7$1");
113+
+ }
114+
+ }
115+
+ net.kyori.adventure.text.Component component;
116+
+ if (this.player.level().purpurConfig.anvilColorsUseMiniMessage && player.hasPermission("purpur.anvil.minimessage")) {
117+
+ component = net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(org.bukkit.ChatColor.stripColor(name));
118+
+ } else {
119+
+ component = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(name);
120+
+ }
121+
+ if (removeItalics) {
122+
+ component = component.decoration(net.kyori.adventure.text.format.TextDecoration.ITALIC, false);
123+
+ }
124+
+ itemStack.set(DataComponents.CUSTOM_NAME, io.papermc.paper.adventure.PaperAdventure.asVanilla(component));
125+
+ }
126+
+ else
127+
+ // Purpur end - Allow anvil colors
128+
itemStack.set(DataComponents.CUSTOM_NAME, Component.literal(this.itemName));
129+
}
130+
} else if (item.has(DataComponents.CUSTOM_NAME)) {
76131
@@ -260,6 +_,12 @@
77132
this.onlyRenaming = true;
78133
}

purpur-server/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ private void silkTouchSettings() {
277277
});
278278
}
279279

280+
public boolean anvilAllowColors = false;
281+
public boolean anvilColorsUseMiniMessage;
282+
private void anvilSettings() {
283+
anvilAllowColors = getBoolean("blocks.anvil.allow-colors", anvilAllowColors);
284+
anvilColorsUseMiniMessage = getBoolean("blocks.anvil.use-mini-message", anvilColorsUseMiniMessage);
285+
}
286+
280287
public boolean bedExplode = true;
281288
public double bedExplosionPower = 5.0D;
282289
public boolean bedExplosionFire = true;

0 commit comments

Comments
 (0)