Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port passthrough hatches to MUI2 and allow them to be togged #2504

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package gregtech.common.metatileentities.multi.multiblockpart;

import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IControllable;
import gregtech.api.capability.impl.FilteredFluidHandler;
import gregtech.api.capability.impl.FluidHandlerProxy;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.capability.impl.NotifiableItemStackHandler;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.widgets.TankWidget;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.multiblock.IMultiblockAbilityPart;
import gregtech.api.metatileentity.multiblock.IPassthroughHatch;
import gregtech.api.metatileentity.multiblock.MultiblockAbility;
import gregtech.api.mui.GTGuiTextures;
import gregtech.api.mui.GTGuis;
import gregtech.client.renderer.texture.Textures;
import gregtech.common.mui.widget.GTFluidSlot;

import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
Expand All @@ -29,13 +31,25 @@
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Matrix4;
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.api.widget.IWidget;
import com.cleanroommc.modularui.factory.PosGuiData;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.value.BoolValue;
import com.cleanroommc.modularui.value.sync.BooleanSyncValue;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
import com.cleanroommc.modularui.widgets.SlotGroupWidget;
import com.cleanroommc.modularui.widgets.ToggleButton;
import com.cleanroommc.modularui.widgets.layout.Grid;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MetaTileEntityPassthroughHatchFluid extends MetaTileEntityMultiblockPart implements IPassthroughHatch,
IMultiblockAbilityPart<IPassthroughHatch> {
IMultiblockAbilityPart<IPassthroughHatch>,
IControllable {

private static final int TANK_SIZE = 16_000;

Expand All @@ -44,8 +58,11 @@ public class MetaTileEntityPassthroughHatchFluid extends MetaTileEntityMultibloc
private IFluidHandler importHandler;
private IFluidHandler exportHandler;

private boolean workingEnabled;

public MetaTileEntityPassthroughHatchFluid(ResourceLocation metaTileEntityId, int tier) {
super(metaTileEntityId, tier);
this.workingEnabled = true;
initializeInventory();
}

Expand All @@ -70,11 +87,25 @@ protected void initializeInventory() {
public void update() {
super.update();
if (!getWorld().isRemote && getOffsetTimer() % 5 == 0) {
pushFluidsIntoNearbyHandlers(getFrontFacing().getOpposite()); // outputs to back
pullFluidsFromNearbyHandlers(getFrontFacing()); // inputs from front
if (workingEnabled) {
pushFluidsIntoNearbyHandlers(getFrontFacing().getOpposite()); // outputs to back
pullFluidsFromNearbyHandlers(getFrontFacing()); // inputs from front
}
}
}

public void setWorkingEnabled(boolean workingEnabled) {
this.workingEnabled = workingEnabled;
World world = getWorld();
if (world != null && !world.isRemote) {
writeCustomData(GregtechDataCodes.WORKING_ENABLED, buf -> buf.writeBoolean(workingEnabled));
}
}

public boolean isWorkingEnabled() {
return this.workingEnabled;
}

@Override
public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) {
super.renderMetaTileEntity(renderState, translation, pipeline);
Expand All @@ -101,40 +132,68 @@ protected IItemHandlerModifiable createImportItemHandler() {
}

@Override
protected ModularUI createUI(EntityPlayer entityPlayer) {
public boolean usesMui2() {
return true;
}

@Override
public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager) {
int rowSize = (int) Math.sqrt(getTier() + 1);
return createUITemplate(entityPlayer, rowSize)
.build(getHolder(), entityPlayer);
}

private ModularUI.Builder createUITemplate(EntityPlayer player, int rowSize) {
ModularUI.Builder builder = ModularUI.builder(GuiTextures.BACKGROUND, 176, 18 + 18 * rowSize + 94)
.label(6, 6, getMetaFullName());

for (int y = 0; y < rowSize; y++) {
for (int x = 0; x < rowSize; x++) {
int index = y * rowSize + x;
builder.widget(
new TankWidget(fluidTankList.getTankAt(index), 89 - rowSize * 9 + x * 18, 18 + y * 18, 18, 18)
.setBackgroundTexture(GuiTextures.FLUID_SLOT)
.setContainerClicking(true, true)
.setAlwaysShowFull(true));

int backgroundWidth = 9 * 18 + 14;
int backgroundHeight = 18 + 18 * rowSize + 94;

List<List<IWidget>> widgets = new ArrayList<>();
for (int i = 0; i < rowSize; i++) {
widgets.add(new ArrayList<>());
for (int j = 0; j < rowSize; j++) {
widgets.get(i).add(new GTFluidSlot().syncHandler(fluidTankList.getTankAt(i * rowSize + j))
.background(GTGuiTextures.FLUID_SLOT));
}
}
return builder.bindPlayerInventory(player.inventory, GuiTextures.SLOT, 7, 18 + 18 * rowSize + 12);

BooleanSyncValue workingStateValue = new BooleanSyncValue(() -> workingEnabled, val -> workingEnabled = val);
guiSyncManager.syncValue("working_state", workingStateValue);

// TODO: Change the position of the name when it's standardized.
return GTGuis.createPanel(this, backgroundWidth, backgroundHeight)
.child(IKey.lang(getMetaFullName()).asWidget().pos(5, 5))
.child(SlotGroupWidget.playerInventory().left(7).bottom(7))
.child(new Grid()
.top(18).height(rowSize * 18)
.minElementMargin(0, 0)
.minColWidth(18).minRowHeight(18)
.alignX(0.5f)
.matrix(widgets))
.child(new ToggleButton()
.top(18 * 2).left(18 * 8 + 7)
.value(new BoolValue.Dynamic(workingStateValue::getBoolValue,
workingStateValue::setBoolValue))
.overlay(GTGuiTextures.BUTTON_FLUID_OUTPUT)
.tooltipBuilder(t -> t.setAutoUpdate(true)
.addLine(workingStateValue.getBoolValue() ?
IKey.lang("gregtech.gui.fluid_passthrough.enabled") :
IKey.lang("gregtech.gui.fluid_passthrough.disabled"))));
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag);
tag.setTag("FluidInventory", fluidTankList.serializeNBT());
tag.setBoolean("WorkingEnabled", workingEnabled);
return tag;
}

@Override
public void readFromNBT(NBTTagCompound tag) {
super.readFromNBT(tag);
this.fluidTankList.deserializeNBT(tag.getCompoundTag("FluidInventory"));
// Passthrough hatches before this change won't have WorkingEnabled at all, so we need to check if it exists
if (tag.hasKey("WorkingEnabled")) {
this.workingEnabled = tag.getBoolean("WorkingEnabled");
} else {
this.workingEnabled = true;
}
}

@Override
Expand Down Expand Up @@ -174,6 +233,8 @@ public <T> T getCapability(Capability<T> capability, EnumFacing side) {
} else if (side == getFrontFacing().getOpposite()) {
return CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.cast(exportHandler);
} else return null;
} else if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this);
}
return super.getCapability(capability, side);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package gregtech.common.metatileentities.multi.multiblockpart;

import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IControllable;
import gregtech.api.capability.impl.ItemHandlerProxy;
import gregtech.api.capability.impl.NotifiableItemStackHandler;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.widgets.SlotWidget;
import gregtech.api.items.itemhandlers.GTItemStackHandler;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.multiblock.IMultiblockAbilityPart;
import gregtech.api.metatileentity.multiblock.IPassthroughHatch;
import gregtech.api.metatileentity.multiblock.MultiblockAbility;
import gregtech.api.mui.GTGuiTextures;
import gregtech.api.mui.GTGuis;
import gregtech.client.renderer.texture.Textures;

import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
Expand All @@ -29,21 +30,38 @@
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Matrix4;
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.api.widget.IWidget;
import com.cleanroommc.modularui.factory.PosGuiData;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.value.BoolValue;
import com.cleanroommc.modularui.value.sync.BooleanSyncValue;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
import com.cleanroommc.modularui.value.sync.SyncHandlers;
import com.cleanroommc.modularui.widgets.ItemSlot;
import com.cleanroommc.modularui.widgets.SlotGroupWidget;
import com.cleanroommc.modularui.widgets.ToggleButton;
import com.cleanroommc.modularui.widgets.layout.Grid;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MetaTileEntityPassthroughHatchItem extends MetaTileEntityMultiblockPart implements IPassthroughHatch,
IMultiblockAbilityPart<IPassthroughHatch> {
IMultiblockAbilityPart<IPassthroughHatch>,
IControllable {

private ItemStackHandler itemStackHandler;

private IItemHandler importHandler;
private IItemHandler exportHandler;

private boolean workingEnabled;

public MetaTileEntityPassthroughHatchItem(ResourceLocation metaTileEntityId, int tier) {
super(metaTileEntityId, tier);
this.workingEnabled = true;
initializeInventory();
}

Expand All @@ -69,8 +87,24 @@ private int getInventorySize() {
public void update() {
super.update();
if (!getWorld().isRemote && getOffsetTimer() % 5 == 0) {
pushItemsIntoNearbyHandlers(getFrontFacing().getOpposite()); // outputs to back
pullItemsFromNearbyHandlers(getFrontFacing()); // inputs from front
if (workingEnabled) {
pushItemsIntoNearbyHandlers(getFrontFacing().getOpposite()); // outputs to back
pullItemsFromNearbyHandlers(getFrontFacing()); // inputs from front
}
}
}

@Override
public boolean isWorkingEnabled() {
return this.workingEnabled;
}

@Override
public void setWorkingEnabled(boolean workingEnabled) {
this.workingEnabled = workingEnabled;
World world = getWorld();
if (world != null && !world.isRemote) {
writeCustomData(GregtechDataCodes.WORKING_ENABLED, buf -> buf.writeBoolean(workingEnabled));
}
}

Expand Down Expand Up @@ -100,38 +134,73 @@ protected IItemHandlerModifiable createImportItemHandler() {
}

@Override
protected ModularUI createUI(EntityPlayer entityPlayer) {
int rowSize = (int) Math.sqrt(getInventorySize());
return createUITemplate(entityPlayer, rowSize)
.build(getHolder(), entityPlayer);
public boolean usesMui2() {
return true;
}

private ModularUI.Builder createUITemplate(EntityPlayer player, int rowSize) {
ModularUI.Builder builder = ModularUI.builder(GuiTextures.BACKGROUND, 176, 18 + 18 * rowSize + 94)
.label(6, 6, getMetaFullName());
@Override
public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager) {
int rowSize = (int) Math.sqrt(getInventorySize());

guiSyncManager.registerSlotGroup("item_inv", rowSize);

int backgroundWidth = 9 * 18 + 14;
int backgroundHeight = 18 + 18 * rowSize + 94;

for (int y = 0; y < rowSize; y++) {
for (int x = 0; x < rowSize; x++) {
int index = y * rowSize + x;
builder.widget(new SlotWidget(itemStackHandler, index,
(88 - rowSize * 9 + x * 18), 18 + y * 18, true, true)
.setBackgroundTexture(GuiTextures.SLOT));
List<List<IWidget>> widgets = new ArrayList<>();
for (int i = 0; i < rowSize; i++) {
widgets.add(new ArrayList<>());
for (int j = 0; j < rowSize; j++) {
widgets.get(i)
.add(new ItemSlot()
.slot(SyncHandlers.itemSlot(itemStackHandler, i * rowSize + j)
.slotGroup("item_inv")
.accessibility(true, true)));
}
}
return builder.bindPlayerInventory(player.inventory, GuiTextures.SLOT, 7, 18 + 18 * rowSize + 12);

BooleanSyncValue workingStateValue = new BooleanSyncValue(() -> workingEnabled, val -> workingEnabled = val);
guiSyncManager.syncValue("working_state", workingStateValue);

// TODO: Change the position of the name when it's standardized.
return GTGuis.createPanel(this, backgroundWidth, backgroundHeight)
.child(IKey.lang(getMetaFullName()).asWidget().pos(5, 5))
.child(SlotGroupWidget.playerInventory().left(7).bottom(7))
.child(new Grid()
.top(18).height(rowSize * 18)
.minElementMargin(0, 0)
.minColWidth(18).minRowHeight(18)
.alignX(0.5f)
.matrix(widgets))
.child(new ToggleButton()
.top(18 * 4).left(18 * 8 + 7)
.value(new BoolValue.Dynamic(workingStateValue::getBoolValue,
workingStateValue::setBoolValue))
.overlay(GTGuiTextures.BUTTON_ITEM_OUTPUT)
.tooltipBuilder(t -> t.setAutoUpdate(true)
.addLine(workingStateValue.getBoolValue() ?
IKey.lang("gregtech.gui.item_passthrough.enabled") :
IKey.lang("gregtech.gui.item_passthrough.disabled"))));
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag);
tag.setTag("Inventory", itemStackHandler.serializeNBT());
tag.setBoolean("WorkingEnabled", workingEnabled);
return tag;
}

@Override
public void readFromNBT(NBTTagCompound tag) {
super.readFromNBT(tag);
this.itemStackHandler.deserializeNBT(tag.getCompoundTag("Inventory"));
// Passthrough hatches before this change won't have WorkingEnabled at all, so we need to check if it exists
if (tag.hasKey("WorkingEnabled")) {
this.workingEnabled = tag.getBoolean("WorkingEnabled");
} else {
this.workingEnabled = true;
}
}

@Override
Expand Down Expand Up @@ -176,6 +245,8 @@ public <T> T getCapability(Capability<T> capability, EnumFacing side) {
} else if (side == getFrontFacing().getOpposite()) {
return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(exportHandler);
} else return null;
} else if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this);
}
return super.getCapability(capability, side);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/assets/gregtech/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -5490,6 +5490,10 @@ gregtech.gui.config_slot.auto_pull_managed=§4Disabled:§7 Managed by Auto-Pull
gregtech.gui.me_bus.extra_slot=Extra Slot/n§7Put extra items for recipes here, like Molds or Lenses
gregtech.gui.me_bus.auto_pull_button=Click to toggle automatic item pulling from ME
gregtech.gui.alarm.radius=Radius:
gregtech.gui.item_passthrough.enabled=Item Passthough Enabled
gregtech.gui.item_passthrough.disabled=Item Passthough Disabled
gregtech.gui.fluid_passthrough.enabled=Fluid Passthough Enabled
gregtech.gui.fluid_passthrough.disabled=Fluid Passthough Disabled


ore.spawnlocation.name=Ore Spawn Information
Expand Down