-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathClientCommandHelper.java
More file actions
119 lines (96 loc) · 5.41 KB
/
ClientCommandHelper.java
File metadata and controls
119 lines (96 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package net.earthcomputer.clientcommands.command;
import com.demonwav.mcdev.annotations.Translatable;
import com.mojang.brigadier.context.CommandContext;
import net.earthcomputer.clientcommands.interfaces.IClientSuggestionsProvider;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class ClientCommandHelper {
public static <T> T getFlag(CommandContext<FabricClientCommandSource> ctx, Flag<T> flag) {
return getFlag(Flag.getActualSource(ctx), flag);
}
public static <T> T getFlag(FabricClientCommandSource source, Flag<T> flag) {
return ((IClientSuggestionsProvider) source).clientcommands_getFlag(flag);
}
public static <T> FabricClientCommandSource withFlag(FabricClientCommandSource source, Flag<T> flag, T value) {
return (FabricClientCommandSource) ((IClientSuggestionsProvider) source).clientcommands_withFlag(flag, value);
}
public static void sendError(Component error) {
sendFeedback(Component.literal("").append(error).withStyle(ChatFormatting.RED));
}
public static void sendHelp(Component help) {
sendFeedback(Component.literal("").append(help).withStyle(ChatFormatting.AQUA));
}
public static void sendFeedback(@Translatable String message, Object... args) {
sendFeedback(Component.translatable(message, args));
}
public static void sendFeedback(Component message) {
Minecraft.getInstance().gui.getChat().addMessage(message);
}
public static void sendRequiresRestart() {
sendFeedback(Component.translatable("commands.client.requiresRestart").withStyle(ChatFormatting.YELLOW));
}
public static void addOverlayMessage(Component message, int time) {
Gui gui = Minecraft.getInstance().gui;
gui.setOverlayMessage(message, false);
gui.overlayMessageTime = time;
}
public static Component getLookCoordsTextComponent(BlockPos pos) {
return getCommandTextComponent(Component.translatable("commands.client.blockpos", pos.getX(), pos.getY(), pos.getZ()),
String.format("/clook block %d %d %d", pos.getX(), pos.getY(), pos.getZ()));
}
public static Component getLookCoordsTextComponent(MutableComponent component, BlockPos pos) {
return getCommandTextComponent(component, String.format("/clook block %d %d %d", pos.getX(), pos.getY(), pos.getZ()));
}
public static Component getGlowCoordsTextComponent(BlockPos pos) {
return getCommandTextComponent(Component.translatable("commands.client.blockpos", pos.getX(), pos.getY(), pos.getZ()),
String.format("/cglow block %d %d %d 10", pos.getX(), pos.getY(), pos.getZ()));
}
public static Component getGlowButtonTextComponent(BlockPos pos) {
return getCommandTextComponent(Component.translatable("commands.client.glow"), String.format("/cglow block %d %d %d 10", pos.getX(), pos.getY(), pos.getZ()));
}
public static Component getGlowButtonTextComponent(Entity entity) {
return getCommandTextComponent(Component.translatable("commands.client.glow"), "/cglow entities " + entity.getStringUUID());
}
public static Component getCommandTextComponent(@Translatable String translationKey, String command) {
return getCommandTextComponent(Component.translatable(translationKey), command);
}
public static Component getCommandTextComponent(MutableComponent component, String command) {
return component.withStyle(style -> style.applyFormat(ChatFormatting.UNDERLINE)
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(command))));
}
public static final Map<String, Runnable> runnables = new HashMap<>();
public static String registerCode(Runnable code) {
String randomString = new Random().ints('0', 'z' + 1)
.filter(i -> (i <= '9' || i >= 'A') && (i <= 'Z' || i >= 'a'))
.limit(10)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
runnables.put(randomString, code);
return randomString;
}
public static void updateOverlayProgressBar(int current, int total, int width, int time) {
MutableComponent builder = Component.empty();
int color = Mth.hsvToRgb(current / (total * 3.0f), 1.0f, 1.0f);
builder.append(Component.literal("[").withColor(0xAAAAAA));
builder.append(Component.literal("~" + Math.round(100.0 * current / total) + "%").withColor(color));
builder.append(Component.literal("] ").withColor(0xAAAAAA));
int filledWidth = (int) Math.round((double) width * current / total);
int unfilledWidth = width - filledWidth;
builder.append(Component.literal("|".repeat(filledWidth)).withColor(color));
builder.append(Component.literal("|".repeat(unfilledWidth)).withColor(0xAAAAAA));
addOverlayMessage(builder, time);
}
}