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;
}
}