Jump to content

(SOLVED) [1.12] How to get the server-side instance of the player in a client-side method?


Recommended Posts

Posted (edited)

Hello, I am trying to make a custom compass that points towards the player's bed, however, IItemPropertyGetter only passes the client-side player in the apply method. The problem is that getBedLocation always returns the world spawn on a client-side player object, the question is: how do I get a server-side instance of the player from nothing?

 

This is the IItemPropertyGetter

Spoiler

this.addPropertyOverride(new ResourceLocation("angle"), new IItemPropertyGetter() {
	    	
	    	@SideOnly(Side.CLIENT)
	        double rotation;
	        @SideOnly(Side.CLIENT)
	        double rota;
	        @SideOnly(Side.CLIENT)
	        long lastUpdateTick;
	        
	        @SideOnly(Side.CLIENT)
	        public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
	        	if (entityIn == null && !stack.isOnItemFrame()) {
	                    return 0.0F;
	            } else {
	            	boolean flag = entityIn != null;
	                Entity entity = (Entity)(flag ? entityIn : stack.getItemFrame());
	                if (worldIn == null) {
	                        worldIn = entity.world;
	                }
	                double d0;
	                if (worldIn.provider.isSurfaceWorld()) {
	                	double d1 = flag ? (double)entity.rotationYaw : this.getFrameRotation((EntityItemFrame)entity);
	                	d1 = MathHelper.positiveModulo(d1 / 360.0D, 1.0D);
	                    double d2 = this.getSpawnToAngle(worldIn, entity) / (Math.PI * 2D);
	                    d0 = 0.5D - (d1 - 0.25D - d2);
	                } else {
	                	d0 = Math.random();
	                }
	                if (flag) {
	                        d0 = this.wobble(worldIn, d0);
	                }
	                return MathHelper.positiveModulo((float)d0, 1.0F);
	            }
	        }
	        
	        @SideOnly(Side.CLIENT)
	        private double wobble(World worldIn, double p_185093_2_) {
	        	if (worldIn.getTotalWorldTime() != this.lastUpdateTick) {
	            	this.lastUpdateTick = worldIn.getTotalWorldTime();
	                double d0 = p_185093_2_ - this.rotation;
	            	d0 = MathHelper.positiveModulo(d0 + 0.5D, 1.0D) - 0.5D;
	                this.rota += d0 * 0.1D;
	                this.rota *= 0.8D;
	                this.rotation = MathHelper.positiveModulo(this.rotation + this.rota, 1.0D);
	            }
	                return this.rotation;
	        }
	        
	        @SideOnly(Side.CLIENT)
	        private double getFrameRotation(EntityItemFrame itemFrame) {
	        	return (double)MathHelper.clampAngle(180 + itemFrame.facingDirection.getHorizontalIndex() * 90);
	        }
	        
	        @SideOnly(Side.CLIENT)
	        private double getSpawnToAngle(World world, Entity player) {
				// ???
	        	return 0;
	        }
	        
	    });

 

 

Edited by xyz
Posted

TLDR:

You can't.  And when you could you shouldn't.

  • Like 2

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)
2 minutes ago, larsgerrits said:

Why can't you use the client side player? The vanilla compass uses the client player as well.

As I explained in the OP, using getBedLocation() on a client-side player always returns the world spawn while I need the player's bed location.

Edited by xyz
Posted

Ok.

Let me put it this way:

 

The logical client is not always on the same physical machine as the logical server.

 

Ergo you have no access to the server-side player entity.  If you want to reach across logical sides, then by all means, go ahead and do so, but your mod will be SSP only.

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

If you were to look at the code the vanilla compass uses to determine the user's bed location (AKA the spawnpoint of the user), you could see you can use World#getSpawnPoint to get the client player's spawnpoint.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted (edited)
8 minutes ago, larsgerrits said:

If you were to look at the code the vanilla compass uses to determine the user's bed location (AKA the spawnpoint of the user), you could see you can use World#getSpawnPoint to get the client player's spawnpoint.

While you are technically correct that still doesn't change that I'm looking for the player's bed location and not the world spawn (the vanilla compass points towards the world spawn).

 

9 minutes ago, Draco18s said:

Ok.

Let me put it this way:

 

The logical client is not always on the same physical machine as the logical server.

 

Ergo you have no access to the server-side player entity.  If you want to reach across logical sides, then by all means, go ahead and do so, but your mod will be SSP only.

Thanks a lot for your explanation.

Edited by xyz
Posted
2 minutes ago, xyz said:

(the vanilla compass points towards the world spawn)

After all this time, how could I forget... My bad.

 

 

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted
17 minutes ago, xyz said:

While you are technically correct that still doesn't change that I'm looking for the player's bed location and not the world spawn (the vanilla compass points towards the world spawn).

 

Thanks a lot for your explanation.

You are gonna want to save the Players Bed spawnpoint to a capability and then update it with packets so the client side also has the data.

  • Like 1

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
1 hour ago, Animefan8888 said:

You are gonna want to save the Players Bed spawnpoint to a capability and then update it with packets so the client side also has the data.

How exactly would I go about using packets? There doesn't really seem to be any up-to-date tutorial on it.

Posted
Just now, xyz said:

How exactly would I go about using packets? There doesn't really seem to be any up-to-date tutorial on it.

Packets have not really changed much since 1.7 unless something happened in 1.12 that i don't know about. So here is a tutorial  from Diesieben.

  • Like 1

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.

Announcements



×
×
  • Create New...

Important Information

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