Fix #2566: Implement Fabric's Fluid-containing Item API (#2706)

This commit is contained in:
Technici4n 2022-01-03 18:29:46 +01:00 committed by GitHub
parent d85501a057
commit 1329c30556
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 287 additions and 318 deletions

View file

@ -107,7 +107,6 @@ public class RebornCore implements ModInitializer {
ServerBoundPackets.init();
IngredientManager.setup();
RebornFluidManager.setupBucketMap();
RebornCoreCommands.setup();

View file

@ -26,7 +26,6 @@ package reborncore.client.gui.guibuilder;
import com.google.common.collect.Lists;
import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
import net.fabricmc.fabric.api.transfer.v1.client.fluid.FluidVariantRendering;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
@ -52,12 +51,11 @@ import reborncore.api.IListInfoProvider;
import reborncore.client.RenderUtil;
import reborncore.client.gui.builder.GuiBase;
import reborncore.client.gui.builder.slot.GuiTab;
import reborncore.common.fluid.FluidUtil;
import reborncore.common.fluid.FluidUtils;
import reborncore.common.fluid.FluidValue;
import reborncore.common.fluid.container.FluidInstance;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PowerSystem.EnergySystem;
import reborncore.common.util.FluidTextHelper;
import reborncore.common.util.StringUtils;
import java.util.ArrayList;
@ -682,7 +680,7 @@ public class GuiBuilder {
new LiteralText(String.format("%s / %s", amount, maxCapacity))
.formatted(Formatting.GOLD)
.append(SPACE_TEXT)
.append(FluidUtil.getFluidName(fluid))
.append(FluidUtils.getFluidName(fluid))
);
}

View file

