Skip to content

Commit d690ed6

Browse files
authored
StyleConstant creation (#259)
* move code generation colors to constant values * extract to StyleConstant, apply to many places * fix minor mistakes * change nbt key coloration * color nbt tag string properly * adjust blockstate appearance * change color for mapper and string, improve javadoc * fix JEI tab not working based on stack size * make NBT letters be lowercase * convert mapper color to be method color * convert style to methods * make copy text be colored * adjust mekanism info commands * act as if colored is always true for info parsers * adjust javadoc * update nbt parser and extract constants * restore using colored parameter
1 parent bb8b1d9 commit d690ed6

File tree

16 files changed

+283
-147
lines changed

16 files changed

+283
-147
lines changed

src/main/java/com/cleanroommc/groovyscript/GroovyScript.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.cleanroommc.groovyscript.documentation.linkgenerator.LinkGeneratorHooks;
1515
import com.cleanroommc.groovyscript.event.EventHandler;
1616
import com.cleanroommc.groovyscript.helper.JsonHelper;
17+
import com.cleanroommc.groovyscript.helper.StyleConstant;
1718
import com.cleanroommc.groovyscript.mapper.ObjectMapper;
1819
import com.cleanroommc.groovyscript.mapper.ObjectMapperManager;
1920
import com.cleanroommc.groovyscript.network.CReload;
@@ -181,10 +182,10 @@ public static long runGroovyScriptsInLoader(LoadStage loadStage) {
181182
public void onPostInit(FMLPostInitializationEvent event) {
182183
CustomClickAction.registerAction("copy", value -> {
183184
GuiScreen.setClipboardString(value);
184-
Minecraft.getMinecraft().player.sendMessage(
185-
new TextComponentTranslation("groovyscript.command.copy.copied_start")
186-
.appendSibling(new TextComponentString(value).setStyle(new Style().setColor(TextFormatting.GOLD)))
187-
.appendSibling(new TextComponentTranslation("groovyscript.command.copy.copied_end")));
185+
var message = new TextComponentTranslation("groovyscript.command.copy.copied_start").setStyle(StyleConstant.getEmphasisStyle())
186+
.appendSibling(new TextComponentString(value).setStyle(new Style().setColor(TextFormatting.RESET)))
187+
.appendSibling(new TextComponentTranslation("groovyscript.command.copy.copied_end").setStyle(StyleConstant.getEmphasisStyle()));
188+
Minecraft.getMinecraft().player.sendMessage(message);
188189
});
189190
}
190191

@@ -279,21 +280,21 @@ public static void postScriptRunResult(ICommandSender sender, boolean onlyLogFai
279280
if (!onlyLogFails) {
280281
if (running) {
281282
String s = packmode ? "changes packmode" : "reloaded scripts";
282-
sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "Successfully " + s + TextFormatting.WHITE + " in " + time + "ms"));
283+
sender.sendMessage(new TextComponentString("Successfully " + s).setStyle(StyleConstant.getSuccessStyle()).appendSibling(new TextComponentString(" in " + time + "ms")));
283284
} else {
284-
sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "No syntax errors found :)"));
285+
sender.sendMessage(new TextComponentString("No syntax errors found :)").setStyle(StyleConstant.getSuccessStyle()));
285286
}
286287
}
287288
} else {
288289
String executing = running ? "running" : "checking";
289-
sender.sendMessage(new TextComponentString(TextFormatting.RED + "Found " + errors.size() + " errors while " + executing + " scripts"));
290+
sender.sendMessage(new TextComponentString("Found " + errors.size() + " errors while " + executing + " scripts").setStyle(StyleConstant.getErrorStyle()));
290291
int n = errors.size();
291292
if (errors.size() >= 10) {
292-
sender.sendMessage(new TextComponentString("Displaying the first 7 errors:"));
293+
sender.sendMessage(new TextComponentString("Displaying the first 7 errors:").setStyle(StyleConstant.getTitleStyle()));
293294
n = 7;
294295
}
295296
for (int i = 0; i < n; i++) {
296-
sender.sendMessage(new TextComponentString(TextFormatting.RED + errors.get(i)));
297+
sender.sendMessage(new TextComponentString(errors.get(i)).setStyle(StyleConstant.getErrorStyle()));
297298
}
298299
GSCommand.postLogFiles(sender);
299300
}

