1313import net .minecraft .world .item .ItemStack ;
1414import net .minecraft .world .item .TooltipFlag ;
1515import net .minecraft .world .level .Level ;
16+ import org .jetbrains .annotations .Nullable ;
1617
1718public class GuideItem extends Item {
1819 public static final ResourceLocation ID = GuideME .makeId ("guide" );
@@ -26,30 +27,34 @@ public GuideItem(Properties properties) {
2627
2728 @ Override
2829 public Component getName (ItemStack stack ) {
29- var guideId = stack .get (GuideME .GUIDE_ID_COMPONENT );
30- var name = GuideMEProxy .instance ().getGuideDisplayName (guideId );
31- if (name != null ) {
32- return name ;
30+ var guideId = getGuideId (stack );
31+ if (guideId != null ) {
32+ var name = GuideMEProxy .instance ().getGuideDisplayName (guideId );
33+ if (name != null ) {
34+ return name ;
35+ }
3336 }
3437 return super .getName (stack );
3538 }
3639
3740 @ Override
3841 public void appendHoverText (ItemStack stack , TooltipContext context , List <Component > lines ,
3942 TooltipFlag tooltipFlag ) {
40- var guideId = stack .get (GuideME .GUIDE_ID_COMPONENT );
41- GuideMEProxy .instance ().addGuideTooltip (
42- guideId ,
43- context ,
44- lines ,
45- tooltipFlag );
43+ var guideId = getGuideId (stack );
44+ if (guideId != null ) {
45+ GuideMEProxy .instance ().addGuideTooltip (
46+ guideId ,
47+ context ,
48+ lines ,
49+ tooltipFlag );
50+ }
4651 }
4752
4853 @ Override
4954 public InteractionResultHolder <ItemStack > use (Level level , Player player , InteractionHand hand ) {
5055 var stack = player .getItemInHand (hand );
5156
52- var guideId = player .getItemInHand (hand ). get ( GuideME . GUIDE_ID_COMPONENT );
57+ var guideId = getGuideId ( player .getItemInHand (hand ));
5358
5459 if (level .isClientSide ) {
5560 if (guideId == null ) {
@@ -61,4 +66,9 @@ public InteractionResultHolder<ItemStack> use(Level level, Player player, Intera
6166
6267 return InteractionResultHolder .success (stack );
6368 }
69+
70+ @ Nullable
71+ public static ResourceLocation getGuideId (ItemStack stack ) {
72+ return stack .get (GuideME .GUIDE_ID_COMPONENT );
73+ }
6474}
0 commit comments