Skip to content

Commit

Permalink
add refund button to patterndualinputhatch
Browse files Browse the repository at this point in the history
  • Loading branch information
reobf committed Feb 22, 2024
1 parent 3e27737 commit 7bb3ebe
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 32 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@

[![](https://github.com/reobf/Programmable-Hatches-Mod/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/reobf/Programmable-Hatches-Mod/actions/workflows/build-and-test.yml)


2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ catch (Exception ignored) {

// Pulls version first from the VERSION env and then git tag
String identifiedVersion = null
String versionOverride = '0.0.2'
String versionOverride = '0.0.3'
try {
// Produce a version based on the tag, or for branches something like 0.2.2-configurable-maven-and-extras.38+43090270b6-dirty
if (versionOverride == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -94,7 +95,7 @@
import reobf.proghatches.main.MyMod;
import reobf.proghatches.util.ProghatchesUtil;

public class BufferedDualInputHatch extends DualInputHatch {
public class BufferedDualInputHatch extends DualInputHatch implements IRecipeProcessingAwareDualHatch {


public Widget circuitSlot(IItemHandlerModifiable inventory, int slot) {
Expand Down Expand Up @@ -653,7 +654,7 @@ public static class CallerCheck {
}
private boolean updateEveryTick;
public boolean updateEveryTick(){return updateEveryTick;}
public boolean prevdirty;
//public boolean prevdirty;
@Override
public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
super.onPostTick(aBaseMetaTileEntity, aTick);
Expand All @@ -678,7 +679,7 @@ public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {

inv0.clearRecipeIfNeeded();
}
prevdirty=dirty;
// prevdirty=dirty;
dirty=false;
}

Expand Down Expand Up @@ -1041,8 +1042,8 @@ private NBTTagCompound cv(String s) {
thiz.disableWorking();
} else {
thiz.enableWorking();
BufferedDualInputHatch bff =(BufferedDualInputHatch) (thiz).getMetaTileEntity();
bff.dirty=true;
//BufferedDualInputHatch bff =(BufferedDualInputHatch) (thiz).getMetaTileEntity();
BufferedDualInputHatch.this.dirty=true;
}
})
.setPlayClickSoundResource( () -> thiz.isAllowedToWork() ? SoundResource.GUI_BUTTON_UP.resourceLocation
Expand Down Expand Up @@ -1087,7 +1088,8 @@ public void detectAndSendChanges(boolean init) {

//flush changes to client
//sometimes vanilla detection will fail so sync it manually
if(last+1>=getBaseMetaTileEntity().getTimer())
// System.out.println(last-getBaseMetaTileEntity().getTimer());
if(last>=getBaseMetaTileEntity().getTimer())
getWindow().getContext().getContainer().inventorySlots
.forEach(s->((Slot)s).onSlotChanged());

Expand Down Expand Up @@ -1215,9 +1217,7 @@ public boolean justUpdated() {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Optional</*? extends */IDualInputInventory> getFirstNonEmptyInventory() {
dirty=true;
//IRecipeProcessingAwareHatch NOT working on dualhatch, so assume multi-te will consume items after calling this
return (Optional) inv0.stream()
return (Optional) inv0.stream()
.filter(not(DualInvBuffer::isEmpty))
.findAny();
}
Expand All @@ -1228,7 +1228,7 @@ private Predicate<DualInvBuffer> not(Predicate<DualInvBuffer> s) {

@Override
public Iterator<? extends IDualInputInventory> inventories() {
dirty=true;

return inv0.stream()
.filter(not(DualInvBuffer::isEmpty))
.iterator();
Expand Down Expand Up @@ -1418,4 +1418,15 @@ public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlaye

return super.onRightclick(aBaseMetaTileEntity, aPlayer);
}

@Override
public void startRecipeProcessing() {

}

@Override
public CheckRecipeResult endRecipeProcessing(GT_MetaTileEntity_MultiBlockBase controller) {
dirty=true;
return CheckRecipeResultRegistry.SUCCESSFUL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package reobf.proghatches.gt.metatileentity;

import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_MultiBlockBase;
import gregtech.api.recipe.check.CheckRecipeResult;
import gregtech.api.recipe.check.CheckRecipeResultRegistry;
import gregtech.common.tileentities.machines.IDualInputHatch;
import gregtech.common.tileentities.machines.IRecipeProcessingAwareHatch;

public interface IRecipeProcessingAwareDualHatch extends
/*future GT Multiblock might check IDualInputHatch for 'instanceof IRecipeProcessingAwareHatch', so do not implement in case it's called twice*/
/*IRecipeProcessingAwareHatch,*/
IDualInputHatch{
void startRecipeProcessing() ;

CheckRecipeResult endRecipeProcessing(GT_MetaTileEntity_MultiBlockBase controller) ;


}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package reobf.proghatches.gt.metatileentity;

import static gregtech.api.metatileentity.BaseTileEntity.TOOLTIP_DELAY;
import static reobf.proghatches.main.Config.defaultObj;

import java.util.EnumSet;
import java.util.Optional;
import java.util.function.Consumer;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
Expand All @@ -16,19 +18,25 @@
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;

import com.glodblock.github.common.item.ItemFluidPacket;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.gtnewhorizons.modularui.api.drawable.IDrawable;
import com.gtnewhorizons.modularui.api.math.Pos2d;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.ModularWindow.Builder;
import com.gtnewhorizons.modularui.api.widget.IWidgetBuilder;
import com.gtnewhorizons.modularui.api.widget.Widget;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.common.internal.wrapper.BaseSlot;
import com.gtnewhorizons.modularui.common.widget.ButtonWidget;
import com.gtnewhorizons.modularui.common.widget.FakeSyncWidget;
import com.gtnewhorizons.modularui.common.widget.SlotWidget;

import appeng.api.AEApi;
import appeng.api.implementations.ICraftingPatternItem;
import appeng.api.implementations.IPowerChannelState;
import appeng.api.networking.GridFlags;
Expand All @@ -37,6 +45,12 @@
import appeng.api.networking.crafting.ICraftingProvider;
import appeng.api.networking.crafting.ICraftingProviderHelper;
import appeng.api.networking.events.MENetworkCraftingPatternChange;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.IActionHost;
import appeng.api.networking.security.MachineSource;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
import appeng.api.util.IInterfaceViewable;
Expand All @@ -45,8 +59,10 @@
import appeng.me.GridAccessException;
import appeng.me.helpers.AENetworkProxy;
import appeng.me.helpers.IGridProxyable;
import appeng.util.Platform;
import gregtech.GT_Mod;
import gregtech.api.GregTech_API;
import gregtech.api.enums.SoundResource;
import gregtech.api.enums.Textures.BlockIcons;
import gregtech.api.gui.modularui.GT_UITextures;
import gregtech.api.interfaces.ITexture;
Expand Down Expand Up @@ -134,10 +150,107 @@ public PatternDualInputHatch(int id, String name, String nameRegional, int tier,
}

ItemStack[] pattern = new ItemStack[36];

@Override


ButtonWidget createRefundButton(IWidgetBuilder<?> builder) {

Widget button = new ButtonWidget().setOnClick((clickData, widget) -> {


PatternDualInputHatch.this.dirty=true;
try {
PatternDualInputHatch.this.refundAll();
} catch (Exception e) {

//e.printStackTrace();
}
})
.setPlayClickSound(true)
.setBackground(GT_UITextures.BUTTON_STANDARD, GT_UITextures.OVERLAY_BUTTON_EXPORT)


.addTooltips(ImmutableList.of("Return all internally stored items back to AE"))

.setPos(new Pos2d(getGUIWidth()-18-3,5+16+2))
.setSize(16, 16);
return (ButtonWidget) button;
}
MachineSource requestSource;
private BaseActionSource getRequest() {

if (requestSource == null) requestSource = new MachineSource((IActionHost) getBaseMetaTileEntity());
return requestSource;
}
private void refundAll() throws Exception {
markDirty();
BaseActionSource src = getRequest();
IMEMonitor<IAEItemStack> sg = getProxy().getStorage()
.getItemInventory();
abstract class Inv{abstract ItemStack[] geti();abstract FluidTank[] getf(); }
Consumer<Inv> consumer= inv->{
try{
for (ItemStack itemStack : inv.geti()) {
if (itemStack == null || itemStack.stackSize == 0) continue;
IAEItemStack rest = Platform.poweredInsert(
getProxy().getEnergy(),
sg,
AEApi.instance()
.storage()
.createItemStack(itemStack),
src);
itemStack.stackSize = rest != null && rest.getStackSize() > 0 ? (int) rest.getStackSize() : 0;
}
IMEMonitor<IAEFluidStack> fsg = getProxy().getStorage()
.getFluidInventory();
for (FluidTank fluidStack : inv.getf()) {
if (fluidStack == null || fluidStack.getFluidAmount() == 0) continue;
IAEFluidStack rest = Platform.poweredInsert(
getProxy().getEnergy(),
fsg,
AEApi.instance()
.storage()
.createFluidStack(fluidStack.getFluid()),
src);
fluidStack.setFluid(Optional.ofNullable(rest).map(IAEFluidStack::getFluidStack).orElse(null));
};}
catch(Exception e){throw new RuntimeException(e);}
};

inv0.stream().map(s->new Inv() {
@Override
ItemStack[] geti() {
return s.mStoredItemInternal;
}

@Override
FluidTank[] getf() {
return s.mStoredFluidInternal;
}
}).forEach(consumer);
;
consumer.accept(new Inv() {

@Override
ItemStack[] geti() {

return mInventory;
}

@Override
FluidTank[] getf() {

return mStoredFluid;
}
});

}


@Override
public void addUIWidgets(Builder builder, UIBuildContext buildContext) {
buildContext.addSyncedWindow(88, this::createPatternWindow);

builder.widget(createRefundButton(builder));
builder.widget(
new ButtonWidget().setOnClick(
(clickData, widget) -> {
Expand All @@ -151,8 +264,8 @@ public void addUIWidgets(Builder builder, UIBuildContext buildContext) {
.addTooltips(
ImmutableList.of(StatCollector.translateToLocalFormatted("programmable_hatches.gt.pattern")))
.setSize(16, 16)
.setPos(10 + 16 * 9, 3 + 16 * 2)

// .setPos(10 + 16 * 9, 3 + 16 * 2)
.setPos(new Pos2d(getGUIWidth()-18-3,5+16+2+16+2))
);

super.addUIWidgets(builder, buildContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -40,6 +41,7 @@
import appeng.api.networking.security.IActionHost;
import appeng.api.networking.security.MachineSource;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
Expand Down Expand Up @@ -174,14 +176,20 @@ public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
}

}

toReturn.forEach(
s -> getStorageGrid().getItemInventory()
.injectItems(
AEItemStack.create(s),
Actionable.MODULATE,
new MachineSource((IActionHost) getBaseMetaTileEntity())));
toReturn.clear();
//IMEMonitor<IAEItemStack> ae = getStorageGrid().getItemInventory();
toReturn.replaceAll(
s -> Optional.ofNullable(
getStorageGrid().getItemInventory()
.injectItems(
AEItemStack.create(s),
Actionable.MODULATE,
new MachineSource((IActionHost) getBaseMetaTileEntity()))
)
.filter(ss->ss.getStackSize()<=0)
.map(ss->ss.getItemStack())
.orElse(null)
);
toReturn.removeIf(Objects::isNull);

super.onPostTick(aBaseMetaTileEntity, aTick);
}
Expand Down
Loading

0 comments on commit 7bb3ebe

Please sign in to comment.