Jump to content

[Solved] >> 4 or /16 for getting chunk?


jredfox

Recommended Posts

Edit: use >> 4

>> 4 or /16 for getting chunk? I have read that >>4 is more optimized yet it fails on negative numbers I am confused since vanilla uses >>4 when it's negative???? I need it for teleporting entities now because of a vanilla bug with the entities not being added to chunks when teleporting if the chunk isn't loaded(never) or not being added to next tick

	/**
     *supports teleporting entities from location to location in the same dimension doesn't support actual cross dimensional teleport
     */
    public static void doTeleport(Entity e, double x, double y, double z,float yaw, float pitch)
    {
        int chunkOldX = (int)e.posX >> 4;
 	int chunkOldZ = (int)e.posZ >> 4;
        int chunkX = (int)x >> 4;
	int chunkZ = (int)z >> 4;
 	//remove from old chunk
        if(chunkOldX != chunkX || chunkOldZ != chunkZ)
        {
        	Chunk chunkOld = e.world.getChunkFromChunkCoords(chunkOldX,chunkOldZ);
        	chunkOld.removeEntity(e);
        }
 		
        if (e instanceof EntityPlayerMP)
        {
            Set<SPacketPlayerPosLook.EnumFlags> set = EnumSet.<SPacketPlayerPosLook.EnumFlags>noneOf(SPacketPlayerPosLook.EnumFlags.class);
            e.dismountRidingEntity();
            e.setLocationAndAngles(x, y, z, e.rotationYaw, e.rotationPitch);
            ((EntityPlayerMP)e).connection.setPlayerLocation(x, y, z, yaw, pitch, set);
        }
        else
        {
            float f2 = (float)MathHelper.wrapDegrees(yaw);
            float f3 = (float)MathHelper.wrapDegrees(pitch);
            f3 = MathHelper.clamp(f3, -90.0F, 90.0F);
            e.setLocationAndAngles(x, y, z, f2, f3);    
        }
        
        if (!(e instanceof EntityLivingBase) || !((EntityLivingBase)e).isElytraFlying())
        {
            e.motionY = 0.0D;
            e.onGround = true;
        }
        //vanilla hotfix add entity to the new chunk if not already added
        Chunk chunk = e.world.getChunkFromChunkCoords(chunkX,chunkZ);
        if(!containsEntity(chunk.getEntityLists(),e))
        	chunk.addEntity(e);
    }

 

	public static boolean containsEntity(ClassInheritanceMultiMap<Entity>[] list,Entity e) 
	{
		for(int i=0;i<list.length;i++)
		{
			ClassInheritanceMultiMap map = list[i];
			Iterator<Entity> it = map.iterator();
			while(it.hasNext())
			{
				Entity e2 = it.next();
				if(e == e2)
					return true;
			}
		}
		return false;
	}

 

Edited by jredfox
Link to comment
Share on other sites

>> 4
Because of negative coordinates.

 

15 >> 4 = 0

15 / 16 = 0

-15 >> 4 = -1

-15 / 16 = 0

 

Edited by Draco18s

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

wtf.png

WTF?

Capture3.png

Edited by Draco18s

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

8 hours ago, jredfox said:

so your saying it's proper to do division then ? why is there an x there then as if that failed?

Jesus Christ, you're as dumb as a post. No, only use >> 4, as I already said twice.

The X is there because it failed to produce the correct result, therefore it is wrong and should not be used.

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

37 minutes ago, jabelar said:

JRedFox was agreeing with you. I'm not sure why you said WTF. You said use bit shifting and he said "okay I'll use bit shifting" then you said "WTF".

The post was edited. I can't view the edit history, but I'm almost certain he said "division" when I made my reply. I've slept since then.

He also has "or MathHelper" in there, and MathHelper does not contain a "BlockToChunk" method (there is exactly one instance of >>4 in it: smallestEncompassingPowerOfTwo), so also wrong.

Edited by Draco18s

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.