BloodMagic/src/main/java/WayofTime/alchemicalWizardry/common/items/sigil/SigilOfGrowth.java

238 lines
7.6 KiB
Java
Raw Normal View History

package WayofTime.alchemicalWizardry.common.items.sigil;
2014-10-13 22:33:20 +02:00
import WayofTime.alchemicalWizardry.api.items.interfaces.ArmourUpgrade;
2015-05-18 18:33:23 -04:00
import WayofTime.alchemicalWizardry.api.items.interfaces.ISigil;
2015-07-30 17:24:20 -04:00
import WayofTime.alchemicalWizardry.common.items.BindableItems;
import net.minecraft.block.IGrowable;
2015-07-31 11:35:05 -04:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-07-31 11:35:05 -04:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.BonemealEvent;
2015-07-31 11:35:05 -04:00
import net.minecraftforge.fml.common.eventhandler.Event;
2014-10-13 22:33:20 +02:00
import java.util.List;
2015-07-31 11:35:05 -04:00
public class SigilOfGrowth extends SigilToggleable implements ArmourUpgrade, ISigil
{
private int tickDelay = 100;
public SigilOfGrowth()
{
super();
setEnergyUsed(150);
}
@Override
public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4)
{
par3List.add(StatCollector.translateToLocal("tooltip.sigilofgrowth.desc1"));
par3List.add(StatCollector.translateToLocal("tooltip.sigilofgrowth.desc2"));
2015-01-16 10:00:50 -05:00
if (!(par1ItemStack.getTagCompound() == null))
{
2015-07-31 11:35:05 -04:00
if (this.getActivated(par1ItemStack))
{
par3List.add(StatCollector.translateToLocal("tooltip.sigil.state.activated"));
} else
{
par3List.add(StatCollector.translateToLocal("tooltip.sigil.state.deactivated"));
}
par3List.add(StatCollector.translateToLocal("tooltip.owner.currentowner") + " " + par1ItemStack.getTagCompound().getString("ownerName"));
}
}
@Override
2015-07-31 11:35:05 -04:00
public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{
2015-07-31 11:35:05 -04:00
if (BindableItems.checkAndSetItemOwner(stack, playerIn))
{
2015-07-31 11:35:05 -04:00
if (applyBonemeal(stack, worldIn, pos, playerIn))
{
2015-07-31 11:35:05 -04:00
BindableItems.syphonBatteries(stack, playerIn, getEnergyUsed());
2015-07-31 11:35:05 -04:00
if (worldIn.isRemote)
{
2015-07-31 11:35:05 -04:00
worldIn.playAuxSFX(2005, pos, 0);
return true;
}
return true;
}
}
return false;
}
@Override
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
2015-07-30 17:24:20 -04:00
if (!BindableItems.checkAndSetItemOwner(par1ItemStack, par3EntityPlayer) || par3EntityPlayer.isSneaking())
2014-07-31 19:45:40 -04:00
{
2014-10-13 22:33:20 +02:00
return par1ItemStack;
2014-07-31 19:45:40 -04:00
}
if (par2World.isRemote)
{
return par1ItemStack;
}
2015-01-16 10:00:50 -05:00
if (par1ItemStack.getTagCompound() == null)
{
par1ItemStack.setTagCompound(new NBTTagCompound());
}
2015-01-16 10:00:50 -05:00
NBTTagCompound tag = par1ItemStack.getTagCompound();
tag.setBoolean("isActive", !(tag.getBoolean("isActive")));
2015-07-30 17:24:20 -04:00
if (tag.getBoolean("isActive") && BindableItems.syphonBatteries(par1ItemStack, par3EntityPlayer, getEnergyUsed()))
{
par1ItemStack.setItemDamage(1);
tag.setInteger("worldTimeDelay", (int) (par2World.getWorldTime() - 1) % tickDelay);
} else
{
par1ItemStack.setItemDamage(par1ItemStack.getMaxDamage());
}
return par1ItemStack;
}
@Override
public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5)
{
if (!(par3Entity instanceof EntityPlayer) || par2World.isRemote)
{
return;
}
2015-01-16 10:00:50 -05:00
if (par1ItemStack.getTagCompound() == null)
{
par1ItemStack.setTagCompound(new NBTTagCompound());
}
2015-07-31 11:35:05 -04:00
if (this.getActivated(par1ItemStack))
{
if (par2World.getWorldTime() % tickDelay == par1ItemStack.getTagCompound().getInteger("worldTimeDelay"))
{
2015-07-30 17:24:20 -04:00
if(!BindableItems.syphonBatteries(par1ItemStack, (EntityPlayer) par3Entity, getEnergyUsed()))
{
2015-07-31 11:35:05 -04:00
this.setActivated(par1ItemStack, false);
}
}
int range = 3;
int verticalRange = 2;
int posX = (int) Math.round(par3Entity.posX - 0.5f);
int posY = (int) par3Entity.posY;
int posZ = (int) Math.round(par3Entity.posZ - 0.5f);
for (int ix = posX - range; ix <= posX + range; ix++)
{
for (int iz = posZ - range; iz <= posZ + range; iz++)
{
for (int iy = posY - verticalRange; iy <= posY + verticalRange; iy++)
{
2015-07-31 11:35:05 -04:00
IBlockState block = par2World.getBlockState(new BlockPos(ix, iy, iz));
if (block instanceof IPlantable || block instanceof IGrowable)
{
2014-11-19 18:48:26 -05:00
if (par2World.rand.nextInt(50) == 0)
{
2015-07-31 11:35:05 -04:00
block.getBlock().updateTick(par2World, new BlockPos(ix, iy, iz), block, par2World.rand);
}
}
}
}
}
}
}
2015-07-31 11:35:05 -04:00
public static boolean applyBonemeal(ItemStack p_179234_0_, World world, BlockPos blockPos, EntityPlayer player)
{
2015-07-31 11:35:05 -04:00
IBlockState block = world.getBlockState(blockPos);
2015-07-31 11:35:05 -04:00
BonemealEvent event = new BonemealEvent(player, world, blockPos, block);
if (MinecraftForge.EVENT_BUS.post(event))
{
return false;
}
2015-07-31 11:35:05 -04:00
if (event.getResult() == Event.Result.ALLOW)
{
return true;
}
if (block instanceof IGrowable)
{
2014-10-13 22:33:20 +02:00
IGrowable igrowable = (IGrowable) block;
2015-07-31 11:35:05 -04:00
if (igrowable.isStillGrowing(world, blockPos, block, world.isRemote))
{
2015-07-31 11:35:05 -04:00
if (!world.isRemote)
{
2015-07-31 11:35:05 -04:00
if (igrowable.canUseBonemeal(world, world.rand, blockPos, block))
{
2015-07-31 11:35:05 -04:00
igrowable.grow(world, world.rand, blockPos, block);
}
}
return true;
}
}
return false;
}
@Override
public void onArmourUpdate(World world, EntityPlayer player, ItemStack thisItemStack)
{
2014-10-13 22:33:20 +02:00
if (world.isRemote)
2014-07-31 19:45:40 -04:00
{
2014-10-13 22:33:20 +02:00
return;
2014-07-31 19:45:40 -04:00
}
2014-10-13 22:33:20 +02:00
int range = 5;
int verticalRange = 2;
int posX = (int) Math.round(player.posX - 0.5f);
int posY = (int) player.posY;
int posZ = (int) Math.round(player.posZ - 0.5f);
for (int ix = posX - range; ix <= posX + range; ix++)
{
for (int iz = posZ - range; iz <= posZ + range; iz++)
{
for (int iy = posY - verticalRange; iy <= posY + verticalRange; iy++)
{
2015-07-31 11:35:05 -04:00
IBlockState block = world.getBlockState(new BlockPos(ix, iy, iz));
2014-10-13 22:33:20 +02:00
if (block instanceof IPlantable)
{
2014-11-19 18:48:26 -05:00
if (world.rand.nextInt(100) == 0)
{
2015-07-31 11:35:05 -04:00
block.getBlock().updateTick(world, new BlockPos(ix, iy, iz), block, world.rand);
}
}
}
}
}
}
@Override
public boolean isUpgrade()
{
return true;
}
@Override
public int getEnergyForTenSeconds()
{
return 50;
}
}