Posted February 12, 20232 yr So, I'm making a custom compass item that points to a player on the same team as the player. The problem comes in the following part of the code: int teamSize = stringTeamPlayers.size(); if (!currentWorld.isClientSide()) { //Null pointer excepction. GetPlayerList only works server side? if (teamSize == 1) { nearestPlayer = currentWorld.getServer().getPlayerList().getPlayerByName(listTeamPlayers.get(0)); } else { Random rand = new Random(); nearestPlayer = currentWorld.getServer().getPlayerList().getPlayerByName(listTeamPlayers.get(rand.nextInt(teamSize))); } distanceToItemUser = userPlayer.distanceTo(nearestPlayer); } else { //Distance and nearestPlayer are still wrongly defined on client side distanceToItemUser = 1; } So the initial problem (before I had the outermost if that makes the code only execute server-side) the session would crash with a Nullpointerexception. I guessed that getPlayerList would only work server-side and the error disappeared. The problem now is that, while the calculation is done server-side, client-side still has undefined (or rather incorrect) result for the distance and NearestPlayer. It looks like it works because I choose a value for the client-side that enters a block of code that deletes the object once the player approaches the targeted player: if (distanceToItemUser < 5) { if (!currentWorld.isClientSide()) { //Delete the item from the inventory once you approach to 5 blocks or less from the tracked player if (currentWorld.isClientSide()) { userPlayer.sendSystemMessage(Component.literal(String.format("poof"))); } Inventory inv = userPlayer.getInventory(); inv.removeItem(itemStack); } }else{ //Code that calculates the angle and changes the texture goes here } That's why it "flickers" (The animation for when the item is deleted is played, as it f it was about to be deleted but the server keeps it from doing so since it has the actual updated info) Obviously, if i change the value to something higher than 5 and let it go to the else it will throw a NullPointerException since it needs data it's only available in the server-only code. Any idea of how can I proceed with this? The main idea I have is to use packets but I'm really lost with that and I don't know if there's a simpler solution to this problem Edited February 12, 20232 yr by chxr Typos
February 12, 20232 yr You could always do what the normal compass does and write the position it is currently pointing to the tag. Then, you could use the CompassItemPropertyFunction to read the position from the tag and just use that. Tags are synced whenever they are changed, so you wouldn't need to do any manual syncing.
February 14, 20232 yr Author You mean CompoundTag, right? I was under the impression that they need to be synchronized manually too. I will try and get back here if i need any more help Edited February 14, 20232 yr by chxr
February 14, 20232 yr 2 hours ago, chxr said: You mean CompoundTag, right? I was under the impression that they need to be synchronized manually too. Correct, and it depends on what is being synced, but the base compound tag is typically synced when changed which you are writing to.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.