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: variables:
MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository" 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 image: maven:3-openjdk-15

View File

@ -1,22 +1,19 @@
<component name="ProjectCodeStyleConfiguration"> <component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173"> <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> <JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS"> <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
<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>
</JetCodeStyleSettings> </JetCodeStyleSettings>
<editorconfig>
<option name="ENABLED" value="false" />
</editorconfig>
<codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</codeStyleSettings>
</code_scheme> </code_scheme>
</component> </component>

View File

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

View File

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

View File

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

View File

@ -27,7 +27,7 @@ class ItemStackParser : CommandParser() {
if (!connection.mapping.doesItemExist(ModIdentifier(argument.key))) { if (!connection.mapping.doesItemExist(ModIdentifier(argument.key))) {
throw ItemNotFoundCommandParseException(stringReader, 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") throw TODO("NBT Data needs to be implemented")
} }
} }

View File

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

View File

@ -26,9 +26,9 @@ public class DoubleSelectorArgumentParser extends EntitySelectorArgumentParser {
public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException { public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException {
Pair<String, String> match = readNextArgument(stringReader); Pair<String, String> match = readNextArgument(stringReader);
try { try {
Double.parseDouble(match.key); Double.parseDouble(match.getKey());
} catch (Exception e) { } 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) { protected Pair<String, String> readNextArgument(ImprovedStringReader stringReader) {
Pair<String, String> match = stringReader.readUntil(ENTITY_VALUE_TERMINATORS); Pair<String, String> match = stringReader.readUntil(ENTITY_VALUE_TERMINATORS);
if (!match.value.equals(" ")) { if (!match.getValue().equals(" ")) {
// set pointer to -- // set pointer to --
stringReader.skip(-1); stringReader.skip(-1);
} }

View File

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

View File

@ -26,9 +26,9 @@ public class IntegerSelectorArgumentParser extends EntitySelectorArgumentParser
public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException { public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException {
Pair<String, String> match = readNextArgument(stringReader); Pair<String, String> match = readNextArgument(stringReader);
try { try {
Integer.parseInt(match.key); Integer.parseInt(match.getKey());
} catch (Exception e) { } 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 @Override
public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException { public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException {
Pair<String, String> match = readNextArgument(stringReader); Pair<String, String> match = readNextArgument(stringReader);
String value = match.key; String value = match.getKey();
if (match.key.startsWith("!")) { if (match.getValue().startsWith("!")) {
value = value.substring(1); value = value.substring(1);
} }
if (!this.values.contains(value)) { 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 { public void isParsable(Connection connection, ImprovedStringReader stringReader) throws CommandParseException {
Pair<String, String> match = readNextArgument(stringReader); Pair<String, String> match = readNextArgument(stringReader);
if (match.key.contains("..")) { if (match.getKey().contains("..")) {
// range // range
String[] split = match.key.split("\\.\\."); String[] split = match.getKey().split("\\.\\.");
if (split.length != 2) { if (split.length != 2) {
throw new RangeBadFormatCommandParseException(stringReader, match.key); throw new RangeBadFormatCommandParseException(stringReader, match.getKey());
} }
double min; double min;
double max; double max;
if (split[0].isBlank()) { if (split[0].isBlank()) {
min = getMinValue(); min = getMinValue();
} else { } else {
min = parseValue(stringReader, match.key, split[0]); min = parseValue(stringReader, match.getKey(), split[0]);
} }
if (split[1].isBlank()) { if (split[1].isBlank()) {
max = getMaxValue(); max = getMaxValue();
} else { } else {
max = parseValue(stringReader, match.key, split[1]); max = parseValue(stringReader, match.getKey(), split[1]);
} }
if (min < getMinValue() || max > getMaxValue()) { if (min < getMinValue() || max > getMaxValue()) {
throw new ValueOutOfRangeCommandParseException(stringReader, minValue, maxValue, match.key); throw new ValueOutOfRangeCommandParseException(stringReader, minValue, maxValue, match.getKey());
} }
if (min > max) { if (min > max) {
throw new MinimumBiggerAsMaximumCommandParseException(stringReader, match.key); throw new MinimumBiggerAsMaximumCommandParseException(stringReader, match.getKey());
} }
return; return;
} }
parseValue(stringReader, match.key, match.key); parseValue(stringReader, match.getKey(), match.getKey());
} }
private double parseValue(ImprovedStringReader stringReader, String match, String value) throws CommandParseException { 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!"; private static final String ERROR_MESSAGE = "Unknown mass selector!";
public UnknownMassSelectorEntityCommandParseException(ImprovedStringReader command, String currentArgument) { public UnknownMassSelectorEntityCommandParseException(ImprovedStringReader command, char currentArgument) {
super(ERROR_MESSAGE, command, currentArgument); super(ERROR_MESSAGE, command, String.valueOf(currentArgument));
} }
public UnknownMassSelectorEntityCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) { 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. * This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/ */
package de.bixilon.minosoft.util
package de.bixilon.minosoft.util; data class Pair<K, V>(
val key: K,
import java.util.Objects; val value: V
) {
public class Pair<K, V> { override fun toString(): String {
return String.format("%s=%s", key, value)
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);
} }
} }

View File

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