mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
refactor: refactor Adapter on Fabric
This commit is contained in:
3 files changed
+74
-37
No files matched your search
@@ -19,6 +19,7 @@
|
||||
|
||||
package net.william278.huskhomes;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.Getter;
|
||||
@@ -34,7 +35,11 @@ import net.fabricmc.loader.api.ModContainer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.platform.fabric.FabricServerAudiences;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.world.TeleportTarget;
|
||||
import net.william278.desertwell.util.Version;
|
||||
import net.william278.huskhomes.command.Command;
|
||||
import net.william278.huskhomes.command.FabricCommand;
|
||||
@@ -54,6 +59,7 @@ import net.william278.huskhomes.network.Broker;
|
||||
import net.william278.huskhomes.network.FabricPluginMessage;
|
||||
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;
|
||||
@@ -242,14 +248,10 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
||||
|
||||
@Override
|
||||
public void setWorldSpawn(@NotNull Position position) {
|
||||
minecraftServer.getWorlds().forEach(world -> {
|
||||
if (!world.getRegistryKey().getValue().toString().equals(position.getWorld().getName())) {
|
||||
return;
|
||||
}
|
||||
world.setSpawnPos(new BlockPos(
|
||||
(int) position.getX(), (int) position.getY(), (int) position.getZ()
|
||||
), position.getYaw());
|
||||
});
|
||||
final ServerWorld world = Adapter.adapt(position, minecraftServer);
|
||||
if (world != null) {
|
||||
world.setSpawnPos(BlockPos.ofFloored(Adapter.adapt(position).position), position.getYaw());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -301,11 +303,8 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
||||
@Override
|
||||
@NotNull
|
||||
public List<World> getWorlds() {
|
||||
final List<World> worlds = new ArrayList<>();
|
||||
minecraftServer.getWorlds().forEach(world -> worlds.add(World.from(
|
||||
world.getRegistryKey().getValue().asString(),
|
||||
UUID.nameUUIDFromBytes(world.getRegistryKey().getValue().asString().getBytes())
|
||||
)));
|
||||
final List<World> worlds = Lists.newArrayList();
|
||||
minecraftServer.getWorlds().forEach(world -> worlds.add(Adapter.adapt(world)));
|
||||
return worlds;
|
||||
}
|
||||
|
||||
@@ -385,4 +384,54 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
||||
return this;
|
||||
}
|
||||
|
||||
public static class Adapter {
|
||||
|
||||
@NotNull
|
||||
public static Location adapt(@NotNull Vec3d pos, @NotNull net.minecraft.world.World world,
|
||||
float yaw, float pitch) {
|
||||
return Position.at(
|
||||
pos.getX(), pos.getY(), pos.getZ(),
|
||||
yaw, pitch,
|
||||
adapt(world)
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Position adapt(@NotNull Vec3d pos, @NotNull net.minecraft.world.World world,
|
||||
float yaw, float pitch, @NotNull String server) {
|
||||
return Position.at(adapt(pos, world, yaw, pitch), server);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static TeleportTarget adapt(@NotNull Location location) {
|
||||
return new TeleportTarget(
|
||||
new Vec3d(location.getX(), location.getY(), location.getZ()),
|
||||
Vec3d.ZERO,
|
||||
location.getYaw(),
|
||||
location.getPitch()
|
||||
);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ServerWorld adapt(@NotNull World world, @NotNull MinecraftServer server) {
|
||||
return server.getWorld(server.getWorldRegistryKeys().stream()
|
||||
.filter(key -> key.getValue().equals(Identifier.tryParse(world.getName())))
|
||||
.findFirst().orElse(null));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ServerWorld adapt(@NotNull Location location, @NotNull MinecraftServer server) {
|
||||
return adapt(location.getWorld(), server);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static World adapt(@NotNull net.minecraft.world.World world) {
|
||||
return World.from(
|
||||
world.getRegistryKey().getRegistry().asMinimalString(),
|
||||
UUID.nameUUIDFromBytes(world.getRegistryKey().getValue().asString().getBytes())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,10 +26,8 @@ import net.minecraft.entity.Entity;
|
||||
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.world.TeleportTarget;
|
||||
import net.william278.huskhomes.FabricHuskHomes;
|
||||
import net.william278.huskhomes.network.FabricPluginMessage;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
@@ -57,13 +55,10 @@ public class FabricUser extends OnlineUser {
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return Position.at(
|
||||
player.getX(), player.getY(), player.getZ(),
|
||||
return FabricHuskHomes.Adapter.adapt(
|
||||
player.getPos(),
|
||||
player.getServerWorld(),
|
||||
player.getYaw(), player.getPitch(),
|
||||
World.from(
|
||||
player.getWorld().getRegistryKey().getValue().asString(),
|
||||
UUID.nameUUIDFromBytes(player.getWorld().getRegistryKey().getValue().asString().getBytes())
|
||||
),
|
||||
plugin.getServerName()
|
||||
);
|
||||
}
|
||||
@@ -141,23 +136,16 @@ public class FabricUser extends OnlineUser {
|
||||
throw new TeleportationException(TeleportationException.Type.ILLEGAL_TARGET_COORDINATES, plugin);
|
||||
}
|
||||
|
||||
// Dismount users
|
||||
player.stopRiding();
|
||||
player.getPassengerList().forEach(Entity::stopRiding);
|
||||
|
||||
FabricDimensions.teleport(
|
||||
player,
|
||||
server.getWorld(server.getWorldRegistryKeys().stream()
|
||||
.filter(key -> key.getValue().equals(Identifier.tryParse(location.getWorld().getName())))
|
||||
.findFirst().orElseThrow(
|
||||
() -> new TeleportationException(TeleportationException.Type.WORLD_NOT_FOUND, plugin)
|
||||
)),
|
||||
new TeleportTarget(
|
||||
new Vec3d(location.getX(), location.getY(), location.getZ()),
|
||||
Vec3d.ZERO,
|
||||
location.getYaw(),
|
||||
location.getPitch()
|
||||
)
|
||||
);
|
||||
// Adapt and teleport
|
||||
final ServerWorld world = FabricHuskHomes.Adapter.adapt(location.getWorld(), server);
|
||||
if (world == null) {
|
||||
throw new TeleportationException(TeleportationException.Type.WORLD_NOT_FOUND, plugin);
|
||||
}
|
||||
FabricDimensions.teleport(player, world, FabricHuskHomes.Adapter.adapt(location));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user