|
| 1 | +package dev.padrewin.hudactionbar; |
| 2 | + |
| 3 | +import dev.padrewin.hudactionbar.manager.CommandManager; |
| 4 | +import dev.padrewin.hudactionbar.manager.LocaleManager; |
| 5 | +import dev.padrewin.hudactionbar.setting.SettingKey; |
| 6 | +import dev.padrewin.colddev.ColdPlugin; |
| 7 | +import dev.padrewin.colddev.config.ColdSetting; |
| 8 | +import dev.padrewin.colddev.manager.Manager; |
| 9 | +import dev.padrewin.colddev.manager.PluginUpdateManager; |
| 10 | +import me.clip.placeholderapi.PlaceholderAPI; |
| 11 | +import net.md_5.bungee.api.ChatColor; |
| 12 | +import net.md_5.bungee.api.ChatMessageType; |
| 13 | +import net.md_5.bungee.api.chat.TextComponent; |
| 14 | +import org.bukkit.Bukkit; |
| 15 | +import org.bukkit.entity.Player; |
| 16 | +import org.bukkit.event.EventHandler; |
| 17 | +import org.bukkit.event.Listener; |
| 18 | +import org.bukkit.event.player.PlayerJoinEvent; |
| 19 | +import org.bukkit.scheduler.BukkitRunnable; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.util.List; |
| 24 | +import java.util.regex.Matcher; |
| 25 | +import java.util.regex.Pattern; |
| 26 | + |
| 27 | +public class HudActionbar extends ColdPlugin implements Listener { |
| 28 | + |
| 29 | + /** |
| 30 | + * Console colors |
| 31 | + */ |
| 32 | + String ANSI_RESET = "\u001B[0m"; |
| 33 | + String ANSI_GREEN = "\u001B[32m"; |
| 34 | + String ANSI_YELLOW = "\u001B[33m"; |
| 35 | + String ANSI_RED = "\u001B[31m"; |
| 36 | + String ANSI_PURPLE = "\u001B[35m"; |
| 37 | + String ANSI_AQUA = "\u001B[36m"; |
| 38 | + |
| 39 | + private static HudActionbar instance; |
| 40 | + private String actionBarMessage; |
| 41 | + |
| 42 | + public HudActionbar() { |
| 43 | + super("Cold-Development", "HudActionbar", 23655, null, LocaleManager.class, null); |
| 44 | + instance = this; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void enable() { |
| 49 | + instance = this; |
| 50 | + getManager(PluginUpdateManager.class); |
| 51 | + |
| 52 | + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") == null) { |
| 53 | + getLogger().warning("PlaceholderAPI is not loaded. This plugin won't work properly."); |
| 54 | + getServer().getPluginManager().disablePlugin(this); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + String name = getDescription().getName(); |
| 59 | + getLogger().info(ANSI_PURPLE + " ____ ___ _ ____ " + ANSI_RESET); |
| 60 | + getLogger().info(ANSI_AQUA + " / ___/ _ \\| | | _ \\ " + ANSI_RESET); |
| 61 | + getLogger().info(ANSI_PURPLE + "| | | | | | | | | | |" + ANSI_RESET); |
| 62 | + getLogger().info(ANSI_AQUA + "| |__| |_| | |___| |_| |" + ANSI_RESET); |
| 63 | + getLogger().info(ANSI_PURPLE + " \\____\\___/|_____|____/ " + ANSI_RESET); |
| 64 | + getLogger().info(" " + ANSI_GREEN + name + ANSI_RED + " v" + getDescription().getVersion() + ANSI_RESET); |
| 65 | + getLogger().info(ANSI_YELLOW + " Author(s): " + ANSI_YELLOW + getDescription().getAuthors().get(0) + ANSI_RESET); |
| 66 | + getLogger().info(ANSI_AQUA + " (c) Cold Development ❄" + ANSI_RESET); |
| 67 | + getLogger().info(""); |
| 68 | + |
| 69 | + File configFile = new File(getDataFolder(), "en_US.yml"); |
| 70 | + if (!configFile.exists()) { |
| 71 | + saveDefaultConfig(); |
| 72 | + } |
| 73 | + |
| 74 | + saveDefaultConfig(); |
| 75 | + loadConfig(); |
| 76 | + getServer().getPluginManager().registerEvents(this, this); |
| 77 | + |
| 78 | + new BukkitRunnable() { |
| 79 | + @Override |
| 80 | + public void run() { |
| 81 | + if (actionBarMessage == null) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + for (Player player : getServer().getOnlinePlayers()) { |
| 86 | + String message = PlaceholderAPI.setPlaceholders(player, actionBarMessage); |
| 87 | + message = translateHexColorCodes(message); |
| 88 | + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message)); |
| 89 | + } |
| 90 | + } |
| 91 | + }.runTaskTimer(this, 0L, 20L); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void disable() { |
| 96 | + getLogger().info("HudActionbar disabled."); |
| 97 | + } |
| 98 | + |
| 99 | + @EventHandler |
| 100 | + public void onPlayerJoin(PlayerJoinEvent event) { |
| 101 | + Player player = event.getPlayer(); |
| 102 | + |
| 103 | + if (actionBarMessage == null) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { |
| 108 | + String message = PlaceholderAPI.setPlaceholders(player, actionBarMessage); |
| 109 | + message = translateHexColorCodes(message); |
| 110 | + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message)); |
| 111 | + } else { |
| 112 | + getLogger().warning("PlaceholderAPI is not loaded."); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public void loadConfig() { |
| 117 | + boolean isActionBarEnabled = getConfig().getBoolean("enableActionBarMessage", true); |
| 118 | + |
| 119 | + if (isActionBarEnabled) { |
| 120 | + actionBarMessage = getConfig().getString("actionBarMessage", "ɻAA7gD63ADïDB3t῰BAhB80C0u╵C6b	F92CC.&#AA9CD3c&#B4A5D9o&#BEAEDFm&#C8B8E5/&#D2C1ECC&#DCCBF2o&#E6D4F8l&#E0C9F8d&#DBBEF8-&#D5B4F8D&#D0A9F7e&#CA9EF7v&#C593F7e&#BF89F7l&#BA7EF7o&#B473F7p&#AF68F6m&#A95EF6e&#A453F6n	E48F6t"); |
| 121 | + if (actionBarMessage != null) { |
| 122 | + actionBarMessage = translateHexColorCodes(actionBarMessage); |
| 123 | + } else { |
| 124 | + getLogger().warning("actionBarMessage is null. Please check the configuration in config.yml."); |
| 125 | + } |
| 126 | + } else { |
| 127 | + actionBarMessage = null; |
| 128 | + getLogger().info("Action bar message is disabled in the config."); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + protected @NotNull List<Class<? extends Manager>> getManagerLoadPriority() { |
| 134 | + return List.of( |
| 135 | + CommandManager.class |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + protected @NotNull List<ColdSetting<?>> getColdConfigSettings() { |
| 141 | + return SettingKey.getKeys(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + protected String[] getColdConfigHeader() { |
| 146 | + return new String[] { |
| 147 | + " ____ ___ _ ____ ", |
| 148 | + " / ___|/ _ \\ | | | _ \\ ", |
| 149 | + "| | | | | || | | | | | ", |
| 150 | + "| |___| |_| || |___ | |_| | ", |
| 151 | + " \\____|\\___/ |_____|_____/ ", |
| 152 | + " " |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + public static HudActionbar getInstance() { |
| 157 | + if (instance == null) { |
| 158 | + throw new IllegalStateException("HudActionbar instance is not initialized!"); |
| 159 | + } |
| 160 | + return instance; |
| 161 | + } |
| 162 | + |
| 163 | + private String translateHexColorCodes(String message) { |
| 164 | + Pattern hexPattern = Pattern.compile("&#([A-Fa-f0-9]{6})"); |
| 165 | + Matcher matcher = hexPattern.matcher(message); |
| 166 | + StringBuffer buffer = new StringBuffer(message.length()); |
| 167 | + |
| 168 | + while (matcher.find()) { |
| 169 | + String hexColor = matcher.group(1); |
| 170 | + String replacement = ChatColor.of("#" + hexColor).toString(); |
| 171 | + matcher.appendReplacement(buffer, replacement); |
| 172 | + } |
| 173 | + |
| 174 | + matcher.appendTail(buffer); |
| 175 | + message = buffer.toString(); |
| 176 | + |
| 177 | + return ChatColor.translateAlternateColorCodes('&', message); |
| 178 | + } |
| 179 | +} |
0 commit comments