diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilText.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilText.java index 771b1ec52..c5a805558 100644 --- a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilText.java +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilText.java @@ -1,52 +1,469 @@ 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 String listToString(Collection inputList, boolean comma) { - String out = ""; +public class UtilText +{ + private static HashMap _characters = new HashMap(); - for (T cur : inputList) { - out += cur.toString() + (comma ? ", " : " "); + public static enum LineFormat + { + LORE(200), CHAT(319); + + private int _length; + + private LineFormat(int length) + { + _length = length; } - if (out.length() > 0) { - out = out.substring(0, out.length() - (comma ? 2 : 1)); + public int getLength() + { + return _length; } - - return out; } - - 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++; - + + 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; + } + } + + inputStream.close(); + } + catch (IOException e) + { + e.printStackTrace(); } - - 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 center(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 = (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 == '�') + { + 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 splitLines(String[] strings, LineFormat lineFormat) + { + ArrayList lines = new ArrayList(); + + for (String s : strings) + { + lines.addAll(splitLine(s, lineFormat)); + } + + return lines; + } + + public static ArrayList splitLine(String string, LineFormat lineFormat) + { + ArrayList strings = new ArrayList(); + 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 == '�') + { + 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) @@ -55,84 +472,136 @@ public class UtilText { { return newString.toLowerCase().equals(oldString.toLowerCase()); } - - for (int i=0 ; i < newString.length() * matchRequirement ; i++) + + for (int i = 0; i < newString.length() * matchRequirement; i++) { int matchFromIndex = 0; - - //Look for substrings starting at i - for (int j=0 ; j < oldString.length() ; j++) + + // Look for substrings starting at i + for (int j = 0; j < oldString.length(); j++) { - //End of newString - if (i+j >= newString.length()) + // End of newString + if (i + j >= newString.length()) { break; } - - //Matched - if (newString.charAt(i+j) == oldString.charAt(j)) + + // Matched + if (newString.charAt(i + j) == oldString.charAt(j)) { matchFromIndex++; - + if (matchFromIndex >= newString.length() * matchRequirement) return true; } - //No Match > Reset + // No Match > Reset else { break; } } } - + return false; } - + + public static String listToString(Collection 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"); } - + public static String repeat(String txt, int times) { 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); } public static String arrayToString(X[] array, String delimiter) { StringBuilder string = new StringBuilder(); - + int index = 0; for (X x : array) { string.append(x.toString()); - + if (index != array.length - 1) { string.append(delimiter); } - + index++; } - + return string.toString(); } - + public static String arrayToString(X[] array) { return arrayToString(array, null); } -} +} \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NBSReader.java b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NBSReader.java index 1ca887ede..e7844c9d1 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NBSReader.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NBSReader.java @@ -16,10 +16,10 @@ public class NBSReader { 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 { @@ -41,6 +41,9 @@ public class NBSReader int blocksRemoved = readInt(stream); String midiFileName = readString(stream); + if ((name == null || name.length() == 0) && defaultName != null) + name = defaultName; + HashMap layerMap = new HashMap(); // Note Block Information diff --git a/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NotePlayer.java b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NotePlayer.java index 70e5a8c1f..941bc1dd9 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NotePlayer.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NotePlayer.java @@ -1,10 +1,7 @@ package mineplex.core.noteblock; import java.util.ArrayList; -import java.util.Collection; import java.util.List; -import java.util.Objects; -import java.util.stream.Stream; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -43,38 +40,49 @@ public class NotePlayer private void startThread() { - Thread thread = new Thread(() -> { - while (!_finished) + Thread thread = new Thread(new Runnable() + { + @Override + public void run() { - _tick++; - if (_tick > _song.getLength()) + long startTime = System.currentTimeMillis(); + while (!_finished) { - if (_loop) + _tick++; + if (_tick > _song.getLength()) { - _tick = 1; + if (_loop) + { + _tick = 1; + } + else + { + _finished = true; + + _plugin.getServer().getScheduler().runTask(_plugin, new Runnable() + { + @Override + public void run() + { + SongFinishEvent event = new SongFinishEvent(_song); + _plugin.getServer().getPluginManager().callEvent(event); + } + }); + + return; + } } - else + + playTick(_tick); + + try { - _finished = true; - - _plugin.getServer().getScheduler().runTask(_plugin, () -> { - SongFinishEvent event = new SongFinishEvent(_song); - _plugin.getServer().getPluginManager().callEvent(event); - }); - - return; + Thread.sleep(_sleepMs); + } + catch (InterruptedException e) + { + e.printStackTrace(); } - } - - playTick(_tick); - - try - { - Thread.sleep(_sleepMs); - } - catch (InterruptedException e) - { - e.printStackTrace(); } } }); @@ -82,18 +90,28 @@ public class NotePlayer thread.start(); } - private void playNote(Note note, float volume, Collection 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) { - Collection players = UtilServer.getPlayersCollection(); + Player[] playerArray = UtilServer.getPlayers(); + List players = new ArrayList<>(playerArray.length); + for (Player player : playerArray) + { + if (_verifier.shouldPlay(player)) + players.add(player); + } - float volume = _volumeMult * 1F; - _song.getLayers().stream().map(layer -> layer.getNote(tick)).filter(Objects::nonNull).forEach((note) -> playNote(note, volume, players)); + for (NoteLayer layer : _song.getLayers()) + { + 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() diff --git a/Plugins/Mineplex.Core/src/mineplex/core/playerCount/PlayerCountManager.java b/Plugins/Mineplex.Core/src/mineplex/core/playerCount/PlayerCountManager.java new file mode 100644 index 000000000..c73f87d00 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/playerCount/PlayerCountManager.java @@ -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 _repository; + private DataRepository _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(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(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(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); + } + +} diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java b/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java index 78bf6f694..2dd2823cb 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java @@ -261,7 +261,7 @@ public class HubManager extends MiniClientPlugin if (file.getName().endsWith(".nbs")) { 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) { _songs.add(song);