diff --git a/src/main/java/de/jottyfan/gta/gdp/item/ModItems.java b/src/main/java/de/jottyfan/gta/gdp/item/ModItems.java index 7dc365f..752d583 100644 --- a/src/main/java/de/jottyfan/gta/gdp/item/ModItems.java +++ b/src/main/java/de/jottyfan/gta/gdp/item/ModItems.java @@ -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); diff --git a/src/main/java/de/jottyfan/gta/gdp/item/StubItem.java b/src/main/java/de/jottyfan/gta/gdp/item/StubItem.java new file mode 100644 index 0000000..fe68231 --- /dev/null +++ b/src/main/java/de/jottyfan/gta/gdp/item/StubItem.java @@ -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 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; + } + + +}