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

Create a Fluid Flow API #74

Open
wants to merge 22 commits into
base: 1.20
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cb9a9d7
Fluid Flow API
OroArmor Jan 30, 2022
d8cd6f7
Merge in 1.18
OroArmor Feb 18, 2022
4b4b788
Merge remote-tracking branch 'quilt/1.18' into fluid-flow-api
OroArmor Feb 23, 2022
f143c61
Fix some issues + actually merge
OroArmor Feb 23, 2022
7069a89
Apply suggestions from code review
OroArmor Jun 20, 2022
10329d7
Merge branch '1.19' into fluid-flow-api
OroArmor Jun 20, 2022
977d2ca
Remove direction event system and just use a parameter
OroArmor Jun 20, 2022
741414a
Merge branch '1.19' into fluid-flow-api
OroArmor Jun 20, 2022
9a16e65
Apply suggestions from code review
OroArmor Jun 20, 2022
871ef92
Merge branch '1.19' into fluid-flow-api
OroArmor Jun 23, 2022
5f14754
Make the test actually work
OroArmor Jun 23, 2022
ef005b3
Merge branch '1.19' into fluid-flow-api
OroArmor Jul 3, 2022
47a822e
Merge branch '1.19' into fluid-flow-api
OroArmor Aug 6, 2022
9ddf88c
Apply suggestions from code review
OroArmor Aug 6, 2022
a2b52d8
Update library/fluid/fluid_flow/src/main/java/org/quiltmc/qsl/fluid/f…
OroArmor Aug 6, 2022
f205fc5
Merge branch '1.19' into fluid-flow-api
OroArmor Aug 10, 2022
87db399
Merge branch '1.19' into fluid-flow-api
OroArmor Sep 7, 2022
1c4b207
Merge branch '1.19' into fluid-flow-api
OroArmor Oct 18, 2022
6f284b0
Merge branch '1.19' into fluid-flow-api
OroArmor Dec 17, 2022
b793511
Merge remote-tracking branch 'quilt/1.20' into fluid-flow-api
OroArmor Jun 6, 2023
a9b0480
Update to 1.20
OroArmor Jun 6, 2023
82c940c
Merge branch '1.20' into fluid-flow-api
EnnuiL Jun 9, 2023
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
8 changes: 8 additions & 0 deletions library/fluid/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
id("qsl.library")
}

qslLibrary {
libraryName = "fluid"
version = "1.0.0"
}
12 changes: 12 additions & 0 deletions library/fluid/fluid-flow/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id("qsl.module")
}