src/main/java/com/cleanroommc/groovyscript/api/infocommand/InfoParser.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.cleanroommc.groovyscript.api.infocommand;
22

3+
import com.cleanroommc.groovyscript.helper.StyleConstant;
34
import net.minecraft.util.text.Style;
4-
import net.minecraft.util.text.TextFormatting;
55

66
public interface InfoParser {
77

88
/**
99
* The style for any parser header - bold and light purple.
10+
*
11+
* @deprecated use {@link com.cleanroommc.groovyscript.helper.StyleConstant#getTitleStyle()}
1012
*/
11-
Style headerStyle = new Style().setColor(TextFormatting.WHITE).setBold(true);
13+
@Deprecated
14+
Style headerStyle = StyleConstant.getTitleStyle();
1215

1316
/**
1417
* Priority of the parser for determining the order they are logged in chat,

src/main/java/com/cleanroommc/groovyscript/command/BaseInfoCommand.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.cleanroommc.groovyscript.api.infocommand.InfoParserPackage;
44
import com.cleanroommc.groovyscript.api.infocommand.InfoParserRegistry;
55
import com.cleanroommc.groovyscript.event.GsHandEvent;
6+
import com.cleanroommc.groovyscript.helper.StyleConstant;
67
import com.google.common.base.Predicates;
78
import net.minecraft.command.CommandBase;
89
import net.minecraft.command.ICommandSender;
@@ -15,9 +16,7 @@
1516
import net.minecraft.util.math.RayTraceResult;
1617
import net.minecraft.util.math.Vec3d;
1718
import net.minecraft.util.text.ITextComponent;
18-
import net.minecraft.util.text.Style;
1919
import net.minecraft.util.text.TextComponentString;
20-
import net.minecraft.util.text.TextFormatting;
2120
import net.minecraftforge.common.MinecraftForge;
2221
import org.jetbrains.annotations.NotNull;
2322
import org.jetbrains.annotations.Nullable;
@@ -112,14 +111,14 @@ public int getRequiredPermissionLevel() {
112111
protected void print(EntityPlayer player, List<ITextComponent> messages, List<String> argList) {
113112
if (messages.isEmpty()) {
114113
if (argList.isEmpty()) {
115-
player.sendMessage(new TextComponentString(String.format("Couldn't find %s!", targetDescription())).setStyle(new Style().setColor(TextFormatting.RED)));
114+
player.sendMessage(new TextComponentString(String.format("Couldn't find %s!", targetDescription())).setStyle(StyleConstant.getErrorStyle()));
116115
} else {
117-
player.sendMessage(new TextComponentString(String.format("Couldn't find %s matching the given arguments!", targetDescription())).setStyle(new Style().setColor(TextFormatting.RED)));
116+
player.sendMessage(new TextComponentString(String.format("Couldn't find %s matching the given arguments!", targetDescription())).setStyle(StyleConstant.getErrorStyle()));
118117
player.sendMessage(new TextComponentString("The following arguments were provided: " + String.join(", ", argList)));
119118
}
120119
} else {
121120
// have a horizontal bar to improve readability when running multiple consecutive info commands
122-
player.sendMessage(new TextComponentString("================================").setStyle(new Style().setColor(TextFormatting.GOLD)));
121+
player.sendMessage(new TextComponentString("================================").setStyle(StyleConstant.getEmphasisStyle()));
123122
messages.forEach(player::sendMessage);
124123
}
125124
}

src/main/java/com/cleanroommc/groovyscript/command/GSCommand.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.cleanroommc.groovyscript.compat.mods.ModSupport;
66
import com.cleanroommc.groovyscript.compat.mods.jei.JeiPlugin;
77
import com.cleanroommc.groovyscript.documentation.Documentation;
8+
import com.cleanroommc.groovyscript.helper.StyleConstant;
89
import com.cleanroommc.groovyscript.network.NetworkHandler;
910
import com.cleanroommc.groovyscript.network.SReloadScripts;
1011
import com.cleanroommc.groovyscript.network.StartLanguageServerPacket;
@@ -15,9 +16,7 @@
1516
import net.minecraft.entity.player.EntityPlayerMP;
1617
import net.minecraft.server.MinecraftServer;
1718
import net.minecraft.util.text.ITextComponent;
18-
import net.minecraft.util.text.Style;
1919
import net.minecraft.util.text.TextComponentString;
20-
import net.minecraft.util.text.TextFormatting;
2120
import net.minecraft.util.text.event.ClickEvent;
2221
import net.minecraft.util.text.event.HoverEvent;
2322
import net.minecraftforge.server.command.CommandTreeBase;
@@ -59,17 +58,8 @@ public GSCommand() {
5958
addSubcommand(new InfoLookingCommand());
6059
addSubcommand(new InfoSelfCommand());
6160

62-
addSubcommand(
63-
new SimpleCommand(
64-
"wiki",
65-
(server, sender, args) -> sender.sendMessage(
66-
new TextComponentString("GroovyScript wiki").setStyle(
67-
new Style().setColor(TextFormatting.GOLD)
68-
.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString("Click to open wiki in browser")))
69-
.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://cleanroommc.com/groovy-script/")))),
70-
"doc",
71-
"docs",
72-
"documentation"));
61+
62+
addSubcommand(new SimpleCommand("wiki", (server, sender, args) -> sender.sendMessage(getTextForUrl("GroovyScript wiki", "Click to open wiki in browser", new TextComponentString("https://cleanroommc.com/groovy-script/"))), "doc", "docs", "documentation"));
7363

7464
addSubcommand(new SimpleCommand("generateWiki", (server, sender, args) -> {
7565
Documentation.generateWiki();
@@ -106,9 +96,9 @@ public GSCommand() {
10696

10797
addSubcommand(new SimpleCommand("deleteScriptCache", (server, sender, args) -> {
10898
if (GroovyScript.getSandbox().deleteScriptCache()) {
109-
sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "Deleted groovy script cache"));
99+
sender.sendMessage(new TextComponentString("Deleted groovy script cache").setStyle(StyleConstant.getSuccessStyle()));
110100
} else {
111-
sender.sendMessage(new TextComponentString(TextFormatting.RED + "An error occurred while deleting groovy script cache"));
101+
sender.sendMessage(new TextComponentString("An error occurred while deleting groovy script cache").setStyle(StyleConstant.getErrorStyle()));
112102
}
113103
}));
114104

@@ -120,7 +110,7 @@ public GSCommand() {
120110

121111
addSubcommand(new SimpleCommand("cleanLog", (server, sender, args) -> {
122112
GroovyLogImpl.LOG.cleanLog();
123-
sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "Cleaned Groovy log"));
113+
sender.sendMessage(new TextComponentString("Cleaned Groovy log").setStyle(StyleConstant.getSuccessStyle()));
124114
}));
125115

126116
if (ModSupport.MEKANISM.isLoaded()) {
@@ -148,8 +138,19 @@ public static void postLogFiles(ICommandSender sender) {
148138
}
149139

150140
public static ITextComponent getTextForFile(String name, String path, ITextComponent hoverText) {
151-
return new TextComponentString(TextFormatting.UNDERLINE + (TextFormatting.GOLD + name))
152-
.setStyle(new Style().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_FILE, path)).setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText)));
141+
return getTextForClickEvent(name, new ClickEvent(ClickEvent.Action.OPEN_FILE, path), hoverText);
142+
}
143+
144+
public static ITextComponent getTextForUrl(String name, String url, ITextComponent hoverText) {
145+
return getTextForClickEvent(name, new ClickEvent(ClickEvent.Action.OPEN_URL, url), hoverText);
146+
}
147+
148+
public static ITextComponent getTextForClickEvent(String name, ClickEvent clickEvent, ITextComponent hoverText) {
149+
return new TextComponentString(name).setStyle(
150+
StyleConstant.getEmphasisStyle()
151+
.setUnderlined(true)
152+
.setClickEvent(clickEvent)
153+
.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText)));
153154
}
154155

155156
public static boolean hasArgument(String[] args, String arg) {

src/main/java/com/cleanroommc/groovyscript/command/GSMekanismCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.cleanroommc.groovyscript.compat.mods.ModSupport;
44
import com.cleanroommc.groovyscript.compat.mods.mekanism.Mekanism;
5+
import com.cleanroommc.groovyscript.helper.StyleConstant;
56
import mekanism.api.gas.Gas;
67
import mekanism.api.gas.GasRegistry;
78
import mekanism.api.infuse.InfuseRegistry;
@@ -10,7 +11,6 @@
1011
import net.minecraft.command.ICommandSender;
1112
import net.minecraft.server.MinecraftServer;
1213
import net.minecraft.util.text.TextComponentString;
13-
import net.minecraft.util.text.TextFormatting;
1414
import net.minecraftforge.server.command.CommandTreeBase;
1515
import org.jetbrains.annotations.NotNull;
1616

@@ -24,22 +24,22 @@ public GSMekanismCommand() {
2424
sender.sendMessage(new TextComponentString("Mekanism gases:"));
2525
for (Gas gas : GasRegistry.getRegisteredGasses()) {
2626
String copyText = Mekanism.asGroovyCode(gas, true);
27-
sender.sendMessage(TextCopyable.string(copyText, " - " + gas.getName()).build());
27+
sender.sendMessage(TextCopyable.string(copyText, " - " + copyText).build());
2828
}
2929
}, "gases"));
3030
addSubcommand(new SimpleCommand("infusionTypes", (server, sender, args) -> {
3131
sender.sendMessage(new TextComponentString("Mekanism infusion types:"));
3232
for (InfuseType infuseType : InfuseRegistry.getInfuseMap().values()) {
33-
String copyText = "'" + infuseType.name + "'";
34-
sender.sendMessage(TextCopyable.string(copyText, " - " + infuseType.name).build());
33+
String copyText = Mekanism.asGroovyCode(infuseType, true);
34+
sender.sendMessage(TextCopyable.string(copyText, " - " + copyText).build());
3535
}
3636
}));
3737
}
3838

3939
@Override
4040
public void execute(@NotNull MinecraftServer server, @NotNull ICommandSender sender, String @NotNull [] args) throws CommandException {
4141
if (!ModSupport.MEKANISM.isLoaded()) {
42-
sender.sendMessage(new TextComponentString(TextFormatting.RED + "Mekanism is not loaded!"));
42+
sender.sendMessage(new TextComponentString("Mekanism is not loaded!").setStyle(StyleConstant.getErrorStyle()));
4343
return;
4444
}
4545
super.execute(server, sender, args);

src/main/java/com/cleanroommc/groovyscript/command/TextCopyable.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
package com.cleanroommc.groovyscript.command;
22

3+
import com.cleanroommc.groovyscript.helper.StyleConstant;
34
import net.minecraft.util.text.*;
45
import net.minecraft.util.text.event.HoverEvent;
56

67
public class TextCopyable {
78

8-
public static Builder string(java.lang.String copyText, java.lang.String msg) {
9+
public static Builder string(String copyText, String msg) {
910
return new Builder(copyText, msg);
1011
}
1112

12-
public static Builder translation(java.lang.String copyText, java.lang.String msg, Object... args) {
13+
public static Builder translation(String copyText, String msg, Object... args) {
1314
return new Builder(copyText, msg).translate(args);
1415
}
1516

1617
public static class Builder {
1718

18-
private final java.lang.String copyText;
19-
private final java.lang.String msg;
19+
private final String copyText;
20+
private final String msg;
2021
private Object[] args;
2122
private ITextComponent hoverMsg;
2223

23-
public Builder(java.lang.String copyText, java.lang.String msg) {
24-
this.copyText = TextFormatting.getTextWithoutFormattingCodes(copyText);
24+
public Builder(String copyText, String msg) {
25+
this.copyText = copyText;
2526
this.msg = msg;
2627
}
2728

@@ -33,16 +34,12 @@ public Builder translate(Object... args) {
3334
public ITextComponent build() {
3435
Style style = new Style();
3536
if (hoverMsg == null) {
36-
hoverMsg = new TextComponentTranslation("groovyscript.command.copy.hover")
37-
.appendSibling(new TextComponentString(" " + copyText).setStyle(new Style().setColor(TextFormatting.GOLD)));
37+
hoverMsg = new TextComponentTranslation("groovyscript.command.copy.hover").setStyle(StyleConstant.getEmphasisStyle())
38+
.appendSibling(new TextComponentString(" " + copyText));
3839
}
3940
style.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverMsg));
40-
style.setClickEvent(CustomClickAction.makeCopyEvent(copyText));
41-
ITextComponent textComponent;
42-
if (args == null)
43-
textComponent = new TextComponentString(msg);
44-
else
45-
textComponent = new TextComponentTranslation(msg, args);
41+
style.setClickEvent(CustomClickAction.makeCopyEvent(TextFormatting.getTextWithoutFormattingCodes(copyText)));
42+
ITextComponent textComponent = args == null ? new TextComponentString(msg) : new TextComponentTranslation(msg, args);
4643
textComponent.setStyle(style);
4744
return textComponent;
4845
}

src/main/java/com/cleanroommc/groovyscript/compat/mods/jei/InfoParserTab.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import com.cleanroommc.groovyscript.api.infocommand.InfoParserPackage;
44
import com.cleanroommc.groovyscript.compat.vanilla.command.infoparser.GenericInfoParser;
55
import com.cleanroommc.groovyscript.core.mixin.jei.ModRegistryAccessor;
6+
import com.cleanroommc.groovyscript.helper.StyleConstant;
67
import mezz.jei.api.recipe.IRecipeCategory;
78
import net.minecraft.item.ItemStack;
8-
import net.minecraft.util.text.TextFormatting;
9+
import net.minecraftforge.oredict.OreDictionary;
910
import org.jetbrains.annotations.NotNull;
1011

1112
import java.util.List;
@@ -29,7 +30,7 @@ public String name() {
2930

3031
@Override
3132
public String text(@NotNull IRecipeCategory entry, boolean colored, boolean prettyNbt) {
32-
return colored ? TextFormatting.YELLOW + entry.getUid() : entry.getUid();
33+
return colored ? StyleConstant.STRING + entry.getUid() : entry.getUid();
3334
}
3435

3536
@Override
@@ -41,7 +42,7 @@ public void parse(InfoParserPackage info) {
4142
List<String> allowed = ((ModRegistryAccessor) JeiPlugin.modRegistry).getRecipeCatalysts()
4243
.entrySet()
4344
.stream()
44-
.filter(entry -> entry.getValue().stream().anyMatch(x -> x instanceof ItemStack stack && ItemStack.areItemStacksEqual(stack, info.getStack())))
45+
.filter(entry -> entry.getValue().stream().anyMatch(x -> x instanceof ItemStack stack && OreDictionary.itemMatches(stack, info.getStack(), false)))
4546
.map(Map.Entry::getKey)
4647
.collect(Collectors.toList());
4748
List<IRecipeCategory> list = JeiPlugin.recipeRegistry.getRecipeCategories(allowed);

0 commit comments

Comments
 (0)