mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 22:17:17 +00:00
Add Sponge (API v8) server mod support (#354)
This commit is contained in:
1 parent
059fc638c6
commit
be43fbc345
35 files changed
+2603
-9
No files matched your search
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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)
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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 org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface SpongeSafetyResolver extends SafetyResolver {
|
||||
|
||||
@Override
|
||||
default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
|
||||
return CompletableFuture.completedFuture(Optional.of(location)); // todo
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* 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.SpongeHuskHomes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.spongepowered.api.scheduler.Task;
|
||||
import org.spongepowered.api.util.Ticks;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public interface SpongeTaskRunner extends TaskRunner {
|
||||
|
||||
Map<Integer, CancellableRunnable> tasks = new HashMap<>();
|
||||
|
||||
@Override
|
||||
default int runAsync(@NotNull Runnable runnable) {
|
||||
final CancellableRunnable task = wrap(runnable);
|
||||
final int taskId = tasks.size();
|
||||
tasks.put(taskId, task);
|
||||
|
||||
getPlugin().getGame().asyncScheduler()
|
||||
.submit(Task.builder()
|
||||
.plugin(getPlugin().getPluginContainer())
|
||||
.execute(task)
|
||||
.build());
|
||||
|
||||
return taskId;
|
||||
}
|
||||
|
||||
@Override
|
||||
default <T> CompletableFuture<T> supplyAsync(@NotNull Supplier<T> supplier) {
|
||||
final CompletableFuture<T> future = new CompletableFuture<>();
|
||||
|
||||
getPlugin().getGame().asyncScheduler()
|
||||
.submit(Task.builder()
|
||||
.plugin(getPlugin().getPluginContainer())
|
||||
.execute(() -> future.complete(supplier.get()))
|
||||
.build());
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
default void runSync(@NotNull Runnable runnable) {
|
||||
getPlugin().getGame().server().scheduler()
|
||||
.submit(Task.builder()
|
||||
.plugin(getPlugin().getPluginContainer())
|
||||
.execute(runnable)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
default int runAsyncRepeating(@NotNull Runnable runnable, long delay) {
|
||||
final CancellableRunnable task = wrap(runnable);
|
||||
final int taskId = tasks.size();
|
||||
tasks.put(taskId, task);
|
||||
|
||||
getPlugin().getGame().asyncScheduler()
|
||||
.submit(Task.builder()
|
||||
.plugin(getPlugin().getPluginContainer())
|
||||
.execute(task)
|
||||
.interval(Ticks.of(delay))
|
||||
.build());
|
||||
|
||||
return taskId;
|
||||
}
|
||||
|
||||
@Override
|
||||
default void runLater(@NotNull Runnable runnable, long delay) {
|
||||
getPlugin().getGame().server().scheduler()
|
||||
.submit(Task.builder()
|
||||
.plugin(getPlugin().getPluginContainer())
|
||||
.execute(runnable)
|
||||
.delay(Ticks.of(delay))
|
||||
.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
default void cancelTask(int taskId) {
|
||||
if (tasks.containsKey(taskId)) {
|
||||
tasks.get(taskId).cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
default void cancelAllTasks() {
|
||||
tasks.values().forEach(CancellableRunnable::cancel);
|
||||
tasks.clear();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CancellableRunnable wrap(@NotNull Runnable runnable) {
|
||||
return new CancellableRunnable(runnable);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
SpongeHuskHomes getPlugin();
|
||||
|
||||
/**
|
||||
* A wrapper for a {@link Runnable} that can be cancelled
|
||||
*/
|
||||
class CancellableRunnable implements Runnable {
|
||||
|
||||
private final Runnable runnable;
|
||||
private boolean cancelled = false;
|
||||
|
||||
private CancellableRunnable(Runnable runnable) {
|
||||
this.runnable = runnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (cancelled) {
|
||||
return;
|
||||
}
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
cancelled = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user