parent
d85501a057
commit
1329c30556
23 changed files with 287 additions and 318 deletions
|
@ -26,7 +26,7 @@ package techreborn.api.generator;
|
|||
|
||||
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import techreborn.utils.FluidUtils;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
|
||||
public record FluidGeneratorRecipe(Fluid fluid, int energyPerMb,
|
||||
EFluidGenerator generatorType) {
|
||||
|
|
|
@ -26,7 +26,7 @@ package techreborn.api.generator;
|
|||
|
||||
import com.google.common.collect.Sets;
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import techreborn.utils.FluidUtils;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
|
|
|
@ -37,7 +37,7 @@ import reborncore.common.crafting.ingredient.RebornIngredient;
|
|||
import reborncore.common.fluid.container.FluidInstance;
|
||||
import reborncore.common.util.Tank;
|
||||
import techreborn.blockentity.machine.multiblock.FluidReplicatorBlockEntity;
|
||||
import techreborn.utils.FluidUtils;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
|
||||
public class FluidReplicatorRecipe extends RebornFluidRecipe {
|
||||
|
||||
|
@ -66,7 +66,7 @@ public class FluidReplicatorRecipe extends RebornFluidRecipe {
|
|||
return false;
|
||||
}
|
||||
final BlockPos hole = blockEntity.getPos().offset(blockEntity.getFacing().getOpposite(), 2);
|
||||
final Fluid fluid = techreborn.utils.FluidUtils.fluidFromBlock(blockEntity.getWorld().getBlockState(hole).getBlock());
|
||||
final Fluid fluid = FluidUtils.fluidFromBlock(blockEntity.getWorld().getBlockState(hole).getBlock());
|
||||
if (fluid == Fluids.EMPTY) {
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -38,7 +38,6 @@ import reborncore.api.blockentity.InventoryProvider;
|
|||
import reborncore.common.blockentity.MachineBaseBlockEntity;
|
||||
import reborncore.common.blocks.BlockMachineBase;
|
||||
import reborncore.common.fluid.FluidValue;
|
||||
import reborncore.common.fluid.container.ItemFluidInfo;
|
||||
import reborncore.common.powerSystem.PowerAcceptorBlockEntity;
|
||||
import reborncore.common.util.RebornInventory;
|
||||
import reborncore.common.util.Tank;
|
||||
|
@ -46,7 +45,7 @@ import techreborn.api.generator.EFluidGenerator;
|
|||
import techreborn.api.generator.FluidGeneratorRecipe;
|
||||
import techreborn.api.generator.FluidGeneratorRecipeList;
|
||||
import techreborn.api.generator.GeneratorRecipeHelper;
|
||||
import techreborn.utils.FluidUtils;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
|
||||
public abstract class BaseFluidGeneratorBlockEntity extends PowerAcceptorBlockEntity implements IToolDrop, InventoryProvider {
|
||||
|
||||
|
@ -89,10 +88,10 @@ public abstract class BaseFluidGeneratorBlockEntity extends PowerAcceptorBlockEn
|
|||
if (ticksSinceLastChange >= 10) {
|
||||
ItemStack inputStack = inventory.getStack(0);
|
||||
if (!inputStack.isEmpty()) {
|
||||
if (FluidUtils.isContainerEmpty(inputStack) && !tank.getFluidAmount().isEmpty()) {
|
||||
FluidUtils.fillContainers(tank, inventory, 0, 1);
|
||||
} else if (inputStack.getItem() instanceof ItemFluidInfo && getRecipes().getRecipeForFluid(((ItemFluidInfo) inputStack.getItem()).getFluid(inputStack)).isPresent()) {
|
||||
if (FluidUtils.containsMatchingFluid(inputStack, f -> getRecipes().getRecipeForFluid(f).isPresent())) {
|
||||
FluidUtils.drainContainers(tank, inventory, 0, 1);
|
||||
} else {
|
||||
FluidUtils.fillContainers(tank, inventory, 0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ import techreborn.config.TechRebornConfig;
|
|||
import techreborn.init.ModRecipes;
|
||||
import techreborn.init.TRBlockEntities;
|
||||
import techreborn.init.TRContent;
|
||||
import techreborn.utils.FluidUtils;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
|
||||
/**
|
||||
* @author drcrazy
|
||||
|
|
|
@ -47,7 +47,7 @@ import techreborn.config.TechRebornConfig;
|
|||
import techreborn.init.ModRecipes;
|
||||
import techreborn.init.TRBlockEntities;
|
||||
import techreborn.init.TRContent;
|
||||
import techreborn.utils.FluidUtils;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
|
||||
public class IndustrialGrinderBlockEntity extends GenericMachineBlockEntity implements BuiltScreenHandlerProvider {
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ import techreborn.config.TechRebornConfig;
|
|||
import techreborn.init.ModRecipes;
|
||||
import techreborn.init.TRBlockEntities;
|
||||
import techreborn.init.TRContent;
|
||||
import techreborn.utils.FluidUtils;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
|
||||
public class IndustrialSawmillBlockEntity extends GenericMachineBlockEntity implements BuiltScreenHandlerProvider {
|
||||
|
||||
|
|
|
@ -43,15 +43,13 @@ import reborncore.client.screen.BuiltScreenHandlerProvider;
|
|||
import reborncore.client.screen.builder.BuiltScreenHandler;
|
||||
import reborncore.client.screen.builder.ScreenHandlerBuilder;
|
||||
import reborncore.common.blockentity.MachineBaseBlockEntity;
|
||||
import reborncore.common.fluid.FluidUtil;
|
||||
import reborncore.common.fluid.FluidValue;
|
||||
import reborncore.common.fluid.container.FluidInstance;
|
||||
import reborncore.common.util.FluidTextHelper;
|
||||
import reborncore.common.util.RebornInventory;
|
||||
import reborncore.common.util.Tank;
|
||||
import techreborn.init.TRBlockEntities;
|
||||
import techreborn.init.TRContent;
|
||||
import techreborn.utils.FluidUtils;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -160,7 +158,7 @@ public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements I
|
|||
info.add(
|
||||
new LiteralText(String.valueOf(this.tank.getFluidAmount()))
|
||||
.append(new TranslatableText("techreborn.tooltip.unit.divider"))
|
||||
.append(WordUtils.capitalize(FluidUtil.getFluidName(this.tank.getFluid())))
|
||||
.append(WordUtils.capitalize(FluidUtils.getFluidName(this.tank.getFluid())))
|
||||
);
|
||||
} else {
|
||||
info.add(new TranslatableText("techreborn.tooltip.unit.empty"));
|
||||
|
|
|
@ -29,7 +29,7 @@ import net.minecraft.entity.player.PlayerEntity;
|
|||
import net.minecraft.text.TranslatableText;
|
||||
import reborncore.client.gui.builder.GuiBase;
|
||||
import reborncore.client.screen.builder.BuiltScreenHandler;
|
||||
import reborncore.common.fluid.FluidUtil;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
import reborncore.common.fluid.container.FluidInstance;
|
||||
import techreborn.blockentity.storage.fluid.TankUnitBaseBlockEntity;
|
||||
|
||||
|
@ -67,7 +67,7 @@ public class GuiTankUnit extends GuiBase<BuiltScreenHandler> {
|
|||
textRenderer.draw(matrixStack, new TranslatableText("techreborn.tooltip.unit.empty"), 10, 20, 4210752);
|
||||
} else {
|
||||
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.tank.type"), 10, 20, 4210752);
|
||||
textRenderer.draw(matrixStack, FluidUtil.getFluidName(fluid).replace("_", " "), 10, 30, 4210752);
|
||||
textRenderer.draw(matrixStack, FluidUtils.getFluidName(fluid).replace("_", " "), 10, 30, 4210752);
|
||||
|
||||
|
||||
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.tank.amount"), 10, 50, 4210752);
|
||||
|
|
|
@ -215,6 +215,7 @@ public class ModRegistry {
|
|||
RebornRegistry.registerItem(TRContent.MANUAL = InitUtils.setup(new ManualItem(), "manual"));
|
||||
RebornRegistry.registerItem(TRContent.DEBUG_TOOL = InitUtils.setup(new DebugToolItem(), "debug_tool"));
|
||||
RebornRegistry.registerItem(TRContent.CELL = InitUtils.setup(new DynamicCellItem(), "cell"));
|
||||
TRContent.CELL.registerFluidApi();
|
||||
|
||||
TechReborn.LOGGER.debug("TechReborns Items Loaded");
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ import net.minecraft.util.math.BlockPos;
|
|||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.event.GameEvent;
|
||||
import techreborn.items.DynamicCellItem;
|
||||
import techreborn.utils.FluidUtils;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
|
||||
public class TRCauldronBehavior {
|
||||
public static void init() {
|
||||
|
|
|
@ -24,6 +24,13 @@
|
|||
|
||||
package techreborn.items;
|
||||
|
||||
import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
|
||||
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
|
||||
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleVariantItemStorage;
|
||||
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.FluidDrainable;
|
||||
import net.minecraft.block.FluidFillable;
|
||||
|
@ -60,12 +67,11 @@ import net.minecraft.world.event.GameEvent;
|
|||
import org.apache.commons.lang3.Validate;
|
||||
import org.apache.commons.lang3.text.WordUtils;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import reborncore.common.fluid.FluidUtil;
|
||||
import reborncore.common.fluid.container.ItemFluidInfo;
|
||||
import reborncore.common.util.ItemNBTHelper;
|
||||
import techreborn.TechReborn;
|
||||
import techreborn.init.TRContent;
|
||||
import techreborn.utils.FluidUtils;
|
||||
import reborncore.common.fluid.FluidUtils;
|
||||
|
||||
/**
|
||||
* Created by modmuss50 on 17/05/2016.
|
||||
|
@ -160,7 +166,7 @@ public class DynamicCellItem extends Item implements ItemFluidInfo {
|
|||
Fluid fluid = getFluid(itemStack);
|
||||
if (fluid != Fluids.EMPTY) {
|
||||
//TODO use translation keys for fluid and the cell https://fabric.asie.pl/wiki/tutorial:lang?s[]=translation might be useful
|
||||
return new LiteralText(WordUtils.capitalizeFully(FluidUtil.getFluidName(fluid).replaceAll("_", " ")) + " Cell");
|
||||
return new LiteralText(WordUtils.capitalizeFully(FluidUtils.getFluidName(fluid).replaceAll("_", " ")) + " Cell");
|
||||
}
|
||||
return super.getName(itemStack);
|
||||
}
|
||||
|
@ -238,10 +244,75 @@ public class DynamicCellItem extends Item implements ItemFluidInfo {
|
|||
|
||||
@Override
|
||||
public Fluid getFluid(ItemStack itemStack) {
|
||||
NbtCompound tag = itemStack.getNbt();
|
||||
return getFluid(itemStack.getNbt());
|
||||
}
|
||||
|
||||
private Fluid getFluid(@Nullable NbtCompound tag) {
|
||||
if (tag != null && tag.contains("fluid")) {
|
||||
return Registry.FLUID.get(new Identifier(tag.getString("fluid")));
|
||||
}
|
||||
return Fluids.EMPTY;
|
||||
}
|
||||
|
||||
public void registerFluidApi() {
|
||||
FluidStorage.ITEM.registerForItems((stack, ctx) -> new CellStorage(ctx), this);
|
||||
}
|
||||
|
||||
public class CellStorage extends SingleVariantItemStorage<FluidVariant> {
|
||||
public CellStorage(ContainerItemContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FluidVariant getBlankResource() {
|
||||
return FluidVariant.blank();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FluidVariant getResource(ItemVariant currentVariant) {
|
||||
return FluidVariant.of(getFluid(currentVariant.getNbt()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected long getAmount(ItemVariant currentVariant) {
|
||||
return getResource(currentVariant).isBlank() ? 0 : FluidConstants.BUCKET;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected long getCapacity(FluidVariant variant) {
|
||||
return FluidConstants.BUCKET;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ItemVariant getUpdatedVariant(ItemVariant currentVariant, FluidVariant newResource, long newAmount) {
|
||||
if (newAmount != 0 && newAmount != FluidConstants.BUCKET) {
|
||||
throw new IllegalArgumentException("Only amounts of 0 and 1 bucket are supported! This is a bug!");
|
||||
}
|
||||
// TODO: this is not ideal since we delete any extra NBT, but it probably doesn't matter in practice?
|
||||
if (newResource.isBlank() || newAmount == 0) {
|
||||
return ItemVariant.of(DynamicCellItem.this);
|
||||
} else {
|
||||
return ItemVariant.of(getCellWithFluid(newResource.getFluid()));
|
||||
}
|
||||
}
|
||||
|
||||
// A few "hacks" to ensure that transfer is always exactly 0 or 1 bucket.
|
||||
@Override
|
||||
public long insert(FluidVariant insertedResource, long maxAmount, TransactionContext transaction) {
|
||||
if (isResourceBlank() && maxAmount >= FluidConstants.BUCKET) {
|
||||
return super.insert(insertedResource, FluidConstants.BUCKET, transaction);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long extract(FluidVariant extractedResource, long maxAmount, TransactionContext transaction) {
|
||||
if (!isResourceBlank() && maxAmount >= FluidConstants.BUCKET) {
|
||||
return super.extract(extractedResource, FluidConstants.BUCKET, transaction);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,142 +0,0 @@
|
|||
/*
|
||||
* This file is part of TechReborn, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2020 TechReborn
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package techreborn.utils;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import net.minecraft.fluid.Fluids;
|
||||
import net.minecraft.inventory.Inventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import reborncore.common.fluid.FluidValue;
|
||||
import reborncore.common.fluid.container.FluidInstance;
|
||||
import reborncore.common.fluid.container.ItemFluidInfo;
|
||||
import reborncore.common.util.Tank;
|
||||
import reborncore.mixin.common.AccessorFluidBlock;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FluidUtils {
|
||||
|
||||
@NotNull
|
||||
public static Fluid fluidFromBlock(Block block) {
|
||||
if (block instanceof AccessorFluidBlock) {
|
||||
return ((AccessorFluidBlock) block).getFluid();
|
||||
}
|
||||
return Fluids.EMPTY;
|
||||
}
|
||||
|
||||
public static List<Fluid> getAllFluids() {
|
||||
return Registry.FLUID.stream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static boolean drainContainers(Tank tank, Inventory inventory, int inputSlot, int outputSlot) {
|
||||
return drainContainers(tank, inventory, inputSlot, outputSlot, false);
|
||||
}
|
||||
|
||||
public static boolean drainContainers(Tank tank, Inventory inventory, int inputSlot, int outputSlot, boolean voidFluid) {
|
||||
ItemStack inputStack = inventory.getStack(inputSlot);
|
||||
ItemStack outputStack = inventory.getStack(outputSlot);
|
||||
|
||||
if (inputStack.isEmpty()) return false;
|
||||
if (!(inputStack.getItem() instanceof ItemFluidInfo itemFluidInfo)) return false;
|
||||
if (FluidUtils.isContainerEmpty(inputStack)) return false;
|
||||
if (outputStack.getCount() >= outputStack.getMaxCount()) return false;
|
||||
if (!outputStack.isEmpty() && !FluidUtils.isContainerEmpty(outputStack)) return false;
|
||||
|
||||
if (!outputStack.isEmpty() && !outputStack.isItemEqual(itemFluidInfo.getEmpty())) return false;
|
||||
|
||||
FluidInstance tankFluidInstance = tank.getFluidInstance();
|
||||
Fluid tankFluid = tankFluidInstance.getFluid();
|
||||
|
||||
if (tankFluidInstance.isEmpty() || tankFluid == itemFluidInfo.getFluid(inputStack)) {
|
||||
FluidValue freeSpace = tank.getFluidValueCapacity().subtract(tankFluidInstance.getAmount());
|
||||
|
||||
if (freeSpace.equalOrMoreThan(FluidValue.BUCKET) || voidFluid) {
|
||||
inputStack.decrement(1);
|
||||
if (!voidFluid) {
|
||||
tankFluidInstance.setFluid(itemFluidInfo.getFluid(inputStack));
|
||||
tankFluidInstance.addAmount(FluidValue.BUCKET);
|
||||
}
|
||||
if (outputStack.isEmpty()) {
|
||||
inventory.setStack(outputSlot, itemFluidInfo.getEmpty());
|
||||
} else {
|
||||
outputStack.increment(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean fillContainers(Tank source, Inventory inventory, int inputSlot, int outputSlot) {
|
||||
ItemStack inputStack = inventory.getStack(inputSlot);
|
||||
ItemStack outputStack = inventory.getStack(outputSlot);
|
||||
|
||||
if (!FluidUtils.isContainerEmpty(inputStack)) return false;
|
||||
ItemFluidInfo itemFluidInfo = (ItemFluidInfo) inputStack.getItem();
|
||||
FluidInstance sourceFluid = source.getFluidInstance();
|
||||
|
||||
if (sourceFluid.getFluid() == Fluids.EMPTY || sourceFluid.getAmount().lessThan(FluidValue.BUCKET)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!outputStack.isEmpty()) {
|
||||
if (outputStack.getCount() >= outputStack.getMaxCount()) return false;
|
||||
if (!(outputStack.getItem() instanceof ItemFluidInfo outputFluidInfo)) return false;
|
||||
if (!outputStack.isItemEqual(itemFluidInfo.getEmpty())) return false;
|
||||
|
||||
if (outputFluidInfo.getFluid(outputStack) != sourceFluid.getFluid()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (outputStack.isEmpty()) {
|
||||
inventory.setStack(outputSlot, itemFluidInfo.getFull(sourceFluid.getFluid()));
|
||||
} else {
|
||||
outputStack.increment(1);
|
||||
}
|
||||
|
||||
sourceFluid.subtractAmount(FluidValue.BUCKET);
|
||||
inputStack.decrement(1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean fluidEquals(@NotNull Fluid fluid, @NotNull Fluid fluid1) {
|
||||
return fluid == fluid1;
|
||||
}
|
||||
|
||||
public static boolean isContainerEmpty(ItemStack stack) {
|
||||
if (stack.isEmpty())
|
||||
return false;
|
||||
if (stack.getItem() instanceof ItemFluidInfo itemFluidInfo)
|
||||
return itemFluidInfo.getFluid(stack) == Fluids.EMPTY;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue