-
Notifications
You must be signed in to change notification settings - Fork 9
Fluid controllers #496
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
Open
Intybyte
wants to merge
13
commits into
master
Choose a base branch
from
vaan/feature/fluid-controllers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fluid controllers #496
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
77ef750
Add FluidLimiter code
Intybyte b634826
Add FluidAccumulator code
Intybyte f271249
Add name and lore
Intybyte fd8ad9d
Allow easier emptying on edit
Intybyte edb3ddc
Add recipes
Intybyte 4b9f1e9
Use translatables for gui
Intybyte 4d80768
Merge branch 'master' into vaan/feature/fluid-controllers
Intybyte a4708ab
Adapt to fluid changes
Intybyte cd324f9
Merge branch 'master' into vaan/feature/fluid-controllers
Intybyte 60adffc
Cleanup fluid accumulator
Intybyte 9dd6a9f
Cleanup lore
Intybyte 0073680
Use ItemStackBuilder
Intybyte 623966b
Use proper capacity
Intybyte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
164 changes: 164 additions & 0 deletions
164
src/main/java/io/github/pylonmc/pylon/base/content/machines/fluid/FluidAccumulator.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| package io.github.pylonmc.pylon.base.content.machines.fluid; | ||
|
|
||
| import io.github.pylonmc.pylon.base.BaseItems; | ||
| import io.github.pylonmc.pylon.base.content.machines.fluid.gui.IntRangeInventory; | ||
| import io.github.pylonmc.pylon.core.block.PylonBlock; | ||
| import io.github.pylonmc.pylon.core.block.base.PylonDirectionalBlock; | ||
| import io.github.pylonmc.pylon.core.block.base.PylonFluidTank; | ||
| import io.github.pylonmc.pylon.core.block.base.PylonInteractBlock; | ||
| import io.github.pylonmc.pylon.core.block.context.BlockCreateContext; | ||
| import io.github.pylonmc.pylon.core.config.adapter.ConfigAdapter; | ||
| import io.github.pylonmc.pylon.core.datatypes.PylonSerializers; | ||
| import io.github.pylonmc.pylon.core.entity.display.ItemDisplayBuilder; | ||
| import io.github.pylonmc.pylon.core.entity.display.transform.TransformBuilder; | ||
| import io.github.pylonmc.pylon.core.fluid.FluidPointType; | ||
| import io.github.pylonmc.pylon.core.fluid.PylonFluid; | ||
| import io.github.pylonmc.pylon.core.item.builder.ItemStackBuilder; | ||
| import net.kyori.adventure.text.Component; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.NamespacedKey; | ||
| import org.bukkit.block.Block; | ||
| import org.bukkit.block.BlockFace; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.Event; | ||
| import org.bukkit.event.player.PlayerInteractEvent; | ||
| import org.bukkit.inventory.EquipmentSlot; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.persistence.PersistentDataContainer; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import xyz.xenondevs.inventoryaccess.component.AdventureComponentWrapper; | ||
| import xyz.xenondevs.invui.window.Window; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import static io.github.pylonmc.pylon.base.util.BaseUtils.baseKey; | ||
|
|
||
| public class FluidAccumulator extends PylonBlock implements PylonDirectionalBlock, PylonFluidTank, PylonInteractBlock { | ||
|
|
||
| public final ItemStackBuilder mainStack = ItemStackBuilder.of(Material.WHITE_CONCRETE) | ||
| .addCustomModelDataString(getKey() + ":main"); | ||
| public final ItemStackBuilder noFluidStack = ItemStackBuilder.of(Material.RED_CONCRETE) | ||
| .addCustomModelDataString(getKey() + ":fluid:none"); | ||
|
|
||
| public final int buffer = getSettings().getOrThrow("buffer", ConfigAdapter.INT); | ||
| public final IntRangeInventory regulator; | ||
|
|
||
| // when true, starts the output and stops the input, opposite when false | ||
| private boolean outputReady; | ||
|
|
||
| public static final NamespacedKey AMOUNT_KEY = baseKey("amount"); | ||
| public static final NamespacedKey OUTPUT_READY_KEY = baseKey("output_ready"); | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public FluidAccumulator(@NotNull Block block, @NotNull BlockCreateContext context) { | ||
| super(block); | ||
| this.regulator = new IntRangeInventory( | ||
| BaseItems.FLUID_ACCUMULATOR, | ||
| () -> buffer, | ||
| Component.translatable("pylon.pylonbase.item.fluid_accumulator.gui.add"), | ||
| Component.translatable("pylon.pylonbase.item.fluid_accumulator.gui.dec"), | ||
| Component.translatable("pylon.pylonbase.item.fluid_accumulator.gui.amount") | ||
| ); | ||
| this.outputReady = false; | ||
| setCapacity(buffer); | ||
|
|
||
| Player player = ((BlockCreateContext.PlayerPlace) context).getPlayer(); | ||
|
|
||
| // fatty filter for now todo: add a proper unique display | ||
| addEntity("main", new ItemDisplayBuilder() | ||
| .itemStack(mainStack) | ||
| .transformation(new TransformBuilder() | ||
| .lookAlong(getFacing()) | ||
| .scale(0.5, 0.25, 0.5) | ||
| ) | ||
| .build(block.getLocation().toCenterLocation()) | ||
| ); | ||
| addEntity("fluid", new ItemDisplayBuilder() | ||
| .itemStack(noFluidStack) | ||
| .transformation(new TransformBuilder() | ||
| .lookAlong(getFacing()) | ||
| .scale(0.3, 0.3, 0.3) | ||
| ) | ||
| .build(block.getLocation().toCenterLocation()) | ||
| ); | ||
| createFluidPoint(FluidPointType.INPUT, BlockFace.EAST, context, false, 0.3F); | ||
| createFluidPoint(FluidPointType.OUTPUT, BlockFace.WEST, context, false, 0.3F); | ||
| setDisableBlockTextureEntity(true); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public FluidAccumulator(@NotNull Block block, @NotNull PersistentDataContainer pdc) { | ||
| super(block); | ||
| this.regulator = new IntRangeInventory( | ||
| BaseItems.FLUID_ACCUMULATOR, | ||
| () -> buffer, | ||
| pdc.get(AMOUNT_KEY, PylonSerializers.INTEGER), | ||
| Component.translatable("pylon.pylonbase.item.fluid_accumulator.gui.add"), | ||
| Component.translatable("pylon.pylonbase.item.fluid_accumulator.gui.dec"), | ||
| Component.translatable("pylon.pylonbase.item.fluid_accumulator.gui.amount") | ||
| ); | ||
| this.outputReady = pdc.get(OUTPUT_READY_KEY, PylonSerializers.BOOLEAN); | ||
| setCapacity(regulator.getAmount()); | ||
| setDisableBlockTextureEntity(true); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(@NotNull PersistentDataContainer pdc) { | ||
| pdc.set(AMOUNT_KEY, PylonSerializers.INTEGER, regulator.getAmount()); | ||
| pdc.set(OUTPUT_READY_KEY, PylonSerializers.BOOLEAN, outputReady); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isAllowedFluid(@NotNull PylonFluid fluid) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void onInteract(PlayerInteractEvent event) { | ||
| if (!event.getAction().isRightClick() | ||
| || event.getPlayer().isSneaking() | ||
| || event.getHand() != EquipmentSlot.HAND | ||
| || event.useInteractedBlock() == Event.Result.DENY) { | ||
| return; | ||
| } | ||
|
|
||
| event.setUseInteractedBlock(Event.Result.DENY); | ||
| event.setUseItemInHand(Event.Result.DENY); | ||
|
|
||
| Window.single() | ||
| .setGui(regulator.makeGui()) | ||
| .setTitle(new AdventureComponentWrapper(getNameTranslationKey())) | ||
| .setViewer(event.getPlayer()) | ||
| .addCloseHandler(() -> setCapacity(regulator.getAmount())) | ||
| .build() | ||
| .open(); | ||
| } | ||
|
|
||
| @Override | ||
| public double fluidAmountRequested(@NotNull PylonFluid fluid) { | ||
| if (getBlock().isBlockIndirectlyPowered()) return 0.0; | ||
|
|
||
| if (getFluidAmount() == 0.0) { | ||
| outputReady = false; | ||
| return PylonFluidTank.super.fluidAmountRequested(fluid); | ||
| } | ||
|
|
||
| if (outputReady) return 0.0; | ||
|
|
||
| return PylonFluidTank.super.fluidAmountRequested(fluid); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull Map<@NotNull PylonFluid, @NotNull Double> getSuppliedFluids() { | ||
| if (getBlock().isBlockIndirectlyPowered()) return PylonFluidTank.super.getSuppliedFluids(); | ||
|
|
||
| if (getFluidSpaceRemaining() == 0.0) { | ||
| outputReady = true; | ||
| return PylonFluidTank.super.getSuppliedFluids(); | ||
| } | ||
|
|
||
| if (outputReady) return PylonFluidTank.super.getSuppliedFluids(); | ||
|
|
||
| return Map.of(); | ||
| } | ||
| } | ||
157 changes: 157 additions & 0 deletions
157
src/main/java/io/github/pylonmc/pylon/base/content/machines/fluid/FluidLimiter.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| package io.github.pylonmc.pylon.base.content.machines.fluid; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import io.github.pylonmc.pylon.base.BaseItems; | ||
| import io.github.pylonmc.pylon.base.content.machines.fluid.gui.IntRangeInventory; | ||
| import io.github.pylonmc.pylon.core.block.PylonBlock; | ||
| import io.github.pylonmc.pylon.core.block.base.PylonDirectionalBlock; | ||
| import io.github.pylonmc.pylon.core.block.base.PylonFluidTank; | ||
| import io.github.pylonmc.pylon.core.block.base.PylonInteractBlock; | ||
| import io.github.pylonmc.pylon.core.block.context.BlockCreateContext; | ||
| import io.github.pylonmc.pylon.core.datatypes.PylonSerializers; | ||
| import io.github.pylonmc.pylon.core.entity.display.ItemDisplayBuilder; | ||
| import io.github.pylonmc.pylon.core.entity.display.transform.TransformBuilder; | ||
| import io.github.pylonmc.pylon.core.fluid.FluidManager; | ||
| import io.github.pylonmc.pylon.core.fluid.FluidPointType; | ||
| import io.github.pylonmc.pylon.core.fluid.PylonFluid; | ||
| import io.github.pylonmc.pylon.core.item.builder.ItemStackBuilder; | ||
| import io.github.pylonmc.pylon.core.util.PylonUtils; | ||
| import net.kyori.adventure.text.Component; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.NamespacedKey; | ||
| import org.bukkit.block.Block; | ||
| import org.bukkit.block.BlockFace; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.Event; | ||
| import org.bukkit.event.player.PlayerInteractEvent; | ||
| import org.bukkit.inventory.EquipmentSlot; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.persistence.PersistentDataContainer; | ||
| import org.bukkit.persistence.PersistentDataType; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import xyz.xenondevs.inventoryaccess.component.AdventureComponentWrapper; | ||
| import xyz.xenondevs.invui.window.Window; | ||
|
|
||
| import static io.github.pylonmc.pylon.base.util.BaseUtils.baseKey; | ||
|
|
||
| public class FluidLimiter extends PylonBlock implements PylonDirectionalBlock, PylonFluidTank, PylonInteractBlock { | ||
|
|
||
| public final ItemStackBuilder mainStack = ItemStackBuilder.of(Material.WHITE_CONCRETE) | ||
| .addCustomModelDataString(getKey() + ":main"); | ||
| public final ItemStackBuilder noFluidStack = ItemStackBuilder.of(Material.RED_CONCRETE) | ||
| .addCustomModelDataString(getKey() + ":fluid:none"); | ||
|
|
||
| public final IntRangeInventory regulator; | ||
|
|
||
| private static final NamespacedKey AMOUNT_KEY = baseKey("amount"); | ||
| public static final NamespacedKey REAL_MAX_CAPACITY = baseKey("real_max_capacity"); | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public FluidLimiter(@NotNull Block block, @NotNull BlockCreateContext context) { | ||
| super(block); | ||
| this.regulator = new IntRangeInventory( | ||
| BaseItems.FLUID_LIMITER, | ||
| this::getPipeMax, | ||
| Component.translatable("pylon.pylonbase.item.fluid_limiter.gui.add"), | ||
| Component.translatable("pylon.pylonbase.item.fluid_limiter.gui.dec"), | ||
| Component.translatable("pylon.pylonbase.item.fluid_limiter.gui.amount") | ||
| ); | ||
|
|
||
| // a bit of a hack - treat capacity as effectively infinite and override | ||
| // fluidAmountRequested to control how much fluid comes in | ||
| setCapacity(1000); | ||
|
|
||
| Preconditions.checkState(context instanceof BlockCreateContext.PlayerPlace, "Fluid filter can only be placed by a player"); | ||
| Player player = ((BlockCreateContext.PlayerPlace) context).getPlayer(); | ||
|
|
||
| // fatty filter for now todo: add a proper unique display | ||
| addEntity("main", new ItemDisplayBuilder() | ||
| .itemStack(mainStack) | ||
| .transformation(new TransformBuilder() | ||
| .lookAlong(getFacing()) | ||
| .scale(0.4, 0.25, 0.5) | ||
| ) | ||
| .build(block.getLocation().toCenterLocation()) | ||
| ); | ||
| addEntity("fluid", new ItemDisplayBuilder() | ||
| .itemStack(noFluidStack) | ||
| .transformation(new TransformBuilder() | ||
| .lookAlong(getFacing()) | ||
| .scale(0.2, 0.3, 0.45) | ||
| ) | ||
| .build(block.getLocation().toCenterLocation()) | ||
| ); | ||
| createFluidPoint(FluidPointType.INPUT, BlockFace.EAST, context, false, 0.25F); | ||
| createFluidPoint(FluidPointType.OUTPUT, BlockFace.WEST, context, false, 0.25F); | ||
| setDisableBlockTextureEntity(true); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public FluidLimiter(@NotNull Block block, @NotNull PersistentDataContainer pdc) { | ||
| super(block); | ||
| this.regulator = new IntRangeInventory( | ||
| BaseItems.FLUID_LIMITER, | ||
| this::getPipeMax, | ||
| pdc.get(AMOUNT_KEY, PylonSerializers.INTEGER), | ||
| Component.translatable("pylon.pylonbase.item.fluid_limiter.gui.add"), | ||
| Component.translatable("pylon.pylonbase.item.fluid_limiter.gui.dec"), | ||
| Component.translatable("pylon.pylonbase.item.fluid_limiter.gui.amount") | ||
| ); | ||
| setDisableBlockTextureEntity(true); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(@NotNull PersistentDataContainer pdc) { | ||
| pdc.set(AMOUNT_KEY, PylonSerializers.INTEGER, regulator.getAmount()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isAllowedFluid(@NotNull PylonFluid fluid) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void onInteract(PlayerInteractEvent event) { | ||
| if (!event.getAction().isRightClick() | ||
| || event.getPlayer().isSneaking() | ||
| || event.getHand() != EquipmentSlot.HAND | ||
| || event.useInteractedBlock() == Event.Result.DENY) { | ||
| return; | ||
| } | ||
|
|
||
| event.setUseInteractedBlock(Event.Result.DENY); | ||
| event.setUseItemInHand(Event.Result.DENY); | ||
|
|
||
| Window.single() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use PylonGuiBlock
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same reason as above |
||
| .setGui(regulator.makeGui()) | ||
| .setTitle(new AdventureComponentWrapper(getNameTranslationKey())) | ||
| .setViewer(event.getPlayer()) | ||
| .addCloseHandler(this::updateFluidPerSecond) | ||
| .build() | ||
| .open(); | ||
| } | ||
|
|
||
| private void updateFluidPerSecond() { | ||
| var output = getFluidPointDisplayOrThrow(FluidPointType.OUTPUT); | ||
| var segment = output.getPoint().getSegment(); | ||
| FluidManager.setFluidPerSecond(segment, regulator.getAmount()); | ||
| } | ||
|
|
||
| private int getPipeMax() { | ||
| return Double.valueOf(getPipeMaxDouble()).intValue(); | ||
| } | ||
|
|
||
| private double getPipeMaxDouble() { | ||
| var output = getFluidPointDisplayOrThrow(FluidPointType.OUTPUT); | ||
|
|
||
| PersistentDataContainer pdc = output.getEntity().getPersistentDataContainer(); | ||
| Double realCapacity = pdc.get(REAL_MAX_CAPACITY, PylonSerializers.DOUBLE); | ||
| if (realCapacity == null) { | ||
| double original = FluidManager.getFluidPerSecond(output.getPoint().getSegment()); | ||
| pdc.set(REAL_MAX_CAPACITY, PersistentDataType.DOUBLE, original); | ||
| return original; | ||
| } else { | ||
| return realCapacity; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use PylonGuiBlock here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no because it doesn't allow me to add close/open handlers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You won't need them after swapping to FluidThresholdButton as I mentioned in my next comment