Spaces + comments.

This commit is contained in:
asofold 2014-08-04 18:38:45 +02:00
parent b2a6962e73
commit eaa4154080
2 changed files with 252 additions and 249 deletions

View File

@ -18,108 +18,107 @@ import fr.neatmonster.nocheatplus.utilities.BlockCache;
public class BlockCacheCBDev extends BlockCache implements IBlockAccess{ public class BlockCacheCBDev extends BlockCache implements IBlockAccess{
/** Box for one time use, no nesting, no extra storing this(!). */ /** Box for one time use, no nesting, no extra storing this(!). */
protected static final AxisAlignedBB useBox = AxisAlignedBB.a(0, 0, 0, 0, 0, 0); protected static final AxisAlignedBB useBox = AxisAlignedBB.a(0, 0, 0, 0, 0, 0);
protected net.minecraft.server.v1_7_R4.WorldServer world; protected net.minecraft.server.v1_7_R4.WorldServer world;
public BlockCacheCBDev(World world) { public BlockCacheCBDev(World world) {
setAccess(world); setAccess(world);
} }
@Override @Override
public void setAccess(World world) { public void setAccess(World world) {
if (world != null) { if (world != null) {
this.maxBlockY = world.getMaxHeight() - 1; this.maxBlockY = world.getMaxHeight() - 1;
this.world = ((CraftWorld) world).getHandle(); this.world = ((CraftWorld) world).getHandle();
} else { } else {
this.world = null; this.world = null;
} }
} }
@Override @Override
public int fetchTypeId(final int x, final int y, final int z) { public int fetchTypeId(final int x, final int y, final int z) {
return world.getTypeId(x, y, z); return world.getTypeId(x, y, z);
} }
@Override @Override
public int fetchData(final int x, final int y, final int z) { public int fetchData(final int x, final int y, final int z) {
return world.getData(x, y, z); return world.getData(x, y, z);
} }
@Override @Override
public double[] fetchBounds(final int x, final int y, final int z){ public double[] fetchBounds(final int x, final int y, final int z){
final int id = getTypeId(x, y, z);
final net.minecraft.server.v1_7_R4.Block block = net.minecraft.server.v1_7_R4.Block.getById(id);
if (block == null) {
// TODO: Convention for null bounds -> full ?
return null;
}
block.updateShape(this, x, y, z);
// TODO: change api for this / use nodes (!) // minX, minY, minZ, maxX, maxY, maxZ
final int id = getTypeId(x, y, z); return new double[]{block.x(), block.z(), block.B(), block.y(), block.A(), block.C()};
final net.minecraft.server.v1_7_R4.Block block = net.minecraft.server.v1_7_R4.Block.getById(id); }
if (block == null) {
return null;
}
block.updateShape(this, x, y, z); // TODO: use THIS instead of world.
// minX, minY, minZ, maxX, maxY, maxZ @Override
return new double[]{block.x(), block.z(), block.B(), block.y(), block.A(), block.C()}; public boolean standsOnEntity(final Entity entity, final double minX, final double minY, final double minZ, final double maxX, final double maxY, final double maxZ){
} try{
// TODO: Find some simplification!
@Override final net.minecraft.server.v1_7_R4.Entity mcEntity = ((CraftEntity) entity).getHandle();
public boolean standsOnEntity(final Entity entity, final double minX, final double minY, final double minZ, final double maxX, final double maxY, final double maxZ){
try{
// TODO: Probably check other ids too before doing this ?
final net.minecraft.server.v1_7_R4.Entity mcEntity = ((CraftEntity) entity).getHandle(); final AxisAlignedBB box = useBox.b(minX, minY, minZ, maxX, maxY, maxZ);
@SuppressWarnings("rawtypes")
final List list = world.getEntities(mcEntity, box);
@SuppressWarnings("rawtypes")
final Iterator iterator = list.iterator();
while (iterator.hasNext()) {
final net.minecraft.server.v1_7_R4.Entity other = (net.minecraft.server.v1_7_R4.Entity) iterator.next();
if (!(other instanceof EntityBoat)){ // && !(other instanceof EntityMinecart)) continue;
continue;
}
if (minY >= other.locY && minY - other.locY <= 0.7){
return true;
}
// Still check this for some reason.
final AxisAlignedBB otherBox = other.boundingBox;
if (box.a > otherBox.d || box.d < otherBox.a || box.b > otherBox.e || box.e < otherBox.b || box.c > otherBox.f || box.f < otherBox.c) {
continue;
}
else {
return true;
}
}
}
catch (Throwable t){
// Ignore exceptions (Context: DisguiseCraft).
}
return false;
}
final AxisAlignedBB box = useBox.b(minX, minY, minZ, maxX, maxY, maxZ); /* (non-Javadoc)
@SuppressWarnings("rawtypes") * @see fr.neatmonster.nocheatplus.utilities.BlockCache#cleanup()
final List list = world.getEntities(mcEntity, box); */
@SuppressWarnings("rawtypes") @Override
final Iterator iterator = list.iterator(); public void cleanup() {
while (iterator.hasNext()) { super.cleanup();
final net.minecraft.server.v1_7_R4.Entity other = (net.minecraft.server.v1_7_R4.Entity) iterator.next(); world = null;
if (!(other instanceof EntityBoat)){ // && !(other instanceof EntityMinecart)) continue; }
continue;
}
if (minY >= other.locY && minY - other.locY <= 0.7){
return true;
}
// Still check this for some reason.
final AxisAlignedBB otherBox = other.boundingBox;
if (box.a > otherBox.d || box.d < otherBox.a || box.b > otherBox.e || box.e < otherBox.b || box.c > otherBox.f || box.f < otherBox.c) {
continue;
}
else {
return true;
}
}
}
catch (Throwable t){
// Ignore exceptions (Context: DisguiseCraft).
}
return false;
}
/* (non-Javadoc) @Override
* @see fr.neatmonster.nocheatplus.utilities.BlockCache#cleanup() public TileEntity getTileEntity(final int x, final int y, final int z) {
*/ return world.getTileEntity(x, y, z);
@Override }
public void cleanup() {
super.cleanup();
world = null;
}
@Override @Override
public TileEntity getTileEntity(final int x, final int y, final int z) { public int getBlockPower(final int arg0, final int arg1, final int arg2, final int arg3) {
return world.getTileEntity(x, y, z); return world.getBlockPower(arg0, arg1, arg2, arg3);
} }
@Override @Override
public int getBlockPower(final int arg0, final int arg1, final int arg2, final int arg3) { public Block getType(int x, int y, int z) {
return world.getBlockPower(arg0, arg1, arg2, arg3); return world.getType(x, y, z);
} }
@Override
public Block getType(int x, int y, int z) {
return world.getType(x, y, z);
}
} }

