Implemented Chunk-Links Mapping

Implemented support for tracking the list of links in each chunk in a
dimension. This will be used for scheduling rift regeneration when
chunks load.
This commit is contained in:
SenseiKiwi 2014-07-10 18:21:10 -04:00
parent 782c6d5e50
commit 85ff28298e
2 changed files with 68 additions and 9 deletions

View file

@ -3,6 +3,7 @@ package StevenDimDoors.mod_pocketDim.core;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.world.ChunkCoordIntPair;
import StevenDimDoors.mod_pocketDim.util.Point4D;
import StevenDimDoors.mod_pocketDim.watcher.ClientLinkData;
@ -15,7 +16,6 @@ public abstract class DimLink
protected DimLink(ClientLinkData link, DimLink parent)
{
if (parent.link.point.getDimension() != link.point.getDimension())
{
// Ban having children in other dimensions to avoid serialization issues with cross-dimensional tails
@ -96,6 +96,11 @@ public abstract class DimLink
return tail.getLinkType();
}
public ChunkCoordIntPair getChunkCoordinates()
{
return new ChunkCoordIntPair(link.point.getX() >> 4, link.point.getZ() >> 4);
}
@Override
public String toString()
{

View file

@ -1,6 +1,7 @@
package StevenDimDoors.mod_pocketDim.core;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
@ -117,6 +118,8 @@ public abstract class NewDimData
}
}
private static int EXPECTED_LINKS_PER_CHUNK = 2;
protected static Random random = new Random();
protected int id;
@ -163,6 +166,7 @@ public abstract class NewDimData
this.origin = null;
this.dungeon = null;
this.linkWatcher = linkWatcher;
this.chunkMapping = new HashMap<ChunkCoordIntPair, List<InnerDimLink>>();
this.modified = true;
//Register with parent
@ -203,6 +207,7 @@ public abstract class NewDimData
this.linkWatcher = null;
this.depth = 0;
this.root = root;
this.chunkMapping = null;
}
@ -304,6 +309,19 @@ public abstract class NewDimData
link = new InnerDimLink(source, linkType, orientation);
linkMapping.put(source, link);
linkList.add(link);
// If this code is running on the server side, add this link to chunkMapping.
if (linkType != LinkTypes.CLIENT_SIDE)
{
ChunkCoordIntPair chunk = link.getChunkCoordinates();
List<InnerDimLink> chunkLinks = chunkMapping.get(chunk);
if (chunkLinks == null)
{
chunkLinks = new ArrayList<InnerDimLink>(EXPECTED_LINKS_PER_CHUNK);
chunkMapping.put(chunk, chunkLinks);
}
chunkLinks.add(link);
}
}
else
{
@ -341,6 +359,20 @@ public abstract class NewDimData
linkMapping.put(source, link);
linkList.add(link);
// If this code is running on the server side, add this link to chunkMapping.
// Granted, the client side code should never create child links anyway...
if (link.linkType() != LinkTypes.CLIENT_SIDE)
{
ChunkCoordIntPair chunk = link.getChunkCoordinates();
List<InnerDimLink> chunkLinks = chunkMapping.get(chunk);
if (chunkLinks == null)
{
chunkLinks = new ArrayList<InnerDimLink>(EXPECTED_LINKS_PER_CHUNK);
chunkMapping.put(chunk, chunkLinks);
}
chunkLinks.add(link);
}
// Link created!
linkWatcher.onCreated(link.link);
}
@ -366,6 +398,18 @@ public abstract class NewDimData
if (target != null)
{
linkList.remove(target);
// If this code is running on the server side, remove this link to chunkMapping.
if (link.linkType() != LinkTypes.CLIENT_SIDE)
{
ChunkCoordIntPair chunk = target.getChunkCoordinates();
List<InnerDimLink> chunkLinks = chunkMapping.get(chunk);
if (chunkLinks != null)
{
chunkLinks.remove(target);
}
}
// Raise deletion event
linkWatcher.onDeleted(target.link);
target.clear();
@ -618,6 +662,16 @@ public abstract class NewDimData
return linkList.get(0);
}
public Iterable<? extends DimLink> getChunkLinks(int chunkX, int chunkZ)
{
List<InnerDimLink> chunkLinks = chunkMapping.get(new ChunkCoordIntPair(chunkX, chunkZ));
if (chunkLinks != null)
{
return chunkLinks;
}
return new ArrayList<InnerDimLink>(0);
}
public boolean isModified()
{
return modified;