warieregg

I want to create a Minecraft mod that allows players to duplicate items on a server. When a player types /dupe in the chat, the item they are currently holding should be duplicated. The duplicated item should be dropped onto the ground while the original item remains in the player's inventory. The duplication should work reliably and consistently on the server. Requirement fabric API It must work even if the player does not have Operator (OP) permissions. It should also work while the player is in Survival mode and should not require Creative mode.

Kodari AI
KodariJul 5 at 6:21 pm 13
ItemDupeMod
A Fabric mod for Minecraft 1.21.11 that lets any player duplicate the item currently in their main hand by typing /dupe in chat. The duplicate spawns as a dropped item entity on the ground while the original stays in the player's inventory. Works in Survival mode, requires no Operator permissions, and functions reliably for every player on the server.
warieregg

The compilation failed with this error:

code
Compilation failed with 2 error(s):

ERROR in src/main/java/com/example/itemdupe/ItemDupeMod.java at line 31: unreported exception CommandSyntaxException; must be caught or declared to be thrown
        ServerPlayerEntity player = source.getPlayerOrThrow();
                                                           ^

ERROR in   src/main/java/com/example/itemdupe/ItemDupeMod.java at line 31: unreported exception CommandSyntaxException; must be caught or declared to be thrown
          ServerPlayerEntity player = source.getPlayerOrThrow();
                                                             ^


=== COMPILATION ERROR ANALYSIS ===

FILES TO INVESTIGATE:
  - src/main/java/com/example/itemdupe/ItemDupeMod.java
  -   src/main/java/com/example/itemdupe/ItemDupeMod.java

Please fix this error by creating a new artifact with the corrected code.

Kodari AI
KodariJul 6 at 4:03 am 8

Fixed — executeDupe now declares throws CommandSyntaxException, since getPlayerOrThrow() can throw it.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 130
src/main/java/com/example/itemdupe/ItemDupeMod.java
package com.example.itemdupe;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import com.mojang.brigadier.Command;
import net.minecraft.item.ItemStack;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ItemDupeMod implements ModInitializer {
    public static final String MOD_ID = "itemdupemod";
    public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

    @Override
    public void onInitialize() {
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
            dispatcher.register(CommandManager.literal("dupe")
                .requires(ServerCommandSource::isExecutedByPlayer)
                .executes(this::executeDupe)
            );
        });
    }

    private int executeDupe(com.mojang.brigadier.context.CommandContext<ServerCommandSource> context) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
        ServerCommandSource source = context.getSource();
        ServerPlayerEntity player = source.getPlayerOrThrow();

        ItemStack heldStack = player.getStackInHand(Hand.MAIN_HAND);

        if (heldStack.isEmpty()) {
            source.sendError(Text.literal("You are not holding any item to duplicate."));
            return 0;
        }

        ItemStack duplicate = heldStack.copy();
        player.dropItem(duplicate, false, false);

        source.sendFeedback(() -> Text.literal("Duplicated " + heldStack.getName().getString() + "!"), false);
        return Command.SINGLE_SUCCESS;
    }
}