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, 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 getCapability(Capability capability, EnumFacing side) { if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) { return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this); } return super.getCapability(capability, side); } @Override public MultiblockAbility getAbility() { return MultiblockAbility.IMPORT_ITEMS; } @Override public void registerAbilities(@NotNull MultiblockAbility key, @NotNull List 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); } } }