Skip to content

Commit 598d953

Browse files
committed
Move some component utility methods to CComponentUtil
1 parent c216d7f commit 598d953

16 files changed

+129
-109
lines changed

src/main/java/net/earthcomputer/clientcommands/command/CEnchantCommand.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.earthcomputer.clientcommands.features.EnchantmentCracker;
88
import net.earthcomputer.clientcommands.features.LegacyEnchantment;
99
import net.earthcomputer.clientcommands.features.PlayerRandCracker;
10+
import net.earthcomputer.clientcommands.util.CComponentUtil;
1011
import net.earthcomputer.clientcommands.util.MultiVersionCompat;
1112
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
1213
import net.minecraft.ChatFormatting;
@@ -58,15 +59,15 @@ private static int cenchant(FabricClientCommandSource source, ItemAndEnchantment
5859
Component component = Component.translatable("commands.cenchant.needEnchantingPrediction")
5960
.withStyle(ChatFormatting.RED)
6061
.append(" ")
61-
.append(getCommandTextComponent("commands.client.enable", "/cconfig clientcommands enchantingPrediction set true"));
62+
.append(CComponentUtil.getCommandTextComponent("commands.client.enable", "/cconfig clientcommands enchantingPrediction set true"));
6263
source.sendFeedback(component);
6364
return Command.SINGLE_SUCCESS;
6465
}
6566
if (!Configs.playerCrackState.knowsSeed() && Configs.enchCrackState != EnchantmentCracker.CrackState.CRACKED) {
6667
Component component = Component.translatable("commands.cenchant.uncracked")
6768
.withStyle(ChatFormatting.RED)
6869
.append(" ")
69-
.append(getCommandTextComponent("commands.client.crack", "/ccrackrng"));
70+
.append(CComponentUtil.getCommandTextComponent("commands.client.crack", "/ccrackrng"));
7071
source.sendFeedback(component);
7172
return Command.SINGLE_SUCCESS;
7273
}
@@ -88,7 +89,7 @@ private static int cenchant(FabricClientCommandSource source, ItemAndEnchantment
8889
if (Configs.playerCrackState != PlayerRandCracker.CrackState.CRACKED) {
8990
MutableComponent help = Component.translatable("commands.cenchant.help.uncrackedPlayerSeed")
9091
.append(" ")
91-
.append(getCommandTextComponent("commands.client.crack", "/ccrackrng"));
92+
.append(CComponentUtil.getCommandTextComponent("commands.client.crack", "/ccrackrng"));
9293
sendHelp(help);
9394
}
9495
} else {
@@ -111,7 +112,7 @@ private static int cenchant(FabricClientCommandSource source, ItemAndEnchantment
111112

112113
source.sendFeedback(Component.translatable("commands.cenchant.success")
113114
.append(" ")
114-
.append(getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName)));
115+
.append(CComponentUtil.getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName)));
115116

116117
return Command.SINGLE_SUCCESS;
117118
}

src/main/java/net/earthcomputer/clientcommands/command/CallbackCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.mojang.brigadier.CommandDispatcher;
55
import com.mojang.brigadier.exceptions.CommandSyntaxException;
66
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
7+
import net.earthcomputer.clientcommands.util.CComponentUtil;
78
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
89
import net.minecraft.network.chat.Component;
910

@@ -22,7 +23,7 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
2223
}
2324

2425
private static int runCallback(UUID uuid) throws CommandSyntaxException {
25-
if (!ClientCommandHelper.runCallback(uuid)) {
26+
if (!CComponentUtil.runCallback(uuid)) {
2627
throw NO_SUCH_CALLBACK_EXCEPTION.create();
2728
}
2829

src/main/java/net/earthcomputer/clientcommands/command/ClientCommandHelper.java

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,9 @@
77
import net.minecraft.ChatFormatting;
88
import net.minecraft.client.Minecraft;
99
import net.minecraft.client.gui.Gui;
10-
import net.minecraft.core.BlockPos;
11-
import net.minecraft.network.chat.ClickEvent;
1210
import net.minecraft.network.chat.Component;
13-
import net.minecraft.network.chat.HoverEvent;
14-
import net.minecraft.network.chat.MutableComponent;
15-
import net.minecraft.world.entity.Entity;
16-
17-
import java.util.Map;
18-
import java.util.UUID;
19-
import java.util.concurrent.ConcurrentHashMap;
2011

2112
public class ClientCommandHelper {
22-
2313
public static <T> T getFlag(CommandContext<FabricClientCommandSource> ctx, Flag<T> flag) {
2414
return getFlag(Flag.getActualSource(ctx), flag);
2515
}
@@ -57,75 +47,4 @@ public static void addOverlayMessage(Component message, int time) {
5747
gui.setOverlayMessage(message, false);
5848
gui.overlayMessageTime = time;
5949
}
60-
61-
public static Component getLookCoordsTextComponent(BlockPos pos) {
62-
return getCommandTextComponent(Component.translatable("commands.client.blockpos", pos.getX(), pos.getY(), pos.getZ()),
63-
String.format("/clook block %d %d %d", pos.getX(), pos.getY(), pos.getZ()));
64-
}
65-
66-
public static Component getLookCoordsTextComponent(MutableComponent component, BlockPos pos) {
67-
return getCommandTextComponent(component, String.format("/clook block %d %d %d", pos.getX(), pos.getY(), pos.getZ()));
68-
}
69-
70-
public static Component getGlowCoordsTextComponent(BlockPos pos) {
71-
return getCommandTextComponent(Component.translatable("commands.client.blockpos", pos.getX(), pos.getY(), pos.getZ()),
72-
String.format("/cglow block %d %d %d 10", pos.getX(), pos.getY(), pos.getZ()));
73-
}
74-
75-
public static Component getGlowButtonTextComponent(BlockPos pos) {
76-
return getCommandTextComponent(Component.translatable("commands.client.glow"), String.format("/cglow block %d %d %d 60", pos.getX(), pos.getY(), pos.getZ()));
77-
}
78-
79-
public static Component getGlowButtonTextComponent(Entity entity) {
80-
return getCommandTextComponent(Component.translatable("commands.client.glow"), "/cglow entities " + entity.getStringUUID());
81-
}
82-
83-
public static Component getCommandTextComponent(@Translatable String translationKey, String command) {
84-
return getCommandTextComponent(Component.translatable(translationKey), command);
85-
}
86-
87-
public static Component getCommandTextComponent(MutableComponent component, String command) {
88-
return component.withStyle(style -> style.applyFormat(ChatFormatting.UNDERLINE)
89-
.withClickEvent(new ClickEvent.RunCommand(command))
90-
.withHoverEvent(new HoverEvent.ShowText(Component.literal(command))));
91-
}
92-
93-
private static final Map<UUID, Callback> callbacks = new ConcurrentHashMap<>();
94-
95-
public static ClickEvent callbackClickEvent(Runnable runnable) {
96-
return callbackClickEvent(runnable, 60_000_000_000L); // 1 minute timeout
97-
}
98-
99-
public static ClickEvent callbackClickEvent(Runnable callback, long timeoutNanos) {
100-
UUID callbackId = UUID.randomUUID();
101-
callbacks.put(callbackId, new Callback(callback, System.nanoTime() + timeoutNanos));
102-
return new ClickEvent.RunCommand("/ccallback " + callbackId);
103-
}
104-
105-
public static boolean runCallback(UUID callbackId) {
106-
Callback callback = callbacks.get(callbackId);
107-
if (callback == null) {
108-
return false;
109-
}
110-
callback.callback.run();
111-
return true;
112-
}
113-
114-
private record Callback(Runnable callback, long timeout) {
115-
static {
116-
Thread.ofVirtual().name("Clientcommands callback cleanup").start(() -> {
117-
while (true) {
118-
try {
119-
// This isn't really "busy-waiting" since the wait time is so long
120-
//noinspection BusyWait
121-
Thread.sleep(1000);
122-
} catch (InterruptedException e) {
123-
break;
124-
}
125-
long now = System.nanoTime();
126-
callbacks.values().removeIf(callback -> now - callback.timeout <= 0);
127-
}
128-
});
129-
}
130-
}
13150
}

src/main/java/net/earthcomputer/clientcommands/command/FindBlockCommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.earthcomputer.clientcommands.command.arguments.ClientBlockPredicateArgument;
88
import net.earthcomputer.clientcommands.task.RenderDistanceScanTask;
99
import net.earthcomputer.clientcommands.task.TaskManager;
10+
import net.earthcomputer.clientcommands.util.CComponentUtil;
1011
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
1112
import net.minecraft.client.Minecraft;
1213
import net.minecraft.client.multiplayer.ClientLevel;
@@ -92,9 +93,9 @@ public void onCompleted() {
9293
Component.translatable(
9394
"commands.cfindblock.success",
9495
Component.empty()
95-
.append(getLookCoordsTextComponent(closestBlock))
96+
.append(CComponentUtil.getLookCoordsTextComponent(closestBlock))
9697
.append(" ")
97-
.append(getGlowButtonTextComponent(closestBlock)),
98+
.append(CComponentUtil.getGlowButtonTextComponent(closestBlock)),
9899
foundRadius
99100
)
100101
);

src/main/java/net/earthcomputer/clientcommands/command/FindCommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import dev.xpple.clientarguments.arguments.CEntitySelector;
88
import net.earthcomputer.clientcommands.task.LongTask;
99
import net.earthcomputer.clientcommands.task.TaskManager;
10+
import net.earthcomputer.clientcommands.util.CComponentUtil;
1011
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
1112
import net.minecraft.ChatFormatting;
1213
import net.minecraft.client.Minecraft;
@@ -42,7 +43,7 @@ private static int listEntities(FabricClientCommandSource source, CEntitySelecto
4243

4344
source.sendFeedback(Component.translatable("commands.cfind.keepSearching.success")
4445
.append(" ")
45-
.append(getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName)));
46+
.append(CComponentUtil.getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName)));
4647

