Skip to content

Commit

Permalink
impl fluidlavainteraction
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Dec 2, 2022
1 parent e52702e commit 57e097a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
package plus.dragons.createdragonlib.fluid;

import java.util.IdentityHashMap;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FlowingFluid;

import org.jetbrains.annotations.Nullable;

import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.minecraft.world.level.block.state.BlockState;
import java.util.IdentityHashMap;

@SuppressWarnings("UnstableApiUsage")
public record FluidLavaReaction(BlockState withLava, BlockState withFlowingLava, BlockState lavaOnSelf) {

private static final IdentityHashMap<FluidVariant, FluidLavaReaction> REACTIONS = new IdentityHashMap<>();

public static void register(FluidVariant type, BlockState withLava, BlockState withFlowingLava, BlockState lavaOnSelf) {
// TODO
// FluidInteractionRegistry.addInteraction(ForgeMod.LAVA_TYPE.get(), new FluidInteractionRegistry.InteractionInformation(
// type, fluidState -> fluidState.isSource() ? withLava : withFlowingLava
// ));
// FluidInteractionRegistry.addInteraction(type, new FluidInteractionRegistry.InteractionInformation(
// ForgeMod.LAVA_TYPE.get(), lavaOnSelf
// ));
REACTIONS.put(type, new FluidLavaReaction(withLava, withFlowingLava, lavaOnSelf));
}

@Nullable
public static FluidLavaReaction get(FluidVariant fluid) {
return REACTIONS.get(fluid);
}


private static final IdentityHashMap<FluidVariant, FluidLavaReaction> REACTIONS = new IdentityHashMap<>();

public static void register(FluidVariant type, BlockState withLava, BlockState withFlowingLava, BlockState lavaOnSelf) {
REACTIONS.put(type, new FluidLavaReaction(withLava, withFlowingLava, lavaOnSelf));
}

@Nullable
public static FluidLavaReaction get(FlowingFluid fluid) {
return REACTIONS.keySet().stream().anyMatch(variant -> variant.isOf(fluid.getSource())) ? REACTIONS.entrySet().stream().filter(p -> p.getKey().isOf(fluid.getSource())).findFirst().get().getValue() : null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import plus.dragons.createdragonlib.fluid.FluidLavaReaction;

@Mixin(value = FluidReactions.class, remap = false)
@SuppressWarnings("UnstableApiUsage")
public class FluidReactionsMixin {

@Inject(method = "handlePipeFlowCollision", at = @At("HEAD"), cancellable = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package plus.dragons.createdragonlib.mixin;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FlowingFluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.LavaFluid;

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.CallbackInfo;
import plus.dragons.createdragonlib.fluid.FluidLavaReaction;

@Mixin(LavaFluid.class)
public class LavaFluidMixin {
@Inject(method = "spreadTo", at = @At("HEAD"), cancellable = true)
private void spread(LevelAccessor levelAccessor, BlockPos blockPos, BlockState blockState, Direction direction, FluidState fluidState, CallbackInfo ci) {
if (direction == Direction.DOWN) {
FluidState fluidState2 = levelAccessor.getFluidState(blockPos);
if (fluidState.is(FluidTags.LAVA) && fluidState2.getType() instanceof FlowingFluid flow) {
FluidLavaReaction reaction = FluidLavaReaction.get(flow);
if (reaction == null) return;
if (blockState.getBlock() instanceof LiquidBlock)
levelAccessor.setBlock(blockPos, reaction.lavaOnSelf(), 3);
levelAccessor.levelEvent(1501, blockPos, 0);
ci.cancel();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package plus.dragons.createdragonlib.mixin;

import static net.minecraft.world.level.block.LiquidBlock.POSSIBLE_FLOW_DIRECTIONS;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FlowingFluid;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import plus.dragons.createdragonlib.fluid.FluidLavaReaction;

@Mixin(LiquidBlock.class)
public class LiquidBlockMixin {
@Shadow
@Final
protected FlowingFluid fluid;

@Inject(method = "shouldSpreadLiquid", at = @At("HEAD"), cancellable = true)
@SuppressWarnings("deprecation")
private void spread(Level level, BlockPos blockPos, BlockState blockState, CallbackInfoReturnable<Boolean> cir) {
if (this.fluid.is(FluidTags.LAVA))
for (Direction direction : POSSIBLE_FLOW_DIRECTIONS) {
BlockPos blockPos2 = blockPos.relative(direction.getOpposite());
FluidLavaReaction reaction = level.getFluidState(blockPos2).getType() instanceof FlowingFluid flow ? FluidLavaReaction.get(flow) : null;
if (reaction != null) {
BlockState state = level.getFluidState(blockPos).isSource() ? reaction.withLava() : reaction.withFlowingLava();
level.setBlockAndUpdate(blockPos, state);
level.levelEvent(1501, blockPos, 0);
cir.setReturnValue(false);
}
}
}
}
4 changes: 3 additions & 1 deletion src/main/resources/create_dragon_lib.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"CreateAdvancementConstructor",
"CreateAdvancementMixin",
"CreateItemGroupBaseMixin",
"FluidReactionsMixin"
"FluidReactionsMixin",
"LavaFluidMixin",
"LiquidBlockMixin"
],
"client": [],
"injectors": {
Expand Down

0 comments on commit 57e097a

Please sign in to comment.