Use more enhanced switches

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-10-22 02:14:12 +02:00
parent b308ce5baa
commit 5030664ff9
13 changed files with 112 additions and 247 deletions

View File

@ -79,18 +79,10 @@ public enum EntitySpawnType {
0
);
switch (paintingMeta.getDirection()) {
case SOUTH:
packet.direction = 0;
break;
case WEST:
packet.direction = 1;
break;
case NORTH:
packet.direction = 2;
break;
case EAST:
packet.direction = 3;
break;
case SOUTH -> packet.direction = 0;
case WEST -> packet.direction = 1;
case NORTH -> packet.direction = 2;
case EAST -> packet.direction = 3;
}
} else {
packet.position = Vec.ZERO;

View File

@ -24,15 +24,9 @@ public class PufferfishMeta extends AbstractFishMeta {
private void updateBoundingBox(State state) {
switch (state) {
case UNPUFFED:
setBoundingBox(.35D, .35D);
break;
case SEMI_PUFFED:
setBoundingBox(.5D, .5D);
break;
default:
setBoundingBox(.7D, .7D);
break;
case UNPUFFED -> setBoundingBox(.35D, .35D);
case SEMI_PUFFED -> setBoundingBox(.5D, .5D);
default -> setBoundingBox(.7D, .7D);
}
}

View File

@ -40,15 +40,9 @@ public class EnchantmentTableInventory extends Inventory {
*/
public void setLevelRequirement(EnchantmentSlot enchantmentSlot, short level) {
switch (enchantmentSlot) {
case TOP:
sendProperty(InventoryProperty.ENCHANTMENT_TABLE_LEVEL_REQUIREMENT_TOP, level);
break;
case MIDDLE:
sendProperty(InventoryProperty.ENCHANTMENT_TABLE_LEVEL_REQUIREMENT_MIDDLE, level);
break;
case BOTTOM:
sendProperty(InventoryProperty.ENCHANTMENT_TABLE_LEVEL_REQUIREMENT_BOTTOM, level);
break;
case TOP -> sendProperty(InventoryProperty.ENCHANTMENT_TABLE_LEVEL_REQUIREMENT_TOP, level);
case MIDDLE -> sendProperty(InventoryProperty.ENCHANTMENT_TABLE_LEVEL_REQUIREMENT_MIDDLE, level);
case BOTTOM -> sendProperty(InventoryProperty.ENCHANTMENT_TABLE_LEVEL_REQUIREMENT_BOTTOM, level);
}
this.levelRequirements[enchantmentSlot.ordinal()] = level;
}
@ -96,15 +90,9 @@ public class EnchantmentTableInventory extends Inventory {
public void setEnchantmentShown(EnchantmentSlot enchantmentSlot, Enchantment enchantment) {
final short id = enchantment == null ? -1 : (short) enchantment.id();
switch (enchantmentSlot) {
case TOP:
sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_ID_TOP, id);
break;
case MIDDLE:
sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_ID_MIDDLE, id);
break;
case BOTTOM:
sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_ID_BOTTOM, id);
break;
case TOP -> sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_ID_TOP, id);
case MIDDLE -> sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_ID_MIDDLE, id);
case BOTTOM -> sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_ID_BOTTOM, id);
}
this.enchantmentShown[enchantmentSlot.ordinal()] = id;
}
@ -129,15 +117,9 @@ public class EnchantmentTableInventory extends Inventory {
*/
public void setEnchantmentLevel(EnchantmentSlot enchantmentSlot, short level) {
switch (enchantmentSlot) {
case TOP:
sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_LEVEL_TOP, level);
break;
case MIDDLE:
sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_LEVEL_MIDDLE, level);
break;
case BOTTOM:
sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_LEVEL_BOTTOM, level);
break;
case TOP -> sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_LEVEL_TOP, level);
case MIDDLE -> sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_LEVEL_MIDDLE, level);
case BOTTOM -> sendProperty(InventoryProperty.ENCHANTMENT_TABLE_ENCH_LEVEL_BOTTOM, level);
}
this.enchantmentLevel[enchantmentSlot.ordinal()] = level;
}

View File

@ -10,21 +10,12 @@ public class EntityActionListener {
public static void listener(ClientEntityActionPacket packet, Player player) {
ClientEntityActionPacket.Action action = packet.action;
switch (action) {
case START_SNEAKING:
EntityActionListener.setSneaking(player, true);
break;
case STOP_SNEAKING:
EntityActionListener.setSneaking(player, false);
break;
case START_SPRINTING:
EntityActionListener.setSprinting(player, true);
break;
case STOP_SPRINTING:
EntityActionListener.setSprinting(player, false);
break;
case START_FLYING_ELYTRA:
EntityActionListener.startFlyingElytra(player);
break;
case START_SNEAKING -> EntityActionListener.setSneaking(player, true);
case STOP_SNEAKING -> EntityActionListener.setSneaking(player, false);
case START_SPRINTING -> EntityActionListener.setSprinting(player, true);
case STOP_SPRINTING -> EntityActionListener.setSprinting(player, false);
case START_FLYING_ELYTRA -> EntityActionListener.startFlyingElytra(player);
// TODO do remaining actions
}
}

View File

@ -13,13 +13,10 @@ public class StatusListener {
public static void listener(ClientStatusPacket packet, Player player) {
switch (packet.action) {
case PERFORM_RESPAWN:
player.respawn();
break;
case REQUEST_STATS:
case PERFORM_RESPAWN -> player.respawn();
case REQUEST_STATS -> {
List<StatisticsPacket.Statistic> statisticList = new ArrayList<>();
StatisticsPacket statisticsPacket = new StatisticsPacket();
final Map<PlayerStatistic, Integer> playerStatisticValueMap = player.getStatisticValueMap();
for (var entry : playerStatisticValueMap.entrySet()) {
final PlayerStatistic playerStatistic = entry.getKey();
@ -32,11 +29,9 @@ public class StatusListener {
statisticList.add(statistic);
}
statisticsPacket.statistics = statisticList.toArray(new StatisticsPacket.Statistic[0]);
player.getPlayerConnection().sendPacket(statisticsPacket);
break;
}
}
}

View File

@ -8,8 +8,8 @@ import net.minestom.server.network.packet.client.handler.ClientLoginPacketsHandl
import net.minestom.server.network.packet.client.handler.ClientPlayPacketsHandler;
import net.minestom.server.network.packet.client.handler.ClientStatusPacketsHandler;
import net.minestom.server.network.packet.client.handshake.HandshakePacket;
import net.minestom.server.network.player.PlayerSocketConnection;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.network.player.PlayerSocketConnection;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.Readable;
import org.jetbrains.annotations.NotNull;
@ -59,23 +59,23 @@ public final class PacketProcessor {
return;
}
switch (connectionState) {
case PLAY:
case PLAY -> {
final Player player = playerConnection.getPlayer();
ClientPlayPacket playPacket = (ClientPlayPacket) playPacketsHandler.getPacketInstance(packetId);
safeRead(playerConnection, playPacket, binaryReader);
assert player != null;
player.addPacketToQueue(playPacket);
break;
case LOGIN:
}
case LOGIN -> {
final ClientPreplayPacket loginPacket = (ClientPreplayPacket) loginPacketsHandler.getPacketInstance(packetId);
safeRead(playerConnection, loginPacket, binaryReader);
loginPacket.process(playerConnection);
break;
case STATUS:
}
case STATUS -> {
final ClientPreplayPacket statusPacket = (ClientPreplayPacket) statusPacketsHandler.getPacketInstance(packetId);
safeRead(playerConnection, statusPacket, binaryReader);
statusPacket.process(playerConnection);
break;
}
}
}

View File

@ -100,42 +100,30 @@ public class DeclareCommandsPacket implements ServerPacket {
}
private byte[] getProperties(BinaryReader reader, String parser) {
switch (parser) {
case "brigadier:double":
return reader.extractBytes(() -> {
byte flags = reader.readByte();
if ((flags & 0x01) == 0x01) {
reader.readDouble(); // min
}
if ((flags & 0x02) == 0x02) {
reader.readDouble(); // max
}
});
case "brigadier:integer":
return reader.extractBytes(() -> {
byte flags = reader.readByte();
if ((flags & 0x01) == 0x01) {
reader.readInt(); // min
}
if ((flags & 0x02) == 0x02) {
reader.readInt(); // max
}
});
case "brigadier:string":
return reader.extractBytes(reader::readVarInt);
case "brigadier:entity":
case "brigadier:score_holder":
return reader.extractBytes(reader::readByte);
case "brigadier:range":
return reader.extractBytes(reader::readBoolean); // https://wiki.vg/Command_Data#minecraft:range, looks fishy
default:
return new byte[0]; // unknown
}
return switch (parser) {
case "brigadier:double" -> reader.extractBytes(() -> {
byte flags = reader.readByte();
if ((flags & 0x01) == 0x01) {
reader.readDouble(); // min
}
if ((flags & 0x02) == 0x02) {
reader.readDouble(); // max
}
});
case "brigadier:integer" -> reader.extractBytes(() -> {
byte flags = reader.readByte();
if ((flags & 0x01) == 0x01) {
reader.readInt(); // min
}
if ((flags & 0x02) == 0x02) {
reader.readInt(); // max
}
});
case "brigadier:string" -> reader.extractBytes(reader::readVarInt);
case "brigadier:entity", "brigadier:score_holder" -> reader.extractBytes(reader::readByte);
case "brigadier:range" -> reader.extractBytes(reader::readBoolean); // https://wiki.vg/Command_Data#minecraft:range, looks fishy
default -> new byte[0]; // unknown
};
}
private boolean isLiteral() {

View File

@ -36,40 +36,15 @@ public class DeclareRecipesPacket implements ServerPacket {
String id = reader.readSizedString();
switch (type) {
case "crafting_shapeless":
recipes[i] = new DeclaredShapelessCraftingRecipe(id, reader);
break;
case "crafting_shaped":
recipes[i] = new DeclaredShapedCraftingRecipe(id, reader);
break;
case "smelting":
recipes[i] = new DeclaredSmeltingRecipe(id, reader);
break;
case "blasting":
recipes[i] = new DeclaredBlastingRecipe(id, reader);
break;
case "smoking":
recipes[i] = new DeclaredSmokingRecipe(id, reader);
break;
case "campfire_cooking":
recipes[i] = new DeclaredCampfireCookingRecipe(id, reader);
break;
case "stonecutter":
recipes[i] = new DeclaredStonecutterRecipe(id, reader);
break;
case "smithing":
recipes[i] = new DeclaredSmithingRecipe(id, reader);
break;
default:
throw new UnsupportedOperationException("Unrecognized type: " + type + " (id is " + id + ")");
case "crafting_shapeless" -> recipes[i] = new DeclaredShapelessCraftingRecipe(id, reader);
case "crafting_shaped" -> recipes[i] = new DeclaredShapedCraftingRecipe(id, reader);
case "smelting" -> recipes[i] = new DeclaredSmeltingRecipe(id, reader);
case "blasting" -> recipes[i] = new DeclaredBlastingRecipe(id, reader);
case "smoking" -> recipes[i] = new DeclaredSmokingRecipe(id, reader);
case "campfire_cooking" -> recipes[i] = new DeclaredCampfireCookingRecipe(id, reader);
case "stonecutter" -> recipes[i] = new DeclaredStonecutterRecipe(id, reader);
case "smithing" -> recipes[i] = new DeclaredSmithingRecipe(id, reader);
default -> throw new UnsupportedOperationException("Unrecognized type: " + type + " (id is " + id + ")");
}
}
}

View File

@ -49,28 +49,13 @@ public class PlayerInfoPacket implements ComponentHoldingServerPacket {
for (int i = 0; i < playerInfoCount; i++) {
UUID uuid = reader.readUuid();
PlayerInfo info;
switch (action) {
case ADD_PLAYER:
info = new AddPlayer(uuid, reader);
break;
case UPDATE_GAMEMODE:
info = new UpdateGamemode(uuid, reader);
break;
case UPDATE_LATENCY:
info = new UpdateLatency(uuid, reader);
break;
case UPDATE_DISPLAY_NAME:
info = new UpdateDisplayName(uuid, reader);
break;
case REMOVE_PLAYER:
info = new RemovePlayer(uuid);
break;
default:
throw new IllegalArgumentException("Unsupported action encountered: " + action.name());
}
PlayerInfo info = switch (action) {
case ADD_PLAYER -> new AddPlayer(uuid, reader);
case UPDATE_GAMEMODE -> new UpdateGamemode(uuid, reader);
case UPDATE_LATENCY -> new UpdateLatency(uuid, reader);
case UPDATE_DISPLAY_NAME -> new UpdateDisplayName(uuid, reader);
case REMOVE_PLAYER -> new RemovePlayer(uuid);
};
playerInfos.set(i, info);
}
}

View File

@ -42,7 +42,7 @@ public class RecipeManager {
List<DeclareRecipesPacket.DeclaredRecipe> recipesCache = new ArrayList<>();
for (Recipe recipe : recipes) {
switch (recipe.recipeType) {
case SHAPELESS: {
case SHAPELESS -> {
ShapelessRecipe shapelessRecipe = (ShapelessRecipe) recipe;
recipesCache.add(
new DeclareRecipesPacket.DeclaredShapelessCraftingRecipe(
@ -52,9 +52,8 @@ public class RecipeManager {
shapelessRecipe.getResult()
)
);
break;
}
case SHAPED: {
case SHAPED -> {
ShapedRecipe shapedRecipe = (ShapedRecipe) recipe;
recipesCache.add(
new DeclareRecipesPacket.DeclaredShapedCraftingRecipe(
@ -66,9 +65,8 @@ public class RecipeManager {
shapedRecipe.getResult()
)
);
break;
}
case SMELTING: {
case SMELTING -> {
SmeltingRecipe smeltingRecipe = (SmeltingRecipe) recipe;
recipesCache.add(
new DeclareRecipesPacket.DeclaredSmeltingRecipe(
@ -80,9 +78,8 @@ public class RecipeManager {
smeltingRecipe.getCookingTime()
)
);
break;
}
case BLASTING: {
case BLASTING -> {
BlastingRecipe blastingRecipe = (BlastingRecipe) recipe;
recipesCache.add(
new DeclareRecipesPacket.DeclaredBlastingRecipe(
@ -94,9 +91,8 @@ public class RecipeManager {
blastingRecipe.getCookingTime()
)
);
break;
}
case SMOKING: {
case SMOKING -> {
SmokingRecipe smokingRecipe = (SmokingRecipe) recipe;
recipesCache.add(
new DeclareRecipesPacket.DeclaredSmokingRecipe(
@ -108,9 +104,8 @@ public class RecipeManager {
smokingRecipe.getCookingTime()
)
);
break;
}
case CAMPFIRE_COOKING: {
case CAMPFIRE_COOKING -> {
CampfireCookingRecipe campfireCookingRecipe = (CampfireCookingRecipe) recipe;
recipesCache.add(
new DeclareRecipesPacket.DeclaredCampfireCookingRecipe(
@ -122,9 +117,8 @@ public class RecipeManager {
campfireCookingRecipe.getCookingTime()
)
);
break;
}
case STONECUTTING: {
case STONECUTTING -> {
StonecutterRecipe stonecuttingRecipe = (StonecutterRecipe) recipe;
recipesCache.add(
new DeclareRecipesPacket.DeclaredStonecutterRecipe(
@ -134,9 +128,8 @@ public class RecipeManager {
stonecuttingRecipe.getResult()
)
);
break;
}
case SMITHING: {
case SMITHING -> {
SmithingRecipe smithingRecipe = (SmithingRecipe) recipe;
recipesCache.add(
new DeclareRecipesPacket.DeclaredSmithingRecipe(
@ -146,7 +139,6 @@ public class RecipeManager {
smithingRecipe.getResult()
)
);
break;
}
}
}

View File

@ -115,32 +115,17 @@ public class StringUtils {
continue;
}
switch (nextChar) {
case '\\':
ch = '\\';
break;
case 'b':
ch = '\b';
break;
case 'f':
ch = '\f';
break;
case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
case 't':
ch = '\t';
break;
case '\"':
ch = '\"';
break;
case '\'':
ch = '\'';
break;
case '\\' -> ch = '\\';
case 'b' -> ch = '\b';
case 'f' -> ch = '\f';
case 'n' -> ch = '\n';
case 'r' -> ch = '\r';
case 't' -> ch = '\t';
case '\"' -> ch = '\"';
case '\'' -> ch = '\'';
// Hex Unicode: u????
case 'u':
case 'u' -> {
if (i >= st.length() - 5) {
ch = 'u';
break;
@ -151,6 +136,7 @@ public class StringUtils {
sb.append(Character.toChars(code));
i += 5;
continue;
}
}
i++;
}

View File

@ -220,22 +220,16 @@ public class EntityFinder {
// Sort & limit
if (entitySort != EntitySort.ARBITRARY || limit != null) {
result = result.stream()
.sorted((ent1, ent2) -> {
switch (entitySort) {
case ARBITRARY:
case RANDOM:
.sorted((ent1, ent2) -> switch (entitySort) {
case ARBITRARY, RANDOM ->
// RANDOM is handled below
return 1;
case FURTHEST:
return pos.distance(ent1.getPosition()) >
pos.distance(ent2.getPosition()) ?
1 : 0;
case NEAREST:
return pos.distance(ent1.getPosition()) <
pos.distance(ent2.getPosition()) ?
1 : 0;
}
return 1;
1;
case FURTHEST -> pos.distance(ent1.getPosition()) >
pos.distance(ent2.getPosition()) ?
1 : 0;
case NEAREST -> pos.distance(ent1.getPosition()) <
pos.distance(ent2.getPosition()) ?
1 : 0;
})
.limit(limit != null ? limit : Integer.MAX_VALUE)
.collect(Collectors.toList());

View File

@ -29,28 +29,19 @@ public final class PlayerInventoryUtils {
* @return a packet which can be use internally with Minestom
*/
public static int convertPlayerInventorySlot(int slot, int offset) {
switch (slot) {
case 0:
return CRAFT_RESULT;
case 1:
return CRAFT_SLOT_1;
case 2:
return CRAFT_SLOT_2;
case 3:
return CRAFT_SLOT_3;
case 4:
return CRAFT_SLOT_4;
case 5:
return HELMET_SLOT;
case 6:
return CHESTPLATE_SLOT;
case 7:
return LEGGINGS_SLOT;
case 8:
return BOOTS_SLOT;
}
return switch (slot) {
case 0 -> CRAFT_RESULT;
case 1 -> CRAFT_SLOT_1;
case 2 -> CRAFT_SLOT_2;
case 3 -> CRAFT_SLOT_3;
case 4 -> CRAFT_SLOT_4;
case 5 -> HELMET_SLOT;
case 6 -> CHESTPLATE_SLOT;
case 7 -> LEGGINGS_SLOT;
case 8 -> BOOTS_SLOT;
default -> convertSlot(slot, offset);
};
return convertSlot(slot, offset);
}
public static int convertSlot(int slot, int offset) {