Use enhanced switches in more places (#4043)

This commit is contained in:
RK_01 2024-07-24 14:32:36 +02:00 committed by GitHub
parent 2b508567db
commit b1f64fd081
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 120 additions and 205 deletions

View File

@ -2,7 +2,7 @@
charset = utf-8
indent_size = 4
indent_style = space
insert_final_newline = false
insert_final_newline = true
tab_width = 4
[*.java]
@ -13,4 +13,4 @@ ij_java_generate_final_locals = true
ij_java_generate_final_parameters = true
[{*.json,*.yml}]
indent_size = 2
indent_size = 2

View File

@ -246,7 +246,7 @@ public class CommonBoss implements BossBar {
wrapper.write(Types.UUID, uuid);
wrapper.write(Types.VAR_INT, action.getId());
switch (action) {
case ADD:
case ADD -> {
try {
wrapper.write(Types.COMPONENT, JsonParser.parseString(this.title));
} catch (Exception e) {
@ -256,26 +256,22 @@ public class CommonBoss implements BossBar {
wrapper.write(Types.VAR_INT, color.getId());
wrapper.write(Types.VAR_INT, style.getId());
wrapper.write(Types.BYTE, (byte) flagToBytes());
break;
case REMOVE:
break;
case UPDATE_HEALTH:
wrapper.write(Types.FLOAT, health);
break;
case UPDATE_TITLE:
}
case REMOVE -> {
}
case UPDATE_HEALTH -> wrapper.write(Types.FLOAT, health);
case UPDATE_TITLE -> {
try {
wrapper.write(Types.COMPONENT, JsonParser.parseString(this.title));
} catch (Exception e) {
wrapper.write(Types.COMPONENT, ComponentUtil.plainToJson(this.title));
}
break;
case UPDATE_STYLE:
}
case UPDATE_STYLE -> {
wrapper.write(Types.VAR_INT, color.getId());
wrapper.write(Types.VAR_INT, style.getId());
break;
case UPDATE_FLAGS:
wrapper.write(Types.BYTE, (byte) flagToBytes());
break;
}
case UPDATE_FLAGS -> wrapper.write(Types.BYTE, (byte) flagToBytes());
}
return wrapper;

View File

@ -779,7 +779,7 @@ public class Protocol1_12_2To1_13 extends AbstractProtocol<ClientboundPackets1_1
// Clone item arrays because array and item are mutable, also make sure the array type is the interface
switch (recipe.type()) {
case "crafting_shapeless": {
case "crafting_shapeless" -> {
recipesPacket.write(Types.STRING, recipe.group());
recipesPacket.write(Types.VAR_INT, recipe.ingredients().length);
for (Item[] ingredient : recipe.ingredients()) {
@ -791,9 +791,8 @@ public class Protocol1_12_2To1_13 extends AbstractProtocol<ClientboundPackets1_1
recipesPacket.write(Types.ITEM1_13_ARRAY, clone);
}
recipesPacket.write(Types.ITEM1_13, recipe.result().copy());
break;
}
case "crafting_shaped": {
case "crafting_shaped" -> {
recipesPacket.write(Types.VAR_INT, recipe.width());
recipesPacket.write(Types.VAR_INT, recipe.height());
recipesPacket.write(Types.STRING, recipe.group());
@ -806,9 +805,8 @@ public class Protocol1_12_2To1_13 extends AbstractProtocol<ClientboundPackets1_1
recipesPacket.write(Types.ITEM1_13_ARRAY, clone);
}
recipesPacket.write(Types.ITEM1_13, recipe.result().copy());
break;
}
case "smelting": {
case "smelting" -> {
recipesPacket.write(Types.STRING, recipe.group());
Item[] ingredient = new Item[recipe.ingredient().length];
for (int i = 0; i < ingredient.length; i++) {
@ -819,7 +817,6 @@ public class Protocol1_12_2To1_13 extends AbstractProtocol<ClientboundPackets1_1
recipesPacket.write(Types.ITEM1_13, recipe.result().copy());
recipesPacket.write(Types.FLOAT, recipe.experience());
recipesPacket.write(Types.VAR_INT, recipe.cookingTime());
break;
}
}
}
@ -922,4 +919,4 @@ public class Protocol1_12_2To1_13 extends AbstractProtocol<ClientboundPackets1_1
public ComponentRewriter1_13 getComponentRewriter() {
return componentRewriter;
}
}
}

View File

@ -61,23 +61,14 @@ public class StairConnectionHandler implements ConnectionHandler {
byte shape;
switch (blockData.getValue("shape")) {
case "straight":
shape = 0;
break;
case "inner_left":
shape = 1;
break;
case "inner_right":
shape = 2;
break;
case "outer_left":
shape = 3;
break;
case "outer_right":
shape = 4;
break;
default:
case "straight" -> shape = 0;
case "inner_left" -> shape = 1;
case "inner_right" -> shape = 2;
case "outer_left" -> shape = 3;
case "outer_right" -> shape = 4;
default -> {
return;
}
}
StairData stairData = new StairData(

View File

@ -453,30 +453,22 @@ public class ItemPacketRewriter1_13 extends ItemRewriter<ClientboundPackets1_12_
public static String getNewPluginChannelId(String old) {
// Default channels that should not be modifiable
switch (old) {
case "MC|TrList":
return "minecraft:trader_list";
case "MC|Brand":
return "minecraft:brand";
case "MC|BOpen":
return "minecraft:book_open";
case "MC|DebugPath":
return "minecraft:debug/paths";
case "MC|DebugNeighborsUpdate":
return "minecraft:debug/neighbors_update";
case "REGISTER":
return "minecraft:register";
case "UNREGISTER":
return "minecraft:unregister";
case "BungeeCord":
return "bungeecord:main";
case "bungeecord:main":
return null;
default:
return switch (old) {
case "MC|TrList" -> "minecraft:trader_list";
case "MC|Brand" -> "minecraft:brand";
case "MC|BOpen" -> "minecraft:book_open";
case "MC|DebugPath" -> "minecraft:debug/paths";
case "MC|DebugNeighborsUpdate" -> "minecraft:debug/neighbors_update";
case "REGISTER" -> "minecraft:register";
case "UNREGISTER" -> "minecraft:unregister";
case "BungeeCord" -> "bungeecord:main";
case "bungeecord:main" -> null;
default -> {
String mappedChannel = Protocol1_12_2To1_13.MAPPINGS.getChannelMappings().get(old);
if (mappedChannel != null) return mappedChannel;
return MappingData1_13.validateNewChannel(old);
}
if (mappedChannel != null) yield mappedChannel;
yield MappingData1_13.validateNewChannel(old);
}
};
}
@Override
@ -687,28 +679,21 @@ public class ItemPacketRewriter1_13 extends ItemRewriter<ClientboundPackets1_12_
if (newId == null) return null;
// Default channels that should not be modifiable
switch (newId) {
case "minecraft:trader_list":
return "MC|TrList";
case "minecraft:book_open":
return "MC|BOpen";
case "minecraft:debug/paths":
return "MC|DebugPath";
case "minecraft:debug/neighbors_update":
return "MC|DebugNeighborsUpdate";
case "minecraft:register":
return "REGISTER";
case "minecraft:unregister":
return "UNREGISTER";
case "minecraft:brand":
return "MC|Brand";
case "bungeecord:main":
return "BungeeCord";
default:
return switch (newId) {
case "minecraft:trader_list" -> "MC|TrList";
case "minecraft:book_open" -> "MC|BOpen";
case "minecraft:debug/paths" -> "MC|DebugPath";
case "minecraft:debug/neighbors_update" -> "MC|DebugNeighborsUpdate";
case "minecraft:register" -> "REGISTER";
case "minecraft:unregister" -> "UNREGISTER";
case "minecraft:brand" -> "MC|Brand";
case "bungeecord:main" -> "BungeeCord";
default -> {
String mappedChannel = Protocol1_12_2To1_13.MAPPINGS.getChannelMappings().inverse().get(newId);
if (mappedChannel != null) return mappedChannel;
return newId.length() > 20 ? newId.substring(0, 20) : newId;
}
if (mappedChannel != null) yield mappedChannel;
yield newId.length() > 20 ? newId.substring(0, 20) : newId;
}
};
}
public static boolean isDamageable(int id) {
@ -724,4 +709,4 @@ public class ItemPacketRewriter1_13 extends ItemRewriter<ClientboundPackets1_12_
|| id == 442 // shield
|| id == 443; // elytra
}
}
}

View File

@ -86,42 +86,21 @@ public class ItemPacketRewriter1_14 extends ItemRewriter<ClientboundPackets1_13,
int typeId = -1;
switch (type) {
case "minecraft:crafting_table":
typeId = 11;
break;
case "minecraft:furnace":
typeId = 13;
break;
case "minecraft:dropper", "minecraft:dispenser":
typeId = 6;
break;
case "minecraft:enchanting_table":
typeId = 12;
break;
case "minecraft:brewing_stand":
typeId = 10;
break;
case "minecraft:villager":
typeId = 18;
break;
case "minecraft:beacon":
typeId = 8;
break;
case "minecraft:anvil":
typeId = 7;
break;
case "minecraft:hopper":
typeId = 15;
break;
case "minecraft:shulker_box":
typeId = 19;
break;
case "minecraft:container", "minecraft:chest":
default:
case "minecraft:crafting_table" -> typeId = 11;
case "minecraft:furnace" -> typeId = 13;
case "minecraft:dropper", "minecraft:dispenser" -> typeId = 6;
case "minecraft:enchanting_table" -> typeId = 12;
case "minecraft:brewing_stand" -> typeId = 10;
case "minecraft:villager" -> typeId = 18;
case "minecraft:beacon" -> typeId = 8;
case "minecraft:anvil" -> typeId = 7;
case "minecraft:hopper" -> typeId = 15;
case "minecraft:shulker_box" -> typeId = 19;
default -> {
if (slots > 0 && slots <= 54) {
typeId = slots / 9 - 1;
}
break;
}
}
if (typeId == -1) {

View File

@ -51,22 +51,23 @@ public class EntityPacketRewriter1_16 extends EntityRewriter<ClientboundPackets1
String dimensionName;
String outputName;
switch (dimension) {
case -1:
case -1 -> {
dimensionName = "minecraft:the_nether";
outputName = map.nether();
break;
case 0:
}
case 0 -> {
dimensionName = "minecraft:overworld";
outputName = map.overworld();
break;
case 1:
}
case 1 -> {
dimensionName = "minecraft:the_end";
outputName = map.end();
break;
default:
}
default -> {
protocol.getLogger().warning("Invalid dimension id: " + dimension);
dimensionName = "minecraft:overworld";
outputName = map.overworld();
}
}
wrapper.write(Types.STRING, dimensionName); // dimension
@ -231,4 +232,4 @@ public class EntityPacketRewriter1_16 extends EntityRewriter<ClientboundPackets1
public EntityType typeFromId(int type) {
return EntityTypes1_16.getTypeFromId(type);
}
}
}

View File

@ -19,7 +19,6 @@ package com.viaversion.viaversion.protocols.v1_19_1to1_19_3.rewriter;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18;
import com.viaversion.viaversion.protocols.v1_19_1to1_19_3.Protocol1_19_1To1_19_3;
@ -66,7 +65,7 @@ public final class ItemPacketRewriter1_19_3 extends ItemRewriter<ClientboundPack
final String type = Key.stripMinecraftNamespace(wrapper.passthrough(Types.STRING));
wrapper.passthrough(Types.STRING); // Recipe Identifier
switch (type) {
case "crafting_shapeless": {
case "crafting_shapeless" -> {
wrapper.passthrough(Types.STRING); // Group
wrapper.write(Types.VAR_INT, MISC_CRAFTING_BOOK_CATEGORY);
final int ingredients = wrapper.passthrough(Types.VAR_INT);
@ -77,9 +76,8 @@ public final class ItemPacketRewriter1_19_3 extends ItemRewriter<ClientboundPack
}
}
handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_13_2)); // Result
break;
}
case "crafting_shaped": {
case "crafting_shaped" -> {
final int ingredients = wrapper.passthrough(Types.VAR_INT) * wrapper.passthrough(Types.VAR_INT);
wrapper.passthrough(Types.STRING); // Group
wrapper.write(Types.VAR_INT, MISC_CRAFTING_BOOK_CATEGORY);
@ -90,12 +88,8 @@ public final class ItemPacketRewriter1_19_3 extends ItemRewriter<ClientboundPack
}
}
handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_13_2)); // Result
break;
}
case "smelting":
case "campfire_cooking":
case "blasting":
case "smoking":
case "smelting", "campfire_cooking", "blasting", "smoking" -> {
wrapper.passthrough(Types.STRING); // Group
wrapper.write(Types.VAR_INT, MISC_CRAFTING_BOOK_CATEGORY);
final Item[] items = wrapper.passthrough(Types.ITEM1_13_2_ARRAY); // Ingredients
@ -105,25 +99,12 @@ public final class ItemPacketRewriter1_19_3 extends ItemRewriter<ClientboundPack
handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_13_2)); // Result
wrapper.passthrough(Types.FLOAT); // EXP
wrapper.passthrough(Types.VAR_INT); // Cooking time
break;
case "crafting_special_armordye":
case "crafting_special_bookcloning":
case "crafting_special_mapcloning":
case "crafting_special_mapextending":
case "crafting_special_firework_rocket":
case "crafting_special_firework_star":
case "crafting_special_firework_star_fade":
case "crafting_special_tippedarrow":
case "crafting_special_bannerduplicate":
case "crafting_special_shielddecoration":
case "crafting_special_shulkerboxcoloring":
case "crafting_special_suspiciousstew":
case "crafting_special_repairitem":
wrapper.write(Types.VAR_INT, MISC_CRAFTING_BOOK_CATEGORY);
break;
default:
recipeRewriter.handleRecipeType(wrapper, type);
break;
}
case "crafting_special_armordye", "crafting_special_bookcloning", "crafting_special_mapcloning", "crafting_special_mapextending",
"crafting_special_firework_rocket", "crafting_special_firework_star", "crafting_special_firework_star_fade", "crafting_special_tippedarrow",
"crafting_special_bannerduplicate", "crafting_special_shielddecoration", "crafting_special_shulkerboxcoloring", "crafting_special_suspiciousstew",
"crafting_special_repairitem" -> wrapper.write(Types.VAR_INT, MISC_CRAFTING_BOOK_CATEGORY);
default -> recipeRewriter.handleRecipeType(wrapper, type);
}
}
});

