Skip to content

Commit

Permalink
update molten lead spread mechanic
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Sep 24, 2024
1 parent 4a7b19e commit 45d17f0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 107 deletions.
126 changes: 48 additions & 78 deletions src/main/java/galena/oreganized/content/block/MoltenLeadBlock.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
package galena.oreganized.content.block;

import galena.oreganized.OreganizedConfig;
import galena.oreganized.index.OBlocks;
import galena.oreganized.index.OEffects;
import galena.oreganized.index.OItems;
import galena.oreganized.index.OTags;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.tags.BlockTags;
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.RandomSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
Expand All @@ -48,7 +39,7 @@
@MethodsReturnNonnullByDefault
public class MoltenLeadBlock extends LiquidBlock {

private static final BooleanProperty MOVING = BooleanProperty.create( "moving" );
public static final BooleanProperty MOVING = BooleanProperty.create("moving");

public static final VoxelShape STABLE_SHAPE = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D);

Expand All @@ -58,74 +49,37 @@ public MoltenLeadBlock(Supplier<? extends FlowingFluid> fluid, Properties proper
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder <Block, BlockState> builder) {
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
super.createBlockStateDefinition(builder);
builder.add(MOVING);
}

@Override
public BlockState updateShape(BlockState pState, Direction pDirection, BlockState neighbour, LevelAccessor pLevel, BlockPos pPos, BlockPos neighbourPos) {
if(pDirection == Direction.DOWN){
pLevel.scheduleTick( pPos , this , 30 );
}
return super.updateShape( pState , pDirection , neighbour , pLevel , pPos , neighbourPos );
}

@Nullable
@Override
public BlockPathTypes getBlockPathType(BlockState state, BlockGetter world, BlockPos pos, @Nullable Mob entity) {
return BlockPathTypes.WALKABLE;
}

@Override
public boolean canEntityDestroy( BlockState state , BlockGetter world , BlockPos pos , Entity entity ){
return false;
}

@Override
public void onPlace( BlockState pState , Level pLevel , BlockPos pPos , BlockState pOldState , boolean pIsMoving ){
if (pIsMoving){
if(!pOldState.getFluidState().is( FluidTags.WATER )){
scheduleFallingTick( pLevel , pPos , 30 );
pLevel.setBlock( pPos , pState.setValue( MOVING , true ) , 3 );
}else{
pLevel.levelEvent( 1501 , pPos , 0 );
pLevel.setBlock( pPos , OBlocks.LEAD_BLOCK.get().defaultBlockState() , 3 );
public void onPlace(BlockState pState, Level pLevel, BlockPos pPos, BlockState pOldState, boolean pIsMoving) {
if (pIsMoving) {
if (!pOldState.getFluidState().is(FluidTags.WATER)) {
pLevel.setBlock(pPos, pState.setValue(MOVING, true), 3);
} else {
pLevel.levelEvent(1501, pPos, 0);
pLevel.setBlock(pPos, OBlocks.LEAD_BLOCK.get().defaultBlockState(), 3);
}
} else {
if(!pOldState.getFluidState().is( FluidTags.WATER )){
pLevel.setBlock( pPos , pState.setValue( MOVING , false ) , 3 );
pLevel.scheduleTick( pPos , this , 300 );
}else{
pLevel.levelEvent( 1501 , pPos , 0 );
pLevel.setBlock( pPos , OBlocks.LEAD_BLOCK.get().defaultBlockState() , 3 );
if (!pOldState.getFluidState().is(FluidTags.WATER)) {
pLevel.setBlock(pPos, pState.setValue(MOVING, false), 3);
} else {
pLevel.levelEvent(1501, pPos, 0);
pLevel.setBlock(pPos, OBlocks.LEAD_BLOCK.get().defaultBlockState(), 3);
}
}
super.onPlace(pState, pLevel, pPos, pOldState, pIsMoving);
}

@Override
public void tick(BlockState pState , ServerLevel pLevel , BlockPos pPos , RandomSource pRandom ){
if (pLevel.getBlockState(pPos.below()).getBlock() == Blocks.AIR || pLevel.getBlockState(pPos.below()).is(BlockTags.REPLACEABLE)
|| pLevel.getBlockState(pPos.below()).getFluidState().is(FluidTags.WATER)
|| pLevel.getBlockState(pPos.below()).is(BlockTags.SMALL_FLOWERS)
|| pLevel.getBlockState(pPos.below()).is(BlockTags.TALL_FLOWERS)){
pLevel.setBlock( pPos , Blocks.AIR.defaultBlockState() , 67 );
pLevel.setBlock( pPos.below() , OBlocks.MOLTEN_LEAD.get().defaultBlockState() , 67 );
}
}

private boolean scheduleFallingTick( LevelAccessor pLevel , BlockPos pPos , int pDelay ){
if(pLevel.getBlockState( pPos.below() ).getBlock() == Blocks.AIR || pLevel.getBlockState(pPos.below()).is(BlockTags.REPLACEABLE)
|| pLevel.getBlockState( pPos.below() ).getFluidState().is(FluidTags.WATER)
|| pLevel.getBlockState( pPos.below() ).is(BlockTags.SMALL_FLOWERS)
|| pLevel.getBlockState(pPos.below()).is(BlockTags.TALL_FLOWERS)){
pLevel.scheduleTick( pPos , this , pDelay );
return true;
}
return false;
}

@Override
public VoxelShape getCollisionShape(BlockState blockState, BlockGetter world, BlockPos blockPos, CollisionContext ctx) {
if (ctx instanceof EntityCollisionContext eCtx && eCtx.getEntity() != null)
Expand All @@ -135,18 +89,40 @@ public VoxelShape getCollisionShape(BlockState blockState, BlockGetter world, Bl
}

@Override
public VoxelShape getShape(BlockState state, BlockGetter blockWorld, BlockPos pos, CollisionContext context) {
if (context.isHoldingItem(OItems.MOLTEN_LEAD_BUCKET.get()))
return blockWorld.getBlockState(pos.above()) != state ? STABLE_SHAPE : Shapes.block();
public void onRemove(BlockState state, Level level, BlockPos pos, BlockState replacedWith, boolean dropXp) {
super.onRemove(state, level, pos, replacedWith, dropXp);

if (replacedWith.isAir() || !replacedWith.getFluidState().isEmpty()) return;

return Shapes.empty();
tryEscape(state, level, pos);
}

@Override
public boolean canBeReplaced(BlockState state, BlockPlaceContext context) {
return state.is(Blocks.LAVA);
private boolean tryEscape(BlockState state, Level level, BlockPos pos, Direction direction) {
var adjancentPos = pos.relative(direction);
var adjacentState = level.getBlockState(adjancentPos);
if (adjacentState.canBeReplaced(getFluid())) {
level.setBlockAndUpdate(adjancentPos, state);

getPickupSound().ifPresent(sound -> {
level.playSound(null, pos, sound, SoundSource.BLOCKS, 1F, 1F);
});

return true;
}

return false;
}

private boolean tryEscape(BlockState state, Level level, BlockPos pos) {
if (tryEscape(state, level, pos, Direction.DOWN)) return true;
for (var direction : Direction.allShuffled(level.random)) {
if (direction.getAxis().isHorizontal() && tryEscape(state, level, pos, direction)) {
return true;
}
}

return false;
}

@Override
public void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) {
Expand All @@ -155,22 +131,16 @@ public void entityInside(BlockState state, Level world, BlockPos pos, Entity ent
entity.makeStuckInBlock(state, new Vec3(0.9F, 1.0D, 0.9F));
}

if (entity instanceof LivingEntity living && living.isUsingItem() && living.getUseItemRemainingTicks() == 0 && living.getItemInHand(living.getUsedItemHand()).isEdible()) {
if (!OreganizedConfig.COMMON.poisonInsteadOfStunning.get()) {
living.addEffect(new MobEffectInstance(OEffects.STUNNING.get(), 40 * 20));
}
living.addEffect(new MobEffectInstance(MobEffects.POISON, 200));
}
entity.setSecondsOnFire(10);
if (!world.isClientSide) entity.setSharedFlagOnFire(true);
}
}

@Override
public void fallOn(Level world, BlockState state, BlockPos pos, Entity entity, float fallDistance) {
if (!((double)fallDistance < 4.0D) && entity instanceof LivingEntity living) {
if (!((double) fallDistance < 4.0D) && entity instanceof LivingEntity living) {
LivingEntity.Fallsounds fallSound = living.getFallSounds();
SoundEvent sound = (double)fallDistance < 7.0D ? fallSound.small() : fallSound.big();
SoundEvent sound = (double) fallDistance < 7.0D ? fallSound.small() : fallSound.big();
entity.playSound(sound, 1.0F, 1.0F);
}
}
Expand All @@ -179,7 +149,7 @@ public static boolean isEntityLighterThanLead(Entity entity) {
if (entity.getType().is(OTags.Entities.LIGHTER_THAN_LEAD)) {
return true;
} else {
return entity instanceof LivingEntity && ((LivingEntity) entity).getItemBySlot(EquipmentSlot.FEET).is(OTags.Items.LIGHTER_THAN_LEAD);
return entity instanceof LivingEntity living && living.getItemBySlot(EquipmentSlot.FEET).is(OTags.Items.LIGHTER_THAN_LEAD);
}
}

Expand Down
46 changes: 18 additions & 28 deletions src/main/java/galena/oreganized/content/fluid/MoltenLeadFluid.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,33 @@
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraftforge.fluids.ForgeFlowingFluid;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
public class MoltenLeadFluid extends ForgeFlowingFluid {
import static galena.oreganized.content.block.MoltenLeadBlock.MOVING;

public static BooleanProperty MOVING = BooleanProperty.create("moving");
@ParametersAreNonnullByDefault
public class MoltenLeadFluid extends ForgeFlowingFluid {


public MoltenLeadFluid(Properties properties) {
super(properties);
registerDefaultState(getStateDefinition().any().setValue(LEVEL, 8).setValue(MOVING, false));
registerDefaultState(defaultFluidState().setValue(LEVEL, 8).setValue(MOVING, false));
}

@Override
protected boolean isRandomlyTicking() {
return true;
}

@Override
protected void createFluidStateDefinition(StateDefinition.Builder<Fluid, FluidState> builder) {
super.createFluidStateDefinition(builder);
builder.add(LEVEL).add(MOVING);
}

@Override
public int getAmount(FluidState state) {
return 8;
Expand All @@ -54,24 +45,23 @@ public boolean isSource(FluidState state) {
}

@Override
protected void spread(Level world, BlockPos blockPos, FluidState fluidState) {
if (!fluidState.isEmpty()) {
if (fluidState.getValue(MOVING)) {
BlockPos belowPos = blockPos.below();
BlockState belowState = world.getBlockState(belowPos);
if (this.canSpreadTo(world, belowPos, belowState, Direction.DOWN, belowPos, belowState, world.getFluidState(belowPos), fluidState.getType())) {
this.spreadTo(world, belowPos, belowState, Direction.DOWN, fluidState);
world.setBlock(blockPos, Blocks.AIR.defaultBlockState(), 3);
}
} else {
world.setBlock(blockPos, fluidState.setValue(MOVING, true).createLegacyBlock(), 3);
}
}
protected void createFluidStateDefinition(StateDefinition.Builder<Fluid, FluidState> builder) {
super.createFluidStateDefinition(builder);
builder.add(LEVEL);
builder.add(MOVING);
}

@Override
public boolean canBeReplacedWith(FluidState p_76233_, BlockGetter p_76234_, BlockPos p_76235_, Fluid p_76236_, Direction p_76237_) {
return false;
protected void spread(Level level, BlockPos pos, FluidState fluidState) {
if (fluidState.isEmpty()) return;
BlockState blockstate = level.getBlockState(pos);
BlockPos belowPos = pos.below();
BlockState belowState = level.getBlockState(belowPos);
FluidState fluidstate = getNewLiquid(level, belowPos, belowState);
if (canSpreadTo(level, pos, blockstate, Direction.DOWN, belowPos, belowState, level.getFluidState(belowPos), fluidstate.getType())) {
spreadTo(level, belowPos, belowState, Direction.DOWN, fluidstate);
level.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/galena/oreganized/index/OFluids.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ public void modifyFogRender(Camera camera, FogRenderer.FogMode mode, float rende
});
public static final RegistryObject<FlowingFluid> MOLTEN_LEAD = FLUIDS.register("molten_lead", () -> new MoltenLeadFluid(OFluids.MOLTEN_LEAD_PROPERTIES));

public static final ForgeFlowingFluid.Properties MOLTEN_LEAD_PROPERTIES = new ForgeFlowingFluid.Properties(MOLTEN_LEAD_TYPE, MOLTEN_LEAD, MOLTEN_LEAD).bucket(OItems.MOLTEN_LEAD_BUCKET).block(OBlocks.MOLTEN_LEAD);
public static final ForgeFlowingFluid.Properties MOLTEN_LEAD_PROPERTIES = new ForgeFlowingFluid.Properties(MOLTEN_LEAD_TYPE, MOLTEN_LEAD, MOLTEN_LEAD).bucket(OItems.MOLTEN_LEAD_BUCKET).block(OBlocks.MOLTEN_LEAD).tickRate(30);
}

0 comments on commit 45d17f0

Please sign in to comment.