cleanup: replace all old switch statements with enhanced switch statements

This commit is contained in:
Bixilon 2020-10-01 17:10:48 +02:00
parent 6703c58295
commit cba9f9ed25
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 44 additions and 71 deletions

View File

@ -94,12 +94,12 @@ public class Minosoft {
if (!path.endsWith(File.separator)) { if (!path.endsWith(File.separator)) {
path += "/"; path += "/";
} }
switch (OSUtil.getOS()) { path += switch (OSUtil.getOS()) {
case LINUX -> path += ".local/share/minosoft/"; case LINUX -> ".local/share/minosoft/";
case WINDOWS -> path += "AppData/Roaming/Minosoft/"; case WINDOWS -> "AppData/Roaming/Minosoft/";
case MAC -> path += "Library/Application Support/Minosoft/"; case MAC -> "Library/Application Support/Minosoft/";
case OTHER -> path += ".minosoft/"; case OTHER -> ".minosoft/";
} };
File folder = new File(path); File folder = new File(path);
if (!folder.exists() && !folder.mkdirs()) { if (!folder.exists() && !folder.mkdirs()) {
// failed creating folder // failed creating folder

View File

@ -41,28 +41,20 @@ public class Pufferfish extends Mob implements MobInterface {
@Override @Override
public float getWidth() { public float getWidth() {
switch (metaData.getPufferState()) { return switch (metaData.getPufferState()) {
case UN_PUFFED: case UN_PUFFED -> 0.35F;
return 0.35F; case SEMI_PUFFED -> 0.5F;
case SEMI_PUFFED: case FULLY_PUFFED -> 0.7F;
return 0.5F; };
case FULLY_PUFFED:
return 0.7F;
}
return 0.35F;
} }
@Override @Override
public float getHeight() { public float getHeight() {
switch (metaData.getPufferState()) { return switch (metaData.getPufferState()) {
case UN_PUFFED: case UN_PUFFED -> 0.35F;
return 0.35F; case SEMI_PUFFED -> 0.5F;
case SEMI_PUFFED: case FULLY_PUFFED -> 0.7F;
return 0.5F; };
case FULLY_PUFFED:
return 0.7F;
}
return 0.35F;
} }
@Override @Override

View File

@ -55,15 +55,12 @@ public class Slime extends Mob implements MobInterface {
@Override @Override
public int getMaxHealth() { public int getMaxHealth() {
switch (metaData.getSize()) { return switch (metaData.getSize()) {
case 1: case 1 -> 1;
return 1; case 2 -> 4;
case 2: case 4 -> 16;
return 4; default -> 0;
case 4: };
return 16;
}
return 0;
} }
@Override @Override

View File

@ -156,7 +156,7 @@ public class Connection {
ConnectionStates previousState = this.state; ConnectionStates previousState = this.state;
this.state = state; this.state = state;
switch (state) { switch (state) {
case HANDSHAKING: case HANDSHAKING -> {
// connection established, starting threads and logging in // connection established, starting threads and logging in
startHandlingThread(); startHandlingThread();
ConnectionStates next = ((reason == ConnectionReasons.CONNECT) ? ConnectionStates.LOGIN : ConnectionStates.STATUS); ConnectionStates next = ((reason == ConnectionReasons.CONNECT) ? ConnectionStates.LOGIN : ConnectionStates.STATUS);
@ -168,19 +168,19 @@ public class Connection {
network.sendPacket(new PacketHandshake(address, next, (next == ConnectionStates.STATUS) ? -1 : getVersion().getProtocolVersion())); network.sendPacket(new PacketHandshake(address, next, (next == ConnectionStates.STATUS) ? -1 : getVersion().getProtocolVersion()));
// after sending it, switch to next state // after sending it, switch to next state
setConnectionState(next); setConnectionState(next);
break; }
case STATUS: case STATUS -> {
// send status request and ping // send status request and ping
network.sendPacket(new PacketStatusRequest()); network.sendPacket(new PacketStatusRequest());
connectionStatusPing = new ConnectionPing(); connectionStatusPing = new ConnectionPing();
network.sendPacket(new PacketStatusPing(connectionStatusPing)); network.sendPacket(new PacketStatusPing(connectionStatusPing));
break; }
case LOGIN: case LOGIN -> {
network.sendPacket(new PacketLoginStart(player)); network.sendPacket(new PacketLoginStart(player));
pluginChannelHandler = new PluginChannelHandler(this); pluginChannelHandler = new PluginChannelHandler(this);
registerDefaultChannels(); registerDefaultChannels();
break; }
case DISCONNECTED: case DISCONNECTED -> {
if (reason == ConnectionReasons.GET_VERSION) { if (reason == ConnectionReasons.GET_VERSION) {
setReason(ConnectionReasons.CONNECT); setReason(ConnectionReasons.CONNECT);
connect(); connect();
@ -188,8 +188,8 @@ public class Connection {
// unregister all custom recipes // unregister all custom recipes
Recipes.removeCustomRecipes(); Recipes.removeCustomRecipes();
} }
break; }
case FAILED: case FAILED -> {
// connect to next hostname, if available // connect to next hostname, if available
if (previousState == ConnectionStates.PLAY) { if (previousState == ConnectionStates.PLAY) {
// connection was good, do not reconnect // connection was good, do not reconnect
@ -205,10 +205,8 @@ public class Connection {
// no connection and no servers available anymore... sorry, but you can not play today :( // no connection and no servers available anymore... sorry, but you can not play today :(
handlePingCallbacks(null); handlePingCallbacks(null);
} }
break; }
case FAILED_NO_RETRY: case FAILED_NO_RETRY -> handlePingCallbacks(null);
handlePingCallbacks(null);
break;
} }
// handle callbacks // handle callbacks
connectionChangeCallbacks.forEach((callback -> callback.handle(this))); connectionChangeCallbacks.forEach((callback -> callback.handle(this)));

View File

@ -38,28 +38,20 @@ public class PacketBossBar implements ClientboundPacket {
uuid = buffer.readUUID(); uuid = buffer.readUUID();
action = BossBarActions.byId(buffer.readVarInt()); action = BossBarActions.byId(buffer.readVarInt());
switch (action) { switch (action) {
case ADD: case ADD -> {
title = buffer.readTextComponent(); title = buffer.readTextComponent();
health = buffer.readFloat(); health = buffer.readFloat();
color = BossBarColors.byId(buffer.readVarInt()); color = BossBarColors.byId(buffer.readVarInt());
divisions = BossBarDivisions.byId(buffer.readVarInt()); divisions = BossBarDivisions.byId(buffer.readVarInt());
flags = buffer.readByte(); flags = buffer.readByte();
break; }
case REMOVE: case UPDATE_HEALTH -> health = buffer.readFloat();
break; case UPDATE_TITLE -> title = buffer.readTextComponent();
case UPDATE_HEALTH: case UPDATE_STYLE -> {
health = buffer.readFloat();
break;
case UPDATE_TITLE:
title = buffer.readTextComponent();
break;
case UPDATE_STYLE:
color = BossBarColors.byId(buffer.readVarInt()); color = BossBarColors.byId(buffer.readVarInt());
divisions = BossBarDivisions.byId(buffer.readVarInt()); divisions = BossBarDivisions.byId(buffer.readVarInt());
break; }
case UPDATE_FLAGS: case UPDATE_FLAGS -> flags = buffer.readByte();
flags = buffer.readByte();
break;
} }
return true; return true;
} }

View File

@ -80,18 +80,12 @@ public class PacketHandler {
long pingDifference = System.currentTimeMillis() - ping.getSendingTime(); long pingDifference = System.currentTimeMillis() - ping.getSendingTime();
Log.debug(String.format("Pong received (ping=%dms, pingBars=%s)", pingDifference, PingBars.byPing(pingDifference))); Log.debug(String.format("Pong received (ping=%dms, pingBars=%s)", pingDifference, PingBars.byPing(pingDifference)));
switch (connection.getReason()) { switch (connection.getReason()) {
case PING: case PING -> connection.disconnect();// pong arrived, closing connection
// pong arrived, closing connection case GET_VERSION -> {
connection.disconnect();
break;
case GET_VERSION:
// reconnect... // reconnect...
connection.disconnect(); connection.disconnect();
Log.info(String.format("Server is running on version %s (%d), reconnecting...", connection.getVersion().getVersionName(), connection.getVersion().getProtocolVersion())); Log.info(String.format("Server is running on version %s (%d), reconnecting...", connection.getVersion().getVersionName(), connection.getVersion().getProtocolVersion()));
break; }
case CONNECT:
// do nothing
break;
} }
} }

View File

@ -2,7 +2,7 @@
version: 1 # do not edit this, used for migration between versions of config version: 1 # do not edit this, used for migration between versions of config
general: general:
log-level: DEBUG # possible values: GAME, FATAL, INFO, WARNING, DEBUG, VERBOSE, PROTOCOL log-level: WARNING # possible values: GAME, FATAL, INFO, WARNING, DEBUG, VERBOSE, PROTOCOL
# game # game
game: game: