Hydrotermal vent ticks fix

This commit is contained in:
paulevsGitch 2021-07-08 12:01:38 +03:00
parent 93ca944472
commit e9e39f7152
2 changed files with 48 additions and 40 deletions

View file

@ -22,46 +22,51 @@ import ru.betterend.registry.EndParticles;
import java.util.List;
public class BlockEntityHydrothermalVent extends BlockEntity {
private final static Vec3 POSITIVE_Y = new Vec3(0.0f, 1.0f, 0.0f);
private static final MutableBlockPos POS = new MutableBlockPos();
public BlockEntityHydrothermalVent(BlockPos blockPos, BlockState blockState) {
super(EndBlockEntities.HYDROTHERMAL_VENT, blockPos, blockState);
}
public static void tick(Level level, BlockPos worldPosition, BlockState state, BlockEntityHydrothermalVent blockEntity) {
if (level != null) {
if (state.is(EndBlocks.HYDROTHERMAL_VENT)) {
boolean active = state.getValue(HydrothermalVentBlock.ACTIVATED);
if (active && level.random.nextInt(20) == 0) {
double x = worldPosition.getX() + level.random.nextDouble();
double y = worldPosition.getY() + 0.9 + level.random.nextDouble() * 0.3;
double z = worldPosition.getZ() + level.random.nextDouble();
if (state.getValue(HydrothermalVentBlock.WATERLOGGED)) {
level.addParticle(EndParticles.GEYSER_PARTICLE, x, y, z, 0, 0, 0);
}
else {
level.addParticle(ParticleTypes.BUBBLE, x, y, z, 0, 0, 0);
}
}
MutableBlockPos mutable = worldPosition.mutable().move(Direction.UP);
int height = active ? 85 : 25;
AABB box = new AABB(mutable.offset(-1, 0, -1), mutable.offset(1, height, 1));
List<LivingEntity> entities = level.getEntitiesOfClass(LivingEntity.class, box);
if (entities.size() > 0) {
while (mutable.getY() < box.maxY) {
BlockState blockState = level.getBlockState(mutable);
if (blockState.isSolidRender(level, mutable)) break;
if (blockState.isAir()) {
double mult = active ? 3.0 : 5.0;
float force = (float) ((1.0 - (mutable.getY() / box.maxY)) / mult);
entities.stream().filter(entity -> (int) entity.getY() == mutable.getY() &&
blockEntity.hasElytra(entity) && entity.isFallFlying())
.forEach(entity -> entity.moveRelative(force, POSITIVE_Y));
}
mutable.move(Direction.UP);
}
public static <T extends BlockEntity> void tick(Level level, BlockPos worldPosition, BlockState state, T uncastedEntity) {
if (level != null && uncastedEntity instanceof BlockEntityHydrothermalVent && state.is(EndBlocks.HYDROTHERMAL_VENT)) {
BlockEntityHydrothermalVent blockEntity = (BlockEntityHydrothermalVent) uncastedEntity;
if (level.isClientSide()) {
clientTick(level, worldPosition, state, blockEntity);
}
else {
serverTick(level, worldPosition, state, blockEntity);
}
}
}
private static void clientTick(Level level, BlockPos worldPosition, BlockState state, BlockEntityHydrothermalVent blockEntity) {
boolean active = state.getValue(HydrothermalVentBlock.ACTIVATED);
if (active && level.random.nextInt(20) == 0 && state.getValue(HydrothermalVentBlock.WATERLOGGED)) {
double x = worldPosition.getX() + level.random.nextDouble();
double y = worldPosition.getY() + 0.9 + level.random.nextDouble() * 0.3;
double z = worldPosition.getZ() + level.random.nextDouble();
level.addParticle(EndParticles.GEYSER_PARTICLE, x, y, z, 0, 0, 0);
}
}
private static void serverTick(Level level, BlockPos worldPosition, BlockState state, BlockEntityHydrothermalVent blockEntity) {
boolean active = state.getValue(HydrothermalVentBlock.ACTIVATED);
POS.set(worldPosition).move(Direction.UP);
int height = active ? 85 : 25;
AABB box = new AABB(POS.offset(-1, 0, -1), POS.offset(1, height, 1));
List<LivingEntity> entities = level.getEntitiesOfClass(LivingEntity.class, box);
if (entities.size() > 0) {
while (POS.getY() < box.maxY) {
BlockState blockState = level.getBlockState(POS);
if (blockState.isSolidRender(level, POS)) break;
if (blockState.isAir()) {
double mult = active ? 3.0 : 5.0;
float force = (float) ((1.0 - (POS.getY() / box.maxY)) / mult);
entities.stream().filter(entity -> (int) entity.getY() == POS.getY() && blockEntity.hasElytra(entity) && entity.isFallFlying()).forEach(entity -> entity.moveRelative(force, POSITIVE_Y));
}
POS.move(Direction.UP);
}
}
}