Jump to content

Weird bug that I can't figure out


KGS

Recommended Posts

So I have a block called a Stopper that whenever it is placed registers its own position within some datastructure called the Tracker. The tracker itself keeps track of the number of stoppers in a chunk. When a Stopper is placed, the Stopper tells the Tracker where the Stopper was placed. The Tracker takes the block coordinates AND the worldID and converts them to chunk coordinates, and then sticks the chunk coordinates and the world id in a custom class (ChunkLocation) which is used as a key in a hash table.

 

The key is used to identify an integer in the Tracker's hash table representing how many stoppers are currently in a particular chunk in a particular dimension. The reason for this is I want some way to quickly look up whether or not there is a stopper in any particular chunk. I suspect this is not the ideal way to keep track of this.

 

// This code is in the Stopper
public void onBlockAdded(World world, int x, int y, int z) {
KGBlocks.tracker.addStopper(x, y, z, world.provider.dimensionId);
}

 

// This is the code in the Tracker
public void addStopper(int x, int y, int z, int w) {
ChunkLocation t = getLocation(x, y, z, w); // This creates a new ChunkLocation from the given coordinates.
if (chunk.containsKey(t)) {
	int counter = chunk.get(t);
	counter++;
	chunk.put(t, counter);
} else{
	chunk.put(t, 1);
}

this.markDirty();
}

 

The bug that occurs is that if I place multiple blocks in quick succession, it seems to register more Stoppers than I place. I get duplicate updates to the hash table. The integer sometimes updates twice instead of once.

 

I currently don't have code to remove stoppers from the tracker, but that will be added when I have this code working.

 

Edit: Maybe it would be safer to store lists instead of integers, and then make the lists contain coordinates for stoppers within a chunk. Checking whether or not there is a stopper within a chunk would then only require checking whether or not that list exists within the tracker OR if the list length is zero.

Link to comment
Share on other sites

diesieben07: My ChunkLocation has both hashCode and equals properly implemented. I don't have the code at hand at the moment, but hashCode implementation is based on the one used in Effective Java.

 

The Multiset looks interesting. This is probably going to be my approach.

 

Stopper doesn't have a tile entity, but your solution proposal is interesting.

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.