Skip to content

Commit d5b7485

Browse files
authored
Use the FabricClientCommandSource to replace calls to other methods (#416)
* Use the `FabricClientCommandSource` to replace calls to other methods * Add back the helper methods for use outside of commands * Remove some static fields + replace occurrences of `client.player`
1 parent 3e12230 commit d5b7485

39 files changed

+326
-398
lines changed

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import net.earthcomputer.clientcommands.features.BrigadierRemover;
1313
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
1414
import net.fabricmc.loader.api.FabricLoader;
15-
import net.minecraft.client.MinecraftClient;
15+
import net.minecraft.text.Text;
1616
import net.minecraft.text.TranslatableText;
1717
import net.minecraft.util.Formatting;
1818
import org.slf4j.Logger;
@@ -25,7 +25,6 @@
2525
import java.util.regex.Pattern;
2626

2727
import static com.mojang.brigadier.arguments.StringArgumentType.*;
28-
import static net.earthcomputer.clientcommands.command.ClientCommandHelper.*;
2928
import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.*;
3029

3130
public class AliasCommand {
@@ -47,26 +46,26 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
4746
.then(literal("add")
4847
.then(argument("key", string())
4948
.then(argument("command", greedyString())
50-
.executes(ctx -> addAlias(getString(ctx, "key"), getString(ctx, "command"))))))
49+
.executes(ctx -> addAlias(ctx.getSource(), getString(ctx, "key"), getString(ctx, "command"))))))
5150
.then(literal("list")
52-
.executes(ctx -> listAliases()))
51+
.executes(ctx -> listAliases(ctx.getSource())))
5352
.then(literal("remove")
5453
.then(argument("key", string())
55-
.executes(ctx -> removeAlias(getString(ctx, "key"))))));
54+
.executes(ctx -> removeAlias(ctx.getSource(), getString(ctx, "key"))))));
5655

57-
for (String key: aliasMap.keySet()) {
56+
for (String key : aliasMap.keySet()) {
5857
if (dispatcher.getRoot().getChildren().stream().map(CommandNode::getName).noneMatch(literal -> literal.equals(key))) {
5958
dispatcher.register(literal(key)
60-
.executes(ctx -> executeAliasCommand(key, null))
59+
.executes(ctx -> executeAliasCommand(ctx.getSource(), key, null))
6160
.then(argument("arguments", greedyString())
62-
.executes(ctx -> executeAliasCommand(key, getString(ctx, "arguments")))));
61+
.executes(ctx -> executeAliasCommand(ctx.getSource(), key, getString(ctx, "arguments")))));
6362
} else {
6463
LOGGER.error("Attempted to register alias /{}, but that command already exists", key);
6564
}
6665
}
6766
}
6867

69-
private static int executeAliasCommand(String aliasKey, String arguments) throws CommandSyntaxException {
68+
private static int executeAliasCommand(FabricClientCommandSource source, String aliasKey, String arguments) throws CommandSyntaxException {
7069
String cmd = aliasMap.get(aliasKey);
7170
if (cmd == null) {
7271
throw NOT_FOUND_EXCEPTION.create(aliasKey);
@@ -87,13 +86,12 @@ private static int executeAliasCommand(String aliasKey, String arguments) throws
8786
} else if (arguments != null){
8887
cmd += " " + arguments;
8988
}
90-
assert MinecraftClient.getInstance().player != null;
91-
MinecraftClient.getInstance().player.sendChatMessage(cmd);
89+
source.getPlayer().sendChatMessage(cmd);
9290

9391
return 0;
9492
}
9593

96-
private static int addAlias(String key, String command) throws CommandSyntaxException {
94+
private static int addAlias(FabricClientCommandSource source, String key, String command) throws CommandSyntaxException {
9795
if (aliasMap.containsKey(key)) {
9896
throw ALIAS_EXISTS_EXCEPTION.create(key);
9997
}
@@ -105,29 +103,29 @@ private static int addAlias(String key, String command) throws CommandSyntaxExce
105103
}
106104

107105
DISPATCHER.register(literal(key)
108-
.executes(ctx -> executeAliasCommand(key, null))
106+
.executes(ctx -> executeAliasCommand(source, key, null))
109107
.then(argument("arguments", greedyString())
110-
.executes(ctx -> executeAliasCommand(key, getString(ctx, "arguments")))));
108+
.executes(ctx -> executeAliasCommand(source, key, getString(ctx, "arguments")))));
111109
aliasMap.put(key, command);
112110

