Files
src
.gitignore
build.gradle
gradle.properties
settings.gradle
Code
12 268
src/main/java/com/crateduplicator/CrateDuplicator.java
package com.crateduplicator;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.crateduplicator.item.TheftKeyItem;
public class CrateDuplicator implements ModInitializer {
public static final String MOD_ID = "crateduplicator";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static Item THEFT_KEY;
public static ItemGroup CRATE_DUPLICATOR_GROUP;
@Override
public void onInitialize() {
RegistryKey<Item> theftKeyKey = RegistryKey.of(RegistryKeys.ITEM, Identifier.of(MOD_ID, "theft_key"));
THEFT_KEY = Registry.register(
Registries.ITEM,
theftKeyKey,
new TheftKeyItem(new Item.Settings().maxCount(1).registryKey(theftKeyKey))
);
RegistryKey<ItemGroup> groupKey = RegistryKey.of(RegistryKeys.ITEM_GROUP, Identifier.of(MOD_ID, "crate_duplicator"));
CRATE_DUPLICATOR_GROUP = Registry.register(
Registries.ITEM_GROUP,
groupKey,
FabricItemGroup.builder()
.icon(() -> new ItemStack(THEFT_KEY))
.displayName(Text.translatable("itemGroup.crateduplicator.crate_duplicator"))
.entries((context, entries) -> entries.add(THEFT_KEY))
.build()
);
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
if (player.isSpectator()) {
return ActionResult.PASS;
}
ItemStack heldItem = player.getStackInHand(hand);
if (!heldItem.isOf(THEFT_KEY)) {
return ActionResult.PASS;
}
BlockEntity blockEntity = world.getBlockEntity(hitResult.getBlockPos());
if (!(blockEntity instanceof Inventory inventory)) {
return ActionResult.PASS;
}
if (!world.isClient) {
int duplicatedCount = 0;
for (int i = 0; i < inventory.size(); i++) {
ItemStack stack = inventory.getStack(i);
if (!stack.isEmpty()) {
ItemStack copy = stack.copy();
if (player.getInventory().insertStack(copy)) {
duplicatedCount++;
} else {
player.dropItem(copy, false);
}
}
}
if (duplicatedCount > 0) {
world.playSound(null, hitResult.getBlockPos(), SoundEvents.BLOCK_CHEST_OPEN, SoundCategory.BLOCKS, 1.0f, 1.2f);
world.playSound(null, player.getBlockPos(), SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.8f, 1.5f);
player.sendMessage(Text.literal("§6Duplicated " + duplicatedCount + " item stacks!"), true);
LOGGER.info("Player {} duplicated {} item stacks from container at {}",
player.getName().getString(), duplicatedCount, hitResult.getBlockPos());
}
}
return ActionResult.SUCCESS;
});
LOGGER.info("Crate Duplicator initialized");
}
}