Maxson71 / BetterEndRod

This mod adds new types of end rod
Other
1 stars 0 forks source link

Make end rods waterloggable #1

Open solonovamax opened 2 weeks ago

solonovamax commented 2 weeks ago

Make it so end rods can be waterlogged.

Here's a mixin that does that, which I am releasing under the MIT license, if you want to use it.

/*
 * Copyright (c) 2024 solonovamax <solonovamax@12oclockpoint.com>
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

@Mixin(EndRodBlock.class)
public class EndRodBlockMixin extends RodBlock implements Waterloggable {
    public EndRodBlockMixin(Settings settings) {
        super(settings);
    }

    @ModifyArg(
            method = "<init>",
            at = @At(value = "INVOKE", target = "Lnet/minecraft/block/EndRodBlock;setDefaultState(Lnet/minecraft/block/BlockState;)V")
    )
    private BlockState modifyDefaultState(BlockState state) {
        return state.with(Properties.WATERLOGGED, Boolean.FALSE);
    }

    @Inject(
            method = "getPlacementState",
            at = @At("RETURN"),
            cancellable = true
    )
    private void getPlacementState(ItemPlacementContext ctx, CallbackInfoReturnable<BlockState> cir) {
        BlockPos blockPos = ctx.getBlockPos();
        FluidState fluidState = ctx.getWorld().getFluidState(blockPos);
        cir.setReturnValue(cir.getReturnValue().with(Properties.WATERLOGGED, fluidState.getFluid() == Fluids.WATER));
    }

    @Inject(
            method = "appendProperties",
            at = @At("TAIL")
    )
    private void appendProperties(StateManager.Builder<Block, BlockState> builder, CallbackInfo ci) {
        builder.add(Properties.WATERLOGGED);
    }

    @Override
    public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
        if (state.get(Properties.WATERLOGGED)) {
            world.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));
        }

        return super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos);
    }

    @Override
    public FluidState getFluidState(BlockState state) {
        return state.get(Properties.WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state);
    }
}
solonovamax commented 2 weeks ago

Was looking to see if there was some mod that added waterlogged end rods and stumbled across this mod, so felt I might as well just contribute the mixin that I wrote.

solonovamax commented 2 weeks ago

oopsies, just realized there was a small bug in the code. updated with the fixed version (replaced TAIL with RETURN in getPlacementState to properly handle placing an end rod against another one under water

Maxson71 commented 2 weeks ago

Thank you for the idea! I've updated the mod so that End Rod can be placed in water. https://modrinth.com/mod/better-end-rod