Skip to content

Commit b4bdb7b

Browse files
authored
Merge pull request #36 from FTBTeam/1.18/dev
1.18/dev
2 parents c343951 + dbfd08b commit b4bdb7b

File tree

7 files changed

+86
-161
lines changed

7 files changed

+86
-161
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
[1802.1.11]
8+
9+
### Fixed
10+
* Backed out dynamic tab-completion functionality and use simple server-side suggestions instead
11+
* FTB Ranks is a server-only mod again and no longer needs to be installed on the client since that's unnecessarily disruptive
12+
713
[1802.1.10]
814

915
### Fixed

common/src/main/java/dev/ftb/mods/ftbranks/FTBRanks.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,5 @@ public FTBRanks() {
2828
CommandRegistrationEvent.EVENT.register(FTBRanksCommands::register);
2929
// TODO: Register with LOWEST priority on forge
3030
ChatEvent.SERVER.register(FTBRanksAPIImpl::serverChat);
31-
32-
ArgumentTypes.register("ftbranks:rank", RankArgumentType.class, new RankArgumentType.Serializer());
3331
}
3432
}

common/src/main/java/dev/ftb/mods/ftbranks/FTBRanksCommands.java

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import com.mojang.brigadier.CommandDispatcher;
55
import com.mojang.brigadier.arguments.StringArgumentType;
66
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7+
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
78
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
9+
import com.mojang.brigadier.suggestion.Suggestions;
10+
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
811
import dev.ftb.mods.ftblibrary.snbt.SNBT;
912
import dev.ftb.mods.ftbranks.api.FTBRanksAPI;
1013
import dev.ftb.mods.ftbranks.api.PermissionValue;
@@ -18,24 +21,26 @@
1821
import net.minecraft.ChatFormatting;
1922
import net.minecraft.commands.CommandSourceStack;
2023
import net.minecraft.commands.Commands;
24+
import net.minecraft.commands.SharedSuggestionProvider;
2125
import net.minecraft.commands.arguments.EntityArgument;
2226
import net.minecraft.commands.arguments.GameProfileArgument;
2327
import net.minecraft.nbt.StringTag;
2428
import net.minecraft.network.chat.Component;
25-
import net.minecraft.network.chat.MutableComponent;
2629
import net.minecraft.network.chat.TextComponent;
27-
import net.minecraft.network.chat.TranslatableComponent;
2830
import net.minecraft.server.level.ServerPlayer;
2931
import org.apache.commons.lang3.math.NumberUtils;
3032

3133
import java.io.IOException;
3234
import java.util.Collection;
3335
import java.util.Collections;
36+
import java.util.concurrent.CompletableFuture;
3437

