11package org .allaymc .server .network .multiversion ;
22
33import org .allaymc .api .player .Player ;
4- import org .allaymc .api .utils .identifier .Identifier ;
54import org .allaymc .server .player .AllayPlayer ;
65import org .cloudburstmc .nbt .NbtMap ;
76import org .cloudburstmc .nbt .NbtType ;
87import org .cloudburstmc .protocol .bedrock .codec .BedrockCodec ;
9- import org .cloudburstmc .protocol .bedrock .codec .v766 .Bedrock_v766 ;
10- import org .cloudburstmc .protocol .bedrock .codec .v800 .Bedrock_v800 ;
118import org .cloudburstmc .protocol .bedrock .codec .v827 .Bedrock_v827 ;
129import org .cloudburstmc .protocol .bedrock .codec .v844 .Bedrock_v844 ;
1310import org .cloudburstmc .protocol .bedrock .codec .v898 .Bedrock_v898 ;
1411import org .cloudburstmc .protocol .bedrock .data .BlockPropertyData ;
1512import org .cloudburstmc .protocol .bedrock .data .ExperimentData ;
16- import org .cloudburstmc .protocol .bedrock .data .definitions .ItemDefinition ;
17- import org .cloudburstmc .protocol .bedrock .data .inventory .ItemData ;
18- import org .cloudburstmc .protocol .bedrock .data .inventory .crafting .recipe .*;
19- import org .cloudburstmc .protocol .bedrock .data .inventory .descriptor .DefaultDescriptor ;
20- import org .cloudburstmc .protocol .bedrock .data .inventory .descriptor .ItemDescriptorWithCount ;
21- import org .cloudburstmc .protocol .bedrock .packet .BiomeDefinitionListPacket ;
22- import org .cloudburstmc .protocol .bedrock .packet .CraftingDataPacket ;
2313
2414import java .util .LinkedList ;
2515import java .util .List ;
26- import java .util .stream .Stream ;
2716
2817/**
2918 * @author daoge_cmd
3019 */
3120@ MultiVersion (version = "*" )
3221public final class MultiVersionHelper {
3322
34- public static CraftingDataPacket adaptCraftingDataPacket (Player player , CraftingDataPacket packet ) {
35- if (!is1_21_50 (player )) {
36- return packet ;
37- }
38-
39- // Remove recipes that contain items not available in 1.21.50
40- packet .getCraftingData ().removeIf (MultiVersionHelper ::containsUnsupportedItem );
41- return packet ;
42- }
43-
44- private static boolean containsUnsupportedItem (RecipeData recipe ) {
45- return switch (recipe ) {
46- case ShapedRecipeData shaped ->
47- containsUnsupportedIngredient (shaped .getIngredients ()) ||
48- containsUnsupportedOutput (shaped .getResults ());
49- case ShapelessRecipeData shapeless ->
50- containsUnsupportedIngredient (shapeless .getIngredients ()) ||
51- containsUnsupportedOutput (shapeless .getResults ());
52- case SmithingTransformRecipeData smithing ->
53- isUnsupportedIngredient (smithing .getTemplate ()) ||
54- isUnsupportedIngredient (smithing .getBase ()) ||
55- isUnsupportedIngredient (smithing .getAddition ()) ||
56- isUnsupportedOutput (smithing .getResult ());
57- case SmithingTrimRecipeData trim ->
58- isUnsupportedIngredient (trim .getTemplate ()) ||
59- isUnsupportedIngredient (trim .getBase ()) ||
60- isUnsupportedIngredient (trim .getAddition ());
61- case FurnaceRecipeData furnace ->
62- isUnsupportedOutput (furnace .getResult ());
63- default -> false ;
64- };
65- }
66-
67- private static boolean containsUnsupportedIngredient (List <ItemDescriptorWithCount > ingredients ) {
68- return ingredients .stream ().anyMatch (MultiVersionHelper ::isUnsupportedIngredient );
69- }
70-
71- private static boolean isUnsupportedIngredient (ItemDescriptorWithCount ingredient ) {
72- if (ingredient .getDescriptor () instanceof DefaultDescriptor descriptor ) {
73- return isItemNotAvailableIn1_21_50 (descriptor .getItemId ().getIdentifier ());
74- }
75- return false ;
76- }
77-
78- private static boolean containsUnsupportedOutput (List <ItemData > outputs ) {
79- return outputs .stream ().anyMatch (MultiVersionHelper ::isUnsupportedOutput );
80- }
81-
82- private static boolean isUnsupportedOutput (ItemData output ) {
83- return isItemNotAvailableIn1_21_50 (output .getDefinition ().getIdentifier ());
84- }
85-
86- public static BiomeDefinitionListPacket adaptBiomeDefinitionListPacket (Player player , BiomeDefinitionListPacket packet ) {
87- if (!is1_21_50 (player )) {
88- return packet ;
89- }
90-
91- var builder = NbtMap .builder ();
92- for (var entry : packet .getBiomes ().getDefinitions ().entrySet ()) {
93- var identifier = entry .getKey ();
94- var definition = entry .getValue ();
95-
96- builder .putCompound (identifier , NbtMap .builder ()
97- .putFloat ("ash" , definition .getAshDensity ())
98- .putFloat ("blue_spores" , definition .getBlueSporeDensity ())
99- .putFloat ("depth" , definition .getDepth ())
100- .putFloat ("downfall" , definition .getDownfall ())
101- .putFloat ("height" , definition .getScale ())
102- .putBoolean ("rain" , definition .isRain ())
103- .putFloat ("red_spores" , definition .getRedSporeDensity ())
104- .putList ("tags" , NbtType .STRING , definition .getTags ())
105- .putFloat ("temperature" , definition .getTemperature ())
106- .putFloat ("waterColorA" , definition .getMapWaterColor ().getAlpha () / 256f )
107- .putFloat ("waterColorB" , definition .getMapWaterColor ().getBlue () / 256f )
108- .putFloat ("waterColorG" , definition .getMapWaterColor ().getGreen () / 256f )
109- .putFloat ("waterColorR" , definition .getMapWaterColor ().getRed () / 256f )
110- .putFloat ("white_ash" , definition .getWhiteAshDensity ())
111- .build ()
112- );
113- }
114-
115- packet .setDefinitions (builder .build ());
116- return packet ;
117- }
118-
119- public static void adaptItemDefinitions (Player player , List <ItemDefinition > definitions ) {
120- if (!is1_21_50 (player )) {
121- return ;
122- }
123-
124- // Remove the items that do not exist in 1.21.50
125- definitions .removeIf (def -> isItemNotAvailableIn1_21_50 (def .getIdentifier ()));
126- }
127-
128- private static boolean isItemNotAvailableIn1_21_50 (String identifier ) {
129- if (!identifier .startsWith (Identifier .DEFAULT_NAMESPACE )) {
130- // Skip custom items
131- return false ;
132- }
133-
134- if (identifier .contains ("lightning_rod" ) &&
135- !identifier .equals ("minecraft:lightning_rod" )) {
136- return true ;
137- }
138-
139- if (Stream .of ("minecraft:bush" , "minecraft:firefly_bush" ).anyMatch (identifier ::equals )) {
140- return true ;
141- }
142-
143- // TODO: find out why exclude "wildflowers", "cactus_flower" will crash the client
144- return Stream .of (
145- // Copper
146- "copper_bars" , "copper_golem" , "copper_lantern" , "copper_chain" ,
147- "copper_helmet" , "copper_chestplate" , "copper_leggings" , "copper_boots" ,
148- "copper_sword" , "copper_axe" , "copper_pickaxe" , "copper_shovel" , "copper_hoe" ,
149- "copper_horse_armor" , "copper_torch" , "copper_chest" , "copper_nugget" ,
150- // Misc
151- "happy_ghast" , "dried_ghast" , "netherite_horse_armor" , "harness" , "nautilus_armor" ,
152- "nautilus_spawn_egg" , "dry_grass" , "spear" , "_shelf" , "iron_chain" , "leaf_litter" ,
153- "brown_egg" , "blue_egg" , "camel_husk_spawn_egg" , "parched_spawn_egg" , "music_disc_tears" ,
154- "music_disc_lava_chicken"
155- ).anyMatch (identifier ::contains );
156- }
157-
15823 public static void adaptExperimentData (Player player , List <ExperimentData > experiments ) {
159- if (is1_21_80 (player )) {
160- // Enables 2025 Content Drop 2 features
161- experiments .add (new ExperimentData ("y_2025_drop_2" , true ));
162- // Enables the locator bar for 1.21.80 clients
163- experiments .add (new ExperimentData ("locator_bar" , true ));
164- // Allows Vibrant Visuals to appear in the settings menu
165- experiments .add (new ExperimentData ("experimental_graphics" , true ));
166- }
16724 if (is1_21_100 (player )) {
16825 experiments .add (new ExperimentData ("y_2025_drop_3" , true ));
16926 }
@@ -282,10 +139,6 @@ private static NbtMap adaptMaterialInstances(NbtMap materialInstances) {
282139 .build ();
283140 }
284141
285- public static boolean is1_21_50 (Player player ) {
286- return getCodec (player ).getProtocolVersion () == Bedrock_v766 .CODEC .getProtocolVersion ();
287- }
288-
289142 private static boolean is1_21_130orHigher (Player player ) {
290143 return getCodec (player ).getProtocolVersion () >= Bedrock_v898 .CODEC .getProtocolVersion ();
291144 }
@@ -298,10 +151,6 @@ private static boolean is1_21_100(Player player) {
298151 return getCodec (player ) == Bedrock_v827 .CODEC ;
299152 }
300153
301- private static boolean is1_21_80 (Player player ) {
302- return getCodec (player ) == Bedrock_v800 .CODEC ;
303- }
304-
305154 private static BedrockCodec getCodec (Player player ) {
306155 return ((AllayPlayer ) player ).getSession ().getCodec ();
307156 }
0 commit comments