feat: target Minecraft 1.21 on Fabric, add API

Also adjusts how the HuskHomes API on Fabric is versioned. Now has the pattern: `net.william278.huskhomes:huskhomes-fabric:<version>+<mc_version>`

Closes #640
This commit is contained in:
William committed 2024-06-24 11:43:03 +01:00
1 parent 33d9868457
commit 1a258264f8
11 files changed
+258 -75

No files matched your search

@@ -34,6 +34,8 @@ import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.fabric.FabricServerAudiences;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
@@ -41,6 +43,7 @@ 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.api.FabricHuskHomesAPI;
import net.william278.huskhomes.command.Command;
import net.william278.huskhomes.command.FabricCommand;
import net.william278.huskhomes.config.Locales;
@@ -192,6 +195,9 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
// Register events
initialize("events", (plugin) -> this.eventListener = new FabricEventListener(this));
// Initialize the API
initialize("API", (plugin) -> FabricHuskHomesAPI.register(this));
this.checkForUpdates();
}
@@ -250,7 +256,10 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
public void setWorldSpawn(@NotNull Position position) {
final ServerWorld world = Adapter.adapt(position, minecraftServer);
if (world != null) {
world.setSpawnPos(BlockPos.ofFloored(Adapter.adapt(position).position), position.getYaw());
world.setSpawnPos(
BlockPos.ofFloored(position.getX(), position.getY(), position.getZ()),
position.getYaw()
);
}
}
@@ -403,25 +412,28 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
}
@NotNull
public static TeleportTarget adapt(@NotNull Location location) {
public static TeleportTarget adapt(@NotNull Location location, @NotNull MinecraftServer server) {
return new TeleportTarget(
adapt(location.getWorld(), server),
new Vec3d(location.getX(), location.getY(), location.getZ()),
Vec3d.ZERO,
location.getYaw(),
location.getPitch()
location.getPitch(),
entity -> entity.refreshPositionAndAngles(
location.getX(), location.getY(), location.getZ(),
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));
return server.getWorld(RegistryKey.of(RegistryKeys.WORLD, Identifier.tryParse(world.getName())));
}
@Nullable
public static ServerWorld adapt(@NotNull Location location, @NotNull MinecraftServer server) {
return adapt(location.getWorld(), server);
public static ServerWorld adapt(@NotNull Position position, @NotNull MinecraftServer server) {
return adapt(position.getWorld(), server);
}
@NotNull
@@ -0,0 +1,173 @@
/*
* 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.api;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.TeleportTarget;
import net.william278.huskhomes.FabricHuskHomes;
import net.william278.huskhomes.config.Server;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.user.FabricUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.User;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* The HuskHomes API implementation for the Fabric platform, providing methods to access ServerPlayerEntity data,
* homes, warps and process teleports
*
* <p>Retrieve an instance of the API class via {@link #getInstance()}.
*
* @since 4.7
*/
@SuppressWarnings("unused")
public class FabricHuskHomesAPI extends BaseHuskHomesAPI {
/**
* <b>(Internal use only)</b> - Constructor, instantiating the API.
*/
@ApiStatus.Internal
private FabricHuskHomesAPI(@NotNull FabricHuskHomes plugin) {
super(plugin);
}
/**
* Get an instance of the HuskHomes API.
*
* @return instance of the HuskHomes API
* @throws NotRegisteredException if the API has not yet been registered.
*/
@NotNull
public static FabricHuskHomesAPI getInstance() throws NotRegisteredException {
return (FabricHuskHomesAPI) BaseHuskHomesAPI.getInstance();
}
/**
* <b>(Internal use only)</b> - Register the API for this platform.
*
* @param plugin the plugin instance
*/
@ApiStatus.Internal
public static void register(@NotNull FabricHuskHomes plugin) {
instance = new FabricHuskHomesAPI(plugin);
}
/**
* Returns an {@link OnlineUser} instance for the given Fabric {@link ServerPlayerEntity}.
*
* @param player the Fabric ServerPlayerEntity to get the {@link User} instance for
* @return the {@link OnlineUser} instance for the given Fabric {@link ServerPlayerEntity}
* @since 4.7
*/
@NotNull
public OnlineUser adaptUser(@NotNull ServerPlayerEntity player) {
return FabricUser.adapt(player, (FabricHuskHomes) plugin);
}
/**
* Returns the Fabric {@link ServerPlayerEntity} being represented by the given {@link OnlineUser}.
*
* @param user {@link OnlineUser} to get the Fabric ServerPlayerEntity from
* @return the Fabric {@link ServerPlayerEntity} being represented by the given {@link OnlineUser}
* @since 4.7
*/
@NotNull
public ServerPlayerEntity getServerPlayerEntity(@NotNull OnlineUser user) {
return ((FabricUser) user).getPlayer();
}
/**
* Returns the Fabric {@link Location} being represented by the given {@link Position}.
*
* @param position the {@link Position} to get the Fabric location from
* @return the fabric {@link ServerWorld} being represented by the given {@link Position}
* @since 4.7
*/
@Nullable
public ServerWorld getServerWorld(@NotNull World position) {
return FabricHuskHomes.Adapter.adapt(position, ((FabricHuskHomes) plugin).getMinecraftServer());
}
/**
* Returns the Fabric {@link TeleportTarget} being represented by the given {@link Location}.
*
* @param location the {@link Location} to get the {@link TeleportTarget} from
* @return the {@link TeleportTarget} being represented by the given {@link Location}
* @since 4.7
*/
@Nullable
public TeleportTarget getTeleportTarget(@NotNull Location location) {
return FabricHuskHomes.Adapter.adapt(location, ((FabricHuskHomes) plugin).getMinecraftServer());
}
/**
* Returns a {@link Location} instance for the given Fabric {@link Location} on the server.
*
* @return the {@link Location} instance for the given Fabric {@link Location}
* @since 4.7
*/
@Nullable
public Location adaptLocation(@NotNull Vec3d pos, @NotNull net.minecraft.world.World world,
float yaw, float pitch) {
return FabricHuskHomes.Adapter.adapt(pos, world, yaw, pitch);
}
/**
* Returns a {@link Position} instance for the given Fabric {@link Location} on the server.
*
* @param pos the {@link Vec3d} position to get the {@link Position} instance for
* @param world the world the position is on
* @param yaw the yaw of the position
* @param pitch the pitch of the position
* @param server the {@link Server} the position is on
* @return the {@link Position} instance for the given Fabric {@link Location} on the given {@link Server}
* @see Position#getServer() to get the server the position is on
* @since 4.7
*/
@NotNull
public Position adaptPosition(@NotNull Vec3d pos, @NotNull net.minecraft.world.World world,
float yaw, float pitch, @NotNull String server) {
return FabricHuskHomes.Adapter.adapt(pos, world, yaw, pitch, server);
}
/**
* Returns a {@link Position} instance for the given Fabric {@link Location} on the server.
*
* @param pos the {@link Vec3d} position to get the {@link Position} instance for
* @param world the world the position is on
* @param yaw the yaw of the position
* @param pitch the pitch of the position
* @return the {@link Position} instance for the given Fabric {@link Location} on the given {@link Server}
* @see Position#getServer() to get the server the position is on
* @since 4.7
*/
@NotNull
public Position adaptPosition(@NotNull Vec3d pos, @NotNull net.minecraft.world.World world,
float yaw, float pitch) {
return FabricHuskHomes.Adapter.adapt(pos, world, yaw, pitch, getServer());
}
}
@@ -39,7 +39,7 @@ public class FabricPlaceholderAPIHook extends Hook {
@SuppressWarnings("SameParameterValue")
@NotNull
private Identifier createIdentifier(@NotNull String name) {
return new Identifier("huskhomes", name);
return Identifier.of("huskhomes", name);
}
@Override
@@ -20,7 +20,6 @@
package net.william278.huskhomes.user;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fabricmc.fabric.api.dimension.v1.FabricDimensions;
import net.kyori.adventure.audience.Audience;
import net.minecraft.entity.Entity;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
@@ -145,7 +144,7 @@ public class FabricUser extends OnlineUser {
if (world == null) {
throw new TeleportationException(TeleportationException.Type.WORLD_NOT_FOUND, plugin);
}
FabricDimensions.teleport(player, world, FabricHuskHomes.Adapter.adapt(location));
player.teleportTo(FabricHuskHomes.Adapter.adapt(location, server));
}
@Override
@@ -178,4 +177,9 @@ public class FabricUser extends OnlineUser {
plugin.runSyncDelayed(() -> player.setInvulnerable(false), this, invulnerabilityTimeInTicks);
}
@NotNull
public ServerPlayerEntity getPlayer() {
return player;
}
}