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
|
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-02-17 16:15:16 +01:00
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.Util;
|
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;
|
2016-01-07 23:42:42 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.EnumParticleTypes;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2015-02-17 16:15:16 +01:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-10-18 15:28:06 +02:00
|
|
|
public class TileEntityFeeder extends TileEntityInventoryBase{
|
2015-02-17 16:15:16 +01:00
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
public static final int THRESHOLD = 30;
|
|
|
|
private static final int TIME = 100;
|
2015-02-17 16:15:16 +01:00
|
|
|
public int currentTimer;
|
|
|
|
public int currentAnimalAmount;
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastAnimalAmount;
|
|
|
|
private int lastTimer;
|
2015-02-17 16:15:16 +01:00
|
|
|
|
|
|
|
public TileEntityFeeder(){
|
2015-04-19 01:50:02 +02:00
|
|
|
super(1, "feeder");
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getCurrentTimerToScale(int i){
|
|
|
|
return this.currentTimer*i/TIME;
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.writeSyncableNBT(compound, sync);
|
|
|
|
compound.setInteger("Timer", this.currentTimer);
|
|
|
|
if(sync){
|
|
|
|
compound.setInteger("Animals", this.currentAnimalAmount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.readSyncableNBT(compound, sync);
|
|
|
|
this.currentTimer = compound.getInteger("Timer");
|
|
|
|
if(sync){
|
|
|
|
this.currentAnimalAmount = compound.getInteger("Animals");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void updateEntity(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2015-02-17 16:15:16 +01:00
|
|
|
if(!worldObj.isRemote){
|
2015-03-19 21:27:56 +01:00
|
|
|
boolean theFlag = this.currentTimer > 0;
|
2015-11-28 19:02:01 +01:00
|
|
|
int range = 5;
|
2016-03-18 23:47:22 +01:00
|
|
|
List<EntityAnimal> animals = worldObj.getEntitiesWithinAABB(EntityAnimal.class, new AxisAlignedBB(this.pos.getX()-range, this.pos.getY()-range, this.pos.getZ()-range, this.pos.getX()+range, this.pos.getY()+range, this.pos.getZ()+range));
|
2015-02-17 16:15:16 +01:00
|
|
|
if(animals != null){
|
|
|
|
this.currentAnimalAmount = animals.size();
|
2015-03-08 14:58:26 +01:00
|
|
|
if(this.currentAnimalAmount >= 2){
|
2015-11-28 19:02:01 +01:00
|
|
|
if(this.currentAnimalAmount < THRESHOLD){
|
|
|
|
if(this.currentTimer >= TIME){
|
2015-02-17 16:15:16 +01:00
|
|
|
this.currentTimer = 0;
|
2015-03-19 21:27:56 +01:00
|
|
|
if(this.slots[0] != null){
|
2015-11-22 18:58:23 +01:00
|
|
|
EntityAnimal randomAnimal = animals.get(Util.RANDOM.nextInt(this.currentAnimalAmount));
|
2015-03-19 21:27:56 +01:00
|
|
|
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
|
|
|
this.feedAnimal(randomAnimal);
|
2015-02-20 22:45:33 +01:00
|
|
|
|
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-02-17 16:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 16:48:01 +02:00
|
|
|
else{
|
|
|
|
this.currentTimer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.currentTimer = 0;
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 16:48:01 +02: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-07-02 04:21:21 +02:00
|
|
|
|
2015-12-01 17:28:50 +01:00
|
|
|
if((this.lastAnimalAmount != this.currentAnimalAmount || this.lastTimer != this.currentTimer) && this.sendUpdateWithInterval()){
|
2015-07-02 04:21:21 +02:00
|
|
|
this.lastAnimalAmount = this.currentAnimalAmount;
|
|
|
|
this.lastTimer = this.currentTimer;
|
|
|
|
}
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
public void feedAnimal(EntityAnimal animal){
|
2016-01-07 23:42:42 +01:00
|
|
|
animal.setInLove(null);
|
2015-02-20 22:45:33 +01:00
|
|
|
for(int i = 0; i < 7; i++){
|
2015-11-22 18:58:23 +01:00
|
|
|
double d = Util.RANDOM.nextGaussian()*0.02D;
|
|
|
|
double d1 = Util.RANDOM.nextGaussian()*0.02D;
|
|
|
|
double d2 = Util.RANDOM.nextGaussian()*0.02D;
|
2016-01-07 23:42:42 +01:00
|
|
|
worldObj.spawnParticle(EnumParticleTypes.HEART, (animal.posX+(double)(Util.RANDOM.nextFloat()*animal.width*2.0F))-animal.width, animal.posY+0.5D+(double)(Util.RANDOM.nextFloat()*animal.height), (animal.posZ+(double)(Util.RANDOM.nextFloat()*animal.width*2.0F))-animal.width, d, d1, d2);
|
2015-02-20 22:45:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
@Override
|
2016-01-07 23:42:42 +01:00
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
|
2015-12-19 10:30:39 +01:00
|
|
|
return this.isItemValidForSlot(slot, stack);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 23:42:42 +01:00
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
|
2015-03-07 02:23:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|