diff --git a/src/main/java/plus/dragons/createdragonlib/fluid/FluidLavaReaction.java b/src/main/java/plus/dragons/createdragonlib/fluid/FluidLavaReaction.java index 3a53d31..5635f30 100644 --- a/src/main/java/plus/dragons/createdragonlib/fluid/FluidLavaReaction.java +++ b/src/main/java/plus/dragons/createdragonlib/fluid/FluidLavaReaction.java @@ -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 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 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; + } + } diff --git a/src/main/java/plus/dragons/createdragonlib/mixin/FluidReactionsMixin.java b/src/main/java/plus/dragons/createdragonlib/mixin/FluidReactionsMixin.java index 60c10bf..16870db 100644 --- a/src/main/java/plus/dragons/createdragonlib/mixin/FluidReactionsMixin.java +++ b/src/main/java/plus/dragons/createdragonlib/mixin/FluidReactionsMixin.java @@ -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) diff --git a/src/main/java/plus/dragons/createdragonlib/mixin/LavaFluidMixin.java b/src/main/java/plus/dragons/createdragonlib/mixin/LavaFluidMixin.java new file mode 100644 index 0000000..6823589 --- /dev/null +++ b/src/main/java/plus/dragons/createdragonlib/mixin/LavaFluidMixin.java @@ -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(); + } + } + } +} diff --git a/src/main/java/plus/dragons/createdragonlib/mixin/LiquidBlockMixin.java b/src/main/java/plus/dragons/createdragonlib/mixin/LiquidBlockMixin.java new file mode 100644 index 0000000..9f001cb --- /dev/null +++ b/src/main/java/plus/dragons/createdragonlib/mixin/LiquidBlockMixin.java @@ -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 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); + } + } + } +} diff --git a/src/main/resources/create_dragon_lib.mixins.json b/src/main/resources/create_dragon_lib.mixins.json index ef836d2..8695455 100644 --- a/src/main/resources/create_dragon_lib.mixins.json +++ b/src/main/resources/create_dragon_lib.mixins.json @@ -9,7 +9,9 @@ "CreateAdvancementConstructor", "CreateAdvancementMixin", "CreateItemGroupBaseMixin", - "FluidReactionsMixin" + "FluidReactionsMixin", + "LavaFluidMixin", + "LiquidBlockMixin" ], "client": [], "injectors": {