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

Filter in pumps #1546

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -53,21 +53,21 @@ public ItemStackHandler getFilterInventory() {
public void initUI(int y, Consumer<Widget> 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);
}

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();
}
Expand All @@ -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);
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +50,7 @@
import java.util.Deque;
import java.util.List;

public class MetaTileEntityPump extends TieredMetaTileEntity {
public class MetaTileEntityPump extends TieredMetaTileEntity implements IDirtyNotifiable {
galyfray marked this conversation as resolved.
Show resolved Hide resolved

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;
Expand All @@ -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
Expand Down Expand Up @@ -129,22 +134,26 @@ public <T> T getCapability(Capability<T> 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(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() {
Expand All @@ -154,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 {
Expand All @@ -173,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;
Expand All @@ -186,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++;
}
}
Expand All @@ -212,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);
}
galyfray marked this conversation as resolved.
Show resolved Hide resolved
shouldCheckNeighbours = true;
Expand All @@ -228,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);
}
}
Expand All @@ -240,10 +249,10 @@ 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) {
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);
Expand All @@ -266,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();
}
}
Expand All @@ -279,13 +288,19 @@ private int getPumpingCycleLength() {
public NBTTagCompound writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
data.setInteger("PumpHeadDepth", pumpHeadY);
data.setTag("Filter", fluidFilter.serializeNBT());
return data;
}

@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
this.pumpHeadY = data.getInteger("PumpHeadDepth");
if (data.hasKey("FluidFilter")) {
this.fluidFilter.deserializeNBT(data);
} else {
galyfray marked this conversation as resolved.
Show resolved Hide resolved
this.fluidFilter.deserializeNBT(data.getCompoundTag("Filter"));
galyfray marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Override
Expand All @@ -297,4 +312,10 @@ public void addInformation(ItemStack stack, @Nullable World player, List<String>
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() {
galyfray marked this conversation as resolved.
Show resolved Hide resolved
while (fluidSourceBlocks.poll() != null); // Empty the queue when a filter is added, removed or switched from whitelist to blacklist
this.markDirty();
}
}