Skip to content

Commit

Permalink
Rework block event mixins to fix stack overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed Jul 26, 2024
1 parent 65fac9c commit 3c42286
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public float getFriction(BlockState state, LevelReader level, BlockPos pos, Enti

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return state.getBlock().getFriction();
return super.getFriction();
copycatBE
.getMaterialItemStorage()
.getAllMaterials()
Expand All @@ -77,12 +77,13 @@ public float getFriction(BlockState state, LevelReader level, BlockPos pos, Enti
bonus.accumulateAndGet(maybeMaterialAs(level, pos, CustomFrictionBlock.class,
mat, (material, frictionBlock) -> frictionBlock.getFriction(material, level, pos, entity),
(material) -> material.is(Blocks.AIR)
? state.getBlock().getFriction()
? super.getFriction()
: material.getBlock().getFriction()), Float::sum);
});
return bonus.get() / count.get();
} else {
return super.getFriction();
}
return state.getBlock().getFriction();
}

@Override
Expand All @@ -92,29 +93,31 @@ public int getLightEmission(BlockState state, BlockGetter level, BlockPos pos) {

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return 0;
return state.getLightEmission();
copycatBE.getMaterialItemStorage().getAllMaterials().forEach(bs -> {
light.accumulateAndGet(bs.getLightEmission(), Math::max);
});
return light.get();
} else {
return state.getLightEmission();
}
return 0;
}

@Override
public float getExplosionResistance(BlockState state, BlockGetter level, BlockPos pos, Explosion explosion) {
if (state.getBlock() instanceof IMultiStateCopycatBlock copycatBlock) {
AtomicReference<Float> explosionResistance = new AtomicReference<>(state.getBlock().getExplosionResistance());
AtomicReference<Float> explosionResistance = new AtomicReference<>(0f);

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return state.getBlock().getExplosionResistance();
return super.getExplosionResistance();
copycatBE.getMaterialItemStorage().getAllMaterials().forEach(bs -> {
explosionResistance.accumulateAndGet(bs.getBlock().getExplosionResistance(), Math::max);
});
return explosionResistance.get();
} else {
return super.getExplosionResistance();
}
return state.getBlock().getExplosionResistance();
}

@Override
Expand All @@ -131,44 +134,45 @@ public ItemStack getPickedStack(BlockState state, BlockGetter level, BlockPos po
(mat, block) -> block.getPickedStack(mat, level, pos, player, result),
mat -> mat.getBlock().getCloneItemStack(level, pos, mat)
);
} else {
return new ItemStack(state.getBlock());
}
return new ItemStack(state.getBlock());
}

@Override
public boolean addLandingEffects(BlockState state1, ServerLevel level, BlockPos pos, BlockState state2, LivingEntity entity, int numberOfParticles) {
if (state1.getBlock() instanceof IMultiStateCopycatBlock copycatBlock) {
BlockHitResult hitResult = level.clip(new ClipContext(entity.position(), entity.position().add(0, -2, 0), ClipContext.Block.COLLIDER, ClipContext.Fluid.ANY, entity));
String property = copycatBlock.getPropertyFromInteraction(state1, level, pos, hitResult, true);
AtomicReference<BlockState> mat = new AtomicReference<>(AllBlocks.COPYCAT_BASE.getDefaultState());

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return false;
mat.set(copycatBE.getMaterialItemStorage().getMaterialItem(property).material());
BlockState mat = copycatBE.getMaterialItemStorage().getMaterialItem(property).material();
return maybeMaterialAs(level, pos, CustomLandingEffectsBlock.class,
mat.get(), (material, frictionBlock) -> frictionBlock.addLandingEffects(material, level, pos, material, entity, numberOfParticles),
mat, (material, frictionBlock) -> frictionBlock.addLandingEffects(material, level, pos, material, entity, numberOfParticles),
(material) -> false);
} else {
return false;
}
return false;
}

@Override
public boolean addRunningEffects(BlockState state, Level level, BlockPos pos, Entity entity) {
if (state.getBlock() instanceof IMultiStateCopycatBlock copycatBlock) {
BlockHitResult hitResult = level.clip(new ClipContext(entity.position(), entity.position().add(0, -2, 0), ClipContext.Block.COLLIDER, ClipContext.Fluid.ANY, entity));
String property = copycatBlock.getPropertyFromInteraction(state, level, pos, hitResult, true);
AtomicReference<BlockState> mat = new AtomicReference<>(AllBlocks.COPYCAT_BASE.getDefaultState());

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return false;
mat.set(copycatBE.getMaterialItemStorage().getMaterialItem(property).material());
BlockState mat = copycatBE.getMaterialItemStorage().getMaterialItem(property).material();
return maybeMaterialAs(level, pos, CustomRunningEffectsBlock.class,
mat.get(), (material, frictionBlock) -> frictionBlock.addRunningEffects(material, level, pos, entity),
mat, (material, frictionBlock) -> frictionBlock.addRunningEffects(material, level, pos, entity),
(material) -> false);
} else {
return false;
}
return false;
}

