Add fabric builds for older MC versions & add checkstyle (#449)

* Remove instance getters, improve command logic, register APi

* Publish multiple fabric builds, enforce checkstyle

* Bump guava

* Bump to 4.4.2
This commit is contained in:
William authored and GitHub committed 2023-07-02 13:26:12 +01:00
1 parent 51baf196b3
commit c9f5262292
219 files changed
+8032 -1392

No files matched your search

@@ -1,88 +0,0 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <[email protected]>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.william278.huskhomes.util;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.world.WorldTypeTemplate;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.api.world.server.WorldManager;
import java.util.Optional;
public final class SpongeAdapter {
/**
* Adapt a Sponge {@link ServerLocation} to a HuskHomes {@link Location}
*
* @param location the Sponge {@link ServerLocation} to adapt
* @return the adapted {@link Location}
*/
public static Optional<ServerLocation> adaptLocation(@NotNull Location location) {
final WorldManager worldManager = Sponge.server().worldManager();
return worldManager
.world(ResourceKey.resolve(location.getWorld().getName()))
.map(world -> ServerLocation.of(world, location.getX(), location.getY(), location.getZ()))
.or(() -> {
final Optional<ResourceKey> worldKey = worldManager.worldKey(location.getWorld().getUuid());
return worldKey.flatMap(resourceKey -> worldManager.world(resourceKey)
.map(world -> ServerLocation.of(world, location.getX(), location.getY(), location.getZ())));
});
}
/**
* Adapt a HuskHomes {@link Location} to a Sponge {@link ServerLocation}
*
* @param location the HuskHomes {@link Location} to adapt
* @return the adapted {@link ServerLocation}
*/
public static Optional<Location> adaptLocation(@NotNull ServerLocation location) {
return Optional.of(Location.at(
location.x(), location.y(), location.z(),
adaptWorld(location.world()).orElse(new World()))
);
}
/**
* Adapt a Sponge {@link ServerWorld} to a HuskHomes {@link World}
*
* @param world the Sponge {@link ServerWorld} to adapt
* @return the adapted {@link World}
*/
public static Optional<World> adaptWorld(@Nullable ServerWorld world) {
if (world == null) {
return Optional.empty();
}
final String worldType = world.properties().worldType().asTemplate().key().asString();
return Optional.of(World.from(
world.key().toString(), world.uniqueId(),
(worldType.equals(WorldTypeTemplate.theNether().key().toString()) ? World.Environment.NETHER
: worldType.equals(WorldTypeTemplate.theEnd().key().toString()) ? World.Environment.THE_END
: worldType.equals(WorldTypeTemplate.overworld().key().toString()) ? World.Environment.OVERWORLD
: World.Environment.CUSTOM)
));
}
}
@@ -1,104 +0,0 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <[email protected]>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.william278.huskhomes.util;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.World;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.registry.RegistryTypes;
import org.spongepowered.api.world.border.WorldBorder;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.math.vector.Vector2d;
import org.spongepowered.math.vector.Vector3i;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public interface SpongeSafetyResolver extends SafetyResolver {
@Override
default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
final Optional<ServerLocation> adaptedLocation = SpongeAdapter.adaptLocation(location);
if (adaptedLocation.isEmpty()) {
return CompletableFuture.completedFuture(Optional.empty());
}
// Ensure the location is within the world border
final ServerLocation serverLocation = adaptedLocation.get();
if (isInBorder(serverLocation.world().border(), serverLocation.blockPosition())) {
return CompletableFuture.completedFuture(Optional.empty());
}
return CompletableFuture.completedFuture(findSafeLocationNear(serverLocation, location.getWorld()));
}
/**
* Search for a safe ground location near the given location
*
* @param location The location to search around
* @param world The world to search in
* @return An optional safe location, within 4 blocks of the given location
*/
private Optional<Location> findSafeLocationNear(@NotNull ServerLocation location, @NotNull World world) {
for (int x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) {
for (int z = -SEARCH_RADIUS; z <= SEARCH_RADIUS; z++) {
final Vector3i cursor = location.asHighestLocation().blockPosition().add(x, -1, z);
final Vector3i cursorBody = location.asHighestLocation().blockPosition().add(x, 0, z);
final Vector3i cursorHead = location.asHighestLocation().blockPosition().add(x, 1, z);
final BlockState blockState = location.world().block(cursor);
final BlockState bodyBlockState = location.world().block(cursorBody);
final BlockState headBlockState = location.world().block(cursorHead);
if (isBlockSafeForStanding(blockState.type().key(RegistryTypes.BLOCK_TYPE).asString())
&& isBlockSafeForOccupation(bodyBlockState.type().key(RegistryTypes.BLOCK_TYPE).asString())
&& isBlockSafeForOccupation(headBlockState.type().key(RegistryTypes.BLOCK_TYPE).asString())) {
double locx = Math.floor(cursor.x());
if (locx < 0) {
locx += 1.5d;
} else {
locx = locx + 0.5d;
}
double locz = Math.floor(cursor.z());
if (locz < 0) {
locz += 1.5d;
} else {
locz = locz + 0.5d;
}
return Optional.of(Location.at(
locx,
cursor.y(),
locz,
world
));
}
}
}
return Optional.empty();
}
private boolean isInBorder(WorldBorder border, Vector3i position) {
final Vector2d center = border.center();
final double radius = border.diameter() / 2;
final double x = position.x() - center.x();
final double z = position.z() - center.y();
return x * x + z * z < radius * radius;
}
}
@@ -1,145 +0,0 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <[email protected]>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.william278.huskhomes.util;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.SpongeHuskHomes;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.api.scheduler.ScheduledTask;
import org.spongepowered.api.util.Ticks;
import static org.spongepowered.api.scheduler.Task.builder;
public interface SpongeTask extends Task {
class Sync extends Task.Sync implements SpongeTask {
private ScheduledTask task;
protected Sync(@NotNull HuskHomes plugin, @NotNull Runnable runnable, long delayTicks) {
super(plugin, runnable, delayTicks);
}
@Override
public void cancel() {
if (task != null && !cancelled) {
task.cancel();
}
super.cancel();
}
@Override
public void run() {
if (!cancelled) {
final SpongeHuskHomes plugin = ((SpongeHuskHomes) getPlugin());
this.task = plugin.getGame().server().scheduler().submit(builder()
.plugin(plugin.getPluginContainer())
.delay(Ticks.of(delayTicks))
.execute(runnable)
.build());
}
}
}
class Async extends Task.Async implements SpongeTask {
private ScheduledTask task;
protected Async(@NotNull HuskHomes plugin, @NotNull Runnable runnable) {
super(plugin, runnable);
}
@Override
public void cancel() {
if (task != null && !cancelled) {
task.cancel();
}
super.cancel();
}
@Override
public void run() {
if (!cancelled) {
final SpongeHuskHomes plugin = ((SpongeHuskHomes) getPlugin());
this.task = plugin.getGame().asyncScheduler().submit(builder()
.plugin(plugin.getPluginContainer())
.execute(runnable)
.build());
}
}
}
class Repeating extends Task.Repeating implements SpongeTask {
private ScheduledTask task;
protected Repeating(@NotNull HuskHomes plugin, @NotNull Runnable runnable, long repeatingTicks) {
super(plugin, runnable, repeatingTicks);
}
@Override
public void cancel() {
if (task != null && !cancelled) {
task.cancel();
}
super.cancel();
}
@Override
public void run() {
if (!cancelled) {
final SpongeHuskHomes plugin = ((SpongeHuskHomes) getPlugin());
this.task = plugin.getGame().asyncScheduler().submit(builder()
.plugin(plugin.getPluginContainer())
.interval(Ticks.of(repeatingTicks))
.execute(runnable)
.build());
}
}
}
interface Supplier extends Task.Supplier {
@NotNull
@Override
default Task.Sync getSyncTask(@NotNull Runnable runnable, long delayTicks) {
return new Sync(getPlugin(), runnable, delayTicks);
}
@NotNull
@Override
default Task.Async getAsyncTask(@NotNull Runnable runnable) {
return new Async(getPlugin(), runnable);
}
@NotNull
@Override
default Task.Repeating getRepeatingTask(@NotNull Runnable runnable, long repeatingTicks) {
return new Repeating(getPlugin(), runnable, repeatingTicks);
}
@Override
default void cancelTasks() {
// Do nothing
}
}
}