Fixes #494 (entity rotation) (related #210)

This commit is contained in:
Jesse Boyd 2017-04-20 01:04:05 +10:00
parent 156473356d
commit 89a0a44d14
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
3 changed files with 76 additions and 20 deletions

View File

@ -107,6 +107,16 @@ public class MCAQueue extends NMSMappedFaweQueue<FaweQueue, FaweChunk, FaweChunk
} }
MCAFile mcaFile = new MCAFile(null, file); MCAFile mcaFile = new MCAFile(null, file);
mcaFile.init(); mcaFile.init();
final long heapSize = Runtime.getRuntime().totalMemory();
final long heapMaxSize = Runtime.getRuntime().maxMemory();
int free = (int) (((heapMaxSize - heapSize) + Runtime.getRuntime().freeMemory()) / (1024 * 1024));
// int obcx = bcx - oCX;
// int obcz = bcz - oCX;
// int otcx = tcx - oCX;
// int otcz = tcz - oCX;
for (int cz = bcz; cz <= tcz; cz++) { for (int cz = bcz; cz <= tcz; cz++) {
for (int cx = bcx; cx <= tcx; cx++) { for (int cx = bcx; cx <= tcx; cx++) {
int bx = cx << 4; int bx = cx << 4;
@ -149,12 +159,15 @@ public class MCAQueue extends NMSMappedFaweQueue<FaweQueue, FaweChunk, FaweChunk
int otherTCX = (otx) >> 4; int otherTCX = (otx) >> 4;
int otherTCZ = (otz) >> 4; int otherTCZ = (otz) >> 4;
MCAChunk newChunk = mcaFile.getChunk(cx, cz); MCAChunk newChunk = mcaFile.getChunk(cx, cz);
boolean created;
if (newChunk == null) { if (newChunk == null) {
newChunk = new MCAChunk(this, cx, cz); newChunk = new MCAChunk(this, cx, cz);
mcaFile.setChunk(newChunk); created = true;
} else { } else {
created = false;
newChunk.setModified(); newChunk.setModified();
} }
boolean modified = false;
int cbx = (cx << 4) - oX; int cbx = (cx << 4) - oX;
int cbz = (cz << 4) - oZ; int cbz = (cz << 4) - oZ;
for (int otherCZ = otherBCZ; otherCZ <= otherTCZ; otherCZ++) { for (int otherCZ = otherBCZ; otherCZ <= otherTCZ; otherCZ++) {
@ -176,11 +189,17 @@ public class MCAQueue extends NMSMappedFaweQueue<FaweQueue, FaweChunk, FaweChunk
int offsetX = ocbx - cbx; int offsetX = ocbx - cbx;
int offsetZ = ocbz - cbz; int offsetZ = ocbz - cbz;
newChunk.copyFrom(other, minX, maxX, minY, maxY, minZ, maxZ, offsetX, offsetY, offsetZ); newChunk.copyFrom(other, minX, maxX, minY, maxY, minZ, maxZ, offsetX, offsetY, offsetZ);
newChunk.setModified();
modified = true;
} }
} }
} }
if (created && modified) {
mcaFile.setChunk(newChunk);
}
} }
} }
pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
mcaFile.close(pool); mcaFile.close(pool);
from.clear(); from.clear();
} }
@ -350,7 +369,6 @@ public class MCAQueue extends NMSMappedFaweQueue<FaweQueue, FaweChunk, FaweChunk
}); });
pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS); pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
original.close(pool); original.close(pool);
pool.shutdown();
if (original != finalFile) finalFile.close(pool); if (original != finalFile) finalFile.close(pool);
} else if (mcaFile.isDeleted()) { } else if (mcaFile.isDeleted()) {
try { try {

View File

@ -38,25 +38,29 @@ public class MCAQueueMap implements IFaweQueueMap {
private int lastX = Integer.MIN_VALUE; private int lastX = Integer.MIN_VALUE;
private int lastZ = Integer.MIN_VALUE; private int lastZ = Integer.MIN_VALUE;
public MCAFile getMCAFile(int cx, int cz) { public synchronized MCAFile getMCAFile(int cx, int cz) {
int mcaX = cx >> 5; int mcaX = cx >> 5;
int mcaZ = cz >> 5; int mcaZ = cz >> 5;
if (mcaX == lastFileX && mcaZ == lastFileZ) { if (mcaX == lastFileX && mcaZ == lastFileZ) {
return lastFile; return lastFile;
} }
long pair = MathMan.pairInt(lastFileX = mcaX, lastFileZ = mcaZ); long pair = MathMan.pairInt(lastFileX = mcaX, lastFileZ = mcaZ);
lastFile = mcaFileMap.get(pair); MCAFile tmp;
lastFile = tmp = mcaFileMap.get(pair);
if (lastFile == null) { if (lastFile == null) {
try { try {
lastFile = new MCAFile(queue, lastFileX, lastFileZ); lastFile = tmp = new MCAFile(queue, lastFileX, lastFileZ);
} catch (FaweException.FaweChunkLoadException ignore) { } catch (FaweException.FaweChunkLoadException ignore) {
lastFile = null;
return null;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return lastFile = null; lastFile = null;
return null;
} }
mcaFileMap.put(pair, lastFile); mcaFileMap.put(pair, tmp);
} }
return lastFile; return tmp;
} }
@Override @Override

View File

@ -19,8 +19,13 @@
package com.sk89q.worldedit.function.entity; package com.sk89q.worldedit.function.entity;
import com.boydti.fawe.util.ReflectionUtils;
import com.sk89q.jnbt.ByteTag;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.CompoundTagBuilder; import com.sk89q.jnbt.FloatTag;
import com.sk89q.jnbt.IntTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.entity.BaseEntity; import com.sk89q.worldedit.entity.BaseEntity;
@ -32,6 +37,8 @@ import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.util.Direction; import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.Direction.Flag; import com.sk89q.worldedit.util.Direction.Flag;
import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.Location;
import java.util.Arrays;
import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -126,36 +133,63 @@ public class ExtentEntityCopy implements EntityFunction {
*/ */
private BaseEntity transformNbtData(BaseEntity state) { private BaseEntity transformNbtData(BaseEntity state) {
CompoundTag tag = state.getNbtData(); CompoundTag tag = state.getNbtData();
if (tag != null) { if (tag != null) {
// Handle hanging entities (paintings, item frames, etc.) // Handle hanging entities (paintings, item frames, etc.)
tag = tag.createBuilder().build();
Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
boolean hasTilePosition = tag.containsKey("TileX") && tag.containsKey("TileY") && tag.containsKey("TileZ"); boolean hasTilePosition = tag.containsKey("TileX") && tag.containsKey("TileY") && tag.containsKey("TileZ");
boolean hasDirection = tag.containsKey("Direction"); boolean hasDirection = tag.containsKey("Direction");
boolean hasLegacyDirection = tag.containsKey("Dir"); boolean hasLegacyDirection = tag.containsKey("Dir");
boolean hasFacing = tag.containsKey("Facing");
if (hasTilePosition) { if (hasTilePosition) {
Vector tilePosition = new Vector(tag.asInt("TileX"), tag.asInt("TileY"), tag.asInt("TileZ")); Vector tilePosition = new Vector(tag.asInt("TileX"), tag.asInt("TileY"), tag.asInt("TileZ"));
Vector newTilePosition = transform.apply(tilePosition.subtract(from)).add(to); Vector newTilePosition = transform.apply(tilePosition.subtract(from)).add(to);
CompoundTagBuilder builder = tag.createBuilder() values.put("TileX", new IntTag(newTilePosition.getBlockX()));
.putInt("TileX", newTilePosition.getBlockX()) values.put("TileY", new IntTag(newTilePosition.getBlockY()));
.putInt("TileY", newTilePosition.getBlockY()) values.put("TileZ", new IntTag(newTilePosition.getBlockZ()));
.putInt("TileZ", newTilePosition.getBlockZ());
if (hasDirection || hasLegacyDirection || hasFacing) {
int d;
if (hasDirection) {
d = tag.asInt("Direction");
} else if (hasLegacyDirection) {
d = MCDirections.fromLegacyHanging((byte) tag.asInt("Dir"));
} else {
d = tag.asInt("Facing");
}
if (hasDirection || hasLegacyDirection) {
int d = hasDirection ? tag.asInt("Direction") : MCDirections.fromLegacyHanging((byte) tag.asInt("Dir"));
Direction direction = MCDirections.fromHanging(d); Direction direction = MCDirections.fromHanging(d);
if (direction != null) { if (direction != null) {
Vector vector = new Vector(transform.apply(direction.toVector())).subtract(transform.apply(Vector.ZERO)).normalize(); Vector vector = transform.apply(direction.toVector()).subtract(transform.apply(Vector.ZERO)).normalize();
Direction newDirection = Direction.findClosest(vector, Flag.CARDINAL); Direction newDirection = Direction.findClosest(vector, Flag.CARDINAL);
builder.putByte("Direction", (byte) MCDirections.toHanging(newDirection)); if (newDirection != null) {
builder.putByte("Dir", MCDirections.toLegacyHanging(MCDirections.toHanging(newDirection))); byte hangingByte = (byte) MCDirections.toHanging(newDirection);
values.put("Direction", new ByteTag(hangingByte));
values.put("Facing", new ByteTag(hangingByte));
values.put("Dir", new ByteTag(MCDirections.toLegacyHanging(MCDirections.toHanging(newDirection))));
}
} }
} }
}
return new BaseEntity(state.getTypeId(), builder.build()); ListTag rotation = tag.getListTag("Rotation");
if (rotation != null) {
double yaw = Math.toRadians(rotation.getFloat(0));
double pitch = Math.toRadians(rotation.getFloat(1));
double xz = Math.cos(pitch);
Vector direction = new Vector(-xz * Math.sin(yaw), -Math.sin(pitch), xz * Math.cos(yaw));
direction = transform.apply(direction);
FloatTag yawTag = new FloatTag(direction.toYaw());
FloatTag pitchTag = new FloatTag(direction.toPitch());
values.put("Rotation", new ListTag(FloatTag.class, Arrays.asList(yawTag, pitchTag)));
} }
} }