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

91 lines
3.9 KiB
Java
Raw Normal View History

2016-07-22 20:23:51 +02:00
package de.ellpeck.actuallyadditions.mod.items;
import java.util.List;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
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.BlockGrass;
import net.minecraft.block.state.IBlockState;
2017-02-06 13:43:38 +01:00
import net.minecraft.entity.EntityLivingBase;
2016-07-22 20:23:51 +02:00
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
2017-02-06 13:43:38 +01:00
import net.minecraft.item.IItemPropertyGetter;
2016-07-22 20:23:51 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
2017-02-06 13:43:38 +01:00
import net.minecraft.util.ResourceLocation;
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;
2018-01-26 08:34:20 +01:00
import net.minecraftforge.fml.common.eventhandler.Event.Result;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
2016-07-22 20:23:51 +02:00
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
2017-02-06 13:43:38 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
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
2019-05-02 09:10:29 +02:00
public ItemWorm(String name) {
2016-07-22 20:23:51 +02:00
super(name);
MinecraftForge.EVENT_BUS.register(this);
2017-02-06 13:43:38 +01:00
2019-05-02 09:10:29 +02:00
this.addPropertyOverride(new ResourceLocation(ActuallyAdditions.MODID, "snail"), new IItemPropertyGetter() {
2017-02-06 13:43:38 +01:00
@Override
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public float apply(ItemStack stack, World world, EntityLivingBase entity) {
2017-02-06 13:43:38 +01:00
return "snail mail".equalsIgnoreCase(stack.getDisplayName()) ? 1F : 0F;
}
});
2016-07-22 20:23:51 +02:00
}
@Override
2019-05-02 09:10:29 +02:00
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float par8, float par9, float par10) {
2016-11-19 21:11:17 +01:00
ItemStack stack = player.getHeldItem(hand);
2016-07-22 20:23:51 +02:00
IBlockState state = world.getBlockState(pos);
2019-05-02 09:10:29 +02:00
if (EntityWorm.canWormify(world, pos, state)) {
List<EntityWorm> worms = world.getEntitiesWithinAABB(EntityWorm.class, new AxisAlignedBB(pos.getX() - 1, pos.getY(), pos.getZ() - 1, pos.getX() + 2, pos.getY() + 1, pos.getZ() + 2));
if (worms == null || worms.isEmpty()) {
if (!world.isRemote) {
2016-07-22 20:23:51 +02:00
EntityWorm worm = new EntityWorm(world);
2019-05-02 09:10:29 +02:00
worm.setPosition(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
2017-02-06 13:43:38 +01:00
worm.setCustomNameTag(stack.getDisplayName());
2016-11-26 21:32:27 +01:00
world.spawnEntity(worm);
2019-05-02 09:10:29 +02:00
if (!player.capabilities.isCreativeMode) stack.shrink(1);
2016-07-22 20:23:51 +02:00
}
return EnumActionResult.SUCCESS;
}
}
2016-11-19 21:11:17 +01:00
return super.onItemUse(player, world, pos, hand, side, par8, par9, par10);
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() != Result.DENY) {
2016-07-22 22:52:18 +02:00
World world = event.getWorld();
2019-05-02 09:10:29 +02:00
if (!world.isRemote) {
2016-07-22 22:52:18 +02:00
BlockPos pos = event.getPos();
2019-05-02 09:10:29 +02:00
if (world.isAirBlock(pos.up())) {
IBlockState state = world.getBlockState(pos);
2019-05-02 09:10:29 +02:00
if (state.getBlock() instanceof BlockGrass && world.rand.nextFloat() >= 0.95F) {
ItemStack stack = new ItemStack(InitItems.itemWorm, world.rand.nextInt(2) + 1);
EntityItem item = new EntityItem(event.getWorld(), pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5, stack);
2016-11-26 21:32:27 +01:00
world.spawnEntity(item);
}
2016-07-22 22:52:18 +02:00
}
2016-07-22 20:23:51 +02:00
}
}
}
@Override
2019-05-02 09:10:29 +02:00
public EnumRarity getRarity(ItemStack stack) {
2016-07-22 20:23:51 +02:00
return EnumRarity.UNCOMMON;
}
}