aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/chrisoft/trashyaddon/commands/BlockDataCommand.java
blob: 11fa8a3ff86fa05afc1de6963f471c6799dbbc2d (plain) (blame)
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
package org.chrisoft.trashyaddon.commands;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import meteordevelopment.meteorclient.commands.Command;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.RaycastContext;
import net.minecraft.command.CommandSource;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.text.Text;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.Vec3d;

import static com.mojang.brigadier.Command.SINGLE_SUCCESS;

public class BlockDataCommand extends Command {
    private final MinecraftClient mc = MinecraftClient.getInstance();
    public BlockDataCommand() {
        super("bd", "Dump client side data of targeted block.");
    }

    @Override
    public void build(LiteralArgumentBuilder<CommandSource> builder) {
        builder.executes(context -> {
            double RANGE = 5;
            Entity player = mc.cameraEntity;
            Vec3d rot = player.getRotationVec(1.f);
            Vec3d min = player.getCameraPosVec(1.f);
            Vec3d max = min.add(rot.multiply(RANGE));
            RaycastContext rc = new RaycastContext(min, max, RaycastContext.ShapeType.OUTLINE, RaycastContext.FluidHandling.NONE, player);
            BlockHitResult bh = mc.world.raycast(rc);
            if (bh == null) {
                error("no block picked");
                return 100;
            }
            BlockPos bp = bh.getBlockPos();
            BlockState bs = mc.world.getBlockState(bp);
            if (bs == null)
            {
                error("no block state");
                return 100;
            }
            info(Text.literal("block is ").append(bs.getBlock().getName()));
            BlockEntity be = mc.world.getBlockEntity(bp);
            if (be == null)
            {
                error("block has no block entity");
                return SINGLE_SUCCESS;
            }
            NbtCompound bd = be.createNbt(mc.world.getRegistryManager());
            info(NbtHelper.toPrettyPrintedText(bd));

            return SINGLE_SUCCESS;
        });
    }
}