@Override
Expand All @@ -178,7 +182,7 @@ public float getEnchantPowerBonus(BlockState state, LevelReader level, BlockPos

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return 0f;
return state.is(BlockTags.ENCHANTMENT_POWER_PROVIDER) ? 1f : 0f;
copycatBE
.getMaterialItemStorage()
.getAllMaterials()
Expand All @@ -193,7 +197,7 @@ public float getEnchantPowerBonus(BlockState state, LevelReader level, BlockPos
});
return bonus.get();
}
return 0f;
return state.is(BlockTags.ENCHANTMENT_POWER_PROVIDER) ? 1f : 0f;
}

@Override
Expand All @@ -207,29 +211,30 @@ public void fallOn(@NotNull Level pLevel, @NotNull BlockState pState, @NotNull B
if (pState.getBlock() instanceof IMultiStateCopycatBlock copycatBlock) {
BlockHitResult hitResult = pLevel.clip(new ClipContext(pEntity.position(), pEntity.position().add(0, -2, 0), ClipContext.Block.COLLIDER, ClipContext.Fluid.ANY, pEntity));
String property = copycatBlock.getPropertyFromInteraction(pState, pLevel, pPos, hitResult, true);
AtomicReference<BlockState> material = new AtomicReference<>(AllBlocks.COPYCAT_BASE.getDefaultState());

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(pLevel, pPos);
if (copycatBE == null)
return;
material.set(copycatBE.getMaterialItemStorage().getMaterialItem(property).material());
material.get().getBlock().fallOn(pLevel, material.get(), pPos, pEntity, p_152430_);
BlockState material = copycatBE.getMaterialItemStorage().getMaterialItem(property).material();
material.getBlock().fallOn(pLevel, material, pPos, pEntity, p_152430_);
} else {
super.fallOn(pLevel, pState, pPos, pEntity, p_152430_);
}
}

@Override
public float getDestroyProgress(@NotNull BlockState pState, @NotNull Player pPlayer, @NotNull BlockGetter pLevel, @NotNull BlockPos pPos) {
if (pState.getBlock() instanceof IMultiStateCopycatBlock copycatBlock) {
String property = copycatBlock.getPropertyFromInteraction(pState, pLevel, pPos, new BlockHitResult(Vec3.atCenterOf(pPos), Direction.UP, pPos, true), true);
AtomicReference<BlockState> material = new AtomicReference<>(AllBlocks.COPYCAT_BASE.getDefaultState());

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(pLevel, pPos);
if (copycatBE == null)
return super.getDestroyProgress(pState, pPlayer, pLevel, pPos);
material.set(copycatBE.getMaterialItemStorage().getMaterialItem(property).material());
return material.get().getDestroyProgress(pPlayer, pLevel, pPos);
BlockState material = copycatBE.getMaterialItemStorage().getMaterialItem(property).material();
return material.getDestroyProgress(pPlayer, pLevel, pPos);
} else {
return super.getDestroyProgress(pState, pPlayer, pLevel, pPos);
}
return super.getDestroyProgress(pState, pPlayer, pLevel, pPos);
}

@Unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public float getFriction(BlockState state, LevelReader level, BlockPos pos, Enti

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return state.getBlock().getFriction();
return super.getFriction(state, level, pos, entity);
copycatBE.getMaterialItemStorage().getAllMaterials().forEach(mat -> {
count.getAndIncrement();
bonus.accumulateAndGet(mat.is(Blocks.AIR) ? state.getFriction(level, pos, entity) : mat.getFriction(level, pos, entity), Float::sum);
});
return bonus.get() / count.get();
}
return state.getBlock().getFriction();
return super.getFriction(state, level, pos, entity);
}

@Override
Expand All @@ -77,13 +77,14 @@ public int getLightEmission(BlockState state, BlockGetter level, BlockPos pos) {

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return 0;
return super.getLightEmission(state, level, pos);
copycatBE.getMaterialItemStorage().getAllMaterials().forEach(bs -> {
light.accumulateAndGet(bs.getLightEmission(), Math::max);
});
return light.get();
} else {
return super.getLightEmission(state, level, pos);
}
return 0;
}

@Override
Expand All @@ -93,13 +94,14 @@ public float getExplosionResistance(BlockState state, BlockGetter level, BlockPo

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return state.getBlock().getExplosionResistance();
return super.getExplosionResistance(state, level, pos, explosion);
copycatBE.getMaterialItemStorage().getAllMaterials().forEach(bs -> {
explosionResistance.accumulateAndGet(bs.getBlock().getExplosionResistance(), Math::max);
});
return explosionResistance.get();
} else {
return super.getExplosionResistance(state, level, pos, explosion);
}
return state.getBlock().getExplosionResistance(state, level, pos, explosion);
}

