33import com .mojang .blaze3d .platform .GlStateManager ;
44import com .mojang .blaze3d .systems .RenderSystem ;
55import net .minecraft .client .Minecraft ;
6+ import net .minecraft .client .Options ;
67import net .minecraft .client .gui .GuiGraphics ;
78import net .minecraft .resources .ResourceLocation ;
89import net .minecraft .core .BlockPos ;
910import net .minecraft .tags .BlockTags ;
1011import net .minecraft .tags .TagKey ;
12+ import net .minecraft .world .MenuProvider ;
13+ import net .minecraft .world .level .GameType ;
14+ import net .minecraft .world .level .Level ;
1115import net .minecraft .world .level .block .Block ;
1216import net .minecraft .world .phys .BlockHitResult ;
17+ import net .minecraft .world .phys .EntityHitResult ;
1318import net .minecraft .world .phys .HitResult ;
1419import net .neoforged .bus .api .SubscribeEvent ;
1520import net .neoforged .fml .common .EventBusSubscriber ;
1621import net .neoforged .neoforge .client .event .RenderGuiEvent ;
1722
23+ import javax .annotation .Nullable ;
24+
1825@ EventBusSubscriber (modid = "ibp" )
1926public class RenderEvent {
2027
@@ -31,22 +38,30 @@ private static TagKey<Block> create(String create){
3138 public static void onInteractiveBlock (RenderGuiEvent .Pre event ){
3239 Minecraft minecraft = Minecraft .getInstance ();
3340
34- //方块识别
35- if (minecraft .hitResult == null || minecraft .hitResult .getType () != HitResult .Type .BLOCK ){
36- return ;
37- }
41+ Options options = minecraft .options ;
42+
43+ boolean isFirstPerson = options .getCameraType ().isFirstPerson ();
44+ boolean isNotSpectator = minecraft .gameMode .getPlayerMode () != GameType .SPECTATOR ;
45+ boolean canRenderForSpectator = canRenderCrosshairForSpectator (minecraft .hitResult );
46+
47+ if (isFirstPerson && (isNotSpectator || canRenderForSpectator )){
48+ //方块识别
49+ if (minecraft .hitResult == null || minecraft .hitResult .getType () != HitResult .Type .BLOCK ){
50+ return ;
51+ }
3852
39- BlockHitResult blockHitResult = (BlockHitResult ) minecraft .hitResult ;
40- BlockPos blockPos = blockHitResult .getBlockPos ();
53+ BlockHitResult blockHitResult = (BlockHitResult ) minecraft .hitResult ;
54+ BlockPos blockPos = blockHitResult .getBlockPos ();
4155
42- if (minecraft .level != null && minecraft .level .getBlockState (blockPos ).is (INTERACTIVE_TAG )) {
43- int screenWidth = event .getGuiGraphics ().guiWidth ();
44- int screenHeight = event .getGuiGraphics ().guiHeight ();
56+ if (minecraft .level != null && minecraft .level .getBlockState (blockPos ).is (INTERACTIVE_TAG )) {
57+ int screenWidth = event .getGuiGraphics ().guiWidth ();
58+ int screenHeight = event .getGuiGraphics ().guiHeight ();
4559
46- int x = screenWidth / 2 + ICON_OFFSET ;
47- int y = screenHeight / 2 - ICON_SIZE / 2 ;
60+ int x = screenWidth / 2 + ICON_OFFSET ;
61+ int y = screenHeight / 2 - ICON_SIZE / 2 ;
4862
49- drawIcon (event .getGuiGraphics (), x , y );
63+ drawIcon (event .getGuiGraphics (), x , y );
64+ }
5065 }
5166 }
5267
@@ -67,8 +82,25 @@ private static void drawIcon(GuiGraphics guiGraphics,int x, int y){
6782 ICON_SIZE ,ICON_SIZE
6883 );
6984
70- // 恢复默认混合模式
7185 RenderSystem .defaultBlendFunc ();
7286 RenderSystem .disableBlend ();
7387 }
88+
89+ //from Gui#canRenderCrosshairForSpectator
90+ private static boolean canRenderCrosshairForSpectator (@ Nullable HitResult rayTrace ) {
91+ if (rayTrace == null ) {
92+ return false ;
93+ } else if (rayTrace .getType () == HitResult .Type .ENTITY ) {
94+ return ((EntityHitResult )rayTrace ).getEntity () instanceof MenuProvider ;
95+ } else if (rayTrace .getType () == HitResult .Type .BLOCK ) {
96+ BlockPos blockpos = ((BlockHitResult )rayTrace ).getBlockPos ();
97+ Level level = Minecraft .getInstance ().level ;
98+ if (level != null ) {
99+ return level .getBlockState (blockpos ).getMenuProvider (level , blockpos ) != null ;
100+ }
101+ } else {
102+ return false ;
103+ }
104+ return true ;
105+ }
74106}
0 commit comments