ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/Worm.java

82 lines
3.2 KiB
Java
Raw Normal View History

2016-07-22 20:23:51 +02:00
/*
* This file ("ItemWorm.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2016-07-22 20:23:51 +02:00
*/
package de.ellpeck.actuallyadditions.mod.items;
2021-11-13 20:09:55 +01:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2021-11-14 19:18:07 +01:00
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
2016-07-22 20:23:51 +02:00
import de.ellpeck.actuallyadditions.mod.entity.EntityWorm;
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
2024-03-04 20:21:48 +01:00
import net.neoforged.bus.api.Event;
import net.neoforged.neoforge.common.ToolActions;
import net.neoforged.neoforge.event.level.BlockEvent;
2021-02-26 22:15:48 +01:00
import java.util.List;
2016-07-22 20:23:51 +02:00
2023-12-20 22:02:25 +01:00
public class Worm extends ItemBase {
2016-07-22 20:23:51 +02:00
2023-12-20 22:02:25 +01:00
public Worm() {
super();
2016-07-22 20:23:51 +02:00
}
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult useOn(UseOnContext context) {
BlockPos pos = context.getClickedPos();
ItemStack stack = context.getPlayer().getItemInHand(context.getHand());
2024-03-02 21:23:08 +01:00
Level level = context.getLevel();
2023-05-16 02:23:26 +02:00
BlockState state = level.getBlockState(pos);
if (level.isClientSide || !EntityWorm.canWormify(level, context.getClickedPos(), state))
return super.useOn(context);
2024-03-02 21:23:08 +01:00
List<EntityWorm> worms = level.getEntitiesOfClass(EntityWorm.class, new AABB(pos.getX() - 1, pos.getY(), pos.getZ() - 1, pos.getX() + 2, pos.getY() + 1, pos.getZ() + 2));
2023-05-16 02:23:26 +02:00
if (!worms.isEmpty())
return super.useOn(context);
EntityWorm worm = new EntityWorm(ActuallyAdditions.ENTITY_WORM.get(), level);
worm.setPos(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
worm.setCustomName(stack.getHoverName());
level.addFreshEntity(worm);
if (!context.getPlayer().isCreative())
stack.shrink(1);
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
2016-07-22 20:23:51 +02:00
}
2024-03-03 01:20:53 +01:00
public static void onHoe(BlockEvent.BlockToolModificationEvent event) {
if (event.getToolAction() == ToolActions.HOE_TILL) {
Level level = event.getPlayer().level();
2023-05-16 02:23:26 +02:00
2024-03-03 01:20:53 +01:00
if (level.isClientSide || !CommonConfig.Other.WORMS.get() || event.getResult() == Event.Result.DENY)
return;
2023-05-16 02:23:26 +02:00
2024-03-03 01:20:53 +01:00
BlockPos pos = event.getContext().getClickedPos();
if (level.isEmptyBlock(pos.above())) {
BlockState state = level.getBlockState(pos);
if (state.getBlock() == Blocks.GRASS_BLOCK && level.random.nextFloat() >= 0.95F) {
ItemStack stack = new ItemStack(ActuallyItems.WORM.get(), level.random.nextInt(2) + 1);
ItemEntity item = new ItemEntity(level, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5, stack);
2023-05-16 02:23:26 +02:00
2024-03-03 01:20:53 +01:00
level.addFreshEntity(item);
}
2016-07-22 20:23:51 +02:00
}
}
}
}