2015-03-07 12:51:28 +01:00
|
|
|
package ellpeck.actuallyadditions.tile;
|
2015-02-17 16:15:16 +01:00
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
import cpw.mods.fml.common.network.NetworkRegistry;
|
2015-02-17 16:15:16 +01:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2015-04-24 19:22:03 +02:00
|
|
|
import ellpeck.actuallyadditions.config.values.ConfigIntValues;
|
2015-03-07 12:51:28 +01:00
|
|
|
import ellpeck.actuallyadditions.network.PacketHandler;
|
|
|
|
import ellpeck.actuallyadditions.network.PacketTileEntityFeeder;
|
2015-02-17 16:15:16 +01:00
|
|
|
import net.minecraft.entity.passive.EntityAnimal;
|
2015-03-07 02:23:31 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2015-02-17 16:15:16 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.AxisAlignedBB;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
public class TileEntityFeeder extends TileEntityInventoryBase{
|
|
|
|
|
2015-04-24 19:22:03 +02:00
|
|
|
public int reach = ConfigIntValues.FEEDER_REACH.getValue();
|
|
|
|
public int timerGoal = ConfigIntValues.FEEDER_TIME.getValue();
|
|
|
|
public int animalThreshold = ConfigIntValues.FEEDER_THRESHOLD.getValue();
|
2015-02-17 16:15:16 +01:00
|
|
|
|
|
|
|
public int currentTimer;
|
|
|
|
public int currentAnimalAmount;
|
|
|
|
|
|
|
|
public TileEntityFeeder(){
|
2015-04-19 01:50:02 +02:00
|
|
|
super(1, "feeder");
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void updateEntity(){
|
|
|
|
if(!worldObj.isRemote){
|
2015-03-19 21:27:56 +01:00
|
|
|
boolean theFlag = this.currentTimer > 0;
|
2015-02-17 16:15:16 +01:00
|
|
|
List<EntityAnimal> animals = worldObj.getEntitiesWithinAABB(EntityAnimal.class, AxisAlignedBB.getBoundingBox(this.xCoord - reach, this.yCoord - reach, this.zCoord - reach, this.xCoord + reach, this.yCoord + reach, this.zCoord + reach));
|
|
|
|
if(animals != null){
|
|
|
|
this.currentAnimalAmount = animals.size();
|
2015-03-08 14:58:26 +01:00
|
|
|
if(this.currentAnimalAmount >= 2){
|
2015-02-17 16:15:16 +01:00
|
|
|
if(this.currentAnimalAmount < this.animalThreshold){
|
2015-03-19 21:27:56 +01:00
|
|
|
if(this.currentTimer >= this.timerGoal){
|
2015-02-17 16:15:16 +01:00
|
|
|
this.currentTimer = 0;
|
2015-03-19 21:27:56 +01:00
|
|
|
if(this.slots[0] != null){
|
|
|
|
EntityAnimal randomAnimal = animals.get(new Random().nextInt(this.currentAnimalAmount));
|
|
|
|
if(!randomAnimal.isInLove() && randomAnimal.getGrowingAge() == 0 && randomAnimal.isBreedingItem(this.slots[0])){
|
2015-02-20 22:45:33 +01:00
|
|
|
|
2015-03-19 21:27:56 +01:00
|
|
|
PacketHandler.theNetwork.sendToAllAround(new PacketTileEntityFeeder(this, randomAnimal.getEntityId()), new NetworkRegistry.TargetPoint(this.worldObj.provider.dimensionId, this.xCoord, this.yCoord, this.zCoord, 80));
|
|
|
|
this.feedAnimal(randomAnimal);
|
2015-02-20 22:45:33 +01:00
|
|
|
|
2015-03-19 21:27:56 +01:00
|
|
|
this.slots[0].stackSize--;
|
|
|
|
if(this.slots[0].stackSize == 0)
|
|
|
|
this.slots[0] = this.slots[0].getItem().getContainerItem(this.slots[0]);
|
|
|
|
}
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-20 22:45:33 +01:00
|
|
|
else this.currentTimer++;
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
2015-02-20 22:45:33 +01:00
|
|
|
else this.currentTimer = 0;
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
2015-02-20 22:45:33 +01:00
|
|
|
else this.currentTimer = 0;
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
2015-03-19 21:27:56 +01:00
|
|
|
|
|
|
|
if(theFlag != this.currentTimer > 0){
|
|
|
|
this.markDirty();
|
|
|
|
}
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
public void feedAnimal(EntityAnimal animal){
|
|
|
|
animal.func_146082_f(null);
|
|
|
|
Random rand = new Random();
|
|
|
|
for(int i = 0; i < 7; i++){
|
|
|
|
double d = rand.nextGaussian() * 0.02D;
|
|
|
|
double d1 = rand.nextGaussian() * 0.02D;
|
|
|
|
double d2 = rand.nextGaussian() * 0.02D;
|
|
|
|
worldObj.spawnParticle("heart", (animal.posX + (double)(rand.nextFloat() * animal.width * 2.0F)) - animal.width, animal.posY + 0.5D + (double)(rand.nextFloat() * animal.height), (animal.posZ + (double)(rand.nextFloat() * animal.width * 2.0F)) - animal.width, d, d1, d2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 16:15:16 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getCurrentTimerToScale(int i){
|
|
|
|
return this.currentTimer * i / this.timerGoal;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public void writeToNBT(NBTTagCompound compound){
|
|
|
|
super.writeToNBT(compound);
|
|
|
|
compound.setInteger("Timer", this.currentTimer);
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public void readFromNBT(NBTTagCompound compound){
|
|
|
|
super.readFromNBT(compound);
|
|
|
|
this.currentTimer = compound.getInteger("Timer");
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|