33import net .just_s .sds .Config ;
44import net .minecraft .block .Block ;
55import net .minecraft .block .BlockState ;
6+ import net .minecraft .component .DataComponentTypes ;
7+ import net .minecraft .component .type .DebugStickStateComponent ;
68import net .minecraft .entity .player .PlayerEntity ;
79import net .minecraft .item .DebugStickItem ;
810import net .minecraft .item .ItemStack ;
9- import net .minecraft .nbt . NbtCompound ;
11+ import net .minecraft .registry . entry . RegistryEntry ;
1012import net .minecraft .state .StateManager ;
1113import net .minecraft .state .property .Property ;
1214import net .minecraft .text .Text ;
13- import net .minecraft .util .Util ;
1415import net .minecraft .util .math .BlockPos ;
15- import net .minecraft .registry .Registries ;
1616import net .minecraft .world .WorldAccess ;
1717import org .jetbrains .annotations .Nullable ;
1818import org .spongepowered .asm .mixin .Mixin ;
@@ -36,13 +36,19 @@ private static <T extends Comparable<T>> BlockState cycle(BlockState state, Prop
3636 return null ;
3737 }
3838
39+ @ Shadow
40+ private static <T > T cycle (Iterable <T > values , @ Nullable T value , boolean inverse ) {
41+ return null ;
42+ }
43+
3944 @ Inject (at = @ At ("HEAD" ), method = "use" , cancellable = true )
4045 private void onUSE (PlayerEntity player , BlockState state , WorldAccess world , BlockPos pos , boolean update , ItemStack stack , CallbackInfoReturnable <Boolean > cir ) {
4146 // if the player already does have the rights to use Debug Stick, the mod should not interfere
4247 if (player .isCreativeLevelTwoOp ()) {return ;}
4348
4449 Block block = state .getBlock ();
45- StateManager <Block , BlockState > stateManager = block .getStateManager ();
50+ RegistryEntry <Block > registryEntry = state .getRegistryEntry ();
51+ StateManager <Block , BlockState > stateManager = (registryEntry .value ()).getStateManager ();
4652 Collection <Property <?>> collection = stateManager .getProperties ();
4753
4854 // check if block is modifiable by the config
@@ -52,22 +58,23 @@ private void onUSE(PlayerEntity player, BlockState state, WorldAccess world, Blo
5258 return ;
5359 }
5460
55- // https://minecraft.fandom.com/ wiki/Debug_Stick#Item_data
61+ // https://minecraft.wiki/w/ Debug_Stick
5662 // to remember the data of which property for which block is chosen,
57- // Minecraft Devs decided to use NBT data for Debug Stick.
58- // Who am I to disagree?
59- NbtCompound nbtCompound = stack .getOrCreateSubNbt ( "DebugProperty" );
63+ // Minecraft Devs decided to use Component for Debug Stick.
64+ // Who am I to disagree? (btw thx to @MrBretze for example code)
65+ DebugStickStateComponent stateComponent = stack .get ( DataComponentTypes . DEBUG_STICK_STATE );
6066
61- String blockName = Registries .BLOCK .getId (block ).toString ();
62- String propertyName = nbtCompound .getString (blockName );
67+ if (stateComponent == null ) {
68+ return ;
69+ }
6370
64- Property <?> property = stateManager . getProperty ( propertyName );
71+ Property <?> property = stateComponent . properties (). get ( registryEntry );
6572
6673 if (player .isSneaking ()) {
6774 // select next property
6875 property = getNextProperty (collection , property , block );
6976 // save chosen property in the NBT data of Debug Stick
70- nbtCompound . putString ( blockName , property . getName ( ));
77+ stack . set ( DataComponentTypes . DEBUG_STICK_STATE , stateComponent . with ( registryEntry , property ));
7178
7279 // send the player a message of successful selecting
7380 sendMessage (player , Text .of (
@@ -105,11 +112,11 @@ private void onUSE(PlayerEntity player, BlockState state, WorldAccess world, Blo
105112 * Choose next property that is appropriate for the configuration file
106113 * */
107114 private Property <?> getNextProperty (Collection <Property <?>> collection , @ Nullable Property <?> property , @ Nullable Block block ) {
108- int len = collection . size () ;
115+ int i = 0 ;
109116 do { // simply scrolling through the list of properties until suitable is found
110- property = Util . next (collection , property );
111- len -- ;
112- } while (len > 0 && !isPropertyModifiable (property , block ));
117+ property = cycle (collection , property , false );
118+ i ++ ;
119+ } while (i < collection . size () && !isPropertyModifiable (property , block ));
113120 return property ;
114121 }
115122
0 commit comments