Updated reactor GUI. Closes #1422

This commit is contained in:
drcrazy 2018-01-29 18:17:32 +03:00
parent 2e115242a2
commit a6df9441ec
5 changed files with 325 additions and 300 deletions

View file

@ -29,7 +29,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import reborncore.api.IToolDrop;
import reborncore.api.tile.IInventoryProvider;
import reborncore.common.powerSystem.TilePowerAcceptor;
@ -46,7 +45,8 @@ import techreborn.init.ModBlocks;
import techreborn.lib.ModInfo;
@RebornRegistry(modID = ModInfo.MOD_ID)
public class TileFusionControlComputer extends TilePowerAcceptor implements IToolDrop, IInventoryProvider, IContainerProvider {
public class TileFusionControlComputer extends TilePowerAcceptor
implements IToolDrop, IInventoryProvider, IContainerProvider {
@ConfigRegistry(config = "machines", category = "fusion_reactor", key = "FusionReactorMaxInput", comment = "Fusion Reactor Max Input (Value in EU)")
public static int maxInput = 8192;
@ -54,10 +54,8 @@ public class TileFusionControlComputer extends TilePowerAcceptor implements IToo
public static int maxOutput = 1000000;
@ConfigRegistry(config = "machines", category = "fusion_reactor", key = "FusionReactorMaxEnergy", comment = "Fusion Reactor Max Energy (Value in EU)")
public static int maxEnergy = 100000000;
// @ConfigRegistry(config = "machines", category = "fusion_reactor", key = "FusionReactorWrenchDropRate", comment = "Fusion Reactor Wrench Drop Rate")
public static float wrenchDropRate = 1.0F;
public Inventory inventory = new Inventory(3, "TileFusionControlComputer", 64, this);
public Inventory inventory;
// 0= no coils, 1 = coils
public int coilStatus = 0;
@ -72,6 +70,206 @@ public class TileFusionControlComputer extends TilePowerAcceptor implements IToo
public TileFusionControlComputer() {
super();
this.inventory = new Inventory(3, "TileFusionControlComputer", 64, this);
}
/**
* Check that reactor has all necessary coils in place
*
* @return boolean Return true if coils are present
*/
public boolean checkCoils() {
int posX = this.getPos().getX();
int posY = this.getPos().getY();
int posZ = this.getPos().getZ();
if (isCoil(posX + 3, posY, posZ + 1) && isCoil(posX + 3, posY, posZ) && isCoil(posX + 3, posY, posZ - 1)
&& isCoil(posX - 3, posY, posZ + 1) && isCoil(posX - 3, posY, posZ) && isCoil(posX - 3, posY, posZ - 1)
&& isCoil(posX + 2, posY, posZ + 2) && isCoil(posX + 2, posY, posZ + 1)
&& isCoil(posX + 2, posY, posZ - 1) && isCoil(posX + 2, posY, posZ - 2)
&& isCoil(posX - 2, posY, posZ + 2) && isCoil(posX - 2, posY, posZ + 1)
&& isCoil(posX - 2, posY, posZ - 1) && isCoil(posX - 2, posY, posZ - 2)
&& isCoil(posX + 1, posY, posZ + 3) && isCoil(posX + 1, posY, posZ + 2)
&& isCoil(posX + 1, posY, posZ - 2) && isCoil(posX + 1, posY, posZ - 3)
&& isCoil(posX - 1, posY, posZ + 3) && isCoil(posX - 1, posY, posZ + 2)
&& isCoil(posX - 1, posY, posZ - 2) && isCoil(posX - 1, posY, posZ - 3) && isCoil(posX, posY, posZ + 3)
&& isCoil(posX, posY, posZ - 3)) {
coilStatus = 1;
return true;
}
coilStatus = 0;
return false;
}
/**
* Checks if block is fusion coil
*
* @param x int X coordinate for block
* @param y int Y coordinate for block
* @param z int Z coordinate for block
* @return boolean Returns true if block is fusion coil
*/
private boolean isCoil(final int x, final int y, final int z) {
return this.world.getBlockState(new BlockPos(x, y, z)).getBlock() == ModBlocks.FUSION_COIL;
}
/**
* Resets crafter progress and recipe
*/
private void resetCrafter() {
this.currentRecipe = null;
this.crafingTickTime = 0;
this.finalTickTime = 0;
this.neededPower = 0;
this.hasStartedCrafting = false;
}
/**
* Checks that ItemStack could be inserted into slot provided, including check
* for existing item in slot and maximum stack size
*
* @param stack ItemStack ItemStack to insert
* @param slot int Slot ID to check
* @param oreDic boolean Should we use ore dictionary
* @return boolean Returns true if ItemStack will fit into slot
*/
public boolean canFitStack(final ItemStack stack, final int slot, final boolean oreDic) {// Checks to see if it can
// fit the stack
if (stack.isEmpty()) {
return true;
}
if (this.inventory.getStackInSlot(slot).isEmpty()) {
return true;
}
if (ItemUtils.isItemEqual(this.inventory.getStackInSlot(slot), stack, true, true, oreDic)) {
if (stack.getCount() + this.inventory.getStackInSlot(slot).getCount() <= stack.getMaxStackSize()) {
return true;
}
}
return false;
}
/**
* Returns progress scaled to input value
*
* @param scale int Maximum value for progress
* @return int Scale of progress
*/
public int getProgressScaled(int scale) {
if (this.crafingTickTime != 0) {
return this.crafingTickTime * scale / this.finalTickTime;
}
return 0;
}
/**
* Tries to set current recipe based in inputs in reactor
*/
private void updateCurrentRecipe() {
for (final FusionReactorRecipe reactorRecipe : FusionReactorRecipeHelper.reactorRecipes) {
if (validateReactorRecipe(reactorRecipe)) {
this.currentRecipe = reactorRecipe;
this.crafingTickTime = 0;
this.finalTickTime = this.currentRecipe.getTickTime();
this.neededPower = (int) this.currentRecipe.getStartEU();
this.hasStartedCrafting = false;
break;
}
}
}
/**
* Validates that reactor can execute recipe provided, e.g. has all inputs and can fit output
*
* @param recipe FusionReactorRecipe Recipe to validate
* @return boolean True if reactor can execute recipe provided
*/
private boolean validateReactorRecipe(FusionReactorRecipe recipe) {
if (ItemUtils.isItemEqual(this.getStackInSlot(topStackSlot), recipe.getTopInput(), true, true, true)) {
if (recipe.getBottomInput() != null) {
if (!ItemUtils.isItemEqual(this.getStackInSlot(bottomStackSlot), recipe.getBottomInput(), true, true, true)) {
return false;
}
}
if (this.canFitStack(recipe.getOutput(), outputStackSlot, true)) {
return true;
}
}
return false;
}
// TilePowerAcceptor
@Override
public void update() {
super.update();
if (this.world.isRemote) {
return;
}
// Force check every second
if (this.world.getTotalWorldTime() % 20 == 0) {
this.checkCoils();
this.inventory.hasChanged = true;
}
if (this.coilStatus == 0) {
this.resetCrafter();
return;
}
if (this.currentRecipe == null && this.inventory.hasChanged == true) {
updateCurrentRecipe();
}
if (this.currentRecipe != null) {
if (this.inventory.hasChanged && !validateReactorRecipe(this.currentRecipe)) {
resetCrafter();
return;
}
if (!this.hasStartedCrafting) {
// Ignition!
if (this.canUseEnergy(this.currentRecipe.getStartEU())) {
this.useEnergy(this.currentRecipe.getStartEU());
this.hasStartedCrafting = true;
}
}
if (hasStartedCrafting && this.crafingTickTime < this.finalTickTime) {
this.crafingTickTime++;
// Power gen
if (this.currentRecipe.getEuTick() > 0) {
// Waste power if it has no where to go
this.addEnergy(this.currentRecipe.getEuTick());
} else { // Power user
if (this.canUseEnergy(this.currentRecipe.getEuTick() * -1)) {
this.setEnergy(this.getEnergy() - this.currentRecipe.getEuTick() * -1);
}
}
} else if (this.crafingTickTime >= this.finalTickTime) {
if (this.canFitStack(this.currentRecipe.getOutput(), this.outputStackSlot, true)) {
if (this.getStackInSlot(this.outputStackSlot).isEmpty()) {
this.setInventorySlotContents(this.outputStackSlot, this.currentRecipe.getOutput().copy());
} else {
this.decrStackSize(this.outputStackSlot, -this.currentRecipe.getOutput().getCount());
}
this.decrStackSize(this.topStackSlot, this.currentRecipe.getTopInput().getCount());
if (!this.currentRecipe.getBottomInput().isEmpty()) {
this.decrStackSize(this.bottomStackSlot, this.currentRecipe.getBottomInput().getCount());
}
if (this.validateReactorRecipe(this.currentRecipe)) {
this.crafingTickTime = 0;
} else {
this.resetCrafter();
}
}
}
this.markDirty();
}
if (this.inventory.hasChanged) {
this.inventory.hasChanged = false;
}
}
@Override
@ -117,16 +315,6 @@ public class TileFusionControlComputer extends TilePowerAcceptor implements IToo
@Override
public NBTTagCompound writeToNBT(final NBTTagCompound tagCompound) {
super.writeToNBT(tagCompound);
if (this.crafingTickTime == -1) {
this.crafingTickTime = 0;
}
if (this.finalTickTime == -1) {
this.finalTickTime = 0;
}
if (this.neededPower == -1) {
this.neededPower = 0;
}
tagCompound.setInteger("crafingTickTime", this.crafingTickTime);
tagCompound.setInteger("finalTickTime", this.finalTickTime);
tagCompound.setInteger("neededPower", this.neededPower);
@ -134,190 +322,56 @@ public class TileFusionControlComputer extends TilePowerAcceptor implements IToo
return tagCompound;
}
public boolean checkCoils() {
if (this.isCoil(this.getPos().getX() + 3, this.getPos().getY(), this.getPos().getZ() + 1)
&& this.isCoil(this.getPos().getX() + 3, this.getPos().getY(), this.getPos().getZ())
&& this.isCoil(this.getPos().getX() + 3, this.getPos().getY(), this.getPos().getZ() - 1)
&& this.isCoil(this.getPos().getX() - 3, this.getPos().getY(), this.getPos().getZ() + 1)
&& this.isCoil(this.getPos().getX() - 3, this.getPos().getY(), this.getPos().getZ())
&& this.isCoil(this.getPos().getX() - 3, this.getPos().getY(), this.getPos().getZ() - 1)
&& this.isCoil(this.getPos().getX() + 2, this.getPos().getY(), this.getPos().getZ() + 2)
&& this.isCoil(this.getPos().getX() + 2, this.getPos().getY(), this.getPos().getZ() + 1)
&& this.isCoil(this.getPos().getX() + 2, this.getPos().getY(), this.getPos().getZ() - 1)
&& this.isCoil(this.getPos().getX() + 2, this.getPos().getY(), this.getPos().getZ() - 2)
&& this.isCoil(this.getPos().getX() - 2, this.getPos().getY(), this.getPos().getZ() + 2)
&& this.isCoil(this.getPos().getX() - 2, this.getPos().getY(), this.getPos().getZ() + 1)
&& this.isCoil(this.getPos().getX() - 2, this.getPos().getY(), this.getPos().getZ() - 1)
&& this.isCoil(this.getPos().getX() - 2, this.getPos().getY(), this.getPos().getZ() - 2)
&& this.isCoil(this.getPos().getX() + 1, this.getPos().getY(), this.getPos().getZ() + 3)
&& this.isCoil(this.getPos().getX() + 1, this.getPos().getY(), this.getPos().getZ() + 2)
&& this.isCoil(this.getPos().getX() + 1, this.getPos().getY(), this.getPos().getZ() - 2)
&& this.isCoil(this.getPos().getX() + 1, this.getPos().getY(), this.getPos().getZ() - 3)
&& this.isCoil(this.getPos().getX() - 1, this.getPos().getY(), this.getPos().getZ() + 3)
&& this.isCoil(this.getPos().getX() - 1, this.getPos().getY(), this.getPos().getZ() + 2)
&& this.isCoil(this.getPos().getX() - 1, this.getPos().getY(), this.getPos().getZ() - 2)
&& this.isCoil(this.getPos().getX() - 1, this.getPos().getY(), this.getPos().getZ() - 3)
&& this.isCoil(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ() + 3)
&& this.isCoil(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ() - 3)) {
this.coilStatus = 1;
return true;
}
this.coilStatus = 0;
return false;
}
private boolean isCoil(final int x, final int y, final int z) {
return this.world.getBlockState(new BlockPos(x, y, z)).getBlock() == ModBlocks.FUSION_COIL;
}
// TileLegacyMachineBase
@Override
public void onLoad() {
super.onLoad();
this.checkCoils();
}
@Override
public void update() {
super.update();
// TODO improve this code a lot
if (this.world.isRemote) {
return;
}
if (this.world.getTotalWorldTime() % 20 == 0) {
this.checkCoils();
}
if (this.coilStatus == 0) {
this.resetCrafter();
return;
}
if (this.currentRecipe == null && this.inventory.hasChanged){
this.crafingTickTime = 0;
for (final FusionReactorRecipe reactorRecipe : FusionReactorRecipeHelper.reactorRecipes) {
if (this.validateRecipe(reactorRecipe)){
this.currentRecipe = reactorRecipe;
this.finalTickTime = this.currentRecipe.getTickTime();
this.neededPower = (int) this.currentRecipe.getStartEU();
break;
}
}
}
else {
if (this.inventory.hasChanged) {
if (!this.validateRecipe(this.currentRecipe)) {
this.resetCrafter();
return;
}
}
}
if (this.currentRecipe != null) {
if (!this.hasStartedCrafting) {
// Ignition!
if (this.canUseEnergy(this.currentRecipe.getStartEU())) {
this.useEnergy(this.currentRecipe.getStartEU());
this.hasStartedCrafting = true;
}
}
if (hasStartedCrafting && this.crafingTickTime < this.finalTickTime) {
this.crafingTickTime++;
// Power gen
if (this.currentRecipe.getEuTick() > 0) {
// Waste power if it has no where to go
this.addEnergy(this.currentRecipe.getEuTick());
} else { // Power user
if (this.canUseEnergy(this.currentRecipe.getEuTick() * -1)) {
this.setEnergy(this.getEnergy() - this.currentRecipe.getEuTick() * -1);
}
}
} else if (this.crafingTickTime >= this.finalTickTime) {
if (this.canFitStack(this.currentRecipe.getOutput(), this.outputStackSlot, true)) {
if (this.getStackInSlot(this.outputStackSlot).isEmpty()) {
this.setInventorySlotContents(this.outputStackSlot, this.currentRecipe.getOutput().copy());
} else {
this.decrStackSize(this.outputStackSlot, -this.currentRecipe.getOutput().getCount());
}
this.decrStackSize(this.topStackSlot, this.currentRecipe.getTopInput().getCount());
if (!this.currentRecipe.getBottomInput().isEmpty()) {
this.decrStackSize(this.bottomStackSlot, this.currentRecipe.getBottomInput().getCount());
}
if (this.validateRecipe(this.currentRecipe)){
this.crafingTickTime = 0;
}
else {
this.resetCrafter();
}
}
}
this.markDirty();
}
if (this.inventory.hasChanged) {
this.inventory.hasChanged = false;
}
}
private boolean validateRecipe(FusionReactorRecipe recipe) {
if (ItemUtils.isItemEqual(this.getStackInSlot(this.topStackSlot), recipe.getTopInput(), true, true, true)) {
if (recipe.getBottomInput() != null) {
if (!ItemUtils.isItemEqual(this.getStackInSlot(this.bottomStackSlot), recipe.getBottomInput(), true, true,
true)) {
return false;
}
}
if (this.canFitStack(recipe.getOutput(), this.outputStackSlot, true)) {
return true;
}
}
@Override
public boolean canBeUpgraded() {
return false;
}
private void resetCrafter() {
this.currentRecipe = null;
this.crafingTickTime = -1;
this.finalTickTime = -1;
this.neededPower = -1;
this.hasStartedCrafting = false;
}
public boolean canFitStack(final ItemStack stack, final int slot, final boolean oreDic) {// Checks to see if it can fit the stack
if (stack.isEmpty()) {
return true;
}
if (this.inventory.getStackInSlot(slot).isEmpty()) {
return true;
}
if (ItemUtils.isItemEqual(this.inventory.getStackInSlot(slot), stack, true, true, oreDic)) {
if (stack.getCount() + this.inventory.getStackInSlot(slot).getCount() <= stack.getMaxStackSize()) {
return true;
}
}
return false;
}
// @Override
// public String getName() {
// return null;
// }
//
// @Override
// public boolean hasCustomName() {
// return false;
// }
//
// @Override
// public ITextComponent getDisplayName() {
// return null;
// }
// IToolDrop
@Override
public String getName() {
return null;
}
@Override
public boolean hasCustomName() {
return false;
}
@Override
public ITextComponent getDisplayName() {
return null;
public ItemStack getToolDrop(EntityPlayer p0) {
return new ItemStack(ModBlocks.FUSION_CONTROL_COMPUTER, 1);
}
// IInventoryProvider
@Override
public Inventory getInventory() {
return this.inventory;
}
// IContainerProvider
@Override
public BuiltContainer createContainer(final EntityPlayer player) {
return new ContainerBuilder("fusionreactor").player(player.inventory).inventory().hotbar()
.addInventory().tile(this).slot(0, 34, 47).slot(1, 126, 47).outputSlot(2, 80, 47).syncEnergyValue()
.syncIntegerValue(this::getCoilStatus, this::setCoilStatus)
.syncIntegerValue(this::getCrafingTickTime, this::setCrafingTickTime)
.syncIntegerValue(this::getFinalTickTime, this::setFinalTickTime)
.syncIntegerValue(this::getNeededPower, this::setNeededPower).addInventory().create(this);
}
public int getCoilStatus() {
return this.coilStatus;
}
@ -349,29 +403,4 @@ public class TileFusionControlComputer extends TilePowerAcceptor implements IToo
public void setNeededPower(final int neededPower) {
this.neededPower = neededPower;
}
public int getProgressScaled() {
return Math.max(0, Math.min(24, (this.getCrafingTickTime() > 0 ? 1 : 0)
+ this.getCrafingTickTime() * 24 / (this.finalTickTime < 1 ? 1 : this.finalTickTime)));
}
@Override
public BuiltContainer createContainer(final EntityPlayer player) {
return new ContainerBuilder("fusionreactor").player(player.inventory).inventory(8, 84).hotbar(8, 142)
.addInventory().tile(this).slot(0, 88, 17).slot(1, 88, 53).outputSlot(2, 148, 35).syncEnergyValue()
.syncIntegerValue(this::getCoilStatus, this::setCoilStatus)
.syncIntegerValue(this::getCrafingTickTime, this::setCrafingTickTime)
.syncIntegerValue(this::getFinalTickTime, this::setFinalTickTime)
.syncIntegerValue(this::getNeededPower, this::setNeededPower).addInventory().create(this);
}
@Override
public ItemStack getToolDrop(EntityPlayer p0) {
return new ItemStack(ModBlocks.FUSION_CONTROL_COMPUTER, 1);
}
@Override
public boolean canBeUpgraded() {
return false;
}
}