add per user locale, and user cache

This commit is contained in:
Ryder Belserion 2024-09-13 02:00:40 -04:00
parent 545bef1602
commit 717941d531
No known key found for this signature in database
15 changed files with 699 additions and 9 deletions

View File

@ -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;
}
}

View File

@ -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<User> 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();
}
}

View File

@ -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());
}
}

View File

@ -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<String> 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();
}
}

View File

@ -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();

View File

@ -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<String, List<String>> 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<CommandSourceStack> literal() {
final LiteralArgumentBuilder<CommandSourceStack> root = Commands.literal("help").requires(source -> source.getSender().hasPermission(getPermission()));
return root.executes(context -> {
final RequiredArgumentBuilder<CommandSourceStack, Integer> 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

View File

@ -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<String, SettingsManager> locales = new HashMap<>();
private static final Map<String, SettingsManager> 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<String> 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"));
}
}

View File

@ -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<String, List<String>> entry = new HashMap<>();
public final EntryProperty populate() {
this.entry.put("1", List.of(
"<bold><gold>━━━━━━━━━━━━━━━━━━━ CrazyAuctions Help ━━━━━━━━━━━━━━━━━━━</gold></bold>",
" ⤷ <red>/crazyauctions help - <white>Opens this help menu",
" ⤷ <red>/crazyauctions reload - <white>Reloads the plugin.",
"<bold><gold>━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━</gold></bold>"
));
return this;
}
public void setEntry(Map<String, List<String>> help) {
this.entry = help;
}
public Map<String, List<String>> getEntry() {
return this.entry;
}
}

View File

@ -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<String> property;
private Property<List<String>> properties;
private boolean isList = false;
Messages(@NotNull final Property<String> property) {
this.property = property;
}
Messages(@NotNull final Property<List<String>> 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<String> 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<String, String> placeholders = new HashMap<>() {{
put(placeholder, replacement);
}};
return getMessage(sender, placeholders);
}
public String getMessage(@NotNull final CommandSender sender, @NotNull final Map<String, String> 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<String, String> 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<String, String> 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<String, String> placeholders) {
sendMessage(this.plugin.getServer().getConsoleSender(), placeholders);
for (Player player : this.plugin.getServer().getOnlinePlayers()) {
sendMessage(player, placeholders);
}
}
}

View File

@ -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<String> prefix = newProperty("root.prefix", "<gray>[<dark_red>Crazy <blue>Auctions<gray>]: ");
@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<Boolean> 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<String> default_locale_file = newProperty("root.per-player-locale.default", "en_US");
@Comment("Should selling be enabled?")
public static final Property<Boolean> feature_selling_enabled = newProperty("features.selling", true);

View File

@ -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<String> internal_error = newProperty("errors.internal-error", "{prefix}<red>An internal error has occurred. Please check the console for the full error.");
public static final Property<String> message_empty = newProperty("errors.message-empty", "{prefix}<red>The message cannot be empty.");
}

View File

@ -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<String> unknown_command = newProperty("misc.unknown-command", "{prefix}<red>{command} is not a known command.");
@Comment("A list of available placeholders: {usage}")
public static final Property<String> correct_usage = newProperty("misc.correct-usage", "{prefix}<red>The correct usage for this command is <yellow>{usage}");
public static final Property<String> plugin_reload = newProperty("misc.plugin-reload", "{prefix}<green>Plugin has been reloaded.");
public static final Property<String> feature_disabled = newProperty("misc.feature-disabled", "{prefix}<red>This feature is disabled.");
@Comment("This is the message, for the /crazyauctions help command!")
public static final Property<EntryProperty> help = newBeanProperty(EntryProperty.class, "misc.command-help", new EntryProperty().populate());
}

View File

@ -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<String> must_be_a_player = newProperty("player.requirements.must-be-player", "{prefix}<red>You must be a player to use this command.");
public static final Property<String> must_be_console_sender = newProperty("player.requirements.must-be-console-sender", "{prefix}<red>You must be using console to use this command.");
@Comment("A list of available placeholders: {player}")
public static final Property<String> not_online = newProperty("player.target-not-online", "{prefix}<red>{player} <gray>is not online.");
public static final Property<String> same_player = newProperty("player.target-same-player", "{prefix}<red>You cannot use this command on yourself.");
public static final Property<String> no_permission = newProperty("player.no-permission", "{prefix}<red>You do not have permission to use that command!");
public static final Property<String> inventory_not_empty = newProperty("player.inventory-not-empty", "{prefix}<red>Inventory is not empty, Please clear up some room.");
}

View File

@ -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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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);
}
}

View File

@ -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}<red>{command} is not a known command."
# A list of available placeholders: {usage}
correct-usage: "{prefix}<red>The correct usage for this command is <yellow>{usage}"
feature-disabled: "{prefix}<red>This feature is disabled."
# This is the message, for the /lobbyplus help command!
command-help:
entry:
"1":
- "<bold><gold>━━━━━━━━━━━━━━━━━━━ CrazyAuctions Help ━━━━━━━━━━━━━━━━━━━</gold></bold>"
- " ⤷ <red>/crazyauctions help - <white>Opens this help menu"
- " ⤷ <red>/crazyauctions reload - <white>Reloads the plugin."
- "<bold><gold>━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━</gold></bold>"
# All messages related to players.
player:
requirements:
must-be-player: "{prefix}<red>You must be a player to use this command."
must-be-console-sender: "{prefix}<red>You must be using console to use this command."
# A list of available placeholders: {player}
target-not-online: "{prefix}<red>{player} <gray>is not online."
target-same-player: "{prefix}<red>You cannot use this command on yourself."
no-permission: "{prefix}<red>You do not have permission to use that command!"
inventory-not-empty: "{prefix}<red>Inventory is not empty, Please clear up some
room."
# All messages related to errors.
errors:
internal-error: "{prefix}<red>An internal error has occurred. Please check the console
for the full error."
message-empty: "{prefix}<red>The message cannot be empty."