adding custom item

This commit is contained in:
Jottyfan
2025-04-06 17:33:48 +02:00
parent 075f2d718f
commit f516aba128
2 changed files with 64 additions and 1 deletions

View File

@ -18,7 +18,7 @@ import net.minecraft.util.Identifier;
*/
public class ModItems {
public static final Item STUB = registerItem(Identifier.of(GTAGDP.MOD_ID, "stub"), new Item.Settings());
public static final Item STUB = registerStubItem(Identifier.of(GTAGDP.MOD_ID, "stub"), new Item.Settings());
public static final Item RUBYBALL = registerItem(Identifier.of(GTAGDP.MOD_ID, "rubyball"), new Item.Settings());
private static Item registerItem(Identifier identifier, Settings settings) {
@ -26,6 +26,11 @@ public class ModItems {
new Item(settings.useItemPrefixedTranslationKey().registryKey(RegistryKey.of(RegistryKeys.ITEM, identifier))));
}
private static Item registerStubItem(Identifier identifier, Settings settings) {
return Registry.register(Registries.ITEM, identifier, new StubItem(
settings.useItemPrefixedTranslationKey().registryKey(RegistryKey.of(RegistryKeys.ITEM, identifier))));
}
public static void registerModItems() {
GTAGDP.LOGGER.info("registering mod items for " + GTAGDP.MOD_ID);

View File

@ -0,0 +1,58 @@
package de.jottyfan.gta.gdp.item;
import java.util.Map;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.entity.ItemEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
/**
*
* @author jotty
*
*/
public class StubItem extends Item {
private static final Map<Block, Item> SLASH_MAP = Map.of(
Blocks.HAY_BLOCK, Items.WHEAT,
Blocks.DRIED_KELP_BLOCK, Items.DRIED_KELP);
public StubItem(Settings settings) {
super(settings);
}
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
World world = context.getWorld();
BlockPos pos = context.getBlockPos();
Block clickedBlock = world.getBlockState(pos).getBlock();
if (SLASH_MAP.containsKey(clickedBlock)) {
if (!world.isClient()) {
world.setBlockState(pos, Blocks.AIR.getDefaultState());
world.playSound(null, pos, SoundEvents.ITEM_HOE_TILL,
SoundCategory.BLOCKS);
for (int i = 0; i < 9; i++) {
ItemStack stack = new ItemStack(SLASH_MAP.get(clickedBlock));
float xScatter = new Random().nextFloat();
float yScatter = new Random().nextFloat();
ItemEntity entity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(),
stack, xScatter, yScatter, 0.2);
world.spawnEntity(entity);
}
}
}
return ActionResult.SUCCESS;
}
}