use of com.sk89q.worldedit.world.block.BaseBlock in project FastAsyncWorldEdit by IntellectualSites.
the class WorldNativeAccess method setBlock.
default <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block, SideEffectSet sideEffects) throws WorldEditException {
checkNotNull(position);
checkNotNull(block);
setCurrentSideEffectSet(sideEffects);
int x = position.getBlockX();
int y = position.getBlockY();
int z = position.getBlockZ();
// First set the block
NC chunk = getChunk(x >> 4, z >> 4);
NP pos = getPosition(x, y, z);
NBS old = getBlockState(chunk, pos);
NBS newState = toNative(block.toImmutableState());
// change block prior to placing if it should be fixed
if (sideEffects.shouldApply(SideEffect.VALIDATION)) {
newState = getValidBlockForPosition(newState, pos);
}
NBS successState = setBlockState(chunk, pos, newState);
boolean successful = successState != null;
// Create the TileEntity
if (successful || old == newState) {
if (block instanceof BaseBlock) {
BaseBlock baseBlock = (BaseBlock) block;
// FAWE start - use CompoundBinaryTag over CompoundTag
CompoundBinaryTag tag = baseBlock.getNbt();
if (tag != null) {
tag = tag.put(ImmutableMap.of("id", StringBinaryTag.of(baseBlock.getNbtId()), "x", IntBinaryTag.of(position.getX()), "y", IntBinaryTag.of(position.getY()), "z", IntBinaryTag.of(position.getZ())));
// update if TE changed as well
successful = updateTileEntity(pos, tag);
}
// FAWE end
}
}
if (successful) {
if (sideEffects.getState(SideEffect.LIGHTING) == SideEffect.State.ON) {
updateLightingForBlock(pos);
}
markAndNotifyBlock(pos, chunk, old, newState, sideEffects);
}
return successful;
}
use of com.sk89q.worldedit.world.block.BaseBlock in project FastAsyncWorldEdit by IntellectualSites.
the class MinecraftStructure method read.
@Override
public Clipboard read(UUID clipboardId) throws IOException {
NamedTag rootTag = inputStream.readNamedTag();
// MC structures are all unnamed, but this doesn't seem to be necessary? might remove this later
if (!rootTag.getName().isEmpty()) {
throw new IOException("Root tag has name - are you sure this is a structure?");
}
Map<String, Tag> tags = ((CompoundTag) rootTag.getTag()).getValue();
ListTag size = (ListTag) tags.get("size");
int width = size.getInt(0);
int height = size.getInt(1);
int length = size.getInt(2);
// Init clipboard
BlockVector3 origin = BlockVector3.at(0, 0, 0);
CuboidRegion region = new CuboidRegion(origin, origin.add(width, height, length).subtract(BlockVector3.ONE));
Clipboard clipboard = new BlockArrayClipboard(region, clipboardId);
// Blocks
ListTag blocks = (ListTag) tags.get("blocks");
if (blocks != null) {
// Palette
List<CompoundTag> palette = (List<CompoundTag>) tags.get("palette").getValue();
BlockState[] combinedArray = new BlockState[palette.size()];
for (int i = 0; i < palette.size(); i++) {
CompoundTag compound = palette.get(i);
Map<String, Tag> map = compound.getValue();
String name = ((StringTag) map.get("Name")).getValue();
BlockType type = BlockTypes.get(name);
BlockState state = type.getDefaultState();
CompoundTag properties = (CompoundTag) map.get("Properties");
if (properties != null) {
for (Map.Entry<String, Tag> entry : properties.getValue().entrySet()) {
String key = entry.getKey();
String value = ((StringTag) entry.getValue()).getValue();
Property<Object> property = type.getProperty(key);
state = state.with(property, property.getValueFor(value));
}
}
combinedArray[i] = state;
}
// Populate blocks
List<CompoundTag> blocksList = (List<CompoundTag>) tags.get("blocks").getValue();
try {
for (CompoundTag compound : blocksList) {
Map<String, Tag> blockMap = compound.getValue();
IntTag stateTag = (IntTag) blockMap.get("state");
ListTag posTag = (ListTag) blockMap.get("pos");
BlockState state = combinedArray[stateTag.getValue()];
int x = posTag.getInt(0);
int y = posTag.getInt(1);
int z = posTag.getInt(2);
if (state.getBlockType().getMaterial().hasContainer()) {
CompoundTag nbt = (CompoundTag) blockMap.get("nbt");
if (nbt != null) {
BaseBlock block = state.toBaseBlock(nbt);
clipboard.setBlock(x, y, z, block);
continue;
}
}
clipboard.setBlock(x, y, z, state);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Entities
ListTag entities = (ListTag) tags.get("entities");
if (entities != null) {
List<CompoundTag> entityList = (List<CompoundTag>) (List<?>) entities.getValue();
for (CompoundTag entityEntry : entityList) {
Map<String, Tag> entityEntryMap = entityEntry.getValue();
ListTag posTag = (ListTag) entityEntryMap.get("pos");
CompoundTag nbtTag = (CompoundTag) entityEntryMap.get("nbt");
String id = nbtTag.getString("Id");
Location location = NBTConversions.toLocation(clipboard, posTag, nbtTag.getListTag("Rotation"));
if (!id.isEmpty()) {
BaseEntity state = new BaseEntity(EntityTypes.get(id), nbtTag);
clipboard.createEntity(location, state);
}
}
}
return clipboard;
}
use of com.sk89q.worldedit.world.block.BaseBlock in project FastAsyncWorldEdit by IntellectualSites.
the class MinecraftStructure method write.
public void write(Clipboard clipboard, String owner) throws IOException {
Region region = clipboard.getRegion();
int width = region.getWidth();
int height = region.getHeight();
int length = region.getLength();
if (width > WARN_SIZE || height > WARN_SIZE || length > WARN_SIZE) {
LOGGER.info("A structure longer than 32 is unsupported by minecraft (but probably still works)");
}
Map<String, Object> structure = FaweCache.INSTANCE.asMap("version", 1, "author", owner);
// ignored: version / owner
Int2ObjectArrayMap<Integer> indexes = new Int2ObjectArrayMap<>();
// Size
structure.put("size", Arrays.asList(width, height, length));
// Palette
ArrayList<HashMap<String, Object>> palette = new ArrayList<>();
for (BlockVector3 point : region) {
BlockState block = clipboard.getBlock(point);
int combined = block.getInternalId();
BlockType type = block.getBlockType();
if (type == BlockTypes.STRUCTURE_VOID || indexes.containsKey(combined)) {
continue;
}
indexes.put(combined, (Integer) palette.size());
HashMap<String, Object> paletteEntry = new HashMap<>();
paletteEntry.put("Name", type.getId());
if (block.getInternalId() != type.getInternalId()) {
Map<String, Object> properties = null;
for (AbstractProperty property : (List<AbstractProperty<?>>) type.getProperties()) {
int propIndex = property.getIndex(block.getInternalId());
if (propIndex != 0) {
if (properties == null) {
properties = new HashMap<>();
}
Object value = property.getValues().get(propIndex);
properties.put(property.getName(), value.toString());
}
}
if (properties != null) {
paletteEntry.put("Properties", properties);
}
}
palette.add(paletteEntry);
}
if (!palette.isEmpty()) {
structure.put("palette", palette);
}
// Blocks
ArrayList<Map<String, Object>> blocks = new ArrayList<>();
BlockVector3 min = region.getMinimumPoint();
for (BlockVector3 point : region) {
BaseBlock block = clipboard.getFullBlock(point);
if (block.getBlockType() != BlockTypes.STRUCTURE_VOID) {
int combined = block.getInternalId();
int index = indexes.get(combined);
List<Integer> pos = Arrays.asList(point.getX() - min.getX(), point.getY() - min.getY(), point.getZ() - min.getZ());
if (!block.hasNbtData()) {
blocks.add(FaweCache.INSTANCE.asMap("state", index, "pos", pos));
} else {
blocks.add(FaweCache.INSTANCE.asMap("state", index, "pos", pos, "nbt", block.getNbtData()));
}
}
}
if (!blocks.isEmpty()) {
structure.put("blocks", blocks);
}
// Entities
ArrayList<Map<String, Object>> entities = new ArrayList<>();
for (Entity entity : clipboard.getEntities()) {
Location loc = entity.getLocation();
List<Double> pos = Arrays.asList(loc.getX(), loc.getY(), loc.getZ());
List<Integer> blockPos = Arrays.asList(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
BaseEntity state = entity.getState();
if (state != null) {
CompoundTag nbt = state.getNbtData();
Map<String, Tag> nbtMap = nbt.getValue();
// Replace rotation data
nbtMap.put("Rotation", writeRotation(entity.getLocation()));
nbtMap.put("id", new StringTag(state.getType().getId()));
Map<String, Object> entityMap = FaweCache.INSTANCE.asMap("pos", pos, "blockPos", blockPos, "nbt", nbt);
entities.add(entityMap);
}
}
if (!entities.isEmpty()) {
structure.put("entities", entities);
}
out.writeNamedTag("", FaweCache.INSTANCE.asTag(structure));
close();
}
use of com.sk89q.worldedit.world.block.BaseBlock in project FastAsyncWorldEdit by IntellectualSites.
the class FastSchematicWriter method write2.
/**
* Writes a version 2 schematic file.
*
* @param clipboard The clipboard
*/
private void write2(Clipboard clipboard) throws IOException {
Region region = clipboard.getRegion();
BlockVector3 origin = clipboard.getOrigin();
BlockVector3 min = region.getMinimumPoint();
BlockVector3 offset = min.subtract(origin);
int width = region.getWidth();
int height = region.getHeight();
int length = region.getLength();
if (width > MAX_SIZE) {
throw new IllegalArgumentException("Width of region too large for a .schematic");
}
if (height > MAX_SIZE) {
throw new IllegalArgumentException("Height of region too large for a .schematic");
}
if (length > MAX_SIZE) {
throw new IllegalArgumentException("Length of region too large for a .schematic");
}
final DataOutput rawStream = outputStream.getOutputStream();
outputStream.writeLazyCompoundTag("Schematic", out -> {
out.writeNamedTag("DataVersion", WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataVersion());
out.writeNamedTag("Version", CURRENT_VERSION);
out.writeNamedTag("Width", (short) width);
out.writeNamedTag("Height", (short) height);
out.writeNamedTag("Length", (short) length);
// The Sponge format Offset refers to the 'min' points location in the world. That's our 'Origin'
out.writeNamedTag("Offset", new int[] { min.getBlockX(), min.getBlockY(), min.getBlockZ() });
out.writeLazyCompoundTag("Metadata", out1 -> {
out1.writeNamedTag("WEOffsetX", offset.getBlockX());
out1.writeNamedTag("WEOffsetY", offset.getBlockY());
out1.writeNamedTag("WEOffsetZ", offset.getBlockZ());
out1.writeNamedTag("FAWEVersion", Fawe.instance().getVersion().build);
});
ByteArrayOutputStream blocksCompressed = new ByteArrayOutputStream();
FaweOutputStream blocksOut = new FaweOutputStream(new DataOutputStream(new LZ4BlockOutputStream(blocksCompressed)));
ByteArrayOutputStream tilesCompressed = new ByteArrayOutputStream();
NBTOutputStream tilesOut = new NBTOutputStream(new LZ4BlockOutputStream(tilesCompressed));
List<Integer> paletteList = new ArrayList<>();
char[] palette = new char[BlockTypesCache.states.length];
Arrays.fill(palette, Character.MAX_VALUE);
int paletteMax = 0;
int numTiles = 0;
Clipboard finalClipboard;
if (clipboard instanceof BlockArrayClipboard) {
finalClipboard = ((BlockArrayClipboard) clipboard).getParent();
} else {
finalClipboard = clipboard;
}
Iterator<BlockVector3> iterator = finalClipboard.iterator(Order.YZX);
while (iterator.hasNext()) {
BlockVector3 pos = iterator.next();
BaseBlock block = pos.getFullBlock(finalClipboard);
CompoundTag nbt = block.getNbtData();
if (nbt != null) {
Map<String, Tag> values = new HashMap<>(nbt.getValue());
// Positions are kept in NBT, we don't want that.
values.remove("x");
values.remove("y");
values.remove("z");
values.put("Id", new StringTag(block.getNbtId()));
// Remove 'id' if it exists. We want 'Id'.
// Do this after we get "getNbtId" cos otherwise "getNbtId" doesn't work.
// Dum.
values.remove("id");
values.put("Pos", new IntArrayTag(new int[] { pos.getX(), pos.getY(), pos.getZ() }));
numTiles++;
tilesOut.writeTagPayload(new CompoundTag(values));
}
int ordinal = block.getOrdinal();
if (ordinal == 0) {
ordinal = 1;
}
char value = palette[ordinal];
if (value == Character.MAX_VALUE) {
int size = paletteMax++;
palette[ordinal] = value = (char) size;
paletteList.add(ordinal);
}
blocksOut.writeVarInt(value);
}
// close
tilesOut.close();
blocksOut.close();
out.writeNamedTag("PaletteMax", paletteMax);
out.writeLazyCompoundTag("Palette", out12 -> {
for (int i = 0; i < paletteList.size(); i++) {
int stateOrdinal = paletteList.get(i);
BlockState state = BlockTypesCache.states[stateOrdinal];
out12.writeNamedTag(state.getAsString(), i);
}
});
out.writeNamedTagName("BlockData", NBTConstants.TYPE_BYTE_ARRAY);
rawStream.writeInt(blocksOut.size());
try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(blocksCompressed.toByteArray()))) {
IOUtil.copy(in, rawStream);
}
if (numTiles != 0) {
out.writeNamedTagName("BlockEntities", NBTConstants.TYPE_LIST);
rawStream.write(NBTConstants.TYPE_COMPOUND);
rawStream.writeInt(numTiles);
try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(tilesCompressed.toByteArray()))) {
IOUtil.copy(in, rawStream);
}
} else {
out.writeNamedEmptyList("BlockEntities");
}
if (finalClipboard.hasBiomes()) {
writeBiomes(finalClipboard, out);
}
List<Tag> entities = new ArrayList<>();
for (Entity entity : finalClipboard.getEntities()) {
BaseEntity state = entity.getState();
if (state != null) {
Map<String, Tag> values = new HashMap<>();
// Put NBT provided data
CompoundTag rawTag = state.getNbtData();
if (rawTag != null) {
values.putAll(rawTag.getValue());
}
// Store our location data, overwriting any
values.remove("id");
Location loc = entity.getLocation();
if (!brokenEntities) {
loc = loc.setPosition(loc.add(min.toVector3()));
}
values.put("Id", new StringTag(state.getType().getId()));
values.put("Pos", writeVector(loc));
values.put("Rotation", writeRotation(entity.getLocation()));
CompoundTag entityTag = new CompoundTag(values);
entities.add(entityTag);
}
}
if (entities.isEmpty()) {
out.writeNamedEmptyList("Entities");
} else {
out.writeNamedTag("Entities", new ListTag(CompoundTag.class, entities));
}
});
}
use of com.sk89q.worldedit.world.block.BaseBlock in project FastAsyncWorldEdit by IntellectualSites.
the class EditSession method regenerate.
public boolean regenerate(Region region, BiomeType biome, Long seed) {
// TODO Optimize - avoid Vector2D creation (make mutable)
final AbstractChangeSet fcs = (AbstractChangeSet) this.getChangeSet();
this.setChangeSet(null);
final FaweRegionExtent fe = this.getRegionExtent();
final boolean cuboid = region instanceof CuboidRegion;
if (fe != null && cuboid) {
BlockVector3 max = region.getMaximumPoint();
BlockVector3 min = region.getMinimumPoint();
if (!fe.contains(max.getBlockX(), max.getBlockY(), max.getBlockZ()) && !fe.contains(min.getBlockX(), min.getBlockY(), min.getBlockZ())) {
throw FaweCache.OUTSIDE_REGION;
}
}
final Set<BlockVector2> chunks = region.getChunks();
MutableBlockVector3 mutable = new MutableBlockVector3();
MutableBlockVector3 mutable2 = new MutableBlockVector3();
MutableBlockVector2 mutable2D = new MutableBlockVector2();
for (BlockVector2 chunk : chunks) {
final int cx = chunk.getBlockX();
final int cz = chunk.getBlockZ();
final int bx = cx << 4;
final int bz = cz << 4;
final BlockVector3 cmin = BlockVector3.at(bx, 0, bz);
final BlockVector3 cmax = cmin.add(15, maxY, 15);
final boolean containsBot1 = fe == null || fe.contains(cmin.getBlockX(), cmin.getBlockY(), cmin.getBlockZ());
final boolean containsBot2 = region.contains(cmin);
final boolean containsTop1 = fe == null || fe.contains(cmax.getBlockX(), cmax.getBlockY(), cmax.getBlockZ());
final boolean containsTop2 = region.contains(cmax);
if (containsBot2 && containsTop2 && !containsBot1 && !containsTop1) {
continue;
}
boolean conNextX = chunks.contains(mutable2D.setComponents(cx + 1, cz));
boolean conNextZ = chunks.contains(mutable2D.setComponents(cx, cz + 1));
boolean containsAny = false;
if (cuboid && containsBot1 && containsBot2 && containsTop1 && containsTop2 && conNextX && conNextZ) {
containsAny = true;
if (fcs != null) {
for (int x = 0; x < 16; x++) {
int xx = x + bx;
for (int z = 0; z < 16; z++) {
int zz = z + bz;
for (int y = minY; y < maxY + 1; y++) {
BaseBlock block = getFullBlock(mutable.setComponents(xx, y, zz));
fcs.add(mutable, block, BlockTypes.AIR.getDefaultState().toBaseBlock());
}
}
}
}
} else {
if (!conNextX) {
setExistingBlocks(mutable.setComponents(bx + 16, 0, bz), mutable2.setComponents(bx + 31, maxY, bz + 15));
}
if (!conNextZ) {
setExistingBlocks(mutable.setComponents(bx, 0, bz + 16), mutable2.setComponents(bx + 15, maxY, bz + 31));
}
if (!chunks.contains(mutable2D.setComponents(cx + 1, cz + 1)) && !conNextX && !conNextZ) {
setExistingBlocks(mutable.setComponents(bx + 16, 0, bz + 16), mutable2.setComponents(bx + 31, maxY, bz + 31));
}
for (int x = 0; x < 16; x++) {
int xx = x + bx;
mutable.mutX(xx);
for (int z = 0; z < 16; z++) {
int zz = z + bz;
mutable.mutZ(zz);
for (int y = minY; y < maxY + 1; y++) {
mutable.mutY(y);
boolean contains = (fe == null || fe.contains(xx, y, zz)) && region.contains(mutable);
if (contains) {
containsAny = true;
if (fcs != null) {
BaseBlock block = getFullBlock(mutable);
fcs.add(mutable, block, BlockTypes.AIR.getDefaultState().toBaseBlock());
}
} else {
BaseBlock block = getFullBlock(mutable);
try {
setBlock(mutable, block);
} catch (MaxChangedBlocksException e) {
throw new RuntimeException(e);
}
}
}
}
}
}
if (containsAny) {
changes++;
TaskManager.taskManager().sync(new RunnableVal<Object>() {
@Override
public void run(Object value) {
regenerateChunk(cx, cz, biome, seed);
}
});
}
}
if (changes != 0) {
flushQueue();
return true;
}
return false;
}
Aggregations