Merge remote-tracking branch 'origin/master'

This commit is contained in:
Shaun Bennett 2015-12-10 23:15:42 -05:00
commit 4588091c5b
4 changed files with 605 additions and 62 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

@ -0,0 +1,18 @@
package mineplex.core.common.util;
public enum LineFormat
{
LORE(200), CHAT(319);
private int _length;
private LineFormat(int length)
{
_length = length;
}
public int getLength()
{
return _length;
}
}

View File

@ -1,52 +1,452 @@
package mineplex.core.common.util;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import javax.imageio.ImageIO;
import mineplex.core.common.CurrencyType;
import org.apache.commons.lang.WordUtils;
import org.bukkit.Material;
import org.bukkit.ChatColor;
public class UtilText {
public static <T> String listToString(Collection<T> inputList, boolean comma) {
String out = "";
public class UtilText
{
private static HashMap<Character, Integer> _characters = new HashMap<Character, Integer>();
for (T cur : inputList) {
out += cur.toString() + (comma ? ", " : " ");
static
{
try
{
InputStream inputStream = CurrencyType.class.getResourceAsStream("ascii.png");
BufferedImage image = ImageIO.read(inputStream);
char[] text = new char[]
{
' ',
'!',
'"',
'#',
'$',
'%',
'&',
'\'',
'(',
')',
'*',
'+',
',',
'-',
'.',
'/',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
':',
';',
'<',
'=',
'>',
'?',
'@',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'[',
'\\',
']',
'^',
'_',
'`',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'{',
'|',
'}',
'~'
};
int x = 0;
int y = 16;
for (char c : text)
{
grab(c, image, x, y);
if (x < 15 * 8)
{
x += 8;
}
else
{
x = 0;
y += 8;
}
}
if (out.length() > 0) {
out = out.substring(0, out.length() - (comma ? 2 : 1));
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return out;
public static String center(String string, LineFormat lineFormat)
{
int length = getLength(string);
if (length > lineFormat.getLength())
{
return string;
}
public static int upperCaseCount(String input) {
int count = 0;
// Get the number of empty pixels on both sides of the string
int div = (int) Math.floor((lineFormat.getLength() - length) / 2D);
for (int k = 0; k < input.length(); k++) {
char ch = input.charAt(k);
if (Character.isUpperCase(ch))
count++;
div -= 2; // For the gap between the strings
return fillLine(" ", div) + string + fillLine(" ", div);
}
return count;
}
public static int lowerCaseCount(String input) {
int count = 0;
for (int k = 0; k < input.length(); k++) {
char ch = input.charAt(k);
if (Character.isLowerCase(ch))
count++;
public static String alignRight(String string, LineFormat lineFormat)
{
int length = getLength(string);
if (length > lineFormat.getLength())
{
return string;
}
return count;
// Get the number of empty pixels on both sides of the string
int div = lineFormat.getLength() - length;
div -= 1; // For the gap between the strings
return fillLine(" ", div) + string;
}
public static String centerChat(String string, LineFormat lineFormat)
{
int length = getLength(string);
if (length > lineFormat.getLength())
{
return string;
}
// Get the number of empty pixels on both sides of the string
int div = (int) Math.floor(((lineFormat.getLength() + 10) - length) / 2D);
div -= 2; // For the gap between the strings
return fillLine(" ", div) + string;
}
public static String substringPixels(String string, int cutoff)
{
int len = 0;
char[] array = string.toCharArray();
boolean bold = false;
for (int i = 0; i < array.length; i++)
{
char c = array[i];
if (c == '<27>')
{
if (++i < array.length)
{
ChatColor color = ChatColor.getByChar(array[i]);
if (color != null)
{
if (color.equals(ChatColor.BOLD))
{
bold = true;
}
else if (color.equals(ChatColor.RESET) || color.isColor())
{
bold = false;
}
}
}
continue;
}
if (!_characters.containsKey(c))
{
continue;
}
int toAdd = _characters.get(c);
if (bold)
{
toAdd++;
}
if (len + toAdd > cutoff)
{
return string.substring(0, Math.max(0, i - 1));
}
if (i + 1 < array.length)
{
len++;
}
}
return string;
}
public static ArrayList<String> splitLines(String[] strings, LineFormat lineFormat)
{
ArrayList<String> lines = new ArrayList<String>();
for (String s : strings)
{
lines.addAll(splitLine(s, lineFormat));
}
return lines;
}
public static ArrayList<String> splitLine(String string, LineFormat lineFormat)
{
ArrayList<String> strings = new ArrayList<String>();
String current = "";
int currentLength = 0;
String[] split = string.split(" ");
String colors = "";
for (int i = 0; i < split.length; i++)
{
String word = split[i];
int wordLength = getLength(colors + word);
if (currentLength + wordLength + 4 > lineFormat.getLength() && !current.isEmpty())
{
strings.add(current);
current = colors + word;
currentLength = wordLength + 1;
continue;
}
if (i != 0)
{
current += " ";
currentLength += 4;
}
current += word;
currentLength += wordLength;
colors = ChatColor.getLastColors(current);
}
if (!current.isEmpty())
{
strings.add(current);
}
return strings;
}
public static String fillLine(String filler, int maxPixels)
{
int pixels = getLength(filler);
if (pixels <= 0)
{
return "";
}
String toReturn = "";
int currentLen = 0;
int offset = maxPixels % 4;
boolean isOffset = false;
if (offset > 0)
{
toReturn += C.Bold;
}
while (currentLen + pixels <= maxPixels)
{
currentLen += pixels + 1;
toReturn += filler;
if (offset-- > 0)
{
currentLen++;
if (offset == 0)
{
isOffset = false;
toReturn += ChatColor.RESET;
}
}
}
if (isOffset)
{
toReturn += ChatColor.RESET;
}
return toReturn;
}
public static boolean fitsOneLine(String string, LineFormat lineFormat)
{
return getLength(string) <= lineFormat.getLength();
}
public static int getLength(String string)
{
int len = 0;
char[] array = string.toCharArray();
boolean bold = false;
for (int i = 0; i < array.length; i++)
{
char c = array[i];
if (c == '<27>')
{
if (++i < array.length)
{
ChatColor color = ChatColor.getByChar(array[i]);
if (color != null)
{
if (color.equals(ChatColor.BOLD))
{
bold = true;
}
else if (color.equals(ChatColor.RESET) || color.isColor())
{
bold = false;
}
}
}
continue;
}
if (!_characters.containsKey(c))
{
continue;
}
len += _characters.get(c);
if (bold)
{
len++;
}
if (i + 1 < array.length)
{
len++;
}
}
return len;
}
private static void grab(Character character, BufferedImage image, int imageX, int imageY)
{
if (character == ' ')
{
_characters.put(character, 3);
return;
}
for (int x = 0; x < 8; x++)
{
boolean isTransparentLine = true;
for (int y = 0; y < 8; y++)
{
int pixel = image.getRGB(imageX + x, imageY + y);
if ((pixel >> 24) != 0x00)
{
isTransparentLine = false;
break;
}
}
if (isTransparentLine)
{
_characters.put(character, x);
return;
}
}
_characters.put(character, 8);
}
public static boolean isStringSimilar(String newString, String oldString, float matchRequirement)
@ -88,12 +488,62 @@ public class UtilText {
return false;
}
public static <T> String listToString(Collection<T> inputList, boolean comma)
{
String out = "";
for (T cur : inputList)
{
out += cur.toString() + (comma ? ", " : " ");
}
if (out.length() > 0)
{
out = out.substring(0, out.length() - (comma ? 2 : 1));
}
return out;
}
public static int lowerCaseCount(String input)
{
int count = 0;
for (int k = 0; k < input.length(); k++)
{
char ch = input.charAt(k);
if (Character.isLowerCase(ch))
count++;
}
return count;
}
public static int upperCaseCount(String input)
{
int count = 0;
for (int k = 0; k < input.length(); k++)
{
char ch = input.charAt(k);
if (Character.isUpperCase(ch))
count++;
}
return count;
}
public static String[] wrap(String text, int lineLength)
{
return wrap(text, lineLength, true);
}
public static String[] wrap(String text, int lineLength, boolean wrapLongerWords) {
public static String[] wrap(String text, int lineLength, boolean wrapLongerWords)
{
return WordUtils.wrap(text, lineLength, "\00D0", wrapLongerWords).split("\00D0");
}
@ -102,11 +552,13 @@ public class UtilText {
return new String(new byte[times]).replace("\0", txt);
}
public static boolean plural(int x){
public static boolean plural(int x)
{
return x <= 0 ? true : x > 1;
}
public static String trim(int maxLength, String s) {
public static String trim(int maxLength, String s)
{
return s.length() <= maxLength ? s : s.substring(0, maxLength);
}

View File

@ -0,0 +1,73 @@
package mineplex.core.playerCount;
import org.bukkit.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.serverdata.Region;
import mineplex.serverdata.data.BungeeServer;
import mineplex.serverdata.data.DataRepository;
import mineplex.serverdata.redis.RedisDataRepository;
import mineplex.serverdata.servers.ConnectionData;
import mineplex.serverdata.servers.ServerManager;
public class PlayerCountManager extends MiniPlugin
{
private Region _region;
private DataRepository<BungeeServer> _repository;
private DataRepository<BungeeServer> _secondRepository;
private volatile int _playerCount;
public PlayerCountManager(JavaPlugin plugin)
{
super("PlayerCount", plugin);
_region = plugin.getConfig().getBoolean("serverstatus.us") ? Region.US : Region.EU;
_repository = new RedisDataRepository<BungeeServer>(ServerManager.getConnection(true, ServerManager.SERVER_STATUS_LABEL), ServerManager.getConnection(false, ServerManager.SERVER_STATUS_LABEL),
Region.ALL, BungeeServer.class, "bungeeServers");
if (_region == Region.US)
_secondRepository = new RedisDataRepository<BungeeServer>(new ConnectionData("10.81.1.156", 6379, ConnectionData.ConnectionType.MASTER, "ServerStatus"), new ConnectionData("10.81.1.156", 6377, ConnectionData.ConnectionType.SLAVE, "ServerStatus"),
Region.ALL, BungeeServer.class, "bungeeServers");
else
_secondRepository = new RedisDataRepository<BungeeServer>(new ConnectionData("10.33.53.16", 6379, ConnectionData.ConnectionType.MASTER, "ServerStatus"), new ConnectionData("10.33.53.16", 6377, ConnectionData.ConnectionType.SLAVE, "ServerStatus"),
Region.ALL, BungeeServer.class, "bungeeServers");
updatePlayerCount();
}
private void updatePlayerCount()
{
int totalPlayers = 0;
for (BungeeServer server : _repository.getElements())
{
totalPlayers += server.getPlayerCount();
}
for (BungeeServer server : _secondRepository.getElements())
{
totalPlayers += server.getPlayerCount();
}
_playerCount = totalPlayers;
}
public int getPlayerCount()
{
return _playerCount;
}
@EventHandler
public void refresh(UpdateEvent event)
{
if (event.getType() != UpdateType.SEC)
return;
runAsync(this::updatePlayerCount);
}
}