More fluid work
This commit is contained in:
parent
739f4c21e8
commit
70279c1800
5 changed files with 89 additions and 52 deletions
|
@ -25,6 +25,7 @@
|
|||
package techreborn.api.fluidreplicator;
|
||||
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.fluid.Fluids;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import reborncore.common.recipes.RecipeCrafter;
|
||||
import reborncore.common.util.RebornInventory;
|
||||
|
@ -74,12 +75,7 @@ public class FluidReplicatorRecipeCrafter extends RecipeCrafter {
|
|||
return true;
|
||||
}
|
||||
|
||||
private boolean canFit(Fluid fluid, Tank tank) {
|
||||
if (tank.fill(new FluidInstance(fluid, 1000), false) != 1000) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public void setCurrentRecipe(FluidReplicatorRecipe recipe) {
|
||||
try {
|
||||
|
@ -117,8 +113,9 @@ public class FluidReplicatorRecipeCrafter extends RecipeCrafter {
|
|||
FluidReplicatorBlockEntity blockEntityFluidReplicator = (FluidReplicatorBlockEntity) blockEntity;
|
||||
// Checks to see if it can fit the output
|
||||
// And fill tank with replicated fluid
|
||||
if (canFit(currentRecipe.getFluid(), blockEntityFluidReplicator.tank) && currentRecipe.onCraft(blockEntityFluidReplicator)) {
|
||||
blockEntityFluidReplicator.tank.fill(new FluidInstance(currentRecipe.getFluid(), 1000), true);
|
||||
if (blockEntityFluidReplicator.tank.canFit(currentRecipe.getFluid(), 1000) && currentRecipe.onCraft(blockEntityFluidReplicator)) {
|
||||
blockEntityFluidReplicator.tank.getFluidInstance().setFluid(currentRecipe.getFluid());
|
||||
blockEntityFluidReplicator.tank.getFluidInstance().addAmount(1000);
|
||||
// This uses all the inputs
|
||||
useAllInputs();
|
||||
// Reset
|
||||
|
@ -150,7 +147,7 @@ public class FluidReplicatorRecipeCrafter extends RecipeCrafter {
|
|||
FluidReplicatorBlockEntity blockEntityFluidReplicator = (FluidReplicatorBlockEntity) blockEntity;
|
||||
for (FluidReplicatorRecipe recipe : FluidReplicatorRecipeList.recipes) {
|
||||
if (recipe.canCraft(blockEntityFluidReplicator) && hasAllInputs(recipe)) {
|
||||
if (!canFit(recipe.getFluid(), blockEntityFluidReplicator.tank)) {
|
||||
if (!blockEntityFluidReplicator.tank.canFit(recipe.getFluid(), 1000)) {
|
||||
this.currentRecipe = null;
|
||||
currentTickTime = 0;
|
||||
setIsActive();
|
||||
|
@ -190,7 +187,7 @@ public class FluidReplicatorRecipeCrafter extends RecipeCrafter {
|
|||
FluidReplicatorBlockEntity blockEntityFluidReplicator = (FluidReplicatorBlockEntity) blockEntity;
|
||||
for (FluidReplicatorRecipe recipe : FluidReplicatorRecipeList.recipes) {
|
||||
if (recipe.canCraft(blockEntityFluidReplicator) && hasAllInputs(recipe)) {
|
||||
if (!canFit(recipe.getFluid(), blockEntityFluidReplicator.tank)) {
|
||||
if (!blockEntityFluidReplicator.tank.canFit(recipe.getFluid(), 1000)) {
|
||||
return false;
|
||||
}
|
||||
if (energy.getEnergy() < recipe.getEuTick()) {
|
||||
|
|
|
@ -24,34 +24,43 @@
|
|||
|
||||
package techreborn.api.recipe.recipes;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.github.prospector.silk.fluid.FluidInstance;
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.fluid.Fluids;
|
||||
import net.minecraft.util.Identifier;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import reborncore.common.crafting.RebornRecipe;
|
||||
import reborncore.common.crafting.RebornRecipeType;
|
||||
import techreborn.blockentity.machine.multiblock.IndustrialSawmillBlockEntity;
|
||||
|
||||
public class IndustrialSawmillRecipe extends RebornRecipe {
|
||||
|
||||
//TODO 1.14 fluids
|
||||
FluidInstance fluidStack = null;
|
||||
@NonNull
|
||||
private FluidInstance fluidInstance = FluidInstance.EMPTY;
|
||||
|
||||
public IndustrialSawmillRecipe(RebornRecipeType<?> type, Identifier name) {
|
||||
super(type, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserialize(JsonObject jsonObject) {
|
||||
super.deserialize(jsonObject);
|
||||
fluidInstance = new FluidInstance(Fluids.WATER, 1000);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCraft(BlockEntity be) {
|
||||
IndustrialSawmillBlockEntity blockEntity = (IndustrialSawmillBlockEntity) be;
|
||||
if (!blockEntity.getMutliBlock()) {
|
||||
return false;
|
||||
}
|
||||
final FluidInstance recipeFluid = fluidStack;
|
||||
final FluidInstance recipeFluid = fluidInstance;
|
||||
final FluidInstance tankFluid = blockEntity.tank.getFluidInstance();
|
||||
if (fluidStack == null) {
|
||||
if (fluidInstance.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
if (tankFluid == null) {
|
||||
if (tankFluid.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (tankFluid.getFluid().equals(recipeFluid.getFluid())) {
|
||||
|
@ -65,22 +74,17 @@ public class IndustrialSawmillRecipe extends RebornRecipe {
|
|||
@Override
|
||||
public boolean onCraft(BlockEntity be) {
|
||||
IndustrialSawmillBlockEntity blockEntity = (IndustrialSawmillBlockEntity) be;
|
||||
final FluidInstance recipeFluid = fluidStack;
|
||||
final FluidInstance recipeFluid = fluidInstance;
|
||||
final FluidInstance tankFluid = blockEntity.tank.getFluidInstance();
|
||||
if (fluidStack == null) {
|
||||
if (fluidInstance.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
if (tankFluid == null) {
|
||||
if (tankFluid.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (tankFluid.getFluid().equals(recipeFluid.getFluid())) {
|
||||
if (tankFluid.getAmount() >= recipeFluid.getAmount()) {
|
||||
if (tankFluid.getAmount() == recipeFluid.getAmount()) {
|
||||
blockEntity.tank.setFluid(null);
|
||||
} else {
|
||||
tankFluid.subtractAmount(recipeFluid.getAmount());
|
||||
}
|
||||
blockEntity.syncWithAll();
|
||||
tankFluid.subtractAmount(recipeFluid.getAmount());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -100,7 +100,7 @@ public abstract class BaseFluidGeneratorBlockEntity extends PowerAcceptorBlockEn
|
|||
pendingWithdraw += millibucketsPerTick;
|
||||
final int currentWithdraw = (int) pendingWithdraw;
|
||||
pendingWithdraw -= currentWithdraw;
|
||||
tank.drain(currentWithdraw, true);
|
||||
tank.getFluidInstance().subtractAmount(currentWithdraw);
|
||||
lastOutput = world.getTime();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ public class IndustrialSawmillBlockEntity extends GenericMachineBlockEntity impl
|
|||
super(TRBlockEntities.INDUSTRIAL_SAWMILL, "IndustrialSawmill", maxInput, maxEnergy, TRContent.Machine.INDUSTRIAL_SAWMILL.block, 6);
|
||||
final int[] inputs = new int[] { 0, 1 };
|
||||
final int[] outputs = new int[] { 2, 3, 4 };
|
||||
this.inventory = new RebornInventory<>(7, "SawmillBlockEntity", 64, this, getInventoryAccess());
|
||||
this.inventory = new RebornInventory<>(7, "SawmillBlockEntity", 64, this, getInventoryAccess()).withConfiguredAccess();
|
||||
this.crafter = new RecipeCrafter(ModRecipes.INDUSTRIAL_SAWMILL, this, 1, 3, this.inventory, inputs, outputs);
|
||||
this.tank = new Tank("SawmillBlockEntity", IndustrialSawmillBlockEntity.TANK_CAPACITY, this);
|
||||
this.ticksSinceLastChange = 0;
|
||||
|
@ -103,9 +103,7 @@ public class IndustrialSawmillBlockEntity extends GenericMachineBlockEntity impl
|
|||
ticksSinceLastChange = 0;
|
||||
}
|
||||
|
||||
if (!world.isClient && getMutliBlock()) {
|
||||
super.tick();
|
||||
}
|
||||
super.tick();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ import net.minecraft.util.math.Direction;
|
|||
import net.minecraft.util.registry.Registry;
|
||||
import reborncore.common.fluid.container.GenericFluidContainer;
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import reborncore.common.fluid.container.ItemFluidInfo;
|
||||
import reborncore.mixin.extensions.FluidBlockExtensions;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -57,44 +58,81 @@ public class FluidUtils {
|
|||
|
||||
if (inputStack.isEmpty()) return false;
|
||||
|
||||
if(inputStack.getCount() > 1){
|
||||
System.out.println("TODO make this support more than 1 item");
|
||||
return false;
|
||||
}
|
||||
if(!(inputStack.getItem() instanceof ItemFluidInfo)) return false;
|
||||
|
||||
if (outputStack.getCount() >= outputStack.getMaxCount()) return false;
|
||||
|
||||
GenericFluidContainer<ItemStack> source = GenericFluidContainer.fromStack(inputStack);
|
||||
if(source == null || source.getFluidInstance(inputStack).isEmpty()){
|
||||
ItemFluidInfo itemFluidInfo = (ItemFluidInfo) inputStack.getItem();
|
||||
|
||||
if(itemFluidInfo.getFluid(inputStack) == Fluids.EMPTY) return false;
|
||||
|
||||
FluidInstance targetFluidInstance = target.getFluidInstance(null);
|
||||
Fluid currentFluid = targetFluidInstance.getFluid();
|
||||
|
||||
if(currentFluid == Fluids.EMPTY || currentFluid == itemFluidInfo.getFluid(inputStack)) {
|
||||
int freeSpace = target.getCapacity(null) - targetFluidInstance.getAmount();
|
||||
|
||||
if(!outputStack.isEmpty()){
|
||||
if(outputStack.getCount() + 1 >= outputStack.getMaxCount()){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(freeSpace >= 1000){
|
||||
inputStack.decrement(1);
|
||||
targetFluidInstance.setFluid(itemFluidInfo.getFluid(inputStack));
|
||||
targetFluidInstance.addAmount(1000);
|
||||
|
||||
if(outputStack.isEmpty()){
|
||||
inventory.setInvStack(outputSlot, itemFluidInfo.getEmpty());
|
||||
} else {
|
||||
outputStack.increment(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean fillContainers(GenericFluidContainer<Direction> source, Inventory inventory, int inputSlot, int outputSlot, Fluid fluidToFill) {
|
||||
ItemStack inputStack = inventory.getInvStack(inputSlot);
|
||||
ItemStack outputStack = inventory.getInvStack(outputSlot);
|
||||
|
||||
if (inputStack.isEmpty()) return false;
|
||||
|
||||
if(!(inputStack.getItem() instanceof ItemFluidInfo)) return false;
|
||||
|
||||
ItemFluidInfo itemFluidInfo = (ItemFluidInfo) inputStack.getItem();
|
||||
if(itemFluidInfo.getFluid(inputStack) != Fluids.EMPTY) return false;
|
||||
|
||||
FluidInstance sourceFluid = source.getFluidInstance(null);
|
||||
|
||||
if(sourceFluid.getFluid() == Fluids.EMPTY || sourceFluid.getAmount() < 1000){
|
||||
return false;
|
||||
}
|
||||
|
||||
Fluid fluidType = source.getFluidInstance(inputStack).getFluid();
|
||||
FluidInstance contained = target.getFluidInstance(null);
|
||||
if(!outputStack.isEmpty()){
|
||||
if (outputStack.getCount() >= outputStack.getMaxCount()) return false;
|
||||
|
||||
if(!contained.isEmpty()){
|
||||
//Check that the fluid we are trying to insert into is the same
|
||||
if(contained.getFluid() != fluidType){
|
||||
if(!(outputStack.getItem() instanceof ItemFluidInfo)) return false;
|
||||
|
||||
ItemFluidInfo outputFluidInfo = (ItemFluidInfo) outputStack.getItem();
|
||||
|
||||
if(outputFluidInfo.getFluid(outputStack) != sourceFluid.getFluid()){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int transferAmount = 10;
|
||||
sourceFluid.subtractAmount(1000);
|
||||
|
||||
if(source.canExtractFluid(inputStack, fluidType, transferAmount) && target.canInsertFluid(null, fluidType, transferAmount)){
|
||||
target.insertFluid(null, fluidType, transferAmount);
|
||||
source.extractFluid(inputStack, fluidType, transferAmount);
|
||||
if(source.getCurrentFluidAmount(inputStack) < 1){
|
||||
inventory.setInvStack(outputSlot, inputStack);
|
||||
inventory.setInvStack(inputSlot, ItemStack.EMPTY);
|
||||
}
|
||||
return true;
|
||||
if(outputStack.isEmpty()){
|
||||
inventory.setInvStack(outputSlot, itemFluidInfo.getFull(sourceFluid.getFluid()));
|
||||
} else {
|
||||
outputStack.increment(1);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
inputStack.decrement(1);
|
||||
|
||||
public static boolean fillContainers(GenericFluidContainer<Direction> source, Inventory inventory, int inputSlot, int outputSlot, Fluid fluidToFill) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue