From 85ff28298ebddd8a9e2bc003ba3838f788d0cc40 Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Thu, 10 Jul 2014 18:21:10 -0400 Subject: [PATCH] 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. --- .../mod_pocketDim/core/DimLink.java | 7 +- .../mod_pocketDim/core/NewDimData.java | 70 ++++++++++++++++--- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/src/main/java/StevenDimDoors/mod_pocketDim/core/DimLink.java b/src/main/java/StevenDimDoors/mod_pocketDim/core/DimLink.java index 8b0e3d8f..409b8158 100644 --- a/src/main/java/StevenDimDoors/mod_pocketDim/core/DimLink.java +++ b/src/main/java/StevenDimDoors/mod_pocketDim/core/DimLink.java @@ -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 @@ -95,6 +95,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() diff --git a/src/main/java/StevenDimDoors/mod_pocketDim/core/NewDimData.java b/src/main/java/StevenDimDoors/mod_pocketDim/core/NewDimData.java index 5d7e50af..5045b9da 100644 --- a/src/main/java/StevenDimDoors/mod_pocketDim/core/NewDimData.java +++ b/src/main/java/StevenDimDoors/mod_pocketDim/core/NewDimData.java @@ -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>(); 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; } @@ -297,13 +302,26 @@ public abstract class NewDimData public DimLink createLink(Point4D source, int linkType, int orientation) { - //Return an existing link if there is one to avoid creating multiple links starting at the same point. + // Return an existing link if there is one to avoid creating multiple links starting at the same point. InnerDimLink link = linkMapping.get(source); if (link == null) { 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 chunkLinks = chunkMapping.get(chunk); + if (chunkLinks == null) + { + chunkLinks = new ArrayList(EXPECTED_LINKS_PER_CHUNK); + chunkMapping.put(chunk, chunkLinks); + } + chunkLinks.add(link); + } } else { @@ -311,7 +329,7 @@ public abstract class NewDimData } modified = true; - //Link created! + // Link created! if (linkType != LinkTypes.CLIENT_SIDE) { linkWatcher.onCreated(link.link); @@ -331,8 +349,8 @@ public abstract class NewDimData private DimLink createChildLink(Point4D source, InnerDimLink parent) { - //To avoid having multiple links at a single point, if we find an existing link then we overwrite - //its destination data instead of creating a new instance. + // To avoid having multiple links at a single point, if we find an existing link then we overwrite + // its destination data instead of creating a new instance. InnerDimLink link = linkMapping.get(source); if (link == null) @@ -341,14 +359,28 @@ public abstract class NewDimData linkMapping.put(source, link); linkList.add(link); - //Link created! + // 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 chunkLinks = chunkMapping.get(chunk); + if (chunkLinks == null) + { + chunkLinks = new ArrayList(EXPECTED_LINKS_PER_CHUNK); + chunkMapping.put(chunk, chunkLinks); + } + chunkLinks.add(link); + } + + // Link created! linkWatcher.onCreated(link.link); } else { if (link.overwrite(parent, parent.link.orientation)) { - //Link created! + // Link created! linkWatcher.onCreated(link.link); } } @@ -366,7 +398,19 @@ public abstract class NewDimData if (target != null) { linkList.remove(target); - //Raise deletion event + + // 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 chunkLinks = chunkMapping.get(chunk); + if (chunkLinks != null) + { + chunkLinks.remove(target); + } + } + + // Raise deletion event linkWatcher.onDeleted(target.link); target.clear(); modified = true; @@ -388,7 +432,7 @@ public abstract class NewDimData public DimLink getLink(Point3D location) { - return linkMapping.get(new Point4D(location.getX(),location.getY(),location.getZ(),this.id)); + return linkMapping.get(new Point4D(location.getX(), location.getY(), location.getZ(), this.id)); } public DimLink getLink(Point4D location) @@ -618,6 +662,16 @@ public abstract class NewDimData return linkList.get(0); } + public Iterable getChunkLinks(int chunkX, int chunkZ) + { + List chunkLinks = chunkMapping.get(new ChunkCoordIntPair(chunkX, chunkZ)); + if (chunkLinks != null) + { + return chunkLinks; + } + return new ArrayList(0); + } + public boolean isModified() { return modified;