summaryrefslogtreecommitdiff
path: root/src/main/java/net/justlearning/arslaan3102/arslaansmagichax/commands/HackGive.java
blob: ea2e1d91f88b18c2a20a8008aed499aa3cdfdb06 (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
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 com.mojang.brigadier.exceptions.CommandSyntaxException;
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.client.MinecraftClient;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class HackGive {
    public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess access) {
        dispatcher.register(ClientCommandManager.literal("hackgive")
                .then(ClientCommandManager.argument("item", StringArgumentType.string())
                        .then(ClientCommandManager.argument("amount", IntegerArgumentType.integer(1))
                                .executes(HackGive::run)
                        )
                )
        );
    }

    private static int run(CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client.world == null || client.player == null) return 0;

        String itemId = StringArgumentType.getString(context, "item");
        int amount = IntegerArgumentType.getInteger(context, "amount");
        Item item = Registries.ITEM.get(Identifier.of(itemId));
        if (item == null) {
            context.getSource().sendError(Text.literal("Unknown item: " + itemId));
            return 0;
        }

        ItemStack stack = new ItemStack(item, amount);
        client.player.getInventory().insertStack(stack);
        return 1;
    }
}