4748
return Command.SINGLE_SUCCESS;
4849
} else {
@@ -66,7 +67,7 @@ private static void sendEntityFoundMessage(FabricClientCommandSource source, Ent
6667
source.sendFeedback(Component.translatable(
6768
"commands.cfind.found",
6869
entity.getName(),
69-
getLookCoordsTextComponent(entity.blockPosition()),
70+
CComponentUtil.getLookCoordsTextComponent(entity.blockPosition()),
7071
distance
7172
));
7273
}

src/main/java/net/earthcomputer/clientcommands/command/FindItemCommand.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.mojang.datafixers.util.Either;
88
import net.earthcomputer.clientcommands.features.ClientcommandsDataQueryHandler;
99
import net.earthcomputer.clientcommands.util.CUtil;
10+
import net.earthcomputer.clientcommands.util.CComponentUtil;
1011
import net.earthcomputer.clientcommands.util.GuiBlocker;
1112
import net.earthcomputer.clientcommands.util.MathUtil;
1213
import net.earthcomputer.clientcommands.command.arguments.WithStringArgument;
@@ -97,7 +98,7 @@ private static int findItem(CommandContext<FabricClientCommandSource> ctx, boole
9798
if (keepSearching) {
9899
ctx.getSource().sendFeedback(Component.translatable("commands.cfinditem.starting.keepSearching", item.string())
99100
.append(" ")
100-
.append(getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName)));
101+
.append(CComponentUtil.getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName)));
101102
} else {
102103
ctx.getSource().sendFeedback(Component.translatable("commands.cfinditem.starting", item.string()));
103104
}
@@ -192,17 +193,17 @@ protected void printEntityLocation(Entity entity, int count) {
192193
count,
193194
searchingForName,
194195
entity.getName(),
195-
getLookCoordsTextComponent(BlockPos.containing(entity.position()))
196+
CComponentUtil.getLookCoordsTextComponent(BlockPos.containing(entity.position()))
196197
)
197198
.append(" ")
198-
.append(getGlowButtonTextComponent(entity))
199+
.append(CComponentUtil.getGlowButtonTextComponent(entity))
199200
);
200201
}
201202

202203
protected void printLocation(BlockPos pos, int count) {
203-
sendFeedback(Component.translatable("commands.cfinditem.match", count, searchingForName, getLookCoordsTextComponent(pos))
204+
sendFeedback(Component.translatable("commands.cfinditem.match", count, searchingForName, CComponentUtil.getLookCoordsTextComponent(pos))
204205
.append(" ")
205-
.append(getGlowButtonTextComponent(pos)));
206+
.append(CComponentUtil.getGlowButtonTextComponent(pos)));
206207
}
207208

208209
protected boolean canSearchEntity(Entity entity) {

src/main/java/net/earthcomputer/clientcommands/command/FishCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.mojang.brigadier.exceptions.CommandSyntaxException;
66
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
77
import net.earthcomputer.clientcommands.Configs;
8+
import net.earthcomputer.clientcommands.util.CComponentUtil;
89
import net.earthcomputer.clientcommands.util.MultiVersionCompat;
910
import net.earthcomputer.clientcommands.command.arguments.ClientItemPredicateArgument;
1011
import net.earthcomputer.clientcommands.command.arguments.WithStringArgument;
@@ -20,7 +21,6 @@
2021
import java.util.Set;
2122

2223
import static com.mojang.brigadier.arguments.IntegerArgumentType.*;
23-
import static net.earthcomputer.clientcommands.command.ClientCommandHelper.*;
2424
import static net.earthcomputer.clientcommands.command.arguments.ClientItemPredicateArgument.*;
2525
import static net.earthcomputer.clientcommands.command.arguments.ItemAndEnchantmentsPredicateArgument.*;
2626
import static net.earthcomputer.clientcommands.command.arguments.WithStringArgument.*;
@@ -34,7 +34,7 @@ public class FishCommand {
3434
Component.translatable("commands.cfish.needFishingManipulation")
3535
.withStyle(style -> style.withColor(ChatFormatting.RED))
3636
.append(" ")
37-
.append(getCommandTextComponent("commands.client.enable", "/cconfig clientcommands fishingManipulation set MANUAL")));
37+
.append(CComponentUtil.getCommandTextComponent("commands.client.enable", "/cconfig clientcommands fishingManipulation set MANUAL")));
3838

