use of net.glowstone.block.entity.BlockEntity in project Glowstone by GlowstoneMC.
the class GlowFallingBlock method placeFallingBlock.
private void placeFallingBlock() {
location.getBlock().setTypeIdAndData(material.getId(), getBlockData(), true);
if (getBlockEntityCompoundTag() != null) {
if (location.getBlock() instanceof GlowBlock) {
GlowBlock block = (GlowBlock) location.getBlock();
BlockEntity blockEntity = block.getBlockEntity();
if (blockEntity != null) {
blockEntity.loadNbt(getBlockEntityCompoundTag());
}
}
}
}
use of net.glowstone.block.entity.BlockEntity in project Glowstone by GlowstoneMC.
the class GlowChunk method createEntity.
/**
* If needed, create a new block entity at the given location.
*/
public BlockEntity createEntity(int cx, int cy, int cz, int type) {
Material material = Material.getMaterial(type);
switch(material) {
case SIGN:
case SIGN_POST:
case WALL_SIGN:
case CHEST:
case TRAPPED_CHEST:
case BURNING_FURNACE:
case FURNACE:
case DISPENSER:
case DROPPER:
case END_GATEWAY:
case HOPPER:
case MOB_SPAWNER:
case NOTE_BLOCK:
case JUKEBOX:
case BREWING_STAND:
case SKULL:
case COMMAND:
case COMMAND_CHAIN:
case COMMAND_REPEATING:
case BEACON:
case BANNER:
case WALL_BANNER:
case STANDING_BANNER:
case FLOWER_POT:
case STRUCTURE_BLOCK:
case WHITE_SHULKER_BOX:
case ORANGE_SHULKER_BOX:
case MAGENTA_SHULKER_BOX:
case LIGHT_BLUE_SHULKER_BOX:
case YELLOW_SHULKER_BOX:
case LIME_SHULKER_BOX:
case PINK_SHULKER_BOX:
case GRAY_SHULKER_BOX:
case SILVER_SHULKER_BOX:
case CYAN_SHULKER_BOX:
case PURPLE_SHULKER_BOX:
case BLUE_SHULKER_BOX:
case BROWN_SHULKER_BOX:
case GREEN_SHULKER_BOX:
case RED_SHULKER_BOX:
case BLACK_SHULKER_BOX:
case ENCHANTMENT_TABLE:
case ENDER_CHEST:
case DAYLIGHT_DETECTOR:
case DAYLIGHT_DETECTOR_INVERTED:
case REDSTONE_COMPARATOR_OFF:
case REDSTONE_COMPARATOR_ON:
BlockType blockType = ItemTable.instance().getBlock(type);
if (blockType == null)
return null;
try {
BlockEntity entity = blockType.createBlockEntity(this, cx, cy, cz);
if (entity == null)
return null;
blockEntities.put(coordinateToIndex(cx, cz, cy), entity);
return entity;
} catch (Exception ex) {
GlowServer.logger.log(Level.SEVERE, "Unable to initialize block entity for " + type, ex);
}
}
return null;
}
use of net.glowstone.block.entity.BlockEntity in project Glowstone by GlowstoneMC.
the class AnvilChunkIoService method write.
/**
* Writes a chunk to its region file.
*
* @param chunk The {@link GlowChunk} to write from.
* @throws IOException if an I/O error occurs.
*/
@Override
public void write(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;
CompoundTag levelTags = new CompoundTag();
// core properties
levelTags.putInt("xPos", chunk.getX());
levelTags.putInt("zPos", chunk.getZ());
levelTags.putBool("TerrainPopulated", chunk.isPopulated());
levelTags.putLong("LastUpdate", 0);
// chunk sections
List<CompoundTag> sectionTags = new ArrayList<>();
GlowChunkSnapshot snapshot = chunk.getChunkSnapshot(true, true, false);
ChunkSection[] sections = snapshot.getRawSections();
for (byte i = 0; i < sections.length; ++i) {
ChunkSection sec = sections[i];
if (sec == null)
continue;
CompoundTag sectionTag = new CompoundTag();
sectionTag.putByte("Y", i);
sec.optimize();
sec.writeToNBT(sectionTag);
sectionTags.add(sectionTag);
}
levelTags.putCompoundList("Sections", sectionTags);
// height map and biomes
levelTags.putIntArray("HeightMap", snapshot.getRawHeightmap());
levelTags.putByteArray("Biomes", snapshot.getRawBiomes());
// entities
List<CompoundTag> entities = new ArrayList<>();
for (GlowEntity entity : chunk.getRawEntities()) {
if (!entity.shouldSave()) {
continue;
}
try {
CompoundTag tag = new CompoundTag();
EntityStorage.save(entity, tag);
entities.add(tag);
} catch (Exception e) {
GlowServer.logger.log(Level.WARNING, "Error saving " + entity + " in " + chunk, e);
}
}
levelTags.putCompoundList("Entities", entities);
// block entities
List<CompoundTag> blockEntities = new ArrayList<>();
for (BlockEntity entity : chunk.getRawBlockEntities()) {
try {
CompoundTag tag = new CompoundTag();
entity.saveNbt(tag);
blockEntities.add(tag);
} catch (Exception ex) {
GlowServer.logger.log(Level.SEVERE, "Error saving block entity at " + entity.getBlock(), ex);
}
}
levelTags.putCompoundList("TileEntities", blockEntities);
List<CompoundTag> tileTicks = new ArrayList<>();
for (Location location : chunk.getWorld().getTickMap()) {
if (location.getChunk().getX() == chunk.getX() && location.getChunk().getZ() == chunk.getZ()) {
int tileX = location.getBlockX();
int tileY = location.getBlockY();
int tileZ = location.getBlockZ();
String type = location.getBlock().getType().name().toLowerCase();
if (type.startsWith("stationary_")) {
type = type.replace("stationary_", "");
} else if (type.equals("water") || type.equals("lava")) {
type = "flowing_" + type;
}
CompoundTag tag = new CompoundTag();
tag.putInt("x", tileX);
tag.putInt("y", tileY);
tag.putInt("z", tileZ);
tag.putString("i", "minecraft:" + type);
tileTicks.add(tag);
}
}
levelTags.putCompoundList("TileTicks", tileTicks);
CompoundTag levelOut = new CompoundTag();
levelOut.putCompound("Level", levelTags);
try (NBTOutputStream nbt = new NBTOutputStream(region.getChunkDataOutputStream(regionX, regionZ), false)) {
nbt.writeTag(levelOut);
}
}
Aggregations