From d94616937e30e33a4a185d1b21002a3edee29d8c Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sat, 3 Oct 2020 14:02:14 +0200 Subject: [PATCH] improve code style even more --- .gitlab-ci.yml | 3 +- .../game/datatypes/entities/VillagerData.java | 1 + .../entities/meta/EntityMetaData.java | 57 +++++++-------- .../entities/meta/ZombieVillagerMetaData.java | 8 +-- .../objectLoader/versions/Version.java | 19 ++--- .../game/datatypes/world/InChunkLocation.java | 8 +-- .../protocol/protocol/InByteBuffer.java | 71 +++++++++---------- .../protocol/protocol/OutByteBuffer.java | 40 +++++------ .../java/de/bixilon/minosoft/util/OSUtil.java | 27 +++---- 9 files changed, 115 insertions(+), 119 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b862df1e6..3d2d84f1e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -24,5 +24,4 @@ code_quality: stage: code_quality artifacts: expose_as: 'Code Quality Report' - paths: [ gl-code-quality-report.json ] - + paths: [ gl-code-quality-report.json ] \ No newline at end of file diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/VillagerData.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/VillagerData.java index a3bf0d6ad..5c51ee06b 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/VillagerData.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/VillagerData.java @@ -136,6 +136,7 @@ public class VillagerData { } } + @Override public boolean equals(Object obj) { if (super.equals(obj)) { diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/meta/EntityMetaData.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/meta/EntityMetaData.java index 08850b30a..b7a6ec11b 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/meta/EntityMetaData.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/meta/EntityMetaData.java @@ -50,49 +50,46 @@ public class EntityMetaData { } public static Object getData(EntityMetaDataValueTypes type, InByteBuffer buffer) { - Object data = null; - - switch (type) { - case BYTE -> data = buffer.readByte(); - case VAR_INT -> data = buffer.readVarInt(); - case SHORT -> data = buffer.readShort(); - case INT -> data = buffer.readInt(); - case FLOAT -> data = buffer.readFloat(); - case STRING -> data = buffer.readString(); - case CHAT -> data = buffer.readTextComponent(); - case BOOLEAN -> data = buffer.readBoolean(); - case VECTOR -> data = new Vector(buffer.readInt(), buffer.readInt(), buffer.readInt()); - case SLOT -> data = buffer.readSlot(); - case ROTATION -> data = new EntityRotation(buffer.readFloat(), buffer.readFloat(), buffer.readFloat()); - case POSITION -> data = buffer.readPosition(); + return switch (type) { + case BYTE -> buffer.readByte(); + case VAR_INT -> buffer.readVarInt(); + case SHORT -> buffer.readShort(); + case INT -> buffer.readInt(); + case FLOAT -> buffer.readFloat(); + case STRING -> buffer.readString(); + case CHAT -> buffer.readTextComponent(); + case BOOLEAN -> buffer.readBoolean(); + case VECTOR -> new Vector(buffer.readInt(), buffer.readInt(), buffer.readInt()); + case SLOT -> buffer.readSlot(); + case ROTATION -> new EntityRotation(buffer.readFloat(), buffer.readFloat(), buffer.readFloat()); + case POSITION -> buffer.readPosition(); case OPT_CHAT -> { if (buffer.readBoolean()) { - data = buffer.readTextComponent(); + yield buffer.readTextComponent(); } + yield null; } case OPT_POSITION -> { if (buffer.readBoolean()) { - data = buffer.readPosition(); + yield buffer.readPosition(); } + yield null; } - case DIRECTION -> data = buffer.readDirection(); + case DIRECTION -> buffer.readDirection(); case OPT_UUID -> { if (buffer.readBoolean()) { - data = buffer.readUUID(); + yield buffer.readUUID(); } + yield null; } - case NBT -> data = buffer.readNBT(); - case PARTICLE -> data = buffer.readParticle(); - case POSE -> data = buffer.readPose(); - case BLOCK_ID -> { - int blockId = buffer.readVarInt(); - data = buffer.getConnection().getMapping().getBlockById(blockId); - } - case OPT_VAR_INT -> data = buffer.readVarInt() - 1; - case VILLAGER_DATA -> data = new VillagerData(VillagerData.VillagerTypes.byId(buffer.readVarInt()), VillagerData.VillagerProfessions.byId(buffer.readVarInt(), buffer.getProtocolId()), VillagerData.VillagerLevels.byId(buffer.readVarInt())); + case NBT -> buffer.readNBT(); + case PARTICLE -> buffer.readParticle(); + case POSE -> buffer.readPose(); + case BLOCK_ID -> buffer.getConnection().getMapping().getBlockById(buffer.readVarInt()); + case OPT_VAR_INT -> buffer.readVarInt() - 1; + case VILLAGER_DATA -> new VillagerData(VillagerData.VillagerTypes.byId(buffer.readVarInt()), VillagerData.VillagerProfessions.byId(buffer.readVarInt(), buffer.getProtocolId()), VillagerData.VillagerLevels.byId(buffer.readVarInt())); default -> throw new IllegalStateException("Unexpected value: " + type); - } - return data; + }; } public MetaDataHashMap getSets() { diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/meta/ZombieVillagerMetaData.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/meta/ZombieVillagerMetaData.java index 528bcb208..ddc62ded1 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/meta/ZombieVillagerMetaData.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/meta/ZombieVillagerMetaData.java @@ -46,10 +46,6 @@ public class ZombieVillagerMetaData extends ZombieMetaData { return super.getLastDataIndex() + 2; } - public VillagerData.VillagerTypes getType() { - return getVillageData().getType(); - } - public VillagerData getVillageData() { final VillagerData defaultValue = new VillagerData(VillagerData.VillagerTypes.PLAINS, VillagerData.VillagerProfessions.NONE, VillagerData.VillagerLevels.APPRENTICE); if (protocolId < 451) { @@ -58,6 +54,10 @@ public class ZombieVillagerMetaData extends ZombieMetaData { return sets.getVillagerData(super.getLastDataIndex() + 2, defaultValue); } + public VillagerData.VillagerTypes getType() { + return getVillageData().getType(); + } + public VillagerData.VillagerLevels getLevel() { return getVillageData().getLevel(); } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/versions/Version.java b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/versions/Version.java index ebc4cb5be..8adb6f092 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/versions/Version.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/versions/Version.java @@ -48,21 +48,16 @@ public class Version { return null; } - public String getVersionName() { - return versionName; - } - public HashMap> getClientboundPacketMapping() { return clientboundPacketMapping; } - public HashMap> getServerboundPacketMapping() { - return serverboundPacketMapping; + public String getVersionName() { + return versionName; } - @Override - public int hashCode() { - return getProtocolVersion(); + public HashMap> getServerboundPacketMapping() { + return serverboundPacketMapping; } public VersionMapping getMapping() { @@ -73,6 +68,11 @@ public class Version { this.mapping = mapping; } + @Override + public int hashCode() { + return getProtocolVersion(); + } + public boolean isGettingLoaded() { return isGettingLoaded; } @@ -81,6 +81,7 @@ public class Version { isGettingLoaded = gettingLoaded; } + public int getProtocolVersion() { return protocolVersion; } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/world/InChunkLocation.java b/src/main/java/de/bixilon/minosoft/game/datatypes/world/InChunkLocation.java index 48ed5c1bb..9dd6fa8f5 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/world/InChunkLocation.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/world/InChunkLocation.java @@ -42,10 +42,6 @@ public class InChunkLocation { return getX() == that.getX() && getY() == that.getY() && getZ() == that.getZ(); } - public ChunkNibbleLocation getChunkNibbleLocation() { - return new ChunkNibbleLocation(getX(), getY() % 16, getZ()); - } - public int getX() { return x; } @@ -54,6 +50,10 @@ public class InChunkLocation { return y; } + public ChunkNibbleLocation getChunkNibbleLocation() { + return new ChunkNibbleLocation(getX(), getY() % 16, getZ()); + } + @Override public String toString() { return String.format("%d %d %d", getX(), getY(), getZ()); diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java index efc22364e..39e91c87d 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java @@ -43,7 +43,7 @@ public class InByteBuffer { final Connection connection; final int protocolId; final byte[] bytes; - int pos; + int position; public InByteBuffer(byte[] bytes, Connection connection) { this.bytes = bytes; @@ -67,19 +67,23 @@ public class InByteBuffer { return buffer.getShort(0); } - public byte[] readBytes(int count) { - byte[] ret = new byte[count]; - System.arraycopy(bytes, pos, ret, 0, count); - pos = pos + count; - return ret; - } - public Long readLong() { ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); buffer.put(readBytes(Long.BYTES)); return buffer.getLong(0); } + public byte[] readBytes(int count) { + byte[] ret = new byte[count]; + System.arraycopy(bytes, position, ret, 0, count); + position = position + count; + return ret; + } + + public double readFixedPointNumberInteger() { + return readInt() / 32.0D; + } + public long readVarLong() { int numRead = 0; long result = 0; @@ -136,36 +140,11 @@ public class InByteBuffer { return new UUID(readLong(), readLong()); } - public byte readByte() { - byte ret; - ret = bytes[pos]; - pos++; - return ret; - } - - public int readInt() { - ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); - buffer.put(readBytes(Integer.BYTES)); - return buffer.getInt(0); - } - - public double readFixedPointNumberInteger() { - return readInt() / 32.0D; - } - public String readString() { int length = readVarInt(); return new String(readBytes(length), StandardCharsets.UTF_8); } - public double readFixedPointNumberByte() { - return readByte() / 32.0D; - } - - public JsonObject readJSON() { - return JsonParser.parseString(readString()).getAsJsonObject(); - } - public int readVarInt() { // thanks https://wiki.vg/Protocol#VarInt_and_VarLong int numRead = 0; @@ -186,6 +165,24 @@ public class InByteBuffer { return result; } + public int readInt() { + ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); + buffer.put(readBytes(Integer.BYTES)); + return buffer.getInt(0); + } + + public double readFixedPointNumberByte() { + return readByte() / 32.0D; + } + + public JsonObject readJSON() { + return JsonParser.parseString(readString()).getAsJsonObject(); + } + + public byte readByte() { + return bytes[position++]; + } + public BlockPosition readPosition() { //ToDo: protocol id 7 if (protocolId < 440) { @@ -315,15 +312,15 @@ public class InByteBuffer { } public int getPosition() { - return this.pos; + return this.position; } public void setPosition(int pos) { - this.pos = pos; + this.position = pos; } public int getBytesLeft() { - return bytes.length - pos; + return bytes.length - position; } byte[] readBytes(int pos, int count) { @@ -414,7 +411,7 @@ public class InByteBuffer { @Override public String toString() { - return "dataLen: " + bytes.length + "; pos: " + pos; + return "dataLen: " + bytes.length + "; position: " + position; } public byte[] getBytes() { diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java index 506b30087..664f9d09d 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java @@ -52,12 +52,32 @@ public class OutByteBuffer { } } + public void writeInt(int i) { + ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); + buffer.putInt(i); + for (byte b : buffer.array()) { + bytes.add(b); + } + } + public void writeBytes(byte[] b) { for (byte value : b) { bytes.add(value); } } + public void writeLong(Long l) { + ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); + buffer.putLong(l); + for (byte b : buffer.array()) { + bytes.add(b); + } + } + + public void writeJSON(JsonObject j) { + writeString(j.toString()); + } + public void writeVarLong(long value) { do { @@ -75,14 +95,6 @@ public class OutByteBuffer { bytes.add(b); } - public void writeInt(int i) { - ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); - buffer.putInt(i); - for (byte b : buffer.array()) { - bytes.add(b); - } - } - public void writeFloat(Float f) { ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES); buffer.putFloat(f); @@ -108,18 +120,6 @@ public class OutByteBuffer { } } - public void writeLong(Long l) { - ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); - buffer.putLong(l); - for (byte b : buffer.array()) { - bytes.add(b); - } - } - - public void writeJSON(JsonObject j) { - writeString(j.toString()); - } - public void writeFixedPointNumberInteger(double d) { writeInt((int) (d * 32.0D)); } diff --git a/src/main/java/de/bixilon/minosoft/util/OSUtil.java b/src/main/java/de/bixilon/minosoft/util/OSUtil.java index 2ceee0a6a..9437efdf1 100644 --- a/src/main/java/de/bixilon/minosoft/util/OSUtil.java +++ b/src/main/java/de/bixilon/minosoft/util/OSUtil.java @@ -14,21 +14,22 @@ package de.bixilon.minosoft.util; public final class OSUtil { - static OS os; + final static OS os; + + static { + String name = System.getProperty("os.name"); + if (name.startsWith("Windows")) { + os = OS.WINDOWS; + } else if (name.startsWith("Linux")) { + os = OS.LINUX; + } else if (name.startsWith("Mac")) { + os = OS.MAC; + } else { + os = OS.OTHER; + } + } public static OS getOS() { - if (os == null) { - String name = System.getProperty("os.name"); - if (name.startsWith("Windows")) { - os = OS.WINDOWS; - } else if (name.startsWith("Linux")) { - os = OS.LINUX; - } else if (name.startsWith("Mac")) { - os = OS.MAC; - } else { - os = OS.OTHER; - } - } return os; }