Skip to content

Commit 66ec7ac

Browse files
authored
Fix info command copying formatting (#305)
* fix formatting chars being copied to hand * disable jei tab info on server * sort info on addition, add javadocs
1 parent 464da11 commit 66ec7ac

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public interface InfoParser {
1616
/**
1717
* Priority of the parser for determining the order they are logged in chat,
1818
* with lowest being first and highest being last.
19-
* The is 100, and {@link com.cleanroommc.groovyscript.compat.vanilla.command.infoparser.InfoParserItem#priority()} is set to 1.
19+
* The default for {@link com.cleanroommc.groovyscript.compat.vanilla.command.infoparser.GenericInfoParser GenericInfoParser} 100,
20+
* and {@link com.cleanroommc.groovyscript.compat.vanilla.command.infoparser.InfoParserItem#priority() InfoParserItem.priority()} is set to 1.
2021
*
2122
* @return the priority of the Parser
2223
*/
Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11
package com.cleanroommc.groovyscript.api.infocommand;
22

3+
import com.google.common.collect.ImmutableList;
4+
35
import java.util.ArrayList;
46
import java.util.Comparator;
57
import java.util.List;
6-
import java.util.stream.Collectors;
78

9+
/**
10+
* To help gather information about items, blocks, the world, etc.
11+
* GroovyScript adds {@link InfoParser}s, which will provide information to the player
12+
* using data from an {@link InfoParserPackage}.
13+
* <p>
14+
* This is currently only used for commands related to {@link com.cleanroommc.groovyscript.command.BaseInfoCommand BaseInfoCommand}.
15+
*
16+
* @see com.cleanroommc.groovyscript.compat.vanilla.command.infoparser.StandardInfoParserRegistry StandardInfoParserRegistry
17+
*/
818
public class InfoParserRegistry {
919

1020
private static final List<InfoParser> INFO_PARSERS = new ArrayList<>();
21+
private static final List<String> IDS = new ArrayList<>();
1122

12-
public static void addInfoParser(InfoParser command) {
13-
INFO_PARSERS.add(command);
23+
/**
24+
* Register the given info parser.
25+
*/
26+
public static void addInfoParser(InfoParser parser) {
27+
INFO_PARSERS.add(parser);
28+
INFO_PARSERS.sort(Comparator.comparing(InfoParser::priority));
1429
}
1530

31+
/**
32+
* @return all registered info parsers
33+
*/
1634
public static List<InfoParser> getInfoParsers() {
17-
return INFO_PARSERS.stream().sorted(Comparator.comparing(InfoParser::priority)).collect(Collectors.toList());
35+
return ImmutableList.copyOf(INFO_PARSERS);
1836
}
1937

38+
/**
39+
* @return the ids of all registered info parsers
40+
*/
2041
public static List<String> getIds() {
21-
return INFO_PARSERS.stream().sorted(Comparator.comparing(InfoParser::priority)).map(InfoParser::id).collect(Collectors.toList());
42+
// generate the list
43+
if (IDS.size() != INFO_PARSERS.size()) {
44+
IDS.clear();
45+
for (var parser : INFO_PARSERS) {
46+
IDS.add(parser.id());
47+
}
48+
}
49+
return ImmutableList.copyOf(IDS);
2250
}
2351
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.cleanroommc.groovyscript.compat.mods.jei;
22

3+
import com.cleanroommc.groovyscript.api.GroovyLog;
34
import com.cleanroommc.groovyscript.api.infocommand.InfoParserPackage;
45
import com.cleanroommc.groovyscript.compat.vanilla.command.infoparser.GenericInfoParser;
56
import com.cleanroommc.groovyscript.core.mixin.jei.ModRegistryAccessor;
67
import com.cleanroommc.groovyscript.helper.StyleConstant;
78
import mezz.jei.api.recipe.IRecipeCategory;
89
import net.minecraft.item.ItemStack;
10+
import net.minecraftforge.fml.common.FMLCommonHandler;
911
import net.minecraftforge.oredict.OreDictionary;
1012
import org.jetbrains.annotations.NotNull;
1113

@@ -36,6 +38,11 @@ public String text(@NotNull IRecipeCategory entry, boolean colored, boolean pret
3638
@Override
3739
@SuppressWarnings("rawtypes")
3840
public void parse(InfoParserPackage info) {
41+
// only runs client-side
42+
if (!FMLCommonHandler.instance().getSide().isClient()) {
43+
GroovyLog.get().debug("Attempted to check the JEI tab via info parser server-side");
44+
return;
45+
}
3946
if (info.getStack().isEmpty()) return;
4047

4148
// this gets all categories the item appears on - and there isn't any inbuilt method to get *just* catalysts.

src/main/java/com/cleanroommc/groovyscript/compat/vanilla/command/infoparser/InfoParserItem.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.minecraft.client.gui.GuiScreen;
66
import net.minecraft.item.ItemStack;
77
import net.minecraft.util.text.ITextComponent;
8+
import net.minecraftforge.fml.common.FMLCommonHandler;
89
import org.jetbrains.annotations.NotNull;
910

1011
import java.util.Iterator;
@@ -14,6 +15,10 @@ public class InfoParserItem extends GenericInfoParser<ItemStack> {
1415

1516
public static final InfoParserItem instance = new InfoParserItem();
1617

18+
private static void copyToClipboard(String text) {
19+
GuiScreen.setClipboardString(text);
20+
}
21+
1722
@Override
1823
public int priority() {
1924
return 1;
@@ -39,7 +44,8 @@ public void iterate(List<ITextComponent> messages, @NotNull Iterator<ItemStack>
3944
if (entries.hasNext()) {
4045
ItemStack entry = entries.next();
4146
messages.add(information(entry, prettyNbt));
42-
GuiScreen.setClipboardString(copyText(entry, prettyNbt));
47+
// can only copy to clipboard if a client is running this
48+
if (FMLCommonHandler.instance().getSide().isClient()) copyToClipboard(copyText(entry, false));
4349
}
4450
while (entries.hasNext()) {
4551
messages.add(information(entries.next(), prettyNbt));

0 commit comments

Comments
 (0)