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

105 lines
4.3 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;
2017-02-06 13:43:38 +01:00
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-07-22 20:23:51 +02:00
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
import java.util.List;
public class ItemWorm extends ItemBase{
public ItemWorm(String name){
super(name);
MinecraftForge.EVENT_BUS.register(this);
2017-02-06 13:43:38 +01:00
this.addPropertyOverride(new ResourceLocation(ModUtil.MOD_ID, "snail"), new IItemPropertyGetter(){
@Override
@SideOnly(Side.CLIENT)
public float apply(ItemStack stack, World world, EntityLivingBase entity){
return "snail mail".equalsIgnoreCase(stack.getDisplayName()) ? 1F : 0F;
}
});
2016-07-22 20:23:51 +02:00
}
@Override
2016-11-19 21:11:17 +01:00
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float par8, float par9, float par10){
ItemStack stack = player.getHeldItem(hand);
2016-07-22 20:23:51 +02:00
IBlockState state = world.getBlockState(pos);
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){
EntityWorm worm = new EntityWorm(world);
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);
2017-02-04 16:48:22 +01:00
2016-12-23 17:00:18 +01:00
if(!player.capabilities.isCreativeMode){
player.setHeldItem(hand, StackUtil.addStackSize(stack, -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)
2016-07-22 20:23:51 +02:00
public void onHoe(UseHoeEvent event){
2018-01-26 08:34:20 +01:00
if(ConfigBoolValues.WORMS.isEnabled() && event.getResult() != Result.DENY){
2016-07-22 22:52:18 +02:00
World world = event.getWorld();
if(!world.isRemote){
BlockPos pos = event.getPos();
if(world.isAirBlock(pos.up())){
IBlockState state = world.getBlockState(pos);
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
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.UNCOMMON;
}
}