问题出在 EntityPlayer.posX 上,它在数值之间出现了“闪烁”。

huangapple go评论88阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月12日 23:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63862148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定