Jump to content

[1.11] Custom Compass - Point to Nearest Block of Certain Type


SirJoseph

Recommended Posts

So I have a custom compass in my mod and have got it working and pointing to x=0, z=0, but I wanted to have it point to a custom block's coordinates from my mod, if it exists in the world, if not point to x=0, z=0, or better yet the players location.

 

Here is the class file, I took out the lines with the package and imports because its not needed.

 

 

 


public class ItemNewCompass extends Item implements ItemModelProvider
{
protected String name;
public static int nearest_z = 0;
public static int nearest_x = 0;

    public ItemNewCompass(String name)
    {
    	this.name = name;
	setUnlocalizedName(name);
	setRegistryName(name);

        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.worldObj;
                    }

                    double d0;

                    if (worldIn.provider.isSurfaceWorld())
                    {
                        double d1 = flag ? (double)entity.rotationYaw : this.getFrameRotation((EntityItemFrame)entity);
                        d1 = MathHelper.func_191273_b(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.func_191273_b(d0 + 0.5D, 1.0D) - 0.5D;
                    this.rota += d0 * 0.1D;
                    this.rota *= 0.8D;
                    this.rotation = MathHelper.func_191273_b(this.rotation + this.rota, 1.0D);
                }

                return this.rotation;
            }
            @SideOnly(Side.CLIENT)
            private double getFrameRotation(EntityItemFrame p_185094_1_)
            {
                return (double)MathHelper.clampAngle(180 + p_185094_1_.facingDirection.getHorizontalIndex() * 90);
            }
            @SideOnly(Side.CLIENT)
            private double getSpawnToAngle(World p_185092_1_, Entity p_185092_2_)
            {
                BlockPos blockpos = p_185092_1_.getSpawnPoint();
                return Math.atan2(nearest_z, nearest_x);
            }
        });
    }
    
    @Override
    public void registerItemModel(Item item)
{
	CompassMod.proxy.registerItemRenderer(this, 0, name);
}

@Override
public ItemNewCompass setCreativeTab(CreativeTabs tab) 
{
	super.setCreativeTab(tab);
	return this;
}

}

 

 

 

Everything works fine as far as it points to x = 0, z = 0, and the custom images are working.

 

What I don't know is, how to do I change the x and z coordinates to the nearest block of a certain type if it exists. I figure if I can get it to point to one of the vanilla blocks I could make it point to a mod block. If anyone knows how to do this I would appreciate the help. Thank you guys! :)

 

Link to comment
Share on other sites

The problem with this is:

 

How far away are you willing to search?

Or are you willing to track the blocks' positions another way?

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

If they're being placed into the world as part of worldgen and there aren't that many of them, they're easy to keep track of in a separate class: as one generates, push its location to a list of known.  When destroyed, remove it.

 

If they're preset (i.e. always at 123,45,987) then you can know where the nearest is before it's even been generated.  Vanilla sort of does this with end portals, though the code is stupidly complex.

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

Thank you. I am try to get the blocks to generate in the world right now, so I will add the coordinates to each one and make another file to keep track of the ones I've found. I'll use a simple distance formula to find the nearest one. I'll share my results when I'm finished or if I run into a problem. :) Thanks again Draco!

Link to comment
Share on other sites

You'll also want to save and load the data.

Feel free to take a look through this:

https://github.com/Draco18s/ReasonableRealism/tree/master/src/main/java/com/draco18s/flowers

Start with the event handler. My data is more complex than yours, but functions similarly (you will also want the vanilla generation events, not COG's).

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

Fixed a typo, stay -> start.

The event handler takes 3 events (effectively, save load and generate) and passes it off to the data structure that holds the information so that it cansave to disk,  read from disk, or create data from the world.  Save/load is just NBT writing.

 

But there's also an Unload event so you can remove data from the data structure when the associated chunk is no longer in memory (avoiding a memory leak).

 

You'll also need lookup ("get nearest") and modify ("block permanently removed") methods.

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.

×
×
  • Create New...

Important Information

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