Sawmill changes & fixes.

This commit is contained in:
Dragon2488 2016-07-24 01:10:37 +07:00
parent 06fad2264f
commit 0011553cf7
16 changed files with 462 additions and 700 deletions

View file

@ -1,259 +0,0 @@
package techreborn.tiles;
import reborncore.common.IWrenchable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fluids.*;
import reborncore.api.IListInfoProvider;
import reborncore.api.power.EnumPowerTier;
import reborncore.api.recipe.IRecipeCrafterProvider;
import reborncore.api.tile.IInventoryProvider;
import reborncore.common.misc.Location;
import reborncore.common.recipes.RecipeCrafter;
import reborncore.common.tile.TilePowerAcceptor;
import reborncore.common.util.FluidUtils;
import reborncore.common.util.Inventory;
import reborncore.common.util.Tank;
import techreborn.api.Reference;
import techreborn.api.recipe.ITileRecipeHandler;
import techreborn.api.recipe.machines.IndustrialSawmillRecipe;
import techreborn.blocks.BlockMachineCasing;
import techreborn.init.ModBlocks;
import techreborn.init.ModFluids;
public class TileIndustrialSawmill extends TilePowerAcceptor implements IWrenchable, IFluidHandler,IInventoryProvider, ISidedInventory, IListInfoProvider, ITileRecipeHandler<IndustrialSawmillRecipe>, IRecipeCrafterProvider {
public static final int TANK_CAPACITY = 16000;
public int tickTime;
public Inventory inventory = new Inventory(5, "TileIndustrialSawmill", 64, this);
public Tank tank = new Tank("TileSawmill", TANK_CAPACITY, this);
public RecipeCrafter crafter;
public TileIndustrialSawmill() {
// TODO configs
// Input slots
int[] inputs = new int[2];
inputs[0] = 0;
inputs[1] = 1;
int[] outputs = new int[3];
outputs[0] = 2;
outputs[1] = 3;
outputs[2] = 4;
crafter = new RecipeCrafter(Reference.industrialSawmillRecipe, this, 2, 3, inventory, inputs, outputs);
}
@Override
public void update() {
super.update();
if (getMutliBlock()) {
crafter.updateEntity();
}
FluidUtils.drainContainers(this, inventory, 0, 4);
FluidUtils.drainContainers(this, inventory, 1, 4);
}
public boolean getMutliBlock() {
for (EnumFacing direction : EnumFacing.values()) {
TileEntity tileEntity = worldObj.getTileEntity(new BlockPos(getPos().getX() + direction.getFrontOffsetX(),
getPos().getY() + direction.getFrontOffsetY(), getPos().getZ() + direction.getFrontOffsetZ()));
if (tileEntity instanceof TileMachineCasing) {
if ((tileEntity.getBlockType() instanceof BlockMachineCasing)) {
int heat;
BlockMachineCasing blockMachineCasing = (BlockMachineCasing) tileEntity.getBlockType();
heat = blockMachineCasing
.getHeatFromState(tileEntity.getWorld().getBlockState(tileEntity.getPos()));
Location location = new Location(getPos().getX(), getPos().getY(), getPos().getZ(), direction);
location.modifyPositionFromSide(direction, 1);
if (worldObj.getBlockState(location.getBlockPos()).getBlock().getUnlocalizedName()
.equals("tile.lava")) {
heat += 500;
}
return true;
}
}
}
return false;
}
@Override
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, EnumFacing side) {
return false;
}
@Override
public EnumFacing getFacing() {
return getFacingEnum();
}
@Override
public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
return entityPlayer.isSneaking();
}
@Override
public float getWrenchDropRate() {
return 1.0F;
}
@Override
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
return new ItemStack(ModBlocks.industrialSawmill, 1);
}
public boolean isComplete() {
return false;
}
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);
tank.readFromNBT(tagCompound);
crafter.readFromNBT(tagCompound);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) {
super.writeToNBT(tagCompound);
tank.writeToNBT(tagCompound);
crafter.writeToNBT(tagCompound);
return tagCompound;
}
/* IFluidHandler */
@Override
public int fill(EnumFacing from, FluidStack resource, boolean doFill) {
if (resource.getFluid() == FluidRegistry.WATER || resource.getFluid() == ModFluids.fluidMercury
|| resource.getFluid() == ModFluids.fluidSodiumpersulfate) {
int filled = tank.fill(resource, doFill);
tank.compareAndUpdate();
return filled;
}
return 0;
}
@Override
public FluidStack drain(EnumFacing from, FluidStack resource, boolean doDrain) {
if (resource == null || !resource.isFluidEqual(tank.getFluid())) {
return null;
}
FluidStack fluidStack = tank.drain(resource.amount, doDrain);
tank.compareAndUpdate();
return fluidStack;
}
@Override
public FluidStack drain(EnumFacing from, int maxDrain, boolean doDrain) {
FluidStack drained = tank.drain(maxDrain, doDrain);
tank.compareAndUpdate();
return drained;
}
@Override
public boolean canFill(EnumFacing from, Fluid fluid) {
return fluid == FluidRegistry.WATER;
}
@Override
public boolean canDrain(EnumFacing from, Fluid fluid) {
return false;
}
@Override
public FluidTankInfo[] getTankInfo(EnumFacing from) {
return new FluidTankInfo[]{tank.getInfo()};
}
// ISidedInventory
@Override
public int[] getSlotsForFace(EnumFacing side) {
return side == EnumFacing.DOWN ? new int[]{0, 1, 2, 3, 4} : new int[]{0, 1, 2, 3, 4};
}
@Override
public boolean canInsertItem(int slotIndex, ItemStack itemStack, EnumFacing side) {
if (slotIndex >= 2)
return false;
return isItemValidForSlot(slotIndex, itemStack);
}
@Override
public boolean canExtractItem(int slotIndex, ItemStack itemStack, EnumFacing side) {
return slotIndex == 2 || slotIndex == 3 || slotIndex == 4;
}
public int getProgressScaled(int scale) {
if (crafter.currentTickTime != 0) {
return crafter.currentTickTime * scale / crafter.currentNeededTicks;
}
return 0;
}
@Override
public double getMaxPower() {
return 64000;
}
@Override
public EnumPowerTier getTier() {
return EnumPowerTier.MEDIUM;
}
@Override
public boolean canCraft(TileEntity tile, IndustrialSawmillRecipe recipe) {
if (recipe.fluidStack == null) {
return true;
}
if (tile instanceof TileIndustrialSawmill) {
TileIndustrialSawmill sawmill = (TileIndustrialSawmill) tile;
if (sawmill.tank.getFluid() == null) {
return false;
}
if (sawmill.tank.getFluid() == recipe.fluidStack) {
if (sawmill.tank.getFluidAmount() >= recipe.fluidStack.amount) {
return true;
}
}
}
return false;
}
@Override
public boolean onCraft(TileEntity tile, IndustrialSawmillRecipe recipe) {
if (recipe.fluidStack == null) {
return true;
}
if (tile instanceof TileIndustrialSawmill) {
TileIndustrialSawmill sawmill = (TileIndustrialSawmill) tile;
if (sawmill.tank.getFluid() == null) {
return false;
}
if (sawmill.tank.getFluid() == recipe.fluidStack) {
if (sawmill.tank.getFluidAmount() >= recipe.fluidStack.amount) {
if (sawmill.tank.getFluidAmount() > 0) {
sawmill.tank.setFluid(new FluidStack(recipe.fluidStack.getFluid(),
sawmill.tank.getFluidAmount() - recipe.fluidStack.amount));
} else {
sawmill.tank.setFluid(null);
}
return true;
}
}
}
return false;
}
@Override
public Inventory getInventory() {
return inventory;
}
@Override
public RecipeCrafter getRecipeCrafter() {
return crafter;
}
}

