Skip to content

Instantly share code, notes, and snippets.

@Zorbatron
Created August 10, 2024 06:55
Show Gist options
  • Save Zorbatron/983568e2f26fe79529bff7642304b563 to your computer and use it in GitHub Desktop.
Save Zorbatron/983568e2f26fe79529bff7642304b563 to your computer and use it in GitHub Desktop.
Dual hatches
package gregtech.common.metatileentities.multi.multiblockpart;
import gregtech.api.GTValues;
import gregtech.api.capability.DualHandler;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.capability.impl.ItemHandlerList;
import gregtech.api.capability.impl.NotifiableFluidTank;
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.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.MultiblockAbility;
import gregtech.api.util.GTUtility;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MetaTileEntityDistinctInputDual extends MetaTileEntityMultiblockNotifiablePart implements
IMultiblockAbilityPart<IItemHandlerModifiable> {
private DualHandler[] dualHandlers;
public MetaTileEntityDistinctInputDual(ResourceLocation metaTileEntityId) {
super(metaTileEntityId, GTValues.LuV, false);
}
@Override
public MetaTileEntity createMetaTileEntity(IGregTechTileEntity tileEntity) {
return new MetaTileEntityDistinctInputDual(metaTileEntityId);
}
@Override
protected void initializeInventory() {
dualHandlers = new DualHandler[2];
for (int i = 0; i < dualHandlers.length; i++) {
var itemStackHandler = new NotifiableItemStackHandler(this, 4, getController(), false);
var fluidStackHandler = new FluidTankList(false, createTanks());
dualHandlers[i] = new DualHandler(itemStackHandler, fluidStackHandler, this.isExportHatch);
}
super.initializeInventory();
}
private IFluidTank[] createTanks() {
return new IFluidTank[] {
new NotifiableFluidTank(32000, null, isExportHatch),
new NotifiableFluidTank(32000, null, isExportHatch)
};
}
@Override
protected IItemHandlerModifiable createImportItemHandler() {
return new ItemHandlerList(Arrays.asList(dualHandlers));
}
@Override
protected FluidTankList createImportFluidHandler() {
return new FluidTankList(dualHandlers[0].allowSameFluidFill(), dualHandlers[0], dualHandlers[1].getFluidTanks().toArray(new IFluidTank[0]));
}
@SuppressWarnings("deprecation")
@Override
protected ModularUI createUI(EntityPlayer entityPlayer) {
ModularUI.Builder builder = ModularUI.builder(GuiTextures.BACKGROUND, 176, 18 + 54 + 94)
.label(10, 5, getMetaFullName());
//First set
int xStart = 7;
int yStart = 20;
builder.widget(new SlotWidget(dualHandlers[0], 0, xStart, yStart)
.setBackgroundTexture(GuiTextures.SLOT));
builder.widget(new SlotWidget(dualHandlers[0], 1, xStart + 18, yStart).
setBackgroundTexture(GuiTextures.SLOT));
builder.widget(new SlotWidget(dualHandlers[0], 2, xStart, yStart + 18)
.setBackgroundTexture(GuiTextures.SLOT));
builder.widget(new SlotWidget(dualHandlers[0], 3, xStart + 18, yStart + 18)
.setBackgroundTexture(GuiTextures.SLOT));
builder.widget(new TankWidget(dualHandlers[0].getTankAt(0), xStart, yStart + 18 * 2, 18, 18)
.setBackgroundTexture(GuiTextures.FLUID_SLOT)
.setAlwaysShowFull(true)
.setContainerClicking(true, true));
builder.widget(new TankWidget(dualHandlers[0].getTankAt(1), xStart + 18, yStart + 18 * 2, 18, 18)
.setBackgroundTexture(GuiTextures.FLUID_SLOT)
.setAlwaysShowFull(true)
.setContainerClicking(true, true));
//Second set
int xOffset = xStart + 18 * 3;
builder.widget(new SlotWidget(dualHandlers[1], 0, xOffset, yStart)
.setBackgroundTexture(GuiTextures.SLOT));
builder.widget(new SlotWidget(dualHandlers[1], 1, xOffset + 18, yStart)
.setBackgroundTexture(GuiTextures.SLOT));
builder.widget(new SlotWidget(dualHandlers[1], 2, xOffset, yStart + 18)
.setBackgroundTexture(GuiTextures.SLOT));
builder.widget(new SlotWidget(dualHandlers[1], 3, xOffset + 18, yStart + 18)
.setBackgroundTexture(GuiTextures.SLOT));
builder.widget(new TankWidget(dualHandlers[1].getTankAt(0), xOffset, yStart + 18 * 2, 18, 18)
.setBackgroundTexture(GuiTextures.FLUID_SLOT)
.setAlwaysShowFull(true)
.setContainerClicking(true, true));
builder.widget(new TankWidget(dualHandlers[1].getTankAt(1), xOffset + 18, yStart + 18 * 2, 18, 18)
.setBackgroundTexture(GuiTextures.FLUID_SLOT)
.setAlwaysShowFull(true)
.setContainerClicking(true, true));
builder.bindPlayerInventory(entityPlayer.inventory, GuiTextures.SLOT, 7, 84);
return builder.build(getHolder(), entityPlayer);
}
@Override
public MultiblockAbility<IItemHandlerModifiable> getAbility() {
return MultiblockAbility.IMPORT_ITEMS;
}
@Override
public void registerAbilities(@NotNull MultiblockAbility<IItemHandlerModifiable> key,
@NotNull List<IItemHandlerModifiable> abilities) {
abilities.addAll(Arrays.asList(dualHandlers));
}
}
package gregtech.common.metatileentities.multi.multiblockpart;
import gregtech.api.GTValues;
import gregtech.api.capability.DualHandler;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IControllable;
import gregtech.api.capability.IGhostSlotConfigurable;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.capability.impl.GhostCircuitItemStackHandler;
import gregtech.api.capability.impl.ItemHandlerList;
import gregtech.api.capability.impl.NotifiableFluidTank;
import gregtech.api.capability.impl.NotifiableItemStackHandler;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.widgets.GhostCircuitSlotWidget;
import gregtech.api.gui.widgets.SlotWidget;
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.MultiblockAbility;
import gregtech.api.metatileentity.multiblock.MultiblockControllerBase;
import gregtech.client.renderer.texture.Textures;
import gregtech.client.renderer.texture.cube.SimpleOverlayRenderer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.items.IItemHandlerModifiable;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Matrix4;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
public class MetaTileEntityDualInputHatch extends MetaTileEntityMultiblockNotifiablePart
implements IMultiblockAbilityPart<IItemHandlerModifiable>,
IGhostSlotConfigurable,
IControllable {
protected GhostCircuitItemStackHandler circuitInventory;
private IItemHandlerModifiable actualImportItems;
private FluidTankList fluidTankList;
private DualHandler dualHandler;
private boolean isWorkingEnabled;
public MetaTileEntityDualInputHatch(ResourceLocation metaTileEntityId) {
super(metaTileEntityId, GTValues.EV, false);
initializeInventory();
this.isWorkingEnabled = true;
}
@Override
public void initializeInventory() {
FluidTank[] fluidsHandlers = new FluidTank[3];
for (int i = 0; i < fluidsHandlers.length; i++) {
fluidsHandlers[i] = new NotifiableFluidTank(32000, this, false);
}
this.fluidTankList = new FluidTankList(false, fluidsHandlers);
super.initializeInventory();
this.circuitInventory = new GhostCircuitItemStackHandler(this);
this.circuitInventory.addNotifiableMetaTileEntity(this);
this.actualImportItems = new ItemHandlerList(Arrays.asList(super.getImportItems(), this.circuitInventory));
this.dualHandler = new DualHandler(this.actualImportItems, this.fluidTankList, false);
}
@Override
protected IItemHandlerModifiable createImportItemHandler() {
return new NotifiableItemStackHandler(this, 9, getController(), false);
}
@Override
protected FluidTankList createImportFluidHandler() {
return this.fluidTankList;
}
@Override
public IItemHandlerModifiable getImportItems() {
return this.actualImportItems;
}
@Override
public MetaTileEntity createMetaTileEntity(IGregTechTileEntity tileEntity) {
return new MetaTileEntityDualInputHatch(metaTileEntityId);
}
@Override
public void update() {
super.update();
if (!getWorld().isRemote && getOffsetTimer() % 5 == 0) {
if (this.isWorkingEnabled) {
pullItemsFromNearbyHandlers(getFrontFacing());
pullFluidsFromNearbyHandlers(getFrontFacing());
}
}
}
@Override
protected ModularUI createUI(EntityPlayer entityPlayer) {
int gridSize = 3;
int backgroundWidth = 176;
int center = backgroundWidth / 2;
int gridStartX = center - (gridSize * 9);
int inventoryStartX = center - 9 - 4 * 18;
int inventoryStartY = 18 + 18 * gridSize + 12;
ModularUI.Builder builder = ModularUI.builder(GuiTextures.BACKGROUND, backgroundWidth, 18 + 18 * gridSize + 94)
.label(10, 5, getMetaFullName());
// Item Slots
for (int y = 0; y < gridSize; y++) {
for (int x = 0; x < gridSize; x++) {
int index = y * gridSize + x;
builder.widget(new SlotWidget(isExportHatch ? exportItems : importItems, index,
gridStartX + x * 18, 18 + y * 18, true, !isExportHatch)
.setBackgroundTexture(GuiTextures.SLOT));
}
}
if (hasGhostCircuitInventory() && this.circuitInventory != null) {
int circuitX = inventoryStartX + 8 * 18;
int circuitY = gridSize * 18;
builder.widget(new GhostCircuitSlotWidget(circuitInventory, 0, circuitX, circuitY)
.setBackgroundTexture(GuiTextures.SLOT, GuiTextures.INT_CIRCUIT_OVERLAY)
.setConsumer(this::getCircuitSlotTooltip));
}
// Fluid Slots
for (int y = 0; y < gridSize; y++) {
builder.widget(
new TankWidget(fluidTankList.getTankAt(y), 124, 18 + y * 18, 18, 18)
.setBackgroundTexture(GuiTextures.FLUID_SLOT)
.setContainerClicking(true, !isExportHatch)
.setAlwaysShowFull(true));
}
builder.bindPlayerInventory(entityPlayer.inventory, GuiTextures.SLOT, inventoryStartX, inventoryStartY);
return builder.build(getHolder(), entityPlayer);
}
protected void getCircuitSlotTooltip(@NotNull SlotWidget widget) {
String configString;
if (circuitInventory == null || circuitInventory.getCircuitValue() == GhostCircuitItemStackHandler.NO_CONFIG) {
configString = new TextComponentTranslation("gregtech.gui.configurator_slot.no_value").getFormattedText();
} else {
configString = String.valueOf(circuitInventory.getCircuitValue());
}
widget.setTooltipText("gregtech.gui.configurator_slot.tooltip", configString);
}
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing side) {
if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this);
}
return super.getCapability(capability, side);
}
@Override
public MultiblockAbility<IItemHandlerModifiable> getAbility() {
return MultiblockAbility.IMPORT_ITEMS;
}
@Override
public void registerAbilities(@NotNull MultiblockAbility<IItemHandlerModifiable> key,
@NotNull List<IItemHandlerModifiable> abilities) {
if (key.equals(MultiblockAbility.IMPORT_ITEMS)) {
abilities.add(this.dualHandler);
}
}
@Override
public void addToMultiBlock(MultiblockControllerBase controllerBase) {
super.addToMultiBlock(controllerBase);
this.circuitInventory.addNotifiableMetaTileEntity(controllerBase);
this.circuitInventory.addToNotifiedList(controllerBase, this.circuitInventory, false);
}
@Override
public void removeFromMultiBlock(MultiblockControllerBase controllerBase) {
super.removeFromMultiBlock(controllerBase);
this.circuitInventory.addNotifiableMetaTileEntity(controllerBase);
}
@Override
public boolean hasGhostCircuitInventory() {
return this.circuitInventory != null;
}
@Override
public void setGhostCircuitConfig(int config) {
if (this.circuitInventory == null || this.circuitInventory.getCircuitValue() == config) {
return;
}
this.circuitInventory.setCircuitValue(config);
if (!getWorld().isRemote) {
markDirty();
}
}
@Override
public boolean isWorkingEnabled() {
return this.isWorkingEnabled;
}
@Override
public void setWorkingEnabled(boolean workingEnabled) {
this.isWorkingEnabled = workingEnabled;
World world = getWorld();
if (world != null && !world.isRemote) {
writeCustomData(GregtechDataCodes.WORKING_ENABLED, buf -> buf.writeBoolean(isWorkingEnabled));
}
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
if (this.circuitInventory != null) {
this.circuitInventory.write(data);
}
return data;
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
if (this.circuitInventory != null) {
this.circuitInventory.read(data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment