use of com.sk89q.worldedit.WorldEditException in project FastAsyncWorldEdit by IntellectualSites.
the class BiomeCommands method biomeInfo.
@Command(name = "biomeinfo", desc = "Get the biome of the targeted block.", descFooter = "By default, uses all blocks in your selection.")
@CommandPermissions("worldedit.biome.info")
public void biomeInfo(Actor actor, World world, LocalSession session, @Switch(name = 't', desc = "Use the block you are looking at.") boolean useLineOfSight, @Switch(name = 'p', desc = "Use the block you are currently in.") boolean usePosition) throws WorldEditException {
BiomeRegistry biomeRegistry = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS).getRegistries().getBiomeRegistry();
Set<BiomeType> biomes = new HashSet<>();
String messageKey;
if (useLineOfSight) {
if (actor instanceof Player) {
Location blockPosition = ((Player) actor).getBlockTrace(300);
if (blockPosition == null) {
actor.print(Caption.of("worldedit.raytrace.noblock"));
return;
}
BiomeType biome = world.getBiome(blockPosition.toVector().toBlockPoint());
biomes.add(biome);
messageKey = "worldedit.biomeinfo.lineofsight";
} else {
actor.print(Caption.of("worldedit.raytrace.require-player"));
return;
}
} else if (usePosition) {
if (actor instanceof Locatable) {
BiomeType biome = world.getBiome(((Locatable) actor).getLocation().toVector().toBlockPoint());
biomes.add(biome);
messageKey = "worldedit.biomeinfo.position";
} else {
actor.print(Caption.of("worldedit.biomeinfo.not-locatable"));
return;
}
} else {
Region region = session.getSelection(world);
for (BlockVector3 pt : region) {
biomes.add(world.getBiome(pt));
}
messageKey = "worldedit.biomeinfo.selection";
}
List<Component> components = biomes.stream().map(biome -> biomeRegistry.getRichName(biome).hoverEvent(HoverEvent.showText(TextComponent.of(biome.getId())))).collect(Collectors.toList());
actor.print(Caption.of(messageKey, TextUtils.join(components, TextComponent.of(", "))));
}
use of com.sk89q.worldedit.WorldEditException in project FastAsyncWorldEdit by IntellectualSites.
the class MCEditSchematicReader method read.
@Override
public Clipboard read() throws IOException {
// Schematic tag
NamedTag rootTag = inputStream.readNamedTag();
if (!rootTag.getName().equals("Schematic")) {
throw new IOException("Tag 'Schematic' does not exist or is not first");
}
CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
// Check
Map<String, Tag> schematic = schematicTag.getValue();
if (!schematic.containsKey("Blocks")) {
throw new IOException("Schematic file is missing a 'Blocks' tag");
}
// Check type of Schematic
String materials = requireTag(schematic, "Materials", StringTag.class).getValue();
if (!materials.equals("Alpha")) {
throw new IOException("Schematic file is not an Alpha schematic");
}
// ====================================================================
// Metadata
// ====================================================================
BlockVector3 origin;
Region region;
// Get information
short width = requireTag(schematic, "Width", ShortTag.class).getValue();
short height = requireTag(schematic, "Height", ShortTag.class).getValue();
short length = requireTag(schematic, "Length", ShortTag.class).getValue();
try {
int originX = requireTag(schematic, "WEOriginX", IntTag.class).getValue();
int originY = requireTag(schematic, "WEOriginY", IntTag.class).getValue();
int originZ = requireTag(schematic, "WEOriginZ", IntTag.class).getValue();
BlockVector3 min = BlockVector3.at(originX, originY, originZ);
int offsetX = requireTag(schematic, "WEOffsetX", IntTag.class).getValue();
int offsetY = requireTag(schematic, "WEOffsetY", IntTag.class).getValue();
int offsetZ = requireTag(schematic, "WEOffsetZ", IntTag.class).getValue();
BlockVector3 offset = BlockVector3.at(offsetX, offsetY, offsetZ);
origin = min.subtract(offset);
region = new CuboidRegion(min, min.add(width, height, length).subtract(BlockVector3.ONE));
} catch (IOException ignored) {
origin = BlockVector3.ZERO;
region = new CuboidRegion(origin, origin.add(width, height, length).subtract(BlockVector3.ONE));
}
// ====================================================================
// Blocks
// ====================================================================
// Get blocks
byte[] blockId = requireTag(schematic, "Blocks", ByteArrayTag.class).getValue();
byte[] blockData = requireTag(schematic, "Data", ByteArrayTag.class).getValue();
byte[] addId = new byte[0];
// Have to later combine IDs
short[] blocks = new short[blockId.length];
// the highest 4 bits are stored in a separate byte array.
if (schematic.containsKey("AddBlocks")) {
addId = requireTag(schematic, "AddBlocks", ByteArrayTag.class).getValue();
}
// Combine the AddBlocks data with the first 8-bit block ID
for (int index = 0; index < blockId.length; index++) {
if ((index >> 1) >= addId.length) {
// No corresponding AddBlocks index
blocks[index] = (short) (blockId[index] & 0xFF);
} else {
if ((index & 1) == 0) {
blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (blockId[index] & 0xFF));
} else {
blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (blockId[index] & 0xFF));
}
}
}
// Need to pull out tile entities
final ListTag tileEntityTag = getTag(schematic, "TileEntities", ListTag.class);
List<Tag> tileEntities = tileEntityTag == null ? new ArrayList<>() : tileEntityTag.getValue();
BlockMap<BaseBlock> tileEntityBlocks = BlockMap.createForBaseBlock();
for (Tag tag : tileEntities) {
if (!(tag instanceof CompoundTag)) {
continue;
}
CompoundTag t = (CompoundTag) tag;
Map<String, Tag> values = new HashMap<>(t.getValue());
String id = t.getString("id");
values.put("id", new StringTag(convertBlockEntityId(id)));
int x = t.getInt("x");
int y = t.getInt("y");
int z = t.getInt("z");
int index = y * width * length + z * width + x;
// position in schematics?
if (index >= blocks.length) {
LOGGER.warn("Skipping corrupt tile entity at position {} {} {} in schematic.", x, y, z);
continue;
}
BlockState block = getBlockState(blocks[index], blockData[index]);
BlockState newBlock = block;
if (newBlock != null) {
for (NBTCompatibilityHandler handler : COMPATIBILITY_HANDLERS) {
if (handler.isAffectedBlock(newBlock)) {
newBlock = handler.updateNBT(block, values).toImmutableState();
if (newBlock == null || values.isEmpty()) {
break;
}
}
}
}
if (values.isEmpty()) {
t = null;
} else {
t = new CompoundTag(values);
}
if (fixer != null && t != null) {
// FAWE start - BinaryTag
t = (CompoundTag) AdventureNBTConverter.fromAdventure(fixer.fixUp(DataFixer.FixTypes.BLOCK_ENTITY, t.asBinaryTag(), -1));
// FAWE end
}
BlockVector3 vec = BlockVector3.at(x, y, z);
// Insert into the map if we have changed the block or have a tag
BlockState blockToInsert = newBlock != null ? newBlock : (t != null ? block : null);
if (blockToInsert != null) {
BaseBlock baseBlock = t != null ? blockToInsert.toBaseBlock(new CompoundTag(t.getValue())) : blockToInsert.toBaseBlock();
tileEntityBlocks.put(vec, baseBlock);
}
}
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
clipboard.setOrigin(origin);
Set<Integer> unknownBlocks = new HashSet<>();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
for (int z = 0; z < length; ++z) {
int index = y * width * length + z * width + x;
BlockVector3 pt = BlockVector3.at(x, y, z);
BaseBlock state = Optional.ofNullable(tileEntityBlocks.get(pt)).orElseGet(() -> {
BlockState blockState = getBlockState(blocks[index], blockData[index]);
return blockState == null ? null : blockState.toBaseBlock();
});
try {
if (state != null) {
clipboard.setBlock(region.getMinimumPoint().add(pt), state);
} else {
short block = blocks[index];
byte data = blockData[index];
int combined = block << 8 | data;
if (unknownBlocks.add(combined)) {
LOGGER.warn("Unknown block when loading schematic: {} {}. This is most likely a" + "bad schematic.", block, data);
}
}
} catch (WorldEditException ignored) {
// BlockArrayClipboard won't throw this
}
}
}
}
// ====================================================================
// Entities
// ====================================================================
ListTag entityList = getTag(schematic, "Entities", ListTag.class);
if (entityList != null) {
List<Tag> entityTags = entityList.getValue();
for (Tag tag : entityTags) {
if (tag instanceof CompoundTag) {
CompoundTag compound = (CompoundTag) tag;
if (fixer != null) {
// FAWE start - BinaryTag
compound = (CompoundTag) AdventureNBTConverter.fromAdventure(fixer.fixUp(DataFixer.FixTypes.ENTITY, compound.asBinaryTag(), -1));
// FAWE end
}
String id = convertEntityId(compound.getString("id"));
Location location = NBTConversions.toLocation(clipboard, compound.getListTag("Pos"), compound.getListTag("Rotation"));
if (!id.isEmpty()) {
EntityType entityType = EntityTypes.get(id.toLowerCase(Locale.ROOT));
if (entityType != null) {
for (EntityNBTCompatibilityHandler compatibilityHandler : ENTITY_COMPATIBILITY_HANDLERS) {
if (compatibilityHandler.isAffectedEntity(entityType, compound)) {
compound = compatibilityHandler.updateNBT(entityType, compound);
}
}
BaseEntity state = new BaseEntity(entityType, compound);
clipboard.createEntity(location, state);
} else {
LOGGER.warn("Unknown entity when pasting schematic: " + id.toLowerCase(Locale.ROOT));
}
}
}
}
}
return clipboard;
}
use of com.sk89q.worldedit.WorldEditException in project FastAsyncWorldEdit by IntellectualSites.
the class SpongeSchematicReader method readVersion1.
private BlockArrayClipboard readVersion1(CompoundTag schematicTag) throws IOException {
BlockVector3 origin;
Region region;
Map<String, Tag> schematic = schematicTag.getValue();
int width = requireTag(schematic, "Width", ShortTag.class).getValue();
int height = requireTag(schematic, "Height", ShortTag.class).getValue();
int length = requireTag(schematic, "Length", ShortTag.class).getValue();
IntArrayTag offsetTag = getTag(schematic, "Offset", IntArrayTag.class);
int[] offsetParts;
if (offsetTag != null) {
offsetParts = offsetTag.getValue();
if (offsetParts.length != 3) {
throw new IOException("Invalid offset specified in schematic.");
}
} else {
offsetParts = new int[] { 0, 0, 0 };
}
BlockVector3 min = BlockVector3.at(offsetParts[0], offsetParts[1], offsetParts[2]);
CompoundTag metadataTag = getTag(schematic, "Metadata", CompoundTag.class);
if (metadataTag != null && metadataTag.containsKey("WEOffsetX")) {
// We appear to have WorldEdit Metadata
Map<String, Tag> metadata = metadataTag.getValue();
int offsetX = requireTag(metadata, "WEOffsetX", IntTag.class).getValue();
int offsetY = requireTag(metadata, "WEOffsetY", IntTag.class).getValue();
int offsetZ = requireTag(metadata, "WEOffsetZ", IntTag.class).getValue();
BlockVector3 offset = BlockVector3.at(offsetX, offsetY, offsetZ);
origin = min.subtract(offset);
region = new CuboidRegion(min, min.add(width, height, length).subtract(BlockVector3.ONE));
} else {
origin = min;
region = new CuboidRegion(origin, origin.add(width, height, length).subtract(BlockVector3.ONE));
}
IntTag paletteMaxTag = getTag(schematic, "PaletteMax", IntTag.class);
Map<String, Tag> paletteObject = requireTag(schematic, "Palette", CompoundTag.class).getValue();
if (paletteMaxTag != null && paletteObject.size() != paletteMaxTag.getValue()) {
throw new IOException("Block palette size does not match expected size.");
}
Map<Integer, BlockState> palette = new HashMap<>();
ParserContext parserContext = new ParserContext();
parserContext.setRestricted(false);
parserContext.setTryLegacy(false);
parserContext.setPreferringWildcard(false);
for (String palettePart : paletteObject.keySet()) {
int id = requireTag(paletteObject, palettePart, IntTag.class).getValue();
if (fixer != null) {
palettePart = fixer.fixUp(DataFixer.FixTypes.BLOCK_STATE, palettePart, dataVersion);
}
BlockState state;
try {
state = WorldEdit.getInstance().getBlockFactory().parseFromInput(palettePart, parserContext).toImmutableState();
} catch (InputParseException e) {
LOGGER.warn("Invalid BlockState in palette: " + palettePart + ". Block will be replaced with air.");
state = BlockTypes.AIR.getDefaultState();
}
palette.put(id, state);
}
byte[] blocks = requireTag(schematic, "BlockData", ByteArrayTag.class).getValue();
Map<BlockVector3, Map<String, Tag>> tileEntitiesMap = new HashMap<>();
ListTag tileEntities = getTag(schematic, "BlockEntities", ListTag.class);
if (tileEntities == null) {
tileEntities = getTag(schematic, "TileEntities", ListTag.class);
}
if (tileEntities != null) {
List<Map<String, Tag>> tileEntityTags = tileEntities.getValue().stream().map(tag -> (CompoundTag) tag).map(CompoundTag::getValue).collect(Collectors.toList());
for (Map<String, Tag> tileEntity : tileEntityTags) {
int[] pos = requireTag(tileEntity, "Pos", IntArrayTag.class).getValue();
final BlockVector3 pt = BlockVector3.at(pos[0], pos[1], pos[2]);
Map<String, Tag> values = Maps.newHashMap(tileEntity);
values.put("x", new IntTag(pt.getBlockX()));
values.put("y", new IntTag(pt.getBlockY()));
values.put("z", new IntTag(pt.getBlockZ()));
// FAWE start - support old, corrupt schematics
Tag id = values.get("Id");
if (id == null) {
id = values.get("id");
}
if (id == null) {
continue;
}
// FAWE end
values.put("id", values.get("Id"));
values.remove("Id");
values.remove("Pos");
if (fixer != null) {
// FAWE start - BinaryTag
tileEntity = ((CompoundTag) AdventureNBTConverter.fromAdventure(fixer.fixUp(DataFixer.FixTypes.BLOCK_ENTITY, new CompoundTag(values).asBinaryTag(), dataVersion))).getValue();
// FAWE end
} else {
tileEntity = values;
}
tileEntitiesMap.put(pt, tileEntity);
}
}
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
clipboard.setOrigin(origin);
int index = 0;
int i = 0;
int value;
int varintLength;
while (i < blocks.length) {
value = 0;
varintLength = 0;
while (true) {
value |= (blocks[i] & 127) << (varintLength++ * 7);
if (varintLength > 5) {
throw new IOException("VarInt too big (probably corrupted data)");
}
if ((blocks[i] & 128) != 128) {
i++;
break;
}
i++;
}
// index = (y * length * width) + (z * width) + x
int y = index / (width * length);
int z = (index % (width * length)) / width;
int x = (index % (width * length)) % width;
BlockState state = palette.get(value);
BlockVector3 pt = BlockVector3.at(x, y, z);
try {
if (tileEntitiesMap.containsKey(pt)) {
clipboard.setBlock(clipboard.getMinimumPoint().add(pt), state.toBaseBlock(new CompoundTag(tileEntitiesMap.get(pt))));
} else {
clipboard.setBlock(clipboard.getMinimumPoint().add(pt), state);
}
} catch (WorldEditException e) {
throw new IOException("Failed to load a block in the schematic");
}
index++;
}
return clipboard;
}
use of com.sk89q.worldedit.WorldEditException in project Plot-System by AlpsBTE.
the class AbstractPlotGenerator method generateOutlines.
/**
* Generates plot schematic and outlines
* @param plotSchematic - schematic file
*/
protected void generateOutlines(File plotSchematic) {
try {
if (plotSchematic != null) {
Vector buildingOutlinesCoordinates = PlotManager.getPlotCenter();
com.sk89q.worldedit.world.World weWorld = new BukkitWorld(plot.getWorld().getBukkitWorld());
Clipboard clipboard = ClipboardFormat.SCHEMATIC.getReader(new FileInputStream(plotSchematic)).read(weWorld.getWorldData());
// Place the bottom part of the schematic 5 blocks above 0
double heightDif = clipboard.getOrigin().getY() - clipboard.getMinimumPoint().getY();
buildingOutlinesCoordinates = buildingOutlinesCoordinates.add(0, heightDif, 0);
ClipboardHolder clipboardHolder = new ClipboardHolder(clipboard, weWorld.getWorldData());
EditSession editSession = PlotSystem.DependencyManager.getWorldEdit().getEditSessionFactory().getEditSession(weWorld, -1);
Operation operation = clipboardHolder.createPaste(editSession, weWorld.getWorldData()).to(buildingOutlinesCoordinates).ignoreAirBlocks(false).build();
Operations.complete(operation);
editSession.flushQueue();
Location spawnLocation = plot.getWorld().getSpawnPoint();
if (spawnLocation != null)
plot.getWorld().getBukkitWorld().setSpawnLocation(spawnLocation);
}
} catch (IOException | WorldEditException ex) {
Bukkit.getLogger().log(Level.SEVERE, "An error occurred while generating plot outlines!", ex);
throw new RuntimeException("Plot outlines generation completed exceptionally");
}
}
use of com.sk89q.worldedit.WorldEditException in project IridiumSkyblock by Iridium-Development.
the class WorldEdit method paste.
@Override
public void paste(File file, Location location, Boolean ignoreAirBlock, CompletableFuture<Void> completableFuture) {
try {
ClipboardFormat format = cachedClipboardFormat.getOrDefault(file, ClipboardFormats.findByFile(file));
ClipboardReader reader = format.getReader(new FileInputStream(file));
Clipboard clipboard = reader.read();
int width = clipboard.getDimensions().getBlockX();
int height = clipboard.getDimensions().getBlockY();
int length = clipboard.getDimensions().getBlockZ();
// Centers the schematic
location.subtract(width / 2.00, height / 2.00, length / 2.00);
// Change the //copy point to the minimum corner
clipboard.setOrigin(clipboard.getRegion().getMinimumPoint());
try (EditSession editSession = com.sk89q.worldedit.WorldEdit.getInstance().newEditSession(new BukkitWorld(location.getWorld()))) {
Operation operation = new ClipboardHolder(clipboard).createPaste(editSession).to(BlockVector3.at(location.getX(), location.getY(), location.getZ())).copyEntities(true).ignoreAirBlocks(ignoreAirBlock).build();
Operations.complete(operation);
Operations.complete(editSession.commit());
cachedClipboardFormat.putIfAbsent(file, format);
completableFuture.complete(null);
}
} catch (IOException | WorldEditException e) {
e.printStackTrace();
}
}
Aggregations