Skip to content

Commit

Permalink
Mudlogging
Browse files Browse the repository at this point in the history
  • Loading branch information
f-raZ0R committed Oct 31, 2024
1 parent a4a69fe commit 3754993
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/main/java/de/dafuqs/spectrum/blocks/FluidLogging.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ default ItemStack tryDrainFluid(WorldAccess world, BlockPos pos, BlockState stat
}
return ItemStack.EMPTY;
}


//TODO: Remove this when we add custom fill sounds for our fluids.
@Override
default Optional<SoundEvent> getBucketFillSound() {
return Fluids.WATER.getBucketFillSound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

public class QuitoxicReedsBlock extends Block implements RevelationAware, FluidLogging.SpectrumFluidLoggable {

public static final EnumProperty<FluidLogging.State> LOGGED = FluidLogging.ANY_INCLUDING_NONE;
public static final EnumProperty<FluidLogging.State> LOGGED = FluidLogging.NONE_WATER_AND_CRYSTAL;
public static final IntProperty AGE = Properties.AGE_7;

// 'always drop' has no cloak and therefore drops normally even when broken 'via the world'
Expand Down Expand Up @@ -70,7 +70,47 @@ public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Ran
world.breakBlock(pos, true);
}
}


@Override
public boolean canFillWithFluid(BlockView world, BlockPos pos, BlockState state, Fluid fluid) {
return state.get(LOGGED) == FluidLogging.State.NOT_LOGGED && (fluid == Fluids.WATER || fluid == SpectrumFluids.LIQUID_CRYSTAL);
}
@Override
public boolean tryFillWithFluid(WorldAccess world, BlockPos pos, BlockState state, FluidState fluidState) {
if (state.get(LOGGED) == FluidLogging.State.NOT_LOGGED) {
if (!world.isClient()) {
if (fluidState.getFluid() == Fluids.WATER) {
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.WATER), Block.NOTIFY_ALL);
world.scheduleFluidTick(pos, fluidState.getFluid(), fluidState.getFluid().getTickRate(world));
} else if (fluidState.getFluid() == SpectrumFluids.LIQUID_CRYSTAL) {
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.LIQUID_CRYSTAL), Block.NOTIFY_ALL);
world.scheduleFluidTick(pos, fluidState.getFluid(), fluidState.getFluid().getTickRate(world));
}
}
return true;
} else {
return false;
}
}
@Override
public ItemStack tryDrainFluid(WorldAccess world, BlockPos pos, BlockState state) {
FluidLogging.State fluidLog = state.get(LOGGED);

if (fluidLog == FluidLogging.State.WATER) {
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.NOT_LOGGED), Block.NOTIFY_ALL);
if (!state.canPlaceAt(world, pos)) {
world.breakBlock(pos, true);
}
return new ItemStack(Items.WATER_BUCKET);
} else if (fluidLog == FluidLogging.State.LIQUID_CRYSTAL) {
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.NOT_LOGGED), Block.NOTIFY_ALL);
if (!state.canPlaceAt(world, pos)) {
world.breakBlock(pos, true);
}
return new ItemStack(SpectrumItems.LIQUID_CRYSTAL_BUCKET);
}
return ItemStack.EMPTY;
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import de.dafuqs.spectrum.blocks.FluidLogging;
import de.dafuqs.spectrum.registries.SpectrumBlocks;
import de.dafuqs.spectrum.registries.SpectrumFluids;
import de.dafuqs.spectrum.registries.SpectrumItems;
import net.minecraft.block.*;
import net.minecraft.entity.Entity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
Expand Down Expand Up @@ -36,7 +41,7 @@

@SuppressWarnings("deprecation")
public class DroopleafBlock extends HorizontalFacingBlock implements Fertilizable, FluidLogging.SpectrumFluidLoggable {
public static final EnumProperty<FluidLogging.State> LOGGED = FluidLogging.ANY_INCLUDING_NONE;
public static final EnumProperty<FluidLogging.State> LOGGED = FluidLogging.NONE_WATER_AND_MUD;
public static final BooleanProperty MUDDY = BooleanProperty.of("muddy");
private static final VoxelShape BASE_SHAPE = Block.createCuboidShape(0.0, 13.0, 0.0, 16.0, 15.0, 16.0);
private static final VoxelShape SHORT_SHAPE = Block.createCuboidShape(0.0, 5.0, 0.0, 16.0, 7.0, 16.0);
Expand All @@ -57,19 +62,20 @@ protected void appendProperties(StateManager.Builder<Block, BlockState> builder)
}
public BlockState getPlacementState(ItemPlacementContext ctx) {
BlockState blockState = ctx.getWorld().getBlockState(ctx.getBlockPos().down());
FluidState fluidState = ctx.getWorld().getFluidState(ctx.getBlockPos());
FluidLogging.State preFluidState = getForFluidState(ctx.getWorld().getFluidState(ctx.getBlockPos()));
FluidLogging.State fluidState = preFluidState!=FluidLogging.State.LIQUID_CRYSTAL ? preFluidState : FluidLogging.State.NOT_LOGGED;
if(blockState.isOf(this) || blockState.isOf(SpectrumBlocks.DROOPLEAF_STEM))
{
if(blockState.isOf(this) && !blockState.get(MUDDY))
{
ctx.getWorld().setBlockState(ctx.getBlockPos().down(), blockState.with(Properties.UNSTABLE, false), Block.NOTIFY_LISTENERS);
ctx.getWorld().scheduleBlockTick(ctx.getBlockPos().down(), blockState.getBlock(), 50);
}
return SpectrumBlocks.DROOPLEAF_STEM.getStateWithProperties(blockState).with(FluidLogging.ANY_INCLUDING_NONE, getForFluidState(fluidState));
return SpectrumBlocks.DROOPLEAF_STEM.getStateWithProperties(blockState).with(LOGGED, fluidState);
}
else
{
return this.getDefaultState().with(FluidLogging.ANY_INCLUDING_NONE, getForFluidState(fluidState)).with(FACING, ctx.getHorizontalPlayerFacing().getOpposite()).with(Properties.UNSTABLE, false).with(MUDDY, fluidState.getFluid() == SpectrumFluids.MUD);
return this.getDefaultState().with(LOGGED, fluidState).with(FACING, ctx.getHorizontalPlayerFacing().getOpposite()).with(Properties.UNSTABLE, false).with(MUDDY, fluidState == FluidLogging.State.MUD);
}
}
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
Expand All @@ -95,6 +101,60 @@ private static VoxelShape getShapeForState(BlockState state) {
}
return VoxelShapes.union(Block.createCuboidShape(0.0, 11.0, 0.0, 16.0, 15.0, 16.0),SHAPES_FOR_DIRECTION.get(state.get(FACING)));
}
@Override
public boolean canFillWithFluid(BlockView world, BlockPos pos, BlockState state, Fluid fluid) {
return state.get(LOGGED) == FluidLogging.State.NOT_LOGGED && (fluid == Fluids.WATER || fluid == SpectrumFluids.MUD);
}
@Override
public boolean tryFillWithFluid(WorldAccess world, BlockPos pos, BlockState state, FluidState fluidState) {
if (state.get(LOGGED) == FluidLogging.State.NOT_LOGGED) {
if (!world.isClient()) {
if (fluidState.getFluid() == Fluids.WATER) {
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.WATER), Block.NOTIFY_ALL);
world.scheduleFluidTick(pos, fluidState.getFluid(), fluidState.getFluid().getTickRate(world));
} else if (fluidState.getFluid() == SpectrumFluids.MUD) {
if (world.getBlockState(pos.down()).getBlock() != SpectrumBlocks.DROOPLEAF_STEM) {
world.setBlockState(pos, state.with(MUDDY, true).with(LOGGED, FluidLogging.State.MUD), Block.NOTIFY_ALL);
}
else{
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.MUD), Block.NOTIFY_ALL);
}
world.scheduleFluidTick(pos, fluidState.getFluid(), fluidState.getFluid().getTickRate(world));
}
}