View file

@ -0,0 +1,85 @@
package techreborn.tiles.multiblock;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import techreborn.blocks.BlockMachineCasing;
import techreborn.init.ModBlocks;
public class MultiblockChecker {
public static final BlockPos ZERO_OFFSET = BlockPos.ORIGIN;
public static final int CASING_NORMAL = 0;
public static final int CASING_REINFORCED = 1;
public static final int CASING_ADVANCED = 2;
private final World world;
private final BlockPos downCenter;
public MultiblockChecker(World world, BlockPos downCenter) {
this.world = world;
this.downCenter = downCenter;
}
public boolean checkCasing(int offX, int offY, int offZ, int type) {
IBlockState block = getBlock(offX, offY, offZ);
if(block.getBlock() == ModBlocks.MachineCasing) {
if(block.getValue(BlockMachineCasing.METADATA) == type)
return true;
}
return false;
}
public boolean checkAir(int offX, int offY, int offZ) {
BlockPos pos = downCenter.add(offX, offY, offZ);
return world.isAirBlock(pos);
}
public IBlockState getBlock(int offX, int offY, int offZ) {
BlockPos pos = downCenter.add(offX, offY, offZ);
return world.getBlockState(pos);
}
public boolean checkRectY(int sizeX, int sizeZ, int casingType, BlockPos offset) {
for(int x = -sizeX; x <= sizeX; x++) {
for(int z = -sizeZ; z <= sizeZ; z++) {
if(!checkCasing(x + offset.getX(), offset.getY(), z + offset.getZ(), casingType))
return false;
}
}
return true;
}
public boolean checkRectZ(int sizeX, int sizeY, int casingType, BlockPos offset) {
for(int x = -sizeX; x <= sizeX; x++) {
for(int y = -sizeY; y <= sizeY; y++) {
if(!checkCasing(x + offset.getX(), y + offset.getY(), offset.getZ(), casingType))
return false;
}
}
return true;
}
public boolean checkRectX(int sizeZ, int sizeY, int casingType, BlockPos offset) {
for(int z = -sizeZ; z <= sizeZ; z++) {
for(int y = -sizeY; y <= sizeY; y++) {
if(!checkCasing(offset.getX(), y + offset.getY(), z + offset.getZ(), casingType))
return false;
}
}
return true;
}
public boolean checkRingY(int sizeX, int sizeZ, int casingType, BlockPos offset) {
for(int x = -sizeX; x <= sizeX; x++) {
for(int z = -sizeZ; z <= sizeZ; z++) {
if((x == sizeX || x == -sizeX) || (z == sizeZ || z == -sizeZ)) {
if (!checkCasing(x + offset.getX(), offset.getY(), z + offset.getZ(), casingType))
return false;
}
}
}
return true;
}
}

