Fluid cleanup and performance improvements (#3581)

Use custom gui widget with custom synchronization for rendering fake fluid slots.
This commit is contained in:
fscan
2018-07-04 19:12:12 +02:00
committed by GitHub
parent 979531afb7
commit 5d26b98ab8
40 changed files with 1499 additions and 777 deletions
@@ -21,11 +21,14 @@ package appeng.fluids.client.gui;
import net.minecraft.entity.player.InventoryPlayer;
import appeng.api.implementations.IUpgradeableHost;
import appeng.client.gui.implementations.GuiUpgradeable;
import appeng.core.localization.GuiText;
import appeng.fluids.client.gui.widgets.GuiFluidSlot;
import appeng.fluids.client.gui.widgets.GuiOptionalFluidSlot;
import appeng.fluids.container.ContainerFluidIO;
import appeng.fluids.parts.PartFluidImportBus;
import appeng.fluids.parts.PartSharedFluidBus;
import appeng.fluids.util.IAEFluidTank;
/**
@@ -35,10 +38,34 @@ import appeng.fluids.parts.PartFluidImportBus;
*/
public class GuiFluidIO extends GuiUpgradeable
{
private final PartSharedFluidBus bus;
public GuiFluidIO( InventoryPlayer inventoryPlayer, IUpgradeableHost te )
public GuiFluidIO( InventoryPlayer inventoryPlayer, PartSharedFluidBus te )
{
super( new ContainerFluidIO( inventoryPlayer, te ) );
this.bus = te;
}
@Override
public void initGui()
{
super.initGui();
final ContainerFluidIO container = (ContainerFluidIO) this.inventorySlots;
final IAEFluidTank inv = this.bus.getConfig();
final int y = 40;
final int x = 80;
this.guiSlots.add( new GuiFluidSlot( inv, 0, 0, x, y ) );
this.guiSlots.add( new GuiOptionalFluidSlot( inv, container, 1, 1, 1, x, y, -1, 0 ) );
this.guiSlots.add( new GuiOptionalFluidSlot( inv, container, 2, 2, 1, x, y, 1, 0 ) );
this.guiSlots.add( new GuiOptionalFluidSlot( inv, container, 3, 3, 1, x, y, 0, -1 ) );
this.guiSlots.add( new GuiOptionalFluidSlot( inv, container, 4, 4, 1, x, y, 0, 1 ) );
this.guiSlots.add( new GuiOptionalFluidSlot( inv, container, 5, 5, 2, x, y, -1, -1 ) );
this.guiSlots.add( new GuiOptionalFluidSlot( inv, container, 6, 6, 2, x, y, 1, -1 ) );
this.guiSlots.add( new GuiOptionalFluidSlot( inv, container, 7, 7, 2, x, y, -1, 1 ) );
this.guiSlots.add( new GuiOptionalFluidSlot( inv, container, 8, 8, 2, x, y, 1, 1 ) );
}
@Override
@@ -24,20 +24,21 @@ import java.io.IOException;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.player.InventoryPlayer;
import appeng.client.gui.AEBaseGui;
import appeng.client.gui.widgets.GuiFluidTank;
import appeng.client.gui.implementations.GuiUpgradeable;
import appeng.client.gui.widgets.GuiTabButton;
import appeng.core.localization.GuiText;
import appeng.core.sync.GuiBridge;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketSwitchGuis;
import appeng.fluids.client.gui.widgets.GuiFluidSlot;
import appeng.fluids.client.gui.widgets.GuiFluidTank;
import appeng.fluids.container.ContainerFluidInterface;
import appeng.fluids.helper.AEFluidTank;
import appeng.fluids.helper.DualityFluidInterface;
import appeng.fluids.helper.IFluidInterfaceHost;
import appeng.fluids.util.IAEFluidTank;
public class GuiFluidInterface extends AEBaseGui
public class GuiFluidInterface extends GuiUpgradeable
{
public final static int ID_BUTTON_TANK = 222;
@@ -56,17 +57,26 @@ public class GuiFluidInterface extends AEBaseGui
{
super.initGui();
final IAEFluidTank configFluids = this.host.getDualityFluidInterface().getConfig();
final IAEFluidTank fluidTank = this.host.getDualityFluidInterface().getTanks();
for( int i = 0; i < DualityFluidInterface.NUMBER_OF_TANKS; ++i )
{
final AEFluidTank fluidTank = this.host.getDualityFluidInterface().getTank( i );
final GuiFluidTank guiTank = new GuiFluidTank( ID_BUTTON_TANK + i, fluidTank, this.getGuiLeft() + 35 + 18 * i, this.getGuiTop() + 53, 16, 68 );
final GuiFluidTank guiTank = new GuiFluidTank( fluidTank, i, DualityFluidInterface.NUMBER_OF_TANKS + i, this.getGuiLeft() + 35 + 18 * i, this
.getGuiTop() + 53, 16, 68 );
this.buttonList.add( guiTank );
this.guiSlots.add( new GuiFluidSlot( configFluids, i, i, 35 + 18 * i, 35 ) );
}
this.priority = new GuiTabButton( this.getGuiLeft() + 154, this.getGuiTop(), 2 + 4 * 16, GuiText.Priority.getLocal(), this.itemRender );
this.buttonList.add( this.priority );
}
@Override
protected void addButtons()
{
}
@Override
public void drawFG( int offsetX, int offsetY, int mouseX, int mouseY )
{
@@ -93,4 +103,10 @@ public class GuiFluidInterface extends AEBaseGui
NetworkHandler.instance().sendToServer( new PacketSwitchGuis( GuiBridge.GUI_PRIORITY ) );
}
}
@Override
protected boolean drawUpgrades()
{
return false;
}
}
@@ -41,8 +41,11 @@ import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketConfigButton;
import appeng.core.sync.packets.PacketSwitchGuis;
import appeng.core.sync.packets.PacketValueConfig;
import appeng.fluids.client.gui.widgets.GuiFluidSlot;
import appeng.fluids.client.gui.widgets.GuiOptionalFluidSlot;
import appeng.fluids.container.ContainerFluidStorageBus;
import appeng.fluids.parts.PartFluidStorageBus;
import appeng.fluids.util.IAEFluidTank;
/**
@@ -57,11 +60,41 @@ public class GuiFluidStorageBus extends GuiUpgradeable
private GuiTabButton priority;
private GuiImgButton partition;
private GuiImgButton clear;
private final PartFluidStorageBus bus;
public GuiFluidStorageBus( InventoryPlayer inventoryPlayer, PartFluidStorageBus te )
{
super( new ContainerFluidStorageBus( inventoryPlayer, te ) );
this.ySize = 251;
this.bus = te;
}
@Override
public void initGui()
{
super.initGui();
final int xo = 8;
final int yo = 23 + 6;
final IAEFluidTank config = this.bus.getConfig();
final ContainerFluidStorageBus container = (ContainerFluidStorageBus) this.inventorySlots;
for( int y = 0; y < 7; y++ )
{
for( int x = 0; x < 9; x++ )
{
final int idx = y * 9 + x;
if( y < 2 )
{
this.guiSlots.add( new GuiFluidSlot( config, idx, idx, xo + x * 18, yo + y * 18 ) );
}
else
{
this.guiSlots.add( new GuiOptionalFluidSlot( config, container, idx, idx, y - 2, xo, yo, x, y ) );
}
}
}
}
@Override
@@ -73,7 +73,6 @@ public class GuiFluidTerminal extends AEBaseMEGui implements ISortSource, IConfi
private final ContainerFluidTerminal container;
private final int offsetX = 9;
private int rows = 6;
private int maxRows = Integer.MAX_VALUE;
private int perRow = 9;
protected PartFluidTerminal terminal;
@@ -172,7 +171,7 @@ public class GuiFluidTerminal extends AEBaseMEGui implements ISortSource, IConfi
{
final IMEFluidSlot fluidSlot = (IMEFluidSlot) slot;
if( fluidSlot.getFluidStack() != null && fluidSlot.shouldRenderAsFluid() )
if( fluidSlot.getAEFluidStack() != null && fluidSlot.shouldRenderAsFluid() )
{
final IAEFluidStack fluidStack = fluidSlot.getAEFluidStack();
final String formattedAmount = NumberFormat.getNumberInstance( Locale.US ).format( fluidStack.getStackSize() ) + " mB";
@@ -0,0 +1,112 @@
package appeng.fluids.client.gui.widgets;
import java.util.Collections;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import appeng.api.storage.data.IAEFluidStack;
import appeng.client.gui.widgets.GuiCustomSlot;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketFluidSlot;
import appeng.fluids.util.AEFluidStack;
import appeng.fluids.util.IAEFluidTank;
public class GuiFluidSlot extends GuiCustomSlot
{
private final IAEFluidTank fluids;
private final int slot;
public GuiFluidSlot( final IAEFluidTank fluids, final int slot, final int id, final int x, final int y )
{
super( id, x, y );
this.fluids = fluids;
this.slot = slot;
}
@Override
public void drawContent( final Minecraft mc, final int mouseX, final int mouseY, final float partialTicks )
{
final IAEFluidStack fs = this.getFluidStack();
if( fs != null )
{
GlStateManager.disableLighting();
GlStateManager.disableBlend();
final Fluid fluid = fs.getFluid();
mc.getTextureManager().bindTexture( TextureMap.LOCATION_BLOCKS_TEXTURE );
final TextureAtlasSprite sprite = mc.getTextureMapBlocks().getAtlasSprite( fluid.getStill().toString() );
// Set color for dynamic fluids
// Convert int color to RGB
final float red = ( fluid.getColor() >> 16 & 255 ) / 255.0F;
final float green = ( fluid.getColor() >> 8 & 255 ) / 255.0F;
final float blue = ( fluid.getColor() & 255 ) / 255.0F;
GlStateManager.color( red, green, blue );
this.drawTexturedModalRect( this.xPos(), this.yPos(), sprite, this.getWidth(), this.getHeight() );
}
}
@Override
public boolean canClick( final EntityPlayer player )
{
final ItemStack mouseStack = player.inventory.getItemStack();
return mouseStack.isEmpty() || mouseStack.hasCapability( CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null );
}
@Override
public void slotClicked( final ItemStack clickStack, int mouseButton )
{
if( clickStack.isEmpty() || mouseButton == 1 )
{
this.setFluidStack( null );
}
else if( mouseButton == 0 )
{
final FluidStack fluid = FluidUtil.getFluidContained( clickStack );
if( fluid != null )
{
this.setFluidStack( AEFluidStack.fromFluidStack( fluid ) );
}
}
}
@Override
public String getMessage()
{
final IAEFluidStack fluid = getFluidStack();
if( fluid != null )
{
return fluid.getFluidStack().getLocalizedName();
}
return null;
}
@Override
public boolean isVisible()
{
return true;
}
public IAEFluidStack getFluidStack()
{
return this.fluids.getFluidInSlot( this.slot );
}
public void setFluidStack( final IAEFluidStack stack )
{
this.fluids.setFluidInSlot( this.slot, stack );
NetworkHandler.instance().sendToServer( new PacketFluidSlot( Collections.singletonMap( this.getId(), this.getFluidStack() ) ) );
}
}
@@ -0,0 +1,130 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2018, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.fluids.client.gui.widgets;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.util.AEColor;
import appeng.client.gui.widgets.ITooltip;
import appeng.fluids.util.IAEFluidTank;
@SideOnly( Side.CLIENT )
public class GuiFluidTank extends GuiButton implements ITooltip
{
private final IAEFluidTank tank;
private final int slot;
public GuiFluidTank( IAEFluidTank tank, int slot, int id, int x, int y, int w, int h )
{
super( id, x, y, w, h, "" );
this.tank = tank;
this.slot = slot;
}
@Override
public void drawButton( final Minecraft mc, final int mouseX, final int mouseY, final float partialTicks )
{
if( this.visible )
{
GlStateManager.disableBlend();
GlStateManager.disableLighting();
drawRect( this.x, this.y, this.x + this.width, this.y + this.height, AEColor.GRAY.blackVariant | 0xFF000000 );
final IAEFluidStack fluid = this.tank.getFluidInSlot( this.slot );
if( fluid != null && fluid.getStackSize() > 0 )
{
mc.getTextureManager().bindTexture( TextureMap.LOCATION_BLOCKS_TEXTURE );
float red = ( fluid.getFluid().getColor() >> 16 & 255 ) / 255.0F;
float green = ( fluid.getFluid().getColor() >> 8 & 255 ) / 255.0F;
float blue = ( fluid.getFluid().getColor() & 255 ) / 255.0F;
GlStateManager.color( red, green, blue );
TextureAtlasSprite sprite = mc.getTextureMapBlocks().getAtlasSprite( fluid.getFluid().getStill().toString() );
final int scaledHeight = (int) ( this.height * ( (float) fluid.getStackSize() / this.tank.getTankProperties()[this.slot].getCapacity() ) );
int iconHeightRemainder = scaledHeight % 16;
if( iconHeightRemainder > 0 )
{
drawTexturedModalRect( this.x, this.y + this.height - iconHeightRemainder, sprite, 16, iconHeightRemainder );
}
for( int i = 0; i < scaledHeight / 16; i++ )
{
drawTexturedModalRect( this.x, this.y + this.height - iconHeightRemainder - ( i + 1 ) * 16, sprite, 16, 16 );
}
}
}
}
@Override
public String getMessage()
{
final IAEFluidStack fluid = this.tank.getFluidInSlot( this.slot );
if( fluid != null && fluid.getStackSize() > 0 )
{
String desc = fluid.getFluid().getLocalizedName( fluid.getFluidStack() );
String amountToText = fluid.getStackSize() + "mB";
return desc + "\n" + amountToText;
}
return null;
}
@Override
public int xPos()
{
return this.x - 2;
}
@Override
public int yPos()
{
return this.y - 2;
}
@Override
public int getWidth()
{
return this.width + 4;
}
@Override
public int getHeight()
{
return this.height + 4;
}
@Override
public boolean isVisible()
{
return true;
}
}
@@ -0,0 +1,63 @@
package appeng.fluids.client.gui.widgets;
import net.minecraft.client.renderer.GlStateManager;
import appeng.api.storage.data.IAEFluidStack;
import appeng.container.slot.IOptionalSlotHost;
import appeng.fluids.util.IAEFluidTank;
public class GuiOptionalFluidSlot extends GuiFluidSlot
{
private final IOptionalSlotHost containerBus;
private final int groupNum;
private final int srcX;
private final int srcY;
public GuiOptionalFluidSlot( IAEFluidTank fluids, final IOptionalSlotHost containerBus, int slot, int id, int groupNum, int x, int y, int xoffs, int yoffs )
{
super( fluids, slot, id, x + xoffs * 18, y + yoffs * 18 );
this.containerBus = containerBus;
this.groupNum = groupNum;
this.srcX = x;
this.srcY = y;
}
@Override
public boolean isSlotEnabled()
{
if( this.containerBus == null )
{
return false;
}
return this.containerBus.isSlotEnabled( this.groupNum );
}
@Override
public IAEFluidStack getFluidStack()
{
if( !this.isSlotEnabled() && super.getFluidStack() != null )
{
this.setFluidStack( null );
}
return super.getFluidStack();
}
@Override
public void drawBackground( int guileft, int guitop )
{
GlStateManager.enableBlend();
if( this.isSlotEnabled() )
{
GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
}
else
{
GlStateManager.color( 1.0F, 1.0F, 1.0F, 0.4F );
}
this.drawTexturedModalRect( guileft + this.xPos() - 1, guitop + this.yPos() - 1, this.srcX - 1, this.srcY - 1, this.getWidth() + 2,
this.getHeight() + 2 );
}
}
@@ -0,0 +1,117 @@
package appeng.fluids.container;
import java.util.Collections;
import java.util.Map;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IContainerListener;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import appeng.api.config.Upgrades;
import appeng.api.implementations.IUpgradeableHost;
import appeng.api.storage.data.IAEFluidStack;
import appeng.container.implementations.ContainerUpgradeable;
import appeng.fluids.helper.FluidSyncHelper;
import appeng.fluids.util.AEFluidStack;
import appeng.fluids.util.IAEFluidTank;
import appeng.util.Platform;
public abstract class ContainerFluidConfigurable extends ContainerUpgradeable implements IFluidSyncContainer
{
private FluidSyncHelper sync = null;
public ContainerFluidConfigurable( InventoryPlayer ip, IUpgradeableHost te )
{
super( ip, te );
}
public abstract IAEFluidTank getFluidConfigInventory();
private FluidSyncHelper getSynchHelper()
{
if( this.sync == null )
{
this.sync = new FluidSyncHelper( getFluidConfigInventory(), 0 );
}
return this.sync;
}
@Override
protected ItemStack transferStackToContainer( ItemStack input )
{
FluidStack fs = FluidUtil.getFluidContained( input );
if( fs != null )
{
final IAEFluidTank t = getFluidConfigInventory();
final IAEFluidStack stack = AEFluidStack.fromFluidStack( fs );
for( int i = 0; i < t.getSlots(); ++i )
{
if( t.getFluidInSlot( i ) == null && this.isValidForConfig( i, stack ) )
{
t.setFluidInSlot( i, stack );
break;
}
}
}
return input;
}
protected boolean isValidForConfig( int slot, IAEFluidStack fs )
{
if( this.supportCapacity() )
{
// assumes 4 slots per upgrade
final int upgrades = this.getUpgradeable().getInstalledUpgrades( Upgrades.CAPACITY );
if( slot > 0 && upgrades < 1 )
{
return false;
}
if( slot > 4 && upgrades < 2 )
{
return false;
}
}
return true;
}
@Override
protected void standardDetectAndSendChanges()
{
if( Platform.isServer() )
{
this.getSynchHelper().sendDiff( this.listeners );
// clear out config items that are no longer valid (eg capacity upgrade removed)
final IAEFluidTank t = getFluidConfigInventory();
for( int i = 0; i < t.getSlots(); ++i )
{
if( t.getFluidInSlot( i ) != null && !this.isValidForConfig( i, t.getFluidInSlot( i ) ) )
{
t.setFluidInSlot( i, null );
}
}
}
super.standardDetectAndSendChanges();
}
@Override
public void addListener( IContainerListener listener )
{
super.addListener( listener );
this.getSynchHelper().sendFull( Collections.singleton( listener ) );
}
@Override
public void receiveFluidSlots( Map<Integer, IAEFluidStack> fluids )
{
this.getSynchHelper().readPacket( fluids );
}
}
@@ -20,15 +20,9 @@ package appeng.fluids.container;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.items.IItemHandler;
import appeng.api.implementations.IUpgradeableHost;
import appeng.container.implementations.ContainerUpgradeable;
import appeng.fluids.container.slots.OptionalSlotFakeFluid;
import appeng.fluids.container.slots.SlotFakeFluid;
import appeng.fluids.parts.PartSharedFluidBus;
import appeng.fluids.util.IAEFluidTank;
/**
@@ -36,40 +30,25 @@ import appeng.fluids.container.slots.SlotFakeFluid;
* @version rv5 - 1/05/2018
* @since rv5 1/05/2018
*/
public class ContainerFluidIO extends ContainerUpgradeable
public class ContainerFluidIO extends ContainerFluidConfigurable
{
public ContainerFluidIO( InventoryPlayer ip, IUpgradeableHost te )
private final PartSharedFluidBus bus;
public ContainerFluidIO( InventoryPlayer ip, PartSharedFluidBus te )
{
super( ip, te );
this.bus = te;
}
@Override
public IAEFluidTank getFluidConfigInventory()
{
return bus.getConfig();
}
@Override
protected void setupConfig()
{
this.setupUpgrades();
final IItemHandler inv = this.getUpgradeable().getInventoryByName( "config" );
final int y = 40;
final int x = 80;
this.addSlotToContainer( new SlotFakeFluid( inv, 0, x, y ) );
if( this.supportCapacity() )
{
this.addSlotToContainer( new OptionalSlotFakeFluid( inv, this, 1, x, y, -1, 0, 1 ) );
this.addSlotToContainer( new OptionalSlotFakeFluid( inv, this, 2, x, y, 1, 0, 1 ) );
this.addSlotToContainer( new OptionalSlotFakeFluid( inv, this, 3, x, y, 0, -1, 1 ) );
this.addSlotToContainer( new OptionalSlotFakeFluid( inv, this, 4, x, y, 0, 1, 1 ) );
this.addSlotToContainer( new OptionalSlotFakeFluid( inv, this, 5, x, y, -1, -1, 2 ) );
this.addSlotToContainer( new OptionalSlotFakeFluid( inv, this, 6, x, y, 1, -1, 2 ) );
this.addSlotToContainer( new OptionalSlotFakeFluid( inv, this, 7, x, y, -1, 1, 2 ) );
this.addSlotToContainer( new OptionalSlotFakeFluid( inv, this, 8, x, y, 1, 1, 2 ) );
}
}
@Override
public boolean isValidForSlot( Slot s, ItemStack i )
{
return s instanceof SlotFakeFluid ? i.hasCapability( CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null ) : super.isValidForSlot( s, i );
}
}
@@ -19,45 +19,45 @@
package appeng.fluids.container;
import java.util.HashMap;
import java.util.Collections;
import java.util.Map;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IContainerListener;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import appeng.api.config.SecurityPermissions;
import appeng.api.parts.IPart;
import appeng.container.AEBaseContainer;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketFluidTank;
import appeng.fluids.container.slots.SlotFakeFluid;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.util.IConfigManager;
import appeng.fluids.helper.DualityFluidInterface;
import appeng.fluids.helper.FluidSyncHelper;
import appeng.fluids.helper.IFluidInterfaceHost;
import appeng.fluids.util.IAEFluidTank;
import appeng.util.Platform;
public class ContainerFluidInterface extends AEBaseContainer
public class ContainerFluidInterface extends ContainerFluidConfigurable
{
private final DualityFluidInterface myDuality;
private final FluidSyncHelper tankSync;
public ContainerFluidInterface( final InventoryPlayer ip, final IFluidInterfaceHost te )
{
super( ip, (TileEntity) ( te instanceof TileEntity ? te : null ), (IPart) ( te instanceof IPart ? te : null ) );
super( ip, te.getDualityFluidInterface().getHost() );
this.myDuality = te.getDualityFluidInterface();
this.tankSync = new FluidSyncHelper( this.myDuality.getTanks(), DualityFluidInterface.NUMBER_OF_TANKS );
}
for( int x = 0; x < DualityFluidInterface.NUMBER_OF_TANKS; x++ )
{
this.addSlotToContainer( new SlotFakeFluid( this.myDuality.getConfig(), x, 35 + 18 * x, 35 ) );
}
@Override
protected int getHeight()
{
return 231;
}
this.bindPlayerInventory( ip, 0, 231 - /* height of player inventory */82 );
@Override
public IAEFluidTank getFluidConfigInventory()
{
return this.myDuality.getConfig();
}
@Override
@@ -67,56 +67,49 @@ public class ContainerFluidInterface extends AEBaseContainer
if( Platform.isServer() )
{
this.sendTankUpdate();
this.tankSync.sendDiff( this.listeners );
}
super.detectAndSendChanges();
}
@Override
public void addListener( IContainerListener listener )
protected void setupConfig()
{
super.addListener( listener );
this.sendTankInfo( listener );
}
private void sendTankUpdate( final IContainerListener l, final HashMap<Integer, NBTTagCompound> updateMap )
{
if( l instanceof EntityPlayerMP )
{
NetworkHandler.instance().sendTo( new PacketFluidTank( updateMap ), (EntityPlayerMP) l );
}
}
private void sendTankUpdate()
{
final HashMap<Integer, NBTTagCompound> updateMap = new HashMap<>();
if( this.myDuality.writeTankInfo( updateMap, false ) )
{
for( final IContainerListener listener : this.listeners )
{
this.sendTankUpdate( listener, updateMap );
}
}
}
private void sendTankInfo( final IContainerListener l )
{
final HashMap<Integer, NBTTagCompound> updateMap = new HashMap<>();
if( this.myDuality.writeTankInfo( updateMap, true ) )
{
this.sendTankUpdate( l, updateMap );
}
}
public void receiveTankInfo( final Map<Integer, NBTTagCompound> tankTags )
{
this.myDuality.readTankInfo( tankTags );
}
@Override
public boolean isValidForSlot( Slot s, ItemStack i )
protected void loadSettingsFromHost( final IConfigManager cm )
{
return s instanceof SlotFakeFluid ? i.hasCapability( CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null ) : super.isValidForSlot( s, i );
}
@Override
public void addListener( IContainerListener listener )
{
super.addListener( listener );
this.tankSync.sendFull( Collections.singleton( listener ) );
}
@Override
public void receiveFluidSlots( Map<Integer, IAEFluidStack> fluids )
{
super.receiveFluidSlots( fluids );
this.tankSync.readPacket( fluids );
}
protected boolean supportCapacity()
{
return false;
}
public int availableUpgrades()
{
return 0;
}
@Override
public boolean hasToolbox()
{
return false;
}
}
@@ -22,7 +22,6 @@ package appeng.fluids.container;
import java.util.Iterator;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import appeng.api.AEApi;
@@ -37,13 +36,10 @@ import appeng.api.storage.channels.IFluidStorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IItemList;
import appeng.container.guisync.GuiSync;
import appeng.container.implementations.ContainerUpgradeable;
import appeng.container.slot.SlotRestrictedInput;
import appeng.fluids.container.slots.OptionalSlotFakeFluid;
import appeng.fluids.container.slots.SlotFakeFluid;
import appeng.fluids.parts.PartFluidStorageBus;
import appeng.fluids.util.IAEFluidTank;
import appeng.util.Platform;
import appeng.util.helpers.ItemHandlerUtil;
import appeng.util.iterators.NullIterator;
@@ -52,7 +48,7 @@ import appeng.util.iterators.NullIterator;
* @version rv6 - 22/05/2018
* @since rv6 22/05/2018
*/
public class ContainerFluidStorageBus extends ContainerUpgradeable
public class ContainerFluidStorageBus extends ContainerFluidConfigurable
{
private final PartFluidStorageBus storageBus;
@@ -78,31 +74,39 @@ public class ContainerFluidStorageBus extends ContainerUpgradeable
@Override
protected void setupConfig()
{
final int xo = 8;
final int yo = 23 + 6;
final IItemHandler upgrades = this.getUpgradeable().getInventoryByName( "upgrades" );
this.addSlotToContainer( ( new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.UPGRADES, upgrades, 0, 187, 8, this.getInventoryPlayer() ) )
.setNotDraggable() );
this.addSlotToContainer(
( new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.UPGRADES, upgrades, 1, 187, 8 + 18, this.getInventoryPlayer() ) )
.setNotDraggable() );
this.addSlotToContainer(
( new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.UPGRADES, upgrades, 2, 187, 8 + 18 * 2, this.getInventoryPlayer() ) )
.setNotDraggable() );
this.addSlotToContainer(
( new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.UPGRADES, upgrades, 3, 187, 8 + 18 * 3, this.getInventoryPlayer() ) )
.setNotDraggable() );
this.addSlotToContainer(
( new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.UPGRADES, upgrades, 4, 187, 8 + 18 * 4, this.getInventoryPlayer() ) )
.setNotDraggable() );
}
final IItemHandler config = this.getUpgradeable().getInventoryByName( "config" );
for( int y = 0; y < 7; y++ )
@Override
protected boolean isValidForConfig( int slot, IAEFluidStack fs )
{
if( this.supportCapacity() )
{
for( int x = 0; x < 9; x++ )
final int upgrades = this.getUpgradeable().getInstalledUpgrades( Upgrades.CAPACITY );
final int y = slot / 9;
if( y >= upgrades + 2 )
{
if( y < 2 )
{
this.addSlotToContainer( new SlotFakeFluid( config, y * 9 + x, xo + x * 18, yo + y * 18 ) );
}
else
{
this.addSlotToContainer( new OptionalSlotFakeFluid( config, this, y * 9 + x, xo, yo, x, y, y - 2 ) );
}
return false;
}
}
final IItemHandler upgrades = this.getUpgradeable().getInventoryByName( "upgrades" );
this.addSlotToContainer( ( new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.UPGRADES, upgrades, 0, 187, 8, this.getInventoryPlayer() ) ).setNotDraggable() );
this.addSlotToContainer( ( new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.UPGRADES, upgrades, 1, 187, 8 + 18, this.getInventoryPlayer() ) ).setNotDraggable() );
this.addSlotToContainer( ( new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.UPGRADES, upgrades, 2, 187, 8 + 18 * 2, this.getInventoryPlayer() ) ).setNotDraggable() );
this.addSlotToContainer( ( new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.UPGRADES, upgrades, 3, 187, 8 + 18 * 3, this.getInventoryPlayer() ) ).setNotDraggable() );
this.addSlotToContainer( ( new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.UPGRADES, upgrades, 4, 187, 8 + 18 * 4, this.getInventoryPlayer() ) ).setNotDraggable() );
return true;
}
@Override
@@ -142,36 +146,39 @@ public class ContainerFluidStorageBus extends ContainerUpgradeable
public void clear()
{
ItemHandlerUtil.clear( this.getUpgradeable().getInventoryByName( "config" ) );
IAEFluidTank h = this.storageBus.getConfig();
for( int i = 0; i < h.getSlots(); ++i )
{
h.setFluidInSlot( i, null );
}
this.detectAndSendChanges();
}
public void partition()
{
final IItemHandler inv = this.getUpgradeable().getInventoryByName( "config" );
IAEFluidTank h = this.storageBus.getConfig();
final IMEInventory<IAEFluidStack> cellInv = this.storageBus.getInternalHandler();
Iterator<IAEFluidStack> i = new NullIterator<>();
if( cellInv != null )
{
final IItemList<IAEFluidStack> list = cellInv.getAvailableItems( AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ).createList() );
final IItemList<IAEFluidStack> list = cellInv
.getAvailableItems( AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ).createList() );
i = list.iterator();
}
for( int x = 0; x < inv.getSlots(); x++ )
for( int x = 0; x < h.getSlots(); x++ )
{
if( i.hasNext() && this.isSlotEnabled( ( x / 9 ) - 2 ) )
{
final ItemStack g = i.next().asItemStackRepresentation();
ItemHandlerUtil.setStackInSlot( inv, x, g );
h.setFluidInSlot( x, i.next() );
}
else
{
ItemHandlerUtil.setStackInSlot( inv, x, ItemStack.EMPTY );
h.setFluidInSlot( x, null );
}
}
this.detectAndSendChanges();
}
@@ -194,4 +201,10 @@ public class ContainerFluidStorageBus extends ContainerUpgradeable
{
this.storageFilter = storageFilter;
}
@Override
public IAEFluidTank getFluidConfigInventory()
{
return this.storageBus.getConfig();
}
}
@@ -0,0 +1,13 @@
package appeng.fluids.container;
import java.util.Map;
import appeng.api.storage.data.IAEFluidStack;
public interface IFluidSyncContainer
{
void receiveFluidSlots( final Map<Integer, IAEFluidStack> fluids );
}
@@ -1,38 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2018, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.fluids.container.slots;
import net.minecraftforge.fluids.FluidStack;
/**
* @author BrockWS
* @version rv6 - 3/05/2018
* @since rv6 3/05/2018
*/
public interface IFluidSlot
{
FluidStack getFluidStack();
default boolean shouldRenderAsFluid()
{
return true;
}
}
@@ -27,7 +27,12 @@ import appeng.api.storage.data.IAEFluidStack;
* @version rv6
* @since rv6
*/
public interface IMEFluidSlot extends IFluidSlot
public interface IMEFluidSlot
{
IAEFluidStack getAEFluidStack();
default boolean shouldRenderAsFluid()
{
return true;
}
}
@@ -1,99 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2018, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.fluids.container.slots;
import javax.annotation.Nonnull;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import appeng.container.slot.IOptionalSlot;
import appeng.container.slot.IOptionalSlotHost;
/**
* @author BrockWS
* @version rv6 - 1/05/2018
* @since rv6 1/05/2018
*/
public class OptionalSlotFakeFluid extends SlotFakeFluid implements IOptionalSlot
{
private final int srcX;
private final int srcY;
private final int groupNum;
private final IOptionalSlotHost host;
private boolean renderDisabled = true;
public OptionalSlotFakeFluid( final IItemHandler inv, final IOptionalSlotHost containerBus, final int idx, final int x, final int y, final int offX, final int offY, final int groupNum )
{
super( inv, idx, x + offX * 18, y + offY * 18 );
this.srcX = x;
this.srcY = y;
this.groupNum = groupNum;
this.host = containerBus;
}
@Override
@Nonnull
public ItemStack getStack()
{
if( !this.isSlotEnabled() )
{
if( !this.getDisplayStack().isEmpty() )
{
this.clearStack();
}
}
return super.getStack();
}
@Override
public boolean isSlotEnabled()
{
if( this.host == null )
{
return false;
}
return this.host.isSlotEnabled( this.groupNum );
}
public boolean isRenderDisabled()
{
return this.renderDisabled;
}
public void setRenderDisabled( final boolean renderDisabled )
{
this.renderDisabled = renderDisabled;
}
public int getSourceX()
{
return this.srcX;
}
public int getSourceY()
{
return this.srcY;
}
}
@@ -1,79 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2018, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.fluids.container.slots;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import net.minecraftforge.items.IItemHandler;
import appeng.container.slot.SlotFake;
/**
* @author BrockWS
* @version rv6 - 1/05/2018
* @since rv6 1/05/2018
*/
public class SlotFakeFluid extends SlotFake implements IFluidSlot
{
public SlotFakeFluid( IItemHandler inv, int idx, int x, int y )
{
super( inv, idx, x, y );
this.setIIcon( 12 * 16 + 15 );
}
@Override
public void putStack( ItemStack is )
{
if( this.isItemValid( is ) || is.isEmpty() )
{
super.putStack( is );
}
}
@Override
public boolean isItemValid( ItemStack stack )
{
return stack.hasCapability( CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null ) && this.getContainer().isValidForSlot( this, stack );
}
@Override
public boolean renderIconWithItem()
{
return true;
}
@Override
public FluidStack getFluidStack()
{
if( !this.getStack().isEmpty() )
{
IFluidHandlerItem fh = this.getStack().getCapability( CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null );
if( fh == null )
{
throw new NullPointerException( "Item did not give IFluidHandlerItem: " + this.getStack().getDisplayName() );
}
return fh.drain( Integer.MAX_VALUE, false );
}
return null;
}
}
@@ -19,25 +19,21 @@
package appeng.fluids.helper;
import java.util.Map;
import java.util.Optional;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import net.minecraftforge.fluids.capability.templates.FluidHandlerConcatenate;
import net.minecraftforge.items.IItemHandler;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.Upgrades;
import appeng.api.implementations.IUpgradeableHost;
import appeng.api.networking.GridFlags;
import appeng.api.networking.IGridNode;
import appeng.api.networking.energy.IEnergySource;
@@ -59,8 +55,12 @@ import appeng.api.storage.data.IAEStack;
import appeng.api.util.AECableType;
import appeng.api.util.AEPartLocation;
import appeng.api.util.DimensionalCoord;
import appeng.api.util.IConfigManager;
import appeng.capabilities.Capabilities;
import appeng.core.settings.TickRates;
import appeng.fluids.util.AEFluidInventory;
import appeng.fluids.util.IAEFluidInventory;
import appeng.fluids.util.IAEFluidTank;
import appeng.helpers.IPriorityHost;
import appeng.me.GridAccessException;
import appeng.me.helpers.AENetworkProxy;
@@ -68,29 +68,26 @@ import appeng.me.helpers.MachineSource;
import appeng.me.storage.MEMonitorIFluidHandler;
import appeng.me.storage.MEMonitorPassThrough;
import appeng.me.storage.NullInventory;
import appeng.tile.inventory.AppEngInternalAEInventory;
import appeng.util.ConfigManager;
import appeng.util.IConfigManagerHost;
import appeng.util.Platform;
import appeng.util.inv.IAEAppEngInventory;
import appeng.util.inv.InvOperation;
public class DualityFluidInterface implements IGridTickable, IStorageMonitorable, IAEFluidInventory, IAEAppEngInventory, IPriorityHost
public class DualityFluidInterface implements IGridTickable, IStorageMonitorable, IAEFluidInventory, IPriorityHost, IUpgradeableHost, IConfigManagerHost
{
public static final int NUMBER_OF_TANKS = 6;
public static final int TANK_CAPACITY = Fluid.BUCKET_VOLUME * 4;
private final ConfigManager cm = new ConfigManager( this );
private final AENetworkProxy gridProxy;
private final IFluidInterfaceHost iHost;
private final IActionSource mySource;
private final IActionSource interfaceRequestSource;
private boolean hasConfig = false;
private final IStorageMonitorableAccessor accessor = this::getMonitorable;
private final AEFluidTank[] tanks;
private final IFluidHandler storage;
private final AppEngInternalAEInventory config = new AppEngInternalAEInventory( this, NUMBER_OF_TANKS );
private final IAEFluidStack[] configStacks;
private final AEFluidInventory tanks = new AEFluidInventory( this, NUMBER_OF_TANKS, TANK_CAPACITY );
private final AEFluidInventory config = new AEFluidInventory( this, NUMBER_OF_TANKS );
private final IAEFluidStack[] requireWork;
private final boolean[] tankChanged;
private int isWorking = -1;
private int priority;
@@ -113,18 +110,24 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
this.fluids.setChangeSource( this.mySource );
this.items.setChangeSource( this.mySource );
this.tanks = new AEFluidTank[NUMBER_OF_TANKS];
this.requireWork = new IAEFluidStack[NUMBER_OF_TANKS];
this.configStacks = new IAEFluidStack[NUMBER_OF_TANKS];
this.tankChanged = new boolean[NUMBER_OF_TANKS];
for( int i = 0; i < NUMBER_OF_TANKS; ++i )
{
this.tanks[i] = new AEFluidTank( this, TANK_CAPACITY );
this.requireWork[i] = null;
this.configStacks[i] = null;
this.tankChanged[i] = false;
}
this.storage = new FluidHandlerConcatenate( this.tanks );
}
public IUpgradeableHost getHost()
{
if( this.iHost instanceof IUpgradeableHost )
{
return (IUpgradeableHost) this.iHost;
}
if( this.iHost instanceof IUpgradeableHost )
{
return (IUpgradeableHost) this.iHost;
}
return null;
}
@Override
@@ -237,7 +240,7 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
{
if( capabilityClass == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY )
{
return (T) this.storage;
return (T) this.tanks;
}
else if( capabilityClass == Capabilities.STORAGE_MONITORABLE_ACCESSOR )
{
@@ -257,20 +260,6 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
for( int x = 0; x < NUMBER_OF_TANKS; x++ )
{
this.configStacks[x] = null;
ItemStack is = this.config.getStackInSlot( x );
if( !is.isEmpty() )
{
IFluidHandlerItem fh = is.getCapability( CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null );
if( fh != null )
{
FluidStack fs = fh.drain( Fluid.BUCKET_VOLUME, false );
if( fs != null && fs.amount > 0 )
{
this.configStacks[x] = AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ).createStack( fs );
}
}
}
this.updatePlan( x );
}
@@ -324,49 +313,38 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
return false;
}
private int getTankSlot( final IFluidHandler inventory )
{
for( int i = 0; i < NUMBER_OF_TANKS; ++i )
{
if( this.tanks[i] == inventory )
{
return i;
}
}
throw new IndexOutOfBoundsException();
}
private void updatePlan( final int slot )
{
final IAEFluidStack req = this.configStacks[slot];
final FluidStack stored = this.tanks[slot].drain( TANK_CAPACITY, false );
final IAEFluidStack req = this.config.getFluidInSlot( slot );
final IAEFluidStack stored = this.tanks.getFluidInSlot( slot );
if( req == null && ( stored != null && stored.amount > 0 ) )
if( req == null && ( stored != null && stored.getStackSize() > 0 ) )
{
final IAEFluidStack work = AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ).createStack( stored );
final IAEFluidStack work = stored.copy();
this.requireWork[slot] = work.setStackSize( -work.getStackSize() );
return;
}
else if( req != null )
{
if( stored == null || stored.amount == 0 ) // need to add stuff!
if( stored == null || stored.getStackSize() == 0 ) // need to add stuff!
{
this.requireWork[slot] = req.copy();
this.requireWork[slot].setStackSize( TANK_CAPACITY );
return;
}
else if( req.getFluid().equals( stored.getFluid() ) ) // same type ( qty different? )!
else if( req.equals( stored ) ) // same type ( qty different? )!
{
if( stored.amount < TANK_CAPACITY )
if( stored.getStackSize() < TANK_CAPACITY )
{
this.requireWork[slot] = req.copy();
this.requireWork[slot].setStackSize( TANK_CAPACITY - stored.amount );
this.requireWork[slot].setStackSize( TANK_CAPACITY - stored.getStackSize() );
return;
}
}
else
// Stored != null; dispose!
{
final IAEFluidStack work = AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ).createStack( stored );
final IAEFluidStack work = stored.copy();
this.requireWork[slot] = work.setStackSize( -work.getStackSize() );
return;
}
@@ -377,7 +355,6 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
private boolean usePlan( final int slot )
{
IFluidHandler tank = this.tanks[slot];
IAEFluidStack work = this.requireWork[slot];
this.isWorking = slot;
@@ -391,7 +368,7 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
if( work.getStackSize() > 0 )
{
// make sure strange things didn't happen...
if( tank.fill( work.getFluidStack(), false ) != work.getStackSize() )
if( this.tanks.fill( slot, work.getFluidStack(), false ) != work.getStackSize() )
{
changed = true;
}
@@ -401,7 +378,7 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
if( acquired != null )
{
changed = true;
final int filled = tank.fill( acquired.getFluidStack(), true );
final int filled = this.tanks.fill( slot, acquired.getFluidStack(), true );
if( filled != acquired.getStackSize() )
{
throw new IllegalStateException( "bad attempt at managing tanks. ( fill )" );
@@ -415,7 +392,7 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
toStore.setStackSize( -toStore.getStackSize() );
// make sure strange things didn't happen...
final FluidStack canExtract = tank.drain( toStore.getFluidStack(), false );
final FluidStack canExtract = this.tanks.drain( slot, toStore.getFluidStack(), false );
if( canExtract == null || canExtract.amount != toStore.getStackSize() )
{
changed = true;
@@ -429,7 +406,7 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
{
// extract items!
changed = true;
final FluidStack removed = tank.drain( toStore.getFluidStack(), true );
final FluidStack removed = this.tanks.drain( slot, toStore.getFluidStack(), true );
if( removed == null || toStore.getStackSize() != removed.amount )
{
throw new IllegalStateException( "bad attempt at managing tanks. ( drain )" );
@@ -453,56 +430,46 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
}
@Override
public void onFluidInventoryChanged( final IFluidHandler inventory )
{
final int slot = this.getTankSlot( inventory );
this.tankChanged[slot] = true;
this.saveChanges();
if( this.isWorking == slot )
{
return;
}
final boolean had = this.hasWorkToDo();
this.updatePlan( slot );
final boolean now = this.hasWorkToDo();
if( had != now )
{
try
{
if( now )
{
this.gridProxy.getTick().alertDevice( this.gridProxy.getNode() );
}
else
{
this.gridProxy.getTick().sleepDevice( this.gridProxy.getNode() );
}
}
catch( final GridAccessException e )
{
// :P
}
}
}
@Override
public void onChangeInventory( IItemHandler inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack )
public void onFluidInventoryChanged( final IAEFluidTank inventory, final int slot )
{
if( this.isWorking == slot )
{
return;
}
if( inv == this.config )
if( inventory == this.config )
{
this.readConfig();
}
else if( inventory == this.tanks )
{
this.saveChanges();
final boolean had = this.hasWorkToDo();
this.updatePlan( slot );
final boolean now = this.hasWorkToDo();
if( had != now )
{
try
{
if( now )
{
this.gridProxy.getTick().alertDevice( this.gridProxy.getNode() );
}
else
{
this.gridProxy.getTick().sleepDevice( this.gridProxy.getNode() );
}
}
catch( final GridAccessException e )
{
// :P
}
}
}
}
@Override
@@ -519,69 +486,27 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
public void writeToNBT( final NBTTagCompound data )
{
final NBTTagList tankContents = new NBTTagList();
for( int i = 0; i < NUMBER_OF_TANKS; ++i )
{
tankContents.appendTag( this.tanks[i].writeToNBT( new NBTTagCompound() ) );
}
data.setInteger( "priority", this.priority );
data.setTag( "storage", tankContents );
this.tanks.writeToNBT( data, "storage" );
this.config.writeToNBT( data, "config" );
}
public void readFromNBT( final NBTTagCompound data )
{
this.config.readFromNBT( data, "config" );
final NBTTagList tankContents = data.getTagList( "storage", 10 );
if( tankContents != null )
{
for( int i = 0; i < Math.min( NUMBER_OF_TANKS, tankContents.tagCount() ); ++i )
{
this.tanks[i].readFromNBT( tankContents.getCompoundTagAt( i ) );
}
}
this.tanks.readFromNBT( data, "storage" );
this.priority = data.getInteger( "priority" );
this.readConfig();
}
public IItemHandler getConfig()
public IAEFluidTank getConfig()
{
return this.config;
}
public AEFluidTank getTank( final int i )
public IAEFluidTank getTanks()
{
return this.tanks[i];
}
public boolean writeTankInfo( final Map<Integer, NBTTagCompound> tagMap, boolean all )
{
boolean empty = true;
for( int i = 0; i < NUMBER_OF_TANKS; ++i )
{
if( all || this.tankChanged[i] )
{
tagMap.put( i, this.tanks[i].writeToNBT( new NBTTagCompound() ) );
this.tankChanged[i] = !all;
empty = false;
}
}
return !empty;
}
public boolean readTankInfo( final Map<Integer, NBTTagCompound> tagMap )
{
boolean changed = false;
for( int i = 0; i < NUMBER_OF_TANKS; ++i )
{
if( tagMap.containsKey( i ) )
{
this.tanks[i].readFromNBT( tagMap.get( i ) );
changed = true;
}
}
return changed;
return this.tanks;
}
private class InterfaceRequestSource extends MachineSource
@@ -620,7 +545,7 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
InterfaceInventory( final DualityFluidInterface tileInterface )
{
super( tileInterface.storage );
super( tileInterface.tanks );
this.setActionSource( new MachineSource( tileInterface.iHost ) );
}
@@ -653,10 +578,37 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable
}
}
@Override
public void saveChanges()
{
this.iHost.getTileEntity().markDirty();
}
@Override
public IConfigManager getConfigManager()
{
return this.cm;
}
@Override
public IItemHandler getInventoryByName( String name )
{
return null;
}
@Override
public int getInstalledUpgrades( Upgrades u )
{
return 0;
}
@Override
public TileEntity getTile()
{
return (TileEntity) ( this.iHost instanceof TileEntity ? this.iHost : null );
}
@Override
public void updateSetting( IConfigManager manager, Enum settingName, Enum newValue )
{
}
}
@@ -16,7 +16,7 @@
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.fluids.contents;
package appeng.fluids.helper;
import javax.annotation.Nonnull;
@@ -0,0 +1,98 @@
package appeng.fluids.helper;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.IContainerListener;
import appeng.api.storage.data.IAEFluidStack;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketFluidSlot;
import appeng.fluids.util.AEFluidInventory;
import appeng.fluids.util.IAEFluidTank;
public class FluidSyncHelper
{
private final IAEFluidTank inv;
private final IAEFluidTank cache;
private final int idOffset;
public FluidSyncHelper( final IAEFluidTank inv, final int idOffset )
{
this.inv = inv;
this.cache = new AEFluidInventory( null, inv.getSlots() );
this.idOffset = idOffset;
}
public void sendFull( final Iterable<IContainerListener> listeners )
{
this.sendDiffMap( this.createDiffMap( true ), listeners );
}
public void sendDiff( final Iterable<IContainerListener> listeners )
{
this.sendDiffMap( this.createDiffMap( false ), listeners );
}
public void readPacket( final Map<Integer, IAEFluidStack> data )
{
for( int i = 0; i < this.inv.getSlots(); ++i )
{
if( data.containsKey( i + this.idOffset ) )
{
this.inv.setFluidInSlot( i, data.get( i + this.idOffset ) );
}
}
}
private void sendDiffMap( final Map<Integer, IAEFluidStack> data, final Iterable<IContainerListener> listeners )
{
if( data.isEmpty() )
{
return;
}
for( final IContainerListener l : listeners )
{
if( l instanceof EntityPlayerMP )
{
NetworkHandler.instance().sendTo( new PacketFluidSlot( data ), (EntityPlayerMP) l );
}
}
}
private final Map<Integer, IAEFluidStack> createDiffMap( final boolean full )
{
final Map<Integer, IAEFluidStack> ret = new HashMap<>();
for( int i = 0; i < this.inv.getSlots(); ++i )
{
if( full || !this.equalsSlot( i ) )
{
ret.put( i + this.idOffset, this.inv.getFluidInSlot( i ) );
}
if( !full )
{
this.cache.setFluidInSlot( i, this.inv.getFluidInSlot( i ) );
}
}
return ret;
}
private final boolean equalsSlot( int slot )
{
final IAEFluidStack stackA = this.inv.getFluidInSlot( slot );
final IAEFluidStack stackB = this.cache.getFluidInSlot( slot );
if( !Objects.equals( stackA, stackB ) )
{
return false;
}
return stackA == null || stackA.getStackSize() == stackB.getStackSize();
}
}
@@ -24,11 +24,12 @@ import java.util.EnumSet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import appeng.api.implementations.IUpgradeableHost;
import appeng.api.networking.security.IActionHost;
import appeng.me.helpers.IGridProxyable;
public interface IFluidInterfaceHost extends IActionHost, IGridProxyable
public interface IFluidInterfaceHost extends IActionHost, IGridProxyable, IUpgradeableHost
{
DualityFluidInterface getDualityFluidInterface();
@@ -27,7 +27,7 @@ import appeng.api.AEApi;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.channels.IFluidStorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.fluids.contents.FluidCellConfig;
import appeng.fluids.helper.FluidCellConfig;
import appeng.items.materials.MaterialType;
import appeng.items.storage.AbstractStorageCell;
import appeng.util.InventoryAdaptor;
@@ -78,6 +78,6 @@ public class FluidDummyItem extends AEBaseItem
@Override
protected void getCheckedSubItems( final CreativeTabs creativeTab, final NonNullList<ItemStack> itemStacks )
{
//Don't show this item in CreativeTabs
// Don't show this item in CreativeTabs
}
}
@@ -21,14 +21,11 @@ package appeng.fluids.parts;
import javax.annotation.Nonnull;
import com.google.common.base.Verify;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import appeng.api.config.Actionable;
import appeng.api.config.FuzzyMode;
@@ -44,10 +41,8 @@ import appeng.api.parts.IPartCollisionHelper;
import appeng.api.parts.IPartModel;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.core.AppEng;
import appeng.core.settings.TickRates;
import appeng.fluids.util.AEFluidStack;
import appeng.items.parts.PartModels;
import appeng.me.GridAccessException;
import appeng.me.helpers.MachineSource;
@@ -120,15 +115,10 @@ public class PartFluidExportBus extends PartSharedFluidBus
{
for( int i = 0; i < this.getConfig().getSlots(); i++ )
{
final IAEItemStack stack = this.getConfig().getAEStackInSlot( i );
if( stack != null && stack.getDefinition() != null )
IAEFluidStack fluid = this.getConfig().getFluidInSlot( i );
if( fluid != null )
{
final IFluidHandlerItem ifh = stack.getDefinition().getCapability( CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null );
Verify.verifyNotNull( ifh, "IFluidHandlerItem is null" );
final AEFluidStack toExtract = AEFluidStack.fromFluidStack( ifh.drain( Integer.MAX_VALUE, false ) );
final IAEFluidStack toExtract = fluid.copy();
toExtract.setStackSize( this.calculateAmountToSend() );
@@ -21,15 +21,12 @@ package appeng.fluids.parts;
import javax.annotation.Nonnull;
import com.google.common.base.Verify;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import appeng.api.config.Actionable;
import appeng.api.config.FuzzyMode;
@@ -44,7 +41,6 @@ import appeng.api.networking.ticking.TickingRequest;
import appeng.api.parts.IPartModel;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.core.AppEng;
import appeng.core.settings.TickRates;
import appeng.fluids.util.AEFluidStack;
@@ -157,20 +153,10 @@ public class PartFluidImportBus extends PartSharedFluidBus
{
for( int i = 0; i < this.getConfig().getSlots(); i++ )
{
final IAEItemStack stack = this.getConfig().getAEStackInSlot( i );
if( stack != null && stack.getDefinition() != null )
final IAEFluidStack stack = this.getConfig().getFluidInSlot( i );
if( stack != null && stack.equals( fluid ) )
{
final IFluidHandlerItem fh = stack.getDefinition().getCapability( CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null );
Verify.verifyNotNull( fh, "IFluidHandlerItem is null" );
final FluidStack filtered = fh.drain( Integer.MAX_VALUE, false );
if( filtered != null && filtered.isFluidEqual( fluid ) )
{
return true;
}
return true;
}
}
return false;
@@ -180,8 +166,7 @@ public class PartFluidImportBus extends PartSharedFluidBus
{
for( int i = 0; i < this.getConfig().getSlots(); i++ )
{
final IAEItemStack stack = this.getConfig().getAEStackInSlot( i );
final IAEFluidStack stack = this.getConfig().getFluidInSlot( i );
if( stack != null )
{
return true;
@@ -30,7 +30,9 @@ import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.IItemHandler;
import appeng.api.config.Upgrades;
import appeng.api.networking.IGridNode;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
@@ -45,6 +47,7 @@ import appeng.api.storage.IStorageChannel;
import appeng.api.storage.IStorageMonitorable;
import appeng.api.storage.data.IAEStack;
import appeng.api.util.AECableType;
import appeng.api.util.IConfigManager;
import appeng.core.AppEng;
import appeng.core.sync.GuiBridge;
import appeng.fluids.helper.DualityFluidInterface;
@@ -216,4 +219,21 @@ public class PartFluidInterface extends PartBasicState implements IGridTickable,
return this.duality.getCapability( capabilityClass, this.getSide().getFacing() );
}
@Override
public int getInstalledUpgrades( Upgrades u )
{
return this.duality.getInstalledUpgrades( u );
}
@Override
public IConfigManager getConfigManager()
{
return this.duality.getConfigManager();
}
@Override
public IItemHandler getInventoryByName( String name )
{
return this.duality.getInventoryByName( name );
}
}
@@ -33,12 +33,8 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import net.minecraftforge.items.IItemHandler;
import appeng.api.AEApi;
import appeng.api.config.AccessRestriction;
@@ -64,14 +60,15 @@ import appeng.api.storage.IStorageMonitorable;
import appeng.api.storage.IStorageMonitorableAccessor;
import appeng.api.storage.channels.IFluidStorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.AEPartLocation;
import appeng.capabilities.Capabilities;
import appeng.core.AppEng;
import appeng.core.settings.TickRates;
import appeng.core.sync.GuiBridge;
import appeng.fluids.util.AEFluidStack;
import appeng.fluids.util.AEFluidInventory;
import appeng.fluids.util.IAEFluidInventory;
import appeng.fluids.util.IAEFluidTank;
import appeng.helpers.IInterfaceHost;
import appeng.items.parts.PartModels;
import appeng.me.GridAccessException;
@@ -80,9 +77,7 @@ import appeng.me.storage.ITickingMonitor;
import appeng.me.storage.MEInventoryHandler;
import appeng.parts.PartModel;
import appeng.parts.misc.PartSharedStorageBus;
import appeng.tile.inventory.AppEngInternalAEInventory;
import appeng.util.Platform;
import appeng.util.inv.InvOperation;
import appeng.util.prioritylist.FuzzyPriorityList;
import appeng.util.prioritylist.PrecisePriorityList;
@@ -92,7 +87,7 @@ import appeng.util.prioritylist.PrecisePriorityList;
* @version rv6 - 22/05/2018
* @since rv6 22/05/2018
*/
public class PartFluidStorageBus extends PartSharedStorageBus implements IMEMonitorHandlerReceiver<IAEFluidStack>
public class PartFluidStorageBus extends PartSharedStorageBus implements IMEMonitorHandlerReceiver<IAEFluidStack>, IAEFluidInventory
{
public static final ResourceLocation MODEL_BASE = new ResourceLocation( AppEng.MOD_ID, "part/fluid_storage_bus_base" );
@PartModels
@@ -103,7 +98,7 @@ public class PartFluidStorageBus extends PartSharedStorageBus implements IMEMoni
public static final IPartModel MODELS_HAS_CHANNEL = new PartModel( MODEL_BASE, new ResourceLocation( AppEng.MOD_ID, "part/fluid_storage_bus_has_channel" ) );
private final IActionSource source;
private final AppEngInternalAEInventory config = new AppEngInternalAEInventory( this, 63 );
private final AEFluidInventory config = new AEFluidInventory( this, 63 );
private boolean cached = false;
private ITickingMonitor monitor = null;
private MEInventoryHandler<IAEFluidStack> handler = null;
@@ -198,7 +193,9 @@ public class PartFluidStorageBus extends PartSharedStorageBus implements IMEMoni
protected void resetCache( final boolean fullReset )
{
if( this.getHost() == null || this.getHost().getTile() == null || this.getHost().getTile().getWorld() == null || this.getHost().getTile().getWorld().isRemote )
if( this.getHost() == null || this.getHost().getTile() == null || this.getHost().getTile().getWorld() == null || this.getHost()
.getTile()
.getWorld().isRemote )
{
return;
}
@@ -239,27 +236,14 @@ public class PartFluidStorageBus extends PartSharedStorageBus implements IMEMoni
}
@Override
public void onChangeInventory( final IItemHandler inv, final int slot, final InvOperation mc, final ItemStack removedStack, final ItemStack newStack )
public void onFluidInventoryChanged( IAEFluidTank inv, int slot )
{
super.onChangeInventory( inv, slot, mc, removedStack, newStack );
if( inv == this.config )
{
this.resetCache( true );
}
}
@Override
public IItemHandler getInventoryByName( final String name )
{
if( name.equals( "config" ) )
{
return this.config;
}
return super.getInventoryByName( name );
}
@Override
public void readFromNBT( final NBTTagCompound data )
{
@@ -287,7 +271,9 @@ public class PartFluidStorageBus extends PartSharedStorageBus implements IMEMoni
{
if( this.getProxy().isActive() )
{
this.getProxy().getStorage().postAlterationOfStoredItems( AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ), change, this.source );
this.getProxy()
.getStorage()
.postAlterationOfStoredItems( AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ), change, this.source );
}
}
catch( final GridAccessException e )
@@ -342,23 +328,17 @@ public class PartFluidStorageBus extends PartSharedStorageBus implements IMEMoni
final int slotsToUse = 18 + this.getInstalledUpgrades( Upgrades.CAPACITY ) * 9;
for( int x = 0; x < this.config.getSlots() && x < slotsToUse; x++ )
{
final IAEItemStack is = this.config.getAEStackInSlot( x );
final IAEFluidStack is = this.config.getFluidInSlot( x );
if( is != null )
{
// Because we store filtered fluid as buckets, we need to grab the fluid from the stack
IFluidHandlerItem fh = FluidUtil.getFluidHandler( is.createItemStack() );
if( fh == null )
{
continue;
}
FluidStack fluid = fh.drain( Integer.MAX_VALUE, false );
priorityList.add( AEFluidStack.fromFluidStack( fluid ) );
priorityList.add( is );
}
}
if( this.getInstalledUpgrades( Upgrades.FUZZY ) > 0 )
{
this.handler.setPartitionList( new FuzzyPriorityList<IAEFluidStack>( priorityList, (FuzzyMode) this.getConfigManager().getSetting( Settings.FUZZY_MODE ) ) );
this.handler.setPartitionList(
new FuzzyPriorityList<IAEFluidStack>( priorityList, (FuzzyMode) this.getConfigManager().getSetting( Settings.FUZZY_MODE ) ) );
}
else
{
@@ -488,6 +468,11 @@ public class PartFluidStorageBus extends PartSharedStorageBus implements IMEMoni
return AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class );
}
public IAEFluidTank getConfig()
{
return this.config;
}
@SuppressWarnings( "Duplicates" )
@Nonnull
@Override
@@ -29,7 +29,6 @@ import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.items.IItemHandler;
import appeng.api.AEApi;
import appeng.api.config.RedstoneMode;
@@ -40,9 +39,10 @@ import appeng.api.parts.IPartCollisionHelper;
import appeng.api.storage.channels.IFluidStorageChannel;
import appeng.api.util.AECableType;
import appeng.core.sync.GuiBridge;
import appeng.fluids.util.AEFluidInventory;
import appeng.fluids.util.IAEFluidTank;
import appeng.me.GridAccessException;
import appeng.parts.automation.PartUpgradeable;
import appeng.tile.inventory.AppEngInternalAEInventory;
import appeng.util.Platform;
@@ -54,7 +54,7 @@ import appeng.util.Platform;
public abstract class PartSharedFluidBus extends PartUpgradeable implements IGridTickable
{
private final AppEngInternalAEInventory config = new AppEngInternalAEInventory( this, 9 );
private final AEFluidInventory config = new AEFluidInventory( null, 9 );
private boolean lastRedstone;
public PartSharedFluidBus( ItemStack is )
@@ -144,17 +144,6 @@ public abstract class PartSharedFluidBus extends PartUpgradeable implements IGri
return null;
}
@Override
public IItemHandler getInventoryByName( final String name )
{
if( name.equals( "config" ) )
{
return this.getConfig();
}
return super.getInventoryByName( name );
}
protected int calculateAmountToSend()
{
double amount = this.getChannel().transferFactor();
@@ -178,22 +167,23 @@ public abstract class PartSharedFluidBus extends PartUpgradeable implements IGri
public void readFromNBT( NBTTagCompound extra )
{
super.readFromNBT( extra );
this.getConfig().readFromNBT( extra, "config" );
this.config.readFromNBT( extra, "config" );
}
@Override
public void writeToNBT( NBTTagCompound extra )
{
super.writeToNBT( extra );
this.getConfig().writeToNBT( extra, "config" );
this.config.writeToNBT( extra, "config" );
}
public AppEngInternalAEInventory getConfig()
public IAEFluidTank getConfig()
{
return this.config;
}
protected IFluidStorageChannel getChannel(){
protected IFluidStorageChannel getChannel()
{
return AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class );
}
@@ -27,7 +27,9 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.IItemHandler;
import appeng.api.config.Upgrades;
import appeng.api.networking.IGridNode;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
@@ -38,6 +40,7 @@ import appeng.api.networking.ticking.TickingRequest;
import appeng.api.util.AECableType;
import appeng.api.util.AEPartLocation;
import appeng.api.util.DimensionalCoord;
import appeng.api.util.IConfigManager;
import appeng.fluids.helper.DualityFluidInterface;
import appeng.fluids.helper.IFluidInterfaceHost;
import appeng.helpers.IPriorityHost;
@@ -151,4 +154,22 @@ public class TileFluidInterface extends AENetworkTile implements IGridTickable,
}
return super.getCapability( capability, facing );
}
@Override
public int getInstalledUpgrades( Upgrades u )
{
return this.duality.getInstalledUpgrades( u );
}
@Override
public IConfigManager getConfigManager()
{
return this.duality.getConfigManager();
}
@Override
public IItemHandler getInventoryByName( String name )
{
return this.duality.getInventoryByName( name );
}
}
@@ -0,0 +1,377 @@
package appeng.fluids.util;
import java.util.Objects;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidTankProperties;
import appeng.api.storage.data.IAEFluidStack;
import appeng.core.AELog;
import appeng.util.Platform;
public class AEFluidInventory implements IAEFluidTank
{
private final IAEFluidStack[] fluids;
private final IAEFluidInventory handler;
private final long capacity;
private IFluidTankProperties[] props = null;
public AEFluidInventory( final IAEFluidInventory handler, final int slots, final long capcity )
{
this.fluids = new IAEFluidStack[slots];
this.handler = handler;
this.capacity = capcity;
}
public AEFluidInventory( final IAEFluidInventory handler, final int slots )
{
this( handler, slots, Long.MAX_VALUE );
}
@Override
public void setFluidInSlot( final int slot, final IAEFluidStack fluid )
{
if( slot >= 0 && slot < this.getSlots() )
{
if( Objects.equals( this.fluids[slot], fluid ) )
{
if( fluid != null && fluid.getStackSize() != this.fluids[slot].getStackSize() )
{
this.fluids[slot].setStackSize( Math.min( fluid.getStackSize(), this.capacity ) );
this.onContentChanged( slot );
}
}
else
{
if( fluid == null )
{
this.fluids[slot] = null;
}
else
{
this.fluids[slot] = fluid.copy();
this.fluids[slot].setStackSize( Math.min( fluid.getStackSize(), this.capacity ) );
}
this.onContentChanged( slot );
}
}
}
private void onContentChanged( final int slot )
{
if( this.handler != null && Platform.isServer() )
{
this.handler.onFluidInventoryChanged( this, slot );
}
}
@Override
public IAEFluidStack getFluidInSlot( final int slot )
{
if( slot >= 0 && slot < this.getSlots() )
{
return this.fluids[slot];
}
return null;
}
@Override
public int getSlots()
{
return fluids.length;
}
@Override
public IFluidTankProperties[] getTankProperties()
{
if( this.props == null )
{
this.props = new IFluidTankProperties[this.getSlots()];
for( int i = 0; i < this.getSlots(); ++i )
{
this.props[i] = new FluidTankPropertiesWrapper( i );
}
}
return this.props;
}
public int fill( final int slot, final FluidStack resource, final boolean doFill )
{
if( resource == null || resource.amount <= 0 )
{
return 0;
}
final IAEFluidStack fluid = this.fluids[slot];
if( !doFill )
{
if( fluid == null )
{
return resource.amount;
}
if( !fluid.equals( resource ) )
{
return 0;
}
return (int) Math.min( Math.min( this.capacity - fluid.getStackSize(), resource.amount ), Integer.MAX_VALUE );
}
if( fluid == null )
{
this.setFluidInSlot( slot, AEFluidStack.fromFluidStack( resource ) );
return resource.amount;
}
if( !fluid.equals( resource ) )
{
return 0;
}
long filled = this.capacity - fluid.getStackSize();
if( resource.amount < filled )
{
fluid.setStackSize( fluid.getStackSize() + resource.amount );
filled = resource.amount;
}
else
{
fluid.setStackSize( this.capacity );
}
onContentChanged( slot );
return (int) filled;
}
public FluidStack drain( final int slot, final FluidStack resource, final boolean doDrain )
{
final IAEFluidStack fluid = this.fluids[slot];
if( resource == null || fluid == null || !fluid.equals( resource ) )
{
return null;
}
return drain( slot, resource.amount, doDrain );
}
public FluidStack drain( final int slot, final int maxDrain, boolean doDrain )
{
final IAEFluidStack fluid = this.fluids[slot];
if( fluid == null || maxDrain <= 0 )
{
return null;
}
int drained = maxDrain;
if( fluid.getStackSize() < drained )
{
drained = (int) fluid.getStackSize();
}
FluidStack stack = new FluidStack( fluid.getFluid(), drained );
if( doDrain )
{
fluid.setStackSize( fluid.getStackSize() - drained );
if( fluid.getStackSize() <= 0 )
{
this.fluids[slot] = null;
}
onContentChanged( slot );
}
return stack;
}
@Override
public int fill( final FluidStack fluid, final boolean doFill )
{
if( fluid == null || fluid.amount <= 0 )
return 0;
final FluidStack insert = fluid.copy();
int totalFillAmount = 0;
for( int slot = 0; slot < this.getSlots(); ++slot )
{
int fillAmount = this.fill( slot, insert, doFill );
totalFillAmount += fillAmount;
insert.amount -= fillAmount;
if( insert.amount <= 0 )
break;
}
return totalFillAmount;
}
@Override
public FluidStack drain( final FluidStack fluid, final boolean doDrain )
{
if( fluid == null || fluid.amount <= 0 )
return null;
final FluidStack resource = fluid.copy();
FluidStack totalDrained = null;
for( int slot = 0; slot < this.getSlots(); ++slot )
{
FluidStack drain = this.drain( slot, resource, doDrain );
if( drain != null )
{
if( totalDrained == null )
totalDrained = drain;
else
totalDrained.amount += drain.amount;
resource.amount -= drain.amount;
if( resource.amount <= 0 )
break;
}
}
return totalDrained;
}
@Override
public FluidStack drain( final int maxDrain, final boolean doDrain )
{
if( maxDrain == 0 )
return null;
FluidStack totalDrained = null;
int toDrain = maxDrain;
for( int slot = 0; slot < this.getSlots(); ++slot )
{
if( totalDrained == null )
{
totalDrained = this.drain( slot, toDrain, doDrain );
if( totalDrained != null )
{
toDrain -= totalDrained.amount;
}
}
else
{
FluidStack copy = totalDrained.copy();
copy.amount = toDrain;
FluidStack drain = this.drain( slot, copy, doDrain );
if( drain != null )
{
totalDrained.amount += drain.amount;
toDrain -= drain.amount;
}
}
if( toDrain <= 0 )
break;
}
return totalDrained;
}
public void writeToNBT( final NBTTagCompound data, final String name )
{
final NBTTagCompound c = new NBTTagCompound();
this.writeToNBT( c );
data.setTag( name, c );
}
private void writeToNBT( final NBTTagCompound target )
{
for( int x = 0; x < this.fluids.length; x++ )
{
try
{
final NBTTagCompound c = new NBTTagCompound();
if( this.fluids[x] != null )
{
this.fluids[x].writeToNBT( c );
}
target.setTag( "#" + x, c );
}
catch( final Exception ignored )
{
}
}
}
public void readFromNBT( final NBTTagCompound data, final String name )
{
final NBTTagCompound c = data.getCompoundTag( name );
if( c != null )
{
this.readFromNBT( c );
}
}
private void readFromNBT( final NBTTagCompound target )
{
for( int x = 0; x < this.fluids.length; x++ )
{
try
{
final NBTTagCompound c = target.getCompoundTag( "#" + x );
if( c != null )
{
this.fluids[x] = AEFluidStack.fromNBT( c );
}
}
catch( final Exception e )
{
AELog.debug( e );
}
}
}
private class FluidTankPropertiesWrapper implements IFluidTankProperties
{
private final int slot;
public FluidTankPropertiesWrapper( final int slot )
{
this.slot = slot;
}
@Override
public FluidStack getContents()
{
return AEFluidInventory.this.fluids[slot] == null ? null : AEFluidInventory.this.fluids[slot].getFluidStack();
}
@Override
public int getCapacity()
{
return (int) Math.min( AEFluidInventory.this.capacity, Integer.MAX_VALUE );
}
@Override
public boolean canFill()
{
return true;
}
@Override
public boolean canDrain()
{
return true;
}
@Override
public boolean canFillFluidType( FluidStack fluidStack )
{
return true;
}
@Override
public boolean canDrainFluidType( FluidStack fluidStack )
{
return fluidStack != null;
}
}
}
@@ -34,13 +34,14 @@ import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import appeng.api.AEApi;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.channels.IFluidStorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.core.Api;
import appeng.fluids.items.FluidDummyItem;
import appeng.util.Platform;
import appeng.util.item.AEStack;
@@ -271,34 +272,13 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
{
if( ia instanceof AEFluidStack )
{
return ( (AEFluidStack) ia ).fluid == this.fluid && this.tagCompound == ( (AEFluidStack) ia ).tagCompound;
final AEFluidStack is = (AEFluidStack) ia;
return is.fluid == this.fluid && Platform.itemComparisons().isNbtTagEqual( this.tagCompound, is.tagCompound );
}
else if( ia instanceof FluidStack )
{
final FluidStack is = (FluidStack) ia;
if( is.getFluid() == this.fluid )
{
final NBTTagCompound ta = this.tagCompound;
final NBTTagCompound tb = is.tag;
if( ta == tb )
{
return true;
}
if( ( ta == null && tb == null ) || ( ta != null && ta.hasNoTags() && tb == null ) || ( tb != null && tb
.hasNoTags() && ta == null ) || ( ta != null && ta.hasNoTags() && tb != null && tb.hasNoTags() ) )
{
return true;
}
if( ( ta == null && tb != null ) || ( ta != null && tb == null ) )
{
return false;
}
return Platform.itemComparisons().isNbtTagEqual( ta, tb );
}
return is.getFluid() == this.fluid && Platform.itemComparisons().isNbtTagEqual( this.tagCompound, is.tag );
}
return false;
}
@@ -336,8 +316,14 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
@Override
public ItemStack asItemStackRepresentation()
{
// TODO: fluids, how do they even work?
return FluidUtil.getFilledBucket( this.getFluidStack() );
ItemStack is = Api.INSTANCE.definitions().items().dummyFluidItem().maybeStack( 1 ).orElse( ItemStack.EMPTY );
if( !is.isEmpty() )
{
FluidDummyItem item = (FluidDummyItem) is.getItem();
item.setFluidStack( is, this.getFluidStack() );
return is;
}
return ItemStack.EMPTY;
}
@Override
@@ -16,13 +16,17 @@
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.fluids.helper;
package appeng.fluids.util;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fluids.FluidTank;
import appeng.api.storage.data.IAEFluidStack;
import appeng.util.Platform;
public class AEFluidTank extends FluidTank
public class AEFluidTank extends FluidTank implements IAEFluidTank
{
private final IAEFluidInventory host;
@@ -30,16 +34,46 @@ public class AEFluidTank extends FluidTank
{
super( capacity );
this.host = host;
if (host instanceof TileEntity)
{
this.setTileEntity( (TileEntity) host );
}
}
@Override
protected void onContentsChanged()
{
if( host != null )
if( host != null && Platform.isServer() )
{
host.onFluidInventoryChanged( this );
host.onFluidInventoryChanged( this, 0 );
}
super.onContentsChanged();
}
@Override
public void setFluidInSlot( int slot, IAEFluidStack fluid )
{
if( slot == 0 )
{
this.setFluid( fluid == null ? null : fluid.getFluidStack() );
this.onContentsChanged();
}
}
@Override
public IAEFluidStack getFluidInSlot( int slot )
{
if( slot == 0 )
{
return AEFluidStack.fromFluidStack( this.getFluid() );
}
return null;
}
@Override
public int getSlots()
{
return 1;
}
}
@@ -16,14 +16,11 @@
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.fluids.helper;
import net.minecraftforge.fluids.capability.IFluidHandler;
package appeng.fluids.util;
@FunctionalInterface
public interface IAEFluidInventory
{
void onFluidInventoryChanged( IFluidHandler inventory );
void onFluidInventoryChanged( final IAEFluidTank inv, final int slot );
}
@@ -0,0 +1,18 @@
package appeng.fluids.util;
import net.minecraftforge.fluids.capability.IFluidHandler;
import appeng.api.storage.data.IAEFluidStack;
public interface IAEFluidTank extends IFluidHandler
{
void setFluidInSlot( final int slot, final IAEFluidStack fluid );
IAEFluidStack getFluidInSlot( final int slot );
int getSlots();
}