forked from MeteorDevelopment/meteor-client
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommand.java
More file actions
115 lines (93 loc) · 4.28 KB
/
Copy pathCommand.java
File metadata and controls
115 lines (93 loc) · 4.28 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
/*
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
* Copyright (c) Meteor Development.
*/
package meteordevelopment.meteorclient.commands;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.systems.config.Config;
import meteordevelopment.meteorclient.utils.Utils;
import meteordevelopment.meteorclient.utils.misc.MeteorTranslations;
import meteordevelopment.meteorclient.utils.player.ChatUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.CommandSource;
import net.minecraft.registry.BuiltinRegistries;
import net.minecraft.server.command.CommandManager;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import java.util.List;
public abstract class Command {
protected static CommandRegistryAccess REGISTRY_ACCESS = CommandManager.createRegistryAccess(BuiltinRegistries.createWrapperLookup());
protected static final int SINGLE_SUCCESS = com.mojang.brigadier.Command.SINGLE_SUCCESS;
protected static final MinecraftClient mc = MeteorClient.mc;
private final String name;
private final String title;
private final List<String> aliases;
public final String translationKey;
public Command(String name, String... aliases) {
this.name = name;
this.title = Utils.nameToTitle(name);
this.aliases = List.of(aliases);
this.translationKey = "command." + name;
}
public Command(String name) {
this(name, new String[0]);
}
// Helper methods to painlessly infer the CommandSource generic type argument
protected static <T> RequiredArgumentBuilder<CommandSource, T> argument(final String name, final ArgumentType<T> type) {
return RequiredArgumentBuilder.argument(name, type);
}
protected static LiteralArgumentBuilder<CommandSource> literal(final String name) {
return LiteralArgumentBuilder.literal(name);
}
public final void registerTo(CommandDispatcher<CommandSource> dispatcher) {
register(dispatcher, name);
for (String alias : aliases) register(dispatcher, alias);
}
public void register(CommandDispatcher<CommandSource> dispatcher, String name) {
LiteralArgumentBuilder<CommandSource> builder = LiteralArgumentBuilder.literal(name);
build(builder);
dispatcher.register(builder);
}
public abstract void build(LiteralArgumentBuilder<CommandSource> builder);
public String getName() {
return name;
}
public List<String> getAliases() {
return aliases;
}
public String toString() {
return Config.get().prefix.get() + name;
}
public String toString(String... args) {
StringBuilder base = new StringBuilder(toString());
for (String arg : args) base.append(' ').append(arg);
return base.toString();
}
public void info(Text message) {
ChatUtils.forceNextPrefixClass(getClass());
ChatUtils.sendMsg(title, message);
}
public void info(String message, Object... args) {
ChatUtils.forceNextPrefixClass(getClass());
ChatUtils.infoPrefix(title, MeteorTranslations.translate(translationKey + ".info." + message, message, args));
}
public void warning(String message, Object... args) {
ChatUtils.forceNextPrefixClass(getClass());
ChatUtils.warningPrefix(title, MeteorTranslations.translate(translationKey + ".warning." + message, message, args));
}
public void error(String message, Object... args) {
ChatUtils.forceNextPrefixClass(getClass());
ChatUtils.errorPrefix(title, MeteorTranslations.translate(translationKey + ".error." + message, message, args));
}
public MutableText translatable(String string, Object... args) {
return MeteorClient.translatable(translationKey + "." + string, args);
}
public MutableText translatable(String string, String fallback, Object... args) {
return MeteorClient.translatable(translationKey + "." + string, fallback, args);
}
}