diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ac07bc..a105aa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,14 +8,15 @@ and this project kind of adheres to [Semantic Versioning](https://semver.org/spe #### Changed - the loading logic of the storage file. - most static classes to non-static, should prevent leaks. -- Changed out java Timers for a tick-based timer. +- changed out java Timers for a tick-based timer (so that it follows the server tick speed). +- fixed the before-teleportation effects not being sent into the correct dimension when switching dimension. #### Added - **a config file!** - The config file starts at **version 0**! - versioning to the storage file for better handling of migrations. - We start at **version 1**, all previous version are version 0. - +- caching for loading the translation files! ### [1.3.4] - Updated Italian translations (Thanks to [Jump-333](https://github.com/Jump-333)!) diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/Constants.java b/common/src/main/java/dev/mrsnowy/teleport_commands/Constants.java index 0be1bf0..2048f1e 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/Constants.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/Constants.java @@ -6,7 +6,7 @@ import org.slf4j.LoggerFactory; public class Constants { public static final String MOD_ID = "teleport_commands"; public static final String MOD_NAME = "Teleport Commands"; - public static final String VERSION = "1.3.4"; + public static final String VERSION = "1.4.0"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME); } diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/TeleportCommands.java b/common/src/main/java/dev/mrsnowy/teleport_commands/TeleportCommands.java index df14cd6..5a7ba32 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/TeleportCommands.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/TeleportCommands.java @@ -4,8 +4,8 @@ import com.mojang.brigadier.CommandDispatcher; import dev.mrsnowy.teleport_commands.storage.StorageManager; import dev.mrsnowy.teleport_commands.commands.*; import dev.mrsnowy.teleport_commands.storage.DeathLocationStorage; -import dev.mrsnowy.teleport_commands.storage.configManager; -import dev.mrsnowy.teleport_commands.utils.teleporter; +import dev.mrsnowy.teleport_commands.storage.ConfigManager; +import dev.mrsnowy.teleport_commands.utils.Teleporter; import net.minecraft.commands.CommandSourceStack; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; @@ -22,11 +22,11 @@ public class TeleportCommands { public Path configDir; public MinecraftServer server; public StorageManager storageManager; - public configManager config; + public ConfigManager config; public DeathLocationStorage deathLocationStorage; - public teleporter teleporter; + public Teleporter teleporter; - // Gets ran when the server starts, initializes the mod :3 + /// Gets ran when the server starts, initializes the mod :3 public void initializeMod(MinecraftServer server) { INSTANCE = this; Constants.LOGGER.info("Initializing Teleport Commands (V{})! Hello {}!", Constants.VERSION, modLoader); @@ -36,14 +36,14 @@ public class TeleportCommands { this.server = server; storageManager = new StorageManager(this); - config = new configManager(this); + config = new ConfigManager(this); deathLocationStorage = new DeathLocationStorage(); - teleporter = new teleporter(this); + teleporter = new Teleporter(this); } - // initialize commands, also allows me to easily disable any when there is a config + /// initialize commands, also allows me to easily disable any when there is a config public void registerCommands(CommandDispatcher dispatcher) { - new back(dispatcher, this); + back.register(dispatcher); home.register(dispatcher); new tpa(dispatcher, this); new warp(dispatcher, this); @@ -51,7 +51,7 @@ public class TeleportCommands { main.register(dispatcher); } - // Runs when the playerDeath mixin calls it, updates the /back command position + /// Runs when the playerDeath mixin calls it, updates the /back command position public void onPlayerDeath(ServerPlayer player) { BlockPos pos = new BlockPos(player.getBlockX(), player.getBlockY(), player.getBlockZ()); String world = player.serverLevel().dimension().location().toString(); diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/back.java b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/back.java index 7d0f2e5..404faf6 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/back.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/back.java @@ -8,37 +8,36 @@ import java.util.*; import dev.mrsnowy.teleport_commands.TeleportCommands; import dev.mrsnowy.teleport_commands.common.DeathLocation; -import dev.mrsnowy.teleport_commands.utils.tools; +import dev.mrsnowy.teleport_commands.utils.Tools; import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.core.BlockPos; -import net.minecraft.network.chat.ClickEvent; -import net.minecraft.network.chat.Component; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.phys.Vec3; -import static dev.mrsnowy.teleport_commands.utils.tools.*; +import static dev.mrsnowy.teleport_commands.utils.Language.getTranslation; +import static dev.mrsnowy.teleport_commands.utils.Tools.*; import static net.minecraft.commands.Commands.argument; public class back { - TeleportCommands teleportCommands; - public back(CommandDispatcher commandDispatcher, TeleportCommands teleportCommands) { - this.teleportCommands = teleportCommands; + public static void register(CommandDispatcher commandDispatcher) { commandDispatcher.register(Commands.literal("back") .requires(source -> source.getPlayer() != null) .executes(context -> { - final ServerPlayer player = context.getSource().getPlayerOrException(); + CommandSourceStack source = context.getSource(); + ServerPlayer player = source.getPlayerOrException(); try { - ToDeathLocation(player, false); + ToDeathLocation(player, player.server, false); } catch (Exception e) { Constants.LOGGER.error("Error while going back! => ", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -47,14 +46,14 @@ public class back { .requires(source -> source.getPlayer() != null) .executes(context -> { final boolean safety = BoolArgumentType.getBool(context, "Disable Safety"); - final ServerPlayer player = context.getSource().getPlayerOrException(); + ServerPlayer player = context.getSource().getPlayerOrException(); try { - ToDeathLocation(player, safety); + ToDeathLocation(player, player.server, safety); } catch (Exception e) { Constants.LOGGER.error("Error while going back! => ", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -66,13 +65,15 @@ public class back { // ----- // Gets the DeathLocation of the player and teleports the player to it - private void ToDeathLocation(ServerPlayer player, boolean safetyDisabled) throws Exception { + private static void ToDeathLocation(ServerPlayer player, MinecraftServer server, boolean safetyDisabled) throws Exception { + TeleportCommands teleportCommands = TeleportCommands.INSTANCE; + DeathLocation deathLocation = teleportCommands.deathLocationStorage .getDeathLocation(player.getStringUUID()) .orElse(null); if (deathLocation == null) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.noLocation", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.common.noLocation", player).withStyle(ChatFormatting.RED), true); return; } @@ -83,9 +84,9 @@ public class back { Constants.LOGGER.warn("({}) Error while going back! \nCouldn't find a world with the id: \"{}\" \nAvailable worlds: {}", player.getName().getString(), deathLocation.getWorldString(), - tools.getWorldIds(teleportCommands.server)); + Tools.getWorldIds(server)); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.worldNotFound", player) + player.displayClientMessage(getTranslation("commands.teleport_commands.common.worldNotFound", player) .withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return; @@ -95,26 +96,11 @@ public class back { // Sets the teleportBlockPos based on if it should do safety checking if (!safetyDisabled) { - Optional safeBlockPos = getSafeBlockPos(deathLocation.getBlockPos(), deathLocationWorld); + Optional safeBlockPos = Tools.getSafeBlockPos(deathLocation.getBlockPos(), deathLocationWorld); // Check if there is a safe BlockPos if (safeBlockPos.isEmpty()) { - // asks the player if they want to teleport anyway - player.displayClientMessage( - Component.empty() - .append(getTranslatedText("commands.teleport_commands.common.noSafeLocation", player) - .withStyle(ChatFormatting.RED, ChatFormatting.BOLD) - ) - .append("\n") - .append(getTranslatedText("commands.teleport_commands.common.safetyIsForLosers", player) - .withStyle(ChatFormatting.WHITE) - ) - .append("\n") - .append(getTranslatedText("commands.teleport_commands.common.forceTeleport", player) - .withStyle(ChatFormatting.DARK_AQUA, ChatFormatting.BOLD) - .withStyle(style -> style.withClickEvent(new ClickEvent.RunCommand("/back true"))) - ) - .append("\n"), false); + Tools.sendSafetyWarning(player, "/back true"); return; } else { @@ -126,14 +112,13 @@ public class back { } // check if the player is already at this location (in the same world) - if (player.blockPosition().equals(teleportBlockPos) && player.level() == deathLocationWorld) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.back.same", player).withStyle(ChatFormatting.AQUA), true); + if (player.blockPosition().equals(teleportBlockPos) && player.serverLevel() == deathLocationWorld) { + player.displayClientMessage(getTranslation("commands.teleport_commands.back.same", player).withStyle(ChatFormatting.AQUA), true); } else { // teleport the player! Vec3 teleportPos = new Vec3(teleportBlockPos.getX() + 0.5, teleportBlockPos.getY(), teleportBlockPos.getZ() + 0.5); -// player.displayClientMessage(getTranslatedText("commands.teleport_commands.back.go", player), true); teleportCommands.teleporter.queue(player, deathLocationWorld, teleportPos, "commands.teleport_commands.back.go"); } } diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/home.java b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/home.java index f4abee4..cfa0a6b 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/home.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/home.java @@ -11,7 +11,7 @@ import dev.mrsnowy.teleport_commands.suggestions.HomeSuggestionProvider; import java.util.List; import java.util.Optional; -import dev.mrsnowy.teleport_commands.utils.tools; +import dev.mrsnowy.teleport_commands.utils.Tools; import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; @@ -24,10 +24,8 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.phys.Vec3; -import static dev.mrsnowy.teleport_commands.storage.StorageManager.STORAGE; -import static dev.mrsnowy.teleport_commands.utils.tools.getTranslatedText; +import static dev.mrsnowy.teleport_commands.utils.Language.getTranslation; import static net.minecraft.commands.Commands.argument; -import static dev.mrsnowy.teleport_commands.utils.tools.Teleporter; public class home { public static void register(CommandDispatcher commandDispatcher) { @@ -44,7 +42,7 @@ public class home { } catch (Exception e) { Constants.LOGGER.error("Error while setting a home! => ", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.setError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.setError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -61,7 +59,7 @@ public class home { } catch (Exception e) { Constants.LOGGER.error("Error while going home! => ", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.goError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.goError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -78,7 +76,7 @@ public class home { } catch (Exception e) { Constants.LOGGER.error("Error while going to a specific home! => ", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.goError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.goError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -97,7 +95,7 @@ public class home { } catch (Exception e) { Constants.LOGGER.error("Error while deleting a home! => ", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.deleteError", player) + player.displayClientMessage(getTranslation("commands.teleport_commands.home.deleteError", player) .withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } @@ -119,7 +117,7 @@ public class home { } catch (Exception e) { Constants.LOGGER.error("Error while renaming a home! => ", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.renameError", player) + player.displayClientMessage(getTranslation("commands.teleport_commands.home.renameError", player) .withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } @@ -139,7 +137,7 @@ public class home { } catch (Exception e) { Constants.LOGGER.error("Error while setting the default home! => ", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.defaultError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.defaultError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -155,7 +153,7 @@ public class home { } catch (Exception e) { Constants.LOGGER.error("Error while printing the homes! => ", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.homes.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.homes.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -182,7 +180,7 @@ public class home { if (homeExists) { // Display error message that the home already exists - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.exists", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.exists", player).withStyle(ChatFormatting.RED), true); } else { // Set it as the default if there are no other homes @@ -191,7 +189,7 @@ public class home { } // Display message that the home has been set - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.set", player), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.set", player), true); } } @@ -202,7 +200,7 @@ public class home { // Get player storage Optional optionalPlayerStorage = STORAGE.getPlayer(player.getStringUUID()); if (optionalPlayerStorage.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); return; } @@ -214,7 +212,7 @@ public class home { if (defaultHome.isEmpty()) { // No default home set! - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.defaultNone", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.defaultNone", player).withStyle(ChatFormatting.AQUA), true); return; } else { @@ -225,7 +223,7 @@ public class home { // Get the home (if it exists) Optional optionalHome = playerStorage.getHome(homeName); if (optionalHome.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.notFound", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.notFound", player).withStyle(ChatFormatting.AQUA), true); return; } @@ -239,9 +237,9 @@ public class home { player.getName().getString(), home.getName(), home.getWorldString(), - tools.getWorldIds()); + Tools.getWorldIds()); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.worldNotFound", player) + player.displayClientMessage(getTranslation("commands.teleport_commands.common.worldNotFound", player) .withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return; @@ -253,13 +251,13 @@ public class home { // Check if the player is already at this location (in the same world) if (player.blockPosition().equals(teleportBlockPos) && player.level() == homeWorld) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.goSame", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.goSame", player).withStyle(ChatFormatting.AQUA), true); } else { // Teleport the player! Vec3 teleportPos = new Vec3(teleportBlockPos.getX() + 0.5, teleportBlockPos.getY(), teleportBlockPos.getZ() + 0.5); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.go", player), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.go", player), true); Teleporter(player, homeWorld, teleportPos); } } @@ -270,7 +268,7 @@ public class home { // Gets player storage Optional optionalPlayerStorage = STORAGE.getPlayer(player.getStringUUID()); if (optionalPlayerStorage.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); return; } @@ -279,7 +277,7 @@ public class home { // Get the home from the player Optional optionalHome = playerStorage.getHome(homeName); if (optionalHome.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.notFound", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.notFound", player).withStyle(ChatFormatting.RED), true); return; } @@ -293,7 +291,7 @@ public class home { // todo! maybe ask the player if they want to set a new default home? :3 } - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.delete", player), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.delete", player), true); } private static void RenameHome(ServerPlayer player, String homeName, String newHomeName) throws Exception { @@ -303,7 +301,7 @@ public class home { // Gets player storage Optional optionalPlayerStorage = STORAGE.getPlayer(player.getStringUUID()); if (optionalPlayerStorage.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); return; } @@ -311,14 +309,14 @@ public class home { // Check if there already is a home with the new name if (playerStorage.getHome(newHomeName).isPresent()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.nameExists", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.common.nameExists", player).withStyle(ChatFormatting.RED), true); return; } // Get the home that needs to be renamed Optional optionalHome = playerStorage.getHome(homeName); if (optionalHome.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.notFound", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.notFound", player).withStyle(ChatFormatting.RED), true); return; } @@ -330,7 +328,7 @@ public class home { playerStorage.setDefaultHome(newHomeName); } - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.rename", player), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.rename", player), true); } private static void SetDefaultHome(ServerPlayer player, String homeName) throws Exception { @@ -339,7 +337,7 @@ public class home { // Gets player storage Optional optionalPlayerStorage = STORAGE.getPlayer(player.getStringUUID()); if (optionalPlayerStorage.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); return; } @@ -347,26 +345,26 @@ public class home { // Check if the new default home exists if ( playerStorage.getHome(homeName).isEmpty() ) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.notFound", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.notFound", player).withStyle(ChatFormatting.RED), true); return; } // Check if the home is already the default if (playerStorage.getDefaultHome().equals(homeName)) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.defaultSame", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.defaultSame", player).withStyle(ChatFormatting.AQUA), true); return; } // set the new default playerStorage.setDefaultHome(homeName); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.default", player), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.default", player), true); } private static void PrintHomes(ServerPlayer player) throws Exception { // Gets player storage, if no storage then the player is homeless! Optional optionalPlayerStorage = STORAGE.getPlayer(player.getStringUUID()); if (optionalPlayerStorage.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); return; } @@ -376,14 +374,14 @@ public class home { // Check if there are any homes lol if (homes.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.homeless", player).withStyle(ChatFormatting.AQUA), true); return; } MutableComponent message = Component.empty(); // make da message - message.append(getTranslatedText("commands.teleport_commands.homes.homes", player) + message.append(getTranslation("commands.teleport_commands.homes.homes", player) .withStyle(ChatFormatting.YELLOW, ChatFormatting.BOLD) ); @@ -405,7 +403,7 @@ public class home { if (playerStorage.getDefaultHome().equals(currentHome.getName())) { message.append(" ") - .append(getTranslatedText("commands.teleport_commands.common.default", player) + .append(getTranslation("commands.teleport_commands.common.default", player) .withStyle(ChatFormatting.AQUA, ChatFormatting.BOLD) ); } @@ -429,7 +427,7 @@ public class home { .withStyle(style -> style.withHoverEvent( new HoverEvent.ShowText( - getTranslatedText("commands.teleport_commands.common.hoverCopy", player) + getTranslation("commands.teleport_commands.common.hoverCopy", player) ) ) ) @@ -446,7 +444,7 @@ public class home { .withStyle(style -> style.withHoverEvent( new HoverEvent.ShowText( - getTranslatedText("commands.teleport_commands.common.hoverCopy", player) + getTranslation("commands.teleport_commands.common.hoverCopy", player) ) ) ) @@ -459,7 +457,7 @@ public class home { message.append(Component.literal(" | ") .withStyle(ChatFormatting.AQUA) ) - .append(getTranslatedText("commands.teleport_commands.common.tp", player) + .append(getTranslation("commands.teleport_commands.common.tp", player) .withStyle(ChatFormatting.GREEN) .withStyle(style -> style.withClickEvent( @@ -470,7 +468,7 @@ public class home { ) ) .append(" ") - .append(getTranslatedText("commands.teleport_commands.common.rename", player) + .append(getTranslation("commands.teleport_commands.common.rename", player) .withStyle(ChatFormatting.BLUE) .withStyle(style -> style.withClickEvent( @@ -484,7 +482,7 @@ public class home { // add set default button if it isn't the default home if (!playerStorage.getDefaultHome().equals(currentHome.getName())) { - message.append(getTranslatedText("commands.teleport_commands.common.defaultPrompt", player) + message.append(getTranslation("commands.teleport_commands.common.defaultPrompt", player) .withStyle(ChatFormatting.DARK_AQUA) .withStyle(style -> style.withClickEvent( @@ -497,7 +495,7 @@ public class home { .append(" "); } - message.append(getTranslatedText("commands.teleport_commands.common.delete", player) + message.append(getTranslation("commands.teleport_commands.common.delete", player) .withStyle(ChatFormatting.RED) .withStyle(style -> style.withClickEvent( diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/main.java b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/main.java index 43ff68d..ab627e8 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/main.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/main.java @@ -6,7 +6,6 @@ import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.suggestion.SuggestionProvider; import dev.mrsnowy.teleport_commands.Constants; import dev.mrsnowy.teleport_commands.TeleportCommands; -import dev.mrsnowy.teleport_commands.storage.configManager; import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; @@ -17,6 +16,8 @@ import net.minecraft.server.level.ServerPlayer; import java.util.Arrays; +import static dev.mrsnowy.teleport_commands.utils.Language.getTranslation; + public class main { // TODO! Make this automatically generate based on the commands we have @@ -43,7 +44,7 @@ public class main { .then(Commands.literal("reloadConfig") .executes(context -> { try { - configManager.configLoader(); +// ConfigManager.configLoader(); } catch (Exception e) { Constants.LOGGER.error("Failed to reload config!", e); throw new SimpleCommandExceptionType(Component.literal(e.toString())).create(); @@ -71,7 +72,7 @@ public class main { } catch (Exception e) { Constants.LOGGER.error("Error while disabling a command! => ", e); // TODO replace the error below with something normal? - throw new SimpleCommandExceptionType(getTranslatedText("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD)).create(); + throw new SimpleCommandExceptionType(getTranslation("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD)).create(); } return 0; }) @@ -97,7 +98,7 @@ public class main { } catch (Exception e) { Constants.LOGGER.error("Error while disabling a command! => ", e); // TODO replace the error below with something normal? - throw new SimpleCommandExceptionType(getTranslatedText("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD)).create(); + throw new SimpleCommandExceptionType(getTranslation("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD)).create(); } return 0; }) @@ -106,7 +107,7 @@ public class main { .then(Commands.literal("reload") .requires(source -> source.hasPermission(4)) // Require OP .executes(context -> { - TeleportCommands.registerCommands(context.getSource().dispatcher()); + TeleportCommands.INSTANCE.registerCommands(context.getSource().dispatcher()); return 0; })) diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/tpa.java b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/tpa.java index b70a6c5..654e3e3 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/tpa.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/tpa.java @@ -5,7 +5,7 @@ import java.util.*; import com.mojang.brigadier.CommandDispatcher; import dev.mrsnowy.teleport_commands.Constants; import dev.mrsnowy.teleport_commands.TeleportCommands; -import dev.mrsnowy.teleport_commands.suggestions.tpaSuggestionProvider; +import dev.mrsnowy.teleport_commands.suggestions.TpaSuggestionProvider; import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; @@ -17,7 +17,8 @@ import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.phys.Vec3; -import static dev.mrsnowy.teleport_commands.utils.tools.*; +import static dev.mrsnowy.teleport_commands.utils.Language.getTranslation; +import static dev.mrsnowy.teleport_commands.utils.Tools.*; public class tpa { TeleportCommands teleportCommands; @@ -78,7 +79,7 @@ public class tpa { commandDispatcher.register(Commands.literal("tpaaccept") .requires(source -> source.getPlayer() != null) - .then(Commands.argument("player", EntityArgument.player()).suggests(new tpaSuggestionProvider()) + .then(Commands.argument("player", EntityArgument.player()).suggests(new TpaSuggestionProvider()) .executes(context -> { final ServerPlayer TargetPlayer = EntityArgument.getPlayer(context, "player"); final ServerPlayer player = context.getSource().getPlayerOrException(); @@ -96,7 +97,7 @@ public class tpa { commandDispatcher.register(Commands.literal("tpadeny") .requires(source -> source.getPlayer() != null) - .then(Commands.argument("player", EntityArgument.player()).suggests(new tpaSuggestionProvider()) + .then(Commands.argument("player", EntityArgument.player()).suggests(new TpaSuggestionProvider()) .executes(context -> { final ServerPlayer TargetPlayer = EntityArgument.getPlayer(context, "player"); final ServerPlayer player = context.getSource().getPlayerOrException(); @@ -106,7 +107,7 @@ public class tpa { } catch (Exception e) { Constants.LOGGER.error("Error while denying a tpa(here) request! => ", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.home.setError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.home.setError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } @@ -122,10 +123,10 @@ public class tpa { .count(); if (FromPlayer == ToPlayer) { - FromPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.self", FromPlayer).withStyle(ChatFormatting.AQUA), true); + FromPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.self", FromPlayer).withStyle(ChatFormatting.AQUA), true); } else if (playerTpaList >= 1) { - FromPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.alreadySent", FromPlayer, Component.literal(Objects.requireNonNull(ToPlayer.getName().getString(), "ToPlayer name cannot be null")).withStyle(ChatFormatting.BOLD)).withStyle(ChatFormatting.AQUA) + FromPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.alreadySent", FromPlayer, Component.literal(Objects.requireNonNull(ToPlayer.getName().getString(), "ToPlayer name cannot be null")).withStyle(ChatFormatting.BOLD)).withStyle(ChatFormatting.AQUA) , true ); @@ -138,14 +139,14 @@ public class tpa { String ReceivedFromPlayer = Objects.requireNonNull(FromPlayer.getName().getString(), "FromPlayer name cannot be null"); String SentToPlayer = Objects.requireNonNull(ToPlayer.getName().getString(), "ToPlayer name cannot be null"); - FromPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.sent", FromPlayer, Component.literal(hereText), Component.literal(SentToPlayer).withStyle(ChatFormatting.BOLD)) + FromPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.sent", FromPlayer, Component.literal(hereText), Component.literal(SentToPlayer).withStyle(ChatFormatting.BOLD)) // .append(Text.literal("\n[Cancel]").formatted(Formatting.BLUE, Formatting.BOLD)) ,true ); - ToPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.received", ToPlayer, Component.literal(hereText), Component.literal(ReceivedFromPlayer).withStyle(ChatFormatting.AQUA, ChatFormatting.BOLD)).withStyle(ChatFormatting.AQUA) + ToPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.received", ToPlayer, Component.literal(hereText), Component.literal(ReceivedFromPlayer).withStyle(ChatFormatting.AQUA, ChatFormatting.BOLD)).withStyle(ChatFormatting.AQUA) .append("\n") - .append(getTranslatedText("commands.teleport_commands.tpa.accept", ToPlayer) + .append(getTranslation("commands.teleport_commands.tpa.accept", ToPlayer) .withStyle(ChatFormatting.GREEN, ChatFormatting.BOLD) .withStyle(style -> style .withClickEvent( @@ -156,7 +157,7 @@ public class tpa { ) ) .append(" ") - .append(getTranslatedText("commands.teleport_commands.tpa.deny", ToPlayer) + .append(getTranslation("commands.teleport_commands.tpa.deny", ToPlayer) .withStyle(ChatFormatting.RED, ChatFormatting.BOLD) .withStyle(style -> style .withClickEvent( @@ -176,8 +177,8 @@ public class tpa { public void run() { boolean successful = tpaList.remove(tpaRequest); if (successful) { - FromPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.expired", FromPlayer, Component.literal(hereText)).withStyle(ChatFormatting.RED, ChatFormatting.BOLD),true); - ToPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.expired", ToPlayer, Component.literal(hereText)).withStyle(ChatFormatting.WHITE),true); + FromPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.expired", FromPlayer, Component.literal(hereText)).withStyle(ChatFormatting.RED, ChatFormatting.BOLD),true); + ToPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.expired", ToPlayer, Component.literal(hereText)).withStyle(ChatFormatting.WHITE),true); } // else not needed since it may be denied/cancelled } @@ -188,7 +189,7 @@ public class tpa { private void tpaAccept(ServerPlayer FromPlayer, ServerPlayer ToPlayer) { if (FromPlayer == ToPlayer) { - FromPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.self", FromPlayer).withStyle(ChatFormatting.AQUA), true); + FromPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.self", FromPlayer).withStyle(ChatFormatting.AQUA), true); return; } @@ -216,19 +217,19 @@ public class tpa { } // if the player teleported then these messages get sent && the request gets removed - FromPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.accepted", FromPlayer).withStyle(ChatFormatting.WHITE),true); - ToPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.accepted", ToPlayer).withStyle(ChatFormatting.GREEN),true); + FromPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.accepted", FromPlayer).withStyle(ChatFormatting.WHITE),true); + ToPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.accepted", ToPlayer).withStyle(ChatFormatting.GREEN),true); tpaList.remove(tpaStorage.get()); } else { // No request found - FromPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.notFound", FromPlayer).withStyle(ChatFormatting.RED),true); + FromPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.notFound", FromPlayer).withStyle(ChatFormatting.RED),true); } } private void tpaDeny(ServerPlayer FromPlayer, ServerPlayer ToPlayer) { if (FromPlayer == ToPlayer) { - FromPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.self", FromPlayer).withStyle(ChatFormatting.AQUA),true); + FromPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.self", FromPlayer).withStyle(ChatFormatting.AQUA),true); } else { Optional tpaStorage = tpaList.stream() @@ -239,11 +240,11 @@ public class tpa { if (tpaStorage.isPresent()) { tpaList.remove(tpaStorage.get()); - ToPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.denied", ToPlayer).withStyle(ChatFormatting.RED, ChatFormatting.BOLD),true); - FromPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.denied", FromPlayer).withStyle(ChatFormatting.WHITE),true); + ToPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.denied", ToPlayer).withStyle(ChatFormatting.RED, ChatFormatting.BOLD),true); + FromPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.denied", FromPlayer).withStyle(ChatFormatting.WHITE),true); } else { - FromPlayer.displayClientMessage(getTranslatedText("commands.teleport_commands.tpa.notFound", FromPlayer).withStyle(ChatFormatting.RED),true); + FromPlayer.displayClientMessage(getTranslation("commands.teleport_commands.tpa.notFound", FromPlayer).withStyle(ChatFormatting.RED),true); } } } diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/warp.java b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/warp.java index 5416eb8..9375503 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/warp.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/warp.java @@ -6,7 +6,7 @@ import dev.mrsnowy.teleport_commands.Constants; import dev.mrsnowy.teleport_commands.TeleportCommands; import dev.mrsnowy.teleport_commands.common.NamedLocation; import dev.mrsnowy.teleport_commands.suggestions.WarpSuggestionProvider; -import dev.mrsnowy.teleport_commands.utils.tools; +import dev.mrsnowy.teleport_commands.utils.Tools; import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; @@ -22,7 +22,7 @@ import net.minecraft.world.phys.Vec3; import java.util.List; import java.util.Optional; -import static dev.mrsnowy.teleport_commands.utils.tools.getTranslatedText; +import static dev.mrsnowy.teleport_commands.utils.Tools.getTranslation; import static net.minecraft.commands.Commands.argument; public class warp { @@ -46,7 +46,7 @@ public class warp { } catch (Exception e) { Constants.LOGGER.error("Error while setting the warp!", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.setError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.setError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -65,7 +65,7 @@ public class warp { } catch (Exception e) { Constants.LOGGER.error("Error while going to the warp!",e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.goError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.goError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -86,7 +86,7 @@ public class warp { } catch (Exception e) { Constants.LOGGER.error("Error while deleting to the warp!", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.deleteError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.deleteError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -109,7 +109,7 @@ public class warp { } catch (Exception e) { Constants.LOGGER.error("Error while renaming the warp!", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.renameError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.renameError", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -125,7 +125,7 @@ public class warp { } catch (Exception e) { Constants.LOGGER.error("Error while printing warps!", e); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warps.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warps.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -148,11 +148,11 @@ public class warp { if (warpExists) { // Display error message that the warp already exists - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.exists", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.exists", player).withStyle(ChatFormatting.RED), true); } else { // Display message that the home as been set - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.set", player), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.set", player), true); } } @@ -162,7 +162,7 @@ public class warp { // Gets warp Optional optionalWarp = STORAGE.getWarp(warpName); if (optionalWarp.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.notFound", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.notFound", player).withStyle(ChatFormatting.RED), true); return; } @@ -176,9 +176,9 @@ public class warp { player.getName().getString(), warp.getName(), warp.getWorldString(), - tools.getWorldIds()); + Tools.getWorldIds()); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.worldNotFound", player) + player.displayClientMessage(getTranslation("commands.teleport_commands.common.worldNotFound", player) .withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return; @@ -190,13 +190,13 @@ public class warp { // Check if the player is already at this location (in the same world) if (player.blockPosition().equals(teleportBlockPos) && player.level() == warpWorld) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.goSame", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.goSame", player).withStyle(ChatFormatting.AQUA), true); } else { // Teleport the player! Vec3 teleportPos = new Vec3(teleportBlockPos.getX() + 0.5, teleportBlockPos.getY(), teleportBlockPos.getZ() + 0.5); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.go", player), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.go", player), true); Teleporter(player, warpWorld, teleportPos); } } @@ -211,11 +211,11 @@ public class warp { // Delete the warp STORAGE.removeWarp(optionalWarp.get()); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.delete", player), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.delete", player), true); } else { // the warp is not found - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.notFound", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.notFound", player).withStyle(ChatFormatting.RED), true); } } @@ -225,7 +225,7 @@ public class warp { // check if there is no existing warp with the new name if (STORAGE.getWarp(newWarpName).isPresent()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.nameExists", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.common.nameExists", player).withStyle(ChatFormatting.RED), true); return; } @@ -236,11 +236,11 @@ public class warp { // set the new name warpToRename.get().setName(newWarpName); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.rename", player), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.rename", player), true); } else { // the warp is not found - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.notFound", player).withStyle(ChatFormatting.RED), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.notFound", player).withStyle(ChatFormatting.RED), true); } } @@ -250,14 +250,14 @@ public class warp { // Check if there are any warps lol if (warps.isEmpty()) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.warp.homeless", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.warp.homeless", player).withStyle(ChatFormatting.AQUA), true); return; } MutableComponent message = Component.empty(); // make da message - message.append(getTranslatedText("commands.teleport_commands.warps.warps", player) + message.append(getTranslation("commands.teleport_commands.warps.warps", player) .withStyle(ChatFormatting.YELLOW, ChatFormatting.BOLD) ); @@ -296,7 +296,7 @@ public class warp { .withStyle(style -> style.withHoverEvent( new HoverEvent.ShowText( - getTranslatedText("commands.teleport_commands.common.hoverCopy", player) + getTranslation("commands.teleport_commands.common.hoverCopy", player) ) ) ) @@ -313,7 +313,7 @@ public class warp { .withStyle(style -> style .withHoverEvent( new HoverEvent.ShowText( - getTranslatedText("commands.teleport_commands.common.hoverCopy", player) + getTranslation("commands.teleport_commands.common.hoverCopy", player) ) ) ) @@ -324,7 +324,7 @@ public class warp { // Teleport button message.append(Component.literal(" | ").withStyle(ChatFormatting.AQUA)) - .append(getTranslatedText("commands.teleport_commands.common.tp", player) + .append(getTranslation("commands.teleport_commands.common.tp", player) .withStyle(ChatFormatting.GREEN) .withStyle(style -> style.withClickEvent( @@ -338,7 +338,7 @@ public class warp { // Rename and delete buttons if admin if (canModify) { - message.append(getTranslatedText("commands.teleport_commands.common.rename", player) + message.append(getTranslation("commands.teleport_commands.common.rename", player) .withStyle(ChatFormatting.BLUE) .withStyle(style -> style .withClickEvent( @@ -349,7 +349,7 @@ public class warp { ) ) .append(" ") - .append(getTranslatedText("commands.teleport_commands.common.delete", player) + .append(getTranslation("commands.teleport_commands.common.delete", player) .withStyle(ChatFormatting.RED) .withStyle(style -> style .withClickEvent( diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/worldspawn.java b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/worldspawn.java index 19cb539..5067902 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/worldspawn.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/worldspawn.java @@ -4,12 +4,11 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.BoolArgumentType; import dev.mrsnowy.teleport_commands.Constants; import dev.mrsnowy.teleport_commands.TeleportCommands; +import dev.mrsnowy.teleport_commands.utils.Tools; import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.core.BlockPos; -import net.minecraft.network.chat.ClickEvent; -import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.phys.Vec3; @@ -17,7 +16,8 @@ import net.minecraft.world.phys.Vec3; import java.util.Objects; import java.util.Optional; -import static dev.mrsnowy.teleport_commands.utils.tools.*; +import static dev.mrsnowy.teleport_commands.utils.Language.getTranslation; +import static dev.mrsnowy.teleport_commands.utils.Tools.*; import static net.minecraft.commands.Commands.argument; import static net.minecraft.world.level.Level.OVERWORLD; @@ -35,7 +35,7 @@ public class worldspawn { } catch (Exception error) { Constants.LOGGER.error("Error while going to the worldspawn! => ", error); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -51,7 +51,7 @@ public class worldspawn { } catch (Exception error) { Constants.LOGGER.error("Error while going to the worldspawn! => ", error); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.common.error", player).withStyle(ChatFormatting.RED, ChatFormatting.BOLD), true); return 1; } return 0; @@ -62,7 +62,7 @@ public class worldspawn { private static void toWorldSpawn(ServerPlayer player, boolean safetyDisabled) throws NullPointerException { // todo! make the dimension customizable - ServerLevel world = TeleportCommands.server.getLevel(OVERWORLD); + ServerLevel world = player.server.getLevel(OVERWORLD); BlockPos worldSpawn = Objects.requireNonNull(world,"Overworld cannot be null!").getSharedSpawnPos(); if (!safetyDisabled) { @@ -74,45 +74,24 @@ public class worldspawn { // check if the player is already at this location if (player.blockPosition().equals(safeBlockPos) && player.level() == world) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.worldspawn.same", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.worldspawn.same", player).withStyle(ChatFormatting.AQUA), true); } else { Vec3 teleportPos = new Vec3(safeBlockPos.getX() + 0.5, safeBlockPos.getY(), safeBlockPos.getZ() + 0.5); - - player.displayClientMessage(getTranslatedText("commands.teleport_commands.worldspawn.go", player), true); - Teleporter(player, world, teleportPos); + TeleportCommands.INSTANCE.teleporter.queue(player, world, teleportPos, "commands.teleport_commands.worldspawn.go"); } } else { - - player.displayClientMessage( - Component.empty() - .append(getTranslatedText("commands.teleport_commands.common.noSafeLocation", player) - .withStyle(ChatFormatting.RED, ChatFormatting.BOLD) - ) - .append("\n") - .append(getTranslatedText("commands.teleport_commands.common.safetyIsForLosers", player) - .withStyle(ChatFormatting.WHITE) - ) - .append("\n") - .append(getTranslatedText("commands.teleport_commands.common.forceTeleport", player) - .withStyle(ChatFormatting.DARK_AQUA, ChatFormatting.BOLD) - .withStyle(style -> style.withClickEvent( - new ClickEvent.RunCommand("/worldspawn true") - ) - ) - ) - .append("\n"), false); + Tools.sendSafetyWarning(player, "/worldspawn true"); } } else { if (player.blockPosition().equals(worldSpawn) && player.level() == world) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.worldspawn.same", player).withStyle(ChatFormatting.AQUA), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.worldspawn.same", player).withStyle(ChatFormatting.AQUA), true); } else { - - player.displayClientMessage(getTranslatedText("commands.teleport_commands.worldspawn.go", player), true); - Teleporter(player, world, new Vec3(worldSpawn.getX() + 0.5, worldSpawn.getY(), worldSpawn.getZ() + 0.5)); + Vec3 teleportPos = new Vec3(worldSpawn.getX() + 0.5, worldSpawn.getY(), worldSpawn.getZ() + 0.5); + TeleportCommands.INSTANCE.teleporter.queue(player, world, teleportPos, "commands.teleport_commands.worldspawn.go"); } } } diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/CommandsMixin.java b/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/CommandsMixin.java index 8df9e19..1ab5fa9 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/CommandsMixin.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/CommandsMixin.java @@ -20,6 +20,7 @@ public class CommandsMixin { Commands self = (Commands) (Object) this; CommandDispatcher dispatcher = self.getDispatcher(); + // This re-registers the commands when the server's reloadable resources get reloaded TeleportCommands.INSTANCE.registerCommands(dispatcher); } } \ No newline at end of file diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/ServerStartMixin.java b/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/MinecraftServerMixin.java similarity index 96% rename from common/src/main/java/dev/mrsnowy/teleport_commands/mixin/ServerStartMixin.java rename to common/src/main/java/dev/mrsnowy/teleport_commands/mixin/MinecraftServerMixin.java index d1baae4..11170d1 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/ServerStartMixin.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/MinecraftServerMixin.java @@ -10,7 +10,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import java.util.function.BooleanSupplier; @Mixin(MinecraftServer.class) -public class ServerStartMixin { +public class MinecraftServerMixin { @Inject(method = "runServer", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;buildServerStatus()Lnet/minecraft/network/protocol/status/ServerStatus;", ordinal = 0)) private void runServer(CallbackInfo info) { diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/PlayerDeathMixin.java b/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/ServerPlayerMixin.java similarity index 93% rename from common/src/main/java/dev/mrsnowy/teleport_commands/mixin/PlayerDeathMixin.java rename to common/src/main/java/dev/mrsnowy/teleport_commands/mixin/ServerPlayerMixin.java index 518c9fb..8124b69 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/PlayerDeathMixin.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/mixin/ServerPlayerMixin.java @@ -1,8 +1,6 @@ package dev.mrsnowy.teleport_commands.mixin; import dev.mrsnowy.teleport_commands.TeleportCommands; -import net.minecraft.client.renderer.item.properties.numeric.Damage; -import net.minecraft.commands.Commands; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.damagesource.DamageSource; @@ -15,7 +13,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ServerPlayer.class) -public class PlayerDeathMixin { +public class ServerPlayerMixin { @Inject(method = "die", at = @At("HEAD")) private void notifyDeath(CallbackInfo info) { diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/storage/configManager.java b/common/src/main/java/dev/mrsnowy/teleport_commands/storage/ConfigManager.java similarity index 97% rename from common/src/main/java/dev/mrsnowy/teleport_commands/storage/configManager.java rename to common/src/main/java/dev/mrsnowy/teleport_commands/storage/ConfigManager.java index 80b87df..11daa7d 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/storage/configManager.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/storage/ConfigManager.java @@ -3,14 +3,14 @@ package dev.mrsnowy.teleport_commands.storage; import com.google.gson.*; import dev.mrsnowy.teleport_commands.Constants; import dev.mrsnowy.teleport_commands.TeleportCommands; -import dev.mrsnowy.teleport_commands.utils.tools; +import dev.mrsnowy.teleport_commands.utils.Tools; import java.io.FileReader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; -public class configManager { +public class ConfigManager { public Path configFile; public ConfigClass config; @@ -18,7 +18,7 @@ public class configManager { private final int defaultVersion = new ConfigClass().getVersion(); private final TeleportCommands teleportCommands; - public configManager(TeleportCommands teleportCommands) { + public ConfigManager(TeleportCommands teleportCommands) { this.teleportCommands = teleportCommands; configFile = teleportCommands.configDir.resolve("teleport_commands.json"); @@ -182,7 +182,7 @@ public class configManager { public void setCommand(String command) throws Exception { this.command = command; configSaver(); - tools.reloadResources(teleportCommands.server); // Reload the commands + Tools.reloadResources(teleportCommands.server); // Reload the commands } public boolean isDeleteAfterTeleport() { @@ -298,7 +298,7 @@ public class configManager { public void setCommand(String command) throws Exception { this.command = command; configSaver(); - tools.reloadResources(teleportCommands.server); // Reload the commands + Tools.reloadResources(teleportCommands.server); // Reload the commands } } } diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/suggestions/tpaSuggestionProvider.java b/common/src/main/java/dev/mrsnowy/teleport_commands/suggestions/TpaSuggestionProvider.java similarity index 96% rename from common/src/main/java/dev/mrsnowy/teleport_commands/suggestions/tpaSuggestionProvider.java rename to common/src/main/java/dev/mrsnowy/teleport_commands/suggestions/TpaSuggestionProvider.java index 095d7cf..b267c4a 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/suggestions/tpaSuggestionProvider.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/suggestions/TpaSuggestionProvider.java @@ -14,7 +14,7 @@ import java.util.*; import java.util.concurrent.CompletableFuture; -public class tpaSuggestionProvider implements SuggestionProvider { +public class TpaSuggestionProvider implements SuggestionProvider { @Override public CompletableFuture getSuggestions(CommandContext context, SuggestionsBuilder builder) { try { diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/utils/Language.java b/common/src/main/java/dev/mrsnowy/teleport_commands/utils/Language.java new file mode 100644 index 0000000..7ce1bfd --- /dev/null +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/utils/Language.java @@ -0,0 +1,143 @@ +package dev.mrsnowy.teleport_commands.utils; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import dev.mrsnowy.teleport_commands.Constants; +import dev.mrsnowy.teleport_commands.TeleportCommands; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.server.level.ServerPlayer; + +import javax.annotation.Nullable; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Language { + record languageObject(@Nullable JsonObject jsonObject, int lastUsedTick) {} + + private static final Map LANGUAGE_CACHE = new HashMap<>(); + private static final Pattern NUMBER_PATTERN = Pattern.compile("%(\\d+)%"); + + /// Attempts to retrieve the JSON file for the language, then caches it in memory. + private static @Nullable JsonObject getLanguageJson(String language) { + int currentTick = TeleportCommands.INSTANCE.server.getTickCount(); + + if (LANGUAGE_CACHE.containsKey(language)) { + languageObject cache = LANGUAGE_CACHE.get(language); + LANGUAGE_CACHE.put(language, new languageObject(cache.jsonObject, currentTick)); + return cache.jsonObject; + } + + String filePath = String.format("/assets/%s/lang/%s.json", Constants.MOD_ID, language); + JsonObject value = null; + try { + InputStream stream = TeleportCommands.class.getResourceAsStream(filePath); + if (stream != null) { + Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8); + value = JsonParser.parseReader(reader).getAsJsonObject(); + } + } catch (Exception e) { + Constants.LOGGER.info("Error while reading language {}, maybe it's not available", language); + } + + if (value == null) { + LANGUAGE_CACHE.put(language, new languageObject(null, currentTick)); + } + + return value; + } + + // I love rustifying things + record TranslationResult(@Nullable String value, @Nullable String error) {} + + /// Gets the translation from a key of a language + private static TranslationResult getTranslation(String language, String key) { + try { + JsonObject json = getLanguageJson(language); + if (json == null) { + throw new Exception("Error getting the json file for the translation"); + } + + JsonElement translation = json.get(key); + if (translation == null) { + throw new Exception("Error getting the key for the translation"); + } + String translationString = translation.getAsString(); + if (translationString.isEmpty()) { + throw new Exception("Empty translation key!"); + } + + return new TranslationResult(translationString, null); + + } catch (Exception e) { + return new TranslationResult(null, e.getMessage()); + } + } + + // Gets the translated text for each player based on their language, this is fully server side and actually works (UNLIKE MOJANG'S TRANSLATED KEY'S WHICH ARE CLIENT SIDE) (I'm not mad, I swear!) + public static MutableComponent getTranslation(String key, ServerPlayer player, MutableComponent... args) { + String language = player.clientInformation().language().toLowerCase(); + TranslationResult translationResult = getTranslation(language, key); + + if (translationResult.value == null) { + if (!language.equals("en_us")) { + + translationResult = getTranslation("en_us", key); + if (translationResult.value == null) { + Constants.LOGGER.error("Error while fall-backing to default translation from \"{}\": \"{}\". Returning raw json to user.", language, translationResult.error); + return Component.literal(key); + } + } else { + Constants.LOGGER.error("Error while loading translation: \"{}\". Returning raw json to user.", translationResult.error); + return Component.literal(key); + } + } + + String translationString = translationResult.value; + + if (args.length == 0) { + return Component.literal(translationString); + } + + try { + // Adds the optional MutableComponents in the correct places + Matcher matcher = NUMBER_PATTERN.matcher(translationString); + MutableComponent component = Component.empty(); + int lastIndex = 0; + + while (matcher.find()) { + int index = Integer.parseInt(matcher.group(1)); + + component.append(Component.literal(translationString.substring(lastIndex, matcher.start()))); + component.append(args[index]); + + lastIndex = matcher.end(); + } + + component.append(translationString.substring(lastIndex)); + return component; + + } catch (Exception e) { + Constants.LOGGER.error("Error while replacing dynamic sections in translation: \"{}\". Returning raw string to user.", e.getMessage()); + return Component.literal(translationString); + } + } + + /// Cleans languages out of the cache if they haven't been used for some time. + // TODO! hook this up! + public static void cacheCleaner(int currentTick, int tps) { + LANGUAGE_CACHE.entrySet().removeIf(entry -> { + if (entry.getValue().lastUsedTick + (120 * tps) < currentTick) { + return true; + } + return false; + }); + } +} diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/utils/teleporter.java b/common/src/main/java/dev/mrsnowy/teleport_commands/utils/Teleporter.java similarity index 76% rename from common/src/main/java/dev/mrsnowy/teleport_commands/utils/teleporter.java rename to common/src/main/java/dev/mrsnowy/teleport_commands/utils/Teleporter.java index 38e0583..c26ed98 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/utils/teleporter.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/utils/Teleporter.java @@ -15,10 +15,10 @@ import net.minecraft.world.phys.Vec3; import javax.annotation.Nullable; import java.util.*; -import static dev.mrsnowy.teleport_commands.utils.tools.getTranslatedText; +import static dev.mrsnowy.teleport_commands.utils.Language.getTranslation; import static net.minecraft.sounds.SoundEvents.ENDERMAN_TELEPORT; -public class teleporter { +public class Teleporter { private final TeleportCommands teleportCommands; private final Map players = new HashMap<>(); @@ -58,7 +58,7 @@ public class teleporter { // ---- - public teleporter(TeleportCommands teleportCommands) { + public Teleporter(TeleportCommands teleportCommands) { this.teleportCommands = teleportCommands; } @@ -83,7 +83,7 @@ public class teleporter { if (wasOnCooldown && !data.teleportOnCooldown && !data.fightOnCooldown) { ServerPlayer player = server.getPlayerList().getPlayer(entry.getKey()); if (player != null) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.teleport.ready", player).withStyle(ChatFormatting.WHITE), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.teleport.ready", player).withStyle(ChatFormatting.WHITE), true); } } @@ -102,7 +102,7 @@ public class teleporter { // check if this is a whole number float secondsLeft = (float) (data.pendingTeleport.teleportDelayUntil - currentTick) / data.pendingTeleport.tps; if ((secondsLeft % 1) == 0 && player != null) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.teleport.progress", player).withStyle(ChatFormatting.WHITE), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.teleport.progress", player).withStyle(ChatFormatting.WHITE), true); } } } @@ -137,7 +137,7 @@ public class teleporter { int ticksLeft = data.pendingTeleport.teleportDelayUntil - currentTick; int secondsLeft = (int) Math.ceil( (double) ticksLeft / data.pendingTeleport.tps); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.teleport.delay", player, + player.displayClientMessage(getTranslation("commands.teleport_commands.teleport.delay", player, Component.literal(String.valueOf(secondsLeft)), Component.literal(String.valueOf(ticksLeft)) ).withStyle(ChatFormatting.WHITE), false); @@ -151,7 +151,7 @@ public class teleporter { int ticksLeft = cooldownUntil - currentTick; int secondsLeft = (int) Math.ceil( (double) ticksLeft / tps); - player.displayClientMessage(getTranslatedText("commands.teleport_commands.teleport.cooldown", player, + player.displayClientMessage(getTranslation("commands.teleport_commands.teleport.cooldown", player, Component.literal(String.valueOf(secondsLeft)), Component.literal(String.valueOf(ticksLeft)) ).withStyle(ChatFormatting.WHITE), false); @@ -170,27 +170,20 @@ public class teleporter { // bypass the delay teleport(player, world, coords, completionMessage); } - - - // save pos and check if they have moved. - - // check if they got hit? whileFighting - - // save when they last got hit and if it exceeds fightCooldown - } /// Teleports the player :P - private void teleport(ServerPlayer player, ServerLevel world, Vec3 coords, String completionMessage) { + private void teleport(ServerPlayer player, ServerLevel to_world, Vec3 to_coords, String completionMessage) { // teleportation effects & sounds before teleporting - world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() + 1, player.getZ(), 20, 0.0D, 0.0D, 0.0D, 0.01); - world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 1.0D, 0.0D, 0.03); - world.playSound(null, player.blockPosition(), SoundEvent.createVariableRangeEvent(ENDERMAN_TELEPORT.location()), SoundSource.PLAYERS, 0.4f, 1.0f); + ServerLevel from_world = player.serverLevel(); + from_world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() + 1, player.getZ(), 20, 0.0D, 0.0D, 0.0D, 0.01); + from_world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 1.0D, 0.0D, 0.03); + from_world.playSound(null, player.blockPosition(), SoundEvent.createVariableRangeEvent(ENDERMAN_TELEPORT.location()), SoundSource.PLAYERS, 0.4f, 1.0f); boolean flying = player.getAbilities().flying; - player.displayClientMessage(getTranslatedText(completionMessage, player).withStyle(ChatFormatting.WHITE), true); - player.teleportTo(world, coords.x, coords.y, coords.z, Set.of(), player.getYRot(), player.getXRot(), false); + player.displayClientMessage(getTranslation(completionMessage, player).withStyle(ChatFormatting.WHITE), true); + player.teleportTo(to_world, to_coords.x, to_coords.y, to_coords.z, Set.of(), player.getYRot(), player.getXRot(), false); // Restore flying when teleporting trough dimensions if (flying) { @@ -199,15 +192,15 @@ public class teleporter { } // teleportation sound && effects after teleport - world.playSound(null, player.blockPosition(), SoundEvent.createVariableRangeEvent(ENDERMAN_TELEPORT.location()), SoundSource.PLAYERS, 0.4f, 1.0f); - world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() , player.getZ(), 20, 0.0D, 1.0D, 0.0D, 0.01); - world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 0.0D, 0.0D, 0.03); + to_world.playSound(null, player.blockPosition(), SoundEvent.createVariableRangeEvent(ENDERMAN_TELEPORT.location()), SoundSource.PLAYERS, 0.4f, 1.0f); + to_world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() , player.getZ(), 20, 0.0D, 1.0D, 0.0D, 0.01); + to_world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 0.0D, 0.0D, 0.03); } - + /// This function gets called by the mixin to notify that the player has moved public void reportPlayerMoved(ServerPlayer player) { if (teleportCommands.config.config.teleporting.isAllowMoving()) { - return; // The config option isn't disabled + return; // The config option is disabled } PlayerData data = players.get(player.getUUID()); @@ -217,16 +210,17 @@ public class teleporter { boolean tooFar = data.pendingTeleport.startingCoords.closerThan(pos, 1.5); // Checks if they moved more than one block (1.5 for diagonals) if (tooFar) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.teleport.moving", player).withStyle(ChatFormatting.WHITE), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.teleport.moving", player).withStyle(ChatFormatting.WHITE), true); data.pendingTeleport = null; } } } + /// This function gets called by the mixin to notify that the player got damaged public void reportPlayerHurt(ServerPlayer player) { if (teleportCommands.config.config.teleporting.isAllowFighting()) { - return; // The config option isn't disabled + return; // The config option is disabled } PlayerData data = players.get(player.getUUID()); @@ -238,10 +232,9 @@ public class teleporter { data.fightOnCooldown = true; if (data.pendingTeleport != null) { - player.displayClientMessage(getTranslatedText("commands.teleport_commands.teleport.fighting", player).withStyle(ChatFormatting.WHITE), true); + player.displayClientMessage(getTranslation("commands.teleport_commands.teleport.fighting", player).withStyle(ChatFormatting.WHITE), true); data.pendingTeleport = null; } } - } } diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/utils/tools.java b/common/src/main/java/dev/mrsnowy/teleport_commands/utils/Tools.java similarity index 51% rename from common/src/main/java/dev/mrsnowy/teleport_commands/utils/tools.java rename to common/src/main/java/dev/mrsnowy/teleport_commands/utils/Tools.java index 10eeab0..33168b8 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/utils/tools.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/utils/Tools.java @@ -1,27 +1,23 @@ package dev.mrsnowy.teleport_commands.utils; import com.google.gson.*; -import dev.mrsnowy.teleport_commands.Constants; -import dev.mrsnowy.teleport_commands.TeleportCommands; import java.io.*; -import java.nio.charset.StandardCharsets; import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.StreamSupport; +import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.ClickEvent; import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.MutableComponent; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; -import static dev.mrsnowy.teleport_commands.Constants.MOD_ID; +import static dev.mrsnowy.teleport_commands.utils.Language.getTranslation; -public class tools { +public class Tools { private static final Set unsafeCollisionFreeBlocks = Set.of("block.minecraft.lava", "block.minecraft.flowing_lava", "block.minecraft.end_portal", "block.minecraft.end_gateway","block.minecraft.fire", "block.minecraft.soul_fire", "block.minecraft.powder_snow", "block.minecraft.nether_portal"); // checks a 7x7x7 location around the player in order to find a safe place to teleport them to. @@ -67,92 +63,25 @@ public class tools { } } - - // Gets the translated text for each player based on their language, this is fully server side and actually works (UNLIKE MOJANG'S TRANSLATED KEY'S WHICH ARE CLIENT SIDE) (I'm not mad, I swear!) - public static MutableComponent getTranslatedText(String key, ServerPlayer player, MutableComponent... args) { - //todo! maybe make this also loaded in memory? - String language = player.clientInformation().language().toLowerCase(); - String regex = "%(\\d+)%"; - Pattern pattern = Pattern.compile(regex); - -// MinecraftServer server = player.getServer(); - -// MinecraftServer.ServerResourcePackInfo silly2 = server.getResourceManager().listResources() - -// java.util.stream.Stream SILLY = server.getResourceManager().listPacks(); -// -// SILLY.forEach(pack -> { -// Constants.LOGGER.info("{} : {} : {}", pack.packId(), pack.location(), pack.getClass()); -// }); - -// player.displayClientMessage(Component.literal(.toString()), false); - - // the try catch stuff is so wacky, but it works fine and I don't need to check everything - try { - String filePath = String.format("/assets/%s/lang/%s.json", MOD_ID, language); - InputStream stream = TeleportCommands.class.getResourceAsStream(filePath); - - Reader reader = new InputStreamReader(Objects.requireNonNull(stream, String.format("Couldn't find the required language file for \"%s\"", language)), StandardCharsets.UTF_8); - JsonElement json = JsonParser.parseReader(reader); - String translation = json.getAsJsonObject().get(key).getAsString(); - - - // Adds the optional MutableComponents in the correct places - Matcher matcher = pattern.matcher(Objects.requireNonNull(translation)); - - MutableComponent component = Component.literal(""); - int lastIndex = 0; - - while (matcher.find()) { - component.append(Component.literal(translation.substring(lastIndex, matcher.start()))); - - int index = Integer.parseInt(matcher.group(1)); - component.append(args[index]); - - lastIndex = matcher.end(); - } - component.append(translation.substring(lastIndex)); - - return component; - - } catch (Exception e) { - - try { - if (!Objects.equals(language, "en_us")) { -// TeleportCommands.LOGGER.warn("Key \"{}\" not found in the language: {}, falling back to default (en_us)", key, language); - - String filePath = String.format("/assets/%s/lang/en_us.json", MOD_ID); - InputStream stream = TeleportCommands.class.getResourceAsStream(filePath); - - Reader reader = new InputStreamReader(Objects.requireNonNull(stream, String.format("Couldn't find the required language file for \"%s\"", language)), StandardCharsets.UTF_8); - JsonElement json = JsonParser.parseReader(reader); - String translation = json.getAsJsonObject().get(key).getAsString(); - - // Adds the optional MutableComponents in the correct places - Matcher matcher = pattern.matcher(Objects.requireNonNull(translation, "translation cannot be null")); - - MutableComponent component = Component.literal(""); - int lastIndex = 0; - - while (matcher.find()) { - component.append(Component.literal(translation.substring(lastIndex, matcher.start()))); - - int index = Integer.parseInt(matcher.group(1)); - component.append(args[index]); - - lastIndex = matcher.end(); - } - component.append(translation.substring(lastIndex)); - - return component; - } - } catch (Exception ignored1) {} - Constants.LOGGER.error("Key \"{}\" not found in the default language (en_us), sending raw key as fallback.", key); - return Component.literal(key); - } + public static void sendSafetyWarning(ServerPlayer player, String command) { + // asks the player if they want to teleport anyway + player.displayClientMessage( + Component.empty() + .append(getTranslation("commands.teleport_commands.common.noSafeLocation", player) + .withStyle(ChatFormatting.RED, ChatFormatting.BOLD) + ) + .append("\n") + .append(getTranslation("commands.teleport_commands.common.safetyIsForLosers", player) + .withStyle(ChatFormatting.WHITE) + ) + .append("\n") + .append(getTranslation("commands.teleport_commands.common.forceTeleport", player) + .withStyle(ChatFormatting.DARK_AQUA, ChatFormatting.BOLD) + .withStyle(style -> style.withClickEvent(new ClickEvent.RunCommand(command))) + ) + .append("\n"), false); } - // Gets the ids of all the worlds public static List getWorldIds(MinecraftServer server) { return StreamSupport.stream(server.getAllLevels().spliterator(), false) diff --git a/common/src/main/resources/teleport_commands.mixins.json b/common/src/main/resources/teleport_commands.mixins.json index 6a35adf..76028db 100644 --- a/common/src/main/resources/teleport_commands.mixins.json +++ b/common/src/main/resources/teleport_commands.mixins.json @@ -5,8 +5,8 @@ "refmap": "${mod_id}.refmap.json", "compatibilityLevel": "JAVA_${java_version}", "mixins": [ - "ServerStartMixin", - "PlayerDeathMixin", + "MinecraftServerMixin", + "ServerPlayerMixin", "CommandsMixin" ], "client": [], diff --git a/gradle.properties b/gradle.properties index ff7372d..e6a135a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ # Every field you add must be added to the root build.gradle expandProps map. # Project -version=1.3.4 +version=1.4.0 group=dev.mrsnowy.teleport_commands java_version=21