Commit 11cd20a0 authored by Giant's avatar Giant
Browse files

Centralize some heavily used libraries.

parent f155e652
package nl.giantit.minecraft.giantcore.Misc;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.command.CommandSender;
/**
*
* @author Giant
*/
public class Heraut {
public static String parse(String input) {
return ChatColor.translateAlternateColorCodes('&', input);//input.replaceAll("(&([a-fA-F0-9]))", "§$2").replace("\\\\\u00A7", "&");
}
public static void say (Player player, String message) {
player.sendMessage(parse(message));
}
public static void say (CommandSender sender, String message) {
sender.sendMessage(message);
}
public static void broadcast (String message) {
for(Player p : Bukkit.getServer().getOnlinePlayers()) {
p.sendMessage(parse(message));
}
}
public static void broadcast (String message, Boolean opOnly) {
if(!opOnly) {
broadcast(message);
return;
}
for(Player p : Bukkit.getServer().getOnlinePlayers()) {
if(p.isOp()) {
p.sendMessage(parse(message));
}
}
}
}
package nl.giantit.minecraft.giantcore.Misc;
import nl.giantit.minecraft.giantcore.GiantPlugin;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
/**
*
* @author Giant
*/
public class Messages {
private final GiantPlugin plugin;
private YamlConfiguration config;
private final double yamlVersion;
private HashMap<String, String> mainMsgs, adminMsgs, errorMsgs;
public enum msgType {
ERROR,
MAIN,
ADMIN
}
private void loadMain() {
this.mainMsgs = new HashMap<String, String>();
Set<String> list = config.getConfigurationSection("main").getKeys(false);
if(list == null) {
this.plugin.getLogger().log(Level.WARNING, "[" + plugin.getName() + "] There are no main messages specified in the msgTemplates.yml file. This might cause errors!");
return;
}
for(String key : list) {
String msg = config.getString("main." + key);
this.mainMsgs.put(key, msg);
}
}
private void loadAdmin() {
this.adminMsgs = new HashMap<String, String>();
Set<String> list = config.getConfigurationSection("admin").getKeys(false);
if(list == null) {
this.plugin.getLogger().log(Level.WARNING, "[" + plugin.getName() + "] There are no admin messages specified in the msgTemplates.yml file. This might cause errors!");
return;
}
for(String key : list) {
String msg = config.getString("admin." + key);
this.adminMsgs.put(key, msg);
}
}
private void loadErrors() {
this.errorMsgs = new HashMap<String, String>();
Set<String> list = config.getConfigurationSection("errors").getKeys(false);
if(list == null) {
this.plugin.getLogger().log(Level.WARNING, "[" + plugin.getName() + "] There are no error messages specified in the msgTemplates.yml file. This might cause errors!");
return;
}
for(String key : list) {
String msg = config.getString("errors." + key);
this.errorMsgs.put(key, msg);
}
}
public Messages(GiantPlugin plugin, double yV) {
this.plugin = plugin;
this.yamlVersion = yV;
File configFile = new File(plugin.getDir(), "msgTemplate.yml");
if(!configFile.exists()) {
this.plugin.getLogger().log(Level.INFO, "[" + plugin.getName() + "] Extracting new msgTemplate.yml file...");
plugin.extract("msgTemplate.yml");
}
config = YamlConfiguration.loadConfiguration(configFile);
double v = config.getDouble("version");
if(v < this.yamlVersion) {
this.plugin.getLogger().log(Level.INFO, "[" + plugin.getName() + "] Your msgTemplate.yml has ran out of date. Updating now!");
File oconfigFile = new File(plugin.getDir(), "msgTemplate.yml." + v + ".bak");
configFile.renameTo(oconfigFile);
plugin.extract("msgTemplate.yml");
config = YamlConfiguration.loadConfiguration(configFile);
}
this.loadMain();
this.loadAdmin();
this.loadErrors();
}
public void reload() {
File configFile = new File(plugin.getDir(), "msgTemplate.yml");
if(!configFile.exists()) {
this.plugin.getLogger().log(Level.INFO, "[" + plugin.getName() + "] Extracting new msgTemplate.yml file...");
plugin.extract("msgTemplate.yml");
}
config = YamlConfiguration.loadConfiguration(configFile);
double v = config.getDouble("version");
if(v < this.yamlVersion) {
this.plugin.getLogger().log(Level.INFO, "[" + plugin.getName() + "] Your msgTemplate.yml has ran out of date. Updating now!");
File oconfigFile = new File(plugin.getDir(), "msgTemplate.yml." + v + ".bak");
configFile.renameTo(oconfigFile);
plugin.extract("msgTemplate.yml");
config = YamlConfiguration.loadConfiguration(configFile);
}
this.loadMain();
this.loadAdmin();
this.loadErrors();
}
public String getMsg(msgType type, String template) {
return this.getMsg(type, template, null);
}
public String getMsg(msgType type, String template, HashMap<String, String> data) {
String Template = null;
if(data == null)
data = new HashMap<String, String>();
switch(type) {
case MAIN:
if(this.mainMsgs.containsKey(template))
Template = this.mainMsgs.get(template);
break;
case ADMIN:
if(this.adminMsgs.containsKey(template))
Template = this.adminMsgs.get(template);
break;
case ERROR:
if(this.errorMsgs.containsKey(template))
Template = this.errorMsgs.get(template);
break;
}
if(Template != null) {
for(Map.Entry<String, String> entry : data.entrySet()) {
if(entry.getKey() != null && entry.getValue() != null) {
Template = Template.replace("%" + entry.getKey(), entry.getValue());
}else{
this.plugin.getLogger().log(Level.SEVERE, "[" + plugin.getName() + "] Message template engine received invalid parameter! (null)");
}
}
Template = Template.replace("%n", plugin.getPubName());
Template = Template.replace("%colon", ":");
Template = Template.replace("%sharp", "#");
Template = Messages.colourfy(Template);
return Template;
}
this.plugin.getLogger().log(Level.SEVERE, "[" + plugin.getName() + "] Template for " + template + " does not exist!");
return "&cRequested template does not exist!";
}
public String getConsoleMsg(msgType type, String template) {
return this.getConsoleMsg(type, template, null);
}
public String getConsoleMsg(msgType type, String template, HashMap<String, String> data) {
String Template = null;
if(data == null)
data = new HashMap<String, String>();
switch(type) {
case MAIN:
if(this.mainMsgs.containsKey(template))
Template = this.mainMsgs.get(template);
break;
case ADMIN:
if(this.adminMsgs.containsKey(template))
Template = this.adminMsgs.get(template);
break;
case ERROR:
if(this.errorMsgs.containsKey(template))
Template = this.errorMsgs.get(template);
break;
}
if(Template != null) {
for(Map.Entry<String, String> entry : data.entrySet()) {
if(entry.getKey() != null && entry.getValue() != null) {
Template = Template.replace("%" + entry.getKey(), entry.getValue());
}else{
this.plugin.getLogger().log(Level.SEVERE, "[" + plugin.getName() + "] Message template engine received invalid parameter! (null)");
}
}
Template = Template.replace("%n", plugin.getPubName());
Template = Template.replace("%colon", ":");
Template = Template.replace("%sharp", "#");
Template = Messages.deColourfy(Template);
return Template;
}
this.plugin.getLogger().log(Level.SEVERE, "[" + plugin.getName() + "] Template for " + template + " does not exist!");
return "&cRequested template does not exist!";
}
public static String colourfy(String template) {
/*template = template.replaceAll("C_BLACK", "&0");
template = template.replaceAll("C_DARKBLUE", "&1");
template = template.replaceAll("C_GREEN", "&2");
template = template.replaceAll("C_BLUE", "&3");
template = template.replaceAll("C_DARKRED", "&4");
template = template.replaceAll("C_MAGENTA", "&5");
template = template.replaceAll("C_GOLD", "&6");
template = template.replaceAll("C_LIGHTGRAY", "&7");
template = template.replaceAll("C_GRAY", "&8");
template = template.replaceAll("C_PURPLE", "&9");
template = template.replaceAll("C_LIGHTGREEN", "&a");
template = template.replaceAll("C_LIGHTBLUE", "&b");
template = template.replaceAll("C_RED", "&c");
template = template.replaceAll("C_PINK", "&d");
template = template.replaceAll("C_YELLOW", "&e");
template = template.replaceAll("C_WHITE", "&f");*/
template = template.replaceAll("C_BLACK", ChatColor.BLACK + "");
template = template.replaceAll("C_DARKBLUE", ChatColor.DARK_BLUE + "");
template = template.replaceAll("C_GREEN", ChatColor.DARK_GREEN + "");
template = template.replaceAll("C_BLUE", ChatColor.DARK_AQUA + "");
template = template.replaceAll("C_DARKRED", ChatColor.DARK_RED + "");
template = template.replaceAll("C_MAGENTA", ChatColor.DARK_PURPLE + "");
template = template.replaceAll("C_GOLD", ChatColor.GOLD + "");
template = template.replaceAll("C_LIGHTGRAY", ChatColor.GRAY + "");
template = template.replaceAll("C_GRAY", ChatColor.DARK_GRAY + "");
template = template.replaceAll("C_PURPLE", ChatColor.BLUE + "");
template = template.replaceAll("C_LIGHTGREEN", ChatColor.GREEN + "");
template = template.replaceAll("C_LIGHTBLUE", ChatColor.AQUA + "");
template = template.replaceAll("C_RED", ChatColor.RED + "");
template = template.replaceAll("C_PINK", ChatColor.LIGHT_PURPLE + "");
template = template.replaceAll("C_YELLOW", ChatColor.YELLOW + "");
template = template.replaceAll("C_WHITE", ChatColor.WHITE + "");
return template;
}
public static String deColourfy(String template) {
template = template.replaceAll("C_BLACK", "");
template = template.replaceAll("C_DARKBLUE", "");
template = template.replaceAll("C_GREEN", "");
template = template.replaceAll("C_BLUE", "");
template = template.replaceAll("C_DARKRED", "");
template = template.replaceAll("C_MAGENTA", "");
template = template.replaceAll("C_GOLD", "");
template = template.replaceAll("C_LIGHTGRAY", "");
template = template.replaceAll("C_GRAY", "");
template = template.replaceAll("C_PURPLE", "");
template = template.replaceAll("C_LIGHTGREEN", "");
template = template.replaceAll("C_LIGHTBLUE", "");
template = template.replaceAll("C_RED", "");
template = template.replaceAll("C_PINK", "");
template = template.replaceAll("C_YELLOW", "");
template = template.replaceAll("C_WHITE", "");
return template;
}
}
package nl.giantit.minecraft.giantcore.Misc;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* @author Giant
*/
public class Misc {
private final static HashMap<String, OfflinePlayer> players = new HashMap<String, OfflinePlayer>();
private static OfflinePlayer getOfflinePlayer(final String name) {
OfflinePlayer found = null;
int lastLength = Integer.MAX_VALUE;
for(OfflinePlayer p : Bukkit.getServer().getOfflinePlayers()) {
if(p.getFirstPlayed() <= 0)
continue;
if (p.getName().toLowerCase().startsWith(name.toLowerCase())) {
int length = p.getName().length() - name.length();
if(length < lastLength) {
found = p;
lastLength = length;
}
if(length == 0)
break;
}
}
if(found != null)
players.put(name, found);
return found;
}
public static boolean isEither(String target, String is, String either) {
if(target.equals(is) || target.equals(either))
return true;
return false;
}
public static boolean isEitherIgnoreCase(String target, String is, String either) {
if(target.equalsIgnoreCase(is) || target.equalsIgnoreCase(either))
return true;
return false;
}
public static boolean isAny(String target, String... arr) {
for(String is : arr) {
if(target.equals(is))
return true;
}
return false;
}
public static boolean isAnyIgnoreCase(String target, String... arr) {
for(String is : arr) {
if(target.equalsIgnoreCase(is))
return true;
}
return false;
}
public static Boolean contains(List<String> haystack, String needle) {
for(String hay : haystack) {
hay = hay.replace("[", "");
hay = hay.replace("]", "");
if(hay.toLowerCase().equalsIgnoreCase(needle.toLowerCase())) {
return true;
}
}
return false;
}
public static String join(String glue, String... input) {
StringBuilder sb = new StringBuilder();
int i = 0;
for(String string : input) {
sb.append(string);
if(i < input.length - 1)
sb.append(glue);
i++;
}
return sb.toString();
}
public static double Round(double r, int precision) {
if(precision < 1)
precision = 1;
double p = Math.pow(10,precision);
return Math.round(r * p) / p;
}
public static float Round(float r, int precision) {
if(precision < 1)
precision = 1;
float p = (float)Math.pow(10,precision);
return Math.round(r * p) / p;
}
public static OfflinePlayer getPlayer(String name) {
if(players.containsKey(name))
return players.get(name);
return getOfflinePlayer(name);
}
public static boolean constainsKeyIgnoreCase(Set<String> haystack, String needle) {
for(String straw : haystack) {
if(straw.equalsIgnoreCase(needle))
return true;
}
return false;
}
public static Object getIgnoreCase(Map<String, ?> haystack, String needle) {
for(String straw : haystack.keySet()) {
if(straw.equalsIgnoreCase(needle))
return haystack.get(straw);
}
return null;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment