Skip to content

Commit

Permalink
Item handling for tanks (#933)
Browse files Browse the repository at this point in the history
* item handling for tanks

* fix capability issue with cover/pump
  • Loading branch information
brachy84 authored May 8, 2022
1 parent bd88d5a commit 149bad0
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gregtech.api.capability.impl;

import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.ItemStackHandler;

import javax.annotation.Nonnull;
import java.util.function.Predicate;

public class FilteredItemHandler extends ItemStackHandler {

public static Predicate<ItemStack> getCapabilityFilter(Capability<?> cap) {
return stack -> stack.hasCapability(cap, null);
}

private Predicate<ItemStack> fillPredicate;

public FilteredItemHandler() {
super(1);
}

public FilteredItemHandler(int size) {
super(size);
}

public FilteredItemHandler(NonNullList<ItemStack> stacks) {
super(stacks);
}

public FilteredItemHandler setFillPredicate(Predicate<ItemStack> fillPredicate) {
this.fillPredicate = fillPredicate;
return this;
}

@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return fillPredicate == null || fillPredicate.test(stack);
}
}
34 changes: 33 additions & 1 deletion src/main/java/gregtech/api/metatileentity/MetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import gregtech.api.cover.ICoverable;
import gregtech.api.gui.ModularUI;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.sound.GTSoundManager;
import gregtech.api.recipes.RecipeMap;
import gregtech.api.sound.GTSoundManager;
import gregtech.api.util.GTTransferUtils;
import gregtech.api.util.GTUtility;
import gregtech.client.renderer.texture.Textures;
Expand Down Expand Up @@ -932,6 +932,38 @@ public boolean fillContainerFromInternalTank(IItemHandlerModifiable importItems,
return false;
}

public void fillInternalTankFromFluidContainer() {
for (int i = 0; i < importItems.getSlots(); i++) {
ItemStack inputContainerStack = importItems.extractItem(i, 1, true);
FluidActionResult result = FluidUtil.tryEmptyContainer(inputContainerStack, importFluids, Integer.MAX_VALUE, null, false);
if (result.isSuccess()) {
ItemStack remainingItem = result.getResult();
if (ItemStack.areItemStacksEqual(inputContainerStack, remainingItem))
continue; //do not fill if item stacks match
if (!remainingItem.isEmpty() && !GTTransferUtils.insertItem(exportItems, remainingItem, true).isEmpty())
continue; //do not fill if can't put remaining item
FluidUtil.tryEmptyContainer(inputContainerStack, importFluids, Integer.MAX_VALUE, null, true);
importItems.extractItem(i, 1, false);
GTTransferUtils.insertItem(exportItems, remainingItem, false);
}
}
}

public void fillContainerFromInternalTank() {
for (int i = 0; i < importItems.getSlots(); i++) {
ItemStack emptyContainer = importItems.extractItem(i, 1, true);
FluidActionResult result = FluidUtil.tryFillContainer(emptyContainer, exportFluids, Integer.MAX_VALUE, null, false);
if (result.isSuccess()) {
ItemStack remainingItem = result.getResult();
if (!remainingItem.isEmpty() && !GTTransferUtils.insertItem(exportItems, remainingItem, true).isEmpty())
continue;
FluidUtil.tryFillContainer(emptyContainer, exportFluids, Integer.MAX_VALUE, null, true);
importItems.extractItem(i, 1, false);
GTTransferUtils.insertItem(exportItems, remainingItem, false);
}
}
}

public void pushFluidsIntoNearbyHandlers(EnumFacing... allowedFaces) {
PooledMutableBlockPos blockPos = PooledMutableBlockPos.retain();
for (EnumFacing nearbyFacing : allowedFaces) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/gregtech/common/covers/CoverConveyor.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ public EnumActionResult onScrewdriverClick(EntityPlayer playerIn, EnumHand hand,
@Override
public <T> T getCapability(Capability<T> capability, T defaultValue) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
if (defaultValue == null) {
return null;
}
IItemHandler delegate = (IItemHandler) defaultValue;
if (itemHandlerWrapper == null || itemHandlerWrapper.delegate != delegate) {
this.itemHandlerWrapper = new CoverableItemHandlerWrapper(delegate);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/gregtech/common/covers/CoverPump.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ public void renderCover(CCRenderState renderState, Matrix4 translation, IVertexO
@Override
public <T> T getCapability(Capability<T> capability, T defaultValue) {
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
if (defaultValue == null) {
return null;
}
IFluidHandler delegate = (IFluidHandler) defaultValue;
if (fluidHandlerWrapper == null || fluidHandlerWrapper.delegate != delegate) {
this.fluidHandlerWrapper = new CoverableFluidHandlerWrapper(delegate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.TieredMetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.client.renderer.texture.Textures;
import gregtech.api.util.GTUtility;
import gregtech.client.renderer.texture.Textures;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.resources.I18n;
Expand Down Expand Up @@ -267,7 +267,7 @@ public void update() {
return;
}
pushFluidsIntoNearbyHandlers(getFrontFacing());
fillContainerFromInternalTank(importItems, exportItems, 0, 0);
fillContainerFromInternalTank();

//do not do anything without enough energy supplied
if (energyContainer.getEnergyStored() < GTValues.V[getTier()] * 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Matrix4;
import gregtech.api.capability.impl.FilteredItemHandler;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
Expand All @@ -17,15 +18,16 @@
import gregtech.client.renderer.ICubeRenderer;
import gregtech.client.renderer.texture.Textures;
import gregtech.common.metatileentities.multi.multiblockpart.MetaTileEntityMultiblockPart;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumTank;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;

Expand All @@ -36,12 +38,10 @@ public class MetaTileEntityPumpHatch extends MetaTileEntityMultiblockPart implem

private static final int FLUID_TANK_SIZE = 1000;
private final FluidTank waterTank;
private final ItemStackHandler containerInventory;

public MetaTileEntityPumpHatch(ResourceLocation metaTileEntityId) {
super(metaTileEntityId, 0);
this.waterTank = new FluidTank(FLUID_TANK_SIZE);
this.containerInventory = new ItemStackHandler(2);
initializeInventory();
}

Expand All @@ -50,31 +50,20 @@ public MetaTileEntity createMetaTileEntity(IGregTechTileEntity tileEntity) {
return new MetaTileEntityPumpHatch(metaTileEntityId);
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
data.setTag("ContainerInventory", containerInventory.serializeNBT());
return data;
}

@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
this.containerInventory.deserializeNBT(data.getCompoundTag("ContainerInventory"));
}

@Override
public void clearMachineInventory(NonNullList<ItemStack> itemBuffer) {
super.clearMachineInventory(itemBuffer);
clearInventory(itemBuffer, containerInventory);
if (data.hasKey("ContainerInventory")) {
MetaTileEntityQuantumTank.legacyTankItemHandlerNBTReading(this, data.getCompoundTag("ContainerInventory"), 0, 1);
}
}

@Override
public void update() {
super.update();
if (!getWorld().isRemote) {
pushFluidsIntoNearbyHandlers(getFrontFacing());
fillContainerFromInternalTank(containerInventory, containerInventory, 0, 1);
fillContainerFromInternalTank();
}
}

Expand All @@ -95,6 +84,16 @@ protected FluidTankList createExportFluidHandler() {
return new FluidTankList(false, waterTank);
}

@Override
protected IItemHandlerModifiable createImportItemHandler() {
return new FilteredItemHandler(1).setFillPredicate(FilteredItemHandler.getCapabilityFilter(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY));
}

@Override
protected IItemHandlerModifiable createExportItemHandler() {
return new ItemStackHandler(1);
}

@Override
public MultiblockAbility<IFluidTank> getAbility() {
return MultiblockAbility.PUMP_FLUID_HATCH;
Expand All @@ -112,11 +111,11 @@ public void registerAbilities(List<IFluidTank> abilityList) {

@Override
protected ModularUI createUI(EntityPlayer entityPlayer) {
return createTankUI(waterTank, containerInventory, getMetaFullName(), entityPlayer)
return createTankUI(waterTank, getMetaFullName(), entityPlayer)
.build(getHolder(), entityPlayer);
}

public ModularUI.Builder createTankUI(IFluidTank fluidTank, IItemHandlerModifiable containerInventory, String title, EntityPlayer entityPlayer) {
public ModularUI.Builder createTankUI(IFluidTank fluidTank, String title, EntityPlayer entityPlayer) {
ModularUI.Builder builder = ModularUI.defaultBuilder();
builder.image(7, 16, 81, 55, GuiTextures.DISPLAY);
TankWidget tankWidget = new TankWidget(fluidTank, 69, 52, 18, 18)
Expand All @@ -126,10 +125,10 @@ public ModularUI.Builder createTankUI(IFluidTank fluidTank, IItemHandlerModifiab
builder.dynamicLabel(11, 30, tankWidget::getFormattedFluidAmount, 0xFFFFFF);
builder.dynamicLabel(11, 40, tankWidget::getFluidLocalizedName, 0xFFFFFF);
return builder.label(6, 6, title)
.widget(new FluidContainerSlotWidget(containerInventory, 0, 90, 17, false)
.widget(new FluidContainerSlotWidget(importItems, 0, 90, 17, false)
.setBackgroundTexture(GuiTextures.SLOT, GuiTextures.IN_SLOT_OVERLAY))
.widget(new ImageWidget(91, 36, 14, 15, GuiTextures.TANK_ICON))
.widget(new SlotWidget(containerInventory, 1, 90, 54, true, false)
.widget(new SlotWidget(exportItems, 0, 90, 54, true, false)
.setBackgroundTexture(GuiTextures.SLOT, GuiTextures.OUT_SLOT_OVERLAY))
.bindPlayerInventory(entityPlayer.inventory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Matrix4;
import gregtech.api.capability.impl.FilteredItemHandler;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.capability.impl.NotifiableFluidTank;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.ModularUI.Builder;
Expand All @@ -15,18 +17,18 @@
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.multiblock.IMultiblockAbilityPart;
import gregtech.api.metatileentity.multiblock.MultiblockAbility;
import gregtech.client.renderer.texture.cube.SimpleOverlayRenderer;
import gregtech.client.renderer.texture.Textures;
import gregtech.api.capability.impl.NotifiableFluidTank;
import gregtech.client.renderer.texture.cube.SimpleOverlayRenderer;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumTank;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;

Expand All @@ -36,12 +38,10 @@
public class MetaTileEntityFluidHatch extends MetaTileEntityMultiblockNotifiablePart implements IMultiblockAbilityPart<IFluidTank> {

private static final int INITIAL_INVENTORY_SIZE = 8000;
private final ItemStackHandler containerInventory;
private final FluidTank fluidTank;

public MetaTileEntityFluidHatch(ResourceLocation metaTileEntityId, int tier, boolean isExportHatch) {
super(metaTileEntityId, tier, isExportHatch);
this.containerInventory = new ItemStackHandler(2);
this.fluidTank = new NotifiableFluidTank(getInventorySize(), this, isExportHatch);
initializeInventory();
}
Expand All @@ -54,33 +54,26 @@ public MetaTileEntity createMetaTileEntity(IGregTechTileEntity tileEntity) {
@Override
public NBTTagCompound writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
data.setTag("ContainerInventory", containerInventory.serializeNBT());
//fluidTank.writeToNBT(data);
return data;
}

@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
containerInventory.deserializeNBT(data.getCompoundTag("ContainerInventory"));
//fluidTank.readFromNBT(data);
}

@Override
public void clearMachineInventory(NonNullList<ItemStack> itemBuffer) {
super.clearMachineInventory(itemBuffer);
clearInventory(itemBuffer, containerInventory);
if (data.hasKey("ContainerInventory")) {
MetaTileEntityQuantumTank.legacyTankItemHandlerNBTReading(this, data.getCompoundTag("ContainerInventory"), 0, 1);
}
}

@Override
public void update() {
super.update();
if (!getWorld().isRemote) {
fillContainerFromInternalTank(containerInventory, containerInventory, 0, 1);
fillContainerFromInternalTank();
if (isExportHatch) {
pushFluidsIntoNearbyHandlers(getFrontFacing());
} else {
fillInternalTankFromFluidContainer(containerInventory, containerInventory, 0, 1);
fillInternalTankFromFluidContainer();
pullFluidsFromNearbyHandlers(getFrontFacing());
}
}
Expand Down Expand Up @@ -111,6 +104,16 @@ protected FluidTankList createExportFluidHandler() {
return new FluidTankList(false, fluidTank);
}

@Override
protected IItemHandlerModifiable createImportItemHandler() {
return new FilteredItemHandler(1).setFillPredicate(FilteredItemHandler.getCapabilityFilter(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY));
}

@Override
protected IItemHandlerModifiable createExportItemHandler() {
return new ItemStackHandler(1);
}

@Override
public MultiblockAbility<IFluidTank> getAbility() {
return isExportHatch ? MultiblockAbility.EXPORT_FLUIDS : MultiblockAbility.IMPORT_FLUIDS;
Expand All @@ -123,11 +126,11 @@ public void registerAbilities(List<IFluidTank> abilityList) {

@Override
protected ModularUI createUI(EntityPlayer entityPlayer) {
return createTankUI((isExportHatch ? exportFluids : importFluids).getTankAt(0), containerInventory, getMetaFullName(), entityPlayer)
return createTankUI((isExportHatch ? exportFluids : importFluids).getTankAt(0), getMetaFullName(), entityPlayer)
.build(getHolder(), entityPlayer);
}

public ModularUI.Builder createTankUI(IFluidTank fluidTank, IItemHandlerModifiable containerInventory, String title, EntityPlayer entityPlayer) {
public ModularUI.Builder createTankUI(IFluidTank fluidTank, String title, EntityPlayer entityPlayer) {
Builder builder = ModularUI.defaultBuilder();
builder.image(7, 16, 81, 55, GuiTextures.DISPLAY);
TankWidget tankWidget = new TankWidget(fluidTank, 69, 52, 18, 18)
Expand All @@ -137,10 +140,10 @@ public ModularUI.Builder createTankUI(IFluidTank fluidTank, IItemHandlerModifiab
builder.dynamicLabel(11, 30, tankWidget::getFormattedFluidAmount, 0xFFFFFF);
builder.dynamicLabel(11, 40, tankWidget::getFluidLocalizedName, 0xFFFFFF);
return builder.label(6, 6, title)
.widget(new FluidContainerSlotWidget(containerInventory, 0, 90, 16, false)
.widget(new FluidContainerSlotWidget(importItems, 0, 90, 16, false)
.setBackgroundTexture(GuiTextures.SLOT, GuiTextures.IN_SLOT_OVERLAY))
.widget(new ImageWidget(91, 36, 14, 15, GuiTextures.TANK_ICON))
.widget(new SlotWidget(containerInventory, 1, 90, 53, true, false)
.widget(new SlotWidget(exportItems, 0, 90, 53, true, false)
.setBackgroundTexture(GuiTextures.SLOT, GuiTextures.OUT_SLOT_OVERLAY))
.bindPlayerInventory(entityPlayer.inventory);
}
Expand Down
Loading

0 comments on commit 149bad0

Please sign in to comment.