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

86 lines
3.7 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;
2016-07-22 22:52:18 +02:00
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
2016-07-22 20:23:51 +02:00
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;
2016-07-22 20:23:51 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
2016-07-22 20:23:51 +02:00
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;
2021-02-26 22:15:48 +01:00
import java.util.List;
2016-07-22 20:23:51 +02:00
2019-05-02 09:10:29 +02:00
public class ItemWorm extends ItemBase {
2016-07-22 20:23:51 +02:00
public ItemWorm() {
super();
2016-07-22 20:23:51 +02:00
MinecraftForge.EVENT_BUS.register(this);
2017-02-06 13:43:38 +01:00
// 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);
2016-07-22 20:23:51 +02:00
}
@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());
2019-05-02 09:10:29 +02:00
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()) {
2021-02-26 22:15:48 +01:00
stack.shrink(1);
}
2016-07-22 20:23:51 +02:00
}
return ActionResultType.SUCCESS;
2016-07-22 20:23:51 +02:00
}
}
return super.onItemUse(context);
2016-07-22 20:23:51 +02:00
}
2018-01-26 08:34:20 +01:00
@SubscribeEvent(priority = EventPriority.LOW)
2019-05-02 09:10:29 +02:00
public void onHoe(UseHoeEvent event) {
if (ConfigBoolValues.WORMS.isEnabled() && event.getResult() != Event.Result.DENY) {
World world = event.getEntity().world;
2019-05-02 09:10:29 +02:00
if (!world.isRemote) {
BlockPos pos = event.getContext().getPos();
2019-05-02 09:10:29 +02:00
if (world.isAirBlock(pos.up())) {
2021-02-26 22:15:48 +01:00
BlockState state = world.getBlockState(pos);
if (state.getBlock() == Blocks.GRASS && world.rand.nextFloat() >= 0.95F) {
2021-05-02 18:10:21 +02:00
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);
}
2016-07-22 22:52:18 +02:00
}
2016-07-22 20:23:51 +02:00
}
}
}
}