use of net.glowstone.util.nbt.NBTInputStream in project Glowstone by GlowstoneMC.
the class AnvilChunkIoService method read.
/**
* Reads a chunk from its region file.
*
* @param chunk The GlowChunk to read into.
* @return Whether the
* @throws IOException if an I/O error occurs.
*/
@Override
public boolean read(GlowChunk chunk) throws IOException {
int x = chunk.getX(), z = chunk.getZ();
RegionFile region = cache.getRegionFile(x, z);
int regionX = x & REGION_SIZE - 1;
int regionZ = z & REGION_SIZE - 1;
if (!region.hasChunk(regionX, regionZ)) {
return false;
}
DataInputStream in = region.getChunkDataInputStream(regionX, regionZ);
CompoundTag levelTag;
try (NBTInputStream nbt = new NBTInputStream(in, false)) {
CompoundTag root = nbt.readCompound();
levelTag = root.getCompound("Level");
}
// read the vertical sections
List<CompoundTag> sectionList = levelTag.getCompoundList("Sections");
ChunkSection[] sections = new ChunkSection[GlowChunk.SEC_COUNT];
for (CompoundTag sectionTag : sectionList) {
int y = sectionTag.getByte("Y");
if (sections[y] != null) {
GlowServer.logger.log(Level.WARNING, "Multiple chunk sections at y " + y + " in " + chunk + "!");
continue;
}
if (y < 0 || y > GlowChunk.SEC_COUNT) {
GlowServer.logger.log(Level.WARNING, "Out of bounds chunk section at y " + y + " in " + chunk + "!");
continue;
}
sections[y] = ChunkSection.fromNBT(sectionTag);
}
// initialize the chunk
chunk.initializeSections(sections);
chunk.setPopulated(levelTag.getBool("TerrainPopulated"));
// read biomes
if (levelTag.isByteArray("Biomes")) {
chunk.setBiomes(levelTag.getByteArray("Biomes"));
}
// read height map
if (levelTag.isIntArray("HeightMap")) {
chunk.setHeightMap(levelTag.getIntArray("HeightMap"));
} else {
chunk.automaticHeightMap();
}
// read entities
if (levelTag.isList("Entities", TagType.COMPOUND)) {
for (CompoundTag entityTag : levelTag.getCompoundList("Entities")) {
try {
// note that creating the entity is sufficient to add it to the world
EntityStorage.loadEntity(chunk.getWorld(), entityTag);
} catch (Exception e) {
String id = entityTag.isString("id") ? entityTag.getString("id") : "<missing>";
if (e.getMessage() != null && e.getMessage().startsWith("Unknown entity type to load:")) {
GlowServer.logger.warning("Unknown entity in " + chunk + ": " + id);
} else {
GlowServer.logger.log(Level.WARNING, "Error loading entity in " + chunk + ": " + id, e);
}
}
}
}
// read block entities
List<CompoundTag> storedBlockEntities = levelTag.getCompoundList("TileEntities");
BlockEntity blockEntity;
for (CompoundTag blockEntityTag : storedBlockEntities) {
int tx = blockEntityTag.getInt("x");
int ty = blockEntityTag.getInt("y");
int tz = blockEntityTag.getInt("z");
blockEntity = chunk.createEntity(tx & 0xf, ty, tz & 0xf, chunk.getType(tx & 0xf, tz & 0xf, ty));
if (blockEntity != null) {
try {
blockEntity.loadNbt(blockEntityTag);
} catch (Exception ex) {
String id = blockEntityTag.isString("id") ? blockEntityTag.getString("id") : "<missing>";
GlowServer.logger.log(Level.SEVERE, "Error loading block entity at " + blockEntity.getBlock() + ": " + id, ex);
}
} else {
String id = blockEntityTag.isString("id") ? blockEntityTag.getString("id") : "<missing>";
GlowServer.logger.warning("Unknown block entity at " + chunk.getWorld().getName() + "," + tx + "," + ty + "," + tz + ": " + id);
}
}
if (levelTag.isList("TileTicks", TagType.COMPOUND)) {
List<CompoundTag> tileTicks = levelTag.getCompoundList("TileTicks");
for (CompoundTag tileTick : tileTicks) {
int tileX = tileTick.getInt("x");
int tileY = tileTick.getInt("y");
int tileZ = tileTick.getInt("z");
String id = tileTick.getString("i");
if (id.startsWith("minecraft:")) {
id = id.replace("minecraft:", "");
if (id.startsWith("flowing_")) {
id = id.replace("flowing_", "");
} else if (id.equals("water") || id.equals("lava")) {
id = "stationary_" + id;
}
}
Material material = Material.getMaterial(id.toUpperCase());
GlowBlock block = chunk.getBlock(tileX, tileY, tileZ);
if (material != block.getType()) {
continue;
}
// TODO tick delay: tileTick.getInt("t");
// TODO ordering: tileTick.getInt("p");
BlockType type = ItemTable.instance().getBlock(material);
if (type == null) {
continue;
}
block.getWorld().requestPulse(block);
}
}
return true;
}
use of net.glowstone.util.nbt.NBTInputStream in project Glowstone by GlowstoneMC.
the class NbtPlayerDataService method readData.
@Override
public void readData(GlowPlayer player) {
File playerFile = getPlayerFile(player.getUniqueId());
CompoundTag playerTag = new CompoundTag();
if (playerFile.exists()) {
try (NBTInputStream in = new NBTInputStream(new FileInputStream(playerFile))) {
playerTag = in.readCompound();
} catch (IOException e) {
player.kickPlayer("Failed to read player data!");
server.getLogger().log(Level.SEVERE, "Failed to read data for " + player.getName() + ": " + playerFile, e);
}
}
readDataImpl(player, playerTag);
}
use of net.glowstone.util.nbt.NBTInputStream in project Glowstone by GlowstoneMC.
the class NbtStructureDataService method readStructuresData.
@Override
public Map<Integer, GlowStructure> readStructuresData() {
Map<Integer, GlowStructure> structures = new HashMap<>();
for (StructureStore<?> store : StructureStorage.getStructureStores()) {
File structureFile = new File(structureDir, store.getId() + ".dat");
if (structureFile.exists()) {
try (NBTInputStream in = new NBTInputStream(new FileInputStream(structureFile))) {
CompoundTag data = new CompoundTag();
data = in.readCompound();
if (data.isCompound("data")) {
data = data.getCompound("data");
if (data.isCompound("Features")) {
CompoundTag features = data.getCompound("Features");
features.getValue().keySet().stream().filter(features::isCompound).forEach(key -> {
GlowStructure structure = StructureStorage.loadStructure(world, features.getCompound(key));
structures.put(new Key(structure.getChunkX(), structure.getChunkZ()).hashCode(), structure);
});
}
} else {
server.getLogger().log(Level.SEVERE, "No data tag in " + structureFile);
}
} catch (IOException e) {
server.getLogger().log(Level.SEVERE, "Failed to read structure data from " + structureFile, e);
}
}
}
return structures;
}
use of net.glowstone.util.nbt.NBTInputStream in project Glowstone by GlowstoneMC.
the class NbtWorldMetadataService method readWorldData.
@Override
public WorldFinalValues readWorldData() {
// determine UUID of world
UUID uid = null;
File uuidFile = new File(dir, "uid.dat");
if (uuidFile.exists()) {
try (DataInputStream in = new DataInputStream(new FileInputStream(uuidFile))) {
uid = new UUID(in.readLong(), in.readLong());
} catch (IOException e) {
handleWorldException("uid.dat", e);
}
}
if (uid == null) {
uid = UUID.randomUUID();
}
// read in world information
CompoundTag level = new CompoundTag();
File levelFile = new File(dir, "level.dat");
if (levelFile.exists()) {
try (NBTInputStream in = new NBTInputStream(new FileInputStream(levelFile))) {
level = in.readCompound();
if (level.isCompound("Data")) {
level = level.getCompound("Data");
} else {
server.getLogger().warning("Loading world \"" + world.getName() + "\": reading from root, not Data");
}
} catch (IOException e) {
handleWorldException("level.dat", e);
}
}
// seed
long seed = 0L;
if (level.isLong("RandomSeed")) {
seed = level.getLong("RandomSeed");
level.remove("RandomSeed");
}
// time of day and weather status
if (level.isByte("thundering")) {
world.setThundering(level.getBool("thundering"));
level.remove("thundering");
}
if (level.isByte("raining")) {
world.setStorm(level.getBool("raining"));
level.remove("raining");
}
if (level.isInt("thunderTime")) {
world.setThunderDuration(level.getInt("thunderTime"));
level.remove("thunderTime");
}
if (level.isInt("rainTime")) {
world.setWeatherDuration(level.getInt("rainTime"));
level.remove("rainTime");
}
if (level.isLong("Time")) {
world.setFullTime(level.getLong("Time"));
level.remove("Time");
}
if (level.isLong("DayTime")) {
world.setTime(level.getLong("DayTime"));
level.remove("DayTime");
}
if (level.isString("generatorName")) {
world.setWorldType(WorldType.getByName(level.getString("generatorName")));
level.remove("generatorName");
}
// spawn position
if (level.isInt("SpawnX") && level.isInt("SpawnY") && level.isInt("SpawnZ")) {
world.setSpawnLocation(level.getInt("SpawnX"), level.getInt("SpawnY"), level.getInt("SpawnZ"));
level.remove("SpawnX");
level.remove("SpawnY");
level.remove("SpawnZ");
}
// game rules
if (level.isCompound("GameRules")) {
CompoundTag gameRules = level.getCompound("GameRules");
gameRules.getValue().keySet().stream().filter(gameRules::isString).forEach(key -> world.setGameRuleValue(key, gameRules.getString(key)));
level.remove("GameRules");
}
// world border
Location borderCenter = new Location(world, 0, 0, 0);
if (level.isDouble("BorderCenterX")) {
borderCenter.setX(level.getDouble("BorderCenterX"));
level.remove("BorderCenterX");
}
if (level.isDouble("BorderCenterZ")) {
borderCenter.setZ(level.getDouble("BorderCenterZ"));
level.remove("BorderCenterZ");
}
world.getWorldBorder().setCenter(borderCenter);
if (level.isDouble("BorderSize")) {
world.getWorldBorder().setSize(level.getDouble("BorderSize"));
level.remove("BorderSize");
}
if (level.isDouble("BorderSizeLerpTarget") && level.isLong("BorderSizeLerpTime")) {
world.getWorldBorder().setSize(level.getDouble("BorderSizeLerpTarget"), level.getLong("BorderSizeLerpTime"));
level.remove("BorderSizeLerpTarget");
level.remove("BorderSizeLerpTime");
}
if (level.isDouble("BorderSafeZone")) {
world.getWorldBorder().setDamageBuffer(level.getDouble("BorderSafeZone"));
level.remove("BorderSafeZone");
}
if (level.isDouble("BorderWarningTime")) {
world.getWorldBorder().setWarningTime((int) level.getDouble("BorderWarningTime"));
level.remove("BorderWarningTime");
}
if (level.isDouble("BorderWarningBlocks")) {
world.getWorldBorder().setWarningDistance((int) level.getDouble("BorderWarningBlocks"));
level.remove("BorderWarningBlocks");
}
if (level.isDouble("BorderDamagePerBlock")) {
world.getWorldBorder().setDamageAmount(level.getDouble("BorderDamagePerBlock"));
level.remove("BorderDamagePerBlock");
}
// strip single-player Player tag if it exists
if (level.isCompound("Player")) {
server.getLogger().warning("World \"" + world.getName() + "\": removing single-player Player tag");
level.remove("Player");
}
// save unknown tags for later
unknownTags = level;
return new WorldFinalValues(seed, uid);
}
use of net.glowstone.util.nbt.NBTInputStream in project Dragonet-Legacy by DragonetMC.
the class PEInventorySlot method readSlot.
public static PEInventorySlot readSlot(PEBinaryReader reader) throws IOException {
//Unsigned
short id = (short) (reader.readShort() & 0xFFFF);
if (id <= 0) {
return new PEInventorySlot((short) 0, (byte) 0, (short) 0);
}
byte count = reader.readByte();
short meta = reader.readShort();
short lNbt = reader.readShort();
if (lNbt <= 0) {
return new PEInventorySlot(id, count, meta);
}
byte[] nbtData = reader.read(lNbt);
NBTInputStream nbtin = new NBTInputStream(new ByteArrayInputStream(nbtData));
CompoundTag nbt = nbtin.readCompound();
nbtin.close();
return new PEInventorySlot(id, count, meta, nbt);
}
Aggregations