Merge remote-tracking branch 'remotes/origin/master' into virizion_speed_builder

This commit is contained in:
Virizion 2015-12-10 22:18:00 -05:00
commit 14b5a9453a
5 changed files with 665 additions and 102 deletions

View File

@ -1,52 +1,469 @@
package mineplex.core.common.util; 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.Collection;
import java.util.HashMap;
import javax.imageio.ImageIO;
import mineplex.core.common.CurrencyType;
import org.apache.commons.lang.WordUtils; import org.apache.commons.lang.WordUtils;
import org.bukkit.Material; import org.bukkit.ChatColor;
public class UtilText { public class UtilText
public static <T> String listToString(Collection<T> inputList, boolean comma) { {
String out = ""; private static HashMap<Character, Integer> _characters = new HashMap<Character, Integer>();
for (T cur : inputList) { public static enum LineFormat
out += cur.toString() + (comma ? ", " : " "); {
LORE(200), CHAT(319);
private int _length;
private LineFormat(int length)
{
_length = length;
} }
if (out.length() > 0) { public int getLength()
out = out.substring(0, out.length() - (comma ? 2 : 1)); {
return _length;
}
} }
return out; 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;
}
} }
public static int upperCaseCount(String input) { inputStream.close();
int count = 0; }
catch (IOException e)
for (int k = 0; k < input.length(); k++) { {
e.printStackTrace();
}
char ch = input.charAt(k);
if (Character.isUpperCase(ch))
count++;
} }
return count; public static String center(String string, LineFormat lineFormat)
} {
public static int lowerCaseCount(String input) { int length = getLength(string);
int count = 0;
for (int k = 0; k < input.length(); k++) {
char ch = input.charAt(k);
if (Character.isLowerCase(ch))
count++;
if (length > lineFormat.getLength())
{
return string;
} }
return count; // Get the number of empty pixels on both sides of the string
int div = (int) Math.floor((lineFormat.getLength() - length) / 2D);
div -= 2; // For the gap between the strings
return fillLine(" ", div) + string + fillLine(" ", div);
}
public static String alignRight(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 = 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) public static boolean isStringSimilar(String newString, String oldString, float matchRequirement)
@ -88,12 +505,62 @@ public class UtilText {
return false; 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) public static String[] wrap(String text, int lineLength)
{ {
return wrap(text, lineLength, true); 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"); return WordUtils.wrap(text, lineLength, "\00D0", wrapLongerWords).split("\00D0");
} }
@ -102,11 +569,13 @@ public class UtilText {
return new String(new byte[times]).replace("\0", txt); 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; 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); return s.length() <= maxLength ? s : s.substring(0, maxLength);
} }

View File

@ -16,10 +16,10 @@ public class NBSReader
{ {
public static NoteSong loadSong(String fileName) throws FileNotFoundException public static NoteSong loadSong(String fileName) throws FileNotFoundException
{ {
return loadSong(new DataInputStream(new FileInputStream(new File(fileName)))); return loadSong(new DataInputStream(new FileInputStream(new File(fileName))), fileName);
} }
public static NoteSong loadSong(DataInputStream stream) public static NoteSong loadSong(DataInputStream stream, String defaultName)
{ {
try try
{ {
@ -41,6 +41,9 @@ public class NBSReader
int blocksRemoved = readInt(stream); int blocksRemoved = readInt(stream);
String midiFileName = readString(stream); String midiFileName = readString(stream);
if ((name == null || name.length() == 0) && defaultName != null)
name = defaultName;
HashMap<Integer, NoteLayer> layerMap = new HashMap<Integer, NoteLayer>(); HashMap<Integer, NoteLayer> layerMap = new HashMap<Integer, NoteLayer>();
// Note Block Information // Note Block Information

View File

@ -1,10 +1,7 @@
package mineplex.core.noteblock; package mineplex.core.noteblock;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -43,7 +40,12 @@ public class NotePlayer
private void startThread() private void startThread()
{ {
Thread thread = new Thread(() -> { Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
long startTime = System.currentTimeMillis();
while (!_finished) while (!_finished)
{ {
_tick++; _tick++;
@ -57,9 +59,14 @@ public class NotePlayer
{ {
_finished = true; _finished = true;
_plugin.getServer().getScheduler().runTask(_plugin, () -> { _plugin.getServer().getScheduler().runTask(_plugin, new Runnable()
{
@Override
public void run()
{
SongFinishEvent event = new SongFinishEvent(_song); SongFinishEvent event = new SongFinishEvent(_song);
_plugin.getServer().getPluginManager().callEvent(event); _plugin.getServer().getPluginManager().callEvent(event);
}
}); });
return; return;
@ -77,23 +84,34 @@ public class NotePlayer
e.printStackTrace(); e.printStackTrace();
} }
} }
}
}); });
thread.start(); thread.start();
} }
private void playNote(Note note, float volume, Collection<? extends Player> players)
{
players.stream().filter(_verifier::shouldPlay).forEach((player) ->
player.playSound(player.getEyeLocation(), UtilNote.getInstrumentSound(note.getInstrument()), volume, (float) UtilNote.getPitch(note.getNote() - 33)));
}
private void playTick(int tick) private void playTick(int tick)
{ {
Collection<? extends Player> players = UtilServer.getPlayersCollection(); Player[] playerArray = UtilServer.getPlayers();
List<Player> players = new ArrayList<>(playerArray.length);
for (Player player : playerArray)
{
if (_verifier.shouldPlay(player))
players.add(player);
}
float volume = _volumeMult * 1F; for (NoteLayer layer : _song.getLayers())
_song.getLayers().stream().map(layer -> layer.getNote(tick)).filter(Objects::nonNull).forEach((note) -> playNote(note, volume, players)); {
Note note = layer.getNote(tick);
if (note != null)
{
float volume = _volumeMult * (layer.getVolume() / 100F);
for (Player player : players)
{
player.playSound(player.getEyeLocation(), UtilNote.getInstrumentSound(note.getInstrument()), volume, (float) UtilNote.getPitch(note.getNote() - 33));
}
}
}
} }
public void cancel() public void cancel()

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);
}
}

View File

@ -261,7 +261,7 @@ public class HubManager extends MiniClientPlugin<HubClient>
if (file.getName().endsWith(".nbs")) if (file.getName().endsWith(".nbs"))
{ {
System.out.println("Loading Song " + file.getPath()); System.out.println("Loading Song " + file.getPath());
NoteSong song = NBSReader.loadSong(new DataInputStream(new FileInputStream(file))); NoteSong song = NBSReader.loadSong(new DataInputStream(new FileInputStream(file)), file.getName().replace("_", " ").replace(".nbs", ""));
if (song != null) if (song != null)
{ {
_songs.add(song); _songs.add(song);