@Override
Expand All @@ -119,31 +121,31 @@ public boolean addLandingEffects(BlockState state1, ServerLevel level, BlockPos
if (state1.getBlock() instanceof IMultiStateCopycatBlock copycatBlock) {
BlockHitResult hitResult = level.clip(new ClipContext(entity.position(), entity.position().add(0, -2, 0), ClipContext.Block.COLLIDER, ClipContext.Fluid.ANY, entity));
String property = copycatBlock.getPropertyFromInteraction(state1, level, pos, hitResult, true);
AtomicReference<BlockState> mat = new AtomicReference<>(AllBlocks.COPYCAT_BASE.getDefaultState());

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return false;
mat.set(copycatBE.getMaterialItemStorage().getMaterialItem(property).material());
return mat.get().addLandingEffects(level, pos, mat.get(), entity, numberOfParticles);
return super.addLandingEffects(state1, level, pos, state2, entity, numberOfParticles);
BlockState material = copycatBE.getMaterialItemStorage().getMaterialItem(property).material();
return material.addLandingEffects(level, pos, material, entity, numberOfParticles);
} else {
return super.addLandingEffects(state1, level, pos, state2, entity, numberOfParticles);
}
return false;
}

@Override
public boolean addRunningEffects(BlockState state, Level level, BlockPos pos, Entity entity) {
if (state.getBlock() instanceof IMultiStateCopycatBlock copycatBlock) {
BlockHitResult hitResult = level.clip(new ClipContext(entity.position(), entity.position().add(0, -2, 0), ClipContext.Block.COLLIDER, ClipContext.Fluid.ANY, entity));
String property = copycatBlock.getPropertyFromInteraction(state, level, pos, hitResult, true);
AtomicReference<BlockState> mat = new AtomicReference<>(AllBlocks.COPYCAT_BASE.getDefaultState());

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return false;
mat.set(copycatBE.getMaterialItemStorage().getMaterialItem(property).material());
return mat.get().addRunningEffects(level, pos, entity);
return super.addRunningEffects(state, level, pos, entity);
BlockState material = copycatBE.getMaterialItemStorage().getMaterialItem(property).material();
return material.addRunningEffects(level, pos, entity);
} else {
return super.addRunningEffects(state, level, pos, entity);
}
return false;
}

@Override
Expand All @@ -153,11 +155,12 @@ public float getEnchantPowerBonus(BlockState state, LevelReader level, BlockPos

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(level, pos);
if (copycatBE == null)
return 0f;
return super.getEnchantPowerBonus(state, level, pos);
copycatBE.getMaterialItemStorage().getAllMaterials().forEach(mat -> bonus.accumulateAndGet(mat.getEnchantPowerBonus(level, pos), Float::max));
return bonus.get();
} else {
return super.getEnchantPowerBonus(state, level, pos);
}
return 0f;
}

@Override
Expand All @@ -171,29 +174,32 @@ public void fallOn(@NotNull Level pLevel, @NotNull BlockState state, @NotNull Bl
if (state.getBlock() instanceof IMultiStateCopycatBlock copycatBlock) {
BlockHitResult hitResult = pLevel.clip(new ClipContext(pEntity.position(), pEntity.position().add(0, -2, 0), ClipContext.Block.COLLIDER, ClipContext.Fluid.ANY, pEntity));
String property = copycatBlock.getPropertyFromInteraction(state, pLevel, pPos, hitResult, true);
AtomicReference<BlockState> material = new AtomicReference<>(AllBlocks.COPYCAT_BASE.getDefaultState());

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(pLevel, pPos);
if (copycatBE == null)
if (copycatBE == null) {
super.fallOn(pLevel, state, pPos, pEntity, p_152430_);
return;
material.set(copycatBE.getMaterialItemStorage().getMaterialItem(property).material());
material.get().getBlock().fallOn(pLevel, material.get(), pPos, pEntity, p_152430_);
}
BlockState material = copycatBE.getMaterialItemStorage().getMaterialItem(property).material();
material.getBlock().fallOn(pLevel, material, pPos, pEntity, p_152430_);
} else {
super.fallOn(pLevel, state, pPos, pEntity, p_152430_);
}
}

@Override
public float getDestroyProgress(@NotNull BlockState pState, @NotNull Player pPlayer, @NotNull BlockGetter pLevel, @NotNull BlockPos pPos) {
if (pState.getBlock() instanceof IMultiStateCopycatBlock copycatBlock) {
String property = copycatBlock.getPropertyFromInteraction(pState, pLevel, pPos, new BlockHitResult(Vec3.atCenterOf(pPos), Direction.UP, pPos, true), true);
AtomicReference<BlockState> material = new AtomicReference<>(AllBlocks.COPYCAT_BASE.getDefaultState());

IMultiStateCopycatBlockEntity copycatBE = copycatBlock.getCopycatBlockEntity(pLevel, pPos);
if (copycatBE == null)
return super.getDestroyProgress(pState, pPlayer, pLevel, pPos);
material.set(copycatBE.getMaterialItemStorage().getMaterialItem(property).material());
return material.get().getDestroyProgress(pPlayer, pLevel, pPos);
BlockState material = copycatBE.getMaterialItemStorage().getMaterialItem(property).material();
return material.getDestroyProgress(pPlayer, pLevel, pPos);
} else {
return super.getDestroyProgress(pState, pPlayer, pLevel, pPos);
}
return super.getDestroyProgress(pState, pPlayer, pLevel, pPos);
}

@Override
Expand Down

0 comments on commit 3c42286

Please sign in to comment.