ActuallyAdditions/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFeeder.java

126 lines
4.9 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("TileEntityFeeder.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://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2015-11-02 20:55:19 +01:00
* © 2015 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2015-03-07 12:51:28 +01:00
package ellpeck.actuallyadditions.tile;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ellpeck.actuallyadditions.config.values.ConfigIntValues;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import java.util.List;
public class TileEntityFeeder extends TileEntityInventoryBase{
public int currentTimer;
public int currentAnimalAmount;
private int lastAnimalAmount;
private int lastTimer;
public TileEntityFeeder(){
2015-04-19 01:50:02 +02:00
super(1, "feeder");
}
@Override
@SuppressWarnings("unchecked")
public void updateEntity(){
if(!worldObj.isRemote){
2015-03-19 21:27:56 +01:00
boolean theFlag = this.currentTimer > 0;
2015-10-02 16:48:01 +02:00
List<EntityAnimal> animals = worldObj.getEntitiesWithinAABB(EntityAnimal.class, AxisAlignedBB.getBoundingBox(this.xCoord-ConfigIntValues.FEEDER_REACH.getValue(), this.yCoord-ConfigIntValues.FEEDER_REACH.getValue(), this.zCoord-ConfigIntValues.FEEDER_REACH.getValue(), this.xCoord+ConfigIntValues.FEEDER_REACH.getValue(), this.yCoord+ConfigIntValues.FEEDER_REACH.getValue(), this.zCoord+ConfigIntValues.FEEDER_REACH.getValue()));
if(animals != null){
this.currentAnimalAmount = animals.size();
2015-03-08 14:58:26 +01:00
if(this.currentAnimalAmount >= 2){
2015-08-15 20:41:45 +02:00
if(this.currentAnimalAmount < ConfigIntValues.FEEDER_THRESHOLD.getValue()){
if(this.currentTimer >= ConfigIntValues.FEEDER_TIME.getValue()){
this.currentTimer = 0;
2015-03-19 21:27:56 +01:00
if(this.slots[0] != null){
EntityAnimal randomAnimal = animals.get(worldObj.rand.nextInt(this.currentAnimalAmount));
2015-03-19 21:27:56 +01:00
if(!randomAnimal.isInLove() && randomAnimal.getGrowingAge() == 0 && randomAnimal.isBreedingItem(this.slots[0])){
2015-03-19 21:27:56 +01:00
this.feedAnimal(randomAnimal);
2015-03-19 21:27:56 +01:00
this.slots[0].stackSize--;
2015-10-02 16:48:01 +02:00
if(this.slots[0].stackSize == 0){
2015-03-19 21:27:56 +01:00
this.slots[0] = this.slots[0].getItem().getContainerItem(this.slots[0]);
2015-10-02 16:48:01 +02:00
}
2015-03-19 21:27:56 +01:00
}
}
}
2015-10-02 16:48:01 +02:00
else{
this.currentTimer++;
}
}
else{
this.currentTimer = 0;
}
}
2015-10-02 16:48:01 +02:00
else{
this.currentTimer = 0;
}
}
2015-03-19 21:27:56 +01:00
if(theFlag != this.currentTimer > 0){
this.markDirty();
}
if(this.lastAnimalAmount != this.currentAnimalAmount || this.lastTimer != this.currentTimer){
this.lastAnimalAmount = this.currentAnimalAmount;
this.lastTimer = this.currentTimer;
this.sendUpdate();
}
}
}
public void feedAnimal(EntityAnimal animal){
animal.func_146082_f(null);
for(int i = 0; i < 7; i++){
double d = animal.worldObj.rand.nextGaussian()*0.02D;
double d1 = animal.worldObj.rand.nextGaussian()*0.02D;
double d2 = animal.worldObj.rand.nextGaussian()*0.02D;
worldObj.spawnParticle("heart", (animal.posX+(double)(animal.worldObj.rand.nextFloat()*animal.width*2.0F))-animal.width, animal.posY+0.5D+(double)(animal.worldObj.rand.nextFloat()*animal.height), (animal.posZ+(double)(animal.worldObj.rand.nextFloat()*animal.width*2.0F))-animal.width, d, d1, d2);
}
}
@SideOnly(Side.CLIENT)
public int getCurrentTimerToScale(int i){
2015-10-02 16:48:01 +02:00
return this.currentTimer*i/ConfigIntValues.FEEDER_TIME.getValue();
}
2015-10-03 22:13:57 +02:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
super.writeSyncableNBT(compound, sync);
2015-10-03 22:13:57 +02:00
compound.setInteger("Timer", this.currentTimer);
}
2015-10-23 16:54:33 +02:00
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
super.readSyncableNBT(compound, sync);
this.currentTimer = compound.getInteger("Timer");
}
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
return true;
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side){
return this.isItemValidForSlot(slot, stack);
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side){
return false;
}
}