Skip to content

Commit e071f72

Browse files
committed
优化显示逻辑
1 parent 9400b37 commit e071f72

1 file changed

Lines changed: 45 additions & 13 deletions

File tree

src/main/java/ink/myumoon/ibp/RenderEvent.java

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@
33
import com.mojang.blaze3d.platform.GlStateManager;
44
import com.mojang.blaze3d.systems.RenderSystem;
55
import net.minecraft.client.Minecraft;
6+
import net.minecraft.client.Options;
67
import net.minecraft.client.gui.GuiGraphics;
78
import net.minecraft.resources.ResourceLocation;
89
import net.minecraft.core.BlockPos;
910
import net.minecraft.tags.BlockTags;
1011
import net.minecraft.tags.TagKey;
12+
import net.minecraft.world.MenuProvider;
13+
import net.minecraft.world.level.GameType;
14+
import net.minecraft.world.level.Level;
1115
import net.minecraft.world.level.block.Block;
1216
import net.minecraft.world.phys.BlockHitResult;
17+
import net.minecraft.world.phys.EntityHitResult;
1318
import net.minecraft.world.phys.HitResult;
1419
import net.neoforged.bus.api.SubscribeEvent;
1520
import net.neoforged.fml.common.EventBusSubscriber;
1621
import net.neoforged.neoforge.client.event.RenderGuiEvent;
1722

23+
import javax.annotation.Nullable;
24+
1825
@EventBusSubscriber(modid = "ibp")
1926
public 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

Comments
 (0)