|
| 1 | +package org.devschool.modules.messages.manager; |
| 2 | + |
| 3 | +import org.bukkit.ChatColor; |
| 4 | +import org.bukkit.entity.Player; |
| 5 | +import org.devschool.Main; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +public class MessagesManager { |
| 12 | + |
| 13 | + private Main plugin; |
| 14 | + |
| 15 | + private HashMap<String, String> messageHashMap; |
| 16 | + private List<String> legacyColors = Arrays.asList("&0", "&1", "&2", "&3", "&4", "&5", "&6", "&7", "&8", "&9", "&a", "&b", "&c", "&d", "&e", "§0", "§1", "§2", "§3", "§4", "§5", "§6", "§7", "§8", "§9", "§a", "§b", "§c", "§d", "§e"); |
| 17 | + private List<String> specialChars = Arrays.asList("&l", "&n", "&o", "&k", "&m", "§l", "§n", "§o", "§k", "§m"); |
| 18 | + |
| 19 | + /** |
| 20 | + * Simple example of manager for messages without database! |
| 21 | + * |
| 22 | + * The easiest way to store messages, |
| 23 | + * because of the education of people only, |
| 24 | + * this way is much cut and easier |
| 25 | + * |
| 26 | + */ |
| 27 | + public MessagesManager() { |
| 28 | + this.plugin = Main.getInstance(); |
| 29 | + loadData(); |
| 30 | + } |
| 31 | + |
| 32 | + public void loadData() { |
| 33 | + messageHashMap = new HashMap<>(); |
| 34 | + |
| 35 | + addMessage("super", "&cSuper zpráva"); |
| 36 | + addMessage("bad", "Super zpráva"); |
| 37 | + addMessage("no_perm", "&cMáš nedostatečná oprávnění na to provést tento příkaz!"); |
| 38 | + addMessage("success_remove", "&aÚspěšně si odstranil zprávů!"); |
| 39 | + } |
| 40 | + |
| 41 | + public void removeMessage(String name) { |
| 42 | + getMessages().remove(name.toLowerCase()); |
| 43 | + } |
| 44 | + |
| 45 | + public String removeColorsAndChars(String input) { |
| 46 | + for (String chars : specialChars) { |
| 47 | + if (!input.contains(chars)) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + input = input.replaceAll(chars, ""); |
| 51 | + } |
| 52 | + |
| 53 | + for (String colors : legacyColors) { |
| 54 | + if (!input.contains(colors)) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + input = input.replaceAll(colors, ""); |
| 58 | + } |
| 59 | + |
| 60 | + return input; |
| 61 | + } |
| 62 | + |
| 63 | + public void sendMessage(Player player, String msg, boolean colorize) { |
| 64 | + if (!colorize) { |
| 65 | + msg = removeColorsAndChars(msg); |
| 66 | + player.sendMessage(msg); |
| 67 | + return; |
| 68 | + } |
| 69 | + player.sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + public void addMessage(String name, String content) { |
| 74 | + getMessages().put(name.toLowerCase(), content); |
| 75 | + } |
| 76 | + |
| 77 | + public String getMsg(String name) { |
| 78 | + return messageHashMap.get(name.toLowerCase()); |
| 79 | + } |
| 80 | + |
| 81 | + public HashMap<String, String> getMessages() { |
| 82 | + return messageHashMap; |
| 83 | + } |
| 84 | +} |
0 commit comments