mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
refactor: refactor Adapter on Sponge
This commit is contained in:
7 files changed
+83
-142
No files matched your search
@@ -42,6 +42,7 @@ import net.william278.huskhomes.manager.Manager;
|
||||
import net.william278.huskhomes.network.Broker;
|
||||
import net.william278.huskhomes.network.PluginMessageBroker;
|
||||
import net.william278.huskhomes.network.RedisBroker;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.position.World;
|
||||
import net.william278.huskhomes.random.NormalDistributionEngine;
|
||||
@@ -50,13 +51,17 @@ import net.william278.huskhomes.user.ConsoleUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.SavedUser;
|
||||
import net.william278.huskhomes.user.SpongeUser;
|
||||
import net.william278.huskhomes.util.*;
|
||||
import net.william278.huskhomes.util.SpongeSafetyResolver;
|
||||
import net.william278.huskhomes.util.SpongeTask;
|
||||
import net.william278.huskhomes.util.UnsafeBlocks;
|
||||
import net.william278.huskhomes.util.Validator;
|
||||
import org.bstats.charts.SimplePie;
|
||||
import org.bstats.sponge.Metrics;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.ResourceKey;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.command.Command.Raw;
|
||||
import org.spongepowered.api.config.ConfigDir;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
@@ -69,6 +74,9 @@ import org.spongepowered.api.network.channel.ChannelBuf;
|
||||
import org.spongepowered.api.network.channel.raw.RawDataChannel;
|
||||
import org.spongepowered.api.network.channel.raw.play.RawPlayDataChannel;
|
||||
import org.spongepowered.api.network.channel.raw.play.RawPlayDataHandler;
|
||||
import org.spongepowered.api.world.WorldTypes;
|
||||
import org.spongepowered.api.world.server.ServerLocation;
|
||||
import org.spongepowered.api.world.server.ServerWorld;
|
||||
import org.spongepowered.plugin.PluginContainer;
|
||||
import org.spongepowered.plugin.builtin.jvm.Plugin;
|
||||
|
||||
@@ -232,9 +240,8 @@ public class SpongeHuskHomes implements HuskHomes, SpongeTask.Supplier, SpongeSa
|
||||
|
||||
@Override
|
||||
public void setWorldSpawn(@NotNull Position position) {
|
||||
SpongeAdapter.adaptLocation(position).ifPresent(
|
||||
loc -> loc.world().properties().setSpawnPosition(loc.blockPosition())
|
||||
);
|
||||
final ServerLocation loc = Adapter.adapt(position);
|
||||
loc.world().properties().setSpawnPosition(loc.blockPosition());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -394,4 +401,44 @@ public class SpongeHuskHomes implements HuskHomes, SpongeTask.Supplier, SpongeSa
|
||||
return this;
|
||||
}
|
||||
|
||||
public static final class Adapter {
|
||||
|
||||
@NotNull
|
||||
public static Location adapt(@NotNull ServerLocation location) {
|
||||
return Location.at(
|
||||
location.x(), location.y(), location.z(),
|
||||
adapt(location.world())
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Position adapt(@NotNull ServerLocation location, @NotNull String server) {
|
||||
return Position.at(adapt(location), server);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ServerLocation adapt(@NotNull Location location) {
|
||||
return ServerLocation.of(
|
||||
Objects.requireNonNull(adapt(location.getWorld())),
|
||||
location.getX(), location.getY(), location.getZ()
|
||||
);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ServerWorld adapt(@NotNull World world) {
|
||||
return Sponge.server().worldManager().world(ResourceKey.resolve(world.getName())).orElse(null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static World adapt(@NotNull ServerWorld world) {
|
||||
final String worldType = world.properties().worldType().key(WorldTypes.registry().type()
|
||||
.asDefaultedType(world::engine)).value();
|
||||
return World.from(
|
||||
world.properties().name(),
|
||||
world.uniqueId(),
|
||||
World.Environment.match(worldType)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -20,9 +20,7 @@
|
||||
package net.william278.huskhomes.listener;
|
||||
|
||||
import net.william278.huskhomes.SpongeHuskHomes;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.user.SpongeUser;
|
||||
import net.william278.huskhomes.util.SpongeAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.spongepowered.api.block.BlockType;
|
||||
import org.spongepowered.api.block.BlockTypes;
|
||||
@@ -80,11 +78,13 @@ public class SpongeEventListener extends EventListener {
|
||||
if (event.entity() instanceof ServerPlayer player) {
|
||||
final Optional<MovementType> type = event.context().get(EventContextKeys.MOVEMENT_TYPE);
|
||||
if (type.isPresent() && type.get().equals(MovementTypes.ENTITY_TELEPORT.get())) {
|
||||
SpongeAdapter.adaptLocation(ServerLocation.of(player.world(), event.originalPosition()))
|
||||
.ifPresent(location -> super.handlePlayerTeleport(
|
||||
SpongeUser.adapt(player, (SpongeHuskHomes) plugin),
|
||||
Position.at(location, plugin.getServerName())
|
||||
));
|
||||
super.handlePlayerTeleport(
|
||||
SpongeUser.adapt(player, (SpongeHuskHomes) plugin),
|
||||
SpongeHuskHomes.Adapter.adapt(
|
||||
ServerLocation.of(player.world(), event.originalPosition()),
|
||||
plugin.getServerName()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,11 +102,13 @@ public class SpongeEventListener extends EventListener {
|
||||
BlockTypes.PURPLE_BED, BlockTypes.RED_BED, BlockTypes.WHITE_BED, BlockTypes.YELLOW_BED,
|
||||
BlockTypes.RESPAWN_ANCHOR)) {
|
||||
|
||||
super.handlePlayerUpdateSpawnPoint(SpongeUser.adapt(player, (SpongeHuskHomes) plugin),
|
||||
Position.at(
|
||||
SpongeAdapter.adaptLocation(player.serverLocation()).orElseThrow(),
|
||||
super.handlePlayerUpdateSpawnPoint(
|
||||
SpongeUser.adapt(player, (SpongeHuskHomes) plugin),
|
||||
SpongeHuskHomes.Adapter.adapt(
|
||||
player.serverLocation(),
|
||||
plugin.getServerName()
|
||||
));
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,13 +23,12 @@ import net.kyori.adventure.audience.Audience;
|
||||
import net.william278.huskhomes.SpongeHuskHomes;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.util.SpongeAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.spongepowered.api.data.Keys;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
import org.spongepowered.api.service.permission.SubjectData;
|
||||
import org.spongepowered.api.util.RespawnLocation;
|
||||
import org.spongepowered.api.world.server.ServerLocation;
|
||||
import org.spongepowered.api.world.server.ServerWorld;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -67,20 +66,15 @@ public class SpongeUser extends OnlineUser {
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return Position.at(
|
||||
SpongeAdapter.adaptLocation(player.serverLocation())
|
||||
.orElseThrow(() -> new IllegalStateException("Failed to adapt player location")),
|
||||
plugin.getServerName()
|
||||
);
|
||||
return SpongeHuskHomes.Adapter.adapt(player.serverLocation(), plugin.getServerName());
|
||||
}
|
||||
|
||||
// Resolve the player's RespawnLocation from the world-key map and adapt as location
|
||||
@Override
|
||||
public Optional<Position> getBedSpawnPosition() {
|
||||
// Resolve the player's RespawnLocation from the world-key map and adapt as location
|
||||
return player.get(Keys.RESPAWN_LOCATIONS)
|
||||
.flatMap(resourceMap -> resourceMap.values().stream().findFirst())
|
||||
.flatMap(RespawnLocation::asLocation).flatMap(SpongeAdapter::adaptLocation)
|
||||
.map(location -> Position.at(location, plugin.getServerName()));
|
||||
.flatMap(resourceMap -> resourceMap.values().stream().findFirst()).flatMap(RespawnLocation::asLocation)
|
||||
.map(location -> SpongeHuskHomes.Adapter.adapt(location, plugin.getServerName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -119,13 +113,13 @@ public class SpongeUser extends OnlineUser {
|
||||
@Override
|
||||
public void teleportLocally(@NotNull Location location, boolean async) {
|
||||
plugin.runSync(() -> {
|
||||
final Optional<ServerLocation> serverLocation = SpongeAdapter.adaptLocation(location);
|
||||
if (serverLocation.isEmpty()) {
|
||||
final ServerWorld world = SpongeHuskHomes.Adapter.adapt(location.getWorld());
|
||||
if (world == null) {
|
||||
return;
|
||||
}
|
||||
player.vehicle().ifPresent(vehicle -> vehicle.get().passengers().remove(player));
|
||||
player.passengers().forEach(passenger -> player.passengers().remove(passenger));
|
||||
player.setLocation(serverLocation.get());
|
||||
player.setLocation(SpongeHuskHomes.Adapter.adapt(location));
|
||||
}, this);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,95 +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.registry.DefaultedRegistryType;
|
||||
import org.spongepowered.api.world.WorldType;
|
||||
import org.spongepowered.api.world.WorldTypes;
|
||||
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();
|
||||
}
|
||||
|
||||
// Get the world type from the world properties
|
||||
final DefaultedRegistryType<WorldType> registry = WorldTypes.registry().type().asDefaultedType(world::engine);
|
||||
return Optional.of(World.from(
|
||||
world.key().toString(),
|
||||
world.uniqueId(),
|
||||
switch (world.properties().worldType().key(registry).asString()) {
|
||||
case "minecraft:overworld" -> World.Environment.OVERWORLD;
|
||||
case "minecraft:the_nether" -> World.Environment.NETHER;
|
||||
case "minecraft:the_end" -> World.Environment.THE_END;
|
||||
default -> World.Environment.CUSTOM;
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
package net.william278.huskhomes.util;
|
||||
|
||||
import net.william278.huskhomes.SpongeHuskHomes;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.World;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -36,13 +37,8 @@ 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();
|
||||
final ServerLocation serverLocation = SpongeHuskHomes.Adapter.adapt(location);
|
||||
if (isInBorder(serverLocation.world().border(), serverLocation.blockPosition())) {
|
||||
return CompletableFuture.completedFuture(Optional.empty());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user