113111
saveAliases();
114-
sendFeedback(new TranslatableText("commands.calias.addAlias.success", key));
112+
source.sendFeedback(new TranslatableText("commands.calias.addAlias.success", key));
115113
return 0;
116114
}
117115

118-
private static int listAliases() {
116+
private static int listAliases(FabricClientCommandSource source) {
119117
if (aliasMap.isEmpty()) {
120-
sendFeedback(new TranslatableText("commands.calias.listAliases.noAliasesRegistered"));
118+
source.sendFeedback(new TranslatableText("commands.calias.listAliases.noAliasesRegistered"));
121119
} else {
122-
sendFeedback("commands.calias.listAliases.success", aliasMap.size());
123-
for (String key: aliasMap.keySet()) {
124-
sendFeedback(Formatting.BOLD + key + Formatting.RESET+ ": "+ aliasMap.get(key).replace("%","%%"));
120+
source.sendFeedback(new TranslatableText("commands.calias.listAliases.success", aliasMap.size()));
121+
for (String key : aliasMap.keySet()) {
122+
source.sendFeedback(Text.of(Formatting.BOLD + key + Formatting.RESET + ": " + aliasMap.get(key).replace("%","%%")));
125123
}
126124
}
127125
return 0;
128126
}
129127

130-
private static int removeAlias(String key) throws CommandSyntaxException {
128+
private static int removeAlias(FabricClientCommandSource source, String key) throws CommandSyntaxException {
131129
if (aliasMap.containsKey(key)) {
132130
BrigadierRemover.of(DISPATCHER).get(key).remove();
133131
aliasMap.remove(key);
@@ -136,7 +134,7 @@ private static int removeAlias(String key) throws CommandSyntaxException {
136134
}
137135

138136
saveAliases();
139-
sendFeedback(new TranslatableText("commands.calias.removeAlias.success", key));
137+
source.sendFeedback(new TranslatableText("commands.calias.removeAlias.success", key));
140138
return 0;
141139
}
142140

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.mojang.brigadier.tree.LiteralCommandNode;
88
import net.earthcomputer.clientcommands.render.RenderQueue;
99
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
10-
import net.minecraft.client.MinecraftClient;
1110
import net.minecraft.client.world.ClientWorld;
1211
import net.minecraft.text.TranslatableText;
1312
import net.minecraft.util.math.BlockPos;
@@ -16,7 +15,6 @@
1615
import net.minecraft.world.chunk.WorldChunk;
1716

1817
import static dev.xpple.clientarguments.arguments.CBlockPosArgumentType.*;
19-
import static net.earthcomputer.clientcommands.command.ClientCommandHelper.*;
2018
import static net.earthcomputer.clientcommands.command.arguments.ListArgumentType.*;
2119
import static net.earthcomputer.clientcommands.command.arguments.ClientBlockPredicateArgumentType.*;
2220
import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.*;
@@ -38,7 +36,7 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
3836
}
3937

4038
private static int areaStats(FabricClientCommandSource source, BlockPos pos1, BlockPos pos2, ClientBlockPredicate blockPredicate) throws CommandSyntaxException {
41-
final ClientWorld world = MinecraftClient.getInstance().world;
39+
final ClientWorld world = source.getWorld();
4240
chunkManager = world.getChunkManager();
4341
assertChunkIsLoaded(pos1.getX() >> 4, pos1.getZ() >> 4);
4442
assertChunkIsLoaded(pos2.getX() >> 4, pos2.getZ() >> 4);
@@ -164,9 +162,9 @@ private static int areaStats(FabricClientCommandSource source, BlockPos pos1, Bl
164162

165163
long endTime = System.nanoTime();
166164

167-
sendFeedback("commands.careastats.output.chunksScanned", chunks, endTime - startTime, (endTime - startTime) / 1000000);
168-
sendFeedback("commands.careastats.output.blocksMatched", blocks, (maxX - minX + 1) * (maxY - minY + 1) * (maxZ - minZ + 1));
169-
sendFeedback("commands.careastats.output.entitiesFound", entities);
165+
source.sendFeedback(new TranslatableText("commands.careastats.output.chunksScanned", chunks, endTime - startTime, (endTime - startTime) / 1000000));
166+
source.sendFeedback(new TranslatableText("commands.careastats.output.blocksMatched", blocks, (maxX - minX + 1) * (maxY - minY + 1) * (maxZ - minZ + 1)));
167+
source.sendFeedback(new TranslatableText("commands.careastats.output.entitiesFound", entities));
170168

171169
return blocks;
172170
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import net.earthcomputer.multiconnect.api.MultiConnectAPI;
77
import net.earthcomputer.multiconnect.api.Protocols;
88
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
9-
import net.minecraft.client.MinecraftClient;
109
import net.minecraft.client.network.ClientPlayerEntity;
11-
import net.minecraft.command.CommandSource;
1210
import net.minecraft.entity.player.PlayerInventory;
1311
import net.minecraft.item.ItemStack;
1412
import net.minecraft.item.Items;
@@ -28,7 +26,6 @@
2826

2927
import static com.mojang.brigadier.arguments.IntegerArgumentType.*;
3028
import static com.mojang.brigadier.arguments.LongArgumentType.*;
31-
import static net.earthcomputer.clientcommands.command.ClientCommandHelper.*;
3229
import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.*;
3330

3431
public class BookCommand {
@@ -73,8 +70,8 @@ private static IntStream ascii(Random rand) {
7370
return rand.ints(0x20, 0x7f);
7471
}
7572

76-
private static int fillBook(CommandSource source, IntStream characterGenerator, int limit) throws CommandSyntaxException {
77-
ClientPlayerEntity player = MinecraftClient.getInstance().player;
73+
private static int fillBook(FabricClientCommandSource source, IntStream characterGenerator, int limit) throws CommandSyntaxException {
74+
ClientPlayerEntity player = source.getPlayer();
7875
assert player != null;
7976

8077
ItemStack heldItem = player.getMainHandStack();
@@ -102,7 +99,7 @@ private static int fillBook(CommandSource source, IntStream characterGenerator,
10299
heldItem.setSubNbt("pages", pagesNbt);
103100
player.networkHandler.sendPacket(new BookUpdateC2SPacket(slot, pages, Optional.empty()));
104101

105-
sendFeedback("commands.cbook.success");
102+
source.sendFeedback(new TranslatableText("commands.cbook.success"));
106103

107104
return 0;
108105
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ private static int cenchant(FabricClientCommandSource source, ItemAndEnchantment
3333
.formatted(Formatting.RED)
3434
.append(" ")
3535
.append(getCommandTextComponent("commands.client.enable", "/ctemprule set enchantingPrediction true"));
36-
sendFeedback(text);
36+
source.sendFeedback(text);
3737
return 0;
3838
}
3939
if (!TempRules.playerCrackState.knowsSeed() && TempRules.enchCrackState != EnchantmentCracker.CrackState.CRACKED) {
4040
Text text = new TranslatableText("commands.cenchant.uncracked")
4141
.formatted(Formatting.RED)
4242
.append(" ")
4343
.append(getCommandTextComponent("commands.client.crack", "/ccrackrng"));
44-
sendFeedback(text);
44+
source.sendFeedback(text);
4545
return 0;
4646
}
4747

@@ -53,29 +53,25 @@ private static int cenchant(FabricClientCommandSource source, ItemAndEnchantment
5353
simulate
5454
);
5555
if (result == null) {
56-
sendFeedback("commands.cenchant.failed");
57-
return 0;
56+
source.sendFeedback(new TranslatableText("commands.cenchant.failed"));
5857
} else {
59-
6058
if (simulate) {
6159
if (result.itemThrows() < 0) {
62-
sendFeedback("enchCrack.insn.itemThrows.noDummy");
60+
source.sendFeedback(new TranslatableText("enchCrack.insn.itemThrows.noDummy"));
6361
} else {
64-
sendFeedback(new TranslatableText("enchCrack.insn.itemThrows", result.itemThrows(), (float)result.itemThrows() / 20f));
62+
source.sendFeedback(new TranslatableText("enchCrack.insn.itemThrows", result.itemThrows(), (float)result.itemThrows() / 20f));
6563
}
66-
sendFeedback(new TranslatableText("enchCrack.insn.bookshelves", result.bookshelves()));
67-
sendFeedback(new TranslatableText("enchCrack.insn.slot", result.slot() + 1));
68-
sendFeedback("enchCrack.insn.enchantments");
64+
source.sendFeedback(new TranslatableText("enchCrack.insn.bookshelves", result.bookshelves()));
65+
source.sendFeedback(new TranslatableText("enchCrack.insn.slot", result.slot() + 1));
66+
source.sendFeedback(new TranslatableText("enchCrack.insn.enchantments"));
6967
for (EnchantmentLevelEntry ench : result.enchantments()) {
70-
sendFeedback(new LiteralText("- ").append(ench.enchantment.getName(ench.level)));
68+
source.sendFeedback(new LiteralText("- ").append(ench.enchantment.getName(ench.level)));
7169
}
72-
return 0;
73-
7470
} else {
75-
sendFeedback("commands.cenchant.success");
76-
return 0;
71+
source.sendFeedback(new TranslatableText("commands.cenchant.success"));
7772
}
7873
}
74+
return 0;
7975
}
8076

8177
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
import com.mojang.brigadier.exceptions.CommandSyntaxException;
55
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
66
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
7-
import net.minecraft.client.MinecraftClient;
87
import net.minecraft.command.argument.ItemStackArgument;
98
import net.minecraft.item.ItemStack;
109
import net.minecraft.text.TranslatableText;
1110

1211
import static com.mojang.brigadier.arguments.IntegerArgumentType.*;
1312
import static dev.xpple.clientarguments.arguments.CItemStackArgumentType.*;
14-
import static net.earthcomputer.clientcommands.command.ClientCommandHelper.*;
1513
import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.*;
1614

1715
public class CGiveCommand {
@@ -27,16 +25,15 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
2725
}
2826

2927
private static int give(FabricClientCommandSource source, ItemStackArgument itemArgument, int count) throws CommandSyntaxException {
30-
final MinecraftClient client = MinecraftClient.getInstance();
31-
if (!client.player.getAbilities().creativeMode) {
28+
if (!source.getPlayer().getAbilities().creativeMode) {
3229
throw NOT_CREATIVE_EXCEPTION.create();
3330
}
3431

3532
ItemStack stack = itemArgument.createStack(Math.min(count, itemArgument.getItem().getMaxCount()), false);
36-
client.interactionManager.clickCreativeStack(stack, 36 + client.player.getInventory().selectedSlot);
37-
client.player.playerScreenHandler.sendContentUpdates();
33+
source.getClient().interactionManager.clickCreativeStack(stack, 36 + source.getPlayer().getInventory().selectedSlot);
34+
source.getPlayer().playerScreenHandler.sendContentUpdates();
3835

39-
sendFeedback(new TranslatableText("commands.cgive.success", count, stack.toHoverableText()));
36+
source.sendFeedback(new TranslatableText("commands.cgive.success", count, stack.toHoverableText()));
4037
return 0;
4138
}
4239
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.mojang.brigadier.exceptions.CommandSyntaxException;
55
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
66
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
7-
import net.minecraft.client.MinecraftClient;
87
import net.minecraft.particle.ParticleEffect;
98
import net.minecraft.particle.ParticleTypes;
109
import net.minecraft.text.TranslatableText;
@@ -17,19 +16,16 @@
1716
import static com.mojang.brigadier.arguments.IntegerArgumentType.*;
1817
import static dev.xpple.clientarguments.arguments.CParticleEffectArgumentType.*;
1918
import static dev.xpple.clientarguments.arguments.CVec3ArgumentType.*;
20-
import static net.earthcomputer.clientcommands.command.ClientCommandHelper.*;
2119
import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.*;
2220

2321
public class CParticleCommand {
2422

2523
private static final SimpleCommandExceptionType UNSUITABLE_PARTICLE_OPTION_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.cparticle.unsuitableParticleOption"));
2624

27-
private static final MinecraftClient client = MinecraftClient.getInstance();
28-
2925
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
3026
dispatcher.register(literal("cparticle")
3127
.then(argument("name", particleEffect())
32-
.executes(ctx -> spawnParticle(ctx.getSource(), getCParticle(ctx, "name"), client.player.getPos(), Vec3d.ZERO, 1, 1, false))
28+
.executes(ctx -> spawnParticle(ctx.getSource(), getCParticle(ctx, "name"), ctx.getSource().getPlayer().getPos(), Vec3d.ZERO, 1, 1, false))
3329
.then(argument("pos", vec3())
3430
.executes(ctx -> spawnParticle(ctx.getSource(), getCParticle(ctx, "name"), getCVec3(ctx, "pos"), Vec3d.ZERO, 1, 1, false))
3531
.then(argument("delta", vec3(false))
@@ -45,7 +41,7 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
4541
}
4642

4743
private static int spawnParticle(FabricClientCommandSource source, ParticleEffect parameters, Vec3d pos, Vec3d delta, float speed, int count, boolean force) throws CommandSyntaxException {
48-
switch (client.options.particles) {
44+
switch (source.getClient().options.particles) {
4945
case MINIMAL:
5046
if (!force) {
5147
throw UNSUITABLE_PARTICLE_OPTION_EXCEPTION.create();
@@ -56,14 +52,14 @@ private static int spawnParticle(FabricClientCommandSource source, ParticleEffec
5652
}
5753
default:
5854
if (count == 0) {
59-
client.world.addParticle(parameters, force, pos.x, pos.y, pos.z, delta.x * speed, delta.y * speed, delta.z * speed);
55+
source.getWorld().addParticle(parameters, force, pos.x, pos.y, pos.z, delta.x * speed, delta.y * speed, delta.z * speed);
6056
} else {
61-
final Random random = client.getNetworkHandler().getWorld().random;
57+
final Random random = source.getClient().getNetworkHandler().getWorld().random;
6258
for (int i = 0; i < count; i++) {
63-
client.world.addParticle(parameters, force, pos.x + delta.x * random.nextGaussian(), pos.y + delta.y * random.nextGaussian(), pos.z + delta.z * random.nextGaussian(), speed * random.nextGaussian(), speed * random.nextGaussian(), speed * random.nextGaussian());
59+
source.getWorld().addParticle(parameters, force, pos.x + delta.x * random.nextGaussian(), pos.y + delta.y * random.nextGaussian(), pos.z + delta.z * random.nextGaussian(), speed * random.nextGaussian(), speed * random.nextGaussian(), speed * random.nextGaussian());
6460
}
6561
}
66-
sendFeedback(new TranslatableText("commands.cparticle.success", Registry.PARTICLE_TYPE.getId(parameters.getType()).toString()));
62+
source.sendFeedback(new TranslatableText("commands.cparticle.success", Registry.PARTICLE_TYPE.getId(parameters.getType()).toString()));
6763
return 0;
6864
}
6965
}

0 commit comments

Comments
 (0)