|
| 1 | +package de.paul2708.memory.file; |
| 2 | + |
| 3 | +import de.paul2708.memory.Memory; |
| 4 | +import de.paul2708.memory.util.Constants; |
| 5 | +import org.bukkit.ChatColor; |
| 6 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | + |
| 11 | +/** |
| 12 | + * Created by Paul on 28.07.2017. |
| 13 | + */ |
| 14 | +public class MessageFile { |
| 15 | + |
| 16 | + private File directory; |
| 17 | + private File configFile; |
| 18 | + private YamlConfiguration configuration; |
| 19 | + |
| 20 | + private boolean first; |
| 21 | + |
| 22 | + public MessageFile(File directory) { |
| 23 | + this.directory = directory; |
| 24 | + |
| 25 | + this.first = false; |
| 26 | + } |
| 27 | + |
| 28 | + public void load() { |
| 29 | + try { |
| 30 | + // Create directory |
| 31 | + if (!directory.exists()) { |
| 32 | + directory.mkdir(); |
| 33 | + } |
| 34 | + |
| 35 | + // Create file |
| 36 | + this.configFile = new File(directory.getPath(), "messages.yml"); |
| 37 | + if (!configFile.exists()) { |
| 38 | + configFile.createNewFile(); |
| 39 | + this.first = true; |
| 40 | + |
| 41 | + Memory.getInstance().log(Constants.TAG + "§amessages.yml was created. §cEdit and restart your server."); |
| 42 | + } |
| 43 | + |
| 44 | + // Load configuration |
| 45 | + this.configuration = YamlConfiguration.loadConfiguration(configFile); |
| 46 | + |
| 47 | + // Create default value |
| 48 | + if (first) { |
| 49 | + createDefaultValues(); |
| 50 | + } |
| 51 | + } catch (IOException e) { |
| 52 | + e.printStackTrace(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private void createDefaultValues() { |
| 57 | + configuration.set("tag", "&8[&eMemory&8]"); |
| 58 | + |
| 59 | + configuration.set("game.against", "%tag% &7You are playing against &e%player%&7."); |
| 60 | + configuration.set("game.first_turn", "%tag% &e%player% &7starts. (Theme: %theme%))"); |
| 61 | + configuration.set("game.turn", "%tag% &6%player% goes on."); |
| 62 | + configuration.set("game.not_your_turn", "%tag% &cIt's not your turn."); |
| 63 | + configuration.set("game.pair_found", "%tag% &e%player% found a pair.."); |
| 64 | + configuration.set("game.no_pair_found", "%tag% &7%player% didn't find a pair."); |
| 65 | + configuration.set("game.again", "%tag% &6%player% can take another card."); |
| 66 | + configuration.set("game.again_player", "%tag% &6You can take another card."); |
| 67 | + |
| 68 | + configuration.set("queue.already_in", "%tag% &cYour are already in the queue."); |
| 69 | + configuration.set("queue.added", "%tag% &aYou joined the queue. &7Waiting for player.."); |
| 70 | + |
| 71 | + configuration.set("result.draw", "%tag% &eDraw! - Nobody won."); |
| 72 | + configuration.set("result.win", "%tag% &e%player% has won the game. (%score% pairs)"); |
| 73 | + |
| 74 | + try { |
| 75 | + configuration.save(configFile); |
| 76 | + } catch (IOException e) { |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public String getMessage(String path, String... replace) { |
| 82 | + String message = configuration.getString(path); |
| 83 | + |
| 84 | + message = message.replaceAll("%tag%", configuration.getString("tag")); |
| 85 | + |
| 86 | + if (replace.length != 0) { |
| 87 | + if (message.contains("%player%")) { |
| 88 | + message = message.replaceAll("%player%", replace[0]); |
| 89 | + } |
| 90 | + if (message.contains("%theme%")) { |
| 91 | + message = message.replaceAll("%theme%", replace[1]); |
| 92 | + } |
| 93 | + if (message.contains("%score%")) { |
| 94 | + message = message.replaceAll("%score%", replace[1]); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + message = ChatColor.translateAlternateColorCodes('&', message); |
| 99 | + |
| 100 | + return message; |
| 101 | + } |
| 102 | +} |
0 commit comments