Jump to content

[1.19.3] Little help regarding sides (and possibly networking)


chxr

Recommended Posts

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 by chxr
Typos
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.