Add safety resolver to sponge, fix fabric permissions (#355)

This commit is contained in:
William authored and GitHub committed 2023-04-15 16:22:44 +01:00
1 parent be43fbc345
commit 67670a552d
4 files changed
+35 -10

No files matched your search

@@ -21,6 +21,10 @@ package net.william278.huskhomes.util;
import net.william278.huskhomes.position.Location;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.registry.RegistryTypes;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.math.vector.Vector3i;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@@ -29,7 +33,28 @@ public interface SpongeSafetyResolver extends SafetyResolver {
@Override
default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
return CompletableFuture.completedFuture(Optional.of(location)); // todo
final Optional<ServerLocation> adaptedLocation = SpongeAdapter.adaptLocation(location);
if (adaptedLocation.isEmpty()) {
return CompletableFuture.completedFuture(Optional.empty());
}
final ServerLocation serverLocation = adaptedLocation.get();
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
final Vector3i cursor = serverLocation.asHighestLocation().blockPosition().add(x, 0, z);
final BlockState blockState = serverLocation.world().block(cursor);
if (isBlockSafe(blockState.type().key(RegistryTypes.BLOCK_TYPE).asString())) {
return CompletableFuture.completedFuture(Optional.of(Location.at(
cursor.x() + 0.5,
cursor.y() + 1,
cursor.z() + 0.5,
location.getWorld()
)));
}
}
}
return CompletableFuture.completedFuture(Optional.empty());
}
}
}