View file

@ -0,0 +1,229 @@
package techreborn.tiles.multiblock;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fluids.*;
import net.minecraftforge.oredict.OreDictionary;
import reborncore.api.IListInfoProvider;
import reborncore.api.power.EnumPowerTier;
import reborncore.api.tile.IInventoryProvider;
import reborncore.common.IWrenchable;
import reborncore.common.tile.TilePowerAcceptor;
import reborncore.common.util.FluidUtils;
import reborncore.common.util.Inventory;
import reborncore.common.util.Tank;
import techreborn.blocks.BlockMachineFrame;
import techreborn.init.ModBlocks;
import techreborn.init.ModFluids;
import techreborn.items.ItemDusts;
import java.util.List;
import java.util.Random;
import static techreborn.tiles.multiblock.MultiblockChecker.*;
public class TileIndustrialSawmill extends TilePowerAcceptor implements IWrenchable, IFluidHandler, IInventoryProvider, ISidedInventory, IListInfoProvider {
public Inventory inventory = new Inventory(5, "Sawmill", 64, this);
public Tank tank = new Tank("Sawmill", 16000, this);
public int tickTime;
public MultiblockChecker multiblockChecker;
@Override
public void update() {
super.update();
if (getMutliBlock()) {
ItemStack wood = inventory.getStackInSlot(0);
if(tickTime == 0) {
if (wood != null) {
for (int id : OreDictionary.getOreIDs(wood)) {
String name = OreDictionary.getOreName(id);
if(name.equals("logWood") &&
canAddOutput(2, 10) &&
canAddOutput(3, 5) &&
canAddOutput(4, 3) &&
canUseEnergy(128.0F) &&
!tank.isEmpty() &&
tank.getFluid().amount >= 1000) {
if(--wood.stackSize == 0)
setInventorySlotContents(0, null);
tank.drain(1000, true);
useEnergy(128.0F);
tickTime = 1;
}
}
}
} else if(++tickTime > 100) {
Random rnd = worldObj.rand;
addOutput(2, new ItemStack(Blocks.PLANKS, 6 + rnd.nextInt(4)));
if(rnd.nextInt(4) != 0) {
ItemStack pulp = ItemDusts.getDustByName("sawDust", 2 + rnd.nextInt(3));
addOutput(3, pulp);
}
if(rnd.nextInt(3) == 0) {
ItemStack paper = new ItemStack(Items.PAPER, 1 + rnd.nextInt(2));
addOutput(4, paper);
}
tickTime = 0;
}
}
FluidUtils.drainContainers(this, inventory, 1, 4);
}
public void addOutput(int slot, ItemStack stack) {
if(getStackInSlot(slot) == null)
setInventorySlotContents(slot, stack);
getStackInSlot(slot).stackSize += stack.stackSize;
}
public boolean canAddOutput(int slot, int amount) {
ItemStack stack = getStackInSlot(slot);
return stack == null || getInventoryStackLimit() - stack.stackSize >= amount;
}
@Override
public void validate() {
super.validate();
multiblockChecker = new MultiblockChecker(worldObj, getPos().down(3));
}
public boolean getMutliBlock() {
boolean down = multiblockChecker.checkRectY(1, 1, CASING_NORMAL, ZERO_OFFSET);
boolean up = multiblockChecker.checkRectY(1, 1, CASING_NORMAL, new BlockPos(0, 2, 0));
boolean blade = multiblockChecker.checkRingY(1, 1, CASING_REINFORCED, new BlockPos(0, 1, 0));
IBlockState centerBlock = multiblockChecker.getBlock(0, 1, 0);
boolean center = centerBlock.getBlock() == Blocks.WATER;
return down && center && blade && up;
}
@Override
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, EnumFacing side) {
return false;
}
@Override
public EnumFacing getFacing() {
return getFacingEnum();
}
@Override
public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
return entityPlayer.isSneaking();
}
@Override
public float getWrenchDropRate() {
return 1.0F;
}
@Override
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
return new ItemStack(ModBlocks.industrialSawmill, 1);
}
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);
tank.readFromNBT(tagCompound);
tickTime = tagCompound.getInteger("tickTime");
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) {
super.writeToNBT(tagCompound);
tank.writeToNBT(tagCompound);
tagCompound.setInteger("tickTime", tickTime);
return tagCompound;
}
/* IFluidHandler */
@Override
public int fill(EnumFacing from, FluidStack resource, boolean doFill) {
if (resource.getFluid() == FluidRegistry.WATER || resource.getFluid() == ModFluids.fluidMercury
|| resource.getFluid() == ModFluids.fluidSodiumpersulfate) {
int filled = tank.fill(resource, doFill);
tank.compareAndUpdate();
return filled;
}
return 0;
}
@Override
public FluidStack drain(EnumFacing from, FluidStack resource, boolean doDrain) {
if (resource == null || !resource.isFluidEqual(tank.getFluid())) {
return null;
}
FluidStack fluidStack = tank.drain(resource.amount, doDrain);
tank.compareAndUpdate();
return fluidStack;
}
@Override
public FluidStack drain(EnumFacing from, int maxDrain, boolean doDrain) {
FluidStack drained = tank.drain(maxDrain, doDrain);
tank.compareAndUpdate();
return drained;
}
@Override
public boolean canFill(EnumFacing from, Fluid fluid) {
return fluid == FluidRegistry.WATER;
}
@Override
public boolean canDrain(EnumFacing from, Fluid fluid) {
return false;
}
@Override
public FluidTankInfo[] getTankInfo(EnumFacing from) {
return new FluidTankInfo[]{tank.getInfo()};
}
// ISidedInventory
@Override
public int[] getSlotsForFace(EnumFacing side) {
return side == EnumFacing.DOWN ? new int[]{0, 1, 2, 3, 4} : new int[]{0, 1, 2, 3, 4};
}
@Override
public boolean canInsertItem(int slotIndex, ItemStack itemStack, EnumFacing side) {
return slotIndex < 2 && isItemValidForSlot(slotIndex, itemStack);
}
@Override
public boolean canExtractItem(int slotIndex, ItemStack itemStack, EnumFacing side) {
return slotIndex == 2 || slotIndex == 3 || slotIndex == 4;
}
public int getProgressScaled(int scale) {
if (tickTime != 0) {
return tickTime * scale / 100;
}
return 0;
}
@Override
public double getMaxPower() {
return 64000;
}
@Override
public EnumPowerTier getTier() {
return EnumPowerTier.MEDIUM;
}
@Override
public Inventory getInventory() {
return inventory;
}
}