use of org.jnbt.Tag in project WorldPainter by Captain-Chaos.
the class RespawnPlayer method respawnPlayer.
public static void respawnPlayer(File levelDatFile) throws IOException {
CompoundTag outerTag;
try (NBTInputStream in = new NBTInputStream(new GZIPInputStream(new FileInputStream(levelDatFile)))) {
outerTag = (CompoundTag) in.readTag();
}
CompoundTag dataTag = (CompoundTag) outerTag.getTag(TAG_DATA);
int spawnX = ((IntTag) dataTag.getTag(TAG_SPAWN_X)).getValue();
int spawnY = ((IntTag) dataTag.getTag(TAG_SPAWN_Y)).getValue();
int spawnZ = ((IntTag) dataTag.getTag(TAG_SPAWN_Z)).getValue();
CompoundTag playerTag = (CompoundTag) dataTag.getTag(TAG_PLAYER);
playerTag.setTag(TAG_DEATH_TIME, new ShortTag(TAG_DEATH_TIME, (short) 0));
playerTag.setTag(TAG_HEALTH, new ShortTag(TAG_HEALTH, (short) 20));
List<Tag> motionList = new ArrayList<>(3);
motionList.add(new DoubleTag(null, 0));
motionList.add(new DoubleTag(null, 0));
motionList.add(new DoubleTag(null, 0));
playerTag.setTag(TAG_MOTION, new ListTag(TAG_MOTION, DoubleTag.class, motionList));
List<Tag> posList = new ArrayList<>(3);
posList.add(new DoubleTag(null, spawnX + 0.5));
posList.add(new DoubleTag(null, spawnY + 3));
posList.add(new DoubleTag(null, spawnZ + 0.5));
playerTag.setTag(TAG_POS, new ListTag(TAG_POS, DoubleTag.class, posList));
try (NBTOutputStream out = new NBTOutputStream(new GZIPOutputStream(new FileOutputStream(levelDatFile)))) {
out.writeTag(outerTag);
}
}
use of org.jnbt.Tag in project WorldPainter by Captain-Chaos.
the class JavaWorldMerger method copyAllChunks.
private String copyAllChunks(MinecraftWorld minecraftWorld, File oldRegionDir, Dimension dimension, Point regionCoords, ProgressReceiver progressReceiver) throws IOException, ProgressReceiver.OperationCancelled {
if (progressReceiver != null) {
progressReceiver.setMessage("Copying chunks unchanged");
}
int lowestChunkX = regionCoords.x << 5;
int highestChunkX = (regionCoords.x << 5) + 31;
int lowestChunkY = regionCoords.y << 5;
int highestChunkY = (regionCoords.y << 5) + 31;
Platform platform = dimension.getWorld().getPlatform();
int maxHeight = dimension.getMaxHeight();
Map<Point, RegionFile> regionFiles = new HashMap<>();
Set<Point> damagedRegions = new HashSet<>();
StringBuilder reportBuilder = new StringBuilder();
try {
int chunkNo = 0;
for (int chunkX = lowestChunkX; chunkX <= highestChunkX; chunkX++) {
for (int chunkY = lowestChunkY; chunkY <= highestChunkY; chunkY++) {
chunkNo++;
if (progressReceiver != null) {
progressReceiver.setProgress((float) chunkNo / 1024);
}
int regionX = chunkX >> 5;
int regionY = chunkY >> 5;
Point coords = new Point(regionX, regionY);
if (damagedRegions.contains(coords)) {
// reported and logged earlier
continue;
}
RegionFile regionFile = regionFiles.get(coords);
if (regionFile == null) {
File file = new File(oldRegionDir, "r." + regionX + "." + regionY + (platform.equals(DefaultPlugin.JAVA_ANVIL) ? ".mca" : ".mcr"));
try {
regionFile = new RegionFile(file);
regionFiles.put(coords, regionFile);
} catch (IOException e) {
reportBuilder.append("I/O error while opening region file " + file + " (message: \"" + e.getMessage() + "\"); skipping region" + EOL);
logger.error("I/O error while opening region file " + file + "; skipping region", e);
damagedRegions.add(coords);
continue;
}
}
int chunkXInRegion = chunkX & 0x1f;
int chunkYInRegion = chunkY & 0x1f;
if (regionFile.containsChunk(chunkXInRegion, chunkYInRegion)) {
Tag tag;
try {
InputStream chunkData = regionFile.getChunkDataInputStream(chunkXInRegion, chunkYInRegion);
if (chunkData == null) {
// This should never happen, since we checked
// with isChunkPresent(), but in practice it
// does. Perhaps corrupted data?
reportBuilder.append("Missing chunk data for chunk " + chunkXInRegion + ", " + chunkYInRegion + " in " + regionFile + "; skipping chunk" + EOL);
logger.warn("Missing chunk data for chunk " + chunkXInRegion + ", " + chunkYInRegion + " in " + regionFile + "; skipping chunk");
continue;
}
try (NBTInputStream in = new NBTInputStream(chunkData)) {
tag = in.readTag();
}
} catch (IOException e) {
reportBuilder.append("I/O error while reading chunk " + chunkXInRegion + ", " + chunkYInRegion + " from file " + regionFile + " (message: \"" + e.getMessage() + "\"); skipping chunk" + EOL);
logger.error("I/O error while reading chunk " + chunkXInRegion + ", " + chunkYInRegion + " from file " + regionFile + "; skipping chunk", e);
continue;
} catch (IllegalArgumentException e) {
reportBuilder.append("Illegal argument exception while reading chunk " + chunkXInRegion + ", " + chunkYInRegion + " from file " + regionFile + " (message: \"" + e.getMessage() + "\"); skipping chunk" + EOL);
logger.error("Illegal argument exception while reading chunk " + chunkXInRegion + ", " + chunkYInRegion + " from file " + regionFile + "; skipping chunk", e);
continue;
}
Chunk existingChunk = platform.equals(DefaultPlugin.JAVA_MCREGION) ? new ChunkImpl((CompoundTag) tag, maxHeight) : new ChunkImpl2((CompoundTag) tag, maxHeight);
minecraftWorld.addChunk(existingChunk);
}
}
}
} finally {
for (RegionFile regionFile : regionFiles.values()) {
regionFile.close();
}
}
if (progressReceiver != null) {
progressReceiver.setProgress(1.0f);
}
return reportBuilder.length() != 0 ? reportBuilder.toString() : null;
}
use of org.jnbt.Tag in project WorldPainter by Captain-Chaos.
the class NBTFileNode method loadChildren.
@Override
protected Node[] loadChildren() {
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
in.mark(2);
Tag tag;
if ((in.read() == 0x1f) && (in.read() == 0x8b)) {
// Gzip signature
in.reset();
tag = new NBTInputStream(new GZIPInputStream(in)).readTag();
} else {
tag = new NBTInputStream(in).readTag();
}
return new Node[] { new TagNode(tag) };
} catch (IOException e) {
throw new RuntimeException("I/O error while reading level.dat file", e);
}
}
use of org.jnbt.Tag in project WorldPainter by Captain-Chaos.
the class AbstractNBTItem method setDoubleList.
protected final void setDoubleList(String name, double[] values) {
List<Tag> list = new ArrayList<>(values.length);
for (double value : values) {
list.add(new DoubleTag(null, value));
}
tag.setTag(name, new ListTag(name, DoubleTag.class, list));
}
use of org.jnbt.Tag in project WorldPainter by Captain-Chaos.
the class AbstractNBTItem method setFloatList.
protected final void setFloatList(String name, float[] values) {
List<Tag> list = new ArrayList<>(values.length);
for (float value : values) {
list.add(new FloatTag(null, value));
}
tag.setTag(name, new ListTag(name, FloatTag.class, list));
}
Aggregations