Some more work on the farm

This commit is contained in:
modmuss50 2015-07-13 16:24:50 +01:00
parent c160abf176
commit ef5d81d8e2
2 changed files with 154 additions and 6 deletions

View file

@ -1,14 +1,97 @@
package techreborn.farm;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import techreborn.api.farm.IFarmLogicDevice;
import techreborn.lib.Location;
import techreborn.tiles.TileFarm;
import techreborn.util.ItemUtils;
import java.util.ArrayList;
public class FarmTree implements IFarmLogicDevice {
@Override
public void tick(TileFarm tileFarm) {
System.out.println("tick");
if(tileFarm.getWorldObj().isRemote){
return;
}
if(tileFarm.getWorldObj().getTotalWorldTime() %100 == 0 || tileFarm.inventory.hasChanged){
calculateFarmLand(tileFarm);
}
if(tileFarm.getWorldObj().getTotalWorldTime() %5 == 0){
farmLandTick(tileFarm);
saplinTick(tileFarm);
}
}
ArrayList<Location> farmlandToPlace = new ArrayList<Location>();
Block farmlandType = Blocks.dirt;
boolean needsToPlaceWater = false;
public void calculateFarmLand(TileFarm tileFarm){
if(farmlandToPlace.isEmpty()){
for (int x = -tileFarm.size + 1; x < tileFarm.size; x++) {
for (int z = -tileFarm.size + 1; z < tileFarm.size; z++) {
int xpos = x + tileFarm.xCoord;
int ypos = tileFarm.yCoord + 1;
int zpos = z + tileFarm.zCoord;
if(xpos == tileFarm.xCoord && zpos == tileFarm.zCoord){
if(tileFarm.getWorldObj().getBlock(xpos,ypos, zpos) != Blocks.water){
needsToPlaceWater = true;
}
} else if(tileFarm.getWorldObj().getBlock(xpos,ypos, zpos) != farmlandType) {
farmlandToPlace.add(new Location(xpos, ypos, zpos));
}
}
}
}
}
public void farmLandTick(TileFarm tileFarm){
if(!farmlandToPlace.isEmpty()){
Location location = farmlandToPlace.get(0);
if(tileFarm.getWorldObj().getBlock(location.getX(),location.getY(), location.getZ()) != farmlandType){
if(removeInputStack(new ItemStack(Blocks.dirt), tileFarm)){
tileFarm.getWorldObj().setBlock(location.getX(),location.getY(), location.getZ(), farmlandType);
}
}
farmlandToPlace.remove(location);
} else {
if(needsToPlaceWater){
//TODO use water out of tank
tileFarm.getWorldObj().setBlock(tileFarm.xCoord, tileFarm.yCoord + 1, tileFarm.zCoord, Blocks.water);
needsToPlaceWater = false;
}
}
}
public void saplinTick(TileFarm tileFarm){
for (int x = -tileFarm.size + 1; x < tileFarm.size; x++) {
for (int z = -tileFarm.size + 1; z < tileFarm.size; z++) {
int xpos = x + tileFarm.xCoord;
int ypos = tileFarm.yCoord + 2;
int zpos = z + tileFarm.zCoord;
//if(tileFarm.getWorldObj().getBlock(xpos,ypos -1, zpos) == farmlandType) {
if(tileFarm.getWorldObj().getBlock(xpos,ypos , zpos) == Blocks.air){
tileFarm.getWorldObj().setBlock(xpos,ypos , zpos, Blocks.sapling);
}
// }
}
}
}
public boolean removeInputStack(ItemStack stack, TileFarm tileFarm){
for (int i = 1; i < 3; i++) {
if(ItemUtils.isItemEqual(stack, tileFarm.getStackInSlot(i), true, true)){
tileFarm.decrStackSize(i, 1);
return true;
}
}
return false;
}
}

View file

@ -1,17 +1,21 @@
package techreborn.tiles;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import techreborn.api.farm.IFarmLogicContainer;
import techreborn.api.farm.IFarmLogicDevice;
import techreborn.farm.FarmTree;
import techreborn.util.Inventory;
public class TileFarm extends TileMachineBase {
public class TileFarm extends TileMachineBase implements IInventory {
public Inventory inventory= new Inventory(14, "TileFarm", 64);
public Inventory inventory = new Inventory(14, "TileFarm", 64);
IFarmLogicDevice logicDevice;
public int size = 4;
public TileFarm() {
}
@ -41,5 +45,66 @@ public class TileFarm extends TileMachineBase {
logicDevice.tick(this);
}
super.updateEntity();
inventory.hasChanged = false;
}
@Override
public int getSizeInventory() {
return inventory.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slot) {
return inventory.getStackInSlot(slot);
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
return inventory.decrStackSize(slot, amount);
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
return inventory.getStackInSlotOnClosing(slot);
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
inventory.setInventorySlotContents(slot, stack);
}
@Override
public String getInventoryName() {
return inventory.getInventoryName();
}
@Override
public boolean hasCustomInventoryName() {
return inventory.hasCustomInventoryName();
}
@Override
public int getInventoryStackLimit() {
return inventory.getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return inventory.isUseableByPlayer(player);
}
@Override
public void openInventory() {
inventory.openInventory();
}
@Override
public void closeInventory() {
inventory.closeInventory();
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return inventory.isItemValidForSlot(slot, stack);
}
}