3939
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandBuildContext context) {
4040
if (MultiVersionCompat.INSTANCE.getProtocolVersion() >= MultiVersionCompat.V1_20) {

src/main/java/net/earthcomputer/clientcommands/command/GlowCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.earthcomputer.clientcommands.render.RenderQueue;
1010
import net.earthcomputer.clientcommands.task.SimpleTask;
1111
import net.earthcomputer.clientcommands.task.TaskManager;
12+
import net.earthcomputer.clientcommands.util.CComponentUtil;
1213
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
1314
import net.minecraft.client.Minecraft;
1415
import net.minecraft.client.player.LocalPlayer;
@@ -99,7 +100,7 @@ protected void onTick() {
99100

100101
source.sendFeedback(Component.translatable("commands.cglow.entity.keepSearching.success")
101102
.append(" ")
102-
.append(getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName)));
103+
.append(CComponentUtil.getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName)));
103104

104105
return Command.SINGLE_SUCCESS;
105106
} else {

src/main/java/net/earthcomputer/clientcommands/command/PosCommand.java

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

33
import com.mojang.brigadier.Command;
44
import com.mojang.brigadier.CommandDispatcher;
5+
import net.earthcomputer.clientcommands.util.CComponentUtil;
56
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
67
import net.minecraft.client.resources.language.I18n;
78
import net.minecraft.core.BlockPos;
@@ -13,7 +14,6 @@
1314

1415
import static dev.xpple.clientarguments.arguments.CBlockPosArgument.*;
1516
import static dev.xpple.clientarguments.arguments.CDimensionArgument.*;
16-
import static net.earthcomputer.clientcommands.command.ClientCommandHelper.*;
1717
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*;
1818

1919
public class PosCommand {
@@ -84,7 +84,7 @@ private static int convertCoords(FabricClientCommandSource source, BlockPos pos,
8484
"commands.cpos.coords",
8585
getCoordsTextComponent(pos),
8686
sourceWorldName,
87-
getLookCoordsTextComponent(targetPos),
87+
CComponentUtil.getLookCoordsTextComponent(targetPos),
8888
targetWorldName)
8989
);
9090

src/main/java/net/earthcomputer/clientcommands/command/PredictBrushablesCommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.seedfinding.mcfeature.loot.item.ItemStack;
1111
import net.earthcomputer.clientcommands.task.RenderDistanceScanTask;
1212
import net.earthcomputer.clientcommands.task.TaskManager;
13+
import net.earthcomputer.clientcommands.util.CComponentUtil;
1314
import net.earthcomputer.clientcommands.util.SeedfindingUtil;
1415
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
1516
import net.minecraft.SharedConstants;
@@ -57,7 +58,7 @@ private static int predictBrushables(CommandContext<FabricClientCommandSource> c
5758
boolean keepSearching = getFlag(ctx, FLAG_KEEP_SEARCHING);
5859
String taskName = TaskManager.addTask("cpredictbrushables", new PredictBrushablesTask(keepSearching));
5960
if (keepSearching) {
60-
sendFeedback(Component.translatable("commands.cpredictbrushables.starting.keepSearching", getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName)));
61+
sendFeedback(Component.translatable("commands.cpredictbrushables.starting.keepSearching", CComponentUtil.getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName)));
6162
} else {
6263
sendFeedback(Component.translatable("commands.cpredictbrushables.starting"));
6364
}
@@ -127,7 +128,7 @@ protected void scanBlock(Entity cameraEntity, BlockPos pos) {
127128
for (ItemStack item : items) {
128129
var mcItemStack = SeedfindingUtil.fromSeedfindingItem(item, level.registryAccess());
129130
mcItemStack.set(DataComponents.LORE, getItemLore(mcItemStack));
130-
ClientCommandHelper.sendFeedback(Component.translatable("commands.cpredictbrushables.foundBrushableBlock", blockState.getBlock().getName(), ClientCommandHelper.getLookCoordsTextComponent(pos), mcItemStack.getDisplayName(), ClientCommandHelper.getGlowButtonTextComponent(pos)));
131+
ClientCommandHelper.sendFeedback(Component.translatable("commands.cpredictbrushables.foundBrushableBlock", blockState.getBlock().getName(), CComponentUtil.getLookCoordsTextComponent(pos), mcItemStack.getDisplayName(), CComponentUtil.getGlowButtonTextComponent(pos)));
131132
}
132133

133134
found = true;

0 commit comments

Comments
 (0)