@ -1,42 +0,0 @@
/*
* This file is part of RebornCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 TeamReborn
*
* 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 reborncore.client.gui.slots;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import reborncore.common.fluid.FluidUtil;
public class SlotFluid extends BaseSlot {
public SlotFluid(Inventory p_i1824_1_, int p_i1824_2_, int p_i1824_3_, int p_i1824_4_) {
super(p_i1824_1_, p_i1824_2_, p_i1824_3_, p_i1824_4_);
}
@Override
public boolean canInsert(ItemStack stack) {
return FluidUtil.getFluidHandler(stack) != null;
}
}

View file

@ -26,9 +26,6 @@ package reborncore.client.screen.builder;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.item.base.SingleStackStorage;
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.inventory.CraftingInventory;
@ -49,7 +46,7 @@ import reborncore.client.gui.slots.SlotOutput;
import reborncore.client.screen.builder.slot.FilteredSlot;
import reborncore.client.screen.builder.slot.UpgradeSlot;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.fluid.container.ItemFluidInfo;
import reborncore.common.fluid.FluidUtils;
import reborncore.common.powerSystem.PowerAcceptorBlockEntity;
import team.reborn.energy.api.EnergyStorageUtil;
@ -114,8 +111,7 @@ public class BlockEntityScreenHandlerBuilder {
}
public BlockEntityScreenHandlerBuilder fluidSlot(final int index, final int x, final int y) {
this.parent.slots.add(new FilteredSlot(this.inventory, index, x, y).setFilter(
stack -> stack.getItem() instanceof ItemFluidInfo));
this.parent.slots.add(new FilteredSlot(this.inventory, index, x, y).setFilter(FluidUtils::isContainer));
return this;
}

View file

@ -28,15 +28,12 @@ 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.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageUtil;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import reborncore.common.fluid.FluidUtil;
import reborncore.common.util.NBTSerializable;
import reborncore.common.util.Tank;
import java.util.ArrayList;
import java.util.Arrays;

View file

@ -56,7 +56,7 @@ import reborncore.api.blockentity.IUpgrade;
import reborncore.api.blockentity.IUpgradeable;
import reborncore.common.BaseBlockEntityProvider;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.fluid.FluidUtil;
import reborncore.common.fluid.FluidUtils;
import reborncore.common.util.ItemHandlerUtils;
import reborncore.common.util.Tank;
import reborncore.common.util.WrenchUtils;
@ -187,7 +187,7 @@ public abstract class BlockMachineBase extends BaseBlockEntityProvider implement
if (blockEntity instanceof MachineBaseBlockEntity) {
Tank tank = ((MachineBaseBlockEntity) blockEntity).getTank();
if (tank != null && FluidUtil.interactWithFluidHandler(playerIn, hand, tank)) {
if (tank != null && FluidUtils.interactWithFluidHandler(playerIn, hand, tank)) {
return ActionResult.SUCCESS;
}
}

View file

@ -1,64 +0,0 @@
/*
* This file is part of RebornCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 TeamReborn
*
* 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 reborncore.common.fluid;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand;
import net.minecraft.util.registry.Registry;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import reborncore.common.fluid.container.FluidInstance;
import reborncore.common.util.Tank;
public class FluidUtil {
@Deprecated
public static FluidInstance getFluidHandler(ItemStack stack) {
return null;
}
@Deprecated
public static boolean interactWithFluidHandler(PlayerEntity playerIn, Hand hand, Tank tank) {
return false;
}
@Deprecated
public static ItemStack getFilledBucket(FluidInstance stack) {
return null;
}
public static String getFluidName(@NotNull FluidInstance fluidInstance) {
// TODO: use FluidVariantRendering
return getFluidName(fluidInstance.getFluid());
}
public static String getFluidName(@NotNull Fluid fluid) {
return StringUtils.capitalize(Registry.FLUID.getId(fluid).getPath());
}
}

View file

@ -0,0 +1,188 @@
/*
* 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 reborncore.common.fluid;
import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
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.InventoryStorage;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageUtil;
import net.fabricmc.fabric.api.transfer.v1.storage.base.FilteringStorage;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.block.Block;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand;
import net.minecraft.util.registry.Registry;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import reborncore.common.fluid.container.FluidInstance;
import reborncore.common.util.Tank;
import reborncore.mixin.common.AccessorFluidBlock;
import java.util.List;
import java.util.function.Predicate;
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) {
Storage<FluidVariant> itemStorage = getItemFluidStorage(inventory, inputSlot, outputSlot);
if (voidFluid) {
// Just extract as much as we can
try (Transaction tx = Transaction.openOuter()) {
boolean didSomething = false;
for (var view : itemStorage.iterable(tx)) {
if (view.isResourceBlank()) continue;
didSomething = didSomething | view.extract(view.getResource(), Long.MAX_VALUE, tx) > 0;
}
tx.commit();
return didSomething;
}
} else {
return StorageUtil.move(itemStorage, tank, fv -> true, Long.MAX_VALUE, null) > 0;
}
}
public static boolean fillContainers(Tank source, Inventory inventory, int inputSlot, int outputSlot) {
return StorageUtil.move(
source,
getItemFluidStorage(inventory, inputSlot, outputSlot),
fv -> true,
Long.MAX_VALUE,
null
) > 0;
}
private static Storage<FluidVariant> getItemFluidStorage(Inventory inventory, int inputSlot, int outputSlot) {
var invWrapper = InventoryStorage.of(inventory, null);
var input = invWrapper.getSlot(inputSlot);
var output = invWrapper.getSlot(outputSlot);
var context = new ContainerItemContext() {
@Override
public SingleSlotStorage<ItemVariant> getMainSlot() {
return input;
}
@Override
public long insertOverflow(ItemVariant itemVariant, long maxAmount, TransactionContext transactionContext) {
return output.insert(itemVariant, maxAmount, transactionContext);
}
@Override
public long insert(ItemVariant itemVariant, long maxAmount, TransactionContext transaction) {
// Don't allow insertion in the input slot
return insertOverflow(itemVariant, maxAmount, transaction);
}
@Override
public List<SingleSlotStorage<ItemVariant>> getAdditionalSlots() {
return List.of();
}
};
var storage = context.find(FluidStorage.ITEM);
return storage != null ? storage : Storage.empty();
}
public static boolean fluidEquals(@NotNull Fluid fluid, @NotNull Fluid fluid1) {
return fluid == fluid1;
}
public static boolean isContainer(ItemStack stack) {
return ContainerItemContext.withInitial(stack).find(FluidStorage.ITEM) != null;
}
public static boolean isContainerEmpty(ItemStack stack) {
var fluidStorage = ContainerItemContext.withInitial(stack).find(FluidStorage.ITEM);
if (fluidStorage == null) return false;
// Use current transaction in case this check is nested in a transfer operation.
try (var tx = Transaction.openNested(Transaction.getCurrentUnsafe())) {
for (var view : fluidStorage.iterable(tx)) {
if (!view.isResourceBlank() && view.getAmount() > 0) {
return false;
}
}
}
return true;
}
public static boolean containsMatchingFluid(ItemStack stack, Predicate<Fluid> predicate) {
var fluidStorage = ContainerItemContext.withInitial(stack).find(FluidStorage.ITEM);
if (fluidStorage == null) return false;
// Use current transaction in case this check is nested in a transfer operation.
try (var tx = Transaction.openNested(Transaction.getCurrentUnsafe())) {
for (var view : fluidStorage.iterable(tx)) {
if (!view.isResourceBlank() && view.getAmount() > 0 && predicate.test(view.getResource().getFluid())) {
return true;
}
}
}
return false;
}
@Deprecated
public static boolean interactWithFluidHandler(PlayerEntity playerIn, Hand hand, Tank tank) {
// TODO
return false;
}
public static String getFluidName(@NotNull FluidInstance fluidInstance) {
// TODO: use FluidVariantRendering
return getFluidName(fluidInstance.getFluid());
}
public static String getFluidName(@NotNull Fluid fluid) {
return StringUtils.capitalize(Registry.FLUID.getId(fluid).getPath());
}
}

View file

@ -24,49 +24,21 @@
package reborncore.common.fluid;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.BucketItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.Lazy;
import net.minecraft.util.registry.Registry;
import reborncore.common.fluid.container.ItemFluidInfo;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
public class RebornFluidManager {
private static final HashMap<Identifier, RebornFluid> fluids = new HashMap<>();
private static Lazy<Map<Fluid, BucketItem>> bucketMap;
public static void register(RebornFluid rebornFluid, Identifier identifier) {
fluids.put(identifier, rebornFluid);
Registry.register(Registry.FLUID, identifier, rebornFluid);
}
public static void setupBucketMap() {
bucketMap = new Lazy<>(() -> {
Map<Fluid, BucketItem> map = new HashMap<>();
Registry.ITEM.stream().filter(item -> item instanceof BucketItem).forEach(item -> {
BucketItem bucketItem = (BucketItem) item;
//We can be sure of this as we add this via a mixin
ItemFluidInfo fluidInfo = (ItemFluidInfo) bucketItem;
Fluid fluid = fluidInfo.getFluid(new ItemStack(item));
if (!map.containsKey(fluid)) {
map.put(fluid, bucketItem);
}
});
return map;
});
}
public static Map<Fluid, BucketItem> getBucketMap() {
return bucketMap.get();
}
public static HashMap<Identifier, RebornFluid> getFluids() {
return fluids;
}

View file

@ -31,7 +31,6 @@ import net.minecraft.item.Items;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import reborncore.common.fluid.RebornFluidManager;
import reborncore.common.fluid.container.ItemFluidInfo;
@Mixin(BucketItem.class)
@ -48,8 +47,7 @@ public class MixinBucketItem implements ItemFluidInfo {
@Override
public ItemStack getFull(Fluid fluid) {
BucketItem item = RebornFluidManager.getBucketMap().get(fluid);
return new ItemStack(item);
return new ItemStack(fluid.getBucketItem());
}
@Override

View file

@ -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) {

View file

@ -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;

View file

@ -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;
}

View file

@ -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);
}
}

View file

@ -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

View file

@ -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 {

View file

@ -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 {

View file

@ -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"));

View file

@ -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);

View file

@ -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");
}

View file

@ -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() {

View file

@ -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;
}
}
}
}

View file

@ -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;
}
}