This commit is contained in:
asiekierka 2015-06-10 23:41:38 +02:00
parent dc090c0e46
commit 26fa22c521
6 changed files with 76 additions and 26 deletions

View file

@ -18,6 +18,7 @@ import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLInitializationEvent;
@ -391,6 +392,7 @@ public class BuildCraftRobotics extends BuildCraftMod {
managerThread.interrupt();
MinecraftForge.EVENT_BUS.unregister(manager);
FMLCommonHandler.instance().bus().unregister(manager);
}
managerThread = null;
@ -412,6 +414,7 @@ public class BuildCraftRobotics extends BuildCraftMod {
managerThread.start();
MinecraftForge.EVENT_BUS.register(manager);
FMLCommonHandler.instance().bus().register(manager);
}
@Mod.EventHandler

View file

@ -82,7 +82,7 @@ public class TileTank extends TileBuildCraft implements IFluidHandler {
}
if (hasUpdate) {
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, blockType);
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, getBlockType());
hasUpdate = false;
}

View file

@ -0,0 +1,12 @@
package buildcraft.robotics.map;
public class MapChunkLoadRequest {
public final MapWorld world;
public final int x, z;
public MapChunkLoadRequest(MapWorld world, int x, int z) {
this.world = world;
this.x = x;
this.z = z;
}
}

View file

@ -2,17 +2,21 @@ package buildcraft.robotics.map;
import java.io.File;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import com.google.common.collect.HashBiMap;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.ChunkEvent;
public class MapManager implements Runnable {
private final HashBiMap<World, MapWorld> worldMap = HashBiMap.create();
private final Set<MapChunkLoadRequest> chunkLoadRequests = new HashSet<MapChunkLoadRequest>();
private final File location;
private boolean stop = false;
@ -99,4 +103,22 @@ public class MapManager implements Runnable {
}
}
}
@SubscribeEvent
public void serverTickEnd(TickEvent.ServerTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
synchronized (chunkLoadRequests) {
for (MapChunkLoadRequest r : chunkLoadRequests) {
r.world.updateChunk(r.x, r.z);
}
chunkLoadRequests.clear();
}
}
}
public void loadChunkForUpdate(MapWorld mapWorld, int x, int z) {
synchronized (chunkLoadRequests) {
chunkLoadRequests.add(new MapChunkLoadRequest(mapWorld, x, z));
}
}
}

View file

@ -57,7 +57,9 @@ public class MapRegion implements INBTStoreable {
MapChunk chunk = chunks.get(i);
if (chunk != null) {
NBTTagCompound chunkNBT = new NBTTagCompound();
chunk.writeToNBT(chunkNBT);
synchronized (chunk) {
chunk.writeToNBT(chunkNBT);
}
tag.setTag("r" + i, chunkNBT);
}
}

View file

@ -18,6 +18,7 @@ import gnu.trove.map.hash.TLongObjectHashMap;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import buildcraft.BuildCraftRobotics;
import buildcraft.core.lib.utils.NBTUtils;
public class MapWorld {
@ -117,8 +118,9 @@ public class MapWorld {
}
public void queueChunkForUpdate(int x, int z, int priority) {
long id = getXzId(x, z);
queuedChunks.add(new QueuedXZ(x, z, priority));
synchronized (queuedChunks) {
queuedChunks.add(new QueuedXZ(x, z, priority));
}
}
public void queueChunkForUpdateIfEmpty(int x, int z, int priority) {
@ -128,27 +130,30 @@ public class MapWorld {
}
public void updateChunkInQueue() {
if (queuedChunks.size() == 0) {
return;
}
QueuedXZ q = queuedChunks.remove();
if (q == null) {
return;
}
if (!world.getChunkProvider().chunkExists(q.x, q.z)) {
long now = (new Date()).getTime();
if (now - lastForcedChunkLoad < 1000) {
q.p++; // Increase priority so it gets looked at later
queuedChunks.add(q);
synchronized (queuedChunks) {
if (queuedChunks.size() == 0) {
return;
} else {
lastForcedChunkLoad = now;
}
}
updateChunk(q.x, q.z);
QueuedXZ q = queuedChunks.remove();
if (q == null) {
return;
}
if (!world.getChunkProvider().chunkExists(q.x, q.z)) {
long now = (new Date()).getTime();
if (now - lastForcedChunkLoad < 3000) {
q.p++; // Increase priority so it gets looked at later
queuedChunks.add(q);
return;
} else {
lastForcedChunkLoad = now;
BuildCraftRobotics.manager.loadChunkForUpdate(this, q.x, q.z);
}
}
updateChunk(q.x, q.z);
}
}
public void save() {
@ -186,12 +191,18 @@ public class MapWorld {
return chunk.getColor(x & 15, z & 15);
}
protected void updateChunk(int x, int z) {
public void updateChunk(int x, int z) {
MapChunk chunk = getChunk(x, z);
chunk.update(world.getChunkFromChunkCoords(x, z));
regionUpdateSet.add(new QueuedXZ(x >> 4, z >> 4, 0));
synchronized (chunk) {
chunk.update(world.getChunkFromChunkCoords(x, z));
}
synchronized (regionUpdateSet) {
regionUpdateSet.add(new QueuedXZ(x >> 4, z >> 4, 0));
}
// priority does not matter - see equals
queuedChunks.remove(new QueuedXZ(x, z, 0));
synchronized (queuedChunks) {
queuedChunks.remove(new QueuedXZ(x, z, 0));
}
}
}