From e99631cd20b518307b1c1dc848f55b5ee6705d85 Mon Sep 17 00:00:00 2001 From: CodedSakura Date: Sat, 5 Aug 2023 13:37:42 +0200 Subject: [PATCH] add fallback to spawn option --- CHANGELOG.md | 5 ++ README.md | 8 +-- gradle.properties | 4 +- .../blossom/homes/BlossomHomes.java | 68 +++++++++++++++---- .../blossom/homes/BlossomHomesConfig.java | 2 + .../resources/data/blossom/lang/en_us.json | 4 +- 6 files changed, 70 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1b78bb..a3b17e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 2.2.0 + +* Add fallback to spawn option +* Update Chinese translations, courtesy of @Pau1am + # 2.1.0 * Add migration command from FabricHomes diff --git a/README.md b/README.md index 910b808..f9bd061 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,7 @@ BlossomHomes is a Minecraft Fabric mod in the Blossom-series mods that provides This mod's config file can be found at `config/BlossomMods/BlossomHomes.json`, after running the server with the mod at least once. -`teleportation`: [TeleportationConfig](https://github.com/BlossomMods/BlossomLib/blob/main/README.md#teleportationconfig) -- +`teleportation`: [TeleportationConfig](https://github.com/BlossomMods/BlossomLib/blob/main/README.md#teleportationconfig) - teleportation settings `standStill`: int - (seconds), how long the player has to stand still before being teleported `cooldown`: int - (seconds), how long the player has to wait after teleporting using this command, before @@ -29,7 +28,8 @@ being able to teleport again `defaultHome`: String - name of the default home `startHomes`: int - default max homes `dimensionBlacklist`: String[] - a list of dimension ids (like `minecraft:the_end`) in which a player can't set a home -`useBlacklistAsWhitelist`: boolean - invert blacklist to function as a whitelist +`useBlacklistAsWhitelist`: boolean - invert blacklist to function as a whitelist +`fallbackToPlayerSpawnPoint`: boolean - use player spawn point if no default home set ## Commands & their permissions @@ -90,4 +90,4 @@ only keys with available arguments are shown, for full list, please see - `blossom.homes.setMax`: 2 arguments - new max, players - `blossom.homes.load-legacy.done`: 2 arguments - home count, player count -`zh_cn` (Chinese, Simplified), `zh_tw` (Chinese, Traditional) - added by @BackWheel +`zh_cn` (Chinese, Simplified), `zh_tw` (Chinese, Traditional) - added by @BackWheel, updated by @Pau1am diff --git a/gradle.properties b/gradle.properties index 04f8e10..0211c0f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx1G # Mod Properties -mod_version=2.1.0 +mod_version=2.2.0 maven_group=dev.codedsakura.blossom mod_slug=BlossomHomes archives_base_name=blossom-homes @@ -15,4 +15,4 @@ yarn_mappings=build.1 loader_version=0.14.21 # Dependencies -blossomlib_version=2.5.1 +blossomlib_version=2.5.4 diff --git a/src/main/java/dev/codedsakura/blossom/homes/BlossomHomes.java b/src/main/java/dev/codedsakura/blossom/homes/BlossomHomes.java index 8fcb689..c2aa26c 100644 --- a/src/main/java/dev/codedsakura/blossom/homes/BlossomHomes.java +++ b/src/main/java/dev/codedsakura/blossom/homes/BlossomHomes.java @@ -16,13 +16,17 @@ import dev.codedsakura.blossom.lib.text.TextUtils; import dev.codedsakura.blossom.lib.utils.CustomLogger; import net.fabricmc.api.ModInitializer; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; import net.minecraft.command.argument.DimensionArgumentType; import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.command.argument.RotationArgumentType; import net.minecraft.command.argument.Vec3ArgumentType; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtIo; +import net.minecraft.registry.tag.BlockTags; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; @@ -30,6 +34,8 @@ import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.util.WorldSavePath; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec2f; import net.minecraft.util.math.Vec3d; import org.apache.commons.io.FilenameUtils; @@ -37,10 +43,7 @@ import java.io.File; import java.io.IOException; -import java.util.Collection; -import java.util.List; -import java.util.Objects; -import java.util.UUID; +import java.util.*; import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; @@ -142,7 +145,7 @@ private int listHomes(CommandContext ctx) throws CommandSyn List homes = homeController.findPlayerHomes(player); - if (homes.size() == 0) { + if (homes.isEmpty()) { TextUtils.send(ctx, "blossom.homes.list.empty", homeController.getMaxHomes(player)); return Command.SINGLE_SUCCESS; } @@ -177,18 +180,55 @@ private int runHome(CommandContext ctx, String homeName) th LOGGER.trace("home player {} to {}", player, home); if (home == null) { - TextUtils.sendErr(ctx, "blossom.homes.not-found", homeName); - } else { - TeleportUtils.teleport( - CONFIG.teleportation, - CONFIG.standStill, - CONFIG.cooldown, - BlossomHomes.class, - player, - () -> home.toDestination(ctx.getSource().getServer()) + if (!(homeName.equals(CONFIG.defaultHome) && CONFIG.fallbackToPlayerSpawnPoint)) { + TextUtils.sendErr(ctx, "blossom.homes.not-found", homeName); + return Command.SINGLE_SUCCESS; + } + + // See PlayerManager.respawnPlayer (viewed 1.20) + MinecraftServer server = player.getServer(); + assert server != null; + BlockPos blockPos = player.getSpawnPointPosition(); + float spawnAngle = player.getSpawnAngle(); + boolean spawnForced = player.isSpawnForced(); + ServerWorld spawnWorld = server.getWorld(player.getSpawnPointDimension()); + Optional position = spawnWorld != null && blockPos != null ? PlayerEntity.findRespawnPosition(spawnWorld, blockPos, spawnAngle, spawnForced, true) : Optional.empty(); + + if (position.isEmpty()) { + TextUtils.sendErr(ctx, "blossom.homes.spawn.not-found"); + return Command.SINGLE_SUCCESS; + } + + BlockState blockState = spawnWorld.getBlockState(blockPos); + if (blockState.isIn(BlockTags.BEDS) || blockState.isOf(Blocks.RESPAWN_ANCHOR)) { + Vec3d vec3d2 = Vec3d.ofBottomCenter(blockPos).subtract(position.get()).normalize(); + spawnAngle = (float) MathHelper.wrapDegrees(MathHelper.atan2(vec3d2.z, vec3d2.x) * 57.2957763671875 - 90.0); + } + LOGGER.trace("found spawn position for {} @ {}", player.getUuidAsString(), position.get()); + + TextUtils.sendWarn(ctx, "blossom.homes.spawn"); + home = new Home( + CONFIG.defaultHome, + spawnWorld.getRegistryKey().getValue().toString(), + position.get().getX(), + position.get().getY(), + position.get().getZ(), + spawnAngle, + 0 ); } + Home finalHome = home; + TeleportUtils.teleport( + CONFIG.teleportation, + CONFIG.standStill, + CONFIG.cooldown, + BlossomHomes.class, + player, + () -> finalHome.toDestination(ctx.getSource().getServer()) + ); + + return Command.SINGLE_SUCCESS; } diff --git a/src/main/java/dev/codedsakura/blossom/homes/BlossomHomesConfig.java b/src/main/java/dev/codedsakura/blossom/homes/BlossomHomesConfig.java index caf399b..93708f5 100644 --- a/src/main/java/dev/codedsakura/blossom/homes/BlossomHomesConfig.java +++ b/src/main/java/dev/codedsakura/blossom/homes/BlossomHomesConfig.java @@ -16,4 +16,6 @@ public class BlossomHomesConfig { List dimensionBlacklist = List.of(); boolean useBlacklistAsWhitelist = false; + + boolean fallbackToPlayerSpawnPoint = true; } diff --git a/src/main/resources/data/blossom/lang/en_us.json b/src/main/resources/data/blossom/lang/en_us.json index eba637b..3f9f4c9 100644 --- a/src/main/resources/data/blossom/lang/en_us.json +++ b/src/main/resources/data/blossom/lang/en_us.json @@ -16,5 +16,7 @@ "blossom.homes.setMax.delimiter": ",", "blossom.homes.load-legacy.info": "Loading legacy homes from FabricHomes...", "blossom.homes.load-legacy.overwrite": "Overwriting existing homes", - "blossom.homes.load-legacy.done": "Done! Loaded %s homes from %s players." + "blossom.homes.load-legacy.done": "Done! Loaded %s homes from %s players.", + "blossom.homes.spawn.not-found": "Did not find a valid spawn point as fallback!", + "blossom.homes.spawn": "Teleporting to spawn point!" }