3538
/**
3639
* @author LatvianModder
3740
*/
3841
public class FTBRanksCommands {
42+
public static final DynamicCommandExceptionType ERROR_UNKNOWN_RANK = new DynamicCommandExceptionType((object) -> new TextComponent("Unknown rank: " + object.toString()));
43+
3944
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, Commands.CommandSelection selection) {
4045
dispatcher.register(Commands.literal("ftbranks")
4146
.requires(source -> source.getServer().isSingleplayer() || source.hasPermission(2))
@@ -54,21 +59,24 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher, Co
5459
)
5560
)
5661
.then(Commands.literal("delete")
57-
.then(Commands.argument("rank", RankArgumentType.rank())
58-
.executes(context -> deleteRank(context.getSource(), RankArgumentType.getRank(context, "rank")))
62+
.then(Commands.argument("rank", StringArgumentType.word())
63+
.suggests((context, builder) -> suggestRanks(builder))
64+
.executes(context -> deleteRank(context.getSource(), StringArgumentType.getString(context, "rank")))
5965
)
6066
)
6167
.then(Commands.literal("add")
6268
.then(Commands.argument("players", GameProfileArgument.gameProfile())
63-
.then(Commands.argument("rank", RankArgumentType.rank())
64-
.executes(context -> addRank(context.getSource(), GameProfileArgument.getGameProfiles(context, "players"), RankArgumentType.getRank(context, "rank")))
69+
.then(Commands.argument("rank", StringArgumentType.word())
70+
.suggests((context, builder) -> suggestRanks(builder))
71+
.executes(context -> addRank(context.getSource(), GameProfileArgument.getGameProfiles(context, "players"), StringArgumentType.getString(context, "rank")))
6572
)
6673
)
6774
)
6875
.then(Commands.literal("remove")
6976
.then(Commands.argument("players", GameProfileArgument.gameProfile())
70-
.then(Commands.argument("rank", RankArgumentType.rank())
71-
.executes(context -> removeRank(context.getSource(), GameProfileArgument.getGameProfiles(context, "players"), RankArgumentType.getRank(context, "rank")))
77+
.then(Commands.argument("rank", StringArgumentType.word())
78+
.suggests((context, builder) -> suggestRanks(builder))
79+
.executes(context -> removeRank(context.getSource(), GameProfileArgument.getGameProfiles(context, "players"), StringArgumentType.getString(context, "rank")))
7280
)
7381
)
7482
)
@@ -78,51 +86,60 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher, Co
7886
)
7987
)
8088
.then(Commands.literal("list_players_with")
81-
.then(Commands.argument("rank", RankArgumentType.rank())
82-
.executes(context -> listPlayersWith(context.getSource(), RankArgumentType.getRank(context, "rank")))
89+
.then(Commands.argument("rank", StringArgumentType.word())
90+
.suggests((context, builder) -> suggestRanks(builder))
91+
.executes(context -> listPlayersWith(context.getSource(), StringArgumentType.getString(context, "rank")))
8392
)
8493
)
8594
.then(Commands.literal("node")
8695
.then(Commands.literal("add")
87-
.then(Commands.argument("rank", RankArgumentType.rank())
96+
.then(Commands.argument("rank", StringArgumentType.word())
97+
.suggests((context, builder) -> suggestRanks(builder))
8898
.then(Commands.argument("node", StringArgumentType.word())
8999
.then(Commands.argument("value", StringArgumentType.greedyString())
90-
.executes(context -> setNode(context.getSource(), RankArgumentType.getRank(context, "rank"), StringArgumentType.getString(context, "node"), StringArgumentType.getString(context, "value")))
100+
.executes(context -> setNode(context.getSource(), StringArgumentType.getString(context, "rank"), StringArgumentType.getString(context, "node"), StringArgumentType.getString(context, "value")))
91101
)
92102
)
93103
)
94104
)
95105
.then(Commands.literal("remove")
96-
.then(Commands.argument("rank", RankArgumentType.rank())
106+
.then(Commands.argument("rank", StringArgumentType.word())
107+
.suggests((context, builder) -> suggestRanks(builder))
97108
.then(Commands.argument("node", StringArgumentType.word())
98-
.executes(context -> setNode(context.getSource(), RankArgumentType.getRank(context, "rank"), StringArgumentType.getString(context, "node"), null))
109+
.executes(context -> setNode(context.getSource(), StringArgumentType.getString(context, "rank"), StringArgumentType.getString(context, "node"), null))
99110
)
100111
)
101112
)
102113
)
103114
.then(Commands.literal("condition")
104-
.then(Commands.argument("rank", RankArgumentType.rank())
115+
.then(Commands.argument("rank", StringArgumentType.word())
116+
.suggests((context, builder) -> suggestRanks(builder))
105117
.then(Commands.argument("value", StringArgumentType.greedyString())
106-
.executes(context -> setCondition(context.getSource(), RankArgumentType.getRank(context, "rank"), StringArgumentType.getString(context, "value")))
118+
.executes(context -> setCondition(context.getSource(), StringArgumentType.getString(context, "rank"), StringArgumentType.getString(context, "value")))
107119
)
108120
)
109121
)
110122
.then(Commands.literal("show_rank")
111-
.then(Commands.argument("rank", RankArgumentType.rank())
112-
.executes(context -> showRank(context.getSource(), RankArgumentType.getRank(context, "rank")))
123+
.then(Commands.argument("rank", StringArgumentType.word())
124+
.suggests((context, builder) -> suggestRanks(builder))
125+
.executes(context -> showRank(context.getSource(), StringArgumentType.getString(context, "rank")))
113126
)
114127
)
115128
);
116129
}
117130

131+
private static CompletableFuture<Suggestions> suggestRanks(SuggestionsBuilder builder) {
132+
return SharedSuggestionProvider.suggest(FTBRanksAPI.INSTANCE.getManager().getAllRanks().stream().map(Rank::getId), builder);
133+
}
134+
118135
private static String normalizeRankName(String name) {
119136
return name.toLowerCase().replace("+", "_plus").replaceAll("[^a-z0-9_]", "_").replaceAll("_{2,}", "_");
120137
}
121138

122139
private static int reloadRanks(CommandSourceStack source) {
123140
try {
124141
FTBRanksAPIImpl.manager.reload();
125-
source.sendSuccess(new TranslatableComponent("ftbranks.reload"), true);
142+
source.sendSuccess(new TextComponent("Ranks reloaded from disk!"), true);
126143

127144
for (ServerPlayer p : source.getServer().getPlayerList().getPlayers()) {
128145
source.getServer().getPlayerList().sendPlayerPermissionLevel(p);
@@ -143,12 +160,12 @@ private static int refreshReadme(CommandSourceStack source) {
143160
ex.printStackTrace();
144161
}
145162

146-
source.sendSuccess(new TranslatableComponent("ftbranks.refresh_readme"), false);
163+
source.sendSuccess(new TextComponent("README file refreshed!"), false);
147164
return 1;
148165
}
149166

150167
private static int listAllRanks(CommandSourceStack source) {
151-
source.sendSuccess(new TranslatableComponent("ftbranks.ranks"), false);
168+
source.sendSuccess(new TextComponent("Ranks:"), false);
152169

153170
for (Rank rank : FTBRanksAPIImpl.manager.getAllRanks()) {
154171
source.sendSuccess(new TextComponent("- " + rank.getName()).withStyle(rank.getCondition().isDefaultCondition() ? ChatFormatting.AQUA : ChatFormatting.YELLOW), false);
@@ -161,44 +178,47 @@ private static int createRank(CommandSourceStack source, String name) {
161178
String id = normalizeRankName(name);
162179

163180
if (FTBRanksAPIImpl.manager.getRank(id).isPresent()) {
164-
source.sendFailure(new TranslatableComponent("ftbranks.rank_taken", name));
181+
source.sendFailure(new TextComponent("Rank '" + name + "' is already taken!"));
165182
return 0;
166183
}
167184

168185
FTBRanksAPIImpl.manager.createRank(id, name);
169-
source.sendSuccess(new TranslatableComponent("ftbranks.rank_created", id), false);
186+
source.sendSuccess(new TextComponent("Rank '" + id + "' created!"), false);
170187
return 1;
171188
}
172189

173-
private static int deleteRank(CommandSourceStack source, Rank rank) {
190+
private static int deleteRank(CommandSourceStack source, String rankName) throws CommandSyntaxException {
191+
Rank rank = getRank(rankName);
174192
FTBRanksAPI.INSTANCE.getManager().deleteRank(rank.getId());
175-
source.sendSuccess(new TranslatableComponent("ftbranks.rank_deleted", rank.getName()), false);
193+
source.sendSuccess(new TextComponent("Rank '" + rank.getName() + "' deleted!"), false);
176194

177195
return 1;
178196
}
179197

180-
private static int addRank(CommandSourceStack source, Collection<GameProfile> players, Rank rank) {
198+
private static int addRank(CommandSourceStack source, Collection<GameProfile> players, String rankName) throws CommandSyntaxException {
199+
Rank rank = getRank(rankName);
181200
for (GameProfile profile : players) {
182201
if (rank.add(profile)) {
183-
source.sendSuccess(new TranslatableComponent("ftbranks.player_added", profile.getName(), rank.getName()), false);
202+
source.sendSuccess(new TextComponent(String.format("Player %s added to rank '%s'!", profile.getName(), rank.getName())), false);
184203
}
185204
}
186205

187206
return 1;
188207
}
189208

190-
private static int removeRank(CommandSourceStack source, Collection<GameProfile> players, Rank rank) {
209+
private static int removeRank(CommandSourceStack source, Collection<GameProfile> players, String rankName) throws CommandSyntaxException {
210+
Rank rank = getRank(rankName);
191211
for (GameProfile profile : players) {
192212
if (rank.remove(profile)) {
193-
source.sendSuccess(new TranslatableComponent("ftbranks.player_removed", profile.getName(), rank.getName()), false);
213+
source.sendSuccess(new TextComponent(String.format("Player %s removed from rank '%s'!", profile.getName(), rank.getName())), false);
194214
}
195215
}
196216

197217
return 1;
198218
}
199219

200220
private static int listRanksOf(CommandSourceStack source, ServerPlayer player) {
201-
source.sendSuccess(new TranslatableComponent("ftbranks.list_ranks_of", player.getGameProfile().getName()), false);
221+
source.sendSuccess(new TextComponent(String.format("Ranks added to player '%s':", player.getGameProfile().getName())), false);
202222

203223
for (Rank rank : FTBRanksAPIImpl.manager.getAllRanks()) {
204224
if (rank.isActive(player)) {
@@ -208,9 +228,10 @@ private static int listRanksOf(CommandSourceStack source, ServerPlayer player) {
208228

209229
return 1;
210230
}
231+
private static int listPlayersWith(CommandSourceStack source, String rankName) throws CommandSyntaxException {
232+
Rank rank = getRank(rankName);
211233

212-
private static int listPlayersWith(CommandSourceStack source, Rank rank) {
213-
source.sendSuccess(new TranslatableComponent("ftbranks.list_players_with", rank.getName()), false);
234+
source.sendSuccess(new TextComponent(String.format("Players with rank '%s':", rank.getName())), false);
214235

215236
for (ServerPlayer player : source.getServer().getPlayerList().getPlayers()) {
216237
if (rank.isActive(player)) {
@@ -221,13 +242,15 @@ private static int listPlayersWith(CommandSourceStack source, Rank rank) {
221242
return 1;
222243
}
223244

224-
private static int setNode(CommandSourceStack source, Rank rank, String node, String value) throws CommandSyntaxException {
245+
private static int setNode(CommandSourceStack source, String rankName, String node, String value) throws CommandSyntaxException {
246+
Rank rank = getRank(rankName);
247+
225248
try {
226249
rank.setPermission(node, strToPermissionValue(value));
227250
if (value != null) {
228-
source.sendSuccess(new TranslatableComponent("ftbranks.node_added", node, rank.getPermission(node), rank), false);
251+
source.sendSuccess(new TextComponent(String.format("Permission node '%s'='%s' added to rank '%s'", node, rank.getPermission(node), rank)), false);
229252
} else {
230-
source.sendSuccess(new TranslatableComponent("ftbranks.node_removed", node, rank), false);
253+
source.sendSuccess(new TextComponent(String.format("Permission node '%s' removed from rank '%s'", node, rank)), false);
231254
}
232255
} catch (IllegalArgumentException e) {
233256
throw new SimpleCommandExceptionType(new TextComponent(e.getMessage())).create();
@@ -236,7 +259,9 @@ private static int setNode(CommandSourceStack source, Rank rank, String node, St
236259
return 1;
237260
}
238261

239-
private static int setCondition(CommandSourceStack source, Rank rank, String value) throws CommandSyntaxException {
262+
private static int setCondition(CommandSourceStack source, String rankName, String value) throws CommandSyntaxException {
263+
Rank rank = getRank(rankName);
264+
240265
try {
241266
RankCondition condition;
242267
if (value.equals("default") || value.equals("\"\"")) {
@@ -247,37 +272,34 @@ private static int setCondition(CommandSourceStack source, Rank rank, String val
247272
condition = FTBRanksAPI.INSTANCE.getManager().createCondition(rank, StringTag.valueOf(value));
248273
}
249274
rank.setCondition(condition);
250-
source.sendSuccess(new TranslatableComponent("ftbranks.node_added", "condition", value, rank), false);
275+
source.sendSuccess(new TextComponent(String.format("Condition '%s' added to rank '%s'", value, rank)), false);
251276
} catch (Exception e) {
252277
throw new SimpleCommandExceptionType(new TextComponent(e.getMessage())).create();
253278
}
254279

255280
return 1;
256281
}
257282

258-
private static int showRank(CommandSourceStack source, Rank rank) {
283+
private static int showRank(CommandSourceStack source, String rankName) throws CommandSyntaxException {
284+
Rank rank = getRank(rankName);
285+
259286
source.sendSuccess(new TextComponent("=".repeat(50)).withStyle(ChatFormatting.GREEN), false);
260287

261-
source.sendSuccess(new TranslatableComponent("ftbranks.show_rank.header", col(rank.getId(), ChatFormatting.WHITE), col(rank.getName(), ChatFormatting.WHITE), col(Integer.toString(rank.getPower()), ChatFormatting.WHITE)).withStyle(ChatFormatting.YELLOW), false);
288+
source.sendSuccess(new TextComponent(String.format("Rank ID: %s, Rank Name: %s, Power: %d", rank.getId(), rank.getName(), rank.getPower())).withStyle(ChatFormatting.YELLOW), false);
262289

263290
String condStr = rank.getCondition().asString();
264291
Component c = condStr.isEmpty() ?
265-
new TranslatableComponent("ftbranks.show_rank.condition.default").withStyle(ChatFormatting.WHITE, ChatFormatting.ITALIC) :
266-
col(condStr, ChatFormatting.WHITE);
267-
source.sendSuccess(new TranslatableComponent("ftbranks.show_rank.condition", c).withStyle(ChatFormatting.YELLOW), false);
292+
new TextComponent("(none: players must be added)").withStyle(ChatFormatting.WHITE, ChatFormatting.ITALIC) : new TextComponent(condStr);
293+
source.sendSuccess(new TextComponent("Condition: ").append(c).withStyle(ChatFormatting.YELLOW), false);
268294

269-
source.sendSuccess(new TranslatableComponent("ftbranks.show_rank.nodes").withStyle(ChatFormatting.YELLOW), false);
295+
source.sendSuccess(new TextComponent("Permission nodes:").withStyle(ChatFormatting.YELLOW), false);
270296
rank.getPermissions().stream().sorted().forEach(node ->
271-
source.sendSuccess(new TranslatableComponent("ftbranks.show_rank.node", col(node, ChatFormatting.AQUA), rank.getPermission(node)).withStyle(ChatFormatting.WHITE), false)
297+
source.sendSuccess(new TextComponent(" - " + node + ": " + rank.getPermission(node)), false)
272298
);
273299

274300
return 0;
275301
}
276302

277-
private static MutableComponent col(String str, ChatFormatting color) {
278-
return new TextComponent(str).withStyle(color);
279-
}
280-
281303
private static PermissionValue strToPermissionValue(String str) {
282304
if (str == null) {
283305
return null;
@@ -291,4 +313,8 @@ private static PermissionValue strToPermissionValue(String str) {
291313
return StringPermissionValue.of(str);
292314
}
293315
}
294-
}
316+
317+
private static Rank getRank(String rankName) throws CommandSyntaxException {
318+
return FTBRanksAPI.INSTANCE.getManager().getRank(rankName).orElseThrow(() -> ERROR_UNKNOWN_RANK.create(rankName));
319+
}
320+
}

0 commit comments

Comments
 (0)