Skip to content

Commit

Permalink
Experimental SpoutcraftChunk tick changes
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Sanders <[email protected]>
  • Loading branch information
Chris Sanders committed Oct 7, 2014
1 parent 9391678 commit 4b5c05a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void setCustomBlockData(byte data) {
getChunk().setCustomBlockData(x, y, z, data);
}

public void removeCustomBlockData() {
public void removeCustomBlockData() {
removeData(SimpleMaterialManager.blockIdString);
}

Expand Down
71 changes: 36 additions & 35 deletions src/main/java/org/getspout/spout/block/SpoutCraftChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;

import gnu.trove.map.hash.TIntIntHashMap;

Expand All @@ -50,7 +51,9 @@
public class SpoutCraftChunk extends CraftChunk implements SpoutChunk {
protected final ConcurrentHashMap<Integer, Integer> queuedId = new ConcurrentHashMap<Integer, Integer>();
protected final ConcurrentHashMap<Integer, Byte> queuedData = new ConcurrentHashMap<Integer, Byte>();
protected static final Set<SpoutCraftChunk> queuedChunks = Collections.newSetFromMap(new ConcurrentHashMap<SpoutCraftChunk, Boolean>());
//TODO Very experimental...may need to be reverted
protected static final Set<SpoutCraftChunk> queuedChunks = new ConcurrentSkipListSet<SpoutCraftChunk>();
//protected static final Set<SpoutCraftChunk> queuedChunks = Collections.newSetFromMap(new ConcurrentHashMap<SpoutCraftChunk, Boolean>());

public final TIntIntHashMap powerOverrides = new TIntIntHashMap();

Expand All @@ -75,42 +78,41 @@ public Block getBlock(int x, int y, int z) {
return new SpoutCraftBlock(this, (getX() << 4) | (x & 0xF), y & worldHeightMinusOne, (getZ() << 4) | (z & 0xF));
}

private Block getBlockFromPos(int pos) throws IllegalAccessException, NoSuchFieldException {
private Block getBlockFromPos(int pos) {
int x = (pos >> xBitShifts) & 0xF;
int y = (pos >> 0) & worldHeightMinusOne;
int y = (pos) & worldHeightMinusOne;
int z = (pos >> zBitShifts) & 0xF;

return getBlock(x, y, z);

}

public void onTick() {
while (!queuedData.isEmpty() || !queuedId.isEmpty()) {
Iterator<Entry<Integer, Integer>> i = queuedId.entrySet().iterator();
while (i.hasNext()) {
Entry<Integer, Integer> entry = i.next();
try {
Block block = getBlockFromPos(entry.getKey());
block.setTypeId(entry.getValue());
i.remove();
} catch (Exception e) {
}
}
Iterator<Entry<Integer, Byte>> j = queuedData.entrySet().iterator();
while (j.hasNext()) {
Entry<Integer, Byte> entry = j.next();
if (queuedId.isEmpty()) {
try {
Block block = getBlockFromPos(entry.getKey());
block.setData(entry.getValue());
j.remove();
} catch (Exception e) {
}
} else {
break;
}
}
}
//Apply queued ids on blocks
if (!queuedId.isEmpty()) {
final Iterator<Entry<Integer, Integer>> i = queuedId.entrySet().iterator();
while (i.hasNext()) {
final Entry<Integer, Integer> entry = i.next();
Block block = getBlockFromPos(entry.getKey());
block.setTypeId(entry.getValue());
i.remove();
}
}
//Apply queued data on blocks
if (queuedId.isEmpty()) {
final Iterator<Entry<Integer, Byte>> j = queuedData.entrySet().iterator();
while (j.hasNext()) {
//If another thread adds to the id queue, we need to halt and let the next tick apply it
//TODO Even possible, needed? Doesn't add much overhead to check this...
if (!queuedId.isEmpty()) {
break;
}
final Entry<Integer, Byte> entry = j.next();
final Block block = getBlockFromPos(entry.getKey());
block.setData(entry.getValue());
j.remove();
}
}
}

protected void onReset() {
Expand Down Expand Up @@ -143,14 +145,13 @@ private static void replaceAllBukkitChunks(boolean reset) {
worldServer.setAccessible(true);
ChunkProviderServer cps = ((WorldServer) worldServer.get(cw)).chunkProviderServer;
for (Chunk c : cps.chunks.values()) {
Chunk chunk = c;
if (reset) {
if (chunk.bukkitChunk instanceof SpoutCraftChunk) {
((SpoutCraftChunk) chunk.bukkitChunk).onReset();
if (reset) {
if (c.bukkitChunk instanceof SpoutCraftChunk) {
((SpoutCraftChunk) c.bukkitChunk).onReset();
}
resetBukkitChunk(chunk.bukkitChunk);
resetBukkitChunk(c.bukkitChunk);
} else {
replaceBukkitChunk(chunk.bukkitChunk);
replaceBukkitChunk(c.bukkitChunk);
}
}
} catch (Exception e) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/getspout/spoutapi/block/SpoutBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,6 @@ public interface SpoutBlock extends Block {
public byte getCustomBlockData();

public void setCustomBlockData(byte dat);

void onTick();
}

0 comments on commit 4b5c05a

Please sign in to comment.