Jump to content

[1.11.X] Compass to point to your personal spawn point


gibraltar

Recommended Posts

I've written a compass that I believe should point to your personal spawn point, if it's been set. It looks a lot like the regular compass code, but instead of pointing to world spawn it gets the bed location for the current user and points to that. The issue I'm seeing is that when I sleep in a new place, the compass doesn't start pointing to the new location. But I've checked that the player's spawn is changing, and the same method, getBedLocation returns a different value when called within other events, so I'm not sure what the problem is. Any help would be appreciated.

 

public class ItemPersonalCompass extends Item {
    public ItemPersonalCompass() {
        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 = flag ? entityIn : stack.getItemFrame();

                    if (worldIn == null) {
                        worldIn = entity.worldObj;
                    }

                    BlockPos destinationPos = new BlockPos(0, 0, 0);
                    if (entity instanceof EntityPlayer) {
                        destinationPos = ((EntityPlayer) entity).getBedLocation(0);
                        if (destinationPos == null) {
                            return 0;
                        }
                    }
                    else {
                        return 0F;
                    }

                    double d0;

                    if (destinationPos != null) {
                        double d1 = flag ? (double) entity.rotationYaw : getFrameRotation((EntityItemFrame) entity);
                        d1 = d1 % 360.0D;
                        double d2 = getPosToAngle(worldIn, entity, destinationPos);
                        d0 = Math.PI - ((d1 - 90.0D) * 0.01745329238474369D - d2);
                    } else {
                        d0 = Math.random() * (Math.PI * 2D);
                    }

                    if (flag) {
                        d0 = wobble(worldIn, d0);
                    }

                    float f = (float) (d0 / (Math.PI * 2D));
                    return MathHelper.positiveModulo(f, 1.0F);
                }
            }

            @SideOnly(Side.CLIENT)
            private double wobble(World p_185093_1_, double p_185093_2_) {
                if (p_185093_1_.getTotalWorldTime() != lastUpdateTick) {
                    lastUpdateTick = p_185093_1_.getTotalWorldTime();
                    double d0 = p_185093_2_ - rotation;
                    d0 = d0 % (Math.PI * 2D);
                    d0 = MathHelper.clamp_double(d0, -1.0D, 1.0D);
                    rota += d0 * 0.1D;
                    rota *= 0.8D;
                    rotation += rota;
                }

                return rotation;
            }

            @SideOnly(Side.CLIENT)
            private double getFrameRotation(EntityItemFrame itemFrame) {
                return (double) MathHelper.clampAngle(180 + itemFrame.facingDirection.getHorizontalIndex() * 90);
            }

            @SideOnly(Side.CLIENT)
            private double getPosToAngle(World world, Entity entity, BlockPos blockpos) {
                return Math.atan2((double) blockpos.getZ() - entity.posZ, (double) blockpos.getX() - entity.posX);
            }
        });

        setUnlocalizedName("compass_personal");
        setCreativeTab(CreativeTabs.TOOLS);
        setRegistryName(Reference.MOD_PREFIX + "compass_personal");

        GameRegistry.register(this);
        GameRegistry.addRecipe(new ItemStack(this), " I ", "IRI", " I ", 'I', Items.IRON_INGOT, 'R', Items.REDSTONE);
        ModelLoader.setCustomModelResourceLocation(this, 0, new ModelResourceLocation("iberia:compass_personal", "inventory"));
    }
}

Link to comment
Share on other sites

It is because you are using the IItemPropertyGetter inside your constructor, so the players spawn point wont change, because the constructor is only called once.

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.

Link to comment
Share on other sites

It is because you are using the IItemPropertyGetter inside your constructor, so the players spawn point wont change, because the constructor is only called once.

 

This is literally gibberish. The IItemPropertyGetter interface sets up a callback method which is invoked when the item is rendered, all the time, every time.

 

Unless you somehow think that during preInit (before! The title screen) there is a player object and a world to look at...

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.

Link to comment
Share on other sites

Right, I'm setting up the IItemPropertyGetter the same way it is set up in the vanilla compass class, so I feel good about that. But I think there must be some difference between the EntityPlayer being passed into the apply method and the EntityPlayer being passed to the PlayerSetSpawn event

Link to comment
Share on other sites

Ok, here's my current theory, after further investigation. The EntityPlayer that gets passed to the IItemPropertyGetter.apply is an AbstractClientPlayer and thus on the client side. I believe that EntityPlayer never has it's spawnChunk field updated when the player's spawn is set (that happens on the server), and the EntityPlayer object on the server side that does have it's spawnChunk set is never communicating that information to the client side.

 

So it appears I need some way, given an AbstractClientPlayer, to ask the server for that player's bed location / spawnChunk. Once that's in place, I'll need a way of caching that info on the client side associated with the AbstractClientPlayer so I'm not asking for it every time we render a compass. I'll be investigating how to do that next, but any tips/pointers to get me started would be awesome.

Link to comment
Share on other sites

Packets, basically.  The client asks the server "hey, were's my bed?" when it logs in.

When asked, or when the bed is set, the server sends that client the information.

 

You don't need to do anything with the AbstractClientPlayer because when the client asks the server, the server already knows which non-abstract player is asking (due to the message context).

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.

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.

Announcements



×
×
  • Create New...

Important Information

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