From 9931c8a80b79c61046c8272acaa375a8b7c7a4e9 Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Fri, 24 Feb 2023 05:21:27 +0000 Subject: [PATCH] Provide API for getting the wall variant of blocks (#255) ## Description This adds an API for getting the wall / attachable variant of a block. Unlike #117 it does not add convenience methods for orientation, and it does not modify the example to use this code. ## Test Plan The extractor is not tested. The API has some tests in the test module. #### Related #115 - it does not fix this, as it doesn't modify the example, but it could lead to it. --------- Co-authored-by: EmperialDev --- crates/valence_protocol/build/block.rs | 27 +++++++++ crates/valence_protocol/src/block.rs | 17 ++++++ crates/valence_protocol/src/codec.rs | 5 ++ crates/valence_protocol/src/lib.rs | 4 +- extracted/blocks.json | 56 +++++++++++++++++++ .../valence/extractor/extractors/Blocks.java | 9 +++ .../extractor/mixin/ExposeWallBlock.java | 12 ++++ .../src/main/resources/extractor.mixins.json | 9 +++ extractor/src/main/resources/fabric.mod.json | 4 +- 9 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 extractor/src/main/java/rs/valence/extractor/mixin/ExposeWallBlock.java create mode 100644 extractor/src/main/resources/extractor.mixins.json diff --git a/crates/valence_protocol/build/block.rs b/crates/valence_protocol/build/block.rs index af7d6e3..45fb414 100644 --- a/crates/valence_protocol/build/block.rs +++ b/crates/valence_protocol/build/block.rs @@ -18,6 +18,7 @@ struct TopLevel { struct Block { id: u16, item_id: u16, + wall_variant_id: Option, translation_key: String, name: String, properties: Vec, @@ -297,6 +298,22 @@ pub fn build() -> anyhow::Result { }) .collect::(); + let state_to_wall_variant_arms = blocks + .iter() + .filter(|b| b.wall_variant_id.is_some()) + .map(|b| { + let block_name = ident(b.name.to_shouty_snake_case()); + let wall_block_name = ident( + blocks[b.wall_variant_id.unwrap() as usize] + .name + .to_shouty_snake_case(), + ); + quote! { + BlockState::#block_name => Some(BlockState::#wall_block_name), + } + }) + .collect::(); + let state_to_block_entity_type_arms = blocks .iter() .flat_map(|b| { @@ -595,6 +612,16 @@ pub fn build() -> anyhow::Result { #max_state_id } + /// Returns the wall variant of the block state. + /// + /// If the given block state doesn't have a wall variant, `None` is returned. + pub const fn wall_block_id(self) -> Option { + match self { + #state_to_wall_variant_arms + _ => None + } + } + /// Gets the value of the property with the given name from this block. /// /// If this block does not have the property, then `None` is returned. diff --git a/crates/valence_protocol/src/block.rs b/crates/valence_protocol/src/block.rs index 80217c6..75f5da2 100644 --- a/crates/valence_protocol/src/block.rs +++ b/crates/valence_protocol/src/block.rs @@ -124,4 +124,21 @@ mod tests { } } } + + #[test] + fn blockstate_to_wall() { + assert_eq!(BlockState::STONE.wall_block_id(), None); + assert_eq!( + BlockState::OAK_SIGN.wall_block_id(), + Some(BlockState::OAK_WALL_SIGN) + ); + assert_eq!( + BlockState::GREEN_BANNER.wall_block_id(), + Some(BlockState::GREEN_WALL_BANNER) + ); + assert_ne!( + BlockState::GREEN_BANNER.wall_block_id(), + Some(BlockState::GREEN_BANNER) + ); + } } diff --git a/crates/valence_protocol/src/codec.rs b/crates/valence_protocol/src/codec.rs index 8c9dbd1..0269af1 100644 --- a/crates/valence_protocol/src/codec.rs +++ b/crates/valence_protocol/src/codec.rs @@ -441,6 +441,11 @@ impl PacketDecoder { } } + #[cfg(feature = "compression")] + pub fn compression(&self) -> bool { + self.compression_enabled + } + #[cfg(feature = "compression")] pub fn set_compression(&mut self, enabled: bool) { self.compression_enabled = enabled; diff --git a/crates/valence_protocol/src/lib.rs b/crates/valence_protocol/src/lib.rs index ceb1a5a..7bf3b7a 100644 --- a/crates/valence_protocol/src/lib.rs +++ b/crates/valence_protocol/src/lib.rs @@ -271,7 +271,7 @@ pub trait Decode<'a>: Sized { /// ``` /// use valence_protocol::{Encode, EncodePacket}; /// -/// #[derive(Encode, EncodePacket)] +/// #[derive(Encode, EncodePacket, Debug)] /// #[packet_id = 42] /// struct MyStruct { /// first: i32, @@ -308,7 +308,7 @@ pub trait EncodePacket: fmt::Debug { /// ``` /// use valence_protocol::{Decode, DecodePacket}; /// -/// #[derive(Decode, DecodePacket)] +/// #[derive(Decode, DecodePacket, Debug)] /// #[packet_id = 42] /// struct MyStruct { /// first: i32, diff --git a/extracted/blocks.json b/extracted/blocks.json index 5e7d5b8..80eeebe 100644 --- a/extracted/blocks.json +++ b/extracted/blocks.json @@ -27676,6 +27676,7 @@ "name": "torch", "translation_key": "block.minecraft.torch", "item_id": 255, + "wall_variant_id": 162, "properties": [], "default_state_id": 2303, "states": [ @@ -42091,6 +42092,7 @@ "name": "oak_sign", "translation_key": "block.minecraft.oak_sign", "item_id": 821, + "wall_variant_id": 188, "properties": [ { "name": "rotation", @@ -42386,6 +42388,7 @@ "name": "spruce_sign", "translation_key": "block.minecraft.spruce_sign", "item_id": 822, + "wall_variant_id": 189, "properties": [ { "name": "rotation", @@ -42681,6 +42684,7 @@ "name": "birch_sign", "translation_key": "block.minecraft.birch_sign", "item_id": 823, + "wall_variant_id": 190, "properties": [ { "name": "rotation", @@ -42976,6 +42980,7 @@ "name": "acacia_sign", "translation_key": "block.minecraft.acacia_sign", "item_id": 825, + "wall_variant_id": 191, "properties": [ { "name": "rotation", @@ -43271,6 +43276,7 @@ "name": "jungle_sign", "translation_key": "block.minecraft.jungle_sign", "item_id": 824, + "wall_variant_id": 192, "properties": [ { "name": "rotation", @@ -43566,6 +43572,7 @@ "name": "dark_oak_sign", "translation_key": "block.minecraft.dark_oak_sign", "item_id": 826, + "wall_variant_id": 193, "properties": [ { "name": "rotation", @@ -43861,6 +43868,7 @@ "name": "mangrove_sign", "translation_key": "block.minecraft.mangrove_sign", "item_id": 827, + "wall_variant_id": 194, "properties": [ { "name": "rotation", @@ -44156,6 +44164,7 @@ "name": "bamboo_sign", "translation_key": "block.minecraft.bamboo_sign", "item_id": 828, + "wall_variant_id": 195, "properties": [ { "name": "rotation", @@ -46967,6 +46976,7 @@ "name": "oak_hanging_sign", "translation_key": "block.minecraft.oak_hanging_sign", "item_id": 831, + "wall_variant_id": 206, "properties": [ { "name": "attached", @@ -47525,6 +47535,7 @@ "name": "spruce_hanging_sign", "translation_key": "block.minecraft.spruce_hanging_sign", "item_id": 832, + "wall_variant_id": 207, "properties": [ { "name": "attached", @@ -48083,6 +48094,7 @@ "name": "birch_hanging_sign", "translation_key": "block.minecraft.birch_hanging_sign", "item_id": 833, + "wall_variant_id": 208, "properties": [ { "name": "attached", @@ -48641,6 +48653,7 @@ "name": "acacia_hanging_sign", "translation_key": "block.minecraft.acacia_hanging_sign", "item_id": 835, + "wall_variant_id": 209, "properties": [ { "name": "attached", @@ -49199,6 +49212,7 @@ "name": "jungle_hanging_sign", "translation_key": "block.minecraft.jungle_hanging_sign", "item_id": 834, + "wall_variant_id": 210, "properties": [ { "name": "attached", @@ -49757,6 +49771,7 @@ "name": "dark_oak_hanging_sign", "translation_key": "block.minecraft.dark_oak_hanging_sign", "item_id": 836, + "wall_variant_id": 211, "properties": [ { "name": "attached", @@ -50315,6 +50330,7 @@ "name": "crimson_hanging_sign", "translation_key": "block.minecraft.crimson_hanging_sign", "item_id": 839, + "wall_variant_id": 213, "properties": [ { "name": "attached", @@ -50873,6 +50889,7 @@ "name": "warped_hanging_sign", "translation_key": "block.minecraft.warped_hanging_sign", "item_id": 840, + "wall_variant_id": 214, "properties": [ { "name": "attached", @@ -51431,6 +51448,7 @@ "name": "mangrove_hanging_sign", "translation_key": "block.minecraft.mangrove_hanging_sign", "item_id": 837, + "wall_variant_id": 212, "properties": [ { "name": "attached", @@ -51989,6 +52007,7 @@ "name": "bamboo_hanging_sign", "translation_key": "block.minecraft.bamboo_hanging_sign", "item_id": 838, + "wall_variant_id": 215, "properties": [ { "name": "attached", @@ -54804,6 +54823,7 @@ "name": "redstone_torch", "translation_key": "block.minecraft.redstone_torch", "item_id": 619, + "wall_variant_id": 230, "properties": [ { "name": "lit", @@ -56155,6 +56175,7 @@ "name": "soul_torch", "translation_key": "block.minecraft.soul_torch", "item_id": 294, + "wall_variant_id": 247, "properties": [], "default_state_id": 5693, "states": [ @@ -86355,6 +86376,7 @@ "name": "skeleton_skull", "translation_key": "block.minecraft.skeleton_skull", "item_id": 1029, + "wall_variant_id": 376, "properties": [ { "name": "rotation", @@ -86607,6 +86629,7 @@ "name": "wither_skeleton_skull", "translation_key": "block.minecraft.wither_skeleton_skull", "item_id": 1030, + "wall_variant_id": 378, "properties": [ { "name": "rotation", @@ -86859,6 +86882,7 @@ "name": "zombie_head", "translation_key": "block.minecraft.zombie_head", "item_id": 1032, + "wall_variant_id": 380, "properties": [ { "name": "rotation", @@ -87111,6 +87135,7 @@ "name": "player_head", "translation_key": "block.minecraft.player_head", "item_id": 1031, + "wall_variant_id": 382, "properties": [ { "name": "rotation", @@ -87363,6 +87388,7 @@ "name": "creeper_head", "translation_key": "block.minecraft.creeper_head", "item_id": 1033, + "wall_variant_id": 384, "properties": [ { "name": "rotation", @@ -87615,6 +87641,7 @@ "name": "dragon_head", "translation_key": "block.minecraft.dragon_head", "item_id": 1034, + "wall_variant_id": 386, "properties": [ { "name": "rotation", @@ -87867,6 +87894,7 @@ "name": "piglin_head", "translation_key": "block.minecraft.piglin_head", "item_id": 1035, + "wall_variant_id": 388, "properties": [ { "name": "rotation", @@ -106046,6 +106074,7 @@ "name": "white_banner", "translation_key": "block.minecraft.white_banner", "item_id": 1059, + "wall_variant_id": 499, "properties": [ { "name": "rotation", @@ -106206,6 +106235,7 @@ "name": "orange_banner", "translation_key": "block.minecraft.orange_banner", "item_id": 1060, + "wall_variant_id": 500, "properties": [ { "name": "rotation", @@ -106366,6 +106396,7 @@ "name": "magenta_banner", "translation_key": "block.minecraft.magenta_banner", "item_id": 1061, + "wall_variant_id": 501, "properties": [ { "name": "rotation", @@ -106526,6 +106557,7 @@ "name": "light_blue_banner", "translation_key": "block.minecraft.light_blue_banner", "item_id": 1062, + "wall_variant_id": 502, "properties": [ { "name": "rotation", @@ -106686,6 +106718,7 @@ "name": "yellow_banner", "translation_key": "block.minecraft.yellow_banner", "item_id": 1063, + "wall_variant_id": 503, "properties": [ { "name": "rotation", @@ -106846,6 +106879,7 @@ "name": "lime_banner", "translation_key": "block.minecraft.lime_banner", "item_id": 1064, + "wall_variant_id": 504, "properties": [ { "name": "rotation", @@ -107006,6 +107040,7 @@ "name": "pink_banner", "translation_key": "block.minecraft.pink_banner", "item_id": 1065, + "wall_variant_id": 505, "properties": [ { "name": "rotation", @@ -107166,6 +107201,7 @@ "name": "gray_banner", "translation_key": "block.minecraft.gray_banner", "item_id": 1066, + "wall_variant_id": 506, "properties": [ { "name": "rotation", @@ -107326,6 +107362,7 @@ "name": "light_gray_banner", "translation_key": "block.minecraft.light_gray_banner", "item_id": 1067, + "wall_variant_id": 507, "properties": [ { "name": "rotation", @@ -107486,6 +107523,7 @@ "name": "cyan_banner", "translation_key": "block.minecraft.cyan_banner", "item_id": 1068, + "wall_variant_id": 508, "properties": [ { "name": "rotation", @@ -107646,6 +107684,7 @@ "name": "purple_banner", "translation_key": "block.minecraft.purple_banner", "item_id": 1069, + "wall_variant_id": 509, "properties": [ { "name": "rotation", @@ -107806,6 +107845,7 @@ "name": "blue_banner", "translation_key": "block.minecraft.blue_banner", "item_id": 1070, + "wall_variant_id": 510, "properties": [ { "name": "rotation", @@ -107966,6 +108006,7 @@ "name": "brown_banner", "translation_key": "block.minecraft.brown_banner", "item_id": 1071, + "wall_variant_id": 511, "properties": [ { "name": "rotation", @@ -108126,6 +108167,7 @@ "name": "green_banner", "translation_key": "block.minecraft.green_banner", "item_id": 1072, + "wall_variant_id": 512, "properties": [ { "name": "rotation", @@ -108286,6 +108328,7 @@ "name": "red_banner", "translation_key": "block.minecraft.red_banner", "item_id": 1073, + "wall_variant_id": 513, "properties": [ { "name": "rotation", @@ -108446,6 +108489,7 @@ "name": "black_banner", "translation_key": "block.minecraft.black_banner", "item_id": 1074, + "wall_variant_id": 514, "properties": [ { "name": "rotation", @@ -127600,6 +127644,7 @@ "name": "dead_tube_coral_fan", "translation_key": "block.minecraft.dead_tube_coral_fan", "item_id": 575, + "wall_variant_id": 685, "properties": [ { "name": "waterlogged", @@ -127632,6 +127677,7 @@ "name": "dead_brain_coral_fan", "translation_key": "block.minecraft.dead_brain_coral_fan", "item_id": 576, + "wall_variant_id": 686, "properties": [ { "name": "waterlogged", @@ -127664,6 +127710,7 @@ "name": "dead_bubble_coral_fan", "translation_key": "block.minecraft.dead_bubble_coral_fan", "item_id": 577, + "wall_variant_id": 687, "properties": [ { "name": "waterlogged", @@ -127696,6 +127743,7 @@ "name": "dead_fire_coral_fan", "translation_key": "block.minecraft.dead_fire_coral_fan", "item_id": 578, + "wall_variant_id": 688, "properties": [ { "name": "waterlogged", @@ -127728,6 +127776,7 @@ "name": "dead_horn_coral_fan", "translation_key": "block.minecraft.dead_horn_coral_fan", "item_id": 579, + "wall_variant_id": 689, "properties": [ { "name": "waterlogged", @@ -127760,6 +127809,7 @@ "name": "tube_coral_fan", "translation_key": "block.minecraft.tube_coral_fan", "item_id": 570, + "wall_variant_id": 690, "properties": [ { "name": "waterlogged", @@ -127792,6 +127842,7 @@ "name": "brain_coral_fan", "translation_key": "block.minecraft.brain_coral_fan", "item_id": 571, + "wall_variant_id": 691, "properties": [ { "name": "waterlogged", @@ -127824,6 +127875,7 @@ "name": "bubble_coral_fan", "translation_key": "block.minecraft.bubble_coral_fan", "item_id": 572, + "wall_variant_id": 692, "properties": [ { "name": "waterlogged", @@ -127856,6 +127908,7 @@ "name": "fire_coral_fan", "translation_key": "block.minecraft.fire_coral_fan", "item_id": 573, + "wall_variant_id": 693, "properties": [ { "name": "waterlogged", @@ -127888,6 +127941,7 @@ "name": "horn_coral_fan", "translation_key": "block.minecraft.horn_coral_fan", "item_id": 574, + "wall_variant_id": 694, "properties": [ { "name": "waterlogged", @@ -199667,6 +199721,7 @@ "name": "crimson_sign", "translation_key": "block.minecraft.crimson_sign", "item_id": 829, + "wall_variant_id": 802, "properties": [ { "name": "rotation", @@ -199962,6 +200017,7 @@ "name": "warped_sign", "translation_key": "block.minecraft.warped_sign", "item_id": 830, + "wall_variant_id": 803, "properties": [ { "name": "rotation", diff --git a/extractor/src/main/java/rs/valence/extractor/extractors/Blocks.java b/extractor/src/main/java/rs/valence/extractor/extractors/Blocks.java index 2fd4364..b514ea2 100644 --- a/extractor/src/main/java/rs/valence/extractor/extractors/Blocks.java +++ b/extractor/src/main/java/rs/valence/extractor/extractors/Blocks.java @@ -4,9 +4,11 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import net.minecraft.registry.Registries; +import net.minecraft.item.VerticallyAttachableBlockItem; import net.minecraft.util.math.BlockPos; import net.minecraft.world.EmptyBlockView; import rs.valence.extractor.Main; +import rs.valence.extractor.mixin.ExposeWallBlock; import java.util.LinkedHashMap; import java.util.Locale; @@ -37,6 +39,13 @@ public class Blocks implements Main.Extractor { blockJson.addProperty("translation_key", block.getTranslationKey()); blockJson.addProperty("item_id", Registries.ITEM.getRawId(block.asItem())); + if (block.asItem() instanceof VerticallyAttachableBlockItem wsbItem) { + if (wsbItem.getBlock() == block) { + var wallBlock = ((ExposeWallBlock) wsbItem).getWallBlock(); + blockJson.addProperty("wall_variant_id", Registries.BLOCK.getRawId(wallBlock)); + } + } + var propsJson = new JsonArray(); for (var prop : block.getStateManager().getProperties()) { var propJson = new JsonObject(); diff --git a/extractor/src/main/java/rs/valence/extractor/mixin/ExposeWallBlock.java b/extractor/src/main/java/rs/valence/extractor/mixin/ExposeWallBlock.java new file mode 100644 index 0000000..b25fd0a --- /dev/null +++ b/extractor/src/main/java/rs/valence/extractor/mixin/ExposeWallBlock.java @@ -0,0 +1,12 @@ +package rs.valence.extractor.mixin; + +import net.minecraft.block.Block; +import net.minecraft.item.VerticallyAttachableBlockItem; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(VerticallyAttachableBlockItem.class) +public interface ExposeWallBlock { + @Accessor + Block getWallBlock(); +} diff --git a/extractor/src/main/resources/extractor.mixins.json b/extractor/src/main/resources/extractor.mixins.json new file mode 100644 index 0000000..e7d59d6 --- /dev/null +++ b/extractor/src/main/resources/extractor.mixins.json @@ -0,0 +1,9 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "rs.valence.extractor.mixin", + "compatibilityLevel": "JAVA_17", + "mixins": [ + "ExposeWallBlock" + ] +} \ No newline at end of file diff --git a/extractor/src/main/resources/fabric.mod.json b/extractor/src/main/resources/fabric.mod.json index 1f2038d..c975a17 100644 --- a/extractor/src/main/resources/fabric.mod.json +++ b/extractor/src/main/resources/fabric.mod.json @@ -17,7 +17,9 @@ "rs.valence.extractor.Main" ] }, - "mixins": [], + "mixins": [ + "extractor.mixins.json" + ], "depends": { "fabricloader": ">=0.14.6", "minecraft": "~1.19",