From 670be72cd35a9230a7a06d8258d2ff13116773c3 Mon Sep 17 00:00:00 2001 From: Galyfray Date: Mon, 22 Mar 2021 23:55:21 +0100 Subject: [PATCH 1/3] pump filters --- .../covers/filter/FluidFilterContainer.java | 16 ++++++++-------- .../electric/MetaTileEntityPump.java | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/java/gregtech/common/covers/filter/FluidFilterContainer.java b/src/main/java/gregtech/common/covers/filter/FluidFilterContainer.java index 067cedd06a..6da4e989f2 100644 --- a/src/main/java/gregtech/common/covers/filter/FluidFilterContainer.java +++ b/src/main/java/gregtech/common/covers/filter/FluidFilterContainer.java @@ -53,7 +53,7 @@ public ItemStackHandler getFilterInventory() { public void initUI(int y, Consumer widgetGroup) { widgetGroup.accept(new LabelWidget(10, y, "cover.pump.fluid_filter.title")); widgetGroup.accept(new SlotWidget(filterInventory, 0, 10, y + 15) - .setBackgroundTexture(GuiTextures.SLOT, GuiTextures.FILTER_SLOT_OVERLAY)); + .setBackgroundTexture(GuiTextures.SLOT, GuiTextures.FILTER_SLOT_OVERLAY)); this.filterWrapper.initUI(y + 15, widgetGroup); } @@ -61,13 +61,13 @@ protected void onFilterSlotChange(boolean notify) { ItemStack filterStack = filterInventory.getStackInSlot(0); FluidFilter newFluidFilter = FilterTypeRegistry.getFluidFilterForStack(filterStack); FluidFilter currentFluidFilter = filterWrapper.getFluidFilter(); - if(newFluidFilter == null) { - if(currentFluidFilter != null) { + if (newFluidFilter == null) { + if (currentFluidFilter != null) { filterWrapper.setFluidFilter(null); if (notify) filterWrapper.onFilterInstanceChange(); } } else if (currentFluidFilter == null || - newFluidFilter.getClass() != currentFluidFilter.getClass()) { + newFluidFilter.getClass() != currentFluidFilter.getClass()) { filterWrapper.setFluidFilter(newFluidFilter); if (notify) filterWrapper.onFilterInstanceChange(); } @@ -82,7 +82,7 @@ public NBTTagCompound serializeNBT() { NBTTagCompound tagCompound = new NBTTagCompound(); tagCompound.setTag("FilterInventory", filterInventory.serializeNBT()); tagCompound.setBoolean("IsBlacklist", filterWrapper.isBlacklistFilter()); - if(filterWrapper.getFluidFilter() != null) { + if (filterWrapper.getFluidFilter() != null) { NBTTagCompound filterInventory = new NBTTagCompound(); filterWrapper.getFluidFilter().writeToNBT(filterInventory); tagCompound.setTag("Filter", filterInventory); @@ -93,15 +93,15 @@ public NBTTagCompound serializeNBT() { @Override public void deserializeNBT(NBTTagCompound tagCompound) { //LEGACY SAVE FORMAT SUPPORT - if(tagCompound.hasKey("FilterTypeInventory")) { + if (tagCompound.hasKey("FilterTypeInventory")) { this.filterInventory.deserializeNBT(tagCompound.getCompoundTag("FilterTypeInventory")); } else { this.filterInventory.deserializeNBT(tagCompound.getCompoundTag("FilterInventory")); } this.filterWrapper.setBlacklistFilter(tagCompound.getBoolean("IsBlacklist")); - if(filterWrapper.getFluidFilter() != null) { + if (filterWrapper.getFluidFilter() != null) { //LEGACY SAVE FORMAT SUPPORT - if(tagCompound.hasKey("FluidFilter")) { + if (tagCompound.hasKey("FluidFilter")) { this.filterWrapper.getFluidFilter().readFromNBT(tagCompound); } else { NBTTagCompound filterInventory = tagCompound.getCompoundTag("Filter"); diff --git a/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java b/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java index 2f3fc9cf6f..ba86a2d538 100644 --- a/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java +++ b/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java @@ -19,6 +19,8 @@ import gregtech.api.metatileentity.TieredMetaTileEntity; import gregtech.api.render.Textures; import gregtech.api.util.GTUtility; +import gregtech.api.util.IDirtyNotifiable; +import gregtech.common.covers.filter.FluidFilterContainer; import net.minecraft.block.BlockLiquid; import net.minecraft.block.state.IBlockState; import net.minecraft.client.resources.I18n; @@ -48,7 +50,7 @@ import java.util.Deque; import java.util.List; -public class MetaTileEntityPump extends TieredMetaTileEntity { +public class MetaTileEntityPump extends TieredMetaTileEntity implements IDirtyNotifiable { private static final Cuboid6 PIPE_CUBOID = new Cuboid6(6 / 16.0, 0.0, 6 / 16.0, 10 / 16.0, 1.0, 10 / 16.0); private static final int BASE_PUMP_RANGE = 32; @@ -60,8 +62,11 @@ public class MetaTileEntityPump extends TieredMetaTileEntity { private boolean initializedQueue = false; private int pumpHeadY; + protected final FluidFilterContainer fluidFilter; + public MetaTileEntityPump(ResourceLocation metaTileEntityId, int tier) { super(metaTileEntityId, tier); + this.fluidFilter = new FluidFilterContainer(this); } @Override @@ -133,10 +138,14 @@ protected ModularUI createUI(EntityPlayer entityPlayer) { builder.image(7, 16, 81, 55, GuiTextures.DISPLAY); TankWidget tankWidget = new TankWidget(exportFluids.getTankAt(0), 69, 52, 18, 18) .setHideTooltip(true).setAlwaysShowFull(true); + builder.widget(tankWidget); builder.label(11, 20, "gregtech.gui.fluid_amount", 0xFFFFFF); builder.dynamicLabel(11, 30, tankWidget::getFormattedFluidAmount, 0xFFFFFF); builder.dynamicLabel(11, 40, tankWidget::getFluidLocalizedName, 0xFFFFFF); + + this.fluidFilter.initUI(88, builder::widget); + return builder.label(6, 6, getMetaFullName()) .widget(new FluidContainerSlotWidget(importItems, 0, 90, 17, false) .setBackgroundTexture(GuiTextures.SLOT, GuiTextures.IN_SLOT_OVERLAY)) @@ -243,7 +252,7 @@ private void tryPumpFirstBlock() { blockHere.getBlock() instanceof IFluidBlock) { IFluidHandler fluidHandler = FluidUtil.getFluidHandler(getWorld(), fluidBlockPos, null); FluidStack drainStack = fluidHandler.drain(Integer.MAX_VALUE, false); - if (drainStack != null && exportFluids.fill(drainStack, false) == drainStack.amount) { + if (drainStack != null && exportFluids.fill(drainStack, false) == drainStack.amount && this.fluidFilter.testFluidStack(drainStack)) { exportFluids.fill(drainStack, true); fluidHandler.drain(drainStack.amount, true); this.fluidSourceBlocks.remove(fluidBlockPos); @@ -297,4 +306,9 @@ public void addInformation(ItemStack stack, @Nullable World player, List tooltip.add(I18n.format("gregtech.universal.tooltip.energy_storage_capacity", energyContainer.getEnergyCapacity())); tooltip.add(I18n.format("gregtech.universal.tooltip.fluid_storage_capacity", exportFluids.getTankAt(0).getCapacity())); } + + @Override + public void markAsDirty() { + this.markDirty(); + } } From 302b831851178b97d5c0a1ea382aec9e6baff972 Mon Sep 17 00:00:00 2001 From: Galyfray Date: Tue, 23 Mar 2021 10:19:10 +0100 Subject: [PATCH 2/3] UI cleanup better queue management --- .../electric/MetaTileEntityPump.java | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java b/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java index ba86a2d538..8988e0579e 100644 --- a/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java +++ b/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java @@ -134,26 +134,26 @@ public T getCapability(Capability capability, EnumFacing side) { @Override protected ModularUI createUI(EntityPlayer entityPlayer) { - Builder builder = ModularUI.defaultBuilder(); + Builder builder = ModularUI.builder(GuiTextures.BACKGROUND, 176, 147 + 82); builder.image(7, 16, 81, 55, GuiTextures.DISPLAY); TankWidget tankWidget = new TankWidget(exportFluids.getTankAt(0), 69, 52, 18, 18) - .setHideTooltip(true).setAlwaysShowFull(true); + .setHideTooltip(true).setAlwaysShowFull(true); builder.widget(tankWidget); builder.label(11, 20, "gregtech.gui.fluid_amount", 0xFFFFFF); builder.dynamicLabel(11, 30, tankWidget::getFormattedFluidAmount, 0xFFFFFF); builder.dynamicLabel(11, 40, tankWidget::getFluidLocalizedName, 0xFFFFFF); - this.fluidFilter.initUI(88, builder::widget); + this.fluidFilter.initUI(75, builder::widget); return builder.label(6, 6, getMetaFullName()) - .widget(new FluidContainerSlotWidget(importItems, 0, 90, 17, false) - .setBackgroundTexture(GuiTextures.SLOT, GuiTextures.IN_SLOT_OVERLAY)) - .widget(new ImageWidget(91, 36, 14, 15, GuiTextures.TANK_ICON)) - .widget(new SlotWidget(exportItems, 0, 90, 54, true, false) - .setBackgroundTexture(GuiTextures.SLOT, GuiTextures.OUT_SLOT_OVERLAY)) - .bindPlayerInventory(entityPlayer.inventory) - .build(getHolder(), entityPlayer); + .widget(new FluidContainerSlotWidget(importItems, 0, 90, 17, false) + .setBackgroundTexture(GuiTextures.SLOT, GuiTextures.IN_SLOT_OVERLAY)) + .widget(new ImageWidget(91, 36, 14, 15, GuiTextures.TANK_ICON)) + .widget(new SlotWidget(exportItems, 0, 90, 54, true, false) + .setBackgroundTexture(GuiTextures.SLOT, GuiTextures.OUT_SLOT_OVERLAY)) + .bindPlayerInventory(entityPlayer.inventory, 147) + .build(getHolder(), entityPlayer); } private int getMaxPumpRange() { @@ -163,15 +163,15 @@ private int getMaxPumpRange() { private boolean isStraightInPumpRange(BlockPos checkPos) { BlockPos pos = getPos(); return checkPos.getX() == pos.getX() && - checkPos.getZ() == pos.getZ() && - pos.getY() < checkPos.getY() && - pos.getY() + pumpHeadY >= checkPos.getY(); + checkPos.getZ() == pos.getZ() && + pos.getY() < checkPos.getY() && + pos.getY() + pumpHeadY >= checkPos.getY(); } private void updateQueueState(int blocksToCheckAmount) { BlockPos selfPos = getPos().down(pumpHeadY); - for(int i = 0; i < blocksToCheckAmount; i++) { + for (int i = 0; i < blocksToCheckAmount; i++) { BlockPos checkPos = null; int amountIterated = 0; do { @@ -182,8 +182,8 @@ private void updateQueueState(int blocksToCheckAmount) { checkPos = blocksToCheck.poll(); } while (checkPos != null && - !getWorld().isBlockLoaded(checkPos) && - amountIterated < blocksToCheck.size()); + !getWorld().isBlockLoaded(checkPos) && + amountIterated < blocksToCheck.size()); if (checkPos != null) { checkFluidBlockAt(selfPos, checkPos); } else break; @@ -195,8 +195,8 @@ private void updateQueueState(int blocksToCheckAmount) { if (downPos != null && downPos.getY() >= 0) { IBlockState downBlock = getWorld().getBlockState(downPos); if (downBlock.getBlock() instanceof BlockLiquid || - downBlock.getBlock() instanceof IFluidBlock || - !downBlock.isTopSolid()) { + downBlock.getBlock() instanceof IFluidBlock || + !downBlock.isTopSolid()) { this.pumpHeadY++; } } @@ -221,10 +221,10 @@ private void checkFluidBlockAt(BlockPos pumpHeadPos, BlockPos checkPos) { boolean shouldCheckNeighbours = isStraightInPumpRange(checkPos); if (blockHere.getBlock() instanceof BlockLiquid || - blockHere.getBlock() instanceof IFluidBlock) { + blockHere.getBlock() instanceof IFluidBlock) { IFluidHandler fluidHandler = FluidUtil.getFluidHandler(getWorld(), checkPos, null); FluidStack drainStack = fluidHandler.drain(Integer.MAX_VALUE, false); - if (drainStack != null && drainStack.amount > 0) { + if (drainStack != null && drainStack.amount > 0 && this.fluidFilter.testFluidStack(drainStack)) { this.fluidSourceBlocks.add(checkPos); } shouldCheckNeighbours = true; @@ -237,7 +237,7 @@ private void checkFluidBlockAt(BlockPos pumpHeadPos, BlockPos checkPos) { if (offsetPos.distanceSq(pumpHeadPos) > maxPumpRange * maxPumpRange) continue; //do not add blocks outside bounds if (!fluidSourceBlocks.contains(offsetPos) && - !blocksToCheck.contains(offsetPos)) { + !blocksToCheck.contains(offsetPos)) { this.blocksToCheck.add(offsetPos); } } @@ -249,7 +249,7 @@ private void tryPumpFirstBlock() { if (fluidBlockPos == null) return; IBlockState blockHere = getWorld().getBlockState(fluidBlockPos); if (blockHere.getBlock() instanceof BlockLiquid || - blockHere.getBlock() instanceof IFluidBlock) { + blockHere.getBlock() instanceof IFluidBlock) { IFluidHandler fluidHandler = FluidUtil.getFluidHandler(getWorld(), fluidBlockPos, null); FluidStack drainStack = fluidHandler.drain(Integer.MAX_VALUE, false); if (drainStack != null && exportFluids.fill(drainStack, false) == drainStack.amount && this.fluidFilter.testFluidStack(drainStack)) { @@ -275,7 +275,7 @@ public void update() { fillContainerFromInternalTank(importItems, exportItems, 0, 0); updateQueueState(getTier()); if (getTimer() % getPumpingCycleLength() == 0 && !fluidSourceBlocks.isEmpty() && - energyContainer.getEnergyStored() >= GTValues.V[getTier()]) { + energyContainer.getEnergyStored() >= GTValues.V[getTier()]) { tryPumpFirstBlock(); } } @@ -288,6 +288,7 @@ private int getPumpingCycleLength() { public NBTTagCompound writeToNBT(NBTTagCompound data) { super.writeToNBT(data); data.setInteger("PumpHeadDepth", pumpHeadY); + data.setTag("Filter", fluidFilter.serializeNBT()); return data; } @@ -295,6 +296,11 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) { public void readFromNBT(NBTTagCompound data) { super.readFromNBT(data); this.pumpHeadY = data.getInteger("PumpHeadDepth"); + if (data.hasKey("FluidFilter")) { + this.fluidFilter.deserializeNBT(data); + } else { + this.fluidFilter.deserializeNBT(data.getCompoundTag("Filter")); + } } @Override @@ -309,6 +315,7 @@ public void addInformation(ItemStack stack, @Nullable World player, List @Override public void markAsDirty() { + while (fluidSourceBlocks.poll() != null); // Empty the queue when a filter is added, removed or switched from whitelist to blacklist this.markDirty(); } } From 820698afb663f7fb8d4827c8a09d5ce5c0f17ca7 Mon Sep 17 00:00:00 2001 From: Galyfray Date: Thu, 13 May 2021 13:36:14 +0200 Subject: [PATCH 3/3] filter drop deques get empty --- .../electric/MetaTileEntityPump.java | 99 ++++++++++++++----- 1 file changed, 73 insertions(+), 26 deletions(-) diff --git a/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java b/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java index 8988e0579e..f50187e6c4 100644 --- a/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java +++ b/src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityPump.java @@ -30,6 +30,7 @@ import net.minecraft.network.PacketBuffer; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing.Axis; +import net.minecraft.util.NonNullList; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -57,8 +58,9 @@ public class MetaTileEntityPump extends TieredMetaTileEntity implements IDirtyNo private static final int EXTRA_PUMP_RANGE = 8; private static final int PUMP_SPEED_BASE = 40; - private Deque fluidSourceBlocks = new ArrayDeque<>(); - private Deque blocksToCheck = new ArrayDeque<>(); + private final Deque fluidSourceBlocks = new ArrayDeque<>(); + private final Deque blocksToCheck = new ArrayDeque<>(); + private final Deque blocksToReCheck = new ArrayDeque<>(); private boolean initializedQueue = false; private int pumpHeadY; @@ -132,6 +134,12 @@ public T getCapability(Capability capability, EnumFacing side) { return (side == null || side.getAxis() != Axis.Y) ? super.getCapability(capability, side) : null; } + @Override + public void clearMachineInventory(NonNullList itemBuffer) { + super.clearMachineInventory(itemBuffer); + clearInventory(itemBuffer, this.fluidFilter.getFilterInventory()); + } + @Override protected ModularUI createUI(EntityPlayer entityPlayer) { Builder builder = ModularUI.builder(GuiTextures.BACKGROUND, 176, 147 + 82); @@ -168,25 +176,35 @@ private boolean isStraightInPumpRange(BlockPos checkPos) { pos.getY() + pumpHeadY >= checkPos.getY(); } + private BlockPos getValidPos(Deque posDeque) { + BlockPos checkPos = null; + int amountIterated = 0; + do { + if (checkPos != null) { + blocksToCheck.push(checkPos); + amountIterated++; + } + checkPos = blocksToCheck.poll(); + + } while (checkPos != null && + !getWorld().isBlockLoaded(checkPos) && + amountIterated < blocksToCheck.size()); + return checkPos; + } + private void updateQueueState(int blocksToCheckAmount) { BlockPos selfPos = getPos().down(pumpHeadY); for (int i = 0; i < blocksToCheckAmount; i++) { - BlockPos checkPos = null; - int amountIterated = 0; - do { - if (checkPos != null) { - blocksToCheck.push(checkPos); - amountIterated++; - } - checkPos = blocksToCheck.poll(); - - } while (checkPos != null && - !getWorld().isBlockLoaded(checkPos) && - amountIterated < blocksToCheck.size()); + BlockPos checkPos = getValidPos(blocksToCheck); if (checkPos != null) { checkFluidBlockAt(selfPos, checkPos); - } else break; + } else { + checkPos = getValidPos(blocksToReCheck); + if (checkPos != null) { + reCheckFluidBlockAt(selfPos, checkPos); + } else break; + } } if (fluidSourceBlocks.isEmpty()) { @@ -231,15 +249,43 @@ private void checkFluidBlockAt(BlockPos pumpHeadPos, BlockPos checkPos) { } if (shouldCheckNeighbours) { - int maxPumpRange = getMaxPumpRange(); - for (EnumFacing facing : EnumFacing.VALUES) { - BlockPos offsetPos = checkPos.offset(facing); - if (offsetPos.distanceSq(pumpHeadPos) > maxPumpRange * maxPumpRange) - continue; //do not add blocks outside bounds - if (!fluidSourceBlocks.contains(offsetPos) && - !blocksToCheck.contains(offsetPos)) { + checkNeighbours(pumpHeadPos, checkPos); + } + } + + private void reCheckFluidBlockAt(BlockPos pumpHeadPos, BlockPos checkPos) { + IBlockState blockHere = getWorld().getBlockState(checkPos); + boolean shouldCheckNeighbours = isStraightInPumpRange(checkPos); + + if (blockHere.getBlock() instanceof BlockLiquid || + blockHere.getBlock() instanceof IFluidBlock) { + IFluidHandler fluidHandler = FluidUtil.getFluidHandler(getWorld(), checkPos, null); + FluidStack drainStack = fluidHandler.drain(Integer.MAX_VALUE, false); + if (drainStack != null && drainStack.amount > 0 && this.fluidFilter.testFluidStack(drainStack)) { + this.fluidSourceBlocks.add(checkPos); + shouldCheckNeighbours = true; + } + } + + if (shouldCheckNeighbours) { + checkNeighbours(pumpHeadPos, checkPos); + } + } + + private void checkNeighbours(BlockPos pumpHeadPos, BlockPos checkPos) { + int maxPumpRange = getMaxPumpRange(); + for (EnumFacing facing : EnumFacing.VALUES) { + BlockPos offsetPos = checkPos.offset(facing); + if (offsetPos.distanceSq(pumpHeadPos) > maxPumpRange * maxPumpRange) + continue; //do not add blocks outside bounds + if (!fluidSourceBlocks.contains(offsetPos) && !blocksToCheck.contains(offsetPos) && !blocksToReCheck.contains(offsetPos)) { + + if (offsetPos.distanceSq(pumpHeadPos) > checkPos.distanceSq(pumpHeadPos)) { this.blocksToCheck.add(offsetPos); + } else { + this.blocksToReCheck.add(offsetPos); } + } } } @@ -296,9 +342,7 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) { public void readFromNBT(NBTTagCompound data) { super.readFromNBT(data); this.pumpHeadY = data.getInteger("PumpHeadDepth"); - if (data.hasKey("FluidFilter")) { - this.fluidFilter.deserializeNBT(data); - } else { + if (data.hasKey("Filter")) { this.fluidFilter.deserializeNBT(data.getCompoundTag("Filter")); } } @@ -315,7 +359,10 @@ public void addInformation(ItemStack stack, @Nullable World player, List @Override public void markAsDirty() { - while (fluidSourceBlocks.poll() != null); // Empty the queue when a filter is added, removed or switched from whitelist to blacklist + // Empty the different queues when a filter is added, removed or switched from whitelist to blacklist + while (fluidSourceBlocks.poll() != null) ; + while (blocksToReCheck.poll() != null) ; + while (blocksToCheck.poll() != null) ; this.markDirty(); } }