View File

@ -399,19 +399,13 @@ public final class Protocol1_19To1_19_1 extends AbstractProtocol<ClientboundPack
for (final StringTag element : parameters) {
JsonElement argument = null;
switch (element.getValue()) {
case "sender":
argument = senderName;
break;
case "content":
argument = message;
break;
case "team_name":
case "target": // So that this method can also be used in VB
case "sender" -> argument = senderName;
case "content" -> argument = message;
case "team_name", "target" /*So that this method can also be used in VB*/ -> {
Preconditions.checkNotNull(targetName, "Team name is null");
argument = targetName;
break;
default:
LOGGER.warning("Unknown parameter for chat decoration: " + element.getValue());
}
default -> LOGGER.warning("Unknown parameter for chat decoration: " + element.getValue());
}
if (argument != null) {
arguments.add(SerializerVersion.V1_18.toComponent(argument));

View File

@ -67,15 +67,16 @@ public class Protocol1_8To1_9 extends AbstractProtocol<ClientboundPackets1_8, Cl
}
public static boolean isSword(int id) {
return switch (id) {
case 267, // Iron sword
268, // Wooden sword
272, // Stone sword
276, // Diamond sword
283 // Gold sword
-> true;
default -> false;
};
switch (id) {
case 267: // Iron sword
case 268: // Wooden sword
case 272: // Stone sword
case 276: // Diamond sword
case 283: // Gold sword
return true;
default:
return false;
}
}
@Override

View File

@ -23,10 +23,10 @@ import com.viaversion.viaversion.api.minecraft.EulerAngle;
import com.viaversion.viaversion.api.minecraft.Vector;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_9;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.entitydata.EntityData;
import com.viaversion.viaversion.api.minecraft.entitydata.EntityDataType;
import com.viaversion.viaversion.api.minecraft.entitydata.types.EntityDataTypes1_8;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
@ -405,7 +405,7 @@ public class EntityPacketRewriter1_9 extends EntityRewriter<ClientboundPackets1_
Object value = data.getValue();
switch (dataIndex.getNewType()) {
case BYTE:
case BYTE -> {
// convert from int, byte
if (dataIndex.getOldType() == EntityDataTypes1_8.BYTE) {
data.setValue(value);
@ -423,8 +423,8 @@ public class EntityPacketRewriter1_9 extends EntityRewriter<ClientboundPackets1_
EntityDataType dataType = EntityDataIndex1_9.PLAYER_HAND.getNewType();
event.createExtraData(new EntityData(newIndex, dataType, val));
}
break;
case OPTIONAL_UUID:
}
case OPTIONAL_UUID -> {
String owner = (String) value;
UUID toWrite = null;
if (!owner.isEmpty()) {
@ -434,8 +434,8 @@ public class EntityPacketRewriter1_9 extends EntityRewriter<ClientboundPackets1_
}
}
data.setValue(toWrite);
break;
case VAR_INT:
}
case VAR_INT -> {
// convert from int, short, byte
if (dataIndex.getOldType() == EntityDataTypes1_8.BYTE) {
data.setValue(((Byte) value).intValue());
@ -446,39 +446,33 @@ public class EntityPacketRewriter1_9 extends EntityRewriter<ClientboundPackets1_
if (dataIndex.getOldType() == EntityDataTypes1_8.INT) {
data.setValue(value);
}
break;
case FLOAT, STRING:
data.setValue(value);
break;
case BOOLEAN:
}
case FLOAT, STRING -> data.setValue(value);
case BOOLEAN -> {
if (dataIndex == EntityDataIndex1_9.ABSTRACT_AGEABLE_AGE)
data.setValue((Byte) value < 0);
else
data.setValue((Byte) value != 0);
break;
case ITEM:
}
case ITEM -> {
data.setValue(value);
protocol.getItemRewriter().handleItemToClient(event.user(), (Item) data.getValue());
break;
case BLOCK_POSITION:
}
case BLOCK_POSITION -> {
Vector vector = (Vector) value;
data.setValue(vector);
break;
case ROTATIONS:
}
case ROTATIONS -> {
EulerAngle angle = (EulerAngle) value;
data.setValue(angle);
break;
case COMPONENT:
}
case COMPONENT -> {
// Was previously also a component, so just convert it
String text = (String) value;
data.setValue(ComponentUtil.convertJsonOrEmpty(text, SerializerVersion.V1_8, SerializerVersion.V1_9));
break;
case OPTIONAL_BLOCK_STATE:
// Convert from int, short, byte
data.setValue(((Number) value).intValue());
break;
default:
throw new RuntimeException("Unhandled EntityDataType: " + dataIndex.getNewType());
}
case OPTIONAL_BLOCK_STATE -> data.setValue(((Number) value).intValue()); // Convert from int, short, byte
default -> throw new RuntimeException("Unhandled EntityDataType: " + dataIndex.getNewType());
}
}

View File

@ -161,12 +161,8 @@ public class ComponentRewriter<C extends ClientboundPacketType> implements com.v
public void passthroughAndProcess(final PacketWrapper wrapper) {
switch (type) {
case JSON:
processText(wrapper.user(), wrapper.passthrough(Types.COMPONENT));
break;
case NBT:
processTag(wrapper.user(), wrapper.passthrough(Types.TAG));
break;
case JSON -> processText(wrapper.user(), wrapper.passthrough(Types.COMPONENT));
case NBT -> processTag(wrapper.user(), wrapper.passthrough(Types.TAG));
}
}