summaryrefslogtreecommitdiff
path: root/src/main/java/com/arslaancodes/arslaansmagichax/commands/HackSetblock.java
blob: a26d0e9f0db53d8bb974ddf65dfe1c91e6aee743 (plain)
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.client.Minecraft;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;

public class HackSetblock {
    public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandBuildContext 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) {
        Minecraft client = Minecraft.getInstance();
        if (client.level == null || client.player == null) return 0;

        String blockId = StringArgumentType.getString(context, "block");
        Block block = BuiltInRegistries.BLOCK.getValue(ResourceLocation.parse(blockId));
        if (block == null) {
            context.getSource().sendError(Component.literal("Unknown block: " + blockId));
            return 0;
        }

        BlockPos pos = client.player.blockPosition();
        BlockState blockState = block.defaultBlockState();

        client.level.setBlockAndUpdate(pos, blockState);

        return 1;
    }

    private static int runAtPos(CommandContext<FabricClientCommandSource> context) {
        Minecraft client = Minecraft.getInstance();
        if (client.level == 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 = BuiltInRegistries.BLOCK.getValue(ResourceLocation.parse(blockId));
        if (block == null) {
            context.getSource().sendError(Component.literal("Unknown block: " + blockId));
            return 0;
        }

        BlockPos pos = new BlockPos(x, y, z);
        BlockState blockState = block.defaultBlockState();

        client.level.setBlockAndUpdate(pos, blockState);

        return 1;
    }
}