英文:
Issue with EntityPlayer.posX "flickering" between values
问题
基本上我正在尝试创建一个坐标显示模组。在使用posX/Y/Z(此示例使用X)访问玩家位置时,显示的坐标会出现奇怪的“闪烁”现象。您可以在此视频中看到。
@SubscribeEvent
public void playerTick(PlayerTickEvent event) {
EntityPlayer player = event.player;
posX = player.posX;
System.out.println(posX); // 用于调试,也出现了问题,这意味着不是round()函数的问题
}
@SubscribeEvent
public void renderOverlay(RenderGameOverlayEvent event) {
if (event.type == RenderGameOverlayEvent.ElementType.TEXT) {
drawString(renderer, String.valueOf(round(posX, 8)), 0, 0, -1);
}
}
我认为这可能与我使用的playerTick有关,但我对Forge和Java都不太熟悉,不知道是否可以使用其他方式(假设这是问题所在)。
英文:
So basically I'm trying to create a coords display mod. When accessing the player's position with posX/Y/Z (this example uses X) there is a weird "flickering" with the displayed coordinate. You can see it in this video.
@SubscribeEvent
public void playerTick(PlayerTickEvent event) {
EntityPlayer player = event.player;
posX = player.posX;
System.out.println(posX); // Used to debug, also had the issue, meaning it isn't an issue with round()
}
@SubscribeEvent
public void renderOverlay(RenderGameOverlayEvent event) {
if (event.type == RenderGameOverlayEvent.ElementType.TEXT) {
drawString(renderer, String.valueOf(round(posX, 8)), 0, 0, -1);
}
}
I think this might have something to do with me using playerTick or something, but I'm new to forge and Java and don't know what I might use instead (assuming that is the problem).
答案1
得分: 0
这是我使用了PlayerTickEvent而不是ClientTickEvent引起的问题。
public void clientTick(ClientTickEvent event) {
EntityPlayerSP player = mc.thePlayer;
if (player == null) return;
posX = player.posX;
}
因为当不在游戏中时,需要if (player == null) return;
,否则mc.thePlayer
会返回null
,导致.posX
引发崩溃。
英文:
It was an issue with me using PlayerTickEvent instead of ClientTickEvent.
public void clientTick(ClientTickEvent event) {
EntityPlayerSP player = mc.thePlayer;
if (player == null) return;
posX = player.posX
}
if (player == null) return;
is required because when not in game, mc.thePlayer
returns null
so .posX
causes a crash.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论