Created
August 13, 2024 08:13
-
-
Save Zorbatron/5131972e675a1d0d45bcb4b48cdbd5b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.AbilityInstances; | |
| 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.fluids.IFluidTank; | |
| 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.Collection; | |
| import java.util.Collections; | |
| 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 && 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 @NotNull List<MultiblockAbility<?>> getAbilities() { | |
| return Arrays.asList(MultiblockAbility.IMPORT_ITEMS, MultiblockAbility.IMPORT_FLUIDS); | |
| } | |
| @Override | |
| public void registerAbilities(@NotNull AbilityInstances abilityInstances) { | |
| if (abilityInstances.isKey(MultiblockAbility.IMPORT_ITEMS)) { | |
| abilityInstances.add(this.dualHandler); | |
| } else if (abilityInstances.isKey(MultiblockAbility.IMPORT_FLUIDS)) { | |
| abilityInstances.addAll(this.dualHandler.unwrap()); | |
| } | |
| } | |
| @Override | |
| public void addToMultiBlock(MultiblockControllerBase controllerBase) { | |
| super.addToMultiBlock(controllerBase); | |
| ((NotifiableItemStackHandler) this.importItems).addNotifiableMetaTileEntity(controllerBase); | |
| this.circuitInventory.addNotifiableMetaTileEntity(controllerBase); | |
| this.circuitInventory.addToNotifiedList(controllerBase, this.circuitInventory, false); | |
| } | |
| @Override | |
| public void removeFromMultiBlock(MultiblockControllerBase controllerBase) { | |
| super.removeFromMultiBlock(controllerBase); | |
| ((NotifiableItemStackHandler) this.importItems).removeNotifiableMetaTileEntity(controllerBase); | |
| this.circuitInventory.removeNotifiableMetaTileEntity(controllerBase); | |
| } | |
| @Override | |
| public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) { | |
| super.renderMetaTileEntity(renderState, translation, pipeline); | |
| Textures.PIPE_IN_OVERLAY.renderSided(getFrontFacing(), renderState, translation, pipeline); | |
| } | |
| @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