fluid pattern terminal ex

This commit is contained in:
GlodBlock committed 2022-05-12 15:24:42 +08:00
1 parent bb4a1e9e58
commit 3e76b36c49
19 files changed
+1247 -30

No files matched your search

@@ -0,0 +1,133 @@
package com.glodblock.github.client.gui;
import appeng.api.config.ActionItems;
import appeng.api.config.ItemSubstitution;
import appeng.api.config.Settings;
import appeng.api.storage.ITerminalHost;
import appeng.client.gui.widgets.GuiImgButton;
import appeng.container.slot.AppEngSlot;
import appeng.core.localization.GuiText;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.gui.container.FCBasePartContainerEx;
import com.glodblock.github.network.CPacketFluidPatternTermBtns;
import com.glodblock.github.util.Ae2Reflect;
import com.glodblock.github.util.ModAndClassUtil;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.player.InventoryPlayer;
import org.lwjgl.input.Keyboard;
public class GuiBaseFluidPatternTerminalEx extends GuiFCBaseMonitor {
private static final String SUBSITUTION_DISABLE = "0";
private static final String SUBSITUTION_ENABLE = "1";
private static final String CRAFTMODE_CRFTING = "1";
private static final String CRAFTMODE_PROCESSING = "0";
public FCBasePartContainerEx container;
private GuiImgButton substitutionsEnabledBtn;
private GuiImgButton substitutionsDisabledBtn;
private GuiImgButton encodeBtn;
private GuiImgButton clearBtn;
private GuiImgButton doubleBtn;
public GuiBaseFluidPatternTerminalEx(final InventoryPlayer inventoryPlayer, final ITerminalHost te )
{
super( inventoryPlayer, te, new FCBasePartContainerEx( inventoryPlayer, te ) );
this.container = (FCBasePartContainerEx) this.inventorySlots;
Ae2Reflect.setReservedSpace(this, 81);
}
@Override
protected void actionPerformed( final GuiButton btn )
{
super.actionPerformed( btn );
if( this.encodeBtn == btn )
{
FluidCraft.proxy.netHandler.sendToServer( new CPacketFluidPatternTermBtns( "PatternTerminal.Encode", isShiftKeyDown() ? "2" : "1" ) );
}
else if( this.clearBtn == btn )
{
FluidCraft.proxy.netHandler.sendToServer( new CPacketFluidPatternTermBtns( "PatternTerminal.Clear", "1" ) );
}
else if( this.substitutionsEnabledBtn == btn || this.substitutionsDisabledBtn == btn )
{
FluidCraft.proxy.netHandler.sendToServer( new CPacketFluidPatternTermBtns( "PatternTerminal.Substitute", this.substitutionsEnabledBtn == btn ? SUBSITUTION_DISABLE : SUBSITUTION_ENABLE ) );
}
else if (ModAndClassUtil.isDoubleButton && doubleBtn == btn)
{
FluidCraft.proxy.netHandler.sendToServer( new CPacketFluidPatternTermBtns( "PatternTerminal.Double", Keyboard.isKeyDown( Keyboard.KEY_LSHIFT ) ? "1": "0") );
}
}
@Override
@SuppressWarnings("unchecked")
public void initGui()
{
super.initGui();
this.substitutionsEnabledBtn = new GuiImgButton( this.guiLeft + 97, this.guiTop + this.ySize - 163, Settings.ACTIONS, ItemSubstitution.ENABLED );
this.substitutionsEnabledBtn.setHalfSize( true );
this.buttonList.add( this.substitutionsEnabledBtn );
this.substitutionsDisabledBtn = new GuiImgButton( this.guiLeft + 97, this.guiTop + this.ySize - 163, Settings.ACTIONS, ItemSubstitution.DISABLED );
this.substitutionsDisabledBtn.setHalfSize( true );
this.buttonList.add( this.substitutionsDisabledBtn );
this.clearBtn = new GuiImgButton( this.guiLeft + 87, this.guiTop + this.ySize - 163, Settings.ACTIONS, ActionItems.CLOSE );
this.clearBtn.setHalfSize( true );
this.buttonList.add( this.clearBtn );
this.encodeBtn = new GuiImgButton( this.guiLeft + 147, this.guiTop + this.ySize - 142, Settings.ACTIONS, ActionItems.ENCODE );
this.buttonList.add( this.encodeBtn );
if (ModAndClassUtil.isDoubleButton) {
this.doubleBtn = new GuiImgButton( this.guiLeft + 97, this.guiTop + this.ySize - 153, Settings.ACTIONS, ActionItems.DOUBLE );
this.doubleBtn.setHalfSize( true );
this.buttonList.add( this.doubleBtn );
}
}
@Override
public void drawFG( final int offsetX, final int offsetY, final int mouseX, final int mouseY )
{
if (ModAndClassUtil.isDoubleButton)
this.doubleBtn.visible = true;
if( this.container.substitute )
{
this.substitutionsEnabledBtn.visible = true;
this.substitutionsDisabledBtn.visible = false;
}
else
{
this.substitutionsEnabledBtn.visible = false;
this.substitutionsDisabledBtn.visible = true;
}
super.drawFG( offsetX, offsetY, mouseX, mouseY );
this.fontRendererObj.drawString( GuiText.PatternTerminal.getLocal(), 8, this.ySize - 96 + 2 - Ae2Reflect.getReservedSpace(this), 4210752 );
}
@Override
protected String getBackground()
{
return "guis/pattern3.png";
}
@Override
protected void repositionSlot( final AppEngSlot s )
{
if( s.isPlayerSide() )
{
s.yDisplayPosition = s.getY() + this.ySize - 78 - 5;
}
else
{
s.yDisplayPosition = s.getY() + this.ySize - 78 - 3;
}
}
}
@@ -32,17 +32,15 @@ import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.gui.container.FCBaseMonitorContain;
import com.glodblock.github.client.gui.container.FCBasePartContainer;
import com.glodblock.github.common.parts.PartFluidPatternTerminal;
import com.glodblock.github.common.parts.PartFluidPatternTerminalEx;
import com.glodblock.github.network.CPacketInventoryAction;
import com.glodblock.github.network.CPacketSetTargetItem;
import com.glodblock.github.util.Ae2Reflect;
import com.glodblock.github.util.ModAndClassUtil;
import com.glodblock.github.util.NameConst;
import com.google.common.base.Stopwatch;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import org.lwjgl.input.Keyboard;
@@ -50,7 +48,6 @@ import org.lwjgl.input.Mouse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class GuiFCBaseMonitor extends AEBaseMEGui implements ISortSource, IConfigManagerHost
{
@@ -62,10 +59,10 @@ public class GuiFCBaseMonitor extends AEBaseMEGui implements ISortSource, IConfi
private final ItemRepo repo;
private final int offsetX = 9;
private final int lowerTextureOffset = 0;
private final IConfigManager configSrc;
public IConfigManager configSrc;
private final boolean viewCell;
private final ItemStack[] myCurrentViewCells = new ItemStack[5];
private final FCBaseMonitorContain monitorableContainer;
public FCBaseMonitorContain monitorableContainer;
private GuiTabButton craftingStatusBtn;
private GuiImgButton craftingStatusImgBtn;
private MEGuiTextField searchField;
@@ -88,7 +85,7 @@ public class GuiFCBaseMonitor extends AEBaseMEGui implements ISortSource, IConfi
this( inventoryPlayer, te, new FCBasePartContainer( inventoryPlayer, te ) );
}
public GuiFCBaseMonitor( final InventoryPlayer inventoryPlayer, final ITerminalHost te, final FCBasePartContainer c )
public GuiFCBaseMonitor( final InventoryPlayer inventoryPlayer, final ITerminalHost te, final FCBaseMonitorContain c )
{
super( c );
@@ -116,6 +113,10 @@ public class GuiFCBaseMonitor extends AEBaseMEGui implements ISortSource, IConfi
{
this.myName = NameConst.GUI_FLUID_PATTERN_TERMINAL;
}
if (te instanceof PartFluidPatternTerminalEx)
{
this.myName = NameConst.GUI_FLUID_PATTERN_TERMINAL_EX;
}
}
public void postUpdate( final List<IAEItemStack> list )
@@ -12,6 +12,7 @@ import appeng.util.Platform;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.gui.container.ContainerFluidCraftConfirm;
import com.glodblock.github.common.parts.PartFluidPatternTerminal;
import com.glodblock.github.common.parts.PartFluidPatternTerminalEx;
import com.glodblock.github.inventory.gui.GuiType;
import com.glodblock.github.network.CPacketFluidPatternTermBtns;
import com.glodblock.github.network.CPacketSwitchGuis;
@@ -61,6 +62,10 @@ public class GuiFluidCraftConfirm extends AEBaseGui {
{
this.OriginalGui = GuiType.FLUID_PATTERN_TERMINAL;
}
if( te instanceof PartFluidPatternTerminalEx)
{
this.OriginalGui = GuiType.FLUID_PATTERN_TERMINAL_EX;
}
}
boolean isAutoStart()
@@ -3,6 +3,8 @@ package com.glodblock.github.client.gui;
import appeng.api.storage.ITerminalHost;
import appeng.client.gui.implementations.GuiCraftingStatus;
import appeng.client.gui.widgets.GuiTabButton;
import com.glodblock.github.common.parts.PartFluidPatternTerminal;
import com.glodblock.github.common.parts.PartFluidPatternTerminalEx;
import com.glodblock.github.inventory.InventoryHandler;
import com.glodblock.github.inventory.gui.GuiType;
import com.glodblock.github.loader.ItemAndBlockHolder;
@@ -14,14 +16,19 @@ import net.minecraft.item.ItemStack;
public class GuiFluidPatternTerminalCraftingStatus extends GuiCraftingStatus {
private GuiTabButton originalGuiBtn;
private final ITerminalHost host;
public GuiFluidPatternTerminalCraftingStatus(InventoryPlayer inventoryPlayer, ITerminalHost te) {
super(inventoryPlayer, te);
host = te;
}
@Override
public void initGui() {
Ae2Reflect.rewriteIcon(this, new ItemStack(ItemAndBlockHolder.FLUID_TERMINAL, 1));
if (host instanceof PartFluidPatternTerminal)
Ae2Reflect.rewriteIcon(this, new ItemStack(ItemAndBlockHolder.FLUID_TERMINAL, 1));
else if (host instanceof PartFluidPatternTerminalEx)
Ae2Reflect.rewriteIcon(this, new ItemStack(ItemAndBlockHolder.FLUID_TERMINAL_EX, 1));
super.initGui();
originalGuiBtn = Ae2Reflect.getOriginalGuiButton(this);
}
@@ -29,7 +36,10 @@ public class GuiFluidPatternTerminalCraftingStatus extends GuiCraftingStatus {
@Override
protected void actionPerformed(final GuiButton btn) {
if (btn == originalGuiBtn) {
InventoryHandler.switchGui(GuiType.FLUID_PATTERN_TERMINAL);
if (host instanceof PartFluidPatternTerminal)
InventoryHandler.switchGui(GuiType.FLUID_PATTERN_TERMINAL);
else if (host instanceof PartFluidPatternTerminalEx)
InventoryHandler.switchGui(GuiType.FLUID_PATTERN_TERMINAL_EX);
} else {
super.actionPerformed(btn);
}
@@ -0,0 +1,75 @@
package com.glodblock.github.client.gui;
import appeng.api.storage.ITerminalHost;
import appeng.api.storage.data.IAEItemStack;
import appeng.client.gui.widgets.GuiTabButton;
import appeng.client.render.AppEngRenderItem;
import appeng.container.slot.SlotFake;
import appeng.util.item.AEItemStack;
import com.glodblock.github.client.gui.container.ContainerFluidPatternTerminalEx;
import com.glodblock.github.common.item.ItemFluidPacket;
import com.glodblock.github.inventory.InventoryHandler;
import com.glodblock.github.inventory.gui.GuiType;
import com.glodblock.github.inventory.slot.SlotSingleItem;
import com.glodblock.github.util.Ae2Reflect;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Slot;
public class GuiFluidPatternTerminalEx extends GuiBaseFluidPatternTerminalEx {
private GuiTabButton craftingStatusBtn;
private final AppEngRenderItem stackSizeRenderer = Ae2Reflect.getStackSizeRenderer(this);
public GuiFluidPatternTerminalEx(InventoryPlayer inventoryPlayer, ITerminalHost te) {
super(inventoryPlayer, te);
ContainerFluidPatternTerminalEx container = new ContainerFluidPatternTerminalEx(inventoryPlayer, te);
container.setGui(this);
this.inventorySlots = container;
this.container = container;
this.monitorableContainer = container;
this.configSrc = container.getConfigManager();
}
@Override
public void initGui() {
super.initGui();
craftingStatusBtn = Ae2Reflect.getCraftingStatusButton(this);
}
@Override
public void func_146977_a( final Slot s )
{
if (drawSlot0(s))
super.func_146977_a( s );
}
public boolean drawSlot0(Slot slot) {
if (slot instanceof SlotFake) {
AEItemStack stack = AEItemStack.create(slot.getStack());
super.func_146977_a(new SlotSingleItem(slot));
if (stack == null) return true;
IAEItemStack fake = stack.copy();
if (fake.getItemStack().getItem() instanceof ItemFluidPacket) {
if (ItemFluidPacket.getFluidStack(stack) != null && ItemFluidPacket.getFluidStack(stack).amount > 0)
fake.setStackSize(ItemFluidPacket.getFluidStack(stack).amount);
}
else
return true;
stackSizeRenderer.setAeStack(fake);
stackSizeRenderer.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), stack.getItemStack(), slot.xDisplayPosition, slot.yDisplayPosition);
return false;
}
return true;
}
@Override
protected void actionPerformed(final GuiButton btn) {
if (btn == craftingStatusBtn) {
InventoryHandler.switchGui(GuiType.FLUID_PAT_TERM_CRAFTING_STATUS);
} else {
super.actionPerformed(btn);
}
}
}
@@ -0,0 +1,178 @@
package com.glodblock.github.client.gui.container;
import appeng.api.AEApi;
import appeng.api.definitions.IDefinitions;
import appeng.api.storage.ITerminalHost;
import appeng.api.storage.data.IAEItemStack;
import appeng.container.slot.SlotFakeCraftingMatrix;
import appeng.container.slot.SlotPatternOutputs;
import appeng.helpers.InventoryAction;
import appeng.util.item.AEItemStack;
import com.glodblock.github.common.item.ItemFluidDrop;
import com.glodblock.github.common.item.ItemFluidEncodedPattern;
import com.glodblock.github.common.item.ItemFluidPacket;
import com.glodblock.github.inventory.PatternConsumer;
import com.glodblock.github.loader.ItemAndBlockHolder;
import com.glodblock.github.util.FluidPatternDetails;
import com.glodblock.github.util.Util;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
import java.util.ArrayList;
import java.util.List;
public class ContainerFluidPatternTerminalEx extends FCBasePartContainerEx implements PatternConsumer {
public ContainerFluidPatternTerminalEx(InventoryPlayer ip, ITerminalHost monitorable) {
super(ip, monitorable);
}
@Override
public void encode() {
if (!checkHasFluidPattern()) {
super.encode();
return;
}
ItemStack stack = this.patternSlotOUT.getStack();
if (stack == null) {
stack = this.patternSlotIN.getStack();
if (!isPattern(stack)) {
return;
}
if (stack.stackSize == 1) {
this.patternSlotIN.putStack(null);
} else {
stack.stackSize --;
}
encodeFluidPattern();
} else if (isPattern(stack)) {
encodeFluidPattern();
}
}
private static boolean isPattern(final ItemStack output) {
if (output == null) {
return false;
}
if (output.getItem() instanceof ItemFluidEncodedPattern) {
return true;
}
final IDefinitions defs = AEApi.instance().definitions();
return defs.items().encodedPattern().isSameAs(output) || defs.materials().blankPattern().isSameAs(output);
}
private boolean checkHasFluidPattern() {
boolean hasFluid = false, search = false;
for (Slot craftingSlot : this.craftingSlots) {
final ItemStack crafting = craftingSlot.getStack();
if (crafting == null) {
continue;
}
search = true;
if (crafting.getItem() instanceof ItemFluidPacket) {
hasFluid = true;
break;
}
}
if (!search) { // search=false -> inputs were empty
return false;
}
// `search` should be true at this point
for (Slot outputSlot : this.outputSlots) {
final ItemStack out = outputSlot.getStack();
if (out == null) {
continue;
}
search = false;
if (hasFluid) {
break;
} else if (out.getItem() instanceof ItemFluidPacket) {
hasFluid = true;
break;
}
}
return hasFluid && !search; // search=true -> outputs were empty
}
private void encodeFluidPattern() {
ItemStack patternStack = new ItemStack(ItemAndBlockHolder.PATTERN);
FluidPatternDetails pattern = new FluidPatternDetails(patternStack);
pattern.setInputs(collectInventory(craftingSlots));
pattern.setOutputs(collectInventory(outputSlots));
patternSlotOUT.putStack(pattern.writeToStack());
}
private static IAEItemStack[] collectInventory(Slot[] slots) {
// see note at top of FluidPatternDetails
List<IAEItemStack> acc = new ArrayList<>();
for (Slot slot : slots) {
ItemStack stack = slot.getStack();
if (stack == null) {
continue;
}
if (stack.getItem() instanceof ItemFluidPacket) {
IAEItemStack dropStack = ItemFluidDrop.newAeStack(ItemFluidPacket.getFluidStack(stack));
if (dropStack != null) {
acc.add(dropStack);
continue;
}
}
IAEItemStack aeStack = AEItemStack.create(stack);
acc.add(aeStack);
}
return acc.toArray(new IAEItemStack[0]);
}
@Override
public void acceptPattern(IAEItemStack[] inputs, IAEItemStack[] outputs) {
if (getPatternTerminal() != null) {
getPatternTerminal().onChangeCrafting(inputs, outputs);
}
}
@Override
public void doAction(EntityPlayerMP player, InventoryAction action, int slotId, long id) {
if (slotId < 0 || slotId >= this.inventorySlots.size()) {
super.doAction(player, action, slotId, id);
return;
}
Slot slot = getSlot(slotId);
ItemStack stack = player.inventory.getItemStack();
if ((slot instanceof SlotFakeCraftingMatrix || slot instanceof SlotPatternOutputs) && stack != null
&& (stack.getItem() instanceof IFluidContainerItem || FluidContainerRegistry.isContainer(stack))) {
FluidStack fluid = null;
switch (action) {
case PICKUP_OR_SET_DOWN:
fluid = Util.getFluidFromItem(stack);
slot.putStack(ItemFluidPacket.newStack(fluid));
break;
case SPLIT_OR_PLACE_SINGLE:
fluid = Util.getFluidFromItem(Util.copyStackWithSize(stack, 1));
FluidStack origin = ItemFluidPacket.getFluidStack(slot.getStack());
if (fluid != null && fluid.equals(origin)) {
fluid.amount += origin.amount;
if (fluid.amount <= 0) fluid = null;
}
slot.putStack(ItemFluidPacket.newStack(fluid));
break;
}
if (fluid == null) {
super.doAction(player, action, slotId, id);
return;
}
return;
}
if (action == InventoryAction.SPLIT_OR_PLACE_SINGLE) {
if (stack == null && slot.getStack() != null && slot.getStack().getItem() instanceof ItemFluidPacket) {
return;
}
}
super.doAction(player, action, slotId, id);
}
}
@@ -0,0 +1,410 @@
package com.glodblock.github.client.gui.container;
import appeng.api.AEApi;
import appeng.api.definitions.IDefinitions;
import appeng.api.storage.ITerminalHost;
import appeng.container.guisync.GuiSync;
import appeng.container.slot.*;
import appeng.helpers.IContainerCraftingPacket;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.IAEAppEngInventory;
import appeng.tile.inventory.InvOperation;
import appeng.util.Platform;
import com.glodblock.github.common.item.ItemFluidPacket;
import com.glodblock.github.common.parts.PartFluidPatternTerminalEx;
import com.glodblock.github.util.Util;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.fluids.FluidStack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
public class FCBasePartContainerEx extends FCBaseMonitorContain implements IAEAppEngInventory, IOptionalSlotHost, IContainerCraftingPacket {
private final PartFluidPatternTerminalEx patternTerminal;
private final AppEngInternalInventory cOut = new AppEngInternalInventory( null, 1 );
private final IInventory crafting;
protected final SlotFakeCraftingMatrix[] craftingSlots = new SlotFakeCraftingMatrix[16];
protected final OptionalSlotFake[] outputSlots = new OptionalSlotFake[4];
protected final SlotRestrictedInput patternSlotIN;
protected final SlotRestrictedInput patternSlotOUT;
@GuiSync( 96 )
public boolean substitute = false;
public FCBasePartContainerEx(final InventoryPlayer ip, final ITerminalHost monitorable )
{
super( ip, monitorable, false );
this.patternTerminal = (PartFluidPatternTerminalEx) monitorable;
final IInventory patternInv = this.getPatternTerminal().getInventoryByName( "pattern" );
final IInventory output = this.getPatternTerminal().getInventoryByName( "output" );
this.crafting = this.getPatternTerminal().getInventoryByName( "crafting" );
for( int y = 0; y < 4; y++ )
{
for( int x = 0; x < 4; x++ )
{
this.addSlotToContainer( this.craftingSlots[x + y * 4] = new SlotFakeCraftingMatrix( this.crafting, x + y * 4, 15 + x * 18, -83 + y * 18 ) );
}
}
for( int y = 0; y < 4; y++ )
{
this.addSlotToContainer( this.outputSlots[y] = new SlotPatternOutputs( output, this, y, 112, -83, 0, y, 1 ) );
this.outputSlots[y].setRenderDisabled( false );
}
this.addSlotToContainer( this.patternSlotIN = new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.BLANK_PATTERN, patternInv, 0, 147, -72 - 9, this.getInventoryPlayer() ) );
this.addSlotToContainer( this.patternSlotOUT = new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.ENCODED_PATTERN, patternInv, 1, 147, -72 + 34, this.getInventoryPlayer() ) );
this.patternSlotOUT.setStackLimit( 1 );
this.bindPlayerInventory( ip, 0, 0 );
}
@Override
public void saveChanges()
{
}
public void clear()
{
for( final Slot s : this.craftingSlots )
{
s.putStack( null );
}
for( final Slot s : this.outputSlots )
{
s.putStack( null );
}
this.detectAndSendChanges();
}
@Override
public void onChangeInventory(final IInventory inv, final int slot, final InvOperation mc, final ItemStack removedStack, final ItemStack newStack )
{
}
public void encodeAndMoveToInventory()
{
encode();
ItemStack output = this.patternSlotOUT.getStack();
if ( output != null )
{
if (!getPlayerInv().addItemStackToInventory( output )){
getPlayerInv().player.entityDropItem(output, 0);
}
this.patternSlotOUT.putStack( null );
}
}
public void encode()
{
ItemStack output = this.patternSlotOUT.getStack();
final ItemStack[] in = this.getInputs();
final ItemStack[] out = this.getOutputs();
// if there is no input, this would be silly.
if( in == null || out == null )
{
return;
}
// first check the output slots, should either be null, or a pattern
if( output != null && !this.isPattern( output ) )
{
return;
}// if nothing is there we should snag a new pattern.
else if( output == null )
{
output = this.patternSlotIN.getStack();
if (!this.isPattern( output ))
{
return; // no blanks.
}
// remove one, and clear the input slot.
output.stackSize--;
if( output.stackSize == 0 )
{
this.patternSlotIN.putStack( null );
}
// add a new encoded pattern.
for( final ItemStack encodedPatternStack : AEApi.instance().definitions().items().encodedPattern().maybeStack( 1 ).asSet() )
{
output = encodedPatternStack;
this.patternSlotOUT.putStack( output );
}
}
// encode the slot.
final NBTTagCompound encodedValue = new NBTTagCompound();
final NBTTagList tagIn = new NBTTagList();
final NBTTagList tagOut = new NBTTagList();
for( final ItemStack i : in )
{
tagIn.appendTag( this.createItemTag( i ) );
}
for( final ItemStack i : out )
{
tagOut.appendTag( this.createItemTag( i ) );
}
encodedValue.setTag( "in", tagIn );
encodedValue.setTag( "out", tagOut );
encodedValue.setBoolean( "substitute", this.isSubstitute() );
output.setTagCompound( encodedValue );
}
private ItemStack[] getInputs()
{
final ItemStack[] input = new ItemStack[16];
boolean hasValue = false;
for( int x = 0; x < this.craftingSlots.length; x++ )
{
input[x] = this.craftingSlots[x].getStack();
if( input[x] != null )
{
hasValue = true;
}
}
if( hasValue )
{
return input;
}
return null;
}
private ItemStack[] getOutputs()
{
final List<ItemStack> list = new ArrayList<>( 4 );
boolean hasValue = false;
for( final OptionalSlotFake outputSlot : this.outputSlots )
{
final ItemStack out = outputSlot.getStack();
if( out != null && out.stackSize > 0 )
{
list.add( out );
hasValue = true;
}
}
if( hasValue )
{
return list.toArray( new ItemStack[list.size()] );
}
return null;
}
private boolean isPattern( final ItemStack output )
{
if( output == null )
{
return false;
}
final IDefinitions definitions = AEApi.instance().definitions();
boolean isPattern = definitions.items().encodedPattern().isSameAs( output );
isPattern |= definitions.materials().blankPattern().isSameAs( output );
return isPattern;
}
private NBTBase createItemTag(final ItemStack i )
{
final NBTTagCompound c = new NBTTagCompound();
if( i != null )
{
i.writeToNBT( c );
}
return c;
}
@Override
public boolean isSlotEnabled( final int idx )
{
if( idx == 1 )
{
Platform.isServer();
return true;
}
else if( idx == 2 )
{
Platform.isServer();
return false;
}
return false;
}
@Override
public void detectAndSendChanges()
{
super.detectAndSendChanges();
if( Platform.isServer() )
{
this.substitute = this.patternTerminal.isSubstitution();
}
}
@Override
public void onSlotChange( final Slot s )
{
if( s == this.patternSlotOUT && Platform.isServer() )
{
for( final Object crafter : this.crafters )
{
final ICrafting icrafting = (ICrafting) crafter;
for( final Object g : this.inventorySlots )
{
if( g instanceof OptionalSlotFake || g instanceof SlotFakeCraftingMatrix )
{
final Slot sri = (Slot) g;
icrafting.sendSlotContents( this, sri.slotNumber, sri.getStack() );
}
}
( (EntityPlayerMP) icrafting ).isChangingQuantityOnly = false;
}
this.detectAndSendChanges();
}
}
@Override
public IInventory getInventoryByName( final String name )
{
if( name.equals( "player" ) )
{
return this.getInventoryPlayer();
}
return this.getPatternTerminal().getInventoryByName( name );
}
@Override
public boolean useRealItems()
{
return false;
}
public void toggleSubstitute()
{
this.substitute = !this.substitute;
this.detectAndSendChanges();
}
public PartFluidPatternTerminalEx getPatternTerminal()
{
return this.patternTerminal;
}
private boolean isSubstitute()
{
return this.substitute;
}
public void setSubstitute( final boolean substitute )
{
this.substitute = substitute;
}
static boolean canDoubleStacks(SlotFake[] slots)
{
List<SlotFake> enabledSlots = Arrays.stream(slots).filter(SlotFake::isEnabled).collect(Collectors.toList());
long emptyStots = enabledSlots.stream().filter(s -> s.getStack() == null).count();
long fullSlots = enabledSlots.stream().filter(s-> s.getStack() != null && s.getStack().stackSize * 2 > 127).count();
return fullSlots <= emptyStots;
}
static void doubleStacksInternal(SlotFake[] slots)
{
List<ItemStack> overFlowStacks = new ArrayList<>();
List<SlotFake> enabledSlots = Arrays.stream(slots).filter(SlotFake::isEnabled).collect(Collectors.toList());
for (final Slot s : enabledSlots)
{
ItemStack st = s.getStack();
if (st == null)
continue;
if (Util.isFluidPacket(st)) {
FluidStack fluidStack = ItemFluidPacket.getFluidStack(st);
if (fluidStack != null) {
fluidStack = ItemFluidPacket.getFluidStack(st).copy();
if (fluidStack.amount < Integer.MAX_VALUE / 2)
fluidStack.amount *= 2;
}
s.putStack(ItemFluidPacket.newStack(fluidStack));
}
else if (st.stackSize * 2 > 127)
{
overFlowStacks.add(st.copy());
}
else
{
st.stackSize *= 2;
s.putStack(st);
}
}
Iterator<ItemStack> ow = overFlowStacks.iterator();
for (final Slot s : enabledSlots) {
if (!ow.hasNext())
break;
if (s.getStack() != null)
continue;
s.putStack(ow.next());
}
assert !ow.hasNext();
}
public void doubleStacks(boolean isShift)
{
if (canDoubleStacks(craftingSlots) && canDoubleStacks(outputSlots))
{
doubleStacksInternal(this.craftingSlots);
doubleStacksInternal(this.outputSlots);
if (isShift && !containsFluid(outputSlots) && !containsFluid(craftingSlots))
{
while (canDoubleStacks(craftingSlots) && canDoubleStacks(outputSlots))
{
doubleStacksInternal(this.craftingSlots);
doubleStacksInternal(this.outputSlots);
}
}
this.detectAndSendChanges();
}
}
static boolean containsFluid(SlotFake[] slots) {
List<SlotFake> enabledSlots = Arrays.stream(slots).filter(SlotFake::isEnabled).collect(Collectors.toList());
long fluid = enabledSlots.stream().filter(s -> Util.isFluidPacket(s.getStack())).count();
return fluid > 0;
}
}