More kotlin, code improvements

This commit is contained in:
Bixilon 2020-12-13 23:34:54 +01:00
parent 848a274862
commit 963afa0681
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
16 changed files with 68 additions and 95 deletions

View File

@ -1,6 +1,6 @@
variables:
MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"
MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
MAVEN_CLI_OPTS: "--errors --fail-at-end --show-version"
image: maven:3-openjdk-15

View File

@ -1,22 +1,19 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<option name="LINE_SEPARATOR" value="&#10;" />
<option name="RIGHT_MARGIN" value="500" />
<option name="SOFT_MARGINS" value="0" />
<JavaCodeStyleSettings>
<option name="RECORD_COMPONENTS_WRAP" value="0" />
</JavaCodeStyleSettings>
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value>
<package name="java.util" alias="false" withSubpackages="false" />
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
<package name="io.ktor" alias="false" withSubpackages="true" />
</value>
</option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
</value>
</option>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<editorconfig>
<option name="ENABLED" value="false" />
</editorconfig>
<codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</codeStyleSettings>
</code_scheme>
</component>

View File

@ -1,5 +1,6 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

View File

@ -41,7 +41,7 @@
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
<jvmTarget>14</jvmTarget>
</configuration>
</plugin>
<plugin>

View File

@ -63,25 +63,25 @@ public class EntityParser extends CommandParser {
@Override
public void isParsable(Connection connection, ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
EntityParserProperties entityParserProperties = (EntityParserProperties) properties;
if (stringReader.getNextChar().equals("@")) {
if (stringReader.getNextChar() == '@') {
// selector
if (entityParserProperties.isOnlySingleEntity()) {
throw new SingleEntityOnlyEntityCommandParseException(stringReader, stringReader.getNextChar());
throw new SingleEntityOnlyEntityCommandParseException(stringReader, String.valueOf(stringReader.getNextChar()));
}
stringReader.skip(1); // skip @
String selectorChar = stringReader.readChar();
if (!selectorChar.equals("a") && !selectorChar.equals("e") && !selectorChar.equals("p") && !selectorChar.equals("r") && !selectorChar.equals("s")) {
char selectorChar = stringReader.readChar();
if (selectorChar != 'a' && selectorChar != 'e' && selectorChar != 'p' && selectorChar != 'r' && selectorChar != 's') {
// only @a, @e, @p, @r and @s possible
throw new UnknownMassSelectorEntityCommandParseException(stringReader, stringReader.getNextChar());
}
if (selectorChar.equals("e") && entityParserProperties.isOnlyPlayers()) {
throw new PlayerOnlyEntityCommandParseException(stringReader, selectorChar);
if (selectorChar == 'e' && entityParserProperties.isOnlyPlayers()) {
throw new PlayerOnlyEntityCommandParseException(stringReader, String.valueOf(selectorChar));
}
// parse entity selector
// example: /msg @a[ name = "Bixilon" ] asd
if (!stringReader.getNextChar().equals("[")) {
if (stringReader.getNextChar() != '[') {
// no meta data given, valid
return;
}
@ -91,7 +91,7 @@ public class EntityParser extends CommandParser {
HashSet<String> parameters = new HashSet<>();
while (true) {
stringReader.skipSpaces();
String parameterName = stringReader.readUntil("=").key.replaceAll("\\s", ""); // ToDo: only remove prefix and suffix spaces!
String parameterName = stringReader.readUntil("=").getKey().replaceAll("\\s", ""); // ToDo: only remove prefix and suffix spaces!
if (parameters.contains(parameterName)) {
throw new DuplicatedParameterEntityCommandParseException(stringReader, parameterName);
}
@ -106,12 +106,12 @@ public class EntityParser extends CommandParser {
stringReader.skipSpaces();
parameters.add(parameterName);
String nextChar = stringReader.getNextChar();
if (nextChar.equals("]")) {
char nextChar = stringReader.getNextChar();
if (nextChar == ']') {
stringReader.skip(1);
break;
}
if (nextChar.equals(",")) {
if (nextChar == ',') {
stringReader.skip(1);
}
}

View File

@ -27,7 +27,7 @@ class ItemStackParser : CommandParser() {
if (!connection.mapping.doesItemExist(ModIdentifier(argument.key))) {
throw ItemNotFoundCommandParseException(stringReader, argument.key)
}
if (argument.value == "{" || stringReader.nextChar == "{") {
if (argument.value == "{" || stringReader.nextChar == '{') {
throw TODO("NBT Data needs to be implemented")
}
}

View File

@ -38,7 +38,7 @@ public class StringParser extends CommandParser {
if (stringReader.get(1).equals("\"")) {
stringReader.skip(1);
StringBuilder builder = new StringBuilder();
builder.append(stringReader.readUntil("\"").key);
builder.append(stringReader.readUntil("\"").getKey());
String currentString = builder.toString();
while (currentString.endsWith("\\") && !currentString.endsWith("\\\\")) {
// quotes are escaped, continue

View File

@ -26,9 +26,9 @@ public class DoubleSelectorArgumentParser extends EntitySelectorArgumentParser {
public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException {
Pair<String, String> match = readNextArgument(stringReader);
try {
Double.parseDouble(match.key);
Double.parseDouble(match.getKey());
} catch (Exception e) {
throw new DoubleCommandParseException(stringReader, match.key, e);
throw new DoubleCommandParseException(stringReader, match.getKey(), e);
}
}
}

View File

@ -25,7 +25,7 @@ public abstract class EntitySelectorArgumentParser {
protected Pair<String, String> readNextArgument(ImprovedStringReader stringReader) {
Pair<String, String> match = stringReader.readUntil(ENTITY_VALUE_TERMINATORS);
if (!match.value.equals(" ")) {
if (!match.getValue().equals(" ")) {
// set pointer to --
stringReader.skip(-1);
}

View File

@ -28,14 +28,14 @@ public class IdentifierSelectorArgumentParser extends EntitySelectorArgumentPars
@Override
public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException {
Pair<String, String> match = readNextArgument(stringReader);
String value = match.key;
if (match.key.startsWith("!")) {
String value = match.getKey();
if (match.getKey().startsWith("!")) {
value = value.substring(1);
}
ModIdentifier identifier = new ModIdentifier(value);
if (this == ENTITY_TYPE_IDENTIFIER_SELECTOR_ARGUMENT_PARSER) {
if (EntityClassMappings.INSTANCE.getByIdentifier(identifier) == null) {
throw new UnknownEntityCommandParseException(stringReader, match.key);
throw new UnknownEntityCommandParseException(stringReader, match.getKey());
}
return;
}

View File

@ -26,9 +26,9 @@ public class IntegerSelectorArgumentParser extends EntitySelectorArgumentParser
public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException {
Pair<String, String> match = readNextArgument(stringReader);
try {
Integer.parseInt(match.key);
Integer.parseInt(match.getKey());
} catch (Exception e) {
throw new IntegerCommandParseException(stringReader, match.key, e);
throw new IntegerCommandParseException(stringReader, match.getKey(), e);
}
}
}

View File

@ -47,12 +47,12 @@ public class ListSelectorArgumentParser extends EntitySelectorArgumentParser {
@Override
public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException {
Pair<String, String> match = readNextArgument(stringReader);
String value = match.key;
if (match.key.startsWith("!")) {
String value = match.getKey();
if (match.getValue().startsWith("!")) {
value = value.substring(1);
}
if (!this.values.contains(value)) {
throw new UnknownEnumValueCommandParseException(stringReader, match.key);
throw new UnknownEnumValueCommandParseException(stringReader, match.getKey());
}
}
}

View File

@ -50,35 +50,35 @@ public class RangeSelectorArgumentParser extends EntitySelectorArgumentParser {
public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException {
Pair<String, String> match = readNextArgument(stringReader);
if (match.key.contains("..")) {
if (match.getKey().contains("..")) {
// range
String[] split = match.key.split("\\.\\.");
String[] split = match.getKey().split("\\.\\.");
if (split.length != 2) {
throw new RangeBadFormatCommandParseException(stringReader, match.key);
throw new RangeBadFormatCommandParseException(stringReader, match.getKey());
}
double min;
double max;
if (split[0].isBlank()) {
min = getMinValue();
} else {
min = parseValue(stringReader, match.key, split[0]);
min = parseValue(stringReader, match.getKey(), split[0]);
}
if (split[1].isBlank()) {
max = getMaxValue();
} else {
max = parseValue(stringReader, match.key, split[1]);
max = parseValue(stringReader, match.getKey(), split[1]);
}
if (min < getMinValue() || max > getMaxValue()) {
throw new ValueOutOfRangeCommandParseException(stringReader, minValue, maxValue, match.key);
throw new ValueOutOfRangeCommandParseException(stringReader, minValue, maxValue, match.getKey());
}
if (min > max) {
throw new MinimumBiggerAsMaximumCommandParseException(stringReader, match.key);
throw new MinimumBiggerAsMaximumCommandParseException(stringReader, match.getKey());
}
return;
}
parseValue(stringReader, match.key, match.key);
parseValue(stringReader, match.getKey(), match.getKey());
}
private double parseValue(ImprovedStringReader stringReader, String match, String value) throws CommandParseException {

View File

@ -20,8 +20,8 @@ public class UnknownMassSelectorEntityCommandParseException extends CommandParse
private static final String ERROR_MESSAGE = "Unknown mass selector!";
public UnknownMassSelectorEntityCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
public UnknownMassSelectorEntityCommandParseException(ImprovedStringReader command, char currentArgument) {
super(ERROR_MESSAGE, command, String.valueOf(currentArgument));
}
public UnknownMassSelectorEntityCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {

View File

@ -10,35 +10,13 @@
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util
package de.bixilon.minosoft.util;
import java.util.Objects;
public class Pair<K, V> {
public final K key;
public final V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(key, pair.key) && Objects.equals(value, pair.value);
}
@Override
public int hashCode() {
return Objects.hash(key, value);
}
public String toString() {
return String.format("%s=%s", key, value);
data class Pair<K, V>(
val key: K,
val value: V
) {
override fun toString(): String {
return String.format("%s=%s", key, value)
}
}

View File

@ -16,8 +16,6 @@ package de.bixilon.minosoft.util.buffers;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.Pair;
import javax.annotation.Nullable;
public class ImprovedStringReader {
private final String string;
private int position;
@ -28,7 +26,7 @@ public class ImprovedStringReader {
public Pair<String, String> readUntil(String... search) {
Pair<String, String> ret = getUntil(search);
position += ret.key.length() + ret.value.length();
position += ret.getKey().length() + ret.getValue().length();
return ret;
}
@ -57,11 +55,11 @@ public class ImprovedStringReader {
}
public String readUntilNextCommandArgument() {
return readUntil(ProtocolDefinition.COMMAND_SEPARATOR).key;
return readUntil(ProtocolDefinition.COMMAND_SEPARATOR).getKey();
}
public String getUntilNextCommandArgument() {
return getUntil(ProtocolDefinition.COMMAND_SEPARATOR).key;
return getUntil(ProtocolDefinition.COMMAND_SEPARATOR).getKey();
}
public String getString() {
@ -80,19 +78,18 @@ public class ImprovedStringReader {
return string.length() - position;
}
public String readChar() {
public char readChar() {
if (getRemainingChars() == 0) {
return null;
return 0;
}
return String.valueOf(string.charAt(position++));
return string.charAt(position++);
}
@Nullable
public String getNextChar() {
public char getNextChar() {
if (getRemainingChars() == 0) {
return null;
return 0;
}
return String.valueOf(string.charAt(position));
return string.charAt(position);
}
public String getRest() {
@ -128,8 +125,8 @@ public class ImprovedStringReader {
public int skipSpaces() {
int skipped = 0;
String nextChar = getNextChar();
while (nextChar != null && getNextChar().equals(" ")) {
char nextChar = getNextChar();
while (getNextChar() == ' ') {
skip(1);
skipped++;
}