Added a general-use fuzz function

This commit is contained in:
Sollace 2022-12-04 15:25:37 +00:00
parent fd7848e8e5
commit 06954aa779

View file

@ -1,7 +1,10 @@
package com.minelittlepony.unicopia.entity.player;
import java.util.Optional;
import com.minelittlepony.unicopia.entity.effect.SunBlindnessStatusEffect;
import net.minecraft.entity.ai.FuzzyPositions;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.SpawnLocating;
import net.minecraft.server.world.ServerWorld;
@ -10,6 +13,24 @@ import net.minecraft.util.math.random.Random;
public class SpawnLocator extends SpawnLocating {
public static BlockPos findSafeSpawnLocation(ServerWorld world, int x, int z) {
return SpawnLocating.findOverworldSpawn(world, x, z);
}
public static Optional<BlockPos> fuzz(ServerWorld world, BlockPos pos, int horizontal, int vertical) {
for (int attempt = 0; attempt < 6; attempt++) {
BlockPos target = FuzzyPositions.localFuzz(world.random, horizontal, vertical);
target = findSafeSpawnLocation(world, target.getX(), target.getZ());
if (target != null) {
return Optional.of(target);
}
}
return Optional.empty();
}
public static void selectSpawnPosition(ServerWorld world, PlayerEntity entity) {
BlockPos spawnPos = world.getSpawnPos();
int spawnRadius = Math.min(
@ -30,7 +51,7 @@ public class SpawnLocator extends SpawnLocating {
int x = tile % (spawnRadius * 2 + 1);
int z = tile / (spawnRadius * 2 + 1);
BlockPos candidatePos = findOverworldSpawn(world,
BlockPos candidatePos = findSafeSpawnLocation(world,
spawnPos.getX() + x - spawnRadius,
spawnPos.getZ() + z - spawnRadius
);