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
|
package com.arslaancodes.arslaansmagichax.commands;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
public class HackSetblock {
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess access) {
dispatcher.register(ClientCommandManager.literal("hacksetblock")
.then(ClientCommandManager.argument("block", StringArgumentType.string())
// Case: just block -> place at player's feet
.executes(HackSetblock::runAtFeet)
// Case: block + xyz -> place at given position
.then(ClientCommandManager.argument("x", IntegerArgumentType.integer())
.then(ClientCommandManager.argument("y", IntegerArgumentType.integer())
.then(ClientCommandManager.argument("z", IntegerArgumentType.integer())
.executes(HackSetblock::runAtPos)
)
)
)
)
);
}
private static int runAtFeet(CommandContext<FabricClientCommandSource> context) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.world == null || client.player == null) return 0;
String blockId = StringArgumentType.getString(context, "block");
Block block = Registries.BLOCK.get(Identifier.of(blockId));
if (block == null) {
context.getSource().sendError(Text.literal("Unknown block: " + blockId));
return 0;
}
BlockPos pos = client.player.getBlockPos();
BlockState blockState = block.getDefaultState();
client.world.setBlockState(pos, blockState);
return 1;
}
private static int runAtPos(CommandContext<FabricClientCommandSource> context) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.world == null) return 0;
String blockId = StringArgumentType.getString(context, "block");
int x = IntegerArgumentType.getInteger(context, "x");
int y = IntegerArgumentType.getInteger(context, "y");
int z = IntegerArgumentType.getInteger(context, "z");
Block block = Registries.BLOCK.get(Identifier.of(blockId));
if (block == null) {
context.getSource().sendError(Text.literal("Unknown block: " + blockId));
return 0;
}
BlockPos pos = new BlockPos(x, y, z);
BlockState blockState = block.getDefaultState();
client.world.setBlockState(pos, blockState);
return 1;
}
}
|