*Fix for other clipboard implementations

This commit is contained in:
Jesse Boyd 2016-08-15 11:39:44 +10:00
parent fb9bdeca7d
commit fb33ecf71e
4 changed files with 72 additions and 24 deletions

View File

@ -1,9 +1,9 @@
package com.boydti.fawe.object; package com.boydti.fawe.object;
public class IntegerTrio { public class IntegerTrio {
private final int z; public final int z;
private final int x; public final int x;
private final int y; public final int y;
public IntegerTrio(int x, int y, int z) { public IntegerTrio(int x, int y, int z) {
this.x = x; this.x = x;

View File

@ -1,6 +1,7 @@
package com.boydti.fawe.object.clipboard; package com.boydti.fawe.object.clipboard;
import com.boydti.fawe.FaweCache; import com.boydti.fawe.FaweCache;
import com.boydti.fawe.object.IntegerTrio;
import com.boydti.fawe.object.RunnableVal2; import com.boydti.fawe.object.RunnableVal2;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
@ -12,20 +13,9 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
public class CPUOptimizedClipboard extends FaweClipboard { public class CPUOptimizedClipboard extends FaweClipboard {
public CPUOptimizedClipboard(int width, int height, int length) {
this.width = width;
this.height = height;
this.length = length;
this.area = width * length;
this.volume = area * height;
ids = new byte[volume];
datas = new byte[volume];
nbtMap = new HashMap<>();
entities = new HashSet<>();
}
private int length; private int length;
private int height; private int height;
private int width; private int width;
@ -34,9 +24,42 @@ public class CPUOptimizedClipboard extends FaweClipboard {
private byte[] ids; private byte[] ids;
private byte[] datas; private byte[] datas;
private byte[] add; private byte[] add;
private final HashMap<Integer, CompoundTag> nbtMap;
private final HashMap<IntegerTrio, CompoundTag> nbtMapLoc;
private final HashMap<Integer, CompoundTag> nbtMapIndex;
private final HashSet<ClipboardEntity> entities; private final HashSet<ClipboardEntity> entities;
public CPUOptimizedClipboard(int width, int height, int length) {
this.width = width;
this.height = height;
this.length = length;
this.area = width * length;
this.volume = area * height;
ids = new byte[volume];
datas = new byte[volume];
nbtMapLoc = new HashMap<>();
nbtMapIndex = new HashMap<>();
entities = new HashSet<>();
}
public void convertTilesToIndex() {
if (nbtMapLoc.isEmpty()) {
return;
}
for (Map.Entry<IntegerTrio, CompoundTag> entry : nbtMapLoc.entrySet()) {
IntegerTrio key = entry.getKey();
nbtMapIndex.put(getIndex(key.x, key.y, key.z), entry.getValue());
}
nbtMapLoc.clear();
}
private CompoundTag getTag(int index) {
convertTilesToIndex();
return nbtMapIndex.get(index);
}
public int getId(int index) { public int getId(int index) {
if (add != null) { if (add != null) {
return ids[index] & 0xFF + add[index] & 0xFF; return ids[index] & 0xFF + add[index] & 0xFF;
@ -115,7 +138,7 @@ public class CPUOptimizedClipboard extends FaweClipboard {
block = FaweCache.getBlock(id, 0); block = FaweCache.getBlock(id, 0);
} }
if (FaweCache.hasNBT(id)) { if (FaweCache.hasNBT(id)) {
CompoundTag nbt = nbtMap.get(index); CompoundTag nbt = getTag(index);
if (nbt != null) { if (nbt != null) {
block = new BaseBlock(block.getId(), block.getData()); block = new BaseBlock(block.getId(), block.getData());
block.setNbtData(nbt); block.setNbtData(nbt);
@ -145,7 +168,7 @@ public class CPUOptimizedClipboard extends FaweClipboard {
@Override @Override
public boolean setTile(int x, int y, int z, CompoundTag tag) { public boolean setTile(int x, int y, int z, CompoundTag tag) {
nbtMap.put(getIndex(x, y, z), tag); nbtMapLoc.put(new IntegerTrio(x, y, z), tag);
return true; return true;
} }
@ -159,7 +182,7 @@ public class CPUOptimizedClipboard extends FaweClipboard {
setData(index, (byte) block.getData()); setData(index, (byte) block.getData());
CompoundTag tile = block.getNbtData(); CompoundTag tile = block.getNbtData();
if (tile != null) { if (tile != null) {
nbtMap.put(index, tile); nbtMapIndex.put(index, tile);
} }
return true; return true;
} }

View File

@ -2,6 +2,7 @@ package com.boydti.fawe.object.clipboard;
import com.boydti.fawe.FaweCache; import com.boydti.fawe.FaweCache;
import com.boydti.fawe.config.Settings; import com.boydti.fawe.config.Settings;
import com.boydti.fawe.object.IntegerTrio;
import com.boydti.fawe.object.RunnableVal2; import com.boydti.fawe.object.RunnableVal2;
import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MainUtil;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
@ -14,6 +15,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
public class MemoryOptimizedClipboard extends FaweClipboard { public class MemoryOptimizedClipboard extends FaweClipboard {
@ -33,7 +35,9 @@ public class MemoryOptimizedClipboard extends FaweClipboard {
private byte[] buffer = new byte[MainUtil.getMaxCompressedLength(BLOCK_SIZE)]; private byte[] buffer = new byte[MainUtil.getMaxCompressedLength(BLOCK_SIZE)];
private final HashMap<Integer, CompoundTag> nbtMap; private final HashMap<IntegerTrio, CompoundTag> nbtMapLoc;
private final HashMap<Integer, CompoundTag> nbtMapIndex;
private final HashSet<ClipboardEntity> entities; private final HashSet<ClipboardEntity> entities;
private int lastIdsI = -1; private int lastIdsI = -1;
@ -62,11 +66,28 @@ public class MemoryOptimizedClipboard extends FaweClipboard {
this.volume = area * height; this.volume = area * height;
ids = new byte[1 + (volume >> BLOCK_SHIFT)][]; ids = new byte[1 + (volume >> BLOCK_SHIFT)][];
datas = new byte[1 + (volume >> BLOCK_SHIFT)][]; datas = new byte[1 + (volume >> BLOCK_SHIFT)][];
nbtMap = new HashMap<>(); nbtMapLoc = new HashMap<>();
nbtMapIndex = new HashMap<>();
entities = new HashSet<>(); entities = new HashSet<>();
this.compressionLevel = compressionLevel; this.compressionLevel = compressionLevel;
} }
public void convertTilesToIndex() {
if (nbtMapLoc.isEmpty()) {
return;
}
for (Map.Entry<IntegerTrio, CompoundTag> entry : nbtMapLoc.entrySet()) {
IntegerTrio key = entry.getKey();
nbtMapIndex.put(getIndex(key.x, key.y, key.z), entry.getValue());
}
nbtMapLoc.clear();
}
private CompoundTag getTag(int index) {
convertTilesToIndex();
return nbtMapIndex.get(index);
}
public int getId(int index) { public int getId(int index) {
int i = index >> BLOCK_SHIFT; int i = index >> BLOCK_SHIFT;
if (i == lastIdsI) { if (i == lastIdsI) {
@ -290,7 +311,7 @@ public class MemoryOptimizedClipboard extends FaweClipboard {
block = FaweCache.getBlock(id, 0); block = FaweCache.getBlock(id, 0);
} }
if (FaweCache.hasNBT(id)) { if (FaweCache.hasNBT(id)) {
CompoundTag nbt = nbtMap.get(index); CompoundTag nbt = getTag(index);
if (nbt != null) { if (nbt != null) {
block = new BaseBlock(block.getId(), block.getData()); block = new BaseBlock(block.getId(), block.getData());
block.setNbtData(nbt); block.setNbtData(nbt);
@ -338,7 +359,7 @@ public class MemoryOptimizedClipboard extends FaweClipboard {
@Override @Override
public boolean setTile(int x, int y, int z, CompoundTag tag) { public boolean setTile(int x, int y, int z, CompoundTag tag) {
nbtMap.put(getIndex(x, y, z), tag); nbtMapLoc.put(new IntegerTrio(x, y, z), tag);
return true; return true;
} }
@ -352,7 +373,7 @@ public class MemoryOptimizedClipboard extends FaweClipboard {
setData(index, (byte) block.getData()); setData(index, (byte) block.getData());
CompoundTag tile = block.getNbtData(); CompoundTag tile = block.getNbtData();
if (tile != null) { if (tile != null) {
nbtMap.put(index, tile); nbtMapIndex.put(index, tile);
} }
return true; return true;
} }

View File

@ -83,6 +83,10 @@ public class MathMan {
return (byte) ((value >> 4) & 0xF); return (byte) ((value >> 4) & 0xF);
} }
public static int lossyFastDivide(int a, int b) {
return (a*((1<<16)/b))>>16;
}
public static int sqrt(int x) { public static int sqrt(int x) {
int xn; int xn;