diff --git a/paper/src/main/java/com/badbones69/crazyauctions/CrazyAuctions.java b/paper/src/main/java/com/badbones69/crazyauctions/CrazyAuctions.java index 2862ecc..a962ab6 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/CrazyAuctions.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/CrazyAuctions.java @@ -1,8 +1,10 @@ package com.badbones69.crazyauctions; import com.badbones69.crazyauctions.api.CrazyManager; +import com.badbones69.crazyauctions.api.UserManager; import com.badbones69.crazyauctions.api.enums.misc.Files; import com.badbones69.crazyauctions.api.enums.Messages; +import com.badbones69.crazyauctions.api.listeners.CacheListener; import com.badbones69.crazyauctions.api.support.MetricsWrapper; import com.badbones69.crazyauctions.commands.AuctionCommand; import com.badbones69.crazyauctions.commands.AuctionTab; @@ -37,6 +39,7 @@ public class CrazyAuctions extends Vital { } private CrazyManager crazyManager; + private UserManager userManager; private VaultSupport support; @@ -50,7 +53,9 @@ public class CrazyAuctions extends Vital { return; } - ConfigManager.load(); + ConfigManager.load(getDataFolder()); + + this.userManager = new UserManager(this); getFileManager().addFile(new File(getDataFolder(), "config.yml")) .addFile(new File(getDataFolder(), "data.yml")) @@ -121,8 +126,9 @@ public class CrazyAuctions extends Vital { this.crazyManager.load(); - getServer().getPluginManager().registerEvents(new GuiListener(), this); + getServer().getPluginManager().registerEvents(new CacheListener(), this); getServer().getPluginManager().registerEvents(new MarcoListener(), this); + getServer().getPluginManager().registerEvents(new GuiListener(), this); registerCommand(getCommand("ca"), new AuctionTab(), new AuctionCommand()); @@ -173,4 +179,8 @@ public class CrazyAuctions extends Vital { public final CrazyManager getCrazyManager() { return this.crazyManager; } + + public final UserManager getUserManager() { + return this.userManager; + } } \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/UserManager.java b/paper/src/main/java/com/badbones69/crazyauctions/api/UserManager.java new file mode 100644 index 0000000..f252504 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/UserManager.java @@ -0,0 +1,72 @@ +package com.badbones69.crazyauctions.api; + +import com.badbones69.crazyauctions.CrazyAuctions; +import com.badbones69.crazyauctions.api.objects.User; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +public class UserManager { + + private final Set users = new HashSet<>(); + + public UserManager(final CrazyAuctions plugin) { + this.users.add(new User(plugin.getServer().getConsoleSender())); + } + + public void addUser(final Player player) { + this.users.add(new User(player)); + } + + public void removeUser(final Player player) { + final User user = getUser(player); + + if (user == null) return; + + this.users.remove(user); + } + + public final User getUser(final CommandSender sender) { + if (sender instanceof Player player) { + return getUser(player); + } + + User user = null; + + final String name = sender.getName(); + + for (final User key : this.users) { + if (name.equals(key.sender.getName())) { + user = key; + + break; + } + } + + return user; + } + + public final User getUser(final Player player) { + User user = null; + + final UUID uuid = player.getUniqueId(); + + for (final User key : this.users) { + if (key.player == null) continue; + + if (uuid.equals(key.player.getUniqueId())) { + user = key; + + break; + } + } + + return user; + } + + public void purge() { + this.users.clear(); + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/listeners/CacheListener.java b/paper/src/main/java/com/badbones69/crazyauctions/api/listeners/CacheListener.java new file mode 100644 index 0000000..5ab4741 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/listeners/CacheListener.java @@ -0,0 +1,40 @@ +package com.badbones69.crazyauctions.api.listeners; + +import com.badbones69.crazyauctions.CrazyAuctions; +import com.badbones69.crazyauctions.api.UserManager; +import com.badbones69.crazyauctions.api.objects.User; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerLocaleChangeEvent; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import java.util.Locale; + +public class CacheListener implements Listener { + + private final CrazyAuctions plugin = CrazyAuctions.getPlugin(); + + private final UserManager userManager = this.plugin.getUserManager(); + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerLogin(PlayerLoginEvent event) { + this.userManager.addUser(event.getPlayer()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerLocaleChange(PlayerLocaleChangeEvent event) { + final Locale locale = event.locale(); + + final User user = this.userManager.getUser(event.getPlayer()); + + if (user == null) return; + + user.updateLocale(locale); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerQuit(PlayerQuitEvent event) { + this.userManager.removeUser(event.getPlayer()); + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/objects/User.java b/paper/src/main/java/com/badbones69/crazyauctions/api/objects/User.java new file mode 100644 index 0000000..f419a50 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/objects/User.java @@ -0,0 +1,93 @@ +package com.badbones69.crazyauctions.api.objects; + +import ch.jalu.configme.SettingsManager; +import com.badbones69.crazyauctions.configs.ConfigManager; +import com.ryderbelserion.vital.paper.api.enums.Support; +import com.ryderbelserion.vital.paper.util.AdvUtil; +import me.clip.placeholderapi.PlaceholderAPI; +import net.kyori.adventure.bossbar.BossBar; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +public class User { + + public final CommandSender sender; + public final Player player; + + public User(final CommandSender sender) { + if (sender instanceof Player target) { + this.sender = target; + this.player = target; + } else { + this.sender = sender; + this.player = null; + } + } + + public String locale = "en_US"; + + public final List activeBypassTypes = new ArrayList<>(); + + public boolean isPvpEnabled = false; + + public transient BossBar bossBar = null; + + public final User showBossBar() { + if (this.player == null) return this; + + final BossBar bar = this.bossBar; + + this.player.showBossBar(bar); + + return this; + } + + public final User createBossBar(final String name) { + if (this.player == null) return this; + + this.bossBar = BossBar.bossBar( + AdvUtil.parse(Support.placeholder_api.isEnabled() ? PlaceholderAPI.setPlaceholders(player, name) : name), + 0, + BossBar.Color.PURPLE, + BossBar.Overlay.NOTCHED_12 + ); + + return this; + } + + public final User hideBossBar() { + if (this.bossBar == null || this.player == null) return null; + + this.player.hideBossBar(this.bossBar); + + this.bossBar = null; + + return this; + } + + public final User updateLocale(final Locale locale) { + if (this.player == null) return this; + + this.locale = locale.getLanguage() + "_" + locale.getCountry(); + + return this; + } + + public final SettingsManager getLocale() { + return ConfigManager.getLocale(this.locale); + } + + // other checks + public final boolean hasPermission(final String permission) { + if (this.player == null) return false; + + return this.player.hasPermission(permission); + } + + public final String getName() { + return this.player != null ? this.player.getName() : this.sender.getName(); + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/AbstractCommand.java b/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/AbstractCommand.java index 253cc4f..b625b00 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/AbstractCommand.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/AbstractCommand.java @@ -3,6 +3,7 @@ package com.badbones69.crazyauctions.commands.v2; import ch.jalu.configme.SettingsManager; import com.badbones69.crazyauctions.CrazyAuctions; import com.badbones69.crazyauctions.api.CrazyManager; +import com.badbones69.crazyauctions.api.UserManager; import com.badbones69.crazyauctions.configs.ConfigManager; import com.badbones69.crazyauctions.configs.enums.Files; import com.ryderbelserion.vital.paper.commands.PaperCommand; @@ -12,6 +13,7 @@ public abstract class AbstractCommand extends PaperCommand { protected final CrazyAuctions plugin = CrazyAuctions.getPlugin(); protected final CrazyManager crazyManager = this.plugin.getCrazyManager(); + protected final UserManager userManager = this.plugin.getUserManager(); protected final Server server = this.plugin.getServer(); protected final SettingsManager config = ConfigManager.getConfig(); diff --git a/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/player/CommandHelp.java b/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/player/CommandHelp.java index 4f35015..34c259e 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/player/CommandHelp.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/player/CommandHelp.java @@ -1,23 +1,33 @@ package com.badbones69.crazyauctions.commands.v2.player; -import com.badbones69.crazyauctions.api.enums.Messages; import com.badbones69.crazyauctions.api.enums.other.Permissions; import com.badbones69.crazyauctions.commands.v2.AbstractCommand; +import com.badbones69.crazyauctions.configs.impl.locale.MiscKeys; +import com.badbones69.crazyauctions.utils.MsgUtils; +import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.tree.LiteralCommandNode; import com.ryderbelserion.vital.paper.commands.context.PaperCommandInfo; import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.command.brigadier.Commands; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import java.util.List; +import java.util.Map; +import static io.papermc.paper.command.brigadier.Commands.argument; public class CommandHelp extends AbstractCommand { @Override public void execute(final PaperCommandInfo info) { + final int page = info.getIntegerArgument("page"); + final CommandSender sender = info.getCommandSender(); - sender.sendMessage(Messages.HELP.getMessage(sender)); + final Map> help = this.userManager.getUser(sender).getLocale().getProperty(MiscKeys.help).getEntry(); + + help.get(String.valueOf(page)).forEach(line -> MsgUtils.sendMessage(sender, line, "{max}", String.valueOf(help.size()))); } @Override @@ -29,11 +39,17 @@ public class CommandHelp extends AbstractCommand { public @NotNull final LiteralCommandNode literal() { final LiteralArgumentBuilder root = Commands.literal("help").requires(source -> source.getSender().hasPermission(getPermission())); - return root.executes(context -> { + final RequiredArgumentBuilder arg1 = argument("page", IntegerArgumentType.integer()).suggests((ctx, builder) -> { + this.userManager.getUser(ctx.getSource().getSender()).getLocale().getProperty(MiscKeys.help).getEntry().keySet().forEach(builder::suggest); + + return builder.buildFuture(); + }).executes(context -> { execute(new PaperCommandInfo(context)); return com.mojang.brigadier.Command.SINGLE_SUCCESS; - }).build(); + }); + + return root.then(arg1).build(); } @Override diff --git a/paper/src/main/java/com/badbones69/crazyauctions/configs/ConfigManager.java b/paper/src/main/java/com/badbones69/crazyauctions/configs/ConfigManager.java index 2a4c82e..7590168 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/configs/ConfigManager.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/configs/ConfigManager.java @@ -3,21 +3,32 @@ package com.badbones69.crazyauctions.configs; import ch.jalu.configme.SettingsManager; import ch.jalu.configme.SettingsManagerBuilder; import ch.jalu.configme.resource.YamlFileResourceOptions; +import com.badbones69.crazyauctions.CrazyAuctions; import com.badbones69.crazyauctions.configs.enums.Files; import com.badbones69.crazyauctions.configs.impl.ConfigKeys; import com.badbones69.crazyauctions.configs.impl.gui.AuctionKeys; +import com.badbones69.crazyauctions.configs.impl.locale.ErrorKeys; +import com.badbones69.crazyauctions.configs.impl.locale.MiscKeys; +import com.badbones69.crazyauctions.configs.impl.locale.PlayerKeys; +import com.ryderbelserion.vital.common.utils.FileUtil; import org.jetbrains.annotations.NotNull; + +import java.io.File; import java.util.HashMap; +import java.util.List; import java.util.Map; public class ConfigManager { + private static final Map locales = new HashMap<>(); private static final Map configs = new HashMap<>(); /** * Loads configuration files. */ - public static void load() { + public static void load(final File dataFolder) { + final File localeFolder = new File(dataFolder, "locale"); + YamlFileResourceOptions options = YamlFileResourceOptions.builder().indentationSize(2).build(); for (Files file : Files.values()) { @@ -37,6 +48,28 @@ public class ConfigManager { configs.put(fileName, builder.create()); } + + FileUtil.extracts(CrazyAuctions.class, "/locale/", dataFolder.toPath().resolve("locale"), false); + + final List files = FileUtil.getFiles(dataFolder, "locale", ".yml"); + + locales.put("en_US", SettingsManagerBuilder + .withYamlFile(new File(localeFolder, "en_US.yml"), options) + .useDefaultMigrationService() + .configurationData(MiscKeys.class, PlayerKeys.class, ErrorKeys.class) + .create()); + + files.forEach(file -> { + if (!locales.containsKey(file)) { + final SettingsManager settings = SettingsManagerBuilder + .withYamlFile(new File(localeFolder, file + ".yml"), options) + .useDefaultMigrationService() + .configurationData(MiscKeys.class, PlayerKeys.class, ErrorKeys.class) + .create(); + + locales.put(file, settings); + } + }); } /** @@ -45,6 +78,8 @@ public class ConfigManager { public static void refresh() { getCustomConfig(Files.config.getFileName()).reload(); getCustomConfig(Files.auctions.getFileName()).reload(); + + locales.values().forEach(SettingsManager::reload); } /** @@ -70,4 +105,18 @@ public class ConfigManager { public @NotNull static SettingsManager getCustomConfig(final String fileName) { return configs.get(fileName); } + + /** + * Gets the locale for this identifier, or the default locale. + * + * @param locale the locale + * @return {@link SettingsManager} + */ + public static SettingsManager getLocale(final String locale) { + if (getConfig().getProperty(ConfigKeys.per_player_locale)) { + return locales.getOrDefault(getConfig().getProperty(ConfigKeys.default_locale_file), locales.get("en_US")); + } + + return locales.getOrDefault(locale, locales.get("en_US")); + } } \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/configs/beans/EntryProperty.java b/paper/src/main/java/com/badbones69/crazyauctions/configs/beans/EntryProperty.java new file mode 100644 index 0000000..4de1bc5 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/configs/beans/EntryProperty.java @@ -0,0 +1,29 @@ +package com.badbones69.crazyauctions.configs.beans; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class EntryProperty { + + private Map> entry = new HashMap<>(); + + public final EntryProperty populate() { + this.entry.put("1", List.of( + "━━━━━━━━━━━━━━━━━━━ CrazyAuctions Help ━━━━━━━━━━━━━━━━━━━", + " ⤷ /crazyauctions help - Opens this help menu", + " ⤷ /crazyauctions reload - Reloads the plugin.", + "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" + )); + + return this; + } + + public void setEntry(Map> help) { + this.entry = help; + } + + public Map> getEntry() { + return this.entry; + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/configs/enums/Messages.java b/paper/src/main/java/com/badbones69/crazyauctions/configs/enums/Messages.java new file mode 100644 index 0000000..29254fa --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/configs/enums/Messages.java @@ -0,0 +1,128 @@ +package com.badbones69.crazyauctions.configs.enums; + +import ch.jalu.configme.SettingsManager; +import ch.jalu.configme.properties.Property; +import com.badbones69.crazyauctions.CrazyAuctions; +import com.badbones69.crazyauctions.api.UserManager; +import com.badbones69.crazyauctions.api.objects.User; +import com.badbones69.crazyauctions.configs.ConfigManager; +import com.badbones69.crazyauctions.configs.impl.ConfigKeys; +import com.badbones69.crazyauctions.configs.impl.locale.ErrorKeys; +import com.badbones69.crazyauctions.configs.impl.locale.MiscKeys; +import com.badbones69.crazyauctions.configs.impl.locale.PlayerKeys; +import com.badbones69.crazyauctions.utils.MsgUtils; +import com.ryderbelserion.vital.common.utils.StringUtil; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public enum Messages { + + feature_disabled(MiscKeys.feature_disabled), + unknown_command(MiscKeys.unknown_command), + correct_usage(MiscKeys.correct_usage), + plugin_reload(MiscKeys.plugin_reload), + + internal_error(ErrorKeys.internal_error), + message_empty(ErrorKeys.message_empty), + + player_not_found(PlayerKeys.not_online), + no_permission(PlayerKeys.no_permission), + same_player(PlayerKeys.same_player), + + must_be_a_player(PlayerKeys.must_be_a_player), + must_be_console_sender(PlayerKeys.must_be_console_sender), + inventory_not_empty(PlayerKeys.inventory_not_empty); + + private Property property; + + private Property> properties; + private boolean isList = false; + + Messages(@NotNull final Property property) { + this.property = property; + } + + Messages(@NotNull final Property> properties, final boolean isList) { + this.properties = properties; + this.isList = isList; + } + + private final CrazyAuctions plugin = CrazyAuctions.getPlugin(); + + private final SettingsManager config = ConfigManager.getConfig(); + + private final UserManager userManager = this.plugin.getUserManager(); + + private boolean isList() { + return this.isList; + } + + public String getString(final CommandSender sender) { + final User user = this.userManager.getUser(sender); + + return ConfigManager.getLocale(user.locale).getProperty(this.property); + } + + public List getList(final CommandSender sender) { + final User user = this.userManager.getUser(sender); + + return ConfigManager.getLocale(user.locale).getProperty(this.properties); + } + + public String getMessage(@NotNull final CommandSender sender) { + return getMessage(sender, new HashMap<>()); + } + + public String getMessage(@NotNull final CommandSender sender, @NotNull final String placeholder, @NotNull final String replacement) { + Map placeholders = new HashMap<>() {{ + put(placeholder, replacement); + }}; + + return getMessage(sender, placeholders); + } + + public String getMessage(@NotNull final CommandSender sender, @NotNull final Map placeholders) { + return parse(sender, placeholders).replaceAll("\\{prefix}", this.config.getProperty(ConfigKeys.prefix)); + } + + public void sendMessage(final CommandSender sender, final String placeholder, final String replacement) { + sender.sendRichMessage(getMessage(sender, placeholder, replacement)); + } + + public void sendMessage(final CommandSender sender, final Map placeholders) { + sender.sendRichMessage(getMessage(sender, placeholders)); + } + + public void sendMessage(final CommandSender sender) { + sender.sendRichMessage(getMessage(sender)); + } + + private @NotNull String parse(@NotNull final CommandSender sender, @Nullable final Map placeholders) { + String message; + + if (isList()) { + message = StringUtil.chomp(StringUtil.convertList(getList(sender))); + } else { + message = getString(sender); + } + + return MsgUtils.getMessage(sender, message, placeholders); + } + + public void broadcast() { + broadcast(null); + } + + public void broadcast(@Nullable final Map placeholders) { + sendMessage(this.plugin.getServer().getConsoleSender(), placeholders); + + for (Player player : this.plugin.getServer().getOnlinePlayers()) { + sendMessage(player, placeholders); + } + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/ConfigKeys.java b/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/ConfigKeys.java index f1dc2d7..9b6fa11 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/ConfigKeys.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/ConfigKeys.java @@ -20,8 +20,8 @@ public class ConfigKeys implements SettingsHolder { "Support: https://discord.gg/badbones-s-live-chat-182615261403283459", "Github: https://github.com/Crazy-Crew", "", - "Issues: https://github.com/Crazy-Crew/BlockParticles/issues", - "Features: https://github.com/Crazy-Crew/BlockParticles/issues", + "Issues: https://github.com/Crazy-Crew/CrazyAuctions/issues", + "Features: https://github.com/Crazy-Crew/CrazyAuctions/issues", "", "List of all sounds: https://minecraft.wiki/w/Sounds.json#Java_Edition_values", "List of all enchantments: https://jd.papermc.io/paper/1.20/org/bukkit/enchantments/Enchantment.html" @@ -54,6 +54,19 @@ public class ConfigKeys implements SettingsHolder { @Comment("The prefix in front of CrazyAuction messages") public static final Property prefix = newProperty("root.prefix", "[Crazy Auctions]: "); + @Comment("This will take into consideration, what the client's locale is set to, on join and when they change it, if this is set to true.") + public static final Property per_player_locale = newProperty("root.per-player-locale.toggle", false); + + @Comment({ + "The default locale file, to display to players if the above option is set to false.", + "", + "A list of available localization:", + " ⤷ en_US (English America)", + " ⤷ de_DE (German)", + "" + }) + public static final Property default_locale_file = newProperty("root.per-player-locale.default", "en_US"); + @Comment("Should selling be enabled?") public static final Property feature_selling_enabled = newProperty("features.selling", true); diff --git a/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/locale/ErrorKeys.java b/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/locale/ErrorKeys.java new file mode 100644 index 0000000..24acd93 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/locale/ErrorKeys.java @@ -0,0 +1,24 @@ +package com.badbones69.crazyauctions.configs.impl.locale; + +import ch.jalu.configme.SettingsHolder; +import ch.jalu.configme.configurationdata.CommentsConfiguration; +import ch.jalu.configme.properties.Property; + +import static ch.jalu.configme.properties.PropertyInitializer.newProperty; + +public class ErrorKeys implements SettingsHolder { + + @Override + public void registerComments(CommentsConfiguration conf) { + String[] header = { + "All messages related to errors." + }; + + conf.setComment("errors", header); + } + + public static final Property internal_error = newProperty("errors.internal-error", "{prefix}An internal error has occurred. Please check the console for the full error."); + + public static final Property message_empty = newProperty("errors.message-empty", "{prefix}The message cannot be empty."); + +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/locale/MiscKeys.java b/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/locale/MiscKeys.java new file mode 100644 index 0000000..9edb8cb --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/locale/MiscKeys.java @@ -0,0 +1,41 @@ +package com.badbones69.crazyauctions.configs.impl.locale; + +import ch.jalu.configme.Comment; +import ch.jalu.configme.SettingsHolder; +import ch.jalu.configme.configurationdata.CommentsConfiguration; +import ch.jalu.configme.properties.Property; +import com.badbones69.crazyauctions.configs.beans.EntryProperty; +import static ch.jalu.configme.properties.PropertyInitializer.newBeanProperty; +import static ch.jalu.configme.properties.PropertyInitializer.newProperty; + +public class MiscKeys implements SettingsHolder { + + @Override + public void registerComments(CommentsConfiguration conf) { + String[] header = { + "Support: https://discord.gg/badbones-s-live-chat-182615261403283459", + "Github: https://github.com/Crazy-Crew", + "", + "Issues: https://github.com/Crazy-Crew/CrazyAuctions/issues", + "Features: https://github.com/Crazy-Crew/CrazyAuctions/issues", + "", + "All messages allow the use of {prefix} unless stated otherwise.", + "" + }; + + conf.setComment("misc", header); + } + + @Comment("A list of available placeholders: {command}") + public static final Property unknown_command = newProperty("misc.unknown-command", "{prefix}{command} is not a known command."); + + @Comment("A list of available placeholders: {usage}") + public static final Property correct_usage = newProperty("misc.correct-usage", "{prefix}The correct usage for this command is {usage}"); + + public static final Property plugin_reload = newProperty("misc.plugin-reload", "{prefix}Plugin has been reloaded."); + + public static final Property feature_disabled = newProperty("misc.feature-disabled", "{prefix}This feature is disabled."); + + @Comment("This is the message, for the /crazyauctions help command!") + public static final Property help = newBeanProperty(EntryProperty.class, "misc.command-help", new EntryProperty().populate()); +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/locale/PlayerKeys.java b/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/locale/PlayerKeys.java new file mode 100644 index 0000000..0f3767c --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/configs/impl/locale/PlayerKeys.java @@ -0,0 +1,33 @@ +package com.badbones69.crazyauctions.configs.impl.locale; + +import ch.jalu.configme.Comment; +import ch.jalu.configme.SettingsHolder; +import ch.jalu.configme.configurationdata.CommentsConfiguration; +import ch.jalu.configme.properties.Property; + +import static ch.jalu.configme.properties.PropertyInitializer.newProperty; + +public class PlayerKeys implements SettingsHolder { + + @Override + public void registerComments(CommentsConfiguration conf) { + String[] header = { + "All messages related to players." + }; + + conf.setComment("player", header); + } + + public static final Property must_be_a_player = newProperty("player.requirements.must-be-player", "{prefix}You must be a player to use this command."); + + public static final Property must_be_console_sender = newProperty("player.requirements.must-be-console-sender", "{prefix}You must be using console to use this command."); + + @Comment("A list of available placeholders: {player}") + public static final Property not_online = newProperty("player.target-not-online", "{prefix}{player} is not online."); + + public static final Property same_player = newProperty("player.target-same-player", "{prefix}You cannot use this command on yourself."); + + public static final Property no_permission = newProperty("player.no-permission", "{prefix}You do not have permission to use that command!"); + + public static final Property inventory_not_empty = newProperty("player.inventory-not-empty", "{prefix}Inventory is not empty, Please clear up some room."); +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/utils/MsgUtils.java b/paper/src/main/java/com/badbones69/crazyauctions/utils/MsgUtils.java new file mode 100644 index 0000000..a8d40ac --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/utils/MsgUtils.java @@ -0,0 +1,103 @@ +package com.badbones69.crazyauctions.utils; + +import ch.jalu.configme.SettingsManager; +import com.badbones69.crazyauctions.CrazyAuctions; +import com.badbones69.crazyauctions.api.objects.User; +import com.badbones69.crazyauctions.configs.ConfigManager; +import com.badbones69.crazyauctions.configs.impl.ConfigKeys; +import com.ryderbelserion.vital.paper.api.enums.Support; +import com.ryderbelserion.vital.paper.util.AdvUtil; +import me.clip.placeholderapi.PlaceholderAPI; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.title.Title; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; + +public class MsgUtils { + + private static final CrazyAuctions plugin = CrazyAuctions.getPlugin(); + + private static final SettingsManager config = ConfigManager.getConfig(); + + public static String getLocale(final CommandSender sender) { + final User user = plugin.getUserManager().getUser(sender); + + String locale = "en_US"; + + if (user != null) { + locale = user.locale; + } + + return locale; + } + + public static void sendMessage(final CommandSender sender, final String message, @Nullable final Map placeholders) { + if (message.isEmpty()) return; + + final String msg = getMessage(sender, message, placeholders); + + sender.sendRichMessage(msg); + } + + public static void sendActionBar(final CommandSender sender, final String message, @Nullable final Map placeholders) { + if (message.isEmpty()) return; + + sender.sendActionBar(AdvUtil.parse(getMessage(sender, message, placeholders))); + } + + public static void sendTitle(final Player player, final String message, @Nullable final Map placeholders) { + if (message.isEmpty()) return; + + final Title.Times times = Title.Times.times(Duration.ofMillis(400), Duration.ofMillis(200), Duration.ofMillis(400)); + final Title title = Title.title(AdvUtil.parse(getMessage(player, message, placeholders)), Component.empty(), times); + + player.showTitle(title); + } + + public static void sendMessage(final CommandSender sender, final String message, final String placeholder, final String replacement) { + sendMessage(sender, message, new HashMap<>() {{ + put(placeholder, replacement); + }}); + } + + public static void sendMessage(final CommandSender sender, final String message) { + sendMessage(sender, message, null); + } + + public static String getMessage(final CommandSender sender, final String message, @Nullable final Map placeholders) { + String msg = message; + + if (sender instanceof Player player) { + if (Support.placeholder_api.isEnabled()) { + msg = PlaceholderAPI.setPlaceholders(player, msg); + } + } + + if (placeholders != null && !placeholders.isEmpty()) { + for (Map.Entry placeholder : placeholders.entrySet()) { + if (placeholder != null) { + final String key = placeholder.getKey(); + final String value = placeholder.getValue(); + + if (key != null && value != null) { + msg = msg.replace(key, value).replace(key.toLowerCase(), value); + } + } + } + } + + return msg; + } + + /** + * @return the {@link String} + */ + public static @NotNull String getPrefix() { + return config.getProperty(ConfigKeys.prefix); + } +} \ No newline at end of file diff --git a/paper/src/main/resources/locale/en_US.yml b/paper/src/main/resources/locale/en_US.yml new file mode 100644 index 0000000..1f837ef --- /dev/null +++ b/paper/src/main/resources/locale/en_US.yml @@ -0,0 +1,37 @@ +# Github: https://github.com/CoreCraftMC +# +# Issues: https://github.com/CoreCraftMC/LobbyPlus/issues +# Features: https://github.com/CoreCraftMC/LobbyPlus/issues +# +# All messages allow the use of {prefix} unless stated otherwise. +# +misc: + # A list of available placeholders: {command} + unknown-command: "{prefix}{command} is not a known command." + # A list of available placeholders: {usage} + correct-usage: "{prefix}The correct usage for this command is {usage}" + feature-disabled: "{prefix}This feature is disabled." + # This is the message, for the /lobbyplus help command! + command-help: + entry: + "1": + - "━━━━━━━━━━━━━━━━━━━ CrazyAuctions Help ━━━━━━━━━━━━━━━━━━━" + - " ⤷ /crazyauctions help - Opens this help menu" + - " ⤷ /crazyauctions reload - Reloads the plugin." + - "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" +# All messages related to players. +player: + requirements: + must-be-player: "{prefix}You must be a player to use this command." + must-be-console-sender: "{prefix}You must be using console to use this command." + # A list of available placeholders: {player} + target-not-online: "{prefix}{player} is not online." + target-same-player: "{prefix}You cannot use this command on yourself." + no-permission: "{prefix}You do not have permission to use that command!" + inventory-not-empty: "{prefix}Inventory is not empty, Please clear up some + room." +# All messages related to errors. +errors: + internal-error: "{prefix}An internal error has occurred. Please check the console + for the full error." + message-empty: "{prefix}The message cannot be empty." \ No newline at end of file