Skip to content

Commit

Permalink
refactor: refactor Adapter on Fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed May 30, 2024
1 parent 7ede171 commit 3c0e931
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public CommandRegistration getCommandRegistrar() {

@Override
@NotNull
public HuskHomes getPlugin() {
public BukkitHuskHomes getPlugin() {
return this;
}

Expand Down
75 changes: 62 additions & 13 deletions fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +35,11 @@
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;
Expand All @@ -54,6 +59,7 @@
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;
Expand Down Expand Up @@ -242,14 +248,10 @@ public Audience getAudience(@NotNull UUID user) {

@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
Expand Down Expand Up @@ -301,11 +303,8 @@ public Path getConfigDirectory() {
@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;
}

Expand Down Expand Up @@ -385,4 +384,54 @@ public FabricHuskHomes getPlugin() {
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())
);
}

}

}
34 changes: 11 additions & 23 deletions fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
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;
Expand Down Expand Up @@ -57,13 +55,10 @@ public static FabricUser adapt(@NotNull ServerPlayerEntity player, @NotNull Fabr

@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()
);
}
Expand Down Expand Up @@ -141,23 +136,16 @@ public void teleportLocally(@NotNull Location location, boolean async) throws Te
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
Expand Down

0 comments on commit 3c0e931

Please sign in to comment.