ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGreenhouseGlass.java

82 lines
3 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("TileEntityGreenhouseGlass.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
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-04-19 01:50:02 +02:00
2016-01-08 13:31:58 +01:00
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.Util;
2015-04-19 01:50:02 +02:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockGrass;
import net.minecraft.block.IGrowable;
import net.minecraft.nbt.NBTTagCompound;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.IPlantable;
2015-04-19 01:50:02 +02:00
public class TileEntityGreenhouseGlass extends TileEntityBase{
private int timeUntilNextFert;
2016-02-01 17:49:55 +01:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean isForSync){
super.writeSyncableNBT(compound, isForSync);
this.timeUntilNextFert = compound.getInteger("Time");
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean isForSync){
super.readSyncableNBT(compound, isForSync);
compound.setInteger("Time", this.timeUntilNextFert);
}
2015-04-19 01:50:02 +02:00
@Override
public void updateEntity(){
super.updateEntity();
2016-05-02 17:46:53 +02:00
if(!this.worldObj.isRemote){
if(this.worldObj.canBlockSeeSky(this.getPos()) && this.worldObj.isDaytime()){
2015-11-29 00:38:22 +01:00
if(this.timeUntilNextFert > 0){
this.timeUntilNextFert--;
2016-05-02 17:46:53 +02:00
if(this.timeUntilNextFert <= 0){
2016-01-08 13:31:58 +01:00
BlockPos blockToFert = this.blockToFertilize();
if(blockToFert != null){
2016-05-02 17:46:53 +02:00
int metaBefore = PosUtil.getMetadata(blockToFert, this.worldObj);
PosUtil.getBlock(blockToFert, this.worldObj).updateTick(this.worldObj, blockToFert, this.worldObj.getBlockState(blockToFert), Util.RANDOM);
2016-05-02 17:46:53 +02:00
if(PosUtil.getMetadata(blockToFert, this.worldObj) != metaBefore){
this.worldObj.playAuxSFX(2005, blockToFert, 0);
}
2015-04-19 01:50:02 +02:00
}
}
2015-11-29 00:38:22 +01:00
}
else{
int time = 300;
this.timeUntilNextFert = time+Util.RANDOM.nextInt(time);
2015-04-19 01:50:02 +02:00
}
}
}
}
2016-01-08 13:31:58 +01:00
public BlockPos blockToFertilize(){
for(int i = -1; i > 0; i--){
2016-01-08 13:31:58 +01:00
BlockPos offset = PosUtil.offset(this.pos, 0, i, 0);
2016-05-02 17:46:53 +02:00
Block block = PosUtil.getBlock(this.pos, this.worldObj);
if(block != null && !(this.worldObj.isAirBlock(offset))){
if((block instanceof IGrowable || block instanceof IPlantable) && !(block instanceof BlockGrass)){
return offset;
2015-04-19 01:50:02 +02:00
}
2015-10-02 16:48:01 +02:00
else{
return null;
}
2015-04-19 01:50:02 +02:00
}
}
return null;
}
}