View File

@ -14,49 +14,49 @@ import fr.neatmonster.nocheatplus.utilities.ds.CoordMap;
*/ */
public abstract class BlockCache { public abstract class BlockCache {
// TODO: New concepts (Might switch to material, inspect MC+CB code for reliability and performance of block-ids during runtime). // TODO: New concepts (Might switch to material, inspect MC+CB code for reliability and performance of block-ids during runtime).
private static final int ID_AIR = 0; private static final int ID_AIR = 0;
/** /**
* Convenience method to check if the bounds as returned by getBounds cover a whole block. * Convenience method to check if the bounds as returned by getBounds cover a whole block.
* @param bounds Can be null, must have 6 fields. * @param bounds Can be null, must have 6 fields.
* @return * @return
*/ */
public static final boolean isFullBounds(final double[] bounds) { public static final boolean isFullBounds(final double[] bounds) {
if (bounds == null) return false; if (bounds == null) return false;
for (int i = 0; i < 3; i ++) { for (int i = 0; i < 3; i ++) {
if (bounds[i] > 0.0) return false; if (bounds[i] > 0.0) return false;
if (bounds[i + 3] < 1.0) return false; if (bounds[i + 3] < 1.0) return false;
} }
return true; return true;
} }
/** /**
* Check if chunks are loaded and load all not yet loaded chunks, using normal world coordinates.<br> * Check if chunks are loaded and load all not yet loaded chunks, using normal world coordinates.<br>
* NOTE: Not sure where to put this. Method does not use any caching. * NOTE: Not sure where to put this. Method does not use any caching.
* @param world * @param world
* @param x * @param x
* @param z * @param z
* @param xzMargin * @param xzMargin
* @return Number of loaded chunks. * @return Number of loaded chunks.
*/ */
public static int ensureChunksLoaded(final World world, final double x, final double z, final double xzMargin) { public static int ensureChunksLoaded(final World world, final double x, final double z, final double xzMargin) {
int loaded = 0; int loaded = 0;
final int minX = Location.locToBlock(x - xzMargin) / 16; final int minX = Location.locToBlock(x - xzMargin) / 16;
final int maxX = Location.locToBlock(x + xzMargin) / 16; final int maxX = Location.locToBlock(x + xzMargin) / 16;
final int minZ = Location.locToBlock(z - xzMargin) / 16; final int minZ = Location.locToBlock(z - xzMargin) / 16;
final int maxZ = Location.locToBlock(z + xzMargin) / 16; final int maxZ = Location.locToBlock(z + xzMargin) / 16;
for (int cx = minX; cx <= maxX; cx ++) { for (int cx = minX; cx <= maxX; cx ++) {
for (int cz = minZ; cz <= maxZ; cz ++) { for (int cz = minZ; cz <= maxZ; cz ++) {
if (!world.isChunkLoaded(cx, cz)) { if (!world.isChunkLoaded(cx, cz)) {
world.loadChunk(cx, cz); world.loadChunk(cx, cz);
loaded ++; loaded ++;
} }
} }
} }
return loaded; return loaded;
} }
/** Cached type-ids. */ /** Cached type-ids. */
private final CoordMap<Integer> idMap = new CoordMap<Integer>(23); private final CoordMap<Integer> idMap = new CoordMap<Integer>(23);
@ -69,11 +69,7 @@ public abstract class BlockCache {
protected int maxBlockY = 255; protected int maxBlockY = 255;
// TODO: switch to nodes with all details on, store a working node ? // TODO: Switch to nodes with all details on?
// TODO: maybe make very fast access arrays for the ray tracing checks.
// private int[] id = null;
// private int[] data = null;
public BlockCache() { public BlockCache() {
} }
@ -104,22 +100,29 @@ public abstract class BlockCache {
* @param z * @param z
* @return * @return
*/ */
public abstract int fetchData(int x, int y, int z); public abstract int fetchData(int x, int y, int z);
public abstract double[] fetchBounds(int x, int y, int z); /**
* Find out bounds for the block, this should not return null for performance reasons.
* @param x
* @param y
* @param z
* @return
*/
public abstract double[] fetchBounds(int x, int y, int z);
/** /**
* This is a on-ground type check just for standing on minecarts / boats. * This is a on-ground type check just for standing on minecarts / boats.
* @param entity * @param entity
* @param minX * @param minX
* @param minY * @param minY
* @param minZ * @param minZ
* @param maxX * @param maxX
* @param maxY * @param maxY
* @param maxZ * @param maxZ
* @return * @return
*/ */
public abstract boolean standsOnEntity(Entity entity, final double minX, final double minY, final double minZ, final double maxX, final double maxY, final double maxZ); public abstract boolean standsOnEntity(Entity entity, final double minX, final double minY, final double minZ, final double maxX, final double maxY, final double maxZ);
/** /**
@ -139,18 +142,18 @@ public abstract class BlockCache {
* @param eZ * @param eZ
* @return * @return
*/ */
public int getTypeId(double x, double y, double z) { public int getTypeId(double x, double y, double z) {
return getTypeId(Location.locToBlock(x), Location.locToBlock(y), Location.locToBlock(z)); return getTypeId(Location.locToBlock(x), Location.locToBlock(y), Location.locToBlock(z));
} }
/** /**
* (convenience method, uses cache). * (convenience method, uses cache).
* @param block * @param block
* @return * @return
*/ */
public int getTypeId(final Block block) { public int getTypeId(final Block block) {
return getTypeId(block.getX(), block.getY(), block.getZ()); return getTypeId(block.getX(), block.getY(), block.getZ());
} }
/** /**
* Get type id with cache access. * Get type id with cache access.
@ -159,67 +162,68 @@ public abstract class BlockCache {
* @param z * @param z
* @return * @return
*/ */
public int getTypeId(final int x, final int y, final int z) { public int getTypeId(final int x, final int y, final int z) {
final Integer pId = idMap.get(x, y, z); final Integer pId = idMap.get(x, y, z);
if (pId != null) { if (pId != null) {
return pId; return pId;
} }
final Integer nId = (y < 0 || y > maxBlockY) ? ID_AIR : fetchTypeId(x, y, z); final Integer nId = (y < 0 || y > maxBlockY) ? ID_AIR : fetchTypeId(x, y, z);
idMap.put(x, y, z, nId); idMap.put(x, y, z, nId);
return nId; return nId;
} }
/** /**
* Get data value with cache access. * Get data value with cache access.
* @param x * @param x
* @param y * @param y
* @param z * @param z
* @return * @return
*/ */
public int getData(final int x, final int y, final int z) { public int getData(final int x, final int y, final int z) {
final Integer pData = dataMap.get(x, y, z); final Integer pData = dataMap.get(x, y, z);
if (pData != null) { if (pData != null) {
return pData; return pData;
} }
final Integer nData = (y < 0 || y > maxBlockY) ? 0 : fetchData(x, y, z); final Integer nData = (y < 0 || y > maxBlockY) ? 0 : fetchData(x, y, z);
dataMap.put(x, y, z, nData); dataMap.put(x, y, z, nData);
return nData; return nData;
} }
/** /**
* Get block bounds - <b>Do not change these in-place, because the returned array is cached internally.</b> * Get block bounds - <b>Do not change these in-place, because the returned array is cached internally.</b>
* @param x * @param x
* @param y * @param y
* @param z * @param z
* @return Array of floats (minX, minY, minZ, maxX, maxY, maxZ), may be null theoretically. Do not change these in place, because they might get cached. * @return Array of floats (minX, minY, minZ, maxX, maxY, maxZ), may be null theoretically. Do not change these in place, because they might get cached.
*/ */
public double[] getBounds(final int x, final int y, final int z) { public double[] getBounds(final int x, final int y, final int z) {
final double[] pBounds = boundsMap.get(x, y, z); final double[] pBounds = boundsMap.get(x, y, z);
if (pBounds != null) { if (pBounds != null) {
return pBounds; return pBounds;
} }
final double[] nBounds = (y < 0 || y > maxBlockY) ? null : fetchBounds(x, y, z); // TODO: Convention for null bounds -> full ?
boundsMap.put(x, y, z, nBounds); final double[] nBounds = (y < 0 || y > maxBlockY) ? null : fetchBounds(x, y, z);
return nBounds; boundsMap.put(x, y, z, nBounds);
} return nBounds;
}
/** /**
* Convenience method to check if the bounds for a block cover the full block. * Convenience method to check if the bounds for a block cover the full block.
* @param x * @param x
* @param y * @param y
* @param z * @param z
* @return * @return
*/ */
public boolean isFullBounds(final int x, final int y, final int z) { public boolean isFullBounds(final int x, final int y, final int z) {
return isFullBounds(getBounds(x, y, z)); return isFullBounds(getBounds(x, y, z));
} }
/** /**
* Get the maximal y coordinate a block can be at (non air). * Get the maximal y coordinate a block can be at (non air).
* @return * @return
*/ */
public int getMaxBlockY() { public int getMaxBlockY() {
return maxBlockY; return maxBlockY;
} }
} }