qslModule {
moduleName = "fluid-flow"
OroArmor marked this conversation as resolved.
Show resolved Hide resolved
version = "1.0.0"
library = "fluid"
coreDependencies([
"qsl_base"
])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2022 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.fluidflow.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jetbrains.annotations.Nullable;
import org.quiltmc.qsl.base.api.event.Event;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.util.Pair;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;


public final class FluidFlowEvents {
private FluidFlowEvents() {
}

// Flowing Block to Interacting Block to Direction to Event
private static final Map<Block, Map<Block, Map<Direction, Event<FluidFlowInteractionCallback>>>> EVENT_MAP = new HashMap<>();
OroArmor marked this conversation as resolved.
Show resolved Hide resolved

/**
* Registers a new event on a fluid flow. The same two blocks can register a different event for different directions, but the same directions will run the event that was registered first.
*
* @param flowingBlock The fluid block that flowed.
* @param interactionBlock The block in one of the {@code interactionDirections}.
* @param interactionDirections The direction to search for {@code interactionBlock}.
* @param interactionEvent The event to run when the conditions are met.
*/
public static void register(Block flowingBlock, Block interactionBlock, Direction[] interactionDirections, FluidFlowInteractionCallback interactionEvent) {
Map<Block, Map<Direction, Event<FluidFlowInteractionCallback>>> flowBlockEvents = EVENT_MAP.computeIfAbsent(flowingBlock, flowing -> new HashMap<>());
Map<Direction, Event<FluidFlowInteractionCallback>> interactionEvents = flowBlockEvents.computeIfAbsent(interactionBlock, interacting -> new HashMap<>());

// Create events for all the different directions for the specified blocks
if (interactionEvents.isEmpty()) {
for (Direction direction : Direction.values()) {
interactionEvents.put(direction, Event.create(FluidFlowInteractionCallback.class, fluidFlowInteractionEvents -> (flowingBlockState, interactingBlockState, flowPos, world) -> {
for (FluidFlowInteractionCallback event : fluidFlowInteractionEvents) {
if (!event.onFlow(flowingBlockState, interactingBlockState, flowPos, world)) {
return false;
}
}

return true;
}));
}
}

// Register the new event callbacks
for (Map.Entry<Direction, Event<FluidFlowInteractionCallback>> pair : interactionEvents.entrySet()) {
for (Direction direction : interactionDirections) {
if (pair.getKey() == direction) {
pair.getValue().register(interactionEvent);
}
}
}
}

/**
* Gets the event from the following blocks and direction.
*
* @param flowingBlock The fluid block that flowed.
* @param interactionBlock The block it interacts with.
* @param interactionDirection The interaction direction
* @return An event if the conditions are met, otherwise {@code null}
*/
public static @Nullable Event<FluidFlowInteractionCallback> getEvent(Block flowingBlock, Block interactionBlock, Direction interactionDirection) {
if (EVENT_MAP.containsKey(flowingBlock)) {
Map<Block, Map<Direction, Event<FluidFlowInteractionCallback>>> flowBlockEvents = EVENT_MAP.get(flowingBlock);

if (flowBlockEvents.containsKey(interactionBlock)) {
Map<Direction, Event<FluidFlowInteractionCallback>> interactionEvents = flowBlockEvents.get(interactionBlock);

for (Map.Entry<Direction, Event<FluidFlowInteractionCallback>> pair : interactionEvents.entrySet()) {
if (pair.getKey() == interactionDirection) {
return pair.getValue();
}
}
}
}

return null;
}

public interface FluidFlowInteractionCallback {

/**
* An event run when a fluid flows next to a block.
*
* @param flowingBlockState The block state of the fluid block.
* @param interactingBlockState The block state of the interacting block.
* @param flowPos The position in the world that the fluid flowed into.
* @param world The world the event took place in.
* @return {@code false} if the event was successful, and {@code true} if it was unsuccessful (don't blame us its minecraft that does this).
*/
boolean onFlow(BlockState flowingBlockState, BlockState interactingBlockState, BlockPos flowPos, World world);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2022 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.fluidflow.mixin;

import org.quiltmc.qsl.base.api.event.Event;
import org.quiltmc.qsl.fluidflow.api.FluidFlowEvents;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.FluidBlock;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;


@Mixin(FluidBlock.class)
public class FluidBlockMixin extends Block {
public FluidBlockMixin(Settings settings) {
super(settings);
}

@Inject(method = "receiveNeighborFluids", at = @At("HEAD"), cancellable = true)
private void receiveNeighborFluids(World world, BlockPos pos, BlockState state, CallbackInfoReturnable<Boolean> cir) {
for (Direction direction : Direction.values()) {
Event<FluidFlowEvents.FluidFlowInteractionCallback> event = FluidFlowEvents.getEvent(this, world.getBlockState(pos.offset(direction)).getBlock(), direction);

if (event != null) {
if (!event.invoker().onFlow(state, world.getBlockState(pos.offset(direction)), pos, world)) {
cir.setReturnValue(false);
return;
}
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions library/fluid/fluid-flow/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"schemaVersion": 1,
"id": "quilt_fluid_flow",
"name": "Quilt Tags API",
"version": "${version}",
"environment": "*",
"license": "Apache-2.0",
"icon": "assets/quilt_fluid_flow/icon.png",
"contact": {
"homepage": "https://quiltmc.org",
"issues": "https://github.com/QuiltMC/quilt-standard-libraries/issues",
"sources": "https://github.com/QuiltMC/quilt-standard-libraries"
},
"authors": [
"QuiltMC"
],
"depends": {
"fabricloader": ">=0.12",
"minecraft": ">=1.18.2-alpha.22.3.a"
},
"description": "Events for flowing fluids.",
"mixins": [
"quilt_fluid_flow.mixins.json"
],
"custom": {
"modmenu": {
"badges": [
"library"
],
"parent": {
"id": "qsl",
"name": "Quilt Standard Libraries",
"description": "A set of libraries to assist in making Quilt mods.",
"icon": "assets/quilt_fluid_flow/icon.png",
"badges": [
"library"
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"required": true,
"package": "org.quiltmc.qsl.fluidflow.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"FluidBlockMixin"
],
"client": [
],
"injectors": {
"defaultRequire": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.fluidflow;

import org.quiltmc.qsl.fluidflow.api.FluidFlowEvents;

import net.minecraft.block.Blocks;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.Direction;

import net.fabricmc.api.ModInitializer;

public class FluidFlowEventTests implements ModInitializer {
OroArmor marked this conversation as resolved.
Show resolved Hide resolved
@Override
public void onInitialize() {
FluidFlowEvents.register(Blocks.WATER, Blocks.BLUE_ICE, new Direction[]{Direction.DOWN}, (flowingBlockState, interactingBlockState, flowPos, world) -> {
world.setBlockState(flowPos, Blocks.ICE.getDefaultState());
world.playSound(null, flowPos, SoundEvents.BLOCK_GLASS_PLACE, SoundCategory.BLOCKS, 1, 1);
return false;
});
}
}
16 changes: 16 additions & 0 deletions library/fluid/fluid-flow/src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"schemaVersion": 1,
"id": "quilt_fluid_flow_testmod",
"name": "Quilt Fluid Flow API Test Mod",
"version": "1.0.0",
"environment": "*",
"license": "Apache-2.0",
"depends": {
"quilt_fluid_flow": "*"
},
"entrypoints": {
"main": [
"org.quiltmc.qsl.fluidflow.FluidFlowEventTests"
]
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ library("core")
library("block")
library("data")
library("item")
library("fluid")
library("command")

def library(String library) {
Expand Down