return true;
} else {
return false;
}
}
@Override
public ItemStack tryDrainFluid(WorldAccess world, BlockPos pos, BlockState state) {
FluidLogging.State fluidLog = state.get(LOGGED);

if (fluidLog == FluidLogging.State.WATER) {
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.NOT_LOGGED), Block.NOTIFY_ALL);
if (!state.canPlaceAt(world, pos)) {
world.breakBlock(pos, true);
}
return new ItemStack(Items.WATER_BUCKET);
} else if (fluidLog == FluidLogging.State.MUD) {
if(world.getBlockState(pos.down()).getBlock() != SpectrumBlocks.DROOPLEAF_STEM)
{
world.setBlockState(pos, state.with(MUDDY, false).with(LOGGED, FluidLogging.State.NOT_LOGGED), Block.NOTIFY_ALL);
world.scheduleBlockTick(pos, this, 50);
}
else
{
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.NOT_LOGGED), Block.NOTIFY_ALL);
}
if (!state.canPlaceAt(world, pos)) {
world.breakBlock(pos, true);
}
return new ItemStack(SpectrumItems.MUD_BUCKET);
}
return ItemStack.EMPTY;
}
public boolean isFertilizable(WorldView world, BlockPos pos, BlockState state, boolean isClient) {
if(state.get(MUDDY))
{
Expand Down Expand Up @@ -129,7 +189,7 @@ protected static boolean canGrowInto(HeightLimitView world, BlockPos pos, BlockS
return !world.isOutOfHeightLimit(pos) && canGrowInto(state);
}
protected static void placeDroopleafAt(WorldAccess world, BlockPos pos, FluidLogging.State fluidState, Direction direction) {
BlockState blockState = SpectrumBlocks.DROOPLEAF.getDefaultState().with(FluidLogging.ANY_INCLUDING_NONE, fluidState).with(FACING, direction).with(Properties.UNSTABLE, false).with(Properties.SHORT,true);
BlockState blockState = SpectrumBlocks.DROOPLEAF.getDefaultState().with(LOGGED, fluidState).with(FACING, direction).with(Properties.UNSTABLE, false).with(Properties.SHORT,true);
world.scheduleBlockTick(pos, SpectrumBlocks.DROOPLEAF, 50);
world.setBlockState(pos, blockState, 3);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import de.dafuqs.spectrum.blocks.FluidLogging;
import de.dafuqs.spectrum.registries.SpectrumBlocks;
import de.dafuqs.spectrum.registries.SpectrumFluids;
import de.dafuqs.spectrum.registries.SpectrumItems;
import net.minecraft.block.*;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
Expand All @@ -22,7 +26,7 @@

@SuppressWarnings("deprecation")
public class DroopleafStemBlock extends HorizontalFacingBlock implements Fertilizable, FluidLogging.SpectrumFluidLoggable {
public static final EnumProperty<FluidLogging.State> LOGGED = FluidLogging.ANY_INCLUDING_NONE;
public static final EnumProperty<FluidLogging.State> LOGGED = FluidLogging.NONE_WATER_AND_MUD;
protected static final VoxelShape NORTH_SHAPE = Block.createCuboidShape(5.0, 0.0, 9.0, 11.0, 16.0, 15.0);
protected static final VoxelShape SOUTH_SHAPE = Block.createCuboidShape(5.0, 0.0, 1.0, 11.0, 16.0, 7.0);
protected static final VoxelShape EAST_SHAPE = Block.createCuboidShape(1.0, 0.0, 5.0, 7.0, 16.0, 11.0);
Expand Down Expand Up @@ -62,6 +66,67 @@ public boolean isFertilizable(WorldView world, BlockPos pos, BlockState state, b
return world.getBlockState(optional.get()).get(Properties.SHORT) || DroopleafBlock.canGrowInto(world, blockPos, blockState);
}
}
@Override
public boolean canFillWithFluid(BlockView world, BlockPos pos, BlockState state, Fluid fluid) {
return state.get(LOGGED) == FluidLogging.State.NOT_LOGGED && (fluid == Fluids.WATER || fluid == SpectrumFluids.MUD);
}
@Override
public boolean tryFillWithFluid(WorldAccess world, BlockPos pos, BlockState state, FluidState fluidState) {
if (state.get(LOGGED) == FluidLogging.State.NOT_LOGGED) {
if (!world.isClient()) {
if (fluidState.getFluid() == Fluids.WATER) {
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.WATER), Block.NOTIFY_ALL);
world.scheduleFluidTick(pos, fluidState.getFluid(), fluidState.getFluid().getTickRate(world));
} else if (fluidState.getFluid() == SpectrumFluids.MUD) {
Block soilBlock = world.getBlockState(pos.down()).getBlock();
if(soilBlock != SpectrumBlocks.DROOPLEAF_STEM && soilBlock != SpectrumBlocks.DROOPLEAF)
{
Optional<BlockPos> optional = BlockLocating.findColumnEnd(world, pos, state.getBlock(), Direction.UP, SpectrumBlocks.DROOPLEAF);
if(optional.isPresent())
{
world.setBlockState(optional.get(), world.getBlockState(optional.get()).with(DroopleafBlock.MUDDY, true), Block.NOTIFY_ALL);
}
}
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.MUD), Block.NOTIFY_ALL);
world.scheduleFluidTick(pos, fluidState.getFluid(), fluidState.getFluid().getTickRate(world));
}
}

return true;
} else {
return false;
}
}
@Override
public ItemStack tryDrainFluid(WorldAccess world, BlockPos pos, BlockState state) {
FluidLogging.State fluidLog = state.get(LOGGED);

if (fluidLog == FluidLogging.State.WATER) {
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.NOT_LOGGED), Block.NOTIFY_ALL);
if (!state.canPlaceAt(world, pos)) {
world.breakBlock(pos, true);
}
return new ItemStack(Items.WATER_BUCKET);
} else if (fluidLog == FluidLogging.State.MUD) {
Block soilBlock = world.getBlockState(pos.down()).getBlock();
if(soilBlock != SpectrumBlocks.DROOPLEAF_STEM && soilBlock != SpectrumBlocks.DROOPLEAF)
{
Optional<BlockPos> optional = BlockLocating.findColumnEnd(world, pos, state.getBlock(), Direction.UP, SpectrumBlocks.DROOPLEAF);
if(optional.isPresent())
{
world.setBlockState(optional.get(), world.getBlockState(optional.get()).with(DroopleafBlock.MUDDY, false), Block.NOTIFY_ALL);
world.scheduleBlockTick(optional.get(), SpectrumBlocks.DROOPLEAF, 50);

}
}
world.setBlockState(pos, state.with(LOGGED, FluidLogging.State.NOT_LOGGED), Block.NOTIFY_ALL);
if (!state.canPlaceAt(world, pos)) {
world.breakBlock(pos, true);
}
return new ItemStack(SpectrumItems.MUD_BUCKET);
}
return ItemStack.EMPTY;
}
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
BlockPos blockPos = pos.down();
BlockState blockState = world.getBlockState(blockPos);
Expand Down Expand Up @@ -96,7 +161,7 @@ public void grow(ServerWorld world, Random random, BlockPos pos, BlockState stat
BlockPos blockPos2 = blockPos.up();
if(world.getBlockState(blockPos).get(Properties.SHORT))
{
world.setBlockState(blockPos, state.with(Properties.SHORT, false), Block.NOTIFY_LISTENERS);
world.setBlockState(blockPos, world.getBlockState(blockPos).with(Properties.SHORT, false), Block.NOTIFY_LISTENERS);
world.scheduleBlockTick(blockPos, SpectrumBlocks.DROOPLEAF, 50);
}
else
Expand All @@ -108,7 +173,7 @@ public void grow(ServerWorld world, Random random, BlockPos pos, BlockState stat
}
}
protected static boolean placeStemAt(WorldAccess world, BlockPos pos, FluidLogging.State fluidState, Direction direction) {
BlockState blockState = SpectrumBlocks.DROOPLEAF_STEM.getDefaultState().with(FluidLogging.ANY_INCLUDING_NONE, fluidState).with(FACING, direction);
BlockState blockState = SpectrumBlocks.DROOPLEAF_STEM.getDefaultState().with(LOGGED, fluidState).with(FACING, direction);
return world.setBlockState(pos, blockState, 3);
}
public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) {
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/de/dafuqs/spectrum/mixin/BucketItemMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.dafuqs.spectrum.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import de.dafuqs.spectrum.blocks.FluidLogging;
import de.dafuqs.spectrum.mixin.accessors.BucketItemAccessor;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FlowableFluid;
import net.minecraft.item.BucketItem;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Debug(export = true)
@Mixin(BucketItem.class)
public class BucketItemMixin {
@Unique
BucketItemAccessor thisObject = (BucketItemAccessor) this;

@Inject(method = "placeFluid(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/hit/BlockHitResult;)Z",
at = @At(value="FIELD", target= "Lnet/minecraft/world/World;isClient:Z", ordinal=0), cancellable = true)
private void spectrum$BucketItemPlaceFluids(PlayerEntity player, World world, BlockPos pos, BlockHitResult hitResult, CallbackInfoReturnable<Boolean> cir)
{

Block block = world.getBlockState(pos).getBlock();
if (block instanceof FluidLogging.SpectrumFluidFillable && ((FluidLogging.SpectrumFluidFillable) block).canFillWithFluid(world,pos,world.getBlockState(pos),thisObject.getFluid())) {
((FluidLogging.SpectrumFluidFillable) block).tryFillWithFluid(world, pos, world.getBlockState(pos), ((FlowableFluid) thisObject.getFluid()).getStill(false));
thisObject.callPlayEmptyingSound(player, world, pos);
cir.setReturnValue(true);
}
}
@ModifyVariable(method = "use(Lnet/minecraft/world/World;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;)Lnet/minecraft/util/TypedActionResult;", at= @At(value="STORE", ordinal= 0), ordinal = 2)
private BlockPos spectrum$BucketItemPlacementPos(BlockPos blockPos3, World world, PlayerEntity user, Hand hand, @Local(ordinal=0) BlockPos blockPos)
{
BlockState blockState = world.getBlockState(blockPos);
BlockPos newPos = blockPos3;
if(blockPos3!=blockPos)
{
newPos = (blockState.getBlock() instanceof FluidLogging.SpectrumFluidFillable && ((FluidLogging.SpectrumFluidFillable) blockState.getBlock()).canFillWithFluid(world,blockPos,blockState,thisObject.getFluid())) ? blockPos : blockPos3;
}
return newPos;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Mixin(PlayerInventory.class)
public abstract class PlayerInventoryMixin {

@Inject(at = @At("HEAD"), method = "addStack(Lnet/minecraft/item/ItemStack;)I", cancellable = true)
@Inject(at = @At("HEAD"), method = "addStack(Lnet/minecraft/item/ItemStack;)I")
private void addStack(ItemStack stack, CallbackInfoReturnable<Integer> callbackInfoReturnable) {
PlayerInventory playerInventory = (PlayerInventory) (Object) this;

Expand Down
Loading

0 comments on commit 3754993

Please sign in to comment.