improve code style even more

This commit is contained in:
Bixilon 2020-10-03 14:02:14 +02:00
parent 44973df41a
commit d94616937e
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 115 additions and 119 deletions

View File

@ -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 ]

View File

@ -136,6 +136,7 @@ public class VillagerData {
}
}
@Override
public boolean equals(Object obj) {
if (super.equals(obj)) {

View File

@ -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() {

View File

@ -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();
}

View File

@ -48,21 +48,16 @@ public class Version {
return null;
}
public String getVersionName() {
return versionName;
}
public HashMap<ConnectionStates, HashBiMap<Packets.Clientbound, Integer>> getClientboundPacketMapping() {
return clientboundPacketMapping;
}
public HashMap<ConnectionStates, HashBiMap<Packets.Serverbound, Integer>> getServerboundPacketMapping() {
return serverboundPacketMapping;
public String getVersionName() {
return versionName;
}
@Override
public int hashCode() {
return getProtocolVersion();
public HashMap<ConnectionStates, HashBiMap<Packets.Serverbound, Integer>> 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;
}

View File

@ -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());

View File

@ -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() {

View File

@ -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));
}

View File

@ -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;
}