diff --git a/README.md b/README.md index 33ccbea7..c3dedd88 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ -SeverUtilities +ServerUtilities =============== -ServerUtilities for server owners. Fork and Rebrand of FTBUtilities, FTBLibrary, and LatCore 1.7.10. +ServerUtilities for server owners. Backport and Rebrand of the 1.12.2 version of FTBUtilities, FTBLibrary, and Aurora for 1.7.10. +Fully backwards compatible with homes, chunks and warps. These will be loaded from FTBU on first load. + +Utilizes a permission system to handle whether a player can use certain commands and do certain things. +A full list of permissions along with their description can be dumped using the command /dump_permissions, +the file will be placed at .minecraft/serverutilities/server/. +They can also be viewed using the Aurora webapi. ### License @@ -18,7 +24,10 @@ Original code Copyright (c) 2016 LatvianModder and licensed MIT (Based on LatMod ![Lat MIT License](lat-mit-license.png) ![Lat FTBU License](lat-ftbu-license-discord.png) -* [FTB Utilities wayback machine license](https://web.archive.org/web/20190418033327/https://minecraft.curseforge.com/projects/ftb-utilities) +* [FTB Utilities wayback machine license](https://web.archive.org/web/20190624234434/https://minecraft.curseforge.com/projects/ftb-utilities) ![FTB Utilities License CF Wayback](ftbu-license-wayback.png) * [FTB Library wayback machine license](https://web.archive.org/web/20190418011645/https://minecraft.curseforge.com/projects/ftblib) ![FTB Library License CF Wayback](ftbl-license-wayback.png) + +* Aurora License +![image](https://github.com/GTNewHorizons/ServerUtilities/assets/3237986/eecd36d3-6b77-4286-9767-3f4aa2dd008f) diff --git a/build.gradle b/build.gradle index b894d647..e59189c8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,4 @@ -//version: 1692122114 +//version: 1697697256 /* DO NOT CHANGE THIS FILE! Also, you may replace this file at any time if there is an update available. @@ -89,6 +89,23 @@ def out = services.get(StyledTextOutputFactory).create('an-output') def projectJavaVersion = JavaLanguageVersion.of(8) boolean disableSpotless = project.hasProperty("disableSpotless") ? project.disableSpotless.toBoolean() : false +boolean disableCheckstyle = project.hasProperty("disableCheckstyle") ? project.disableCheckstyle.toBoolean() : false + +final String CHECKSTYLE_CONFIG = """ + + + + + + + + + + + +""" checkPropertyExists("modName") checkPropertyExists("modId") @@ -140,6 +157,17 @@ if (!disableSpotless) { apply from: Blowdryer.file('spotless.gradle') } +if (!disableCheckstyle) { + apply plugin: 'checkstyle' + tasks.named("checkstylePatchedMc") { enabled = false } + tasks.named("checkstyleMcLauncher") { enabled = false } + tasks.named("checkstyleIdeVirtualMain") { enabled = false } + tasks.named("checkstyleInjectedTags") { enabled = false } + checkstyle { + config = resources.text.fromString(CHECKSTYLE_CONFIG) + } +} + String javaSourceDir = "src/main/java/" String scalaSourceDir = "src/main/scala/" String kotlinSourceDir = "src/main/kotlin/" @@ -600,15 +628,10 @@ repositories { } maven { name = "ic2" - url = "https://maven.ic2.player.to/" - metadataSources { - mavenPom() - artifact() + url = getURL("https://maven.ic2.player.to/", "https://maven2.ic2.player.to/") + content { + includeGroup "net.industrial-craft" } - } - maven { - name = "ic2-mirror" - url = "https://maven2.ic2.player.to/" metadataSources { mavenPom() artifact() @@ -770,23 +793,14 @@ ext.java17PatchDependenciesCfg = configurations.create("java17PatchDependencies" } dependencies { - def lwjgl3ifyVersion = '1.4.0' - def asmVersion = '9.4' + def lwjgl3ifyVersion = '1.5.1' if (modId != 'lwjgl3ify') { java17Dependencies("com.github.GTNewHorizons:lwjgl3ify:${lwjgl3ifyVersion}") } if (modId != 'hodgepodge') { - java17Dependencies('com.github.GTNewHorizons:Hodgepodge:2.2.26') + java17Dependencies('com.github.GTNewHorizons:Hodgepodge:2.3.17') } - java17PatchDependencies('net.minecraft:launchwrapper:1.17.2') {transitive = false} - java17PatchDependencies("org.ow2.asm:asm:${asmVersion}") - java17PatchDependencies("org.ow2.asm:asm-commons:${asmVersion}") - java17PatchDependencies("org.ow2.asm:asm-tree:${asmVersion}") - java17PatchDependencies("org.ow2.asm:asm-analysis:${asmVersion}") - java17PatchDependencies("org.ow2.asm:asm-util:${asmVersion}") - java17PatchDependencies('org.ow2.asm:asm-deprecated:7.1') - java17PatchDependencies("org.apache.commons:commons-lang3:3.12.0") java17PatchDependencies("com.github.GTNewHorizons:lwjgl3ify:${lwjgl3ifyVersion}:forgePatches") {transitive = false} } @@ -1576,6 +1590,25 @@ def getSecondaryArtifacts() { return secondaryArtifacts } +def getURL(String main, String fallback) { + return pingURL(main, 10000) ? main : fallback +} + +// credit: https://stackoverflow.com/a/3584332 +def pingURL(String url, int timeout) { + url = url.replaceFirst("^https", "http") // Otherwise an exception may be thrown on invalid SSL certificates. + try { + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection() + connection.setConnectTimeout(timeout) + connection.setReadTimeout(timeout) + connection.setRequestMethod("HEAD") + int responseCode = connection.getResponseCode() + return 200 <= responseCode && responseCode <= 399 + } catch (IOException ignored) { + return false + } +} + // For easier scripting of things that require variables defined earlier in the buildscript if (file('addon.late.gradle.kts').exists()) { apply from: 'addon.late.gradle.kts' diff --git a/dependencies.gradle b/dependencies.gradle index bb71379a..d6192fc7 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,5 +1,10 @@ // Add your dependencies here dependencies { - api("com.github.GTNewHorizons:waila:1.6.0:dev") + + compileOnly("com.github.GTNewHorizons:NotEnoughItems:2.3.81-GTNH:dev") + + runtimeOnlyNonPublishable("com.github.GTNewHorizons:waila:1.6.2:dev") + + shadowCompile("it.unimi.dsi:fastutil:8.5.12") } diff --git a/gradle.properties b/gradle.properties index 021ef6d4..7f0fb492 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,9 +1,9 @@ modName = ServerUtilities # This is a case-sensitive string to identify your mod. Convention is to use lower case. -modId = ServerUtilities +modId = serverutilities -modGroup = serverutils.utils +modGroup = serverutils # WHY is there no version field? # The build script relies on git to provide a version via tags. It is super easy and will enable you to always know the @@ -21,12 +21,16 @@ forgeVersion = 10.13.4.1614 # Do you need to test how your custom blocks interacts with a player that is not the owner? -> leave name empty developmentEnvironmentUserName = Developer +# Enables using modern java syntax (up to version 17) via Jabel, while still targetting JVM 8. +# See https://github.com/bsideup/jabel for details on how this works. +enableModernJavaSyntax = true + # Define a source file of your project with: # public static final String VERSION = "GRADLETOKEN_VERSION"; # The string's content will be replaced with your mod's version when compiled. You should use this to specify your mod's # version in @Mod([...], version = VERSION, [...]) # Leave these properties empty to skip individual token replacements -replaceGradleTokenInFile = ServerUtilitiesFinals.java +replaceGradleTokenInFile = ServerUtilities.java gradleTokenModId = gradleTokenModName = gradleTokenVersion = GRADLETOKEN_VERSION @@ -39,7 +43,7 @@ apiPackage = # Specify the configuration file for Forge's access transformers here. It must be placed into /src/main/resources/META-INF/ # Example value: mymodid_at.cfg -accessTransformersFile = +accessTransformersFile = serverutil_at.cfg # Provides setup for Mixins if enabled. If you don't know what mixins are: Keep it disabled! usesMixins = false @@ -57,7 +61,7 @@ containsMixinsAndOrCoreModOnly = false # If enabled, you may use 'shadowCompile' for dependencies. They will be integrated in your jar. It is your # responsibility check the licence and request permission for distribution, if required. -usesShadowedDependencies = false +usesShadowedDependencies = true # Optional parameter to customize the produced artifacts. Use this to preserver artifact naming when migrating older # projects. New projects should not use this parameter. diff --git a/src/main/java/latmod/lib/Bits.java b/src/main/java/latmod/lib/Bits.java deleted file mode 100644 index f60b210a..00000000 --- a/src/main/java/latmod/lib/Bits.java +++ /dev/null @@ -1,143 +0,0 @@ -package latmod.lib; - -import java.util.UUID; - -/** - * Made by LatvianModder - */ -public class Bits { - - private static final int MAX_BYTE = 0xFF; - private static final int MAX_SHORT = 0xFFFF; - private static final long MAX_INT = 0xFFFFFFFFL; - - public static int toInt(boolean[] b) { - int d = 0; - for (int i = 0; i < b.length; i++) d |= (b[i] ? 1 : 0) << i; - return d; - } - - public static void toBool(boolean[] b, int d) { - for (int j = 0; j < b.length; j++) b[j] = ((d >> j) & 1) == 1; - } - - public static boolean getBit(byte bits, byte i) { - return ((bits >> i) & 1) == 1; - } - - public static int toBit(boolean b, byte i) { - return (b ? 1 : 0) << i; - } - - public static byte setBit(byte bits, byte i, boolean v) { - if (v) return (byte) ((bits & 0xFF) | (1 << i)); - else return (byte) ((bits & 0xFF) & (not(1 << i) & 0xFF)); - } - - public static int not(int bits) { - return (~bits) & 0xFF; - } - - // - - // Int - public static long intsToLong(int a, int b) { - return (((long) a) << 32) | (b & MAX_INT); - } - - public static int intFromLongA(long l) { - return (int) (l >> 32); - } - - public static int intFromLongB(long l) { - return (int) l; - } - - // Short - public static int shortsToInt(int a, int b) { - return ((short) a << 16) | ((short) b & MAX_SHORT); - } - - public static short shortFromIntA(int i) { - return (short) (i >> 16); - } - - public static short shortFromIntB(int i) { - return (short) (i & MAX_SHORT); - } - - // Byte - public static short bytesToShort(int a, int b) { - return (short) (((a & MAX_BYTE) << 8) | (b & MAX_BYTE)); - } - - public static byte byteFromShortA(short s) { - return (byte) ((s >> 8) & MAX_BYTE); - } - - public static byte byteFromShortB(short s) { - return (byte) (s & MAX_BYTE); - } - - // - // - - public static int toUShort(byte[] b, int off) { - int ch1 = b[off] & MAX_BYTE; - int ch2 = b[off + 1] & MAX_BYTE; - return (ch1 << 8) + ch2; - } - - public static int toInt(byte[] b, int off) { - int ch1 = b[off] & MAX_BYTE; - int ch2 = b[off + 1] & MAX_BYTE; - int ch3 = b[off + 2] & MAX_BYTE; - int ch4 = b[off + 3] & MAX_BYTE; - return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4; - } - - public static long toLong(byte[] b, int off) { - return (((long) b[off] << 56) + ((long) (b[off + 1] & MAX_BYTE) << 48) - + ((long) (b[off + 2] & MAX_BYTE) << 40) - + ((long) (b[off + 3] & MAX_BYTE) << 32) - + ((long) (b[off + 4] & MAX_BYTE) << 24) - + ((b[off + 5] & MAX_BYTE) << 16) - + ((b[off + 6] & MAX_BYTE) << 8) - + ((b[off + 7] & MAX_BYTE))); - } - - public static UUID toUUID(byte[] b, int off) { - long msb = toLong(b, off); - long lsb = toLong(b, off + 8); - return new UUID(msb, lsb); - } - - // - // - - public static void fromUShort(byte[] b, int off, int v) { - b[off] = (byte) (v >>> 8); - b[off + 1] = (byte) v; - } - - public static void fromInt(byte[] b, int off, int v) { - b[off] = (byte) (v >>> 24); - b[off + 1] = (byte) (v >>> 16); - b[off + 2] = (byte) (v >>> 8); - b[off + 3] = (byte) v; - } - - public static void fromLong(byte[] b, int off, long v) { - b[off] = (byte) (v >>> 56); - b[off + 1] = (byte) (v >>> 48); - b[off + 2] = (byte) (v >>> 40); - b[off + 3] = (byte) (v >>> 32); - b[off + 4] = (byte) (v >>> 24); - b[off + 5] = (byte) (v >>> 16); - b[off + 6] = (byte) (v >>> 8); - b[off + 7] = (byte) v; - } - - public static void fromUUID(byte[] b, int off, UUID uuid) { - fromLong(b, off, uuid.getMostSignificantBits()); - fromLong(b, off + 8, uuid.getLeastSignificantBits()); - } -} diff --git a/src/main/java/latmod/lib/ByteCompressor.java b/src/main/java/latmod/lib/ByteCompressor.java deleted file mode 100644 index aa0e7e83..00000000 --- a/src/main/java/latmod/lib/ByteCompressor.java +++ /dev/null @@ -1,43 +0,0 @@ -package latmod.lib; - -import java.io.ByteArrayOutputStream; -import java.util.zip.Deflater; -import java.util.zip.Inflater; - -public class ByteCompressor { - - public static byte[] compress(byte[] data, int off, int len) throws Exception { - Deflater d = new Deflater(); - d.setInput(data, off, len); - ByteArrayOutputStream os = new ByteArrayOutputStream(len); - d.finish(); - - byte[] buffer = new byte[1024]; - while (!d.finished()) { - int count = d.deflate(buffer); - os.write(buffer, 0, count); - } - - os.close(); - byte[] output = os.toByteArray(); - d.end(); - return output; - } - - public static byte[] decompress(byte[] data, int off, int len) throws Exception { - Inflater i = new Inflater(); - i.setInput(data, off, len); - - ByteArrayOutputStream os = new ByteArrayOutputStream(len); - byte[] buffer = new byte[1024]; - while (!i.finished()) { - int count = i.inflate(buffer); - os.write(buffer, 0, count); - } - - os.close(); - byte[] output = os.toByteArray(); - i.end(); - return output; - } -} diff --git a/src/main/java/latmod/lib/ByteCount.java b/src/main/java/latmod/lib/ByteCount.java deleted file mode 100644 index b35db212..00000000 --- a/src/main/java/latmod/lib/ByteCount.java +++ /dev/null @@ -1,33 +0,0 @@ -package latmod.lib; - -public enum ByteCount { - - BYTE(1), - SHORT(2), - INT(4); - - public final int bytes; - - ByteCount(int i) { - bytes = i; - } - - public void write(ByteIOStream io, int num) { - if (this == BYTE) io.writeByte(num); - else if (this == SHORT) io.writeShort(num); - else io.writeInt(num); - } - - public int read(ByteIOStream io) { - if (this == BYTE) { - byte b = io.readByte(); - if (b == -1) return -1; - return b & 0xFF; - } else if (this == SHORT) { - short s = io.readShort(); - if (s == -1) return -1; - return s & 0xFFFF; - } - return io.readInt(); - } -} diff --git a/src/main/java/latmod/lib/ByteIOStream.java b/src/main/java/latmod/lib/ByteIOStream.java deleted file mode 100644 index be9a4de6..00000000 --- a/src/main/java/latmod/lib/ByteIOStream.java +++ /dev/null @@ -1,427 +0,0 @@ -package latmod.lib; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UTFDataFormatException; -import java.util.UUID; - -/** - * Made by LatvianModder - */ -public final class ByteIOStream implements DataInput, DataOutput { - - protected byte[] bytes; - protected int pos; - - private static void throwUTFException(String s) { - try { - throw new UTFDataFormatException(s); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public ByteIOStream(int size) { - bytes = new byte[size]; - } - - public ByteIOStream() { - this(0); - } - - public int getDataPos() { - return pos; - } - - public int available() { - return bytes.length - pos; - } - - public byte[] toByteArray() { - if (pos == bytes.length) return bytes; - byte[] b = new byte[pos]; - System.arraycopy(bytes, 0, b, 0, pos); - return b; - } - - public byte[] toCompressedByteArray() { - try { - return ByteCompressor.compress(bytes, 0, pos); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public void expand(int c) { - if (pos + c >= bytes.length) { - byte[] b = new byte[bytes.length + Math.max(c, 16)]; - System.arraycopy(bytes, 0, b, 0, pos); - bytes = b; - } - } - - public void flip() { - pos = 0; - } - - public void setData(byte[] b) { - bytes = b; - flip(); - } - - public void setCompressedData(byte[] b) { - try { - setData(ByteCompressor.decompress(b, 0, b.length)); - } catch (Exception e) { - e.printStackTrace(); - setData(null); - } - } - - public String toString() { - return toString(false); - } - - public String toString(boolean compressed) { - byte[] b = compressed ? toCompressedByteArray() : toByteArray(); - return "[ (" + b.length + ") " + LMStringUtils.stripB(b) + " ]"; - } - - public OutputStream createOutputStream() { - return new OutputStream() { - - public void write(int b) throws IOException { - ByteIOStream.this.write(b); - } - - public void write(byte b[], int off, int len) throws IOException { - ByteIOStream.this.write(b, off, len); - } - }; - } - - public InputStream createInputStream() { - return new InputStream() { - - public int read() throws IOException { - return (available() <= 0) ? -1 : ByteIOStream.this.readUnsignedByte(); - } - - public int read(byte b[], int off, int len) throws IOException { - ByteIOStream.this.readFully(b, off, len); - return len; - } - - public int available() { - return ByteIOStream.this.available(); - } - }; - } - - // Read functions // - - public byte readByte() { - byte b = bytes[pos]; - pos++; - return b; - } - - public void readFully(byte[] b, int off, int len) { - if (b == null || len == 0) return; - System.arraycopy(bytes, pos, b, off, len); - pos += len; - } - - public int readUnsignedByte() { - return readByte() & 0xFF; - } - - public void readFully(byte[] b) { - readFully(b, 0, b.length); - } - - public byte[] readByteArray(ByteCount c) { - int s = c.read(this); - if (s == -1) return null; - byte[] b = new byte[s]; - readFully(b); - return b; - } - - public boolean readBoolean() { - return readUnsignedByte() == 1; - } - - public char readChar() { - return (char) readUnsignedShort(); - } - - public String readUTF() { - int l = readUnsignedShort(); - if (l == 65535) return null; - else if (l == 0) return ""; - - char[] utf_chars = new char[l]; - - int c, c2, c3, c1 = 0, cac = 0, pos0 = pos; - - pos += l; - - while (c1 < l) { - c = (int) bytes[c1 + pos0] & 0xFF; - if (c > 127) break; - c1++; - utf_chars[cac++] = (char) c; - } - - while (c1 < l) { - c = (int) bytes[c1 + pos0] & 0xFF; - - switch (c >> 4) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - c1++; - utf_chars[cac++] = (char) c; - break; - case 12: - case 13: - c1 += 2; - if (c1 > l) throwUTFException("malformed input: partial character at end"); - c2 = (int) bytes[c1 + pos0 - 1]; - if ((c2 & 0xC0) != 0x80) throwUTFException("malformed input around byte " + c1); - utf_chars[cac++] = (char) (((c & 0x1F) << 6) | (c2 & 0x3F)); - break; - case 14: - c1 += 3; - if (c1 > l) throwUTFException("malformed input: partial character at end"); - c2 = (int) bytes[c1 + pos0 - 2]; - c3 = (int) bytes[c1 + pos0 - 1]; - if (((c2 & 0xC0) != 0x80) || ((c3 & 0xC0) != 0x80)) - throwUTFException("malformed input around byte " + (c1 - 1)); - utf_chars[cac++] = (char) (((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F))); - break; - default: - throwUTFException("malformed input around byte " + c1); - } - } - - String s = new String(utf_chars, 0, cac); - utf_chars = null; - return s; - } - - @Deprecated - public String readLine() { - return null; - } - - public int readUnsignedShort() { - int v = Bits.toUShort(bytes, pos); - pos += 2; - return v; - } - - public short readShort() { - return (short) readUnsignedShort(); - } - - public int readInt() { - int v = Bits.toInt(bytes, pos); - pos += 4; - return v; - } - - public long readLong() { - long v = Bits.toLong(bytes, pos); - pos += 8; - return v; - } - - public float readFloat() { - return Float.intBitsToFloat(readInt()); - } - - public double readDouble() { - return Double.longBitsToDouble(readLong()); - } - - public UUID readUUID() { - UUID v = Bits.toUUID(bytes, pos); - pos += 16; - return v; - } - - public int[] readIntArray(ByteCount c) { - int len = c.read(this); - if (len == -1) return null; - int[] ai = new int[len]; - for (int i = 0; i < len; i++) ai[i] = readInt(); - return ai; - } - - // Write functions // - - public void writeByte(int i) { - expand(1); - bytes[pos] = (byte) i; - pos++; - } - - public void write(int b) { - writeByte(b); - } - - public void write(byte[] b, int off, int len) { - if (b == null || len == 0) return; - expand(len); - System.arraycopy(b, off, bytes, pos, len); - pos += len; - } - - public void write(byte[] b) { - write(b, 0, b.length); - } - - public void writeByteArray(byte[] b, ByteCount c) { - if (b == null) { - c.write(this, -1); - return; - } - c.write(this, b.length); - write(b); - } - - public void writeBoolean(boolean b) { - writeByte(b ? 1 : 0); - } - - public void writeChar(int c) { - writeShort(c); - } - - public void writeUTF(String s) { - if (s == null) { - writeShort(-1); - return; - } - int sl = s.length(); - if (sl == 0) { - writeShort(0); - return; - } - int l = 0; - int c; - - for (int i = 0; i < sl; i++) { - c = s.charAt(i); - if ((c >= 0x0001) && (c <= 0x007F)) l++; - else if (c > 0x07FF) l += 3; - else l += 2; - } - - if (l >= 65535) throwUTFException("encoded string too long: " + l + " bytes"); - - writeShort(l); - expand(l); - - int i = 0; - for (i = 0; i < sl; i++) { - c = s.charAt(i); - if (!(c >= 0x0001 && c <= 0x007F)) break; - writeByte(c); - } - - for (; i < sl; i++) { - c = s.charAt(i); - if (c >= 0x0001 && c <= 0x007F) writeByte(c); - else if (c > 0x07FF) { - writeByte(0xE0 | ((c >> 12) & 0x0F)); - writeByte(0x80 | ((c >> 6) & 0x3F)); - writeByte(0x80 | ((c) & 0x3F)); - } else { - writeByte(0xC0 | ((c >> 6) & 0x1F)); - writeByte(0x80 | ((c) & 0x3F)); - } - } - } - - public void writeBytes(String s) { - if (s == null || s.isEmpty()) return; - for (int i = 0; i < s.length(); i++) writeByte((byte) s.charAt(i)); - } - - public void writeChars(String s) { - if (s == null || s.isEmpty()) return; - for (int i = 0; i < s.length(); i++) writeChar(s.charAt(i)); - } - - public void writeShort(int s) { - expand(2); - Bits.fromUShort(bytes, pos, s); - pos += 2; - } - - public void writeInt(int i) { - expand(4); - Bits.fromInt(bytes, pos, i); - pos += 4; - } - - public void writeLong(long l) { - expand(8); - Bits.fromLong(bytes, pos, l); - pos += 8; - } - - public void writeFloat(float f) { - writeInt(Float.floatToIntBits(f)); - } - - public void writeDouble(double d) { - writeLong(Double.doubleToLongBits(d)); - } - - public void writeUUID(UUID uuid) { - expand(16); - Bits.fromUUID(bytes, pos, uuid); - pos += 16; - } - - public void writeIntArray(int[] ai, ByteCount c) { - int asize = (ai == null) ? -1 : ai.length; - c.write(this, asize); - for (int i = 0; i < asize; i++) writeInt(ai[i]); - } - - public int skipBytes(int n) { - return 0; - } - - public static int getUTFLength(String data) { - if (data == null) return -1; - else if (data.isEmpty()) return 0; - else { - int len = 0; - char c; - - for (int i = 0; i < data.length(); i++) { - c = data.charAt(i); - if ((c >= 0x0001) && (c <= 0x007F)) len++; - else if (c > 0x07FF) len += 3; - else len += 2; - } - - return len; - } - } -} diff --git a/src/main/java/latmod/lib/Converter.java b/src/main/java/latmod/lib/Converter.java deleted file mode 100644 index 7c3b8f69..00000000 --- a/src/main/java/latmod/lib/Converter.java +++ /dev/null @@ -1,141 +0,0 @@ -package latmod.lib; - -import java.util.Arrays; - -/** - * Made by LatvianModder - */ -public class Converter { - - public static int[] toInts(byte[] b) { - if (b == null) return null; - int ai[] = new int[b.length]; - for (int i = 0; i < ai.length; i++) ai[i] = b[i] & 0xFF; - return ai; - } - - public static int[] toInts(short[] b) { - if (b == null) return null; - int ai[] = new int[b.length]; - for (int i = 0; i < ai.length; i++) ai[i] = b[i]; - return ai; - } - - public static byte[] toBytes(int[] b) { - if (b == null) return null; - byte ai[] = new byte[b.length]; - for (int i = 0; i < ai.length; i++) ai[i] = (byte) b[i]; - return ai; - } - - public static Integer[] fromInts(int[] i) { - if (i == null) return null; - Integer ai[] = new Integer[i.length]; - for (int j = 0; j < ai.length; j++) ai[j] = i[j]; - return ai; - } - - public static int[] toInts(Integer[] i) { - if (i == null) return null; - int ai[] = new int[i.length]; - for (int j = 0; j < ai.length; j++) ai[j] = i[j]; - return ai; - } - - public static Float[] fromFloats(float[] i) { - if (i == null) return null; - Float ai[] = new Float[i.length]; - for (int j = 0; j < ai.length; j++) ai[j] = i[j]; - return ai; - } - - public static float[] toFloats(Float[] i) { - if (i == null) return null; - float ai[] = new float[i.length]; - for (int j = 0; j < ai.length; j++) ai[j] = i[j]; - return ai; - } - - public static Double[] fromDoubles(double[] i) { - if (i == null) return null; - Double ai[] = new Double[i.length]; - for (int j = 0; j < ai.length; j++) ai[j] = i[j]; - return ai; - } - - public static double[] toDoubles(Double[] i) { - if (i == null) return null; - double ai[] = new double[i.length]; - for (int j = 0; j < ai.length; j++) ai[j] = i[j]; - return ai; - } - - public static Byte[] fromBytes(byte[] i) { - if (i == null) return null; - Byte ai[] = new Byte[i.length]; - for (int j = 0; j < ai.length; j++) ai[j] = i[j]; - return ai; - } - - public static byte[] toBytes(Byte[] i) { - if (i == null) return null; - byte ai[] = new byte[i.length]; - for (int j = 0; j < ai.length; j++) ai[j] = i[j]; - return ai; - } - - public static float[] toFloats(double[] i) { - if (i == null) return null; - float ai[] = new float[i.length]; - for (int j = 0; j < ai.length; j++) ai[j] = (float) i[j]; - return ai; - } - - public static double[] toDoubles(float[] i) { - if (i == null) return null; - double ai[] = new double[i.length]; - for (int j = 0; j < ai.length; j++) ai[j] = i[j]; - return ai; - } - - public static void toBools(boolean[] bools, IntList idx, boolean isTrue) { - Arrays.fill(bools, !isTrue); - for (int i = 0; i < idx.size(); i++) bools[idx.get(i)] = isTrue; - } - - public static void fromBools(boolean[] bools, IntList il, boolean isTrue) { - il.clear(); - for (int i = 0; i < bools.length; i++) if (bools[i] == isTrue) il.add(i); - } - - public static boolean canParseInt(String s) { - try { - Integer.parseInt(s); - return true; - } catch (Exception e) {} - return false; - } - - public static boolean canParseDouble(String s) { - try { - Double.parseDouble(s); - return true; - } catch (Exception e) {} - return false; - } - - public static int toInt(String text, int def) { - try { - return Integer.parseInt(text); - } catch (Exception e) {} - return def; - } - - public static int nonNull(Integer i) { - return (i == null) ? 0 : i; - } - - public static double nonNull(Double d) { - return (d == null) ? 0D : d; - } -} diff --git a/src/main/java/latmod/lib/IIDObject.java b/src/main/java/latmod/lib/IIDObject.java deleted file mode 100644 index 8c48e9b1..00000000 --- a/src/main/java/latmod/lib/IIDObject.java +++ /dev/null @@ -1,9 +0,0 @@ -package latmod.lib; - -/** - * Created by LatvianModder on 05.03.2016. - */ -public interface IIDObject { - - String getID(); -} diff --git a/src/main/java/latmod/lib/IntList.java b/src/main/java/latmod/lib/IntList.java deleted file mode 100644 index 0f051aec..00000000 --- a/src/main/java/latmod/lib/IntList.java +++ /dev/null @@ -1,264 +0,0 @@ -package latmod.lib; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import com.google.gson.JsonElement; - -public class IntList implements Iterable { - - private int defVal = -1; - private int array[]; - private int size; - - public IntList(int i) { - array = new int[i]; - } - - public IntList() { - this(0); - } - - public IntList(int[] ai) { - if (ai != null && ai.length > 0) { - size = ai.length; - array = new int[size]; - System.arraycopy(ai, 0, array, 0, size); - } else { - size = 0; - array = new int[0]; - } - } - - public int size() { - return size; - } - - public void clear() { - if (size > 0) { - size = 0; - array = new int[0]; - } - } - - public IntList setDefVal(int value) { - defVal = value; - return this; - } - - public void expand(int s) { - if (size + s > array.length) { - int ai[] = new int[size + Math.max(s, 10)]; - System.arraycopy(array, 0, ai, 0, size); - array = ai; - } - } - - public void add(int value) { - expand(1); - array[size] = value; - size++; - } - - public void addAll(int... values) { - if (values != null && values.length > 0) { - expand(values.length); - System.arraycopy(values, 0, array, size, values.length); - size += values.length; - } - } - - public void addAll(IntList l) { - if (l != null && l.size > 0) { - expand(l.size); - System.arraycopy(l.array, 0, array, size, l.size); - size += l.size; - } - } - - public int get(int index) { - return (index >= 0 && index < size) ? array[index] : defVal; - } - - public int indexOf(int value) { - if (size == 0) return -1; - for (int i = 0; i < size; i++) if (array[i] == value) return i; - return -1; - } - - public boolean contains(int value) { - return indexOf(value) != -1; - } - - public int removeKey(int key) { - if (key < 0 || key >= size) return defVal; - int rem = get(key); - size--; - System.arraycopy(array, key + 1, array, key, size - key); - return rem; - } - - public int removeValue(int value) { - return removeKey(indexOf(value)); - } - - public void set(int i, int value) { - array[i] = value; - } - - public boolean isEmpty() { - return size <= 0; - } - - public int[] toArray() { - return toArray(null); - } - - public int[] toArray(int[] a) { - if (a == null || a.length != size) a = new int[size]; - if (size > 0) System.arraycopy(array, 0, a, 0, size); - return a; - } - - public List toList() { - ArrayList l = new ArrayList<>(); - if (size == 0) return l; - for (int i = 0; i < size; i++) l.add(array[i]); - return l; - } - - public void sort() { - if (size < 2) return; - Arrays.sort(array, 0, size); - } - - public int[] toSortedArray() { - if (size == 0) return new int[0]; - int[] a = toArray(); - Arrays.sort(a); - return a; - } - - public int hashCode() { - int h = 0; - for (int i = 0; i < size; i++) h = h * 31 + array[i]; - return h; - } - - public boolean equals(Object o) { - if (o == null) return false; - else if (o == this) return true; - else { - IntList l = (IntList) o; - if (size != l.size) return false; - for (int i = 0; i < size; i++) { - if (array[i] != l.array[i]) return false; - } - return true; - } - } - - public boolean equalsIgnoreOrder(IntList l) { - if (l == null) return false; - else if (l == this) return true; - else { - if (size != l.size) return false; - - IntList l1 = l.copy(); - - for (int i = 0; i < size; i++) l1.removeValue(array[i]); - - return l1.isEmpty(); - } - } - - public String toString() { - if (size == 0) return "[ ]"; - StringBuilder sb = new StringBuilder(); - sb.append('['); - sb.append(' '); - - for (int i = 0; i < size; i++) { - sb.append(array[i]); - - if (i != size - 1) { - sb.append(','); - sb.append(' '); - } - } - - sb.append(' '); - sb.append(']'); - return sb.toString(); - } - - public Iterator iterator() { - return new IntIterator(array); - } - - public IntList copy() { - IntList l = new IntList(size); - System.arraycopy(array, 0, l.array, 0, size); - l.size = size; - l.defVal = defVal; - return l; - } - - // Value, IsInNewList - public Map getDifferenceMap(IntList newList) { - HashMap map = new HashMap<>(); - - if (isEmpty() && newList.isEmpty()) return map; - - for (int i = 0; i < size; i++) { - if (!newList.contains(array[i])) map.put(array[i], false); - } - - for (int i = 0; i < newList.size; i++) { - if (!contains(newList.array[i])) map.put(array[i], true); - } - - return map; - } - - public static IntList asList(int... values) { - IntList l = new IntList(values.length); - l.addAll(values); - return l; - } - - public void setJson(JsonElement e) { - clear(); - addAll(LMJsonUtils.fromIntArray(e)); - } - - public JsonElement getJson() { - return LMJsonUtils.toIntArray(array); - } - - public static class IntIterator implements Iterator { - - public final int[] values; - public int pos = -1; - - public IntIterator(int[] v) { - values = v; - } - - public boolean hasNext() { - return pos < values.length; - } - - public Integer next() { - return values[++pos]; - } - - public void remove() { - throw new UnsupportedOperationException(); - } - } -} diff --git a/src/main/java/latmod/lib/IntMap.java b/src/main/java/latmod/lib/IntMap.java deleted file mode 100644 index 1a30819f..00000000 --- a/src/main/java/latmod/lib/IntMap.java +++ /dev/null @@ -1,127 +0,0 @@ -package latmod.lib; - -import java.util.HashMap; -import java.util.Map; - -/** - * Created by LatvianModder on 06.03.2016. - */ -public class IntMap { - - public final IntList list; - - public IntMap(int s) { - list = new IntList(s * 2); - } - - public IntMap() { - this(0); - } - - public int size() { - return list.size() / 2; - } - - public String toString() { - if (list.isEmpty()) return "{ }"; - StringBuilder sb = new StringBuilder(); - sb.append('{'); - sb.append(' '); - - for (int i = 0; i < list.size(); i += 2) { - sb.append(list.get(i)); - sb.append('='); - sb.append(list.get(i + 1)); - - if (i != list.size() - 2) { - sb.append(','); - sb.append(' '); - } - } - - sb.append(' '); - sb.append('}'); - return sb.toString(); - } - - public int[] toArray() { - return list.toArray(); - } - - public void fromArray(int[] ai) { - list.clear(); - list.addAll(ai); - } - - private int keyIndex(int key) { - for (int i = 0; i < list.size(); i += 2) { - if (list.get(i) == key) return i; - } - - return -1; - } - - public void put(int key, int value) { - int index = keyIndex(key); - if (index != -1) { - list.set(index + 1, value); - } else { - list.add(key); - list.add(value); - } - } - - public int get(int key) { - return list.get(keyIndex(key) + 1); - } - - public Map toMap() { - HashMap map = new HashMap<>(); - for (int i = 0; i < list.size(); i += 2) { - map.put(list.get(i), list.get(i + 1)); - } - return map; - } - - public static IntMap fromMap(Map map) { - if (map == null) return null; - else if (map.isEmpty()) return new IntMap(); - else { - IntMap m = new IntMap(map.size()); - for (Map.Entry e : map.entrySet()) { - m.put(e.getKey(), e.getValue()); - } - return m; - } - } - - public boolean containsKey(int key) { - return keyIndex(key) != -1; - } - - public void clear() { - list.clear(); - } - - public IntList getKeys() { - IntList list1 = new IntList(list.size() / 2); - if (list.isEmpty()) return list1; - - for (int i = 0; i < list.size(); i += 2) { - list1.add(list.get(i)); - } - - return list1; - } - - public IntList getValues() { - IntList list1 = new IntList(list.size() / 2); - if (list.isEmpty()) return list1; - - for (int i = 0; i < list.size(); i += 2) { - list1.add(list.get(i + 1)); - } - - return list1; - } -} diff --git a/src/main/java/latmod/lib/LMColor.java b/src/main/java/latmod/lib/LMColor.java deleted file mode 100644 index 9ca41e4d..00000000 --- a/src/main/java/latmod/lib/LMColor.java +++ /dev/null @@ -1,262 +0,0 @@ -package latmod.lib; - -/** - * Created by LatvianModder on 08.01.2016. - */ -public abstract class LMColor { - - public static final LMColor WHITE = new ImmutableColor(0xFFFFFFFF); - public static final LMColor BLACK = new ImmutableColor(0xFF000000); - public static final LMColor TRANSPARENT = new ImmutableColor(0x00000000); - - public static class RGB extends LMColor { - - private int red = 255, green = 255, blue = 255; - - public RGB() {} - - public RGB(int r, int g, int b) { - setRGBA(r, g, b, 255); - } - - public void set(LMColor col) { - red = col.red(); - green = col.green(); - blue = col.blue(); - } - - public void setRGBA(int r, int g, int b, int a) { - red = r; - green = g; - blue = b; - } - - public void setHSB(float h, float s, float b) { - setRGBA(0xFF000000 | java.awt.Color.HSBtoRGB(h, s, b)); - } - - public void setRGBA(int col) { - red = LMColorUtils.getRed(col); - green = LMColorUtils.getGreen(col); - blue = LMColorUtils.getBlue(col); - } - - public void setRGBAF(float r, float g, float b, float a) { - setRGBA((int) (r * 255F), (int) (g * 255F), (int) (b * 255F), (int) (a * 255F)); - } - - public int color() { - return LMColorUtils.getRGBA(red(), green(), blue(), alpha()); - } - - public int red() { - return red; - } - - public int green() { - return green; - } - - public int blue() { - return blue; - } - - public int alpha() { - return 255; - } - - public float hue() { - float[] hsb = new float[3]; - java.awt.Color.RGBtoHSB(red, green, blue, hsb); - return hsb[0]; - } - - public float saturation() { - float[] hsb = new float[3]; - java.awt.Color.RGBtoHSB(red, green, blue, hsb); - return hsb[1]; - } - - public float brightness() { - float[] hsb = new float[3]; - java.awt.Color.RGBtoHSB(red, green, blue, hsb); - return hsb[2]; - } - - public RGB copy() { - RGB col = new RGB(); - col.set(this); - return col; - } - } - - public static class RGBA extends RGB { - - private int alpha = 255; - - public RGBA() {} - - public RGBA(int r, int g, int b, int a) { - setRGBA(r, g, b, a); - } - - public void setRGBA(int col) { - super.setRGBA(col); - alpha = LMColorUtils.getAlpha(col); - } - - public void setRGBA(int r, int g, int b, int a) { - super.setRGBA(r, g, b, a); - alpha = a; - } - - public int alpha() { - return alpha; - } - } - - public static class HSB extends LMColor { - - private int color; - private final float[] hsb; - - public HSB(float h, float s, float b) { - hsb = new float[3]; - setHSB(h, s, b); - } - - public HSB() { - this(0F, 1F, 1F); - } - - public void set(LMColor col) { - color = 0xFF000000 | col.color(); - hsb[0] = col.hue(); - hsb[1] = col.saturation(); - hsb[2] = col.brightness(); - } - - public void setRGBA(int rgba) { - setRGBA(LMColorUtils.getRed(rgba), LMColorUtils.getGreen(rgba), LMColorUtils.getBlue(rgba), 255); - } - - public void setRGBA(int r, int g, int b, int a) { - color = LMColorUtils.getRGBA(r, g, b, 255); - java.awt.Color.RGBtoHSB(r, g, b, hsb); - } - - public void setHSB(float h, float s, float b) { - hsb[0] = h % 1F; - hsb[1] = MathHelperLM.clampFloat(s, 0F, 1F); - hsb[2] = MathHelperLM.clampFloat(b, 0F, 1F); - color = 0xFF000000 | java.awt.Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]); - } - - public void addHue(float hue) { - setHSB(hsb[0] + hue, hsb[1], hsb[2]); - } - - public int color() { - return color; - } - - public int red() { - return LMColorUtils.getRed(color); - } - - public int green() { - return LMColorUtils.getGreen(color); - } - - public int blue() { - return LMColorUtils.getBlue(color); - } - - public int alpha() { - return 255; - } - - public float hue() { - return hsb[0]; - } - - public float saturation() { - return hsb[1]; - } - - public float brightness() { - return hsb[2]; - } - - public HSB copy() { - HSB col = new HSB(); - col.set(this); - return col; - } - } - - public abstract void set(LMColor col); - - public abstract void setRGBA(int rgba); - - public abstract void setRGBA(int r, int g, int b, int a); - - public abstract void setHSB(float h, float s, float b); - - public abstract int color(); - - public abstract int red(); - - public abstract int green(); - - public abstract int blue(); - - public abstract int alpha(); - - public abstract float hue(); - - public abstract float saturation(); - - public abstract float brightness(); - - public abstract LMColor copy(); - - public final String toString() { - return LMColorUtils.getHex(color()); - } - - public final int hashCode() { - return color(); - } - - public final boolean equals(Object o) { - return hashCode() == o.hashCode(); - } - - public final LMColor immutable() { - return new ImmutableColor(this); - } - - public static class ImmutableColor extends RGBA { - - public ImmutableColor(LMColor col) { - super.set(col); - } - - public ImmutableColor(int col) { - super.setRGBA(col); - } - - public void set(LMColor col) {} - - public void setRGBA(int rgba) {} - - public void setRGBA(int r, int g, int b, int a) {} - - public void setHSB(float h, float s, float b) {} - - public ImmutableColor copy() { - return new ImmutableColor(this); - } - } -} diff --git a/src/main/java/latmod/lib/LMColorUtils.java b/src/main/java/latmod/lib/LMColorUtils.java deleted file mode 100644 index 4a20eccc..00000000 --- a/src/main/java/latmod/lib/LMColorUtils.java +++ /dev/null @@ -1,122 +0,0 @@ -package latmod.lib; - -import com.google.gson.JsonElement; -import com.google.gson.JsonPrimitive; - -public class LMColorUtils { - - public static final int[] chatFormattingColors = new int[16]; - - public static final int BLACK = 0xFF000000; - public static final int WHITE = 0xFFFFFFFF; - public static final int LIGHT_GRAY = 0xFFAAAAAA; - public static final int DARK_GRAY = 0xFF333333; - public static final int WIDGETS = 0xFF000000; - - static { - for (int i = 0; i < 16; i++) { - int j = (i >> 3 & 1) * 85; - int r = (i >> 2 & 1) * 170 + j; - int g = (i >> 1 & 1) * 170 + j; - int b = (i & 1) * 170 + j; - if (i == 6) r += 85; - chatFormattingColors[i] = getRGBA(r, g, b, 255); - } - } - - public static JsonElement serialize(int col) { - return new JsonPrimitive('#' + Integer.toHexString(col).toUpperCase()); - } - - public static int deserialize(JsonElement e) { - if (e == null || !e.isJsonPrimitive()) return 0xFF000000; - return (int) Long.parseLong(e.getAsString().substring(1), 16); - } - - public static int getRGBA(int r, int g, int b, int a) { - return ((a & 255) << 24) | ((r & 255) << 16) | ((g & 255) << 8) | ((b & 255)); - } - - public static int getRGBAF(float r, float g, float b, float a) { - return getRGBA((int) (r * 255F), (int) (g * 255F), (int) (b * 255F), (int) (a * 255F)); - } - - public static int getRed(int c) { - return (c >> 16) & 255; - } - - public static int getGreen(int c) { - return (c >> 8) & 255; - } - - public static int getBlue(int c) { - return (c) & 255; - } - - public static int getAlpha(int c) { - return (c >> 24) & 255; - } - - public static float getRedF(int c) { - return getRed(c) / 255F; - } - - public static float getGreenF(int c) { - return getGreen(c) / 255F; - } - - public static float getBlueF(int c) { - return getBlue(c) / 255F; - } - - public static float getAlphaF(int c) { - return getAlpha(c) / 255F; - } - - public static String getHex(int c) { - return '#' + Integer.toHexString(getRGBA(c, 255)).substring(2).toUpperCase(); - } - - public static int getRGBA(int c, int a) { - return getRGBA(getRed(c), getGreen(c), getBlue(c), a); - } - - public static int addBrightness(int c, int b) { - int red = MathHelperLM.clampInt(getRed(c) + b, 0, 255); - int green = MathHelperLM.clampInt(getGreen(c) + b, 0, 255); - int blue = MathHelperLM.clampInt(getBlue(c) + b, 0, 255); - return getRGBA(red, green, blue, getAlpha(c)); - } - - public static void addHSB(int pixels[], float h, float s, float b) { - if (pixels == null || pixels.length == 0) return; - float[] hsb = new float[3]; - - for (int i = 0; i < pixels.length; i++) { - java.awt.Color.RGBtoHSB(getRed(pixels[i]), getGreen(pixels[i]), getBlue(pixels[i]), hsb); - hsb[0] += h; - hsb[1] = MathHelperLM.clampFloat(hsb[1] + s, 0F, 1F); - hsb[2] = MathHelperLM.clampFloat(hsb[2] + b, 0F, 1F); - pixels[i] = getRGBA(java.awt.Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]), 255); - } - } - - public static int lerp(int col1, int col2, double m, int alpha) { - m = MathHelperLM.clamp(m, 0F, 1F); - int r = MathHelperLM.lerpInt(getRed(col1), getRed(col2), m); - int g = MathHelperLM.lerpInt(getGreen(col1), getGreen(col2), m); - int b = MathHelperLM.lerpInt(getBlue(col1), getBlue(col2), m); - return getRGBA(r, g, b, alpha); - } - - public static int lerp(int col1, int col2, double m) { - return lerp(col1, col2, m, getAlpha(col1)); - } - - public static int multiply(int col1, int col2, int a) { - float r = MathHelperLM.clampFloat(getRedF(col1) * getRedF(col2), 0F, 1F); - float g = MathHelperLM.clampFloat(getGreenF(col1) * getGreenF(col2), 0F, 1F); - float b = MathHelperLM.clampFloat(getBlueF(col1) * getBlueF(col2), 0F, 1F); - return getRGBA((int) (r * 255F), (int) (g * 255F), (int) (b * 255F), a); - } -} diff --git a/src/main/java/latmod/lib/LMFileUtils.java b/src/main/java/latmod/lib/LMFileUtils.java deleted file mode 100644 index 8f032cf9..00000000 --- a/src/main/java/latmod/lib/LMFileUtils.java +++ /dev/null @@ -1,199 +0,0 @@ -package latmod.lib; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileWriter; -import java.net.URL; -import java.nio.channels.Channels; -import java.nio.channels.FileChannel; -import java.nio.channels.ReadableByteChannel; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; - -public class LMFileUtils { - - public static final File latmodHomeFolder = getFolder(); - - public static final int KB = 1024; - public static final int MB = KB * 1024; - public static final int GB = MB * 1024; - - public static final double KB_D = 1024D; - public static final double MB_D = KB_D * 1024D; - public static final double GB_D = MB_D * 1024D; - - public static final Comparator fileComparator = new Comparator() { - - public int compare(File o1, File o2) { - return o1.getName().compareToIgnoreCase(o2.getName()); - } - }; - - public static final Comparator deepFileComparator = new Comparator() { - - public int compare(File o1, File o2) { - return o1.getAbsolutePath().compareToIgnoreCase(o2.getAbsolutePath()); - } - }; - - private static File getFolder() { - // if(!f.exists()) f.mkdirs(); - return new File(System.getProperty("user.home"), "/LatMod/"); - } - - public static File newFile(File f) { - if (f == null || f.exists()) return f; - - try { - File pf = f.getParentFile(); - if (!pf.exists()) pf.mkdirs(); - f.createNewFile(); - return f; - } catch (Exception e) { - e.printStackTrace(); - } - - return f; - } - - public static void save(File f, List al) throws Exception { - save(f, LMStringUtils.fromStringList(al)); - } - - public static void save(File f, String s) throws Exception { - FileWriter fw = new FileWriter(newFile(f)); - BufferedWriter br = new BufferedWriter(fw); - br.write(s); - br.close(); - fw.close(); - } - - public static List load(File f) throws Exception { - return LMStringUtils.readStringList(new FileInputStream(f)); - } - - public static String loadAsText(File f) throws Exception { - return LMStringUtils.readString(new FileInputStream(f)); - } - - public static boolean downloadFile(String url, File out) { - try { - URL website = new URL(url); - ReadableByteChannel rbc = Channels.newChannel(website.openStream()); - FileOutputStream fos = new FileOutputStream(out); - fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); - fos.close(); - return true; - } catch (Exception e) {} - return false; - } - - public static List listAll(File f) { - ArrayList l = new ArrayList<>(); - addAllFiles(l, f); - return l; - } - - private static void addAllFiles(ArrayList l, File f) { - if (f.isDirectory()) { - File[] fl = f.listFiles(); - - if (fl != null && fl.length > 0) { - for (File aFl : fl) addAllFiles(l, aFl); - } - } else if (f.isFile()) l.add(f); - } - - public static long getSize(File f) { - if (f == null || !f.exists()) return 0L; - else if (f.isFile()) return f.length(); - else if (f.isDirectory()) { - long length = 0L; - File[] f1 = f.listFiles(); - if (f1 != null && f1.length > 0) for (File aF1 : f1) length += getSize(aF1); - return length; - } - return 0L; - } - - public static String getSizeS(double b) { - if (b >= GB_D) { - b /= GB_D; - b = (long) (b * 10D) / 10D; - return b + "GB"; - } else if (b >= MB_D) { - b /= MB_D; - b = (long) (b * 10D) / 10D; - return b + "MB"; - } else if (b >= KB_D) { - b /= KB_D; - b = (long) (b * 10D) / 10D; - return b + "KB"; - } - - return b + "B"; - } - - public static String getSizeS(File f) { - return getSizeS(getSize(f)); - } - - @SuppressWarnings("resource") - public static Exception copyFile(File src, File dst) { - if (src != null && dst != null && src.exists() && !src.equals(dst)) { - if (src.isDirectory() && dst.isDirectory()) { - for (File f : listAll(src)) { - File dst1 = new File( - dst.getAbsolutePath() + File.separatorChar - + (f.getAbsolutePath().replace(src.getAbsolutePath(), ""))); - Exception e = copyFile(f, dst1); - if (e != null) return e; - } - - return null; - } - - dst = newFile(dst); - - FileChannel srcC, dstC; - - try { - srcC = new FileInputStream(src).getChannel(); - dstC = new FileOutputStream(dst).getChannel(); - dstC.transferFrom(srcC, 0L, srcC.size()); - srcC.close(); - dstC.close(); - return null; - } catch (Exception e) { - return e; - } - } - - return null; - } - - public static boolean delete(File f) { - if (f == null || !f.exists()) return false; - if (f.isFile()) return f.delete(); - String[] files = f.list(); - for (String file : files) delete(new File(f, file)); - return f.delete(); - } - - public static File getSourceDirectory(Class c) { - return new File(c.getProtectionDomain().getCodeSource().getLocation().getFile()); - } - - public static String getRawFileName(File f) { - if (f == null || !f.exists()) return null; - else if (f.isDirectory()) return f.getName(); - else if (f.isFile()) { - String s = f.getName(); - return s.substring(0, s.lastIndexOf('.')); - } - return null; - } -} diff --git a/src/main/java/latmod/lib/LMJsonUtils.java b/src/main/java/latmod/lib/LMJsonUtils.java deleted file mode 100644 index e8a6075e..00000000 --- a/src/main/java/latmod/lib/LMJsonUtils.java +++ /dev/null @@ -1,247 +0,0 @@ -package latmod.lib; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.Reader; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonParser; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; - -/** - * Type for Lists: new TypeToken>() {}.getType() - */ -public class LMJsonUtils { - - private static Gson gson = null; - private static Gson gson_pretty = null; - public static JsonDeserializationContext deserializationContext; - public static JsonSerializationContext serializationContext, prettySerializationContext; - - public static Gson getGson(boolean pretty) { - if (gson == null || gson_pretty == null) { - GsonBuilder gb = new GsonBuilder(); - gson = gb.create(); - gb.setPrettyPrinting(); - gson_pretty = gb.create(); - - deserializationContext = new JsonDeserializationContext() { - - @SuppressWarnings("unchecked") - public T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { - return (T) gson.fromJson(json, typeOfT); - } - }; - - serializationContext = new JsonSerializationContext() { - - public JsonElement serialize(Object src) { - return gson.toJsonTree(src); - } - - public JsonElement serialize(Object src, Type typeOfSrc) { - return gson.toJsonTree(src, typeOfSrc); - } - }; - - prettySerializationContext = new JsonSerializationContext() { - - public JsonElement serialize(Object src) { - return gson_pretty.toJsonTree(src); - } - - public JsonElement serialize(Object src, Type typeOfSrc) { - return gson_pretty.toJsonTree(src, typeOfSrc); - } - }; - } - - return pretty ? gson_pretty : gson; - } - - public static String toJson(Gson gson, JsonElement e) { - if (e == null) return null; - return gson.toJson(e); - } - - public static boolean toJson(Gson gson, File f, JsonElement o) { - if (o == null) return false; - - try { - String s = toJson(gson, o); - LMFileUtils.save(f, s); - return true; - } catch (Exception e) { - e.printStackTrace(); - } - - return false; - } - - public static String toJson(JsonElement o) { - return toJson(getGson(false), o); - } - - public static boolean toJson(File f, JsonElement o) { - return toJson(getGson(true), f, o); - } - - public static JsonElement fromJson(String json) { - return (json == null || json.isEmpty()) ? JsonNull.INSTANCE : new JsonParser().parse(json); - } - - public static JsonElement fromJson(Reader json) { - return (json == null) ? JsonNull.INSTANCE : new JsonParser().parse(json); - } - - public static JsonElement fromJson(File json) { - try { - if (json == null || !json.exists()) return JsonNull.INSTANCE; - BufferedReader reader = new BufferedReader(new FileReader(json)); - JsonElement e = fromJson(reader); - reader.close(); - return e; - } catch (Exception ex) {} - return JsonNull.INSTANCE; - } - - public static JsonArray join(JsonArray... a) { - JsonArray a1 = new JsonArray(); - - for (JsonArray anA : a) { - if (anA != null) { - for (int j = 0; j < anA.size(); j++) a1.add(anA.get(j)); - } - } - - return a1; - } - - // -- // - - public static JsonArray toIntArray(int[] ai) { - if (ai == null) return null; - JsonArray a = new JsonArray(); - if (ai.length == 0) return a; - for (int anAi : ai) a.add(new JsonPrimitive(anAi)); - return a; - } - - public static int[] fromIntArray(JsonElement e) { - if (e == null || e.isJsonNull()) return null; - - if (e.isJsonArray()) { - JsonArray a = e.getAsJsonArray(); - int[] ai = new int[a.size()]; - if (ai.length == 0) return ai; - for (int i = 0; i < ai.length; i++) ai[i] = a.get(i).getAsInt(); - return ai; - } - - return new int[] { e.getAsInt() }; - } - - public static JsonArray toNumberArray(Number[] ai) { - if (ai == null) return null; - JsonArray a = new JsonArray(); - if (ai.length == 0) return a; - for (Number anAi : ai) a.add(new JsonPrimitive(anAi)); - return a; - } - - public static Number[] fromNumberArray(JsonElement e) { - if (e == null || e.isJsonNull()) return null; - - if (e.isJsonArray()) { - JsonArray a = e.getAsJsonArray(); - Number[] ai = new Number[a.size()]; - if (ai.length == 0) return ai; - for (int i = 0; i < ai.length; i++) ai[i] = a.get(i).getAsNumber(); - return ai; - } - - return new Number[] { e.getAsNumber() }; - } - - public static JsonArray toStringArray(String[] ai) { - if (ai == null) return null; - JsonArray a = new JsonArray(); - if (ai.length == 0) return a; - for (String anAi : ai) a.add(new JsonPrimitive(anAi)); - return a; - } - - public static String[] fromStringArray(JsonElement e) { - if (e == null || e.isJsonNull() || !e.isJsonArray()) return null; - JsonArray a = e.getAsJsonArray(); - String[] ai = new String[a.size()]; - if (ai.length == 0) return ai; - for (int i = 0; i < ai.length; i++) ai[i] = a.get(i).getAsString(); - return ai; - } - - public static JsonObject join(JsonObject... o) { - JsonObject o1 = new JsonObject(); - - for (JsonObject anO : o) { - if (anO != null) { - for (Map.Entry e : anO.entrySet()) o1.add(e.getKey(), e.getValue()); - } - } - - return o1; - } - - public static void printPretty(JsonElement e) { - System.out.println(toJson(getGson(true), e)); - } - - public static List deserializeText(List text) { - List elements = new ArrayList<>(); - - StringBuilder sb = new StringBuilder(); - int inc = 0; - - for (String s : text) { - s = LMStringUtils.trimAllWhitespace(s); - - System.out.println(s); - - if (s.isEmpty()) { - elements.add(JsonNull.INSTANCE); - } else { - if (inc > 0 || s.startsWith("{")) { - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (c == '{') inc++; - else if (c == '}') inc--; - sb.append(c); - - if (inc == 0) { - System.out.println(":: " + sb.toString()); - elements.add(fromJson(sb.toString())); - sb.setLength(0); - } - } - } else { - elements.add(new JsonPrimitive(s)); - } - } - } - - return elements; - } -} diff --git a/src/main/java/latmod/lib/LMListUtils.java b/src/main/java/latmod/lib/LMListUtils.java deleted file mode 100644 index 7e449cb7..00000000 --- a/src/main/java/latmod/lib/LMListUtils.java +++ /dev/null @@ -1,132 +0,0 @@ -package latmod.lib; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * Created by LatvianModder on 06.01.2016. - */ -public class LMListUtils { - - public static String toString(Collection c) { - String[] s = toStringArray(c); - if (s == null) return null; - if (s.length == 0) return "[ ]"; - StringBuilder sb = new StringBuilder(); - sb.append('['); - sb.append(' '); - - for (int i = 0; i < s.length; i++) { - sb.append(s[i]); - - if (i != s.length - 1) { - sb.append(','); - sb.append(' '); - } - } - - sb.append(' '); - sb.append(']'); - return sb.toString(); - } - - public static String[] toStringArray(Collection c) { - if (c == null) return null; - String[] s = new String[c.size()]; - if (s.length == 0) return s; - int i = -1; - for (Object o : c) s[++i] = String.valueOf(o); - return s; - } - - public static List toStringList(Collection c) { - if (c == null) return null; - List list = new ArrayList<>(c.size()); - if (c.isEmpty()) return list; - for (Object o : c) list.add(String.valueOf(o)); - return list; - } - - public static int[] toHashCodeArray(Collection c) { - if (c == null) return null; - int[] s = new int[c.size()]; - int i = -1; - for (Object o : c) s[++i] = LMUtils.hashCodeOf(o); - return s; - } - - public List flip(List list) { - if (list == null || list.isEmpty()) return list; - int s = list.size(); - ArrayList al1 = new ArrayList<>(s); - for (int i = 0; i < s; i++) al1.add(list.get(s - i - 1)); - return al1; - } - - public static void removeNullValues(List list) { - if (list == null) return; - for (int i = list.size() - 1; i >= 0; i--) if (list.get(i) == null) list.remove(i); - } - - public static void removeAll(List list, IntList l) { - if (list == null) return; - for (int i = 0; i < l.size(); i++) list.remove(l.get(i)); - } - - public static void removeAll(List list, RemoveFilter f) { - if (list == null) return; - if (f == null) list.clear(); - else { - for (int i = list.size() - 1; i >= 0; i--) { - if (f.remove(list.get(i))) list.remove(i); - } - } - } - - public static List sortToNew(Collection c, Comparator comparator) { - if (c == null) return null; - else if (c.isEmpty()) return new ArrayList<>(); - ArrayList list = new ArrayList<>(c.size()); - list.addAll(c); - Collections.sort(list, comparator); - return list; - } - - public static boolean trim(List list, int t) { - if (list != null && list.size() > t) { - while (list.size() > t) { - list.remove(t); - t--; - } - return true; - } - - return false; - } - - public static List clone(Collection c) { - if (c == null) return null; - if (c.isEmpty()) return new ArrayList<>(); - ArrayList list1 = new ArrayList<>(c.size()); - list1.addAll(c); - return list1; - } - - // TODO: Remove me - public static void addAll(Collection c, E[] e) { - if (c != null && e != null && e.length > 0) { - Collections.addAll(c, e); - } - } - - public static boolean containsAny(Collection c, Collection c1) { - for (Object o : c1) { - if (c.contains(o)) return true; - } - - return false; - } -} diff --git a/src/main/java/latmod/lib/LMMapUtils.java b/src/main/java/latmod/lib/LMMapUtils.java deleted file mode 100644 index 1ab7c40e..00000000 --- a/src/main/java/latmod/lib/LMMapUtils.java +++ /dev/null @@ -1,128 +0,0 @@ -package latmod.lib; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -/** - * Created by LatvianModder on 06.01.2016. - */ -public class LMMapUtils { - - public static String toString(Map map) { - if (map == null) return null; - StringBuilder sb = new StringBuilder(); - sb.append('{'); - sb.append(' '); - - int s = map.size(); - - if (s > 0) { - int i = 0; - for (Map.Entry e : map.entrySet()) { - sb.append(e.getKey()); - sb.append(':'); - sb.append(' '); - sb.append(e.getValue()); - - if (i != s - 1) { - sb.append(','); - sb.append(' '); - } - i++; - } - - sb.append(' '); - } - - sb.append('}'); - return sb.toString(); - } - - public static Map toStringMap(Map map) { - if (map == null) return null; - HashMap map1 = new HashMap<>(); - for (Map.Entry e : map.entrySet()) map1.put(String.valueOf(e.getKey()), String.valueOf(e.getValue())); - return map1; - } - - public static Map inverse(Map map) { - if (map == null) return null; - HashMap map1 = new HashMap<>(); - for (Map.Entry e : map.entrySet()) map1.put(e.getValue(), e.getKey()); - return map1; - } - - public static List> sortedEntryList(Map map, Comparator> c) { - if (map == null) return null; - ArrayList> list = new ArrayList<>(); - - if (c == null) c = new Comparator>() { - - public int compare(Map.Entry o1, Map.Entry o2) { - return ((Comparable) o1.getKey()).compareTo(o2.getKey()); - } - }; - - list.addAll(map.entrySet()); - Collections.sort(list, c); - return list; - } - - public static List values(Map map, Comparator> c) { - if (map == null) return null; - ArrayList list = new ArrayList<>(); - for (Map.Entry entry : sortedEntryList(map, c)) list.add(entry.getValue()); - return list; - } - - public static Comparator> byKeyNames(final boolean ignoreCase) { - return new Comparator>() { - - public int compare(Map.Entry o1, Map.Entry o2) { - if (ignoreCase) return String.valueOf(o1.getKey()).compareToIgnoreCase(String.valueOf(o2.getKey())); - else return String.valueOf(o1.getKey()).compareTo(String.valueOf(o2.getKey())); - } - }; - } - - public static Comparator> byKeyNumbers() { - return new Comparator>() { - - public int compare(Map.Entry o1, Map.Entry o2) { - Number n1 = ((Number) o1.getKey()); - Number n2 = ((Number) o1.getKey()); - return Long.compare((n1 == null) ? 0L : n1.longValue(), (n2 == null) ? 0L : n2.longValue()); - } - }; - } - - public static void removeAll(Map map, RemoveFilter> f) { - if (map == null) return; - if (f == null) map.clear(); - else { - List> set = new ArrayList<>(map.entrySet()); - map.clear(); - - for (Map.Entry e : set) { - if (!f.remove(e)) map.put(e.getKey(), e.getValue()); - } - } - } - - public static void sortMap(LinkedHashMap map, Comparator> comparator) { - if (map == null || map.isEmpty()) return; - List> list = new ArrayList<>(); - list.addAll(map.entrySet()); - Collections.sort(list, comparator); - map.clear(); - - for (Map.Entry e : list) { - map.put(e.getKey(), e.getValue()); - } - } -} diff --git a/src/main/java/latmod/lib/LMStringUtils.java b/src/main/java/latmod/lib/LMStringUtils.java deleted file mode 100644 index cd6e553a..00000000 --- a/src/main/java/latmod/lib/LMStringUtils.java +++ /dev/null @@ -1,383 +0,0 @@ -package latmod.lib; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; - -public class LMStringUtils { - - public static final int DAY24 = 24 * 60 * 60; - public static final Charset UTF_8 = Charset.forName("UTF-8"); - - public static final String STRIP_SEP = ", "; - public static final String ALLOWED_TEXT_CHARS = " -_!@#$%^&*()+=\\/,.<>?\'\"[]{}|;:`~"; - - public static final Comparator ignoreCaseComparator = new Comparator() { - - public int compare(Object o1, Object o2) { - return String.valueOf(o1).compareToIgnoreCase(String.valueOf(o2)); - } - }; - - public static boolean isValid(String s) { - return s != null && s.length() > 0; - } - - public static String[] shiftArray(String[] s) { - if (s == null || s.length == 0) return new String[0]; - String[] s1 = new String[s.length - 1]; - System.arraycopy(s, 1, s1, 0, s1.length); - return s1; - } - - public static String readString(InputStream is) throws Exception { - final char[] buffer = new char[0x10000]; - final StringBuilder out = new StringBuilder(); - try (Reader in = new InputStreamReader(is, "UTF-8")) { - int read; - do { - read = in.read(buffer, 0, buffer.length); - if (read > 0) { - out.append(buffer, 0, read); - } - } while (read >= 0); - } - return out.toString(); - } - - public static List readStringList(InputStream is) throws Exception { - ArrayList l = new ArrayList<>(); - BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); - String s; - while ((s = reader.readLine()) != null) l.add(s); - reader.close(); - return l; - } - - public static List toStringList(String s, String regex) { - ArrayList al = new ArrayList<>(); - String[] s1 = s.split(regex); - if (s1.length > 0) for (String aS1 : s1) al.add(aS1.trim()); - return al; - } - - public static String fromStringList(List l) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < l.size(); i++) { - sb.append(l.get(i)); - if (i != l.size() - 1) sb.append('\n'); - } - return sb.toString(); - } - - public static boolean isASCIIChar(char c) { - return c > 0 && c < 256; - } - - public static boolean isTextChar(char c, boolean onlyAZ09) { - if (!isASCIIChar(c)) return false; - if (c >= '0' && c <= '9') return true; - if (c >= 'a' && c <= 'z') return true; - return c >= 'A' && c <= 'Z' || !onlyAZ09 && (ALLOWED_TEXT_CHARS.indexOf(c) != -1); - } - - public static void replace(List txt, String s, String s1) { - if (txt == null || txt.isEmpty() || s == null || s1 == null) return; - String s2; - for (int i = 0; i < txt.size(); i++) { - s2 = txt.get(i); - if (s2 != null && s2.length() > 0) { - s2 = s2.replace(s, s1); - txt.set(i, s2); - } - } - } - - public static String replace(String s, char c, char with) { - if (s == null || s.isEmpty()) return s; - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < s.length(); i++) { - char c1 = s.charAt(i); - sb.append((c1 == c) ? with : c1); - } - return sb.toString(); - } - - public static String[] toStrings(E[] o) { - if (o == null) return null; - String[] s = new String[o.length]; - for (int i = 0; i < o.length; i++) s[i] = String.valueOf(o[i]); - return s; - } - - public static String strip(String... o) { - if (o == null) return null; - if (o.length == 0) return ""; - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < o.length; i++) { - sb.append(o[i]); - if (i != o.length - 1) sb.append(STRIP_SEP); - } - - return sb.toString(); - } - - public static String strip(Collection c) { - if (c == null) return null; - if (c.isEmpty()) return ""; - StringBuilder sb = new StringBuilder(); - - int idx = 0; - int eidx = c.size() - 1; - for (Object o : c) { - sb.append(o); - if (idx != eidx) sb.append(STRIP_SEP); - idx++; - } - - return sb.toString(); - } - - public static String stripD(double... o) { - if (o == null) return null; - if (o.length == 0) return ""; - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < o.length; i++) { - sb.append(MathHelperLM.toSmallDouble(o[i])); - if (i != o.length - 1) sb.append(STRIP_SEP); - } - - return sb.toString(); - } - - public static String stripDI(double... o) { - if (o == null) return null; - if (o.length == 0) return ""; - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < o.length; i++) { - sb.append((long) o[i]); - if (i != o.length - 1) sb.append(STRIP_SEP); - } - - return sb.toString(); - } - - public static String stripI(int... o) { - if (o == null) return null; - if (o.length == 0) return ""; - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < o.length; i++) { - sb.append(o[i]); - if (i != o.length - 1) sb.append(STRIP_SEP); - } - - return sb.toString(); - } - - public static String stripB(boolean... o) { - if (o == null) return null; - if (o.length == 0) return ""; - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < o.length; i++) { - sb.append(o[i] ? '1' : '0'); - if (i != o.length - 1) sb.append(STRIP_SEP); - } - - return sb.toString(); - } - - public static String stripB(byte... o) { - if (o == null) return null; - if (o.length == 0) return ""; - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < o.length; i++) { - sb.append(o[i]); - if (i != o.length - 1) sb.append(STRIP_SEP); - } - - return sb.toString(); - } - - public static String unsplit(String[] s, String s1) { - if (s == null) return null; - StringBuilder sb = new StringBuilder(); - if (s.length == 1) return s[0]; - for (int i = 0; i < s.length; i++) { - sb.append(s[i]); - if (i != s.length - 1) sb.append(s1); - } - return sb.toString(); - } - - public static String unsplit(Object[] o, String s1) { - if (o == null) return null; - if (o.length == 1) return String.valueOf(o[0]); - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < o.length; i++) { - sb.append(o[i]); - if (i != o.length - 1) sb.append(s1); - } - return sb.toString(); - } - - public static String unsplitSpaceUntilEnd(int startIndex, String[] o) { - if (o == null || startIndex < 0 || o.length <= startIndex) return null; - StringBuilder sb = new StringBuilder(); - - for (int i = startIndex; i < o.length; i++) { - sb.append(o[i]); - if (i != o.length - 1) sb.append(' '); - } - - return sb.toString(); - } - - public static String firstUppercase(String s) { - if (s == null || s.length() == 0) return s; - char c = Character.toUpperCase(s.charAt(0)); - if (s.length() == 1) return Character.toString(c); - return String.valueOf(c) + s.substring(1); - } - - public static boolean areStringsEqual(String s0, String s1) { - if (s0 == null && s1 == null) return true; - if (s0 == null || s1 == null) return false; - if (s0.length() != s1.length()) return false; - return s0.isEmpty() && s1.isEmpty() || s0.equals(s1); - } - - public static String fillString(String s, char fill, int length) { - int sl = s.length(); - - char[] c = new char[Math.max(sl, length)]; - - for (int i = 0; i < c.length; i++) { - if (i >= sl) c[i] = fill; - else c[i] = s.charAt(i); - } - - return new String(c); - } - - public static boolean contains(String[] s, String s1) { - if (s == null || s1 == null || s.length == 0) return false; - for (String value : s) if (value != null && (value == s1 || value.equals(s1))) return true; - return false; - } - - public static String substring(String s, String pre, String post) { - int preI = s.indexOf(pre); - int postI = s.lastIndexOf(post); - return s.substring(preI + 1, postI); - } - - public static String substring(String s, char pre, char post) { - int preI = s.indexOf(pre); - int postI = s.lastIndexOf(post); - return s.substring(preI + 1, postI); - } - - public static String removeAllWhitespace(String s) { - if (s == null || s.isEmpty()) return s; - else return s.replaceAll("\\s+", ""); - } - - public static String trimAllWhitespace(String s) { - if (s == null || s.isEmpty()) return s; - else return s.replace("^\\s*(.*?)\\s*$", "$1"); - } - - public static String formatInt(int i) { - return formatInt(i, 1); - } - - public static String formatInt(int i, int z) { - String s0 = Integer.toString(i); - if (z <= 0) return s0; - z += 1; - - StringBuilder sb = new StringBuilder(); - - int l = z - s0.length(); - for (int j = 0; j < l; j++) sb.append('0'); - - sb.append(i); - return sb.toString(); - } - - public static String formatDouble(double d) { - if (d == Double.POSITIVE_INFINITY) return "+Inf"; - else if (d == Double.NEGATIVE_INFINITY) return "-Inf"; - else if (d == Double.NaN) return "NaN"; - - d = ((long) (d * 1000D)) / 1000D; - String s = String.valueOf(d); - if (s.endsWith(".0")) return s.substring(0, s.length() - 2); - return s; - } - - public static String getTimeString(long millis) { - return getTimeString(millis, true); - } - - public static String getTimeString(long millis, boolean days) { - long secs = millis / 1000L; - StringBuilder sb = new StringBuilder(); - - long h = (secs / 3600L) % 24; - long m = (secs / 60L) % 60L; - long s = secs % 60L; - - if (days && secs >= DAY24) { - sb.append(secs / DAY24); - // sb.append("d "); - sb.append(':'); - } - - if (h < 10) sb.append('0'); - sb.append(h); - // sb.append("h "); - sb.append(':'); - if (m < 10) sb.append('0'); - sb.append(m); - // sb.append("m "); - sb.append(':'); - if (s < 10) sb.append('0'); - sb.append(s); - // sb.append('s'); - - return sb.toString(); - } - - public static byte[] toBytes(String s) { - if (s == null) return null; - else if (s.length() == 0) return new byte[0]; - else { - byte[] b = new byte[s.length()]; - for (int i = 0; i < b.length; i++) b[i] = (byte) s.charAt(i); - return b; - } - } - - public static String fromBytes(byte[] b) { - if (b == null) return null; - else if (b.length == 0) return ""; - else { - char[] c = new char[b.length]; - for (int i = 0; i < b.length; i++) c[i] = (char) (b[i] & 0xFF); - return new String(c); - } - } -} diff --git a/src/main/java/latmod/lib/LMUtils.java b/src/main/java/latmod/lib/LMUtils.java deleted file mode 100644 index 41da393f..00000000 --- a/src/main/java/latmod/lib/LMUtils.java +++ /dev/null @@ -1,202 +0,0 @@ -package latmod.lib; - -import java.io.InputStream; -import java.io.OutputStream; -import java.lang.reflect.Constructor; -import java.net.InetAddress; -import java.net.URI; -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import java.util.UUID; - -/** - * Made by LatvianModder - */ -public class LMUtils { - // Class / Object // - - public static final Comparator packageComparator = new Comparator() { - - public int compare(Package o1, Package o2) { - return o1.getName().compareToIgnoreCase(o2.getName()); - } - }; - - public static final Comparator IDObjectComparator = new Comparator() { - - public int compare(IIDObject o1, IIDObject o2) { - return o1.getID().compareToIgnoreCase(o2.getID()); - } - }; - - @SuppressWarnings("all") - public static E newObject(Class c, Object... o) throws Exception { - if (c == null) return null; - - if (o != null && o.length > 0) { - Class[] params = new Class[o.length]; - for (int i = 0; i < o.length; i++) params[i] = o.getClass(); - - Constructor c1 = c.getConstructor(params); - return (E) c1.newInstance(o); - } - - return (E) c.newInstance(); - } - - public static Package[] getAllPackages() { - Package[] p = Package.getPackages(); - Arrays.sort(p, packageComparator); - return p; - } - - public static String classpath(Class c) { - return (c == null) ? null : c.getName(); - } - - public static List> addSubclasses(Class c, List> al, boolean all) { - if (c == null) return null; - if (al == null) al = new ArrayList<>(); - ArrayList> al1 = new ArrayList<>(); - LMListUtils.addAll(al1, c.getDeclaredClasses()); - if (all && !al1.isEmpty()) for (Class anAl1 : al1) al.addAll(addSubclasses(anAl1, null, true)); - al.addAll(al1); - return al; - } - - public static boolean areObjectsEqual(Object o1, Object o2, boolean allowNulls) { - if (o1 == null && o2 == null) return allowNulls; - return !(o1 == null || o2 == null) && (o1 == o2 || o1.equals(o2)); - } - - public static int hashCodeOf(Object o) { - return o == null ? 0 : o.hashCode(); - } - - public static int hashCode(Object... o) { - if (o == null || o.length == 0) return 0; - if (o.length == 1) return hashCodeOf(o[0]); - int h = 0; - for (Object anO : o) h = h * 31 + hashCodeOf(anO); - return h; - } - - public static long longHashCode(Object... o) { - if (o == null || o.length == 0) return 0; - if (o.length == 1) return hashCodeOf(o[0]); - long h = 0L; - for (Object anO : o) h = h * 31L + hashCodeOf(anO); - return h; - } - - public static void throwException(Exception e) throws Exception { - if (e != null) throw e; - } - - // Net // - - public static String getHostAddress() { - try { - return InetAddress.getLocalHost().getHostAddress(); - } catch (Exception e) {} - return null; - } - - public static String getExternalAddress() { - try { - return LMStringUtils.readString(new URL("http://checkip.amazonaws.com").openStream()); - } catch (Exception e) {} - return null; - } - - // Misc // - - public static boolean openURI(URI uri) throws Exception { - Class oclass = Class.forName("java.awt.Desktop"); - Object object = oclass.getMethod("getDesktop", new Class[0]).invoke(null); - oclass.getMethod("browse", new Class[] { URI.class }).invoke(object, uri); - return true; - } - - public static long millis() { - return System.currentTimeMillis(); - } - - public static void moveBytes(InputStream is, OutputStream os, boolean close) throws Exception { - byte[] buffer = new byte[1024]; - int len; - while ((len = is.read(buffer, 0, buffer.length)) > 0) os.write(buffer, 0, len); - os.flush(); - - if (close) { - is.close(); - os.close(); - } - } - - @SuppressWarnings("all") - public static T[] newArray(int length, Class typeClass) { - return (T[]) new Object[length]; - } - - @SuppressWarnings("all") - public static T[] convertArray(Object[] array, Class typeClass) { - if (array == null) return null; - T[] t = newArray(array.length, typeClass); - System.arraycopy(array, 0, t, 0, t.length); - return t; - } - - public static String getID(Object o) { - if (o == null) return null; - else if (o instanceof IIDObject) return ((IIDObject) o).getID(); - else return o.toString(); - } - - public static T nonNull(T t) { - if (t == null) throw new NullPointerException(); - return t; - } - - public static String fromUUID(UUID id) { - if (id == null) return null; - long msb = id.getMostSignificantBits(); - long lsb = id.getLeastSignificantBits(); - StringBuilder sb = new StringBuilder(32); - digitsUUID(sb, msb >> 32, 8); - digitsUUID(sb, msb >> 16, 4); - digitsUUID(sb, msb, 4); - digitsUUID(sb, lsb >> 48, 4); - digitsUUID(sb, lsb, 12); - return sb.toString(); - } - - private static void digitsUUID(StringBuilder sb, long val, int digits) { - long hi = 1L << (digits * 4); - String s = Long.toHexString(hi | (val & (hi - 1))); - sb.append(s, 1, s.length()); - } - - public static UUID fromString(String s) { - if (s == null || !(s.length() == 32 || s.length() == 36)) return null; - - try { - if (s.indexOf('-') != -1) return UUID.fromString(s); - - int l = s.length(); - StringBuilder sb = new StringBuilder(36); - for (int i = 0; i < l; i++) { - sb.append(s.charAt(i)); - if (i == 7 || i == 11 || i == 15 || i == 19) sb.append('-'); - } - - return UUID.fromString(sb.toString()); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } -} diff --git a/src/main/java/latmod/lib/MathHelperLM.java b/src/main/java/latmod/lib/MathHelperLM.java deleted file mode 100644 index c0128fb1..00000000 --- a/src/main/java/latmod/lib/MathHelperLM.java +++ /dev/null @@ -1,292 +0,0 @@ -package latmod.lib; - -import java.text.DecimalFormat; -import java.util.Random; - -import latmod.lib.util.VecLM; - -/** - * Made by LatvianModder - */ -public class MathHelperLM { - - public static final Random rand = new Random(); - public static final DecimalFormat smallDoubleFormatter = new DecimalFormat("#0.00"); - - public static final double RAD = Math.PI / 180D; - public static final double DEG = 180D / Math.PI; - public static final double TWO_PI = Math.PI * 2D; - public static final double HALF_PI = Math.PI / 2D; - public static final float PI_F = (float) Math.PI; - public static final float TWO_PI_F = (float) TWO_PI; - public static final float HALF_PI_F = (float) HALF_PI; - public static final double SQRT_2 = Math.sqrt(2D); - - public static final float RAD_F = (float) RAD; - public static final float DEG_F = (float) DEG; - - private static final int SIN_TABLE_SIZE = 65536; - private static final int SIN_TABLE_SIZE_1 = SIN_TABLE_SIZE - 1; - private static final double[] SIN_TABLE = new double[SIN_TABLE_SIZE]; - private static final double SIN_SCALE = SIN_TABLE_SIZE / TWO_PI; - private static final double COS_SHIFT = SIN_TABLE_SIZE / 4D; - - static { - double ds = TWO_PI / (double) SIN_TABLE_SIZE; - for (int i = 0; i < SIN_TABLE_SIZE; i++) SIN_TABLE[i] = Math.sin(i * ds); - } - - public static double sin(double d) { - return SIN_TABLE[(int) (d * SIN_SCALE) & SIN_TABLE_SIZE_1]; - } - - public static double cos(double d) { - return SIN_TABLE[(int) (d * SIN_SCALE + COS_SHIFT) & SIN_TABLE_SIZE_1]; - } - - public static double tan(double d) { - return sin(d) / cos(d); - } - - public static double sinFromDeg(double f) { - return sin(f * RAD); - } - - public static double cosFromDeg(double f) { - return cos(f * RAD); - } - - public static double tanFromDeg(double f) { - return tan(f * RAD); - } - - public static double sqrt(double d) { - if (d == 0D) return 0D; - else if (d == 1D) return 1D; - else return Math.sqrt(d); - } - - public static double sqrt2sq(double x, double y) { - return sqrt(sq(x) + sq(y)); - } - - public static double sqrt3sq(double x, double y, double z) { - return sqrt(sq(x) + sq(y) + sq(z)); - } - - public static double sq(double f) { - return f * f; - } - - public static double power(double i, int n) { - if (n == 2) return i * i; - double i1 = 1D; - for (int j = 0; j < n; j++) i1 *= i; - return i1; - } - - public static int power(int i, int n) { - if (n == 2) return i * i; - int i1 = 1; - for (int j = 0; j < n; j++) i1 *= i; - return i1; - } - - public static long power(long i, int n) { - if (n == 2) return i * i; - long i1 = 1L; - for (int j = 0; j < n; j++) i1 *= i; - return i1; - } - - public static double distSq(double x1, double y1, double z1, double x2, double y2, double z2) { - if (x1 == x2 && y1 == y2 && z1 == z2) return 0D; - return (sq(x2 - x1) + sq(y2 - y1) + sq(z2 - z1)); - } - - public static double dist(double x1, double y1, double z1, double x2, double y2, double z2) { - return sqrt(distSq(x1, y1, z1, x2, y2, z2)); - } - - public static double distSq(double x1, double y1, double x2, double y2) { - if (x1 == x2 && y1 == y2) return 0D; - return (sq(x2 - x1) + sq(y2 - y1)); - } - - public static double dist(double x1, double y1, double x2, double y2) { - return sqrt(distSq(x1, y1, x2, y2)); - } - - public static VecLM getLook(VecLM v, double yaw, double pitch, double dist) { - if (v == null) v = new VecLM(); - double f = cos(pitch * RAD); - double x1 = cos(-yaw * RAD + HALF_PI); - double z1 = sin(-yaw * RAD + HALF_PI); - double y1 = sin(pitch * RAD); - v.set(x1 * f * dist, y1 * dist, z1 * f * dist); - return v; - } - - public static int floor(double d) { - int i = (int) d; - return d < (double) i ? i - 1 : i; - } - - public static int ceil(double d) { - int i = (int) d; - return d > (double) i ? i + 1 : i; - } - - public static int chunk(int i) { - return i >> 4; - } - - public static int unchunk(int i) { - return i << 4; - } - - public static int chunk(double d) { - return chunk(floor(d)); - } - - public static int randomInt(Random r, int min, int max) { - if (min == max) return min; - if (min > max) { - int min0 = min; - min = max; - max = min0; - } - return min + r.nextInt(max - min + 1); - } - - public static double randomDouble(Random r, double min, double max) { - if (min == max) return min; - if (min > max) { - double min0 = min; - min = max; - max = min0; - } - return min + r.nextDouble() * (max - min); - } - - public static boolean isRound(double d) { - return Math.round(d) == d; - } - - public static int lerpInt(int i1, int i2, double f) { - return i1 + (int) ((i2 - i1) * f); - } - - public static double lerp(double f1, double f2, double f) { - return f1 + (f2 - f1) * f; - } - - public static double clamp(double n, double min, double max) { - if (n < min) return min; - if (n > max) return max; - return n; - } - - public static int clampInt(int n, int min, int max) { - if (n < min) return min; - if (n > max) return max; - return n; - } - - public static float clampFloat(float n, float min, float max) { - if (n < min) return min; - if (n > max) return max; - return n; - } - - public static double[] clamp(double[] d, double min, double max) { - for (int i = 0; i < d.length; i++) d[i] = clamp(d[i], min, max); - return d; - } - - public static int[] clampInt(int[] i, int min, int max) { - for (int j = 0; j < i.length; j++) i[j] = clampInt(i[j], min, max); - return i; - } - - public static String toSmallDouble(double d) { - return smallDoubleFormatter.format(d); - } - - public static double map(double val, double min1, double max1, double min2, double max2) { - return min2 + (max2 - min2) * ((val - min1) / (max1 - min1)); - } - - public static int mapInt(int val, int min1, int max1, int min2, int max2) { - return min2 + (max2 - min2) * ((val - min1) / (max1 - min1)); - } - - public static VecLM getMidPoint(double x1, double y1, double z1, double x2, double y2, double z2, double p) { - double x = x2 - x1; - double y = y2 - y1; - double z = z2 - z1; - double d = Math.sqrt(x * x + y * y + z * z); - return new VecLM(x1 + (x / d) * (d * p), y1 + (y / d) * (d * p), z1 + (z / d) * (d * p)); - } - - public static VecLM getMidPoint(VecLM v1, VecLM v2, double p) { - return getMidPoint(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z, p); - } - - public static int getRotations(double yaw, int max) { - return floor((yaw * max / 360D) + 0.5D) & (max - 1); - } - - public static int getRotYaw(int rot) { - if (rot == 2) return 180; - else if (rot == 3) return 0; - else if (rot == 4) return 90; - else if (rot == 5) return -90; - return 0; - } - - public static int getRotPitch(int rot) { - if (rot == 0) return 90; - else if (rot == 1) return -90; - return 0; - } - - public static boolean inRange(double d, double min, double max) { - return d >= min && d <= max; - } - - public static int percent(double d, double max) { - return (int) (d / max * 100D); - } - - public static int[] flip(int[] i) { - if (i == null) return null; - int ai[] = new int[i.length]; - for (int j = 0; j < i.length; j++) ai[j] = i[(i.length - 1) - j]; - return ai; - } - - public static int[] getAllInts(int min, int size) { - int[] ai = new int[size]; - for (int i = 0; i < size; i++) ai[i] = min + i; - return ai; - } - - public static double wrap(double i, double n) { - i = i % n; - if (i < 0) i += n; - return i; - } - - public static int wrap(int i, int n) { - i = i % n; - if (i < 0) i += n; - return i; - } - - public static boolean isPow2(int i) { - return i != 0 && (i & i - 1) == 0; - } - - // MathHelper -} diff --git a/src/main/java/latmod/lib/PixelBuffer.java b/src/main/java/latmod/lib/PixelBuffer.java deleted file mode 100644 index 3b430d22..00000000 --- a/src/main/java/latmod/lib/PixelBuffer.java +++ /dev/null @@ -1,98 +0,0 @@ -package latmod.lib; - -import java.awt.image.BufferedImage; -import java.util.Arrays; - -public class PixelBuffer { - - public final int width, height; - public final int[] pixels; - - public PixelBuffer(int w, int h) { - width = w; - height = h; - pixels = new int[w * h]; - } - - public PixelBuffer(BufferedImage img) { - this(img.getWidth(), img.getHeight()); - img.getRGB(0, 0, width, height, pixels, 0, width); - } - - public void setPixels(int[] rgbArray) { - if (rgbArray.length == pixels.length) System.arraycopy(rgbArray, 0, pixels, 0, pixels.length); - } - - public void setRGB(int x, int y, int col) { - pixels[x + y * width] = col; - } - - public int getRGB(int x, int y) { - return pixels[x + y * width]; - } - - public void setRGB(int startX, int startY, int w, int h, int[] rgbArray) { - int off = -1; - for (int y = startY; y < startY + h; y++) - for (int x = startX; x < startX + w; x++) setRGB(x, y, rgbArray[++off]); - } - - public void setRGB(int startX, int startY, PixelBuffer buffer) { - setRGB(startX, startY, buffer.width, buffer.height, buffer.pixels); - } - - public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray) { - if (rgbArray == null || rgbArray.length != w * h) rgbArray = new int[w * h]; - int off = -1; - for (int y = startY; y < startY + h; y++) - for (int x = startX; x < startX + w; x++) rgbArray[++off] = getRGB(x, y); - return rgbArray; - } - - public BufferedImage toImage(int type) { - BufferedImage image = new BufferedImage(width, height, type); - image.setRGB(0, 0, width, height, pixels, 0, width); - return image; - } - - public void fill(int col) { - Arrays.fill(pixels, col); - } - - public void fill(int startX, int startY, int w, int h, int col) { - for (int y = startY; y < startY + h; y++) for (int x = startX; x < startX + w; x++) setRGB(x, y, col); - } - - public boolean equals(Object o) { - if (o == null) return false; - else if (o == this) return true; - if (o instanceof PixelBuffer) { - PixelBuffer b = (PixelBuffer) o; - if (width == b.width && height == b.height) { - for (int i = 0; i < pixels.length; i++) if (pixels[i] != b.pixels[i]) return false; - return true; - } - } - return false; - } - - public int hashCode() { - return LMUtils.hashCode(width, height, pixels); - } - - public PixelBuffer copy() { - PixelBuffer b = new PixelBuffer(width, height); - System.arraycopy(pixels, 0, b.pixels, 0, pixels.length); - return b; - } - - public PixelBuffer getSubimage(int x, int y, int w, int h) { - PixelBuffer b = new PixelBuffer(w, h); - getRGB(x, y, w, h, b.pixels); - return b; - } - - public void addHSB(float h, float s, float b) { - LMColorUtils.addHSB(pixels, h, s, b); - } -} diff --git a/src/main/java/latmod/lib/RemoveFilter.java b/src/main/java/latmod/lib/RemoveFilter.java deleted file mode 100644 index ebda6ed1..00000000 --- a/src/main/java/latmod/lib/RemoveFilter.java +++ /dev/null @@ -1,9 +0,0 @@ -package latmod.lib; - -/** - * Created by LatvianModder on 03.01.2016. - */ -public interface RemoveFilter { - - boolean remove(E e); -} diff --git a/src/main/java/latmod/lib/Time.java b/src/main/java/latmod/lib/Time.java deleted file mode 100644 index 5c061e75..00000000 --- a/src/main/java/latmod/lib/Time.java +++ /dev/null @@ -1,151 +0,0 @@ -package latmod.lib; - -import java.util.Calendar; - -import com.google.gson.JsonElement; -import com.google.gson.JsonPrimitive; - -public final class Time implements Comparable