Skip to content

Commit

Permalink
add fallback to spawn option
Browse files Browse the repository at this point in the history
  • Loading branch information
CodedSakura committed Aug 5, 2023
1 parent 7487608 commit e99631c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ 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
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

Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,4 +15,4 @@ yarn_mappings=build.1
loader_version=0.14.21

# Dependencies
blossomlib_version=2.5.1
blossomlib_version=2.5.4
68 changes: 54 additions & 14 deletions src/main/java/dev/codedsakura/blossom/homes/BlossomHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,34 @@
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;
import net.minecraft.server.world.ServerWorld;
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;
import org.apache.logging.log4j.core.Logger;

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;
Expand Down Expand Up @@ -142,7 +145,7 @@ private int listHomes(CommandContext<ServerCommandSource> ctx) throws CommandSyn

List<Home> 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;
}
Expand Down Expand Up @@ -177,18 +180,55 @@ private int runHome(CommandContext<ServerCommandSource> 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<Vec3d> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class BlossomHomesConfig {

List<String> dimensionBlacklist = List.of();
boolean useBlacklistAsWhitelist = false;

boolean fallbackToPlayerSpawnPoint = true;
}
4 changes: 3 additions & 1 deletion src/main/resources/data/blossom/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
}

0 comments on commit e99631c

Please sign in to comment.