ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemWorm.java
2021-05-02 11:10:21 -05:00

86 lines
3.7 KiB
Java

/*
* 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
*
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.items;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
import de.ellpeck.actuallyadditions.mod.entity.EntityWorm;
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.UseHoeEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import java.util.List;
public class ItemWorm extends ItemBase {
public ItemWorm() {
super();
MinecraftForge.EVENT_BUS.register(this);
// TODO: [port] Not sure what this does
// this.addPropertyOverride(new ResourceLocation(ActuallyAdditions.MODID, "snail"), (IItemPropertyGetter) (stack, world, entity) -> "snail mail".equalsIgnoreCase(stack.getDisplayName().getString())
// ? 1F
// : 0F);
}
@Override
public ActionResultType onItemUse(ItemUseContext context) {
BlockPos pos = context.getPos();
ItemStack stack = context.getPlayer().getHeldItem(context.getHand());
BlockState state = context.getWorld().getBlockState(pos);
if (EntityWorm.canWormify(context.getWorld(), context.getPos(), state)) {
List<EntityWorm> worms = context.getWorld().getEntitiesWithinAABB(EntityWorm.class, new AxisAlignedBB(pos.getX() - 1, pos.getY(), pos.getZ() - 1, pos.getX() + 2, pos.getY() + 1, pos.getZ() + 2));
if (worms.isEmpty()) {
if (!context.getWorld().isRemote) {
EntityWorm worm = new EntityWorm(context.getWorld());
worm.setPosition(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
worm.setCustomName(stack.getDisplayName()); // TODO: WHAT DOES THIS EVEN DO?
context.getWorld().addEntity(worm);
if (!context.getPlayer().isCreative()) {
stack.shrink(1);
}
}
return ActionResultType.SUCCESS;
}
}
return super.onItemUse(context);
}
@SubscribeEvent(priority = EventPriority.LOW)
public void onHoe(UseHoeEvent event) {
if (ConfigBoolValues.WORMS.isEnabled() && event.getResult() != Event.Result.DENY) {
World world = event.getEntity().world;
if (!world.isRemote) {
BlockPos pos = event.getContext().getPos();
if (world.isAirBlock(pos.up())) {
BlockState state = world.getBlockState(pos);
if (state.getBlock() == Blocks.GRASS && world.rand.nextFloat() >= 0.95F) {
ItemStack stack = new ItemStack(ActuallyItems.WORM.get(), world.rand.nextInt(2) + 1);
ItemEntity item = new ItemEntity(world, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5, stack);
world.addEntity(item);
}
}
}
}
}
}