Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

How to get the server world from a chunk generator


 Share

Recommended Posts

The problem here really is mutability. You need to have the data that is shared between the two threads in an immutable data class (basically a class with all final fields, and no mutable collections in it). Then you need to have that data in a ConcurrentHashMap and then you can safely read it from the other thread.

Link to comment
Share on other sites

Yeah maybe it would be a better idea to have a separate immutable struct inside of my TardisManager.Tardis called TardisGenerationInfo or something that was always written in a thread safe way and was the only thing that the chunk generator was allowed to read from.

So the reference to tardisManager is gotten through a ConcurrentMap, as well as the reference to the specific Tardis. so maybe this new object is stored in a separate hash map, or a reference to it can only be used from within the Tardis data structure and contains basically an immutable structure that be accesed by the chunk generator and if any of the data updates, a lock is clicked denying read access, the reference is replaced with a new instance of TardisGenerationInfo and then the lock is unlocked.

actually if I made a system like this would I need to make it all immutable fields each read and write into this class was dependent on the lock.

Link to comment
Share on other sites

Ok so I the version I am currently using looks a bit like this.

I still have something that looks like this in order to pull a TardisManager.

public static final Map<World,TardisManager> INSTANCES = new MapMaker().weakKeys().makeMap();

and the TardisManager contains this map.

private final ConcurrentHashMap<Integer,Tardis> ALL_TARDISES = new ConcurrentHashMap();

so I can then pull a tardis object, I then have some stuff that looks like this inside of the Tardis class

private ReentrantReadWriteLock tardis_lock;
public InteriorStyle int_style;

public InteriorStyle getIntStyle() {
	tardis_lock.readLock().lock();
	InteriorStyle ret_style = int_style;
	tardis_lock.readLock().unlock();
	return ret_style;
}
public void setIntStyle(InteriorStyle new_style) {
    tardis_lock.writeLock().lock();
    int_style = new_style;
    tardis_lock.writeLock().unlock();
}

and that returns an InteriorStyle object which is an enum so all fields are final.

Is that what I'm supposed to do?

Link to comment
Share on other sites

Ok cool, I changed it to something like this:

public InteriorStyle getIntStyle() {
  try {
    tardis_lock.readLock().lock();
    InteriorStyle ret_style = int_style;
    return ret_style;
  } finally {
    tardis_lock.readLock().unlock();
  }
}

if there is any lock you would recommend over ReentrantReadWriteLock I'd be happy to use that, other wise you have been super helpful and I am very grateful :)

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.

Guest
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.