Skip to content

Commit 45d17f0

Browse files
committed
update molten lead spread mechanic
1 parent 4a7b19e commit 45d17f0

File tree

3 files changed

+67
-107
lines changed

3 files changed

+67
-107
lines changed

src/main/java/galena/oreganized/content/block/MoltenLeadBlock.java

Lines changed: 48 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
package galena.oreganized.content.block;
22

3-
import galena.oreganized.OreganizedConfig;
43
import galena.oreganized.index.OBlocks;
5-
import galena.oreganized.index.OEffects;
6-
import galena.oreganized.index.OItems;
74
import galena.oreganized.index.OTags;
85
import net.minecraft.MethodsReturnNonnullByDefault;
96
import net.minecraft.core.BlockPos;
107
import net.minecraft.core.Direction;
11-
import net.minecraft.server.level.ServerLevel;
8+
import net.minecraft.core.particles.ParticleTypes;
129
import net.minecraft.sounds.SoundEvent;
1310
import net.minecraft.sounds.SoundEvents;
14-
import net.minecraft.tags.BlockTags;
11+
import net.minecraft.sounds.SoundSource;
1512
import net.minecraft.tags.FluidTags;
16-
import net.minecraft.util.RandomSource;
17-
import net.minecraft.world.effect.MobEffectInstance;
18-
import net.minecraft.world.effect.MobEffects;
1913
import net.minecraft.world.entity.Entity;
2014
import net.minecraft.world.entity.EquipmentSlot;
2115
import net.minecraft.world.entity.LivingEntity;
2216
import net.minecraft.world.entity.Mob;
23-
import net.minecraft.world.item.context.BlockPlaceContext;
2417
import net.minecraft.world.level.BlockGetter;
2518
import net.minecraft.world.level.Level;
26-
import net.minecraft.world.level.LevelAccessor;
2719
import net.minecraft.world.level.block.Block;
28-
import net.minecraft.world.level.block.Blocks;
2920
import net.minecraft.world.level.block.LiquidBlock;
3021
import net.minecraft.world.level.block.state.BlockState;
3122
import net.minecraft.world.level.block.state.StateDefinition;
@@ -48,7 +39,7 @@
4839
@MethodsReturnNonnullByDefault
4940
public class MoltenLeadBlock extends LiquidBlock {
5041

51-
private static final BooleanProperty MOVING = BooleanProperty.create( "moving" );
42+
public static final BooleanProperty MOVING = BooleanProperty.create("moving");
5243

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

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

6051
@Override
61-
protected void createBlockStateDefinition(StateDefinition.Builder <Block, BlockState> builder) {
52+
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
6253
super.createBlockStateDefinition(builder);
6354
builder.add(MOVING);
6455
}
6556

66-
@Override
67-
public BlockState updateShape(BlockState pState, Direction pDirection, BlockState neighbour, LevelAccessor pLevel, BlockPos pPos, BlockPos neighbourPos) {
68-
if(pDirection == Direction.DOWN){
69-
pLevel.scheduleTick( pPos , this , 30 );
70-
}
71-
return super.updateShape( pState , pDirection , neighbour , pLevel , pPos , neighbourPos );
72-
}
73-
7457
@Nullable
7558
@Override
7659
public BlockPathTypes getBlockPathType(BlockState state, BlockGetter world, BlockPos pos, @Nullable Mob entity) {
7760
return BlockPathTypes.WALKABLE;
7861
}
7962

8063
@Override
81-
public boolean canEntityDestroy( BlockState state , BlockGetter world , BlockPos pos , Entity entity ){
82-
return false;
83-
}
84-
85-
@Override
86-
public void onPlace( BlockState pState , Level pLevel , BlockPos pPos , BlockState pOldState , boolean pIsMoving ){
87-
if (pIsMoving){
88-
if(!pOldState.getFluidState().is( FluidTags.WATER )){
89-
scheduleFallingTick( pLevel , pPos , 30 );
90-
pLevel.setBlock( pPos , pState.setValue( MOVING , true ) , 3 );
91-
}else{
92-
pLevel.levelEvent( 1501 , pPos , 0 );
93-
pLevel.setBlock( pPos , OBlocks.LEAD_BLOCK.get().defaultBlockState() , 3 );
64+
public void onPlace(BlockState pState, Level pLevel, BlockPos pPos, BlockState pOldState, boolean pIsMoving) {
65+
if (pIsMoving) {
66+
if (!pOldState.getFluidState().is(FluidTags.WATER)) {
67+
pLevel.setBlock(pPos, pState.setValue(MOVING, true), 3);
68+
} else {
69+
pLevel.levelEvent(1501, pPos, 0);
70+
pLevel.setBlock(pPos, OBlocks.LEAD_BLOCK.get().defaultBlockState(), 3);
9471
}
9572
} else {
96-
if(!pOldState.getFluidState().is( FluidTags.WATER )){
97-
pLevel.setBlock( pPos , pState.setValue( MOVING , false ) , 3 );
98-
pLevel.scheduleTick( pPos , this , 300 );
99-
}else{
100-
pLevel.levelEvent( 1501 , pPos , 0 );
101-
pLevel.setBlock( pPos , OBlocks.LEAD_BLOCK.get().defaultBlockState() , 3 );
73+
if (!pOldState.getFluidState().is(FluidTags.WATER)) {
74+
pLevel.setBlock(pPos, pState.setValue(MOVING, false), 3);
75+
} else {
76+
pLevel.levelEvent(1501, pPos, 0);
77+
pLevel.setBlock(pPos, OBlocks.LEAD_BLOCK.get().defaultBlockState(), 3);
10278
}
10379
}
10480
super.onPlace(pState, pLevel, pPos, pOldState, pIsMoving);
10581
}
10682

107-
@Override
108-
public void tick(BlockState pState , ServerLevel pLevel , BlockPos pPos , RandomSource pRandom ){
109-
if (pLevel.getBlockState(pPos.below()).getBlock() == Blocks.AIR || pLevel.getBlockState(pPos.below()).is(BlockTags.REPLACEABLE)
110-
|| pLevel.getBlockState(pPos.below()).getFluidState().is(FluidTags.WATER)
111-
|| pLevel.getBlockState(pPos.below()).is(BlockTags.SMALL_FLOWERS)
112-
|| pLevel.getBlockState(pPos.below()).is(BlockTags.TALL_FLOWERS)){
113-
pLevel.setBlock( pPos , Blocks.AIR.defaultBlockState() , 67 );
114-
pLevel.setBlock( pPos.below() , OBlocks.MOLTEN_LEAD.get().defaultBlockState() , 67 );
115-
}
116-
}
117-
118-
private boolean scheduleFallingTick( LevelAccessor pLevel , BlockPos pPos , int pDelay ){
119-
if(pLevel.getBlockState( pPos.below() ).getBlock() == Blocks.AIR || pLevel.getBlockState(pPos.below()).is(BlockTags.REPLACEABLE)
120-
|| pLevel.getBlockState( pPos.below() ).getFluidState().is(FluidTags.WATER)
121-
|| pLevel.getBlockState( pPos.below() ).is(BlockTags.SMALL_FLOWERS)
122-
|| pLevel.getBlockState(pPos.below()).is(BlockTags.TALL_FLOWERS)){
123-
pLevel.scheduleTick( pPos , this , pDelay );
124-
return true;
125-
}
126-
return false;
127-
}
128-
12983
@Override
13084
public VoxelShape getCollisionShape(BlockState blockState, BlockGetter world, BlockPos blockPos, CollisionContext ctx) {
13185
if (ctx instanceof EntityCollisionContext eCtx && eCtx.getEntity() != null)
@@ -135,18 +89,40 @@ public VoxelShape getCollisionShape(BlockState blockState, BlockGetter world, Bl
13589
}
13690

13791
@Override
138-
public VoxelShape getShape(BlockState state, BlockGetter blockWorld, BlockPos pos, CollisionContext context) {
139-
if (context.isHoldingItem(OItems.MOLTEN_LEAD_BUCKET.get()))
140-
return blockWorld.getBlockState(pos.above()) != state ? STABLE_SHAPE : Shapes.block();
92+
public void onRemove(BlockState state, Level level, BlockPos pos, BlockState replacedWith, boolean dropXp) {
93+
super.onRemove(state, level, pos, replacedWith, dropXp);
94+
95+
if (replacedWith.isAir() || !replacedWith.getFluidState().isEmpty()) return;
14196

142-
return Shapes.empty();
97+
tryEscape(state, level, pos);
14398
}
14499

145-
@Override
146-
public boolean canBeReplaced(BlockState state, BlockPlaceContext context) {
147-
return state.is(Blocks.LAVA);
100+
private boolean tryEscape(BlockState state, Level level, BlockPos pos, Direction direction) {
101+
var adjancentPos = pos.relative(direction);
102+
var adjacentState = level.getBlockState(adjancentPos);
103+
if (adjacentState.canBeReplaced(getFluid())) {
104+
level.setBlockAndUpdate(adjancentPos, state);
105+
106+
getPickupSound().ifPresent(sound -> {
107+
level.playSound(null, pos, sound, SoundSource.BLOCKS, 1F, 1F);
108+
});
109+
110+
return true;
111+
}
112+
113+
return false;
148114
}
149115

116+
private boolean tryEscape(BlockState state, Level level, BlockPos pos) {
117+
if (tryEscape(state, level, pos, Direction.DOWN)) return true;
118+
for (var direction : Direction.allShuffled(level.random)) {
119+
if (direction.getAxis().isHorizontal() && tryEscape(state, level, pos, direction)) {
120+
return true;
121+
}
122+
}
123+
124+
return false;
125+
}
150126

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

158-
if (entity instanceof LivingEntity living && living.isUsingItem() && living.getUseItemRemainingTicks() == 0 && living.getItemInHand(living.getUsedItemHand()).isEdible()) {
159-
if (!OreganizedConfig.COMMON.poisonInsteadOfStunning.get()) {
160-
living.addEffect(new MobEffectInstance(OEffects.STUNNING.get(), 40 * 20));
161-
}
162-
living.addEffect(new MobEffectInstance(MobEffects.POISON, 200));
163-
}
164134
entity.setSecondsOnFire(10);
165135
if (!world.isClientSide) entity.setSharedFlagOnFire(true);
166136
}
167137
}
168138

169139
@Override
170140
public void fallOn(Level world, BlockState state, BlockPos pos, Entity entity, float fallDistance) {
171-
if (!((double)fallDistance < 4.0D) && entity instanceof LivingEntity living) {
141+
if (!((double) fallDistance < 4.0D) && entity instanceof LivingEntity living) {
172142
LivingEntity.Fallsounds fallSound = living.getFallSounds();
173-
SoundEvent sound = (double)fallDistance < 7.0D ? fallSound.small() : fallSound.big();
143+
SoundEvent sound = (double) fallDistance < 7.0D ? fallSound.small() : fallSound.big();
174144
entity.playSound(sound, 1.0F, 1.0F);
175145
}
176146
}
@@ -179,7 +149,7 @@ public static boolean isEntityLighterThanLead(Entity entity) {
179149
if (entity.getType().is(OTags.Entities.LIGHTER_THAN_LEAD)) {
180150
return true;
181151
} else {
182-
return entity instanceof LivingEntity && ((LivingEntity) entity).getItemBySlot(EquipmentSlot.FEET).is(OTags.Items.LIGHTER_THAN_LEAD);
152+
return entity instanceof LivingEntity living && living.getItemBySlot(EquipmentSlot.FEET).is(OTags.Items.LIGHTER_THAN_LEAD);
183153
}
184154
}
185155

src/main/java/galena/oreganized/content/fluid/MoltenLeadFluid.java

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,33 @@
77
import net.minecraft.sounds.SoundEvents;
88
import net.minecraft.sounds.SoundSource;
99
import net.minecraft.util.RandomSource;
10-
import net.minecraft.world.level.BlockGetter;
1110
import net.minecraft.world.level.Level;
12-
import net.minecraft.world.level.LevelAccessor;
1311
import net.minecraft.world.level.block.Blocks;
1412
import net.minecraft.world.level.block.state.BlockState;
1513
import net.minecraft.world.level.block.state.StateDefinition;
16-
import net.minecraft.world.level.block.state.properties.BooleanProperty;
1714
import net.minecraft.world.level.material.Fluid;
1815
import net.minecraft.world.level.material.FluidState;
1916
import net.minecraftforge.fluids.ForgeFlowingFluid;
2017

2118
import javax.annotation.Nullable;
2219
import javax.annotation.ParametersAreNonnullByDefault;
2320

24-
@ParametersAreNonnullByDefault
25-
public class MoltenLeadFluid extends ForgeFlowingFluid {
21+
import static galena.oreganized.content.block.MoltenLeadBlock.MOVING;
2622

27-
public static BooleanProperty MOVING = BooleanProperty.create("moving");
23+
@ParametersAreNonnullByDefault
24+
public class MoltenLeadFluid extends ForgeFlowingFluid {
2825

2926

3027
public MoltenLeadFluid(Properties properties) {
3128
super(properties);
32-
registerDefaultState(getStateDefinition().any().setValue(LEVEL, 8).setValue(MOVING, false));
29+
registerDefaultState(defaultFluidState().setValue(LEVEL, 8).setValue(MOVING, false));
3330
}
3431

3532
@Override
3633
protected boolean isRandomlyTicking() {
3734
return true;
3835
}
3936

40-
@Override
41-
protected void createFluidStateDefinition(StateDefinition.Builder<Fluid, FluidState> builder) {
42-
super.createFluidStateDefinition(builder);
43-
builder.add(LEVEL).add(MOVING);
44-
}
45-
4637
@Override
4738
public int getAmount(FluidState state) {
4839
return 8;
@@ -54,24 +45,23 @@ public boolean isSource(FluidState state) {
5445
}
5546

5647
@Override
57-
protected void spread(Level world, BlockPos blockPos, FluidState fluidState) {
58-
if (!fluidState.isEmpty()) {
59-
if (fluidState.getValue(MOVING)) {
60-
BlockPos belowPos = blockPos.below();
61-
BlockState belowState = world.getBlockState(belowPos);
62-
if (this.canSpreadTo(world, belowPos, belowState, Direction.DOWN, belowPos, belowState, world.getFluidState(belowPos), fluidState.getType())) {
63-
this.spreadTo(world, belowPos, belowState, Direction.DOWN, fluidState);
64-
world.setBlock(blockPos, Blocks.AIR.defaultBlockState(), 3);
65-
}
66-
} else {
67-
world.setBlock(blockPos, fluidState.setValue(MOVING, true).createLegacyBlock(), 3);
68-
}
69-
}
48+
protected void createFluidStateDefinition(StateDefinition.Builder<Fluid, FluidState> builder) {
49+
super.createFluidStateDefinition(builder);
50+
builder.add(LEVEL);
51+
builder.add(MOVING);
7052
}
7153

7254
@Override
73-
public boolean canBeReplacedWith(FluidState p_76233_, BlockGetter p_76234_, BlockPos p_76235_, Fluid p_76236_, Direction p_76237_) {
74-
return false;
55+
protected void spread(Level level, BlockPos pos, FluidState fluidState) {
56+
if (fluidState.isEmpty()) return;
57+
BlockState blockstate = level.getBlockState(pos);
58+
BlockPos belowPos = pos.below();
59+
BlockState belowState = level.getBlockState(belowPos);
60+
FluidState fluidstate = getNewLiquid(level, belowPos, belowState);
61+
if (canSpreadTo(level, pos, blockstate, Direction.DOWN, belowPos, belowState, level.getFluidState(belowPos), fluidstate.getType())) {
62+
spreadTo(level, belowPos, belowState, Direction.DOWN, fluidstate);
63+
level.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
64+
}
7565
}
7666

7767
@Override

src/main/java/galena/oreganized/index/OFluids.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ public void modifyFogRender(Camera camera, FogRenderer.FogMode mode, float rende
7272
});
7373
public static final RegistryObject<FlowingFluid> MOLTEN_LEAD = FLUIDS.register("molten_lead", () -> new MoltenLeadFluid(OFluids.MOLTEN_LEAD_PROPERTIES));
7474

75-
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);
75+
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);
7676
}

0 commit comments

Comments
 (0)