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