Created
June 13, 2013 02:44
-
-
Save sahirshahryar/5770884 to your computer and use it in GitHub Desktop.
Assists in loading messages for your plugin. Inspired by ProjectInfinity's version for ReportRTS, but made to be instantiated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.futuredev.tracker.util.msg; | |
import org.bukkit.ChatColor; | |
import org.bukkit.configuration.file.FileConfiguration; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import java.io.File; | |
import java.io.InputStream; | |
import java.text.MessageFormat; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Set; | |
// Represents a messages map. | |
// Inspired by ProjectInfinity's message reader, but designed to be instantiable to a plugin. | |
public class MessageAssist { | |
File messageFile; | |
JavaPlugin plugin; | |
String fileName; | |
FileConfiguration fileConfig; | |
Map<String, String> messageMap = new HashMap<String, String>(); | |
public MessageAssist (JavaPlugin plugin, String fileName) { | |
this.plugin = plugin; | |
this.fileName = fileName + ".yml"; | |
messageFile = new File(this.plugin.getDataFolder(), fileName); | |
messageMap = setupMessageMap(); | |
} | |
public void reload () { | |
InputStream inputStream = plugin.getResource(fileName); | |
if (inputStream != null) { | |
YamlConfiguration config = YamlConfiguration.loadConfiguration(inputStream); | |
fileConfig.setDefaults(config); | |
} | |
} | |
public Map<String, String> setupMessageMap () { | |
Set<String> messages = fileConfig.getKeys(true); | |
for (String s : messages) { | |
messageMap.put(s, fileConfig.getString(s)); | |
} return messageMap; | |
} | |
public String read (String str, Object ... variables) { | |
Object obj = messageMap.get(str); | |
if (obj == null || !(obj instanceof String)) { | |
if (!fileConfig.getDefaults().contains(str)) { | |
return ChatColor.RED + "Unable to find a value for " + str + "."; | |
} messageMap.put(str, fileConfig.getDefaults().getString(str)); | |
fileConfig.set(str, fileConfig.getDefaults().getString(str)); | |
obj = fileConfig.getDefaults().getString(str); | |
} String msg = (String) obj; | |
msg = MessageFormat.format(msg, variables); | |
return transformMessage(msg, true, true, false); | |
} | |
public boolean messageExists (String str) { | |
return messageMap.get(str) != null; | |
} | |
public String parseChatColours (String msg) { | |
for(ChatColor colour : ChatColor.values()){ | |
msg = msg.replaceAll("&" + colour.getChar(), colour.toString()); | |
} return msg; | |
} | |
public String stripColour (String msg) { | |
for(ChatColor colour : ChatColor.values()){ | |
msg = msg.replaceAll("&" + colour.getChar(), ""); | |
} return msg; | |
} | |
public String transformMessage (String msg, boolean newLine, boolean colours, boolean strip) { | |
msg = (strip ? stripColour(msg) : msg); | |
msg = (newLine ? msg.replaceAll("$n", "\n") : msg); | |
msg = (colours ? parseChatColours(msg) : msg); | |
return msg; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment