mojang: remove status, more enum refactoring, code quality improvement

This commit is contained in:
Bixilon 2020-10-14 15:00:32 +02:00
parent 19626a37d9
commit 2029deeca7
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
18 changed files with 90 additions and 348 deletions

View File

@ -14,22 +14,12 @@
package de.bixilon.minosoft.data;
public enum GameModes {
SURVIVAL(0),
CREATIVE(1),
ADVENTURE(2),
SPECTATOR(3);
final int id;
GameModes(int id) {
this.id = id;
}
SURVIVAL,
CREATIVE,
ADVENTURE,
SPECTATOR;
public static GameModes byId(int id) {
return values()[id];
}
public int getId() {
return id;
}
}

View File

@ -1,21 +0,0 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.entities;
public interface EntityEnumInterface {
int getType();
Class<? extends Entity> getClazz();
}

View File

@ -13,33 +13,37 @@
package de.bixilon.minosoft.data.entities;
import de.bixilon.minosoft.data.ChangeableIdentifier;
import com.google.common.collect.HashBiMap;
public enum EntityPropertyKeys {
MAX_HEALTH(new ChangeableIdentifier("generic.maxHealth")),
FOLLOW_RANGE(new ChangeableIdentifier("generic.followRange")),
KNOCKBACK_RESISTANCE(new ChangeableIdentifier("generic.knockbackResistance")),
MOVEMENT_SPEED(new ChangeableIdentifier("generic.movementSpeed")),
ATTACK_DAMAGE(new ChangeableIdentifier("generic.attackDamage")),
HORSE_JUMP_STRENGTH(new ChangeableIdentifier("horse.jumpStrength")),
ZOMBIE_SPAWN_REINFORCEMENT(new ChangeableIdentifier("zombie.spawnReinforcements"));
MAX_HEALTH("generic.maxHealth"),
FOLLOW_RANGE("generic.followRange"),
KNOCKBACK_RESISTANCE("generic.knockbackResistance"),
MOVEMENT_SPEED("generic.movementSpeed"),
ATTACK_DAMAGE("generic.attackDamage"),
HORSE_JUMP_STRENGTH("horse.jumpStrength"),
ZOMBIE_SPAWN_REINFORCEMENT("zombie.spawnReinforcements");
final ChangeableIdentifier changeableIdentifier;
final static HashBiMap<String, EntityPropertyKeys> keys = HashBiMap.create();
EntityPropertyKeys(ChangeableIdentifier changeableIdentifier) {
this.changeableIdentifier = changeableIdentifier;
}
public static EntityPropertyKeys byName(String name, int protocolId) {
for (EntityPropertyKeys propertyKey : values()) {
if (propertyKey.getChangeableIdentifier().isValidName(name, protocolId)) {
return propertyKey;
static {
for (EntityPropertyKeys key : values()) {
keys.put(key.getIdentifier(), key);
}
}
return null;
final String identifier;
EntityPropertyKeys(String identifier) {
this.identifier = identifier;
}
public ChangeableIdentifier getChangeableIdentifier() {
return changeableIdentifier;
public static EntityPropertyKeys byName(String name) {
return keys.get(name);
}
public String getIdentifier() {
return identifier;
}
}

View File

@ -13,9 +13,10 @@
package de.bixilon.minosoft.data.entities;
import com.google.common.collect.HashBiMap;
import de.bixilon.minosoft.data.entities.objects.*;
public enum Objects implements EntityEnumInterface {
public enum Objects {
BOAT(1, Boat.class),
ITEM_STACK(2, ItemStack.class),
AREA_EFFECT_CLOUD(3, AreaEffectCloud.class),
@ -46,31 +47,30 @@ public enum Objects implements EntityEnumInterface {
DRAGON_FIREBALL(93, DragonFireball.class),
TRIDENT(94, Trident.class);
// ToDo: size changed between versions, fix it!
final static HashBiMap<Integer, Objects> objects = HashBiMap.create();
final int type;
static {
for (Objects object : values()) {
objects.put(object.getId(), object);
}
}
final int id;
final Class<? extends EntityObject> clazz;
Objects(int type, Class<? extends EntityObject> clazz) {
this.type = type;
Objects(int id, Class<? extends EntityObject> clazz) {
this.id = id;
this.clazz = clazz;
}
public static Objects byType(int type) {
for (Objects object : values()) {
if (object.getType() == type) {
return object;
}
}
return null;
public static Objects byId(int id) {
return objects.get(id);
}
@Override
public int getType() {
return type;
public int getId() {
return id;
}
@Override
public Class<? extends EntityObject> getClazz() {
return clazz;
}

View File

@ -19,7 +19,7 @@ public class MooshroomMetaData extends AnimalMetaData {
}
public MooshroomTypes getType() {
final String defaultValue = MooshroomTypes.RED.getTypeName();
final String defaultValue = MooshroomTypes.RED.name();
if (protocolId < 461) {
return MooshroomTypes.byTypeName(defaultValue);
}
@ -35,26 +35,12 @@ public class MooshroomMetaData extends AnimalMetaData {
}
public enum MooshroomTypes {
RED("red"),
BROWN("brown");
final String typeName;
MooshroomTypes(String typeName) {
this.typeName = typeName;
}
RED,
BROWN;
public static MooshroomTypes byTypeName(String typeName) {
for (MooshroomTypes type : values()) {
if (type.getTypeName().equals(typeName)) {
return type;
}
}
return null;
return valueOf(typeName.toUpperCase());
}
public String getTypeName() {
return typeName;
}
}
}

View File

@ -46,7 +46,7 @@ public class Log {
}
/**
* Logs all game related things (chunk loading, rendering, ...)
* Logs all game related things (mostly visible stuff to the user)
*
* @param message Raw message to log
*/
@ -97,7 +97,7 @@ public class Log {
}
/**
* Logs all warnings (connection to server failed, ...)
* Logs all warnings (error occurrence, ...)
*
* @param message Raw message to log
*/
@ -106,7 +106,7 @@ public class Log {
}
/**
* Logs all debug relevant infos (...)
* Logs way more data (data that might be important for resolving issues)
*
* @param message Raw message to log
*/
@ -115,7 +115,7 @@ public class Log {
}
/**
* Logs all debug relevant infos (even higher level!) (connection status, ...)
* Logs all debug relevant infos (even higher level!) (connection status, ...). Basically everything that happens
*
* @param message Raw message to log
*/
@ -124,7 +124,7 @@ public class Log {
}
/**
* Logs all protocol data (received protocol with length and command x,...)
* Logs all protocol data (received packet x with data, etc). Should only be used in packets
*
* @param message Raw message to log
*/
@ -154,7 +154,7 @@ public class Log {
}
/**
* Logs all general infos (connecting to server, ...)
* Logs all general infos, that are more or less important to the user (connecting to server, ...)
*
* @param message Raw message to log
*/

View File

@ -25,11 +25,6 @@ public class Logger {
this.modName = modName;
}
/**
* Logs all game related things (chunk loading, rendering, ...)
*
* @param message Raw message to log
*/
public void game(String message) {
log(LogLevels.GAME, message, ChatColors.getColorByName("green"));
}
@ -38,57 +33,23 @@ public class Logger {
Log.log(level, String.format("[%s] ", modName), message, color);
}
/**
* Logs all fatal errors (critical exceptions, etc)
*
* @param message Raw message to log
*/
public void fatal(String message) {
log(LogLevels.FATAL, message, ChatColors.getColorByName("dark_red"));
}
/**
* Logs all general infos (connecting to server, ...)
*
* @param message Raw message to log
*/
public void info(String message) {
log(LogLevels.INFO, message, ChatColors.getColorByName("white"));
}
/**
* Logs all warnings (connection to server failed, ...)
*
* @param message Raw message to log
*/
public void warn(String message) {
log(LogLevels.WARNING, message, ChatColors.getColorByName("red"));
}
/**
* Logs all debug relevant infos (...)
*
* @param message Raw message to log
*/
public void debug(String message) {
log(LogLevels.DEBUG, message, ChatColors.getColorByName("gray"));
}
/**
* Logs all debug relevant infos (even higher level!) (connection status, ...)
*
* @param message Raw message to log
*/
public void verbose(String message) {
log(LogLevels.VERBOSE, message, ChatColors.getColorByName("yellow"));
}
/**
* Logs all protocol data (received protocol with length and command x,...)
*
* @param message Raw message to log
*/
public void protocol(String message) {
log(LogLevels.PROTOCOL, message, ChatColors.getColorByName("blue"));
}
}

View File

@ -33,7 +33,7 @@ public class PacketEntityProperties implements ClientboundPacket {
if (buffer.getProtocolId() < 7) {
int count = buffer.readInt();
for (int i = 0; i < count; i++) {
EntityPropertyKeys key = EntityPropertyKeys.byName(buffer.readString(), buffer.getProtocolId());
EntityPropertyKeys key = EntityPropertyKeys.byName(buffer.readString());
double value = buffer.readDouble();
short listLength = buffer.readShort();
for (int ii = 0; ii < listLength; ii++) {
@ -48,7 +48,7 @@ public class PacketEntityProperties implements ClientboundPacket {
}
int count = buffer.readInt();
for (int i = 0; i < count; i++) {
EntityPropertyKeys key = EntityPropertyKeys.byName(buffer.readString(), buffer.getProtocolId());
EntityPropertyKeys key = EntityPropertyKeys.byName(buffer.readString());
double value = buffer.readDouble();
int listLength = buffer.readVarInt();
for (int ii = 0; ii < listLength; ii++) {

View File

@ -47,7 +47,7 @@ public class PacketSpawnObject implements ClientboundPacket {
}
Class<? extends Entity> typeClass;
if (buffer.getProtocolId() < 458) {
typeClass = Objects.byType(type).getClazz();
typeClass = Objects.byId(type).getClazz();
} else {
typeClass = Entities.getClassByIdentifier(buffer.getConnection().getMapping().getEntityIdentifierById(type));
}

View File

@ -36,7 +36,7 @@ public class PacketAdvancementTab implements ServerboundPacket {
@Override
public OutPacketBuffer write(Connection connection) {
OutPacketBuffer buffer = new OutPacketBuffer(connection, Packets.Serverbound.PLAY_ADVANCEMENT_TAB);
buffer.writeVarInt(action.getId());
buffer.writeVarInt(action.ordinal());
if (action == AdvancementTabStatus.OPEN_TAB) {
buffer.writeString(tabToOpen);
}
@ -49,26 +49,11 @@ public class PacketAdvancementTab implements ServerboundPacket {
}
public enum AdvancementTabStatus {
OPEN_TAB(0),
CLOSE_TAB(1);
final int id;
AdvancementTabStatus(int id) {
this.id = id;
}
OPEN_TAB,
CLOSE_TAB;
public static AdvancementTabStatus byId(int id) {
for (AdvancementTabStatus action : values()) {
if (action.getId() == id) {
return action;
}
}
return null;
}
public int getId() {
return id;
return values()[id];
}
}
}

View File

@ -69,7 +69,7 @@ public class PacketInteractEntity implements ServerboundPacket {
click = EntityInteractionClicks.INTERACT;
}
}
buffer.writeByte((byte) click.getId());
buffer.writeByte((byte) click.ordinal());
if (buffer.getProtocolId() >= 33) {
if (click == EntityInteractionClicks.INTERACT_AT) {
// position
@ -100,18 +100,8 @@ public class PacketInteractEntity implements ServerboundPacket {
}
public enum EntityInteractionClicks {
INTERACT(0),
ATTACK(1),
INTERACT_AT(2);
final int id;
EntityInteractionClicks(int id) {
this.id = id;
}
public int getId() {
return id;
}
INTERACT,
ATTACK,
INTERACT_AT
}
}

View File

@ -34,7 +34,7 @@ public class PacketResourcePackStatus implements ServerboundPacket {
if (buffer.getProtocolId() < 204) {
buffer.writeString(hash);
}
buffer.writeVarInt(status.getId());
buffer.writeVarInt(status.ordinal());
return buffer;
}
@ -44,19 +44,9 @@ public class PacketResourcePackStatus implements ServerboundPacket {
}
public enum ResourcePackStates {
SUCCESSFULLY(0),
DECLINED(1),
FAILED_DOWNLOAD(2),
ACCEPTED(3);
final int id;
ResourcePackStates(int id) {
this.id = id;
}
public int getId() {
return id;
}
SUCCESSFULLY,
DECLINED,
FAILED_DOWNLOAD,
ACCEPTED
}
}

View File

@ -21,7 +21,7 @@ import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusRespo
public class Packets {
public enum Serverbound implements PacketBoundary {
public enum Serverbound {
HANDSHAKING_HANDSHAKE,
STATUS_PING,
STATUS_REQUEST,
@ -92,7 +92,7 @@ public class Packets {
}
}
public enum Clientbound implements PacketBoundary {
public enum Clientbound {
STATUS_RESPONSE(PacketStatusResponse.class),
STATUS_PONG(PacketStatusPong.class),
LOGIN_DISCONNECT(PacketLoginDisconnect.class),
@ -216,7 +216,4 @@ public class Packets {
return clazz;
}
}
public interface PacketBoundary {
}
}

View File

@ -1,116 +0,0 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.mojang.api;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.logging.LogLevels;
import de.bixilon.minosoft.util.HTTP;
import java.net.http.HttpResponse;
import java.util.HashMap;
public final class MojangStatus {
public static HashMap<Services, ServiceStatus> getStatus() {
HttpResponse<String> response = HTTP.get(MojangURLs.STATUS.getUrl());
if (response == null) {
Log.mojang("Failed to fetch Status");
return getUnknownStatusMap();
}
if (response.statusCode() != 200) {
Log.mojang(String.format("Failed to fetch Status with error code %d", response.statusCode()));
return getUnknownStatusMap();
}
// now it is hopefully okay
HashMap<Services, ServiceStatus> ret = new HashMap<>();
try {
JsonArray json = JsonParser.parseString(response.body()).getAsJsonArray();
for (int i = 0; i < json.size(); i++) {
JsonObject innerJson = json.get(i).getAsJsonObject();
for (String key : innerJson.keySet()) {
Services service = Services.byKey(key);
ret.put(service, ServiceStatus.byKey(innerJson.get(key).getAsString()));
}
}
if (ret.size() != Services.values().length) {
// new service or old one removed, technically an error
return ret;
}
return ret;
} catch (NullPointerException | JsonParseException e) {
if (Log.getLevel().ordinal() >= LogLevels.DEBUG.ordinal()) {
e.printStackTrace();
}
return getUnknownStatusMap();
}
}
static HashMap<Services, ServiceStatus> getUnknownStatusMap() {
HashMap<Services, ServiceStatus> ret = new HashMap<>();
for (Services service : Services.values()) {
ret.put(service, ServiceStatus.UNKNOWN);
}
return ret;
}
enum Services {
MINECRAFT_NET("minecraft.net"),
ACCOUNT("account.mojang.com"),
AUTH("auth.mojang.com"),
AUTHENTICATION_SERVER("authserver.mojang.com"),
SESSION_SERVER("sessionserver.mojang.com"),
API("api.mojang.com"),
TEXTURES("textures.minecraft.net"),
MOJANG_COM("mojang.com");
final String key;
Services(String key) {
this.key = key;
}
public static Services byKey(String key) {
for (Services service : values()) {
if (service.getKey().equals(key)) {
return service;
}
}
return null;
}
public String getKey() {
return key;
}
}
enum ServiceStatus {
GREEN,
YELLOW,
RED,
UNKNOWN;
public static ServiceStatus byKey(String key) {
for (ServiceStatus status : values()) {
if (status.name().equals(key)) {
return status;
}
}
return UNKNOWN;
}
}
}

View File

@ -14,7 +14,6 @@
package de.bixilon.minosoft.util.mojang.api;
public enum MojangURLs {
STATUS("https://status.mojang.com/check"),
BLOCKED_SERVERS("https://sessionserver.mojang.com/blockedservers"),
LOGIN("https://authserver.mojang.com/authenticate"),
JOIN("https://sessionserver.mojang.com/session/minecraft/join"),
@ -26,15 +25,6 @@ public enum MojangURLs {
this.url = url;
}
public static MojangURLs byUrl(String key) {
for (MojangURLs url : values()) {
if (url.getUrl().equals(key)) {
return url;
}
}
return null;
}
public String getUrl() {
return url;
}

View File

@ -66,7 +66,7 @@ public class CompoundTag extends NBTTag {
@Override
public void writeBytes(OutByteBuffer buffer) {
buffer.writeByte((byte) TagTypes.COMPOUND.getId());
buffer.writeByte((byte) TagTypes.COMPOUND.ordinal());
buffer.writeShort((short) name.length());
buffer.writeStringNoLength(name);
// now with prefixed name, etc it is technically the same as a subtag
@ -75,7 +75,7 @@ public class CompoundTag extends NBTTag {
public void writeBytesSubTag(OutByteBuffer buffer) {
for (Map.Entry<String, NBTTag> set : data.entrySet()) {
buffer.writeByte((byte) set.getValue().getType().getId());
buffer.writeByte((byte) set.getValue().getType().ordinal());
buffer.writeShort((short) set.getKey().length());
buffer.writeStringNoLength(set.getKey());

View File

@ -60,7 +60,7 @@ public class ListTag extends NBTTag {
@Override
public void writeBytes(OutByteBuffer buffer) {
new ByteTag((byte) type.getId()).writeBytes(buffer);
new ByteTag((byte) type.ordinal()).writeBytes(buffer);
new IntTag(list.size()).writeBytes(buffer);

View File

@ -14,36 +14,22 @@
package de.bixilon.minosoft.util.nbt.tag;
public enum TagTypes {
END(0),
BYTE(1),
SHORT(2),
INT(3),
LONG(4),
FLOAT(5),
DOUBLE(6),
BYTE_ARRAY(7),
STRING(8),
LIST(9),
COMPOUND(10),
INT_ARRAY(11),
LONG_ARRAY(12);
END,
BYTE,
SHORT,
INT,
LONG,
FLOAT,
DOUBLE,
BYTE_ARRAY,
STRING,
LIST,
COMPOUND,
INT_ARRAY,
LONG_ARRAY;
final int id;
TagTypes(int id) {
this.id = id;
}
public static TagTypes getById(int id) {
for (TagTypes state : values()) {
if (state.getId() == id) {
return state;
}
}
return null;
}
public int getId() {
return this.id;
return values()[id];
}
}