Skip to content

Commit

Permalink
Remove transfer wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Oct 31, 2023
1 parent 583a0e7 commit d6daf00
Show file tree
Hide file tree
Showing 47 changed files with 135 additions and 2,530 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ dependencies {
modImplementation("net.fabricmc:fabric-loader:${project.loader_version}")
modImplementation("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")

modApi(include("io.github.fabricators_of_create.Porting-Lib:Porting-Lib:${project.port_lib_version}+1.20-entity-refactor"))
modImplementation("io.github.fabricators_of_create.Porting-Lib:Porting-Lib:${project.port_lib_version}+1.20-entity-refactor")

// compile against the REI API but do not include it at runtime
modCompileOnly("me.shedaniel:RoughlyEnoughItems-api-fabric:${project.rei_version}")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ satin_version = 1.11.0
modmenu_version=7.2.1
cca_version=5.2.2

port_lib_version = 2.1.1136
port_lib_version = 2.1.1151
star_version = 1.5.1
3 changes: 0 additions & 3 deletions src/main/java/slimeknights/mantle/Mantle.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import slimeknights.mantle.registration.MantleRegistrations;
import slimeknights.mantle.registration.adapter.BlockEntityTypeRegistryAdapter;
import slimeknights.mantle.registration.adapter.RegistryAdapter;
import slimeknights.mantle.transfer.TransferUtil;

/**
* Mantle
Expand Down Expand Up @@ -72,8 +71,6 @@ public void onInitialize() {
this.registerBlockEntities();
MantleLoot.registerGlobalLootModifiers();
UseBlockCallback.EVENT.register(LecternBookItem::interactWithBlock);
TransferUtil.registerFluidStorage();
TransferUtil.registerItemStorage();
}

private void registerCapabilities() {
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/slimeknights/mantle/block/InventoryBlock.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package slimeknights.mantle.block;

import io.github.fabricators_of_create.porting_lib.util.NetworkHooks;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
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.StorageView;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Containers;
Expand All @@ -19,8 +23,6 @@
import net.minecraft.world.phys.BlockHitResult;
import slimeknights.mantle.block.entity.INameableMenuProvider;
import slimeknights.mantle.inventory.BaseContainerMenu;
import slimeknights.mantle.transfer.TransferUtil;
import slimeknights.mantle.transfer.item.IItemHandler;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -101,11 +103,11 @@ public MenuProvider getMenuProvider(BlockState state, Level worldIn, BlockPos po
@Override
public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (state.getBlock() != newState.getBlock()) {
BlockEntity te = worldIn.getBlockEntity(pos);
if (te != null) {
TransferUtil.getItemHandler(te).ifPresent(inventory -> dropInventoryItems(state, worldIn, pos, inventory));
Storage<ItemVariant> inventory = ItemStorage.SIDED.find(worldIn, pos, null);
if (inventory != null)
dropInventoryItems(state, worldIn, pos, inventory);
if (worldIn.getBlockEntity(pos) != null)
worldIn.updateNeighbourForOutputSignal(pos, this);
}
}

super.onRemove(state, worldIn, pos, newState, isMoving);
Expand All @@ -118,7 +120,7 @@ public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState n
* @param pos Tile position
* @param inventory Item handler
*/
protected void dropInventoryItems(BlockState state, Level worldIn, BlockPos pos, IItemHandler inventory) {
protected void dropInventoryItems(BlockState state, Level worldIn, BlockPos pos, Storage<ItemVariant> inventory) {
dropInventoryItems(worldIn, pos, inventory);
}

Expand All @@ -128,12 +130,12 @@ protected void dropInventoryItems(BlockState state, Level worldIn, BlockPos pos,
* @param pos Position to drop
* @param inventory Inventory instance
*/
public static void dropInventoryItems(Level world, BlockPos pos, IItemHandler inventory) {
public static void dropInventoryItems(Level world, BlockPos pos, Storage<ItemVariant> inventory) {
double x = pos.getX();
double y = pos.getY();
double z = pos.getZ();
for(int i = 0; i < inventory.getSlots(); ++i) {
Containers.dropItemStack(world, x, y, z, inventory.getStackInSlot(i));
for(StorageView<ItemVariant> view : inventory) {
Containers.dropItemStack(world, x, y, z, view.getResource().toStack((int) Math.max(view.getResource().getItem().getMaxStackSize(), view.getAmount())));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package slimeknights.mantle.block.entity;

import lombok.Getter;
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.base.SidedStorageBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
Expand All @@ -17,17 +21,12 @@
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
import slimeknights.mantle.transfer.item.IItemHandler;
import slimeknights.mantle.transfer.item.IItemHandlerModifiable;
import slimeknights.mantle.transfer.item.wrapper.InvWrapper;
import slimeknights.mantle.transfer.item.ItemTransferable;
import io.github.fabricators_of_create.porting_lib.util.LazyOptional;
import slimeknights.mantle.util.ItemStackList;

import javax.annotation.Nonnull;

// Updated version of InventoryLogic in Mantle. Also contains a few bugfixes DOES NOT OVERRIDE createMenu
public abstract class InventoryBlockEntity extends NameableBlockEntity implements Container, MenuProvider, Nameable, ItemTransferable {
public abstract class InventoryBlockEntity extends NameableBlockEntity implements Container, MenuProvider, Nameable, SidedStorageBlockEntity {
private static final String TAG_INVENTORY_SIZE = "InventorySize";
private static final String TAG_ITEMS = "Items";
private static final String TAG_SLOT = "Slot";
Expand All @@ -37,8 +36,7 @@ public abstract class InventoryBlockEntity extends NameableBlockEntity implement
private final boolean saveSizeToNBT;
protected int stackSizeLimit;
@Getter
protected IItemHandlerModifiable itemHandler;
protected LazyOptional<IItemHandlerModifiable> itemHandlerCap;
protected InventoryStorage itemHandler;

/**
* @param name Localization String for the inventory title. Can be overridden through setCustomName
Expand All @@ -55,20 +53,13 @@ public InventoryBlockEntity(BlockEntityType<?> tileEntityTypeIn, BlockPos pos, B
this.saveSizeToNBT = saveSizeToNBT;
this.inventory = NonNullList.withSize(inventorySize, ItemStack.EMPTY);
this.stackSizeLimit = maxStackSize;
this.itemHandler = new InvWrapper(this);
this.itemHandlerCap = LazyOptional.of(() -> this.itemHandler);
this.itemHandler = InventoryStorage.of(this, null);
}

@Nonnull
@Override
public LazyOptional<IItemHandler> getItemHandler(@Nullable Direction direction) {
return this.itemHandlerCap.cast();
}

// @Override
public void invalidateCaps() {
// super.invalidateCaps();
this.itemHandlerCap.invalidate();
public Storage<ItemVariant> getItemStorage(@Nullable Direction direction) {
return this.itemHandler;
}

/* Inventory management */
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/slimeknights/mantle/command/TagsForCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
import io.github.fabricators_of_create.porting_lib.attributes.PortingLibAttributes;
import io.github.fabricators_of_create.porting_lib.fluids.FluidStack;
import io.github.fabricators_of_create.porting_lib.util.LazyOptional;
import io.github.fabricators_of_create.porting_lib.transfer.TransferUtil;
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.storage.Storage;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.ResourceLocationArgument;
Expand All @@ -17,6 +21,7 @@
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.ProjectileUtil;
Expand All @@ -40,10 +45,6 @@
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import slimeknights.mantle.transfer.TransferUtil;
import slimeknights.mantle.transfer.fluid.EmptyFluidHandler;
import slimeknights.mantle.transfer.fluid.IFluidHandler;
import slimeknights.mantle.transfer.fluid.IFluidHandlerItem;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -158,16 +159,14 @@ private static int heldBlock(CommandContext<CommandSourceStack> context) throws
/** Fluid tags for held item */
private static int heldFluid(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
CommandSourceStack source = context.getSource();
ItemStack stack = source.getPlayerOrException().getMainHandItem();
LazyOptional<IFluidHandlerItem> capability = TransferUtil.getFluidHandlerItem(stack);
if (capability.isPresent()) {
IFluidHandler handler = capability.map(h -> (IFluidHandler) h).orElse(EmptyFluidHandler.INSTANCE);
if (handler.getTanks() > 0) {
FluidStack fluidStack = handler.getFluidInTank(0);
if (!fluidStack.isEmpty()) {
Fluid fluid = fluidStack.getFluid();
return printOwningTags(context, BuiltInRegistries.FLUID, fluid);
}
Player player = source.getPlayerOrException();
ItemStack stack = player.getMainHandItem();
Storage<FluidVariant> handler = FluidStorage.ITEM.find(stack, ContainerItemContext.ofPlayerHand(player, InteractionHand.MAIN_HAND));
if (handler != null) {
FluidStack fluidStack = TransferUtil.getFirstFluid(handler);
if (fluidStack != null && !fluidStack.isEmpty()) {
Fluid fluid = fluidStack.getFluid();
return printOwningTags(context, BuiltInRegistries.FLUID, fluid);
}
}
source.sendSuccess(() -> NO_HELD_FLUID, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.fabricators_of_create.porting_lib.mixin.accessors.common.accessor.BucketItemAccessor;
import io.github.fabricators_of_create.porting_lib.transfer.TransferUtil;
import io.github.fabricators_of_create.porting_lib.fluids.FluidStack;
import io.github.fabricators_of_create.porting_lib.transfer.item.ItemHandlerHelper;
import io.github.fabricators_of_create.porting_lib.util.FluidTextUtil;
import io.github.fabricators_of_create.porting_lib.util.FluidUnit;
import lombok.AccessLevel;
Expand Down Expand Up @@ -34,7 +35,6 @@
import slimeknights.mantle.fluid.transfer.FluidContainerTransferManager;
import slimeknights.mantle.fluid.transfer.IFluidContainerTransfer;
import slimeknights.mantle.fluid.transfer.IFluidContainerTransfer.TransferResult;
import slimeknights.mantle.transfer.item.ItemHandlerHelper;

/**
* Alternative to {@link net.minecraftforge.fluids.FluidUtil} since no one has time to make the forge util not a buggy mess
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import io.github.fabricators_of_create.porting_lib.transfer.TransferUtil;
import io.github.fabricators_of_create.porting_lib.fluids.FluidStack;
import io.github.fabricators_of_create.porting_lib.transfer.TransferUtil;
import lombok.RequiredArgsConstructor;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
Expand All @@ -22,7 +22,6 @@
import slimeknights.mantle.Mantle;
import slimeknights.mantle.recipe.helper.ItemOutput;
import slimeknights.mantle.recipe.helper.RecipeHelper;
import slimeknights.mantle.transfer.fluid.IFluidHandler;
import slimeknights.mantle.util.JsonHelper;

import java.lang.reflect.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import io.github.fabricators_of_create.porting_lib.fluids.FluidStack;
import io.github.fabricators_of_create.porting_lib.transfer.TransferUtil;
import lombok.RequiredArgsConstructor;
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.transaction.Transaction;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import slimeknights.mantle.transfer.fluid.IFluidHandler;
import io.github.fabricators_of_create.porting_lib.fluids.FluidStack;
import lombok.RequiredArgsConstructor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package slimeknights.mantle.fluid.transfer;

import io.github.fabricators_of_create.porting_lib.fluids.FluidStack;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import slimeknights.mantle.transfer.fluid.IFluidHandler;
import io.github.fabricators_of_create.porting_lib.fluids.FluidStack;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import slimeknights.mantle.data.GenericRegisteredSerializer.IJsonSerializable;
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/slimeknights/mantle/inventory/EmptyItemHandler.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package slimeknights.mantle.inventory;

import io.github.fabricators_of_create.porting_lib.transfer.item.SlottedStackStorage;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.world.item.ItemStack;
import slimeknights.mantle.transfer.item.IItemHandler;

import javax.annotation.Nonnull;

/**
* Item handler that contains no items. Use similarly to {@link slimeknights.mantle.lib.transfer.fluid.EmptyFluidHandler}
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class EmptyItemHandler implements IItemHandler {
public class EmptyItemHandler implements SlottedStackStorage {
public static final EmptyItemHandler INSTANCE = new EmptyItemHandler();

@Override
public int getSlots() {
public int getSlotCount() {
return 0;
}

@Override
public SingleSlotStorage<ItemVariant> getSlot(int slot) {
return EmptyItemSlot.INSTANCE;
}

@Override
public int getSlotLimit(int slot) {
return 0;
Expand All @@ -31,19 +39,20 @@ public ItemStack getStackInSlot(int slot) {
}

@Override
public boolean isItemValid(int slot, ItemStack stack) {
public void setStackInSlot(int slot, ItemStack stack) {}

@Override
public boolean isItemValid(int slot, ItemVariant variant, int count) {
return false;
}

@Nonnull
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
return stack;
public long insert(ItemVariant resource, long maxAmount, TransactionContext transaction) {
return 0;
}

@Nonnull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
return ItemStack.EMPTY;
public long extract(ItemVariant resource, long maxAmount, TransactionContext transaction) {
return 0;
}
}
39 changes: 39 additions & 0 deletions src/main/java/slimeknights/mantle/inventory/EmptyItemSlot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package slimeknights.mantle.inventory;

import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;

public class EmptyItemSlot implements SingleSlotStorage<ItemVariant> {
public static final EmptyItemSlot INSTANCE = new EmptyItemSlot();

@Override
public long insert(ItemVariant resource, long maxAmount, TransactionContext transaction) {
return 0;
}

@Override
public long extract(ItemVariant resource, long maxAmount, TransactionContext transaction) {
return 0;
}

@Override
public boolean isResourceBlank() {
return true;
}

@Override
public ItemVariant getResource() {
return ItemVariant.blank();
}

@Override
public long getAmount() {
return 0;
}

@Override
public long getCapacity() {
return 0;
}
}
Loading

0 comments on commit d6daf00

Please sign in to comment.