private Vector translate(Vector pos) { StructureBoundingBox boundingBox = structure.getBoundingBox(); switch (structure.getOrientation()) { case EAST: return new Vector( boundingBox.getMax().getBlockX() - pos.getBlockZ(), boundingBox.getMin().getBlockY() + pos.getBlockY(), boundingBox.getMin().getBlockZ() + pos.getBlockX()); case SOUTH: return new Vector( boundingBox.getMin().getBlockX() + pos.getBlockX(), boundingBox.getMin().getBlockY() + pos.getBlockY(), boundingBox.getMax().getBlockZ() - pos.getBlockZ()); case WEST: return new Vector( boundingBox.getMin().getBlockX() + pos.getBlockZ(), boundingBox.getMin().getBlockY() + pos.getBlockY(), boundingBox.getMin().getBlockZ() + pos.getBlockX()); default: // NORTH return new Vector( boundingBox.getMin().getBlockX() + pos.getBlockX(), boundingBox.getMin().getBlockY() + pos.getBlockY(), boundingBox.getMin().getBlockZ() + pos.getBlockZ()); } }
public void fill( Vector min, Vector max, Material outerType, MaterialData outerData, Material innerType, MaterialData innerData) { for (int y = min.getBlockY(); y <= max.getBlockY(); y++) { for (int x = min.getBlockX(); x <= max.getBlockX(); x++) { for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) { Material type; MaterialData data; if (x != min.getBlockX() && x != max.getBlockX() && z != min.getBlockZ() && z != max.getBlockZ() && y != min.getBlockY() && y != max.getBlockY()) { type = innerType; data = innerData; } else { type = outerType; data = outerData; } setBlock(new Vector(x, y, z), type, data); } } } }
public void setBlockDownward(Vector pos, Material type, MaterialData data) { Vector vec = translate(pos); if (boundingBox.isVectorInside(vec)) { int y = vec.getBlockY(); while (!world.getBlockAt(vec.getBlockX(), y, vec.getBlockZ()).getType().isSolid() && y > 1) { delegate.setTypeAndData(world, vec.getBlockX(), y, vec.getBlockZ(), type, data); y--; } } }
public void fillWithRandomMaterial( Vector min, Vector max, Random random, Map<StructureMaterial, Integer> materials) { for (int y = min.getBlockY(); y <= max.getBlockY(); y++) { for (int x = min.getBlockX(); x <= max.getBlockX(); x++) { for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) { StructureMaterial material = getRandomMaterial(random, materials); setBlock(new Vector(x, y, z), material.getType(), material.getData()); } } } }
private void setArea(Material m) { Vector min = area.getCorners()[0]; Vector max = area.getCorners()[1]; World w = Bukkit.getWorld(area.getWorld()); for (int x = min.getBlockX(); x <= max.getBlockX(); x++) { for (int y = min.getBlockY(); y <= max.getBlockY(); y++) { for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) { w.getBlockAt(x, y, z).setType(m); } } } }
@Override public boolean contains(Vector v) { int x = v.getBlockX() + center.getBlockX(); int y = v.getBlockY() + center.getBlockY(); int z = v.getBlockZ() + center.getBlockZ(); return (x >= 0 && x <= size.getBlockX() && y >= 0 && y <= size.getBlockY() && z >= 0 && z <= size.getBlockZ()); }
@Override public void run() { if (master.contchunks.isEmpty()) return; World world = master.team.getMatch().getWorld(); Location loc = new Location(world, 0, 0, 0); Set<BlockData> goals = master.searching; for (int i = 0; i < 15; i++) // 15 chunks per run { Vector vec = master.entitychunks.poll(); if (vec == null) { this.cancel(); return; } BlockState[] containers = world .getChunkAt(vec.getBlockX(), vec.getBlockZ()) .getTileEntities(); // actually chunk-coords for (BlockState state : containers) { if (state instanceof InventoryHolder) { Inventory inv = ((InventoryHolder) state).getInventory(); if (state instanceof Chest) inv = ((Chest) state).getBlockInventory(); checkInventory(inv, goals, state.getLocation(loc)); } // TODO: spawners } } }
public boolean spawnMob(Vector pos, EntityType entityType) { Vector vec = translate(pos); return boundingBox.isVectorInside(vec) && world.spawnEntity( new Location(world, vec.getBlockX(), vec.getBlockY(), vec.getBlockZ()), entityType) != null; }
public void createMobSpawner(Vector pos, EntityType entityType) { Vector vec = translate(pos); if (boundingBox.isVectorInside(vec)) { BlockState state = world.getBlockAt(vec.getBlockX(), vec.getBlockY(), vec.getBlockZ()).getState(); delegate.backupBlockState(state.getBlock()); state.setType(Material.MOB_SPAWNER); state.update(true); state = world.getBlockAt(vec.getBlockX(), vec.getBlockY(), vec.getBlockZ()).getState(); if (state instanceof CreatureSpawner) { ((CreatureSpawner) state).setSpawnedType(entityType); } } }
@Override public boolean doPowerSign( PowerSigns plugin, Block signBlock, String action, Matcher argsm, Boolean isOn) { Sign signState = (Sign) signBlock.getState(); BlockFace signDir = PowerSigns.getSignDirection(signBlock); Vector dir = PowerSigns.strToVector(argsm.group(1), signDir); Block invBlock = signBlock.getRelative(dir.getBlockX(), dir.getBlockY(), dir.getBlockZ()); Inventory inventory; BlockState state = invBlock.getState(); if (state instanceof InventoryHolder) inventory = ((InventoryHolder) state).getInventory(); else return plugin.debugFail("bad inv:" + invBlock.getType().toString() + " " + dir.toString()); Material[] materials = PowerSigns.getMaterials(signState.getLine(1)); int count = 0; for (Material material : materials) count += PowerSigns.inventoryCount(inventory, material); signState.setLine(2, Integer.toString(count)); plugin.updateSignState(signState); return true; }
public void setBlock(Vector pos, Material type, int data) { Vector vec = translate(pos); if (boundingBox.isVectorInside(vec)) { delegate.setTypeAndRawData( world, vec.getBlockX(), vec.getBlockY(), vec.getBlockZ(), type, data); } }
/** * Get next block * * @return next block position */ public boolean getNextBlock() { prevPos = targetPos; do { curDistance += checkDistance; targetPosDouble.add(offset); targetPos = targetPosDouble.toBlockVector(); } while (curDistance <= maxDistance && targetPos.getBlockX() == prevPos.getBlockX() && targetPos.getBlockY() == prevPos.getBlockY() && targetPos.getBlockZ() == prevPos.getBlockZ()); if (curDistance > maxDistance) { return false; } return true; }
/** * Fill the {@link Cube} with a specific block type. * * @param type Block type. * @see http://www.minecraftwiki.net/wiki/Data_values */ public void fill(int type) { World world = point.getWorld(); Vector end = end(); for (int x = point.getBlockX(); x < end.getBlockX(); x++) { for (int y = point.getBlockY(); y < end.getBlockY(); y++) { for (int z = point.getBlockZ(); z < end.getBlockZ(); z++) { Block block = world.getBlockAt(x, y, z); block.setTypeId(type); } } } }
/** * Find a suitable location for a player to teleport to in the given world. This method uses * WorldBorder as the configuration source. * * @param world World to teleport to * @return Location to teleport to */ private Location findSuitableLocationWB(World world) { BorderData borderData = WorldBorder.plugin.GetWorldBorder(world.getName()); if (borderData == null) { // throw new IllegalStateException(String.format("World %1$s isn't configured in // WorldBorder.", world.getName())); return null; } for (int i = 0; i < 100; i++) { Vector position = new Vector(borderData.getX(), 0.0, borderData.getZ()); // Get a uniform-area random position within the world border's geometry boolean isRound = (borderData.getShape() == null) ? true : borderData.getShape(); if (isRound) { position.add( getRandomPointInEllipse(random, borderData.getRadiusX(), borderData.getRadiusZ())); } else { position.add( getRandomPointInRectangle(random, borderData.getRadiusX(), borderData.getRadiusZ())); } // Ensure there's a solid block to stand on Block highestBlock = world.getHighestBlockAt(position.getBlockX(), position.getBlockZ()); if (highestBlock == null) continue; highestBlock = highestBlock.getRelative(0, -1, 0); // Because the javadocs are wrong. if (highestBlock == null) continue; if (highestBlock.getY() < 1 || highestBlock.getY() >= world.getMaxHeight() - 2) continue; if (highestBlock.isLiquid()) continue; position.setX((double) position.getBlockX() + 0.5); position.setY(highestBlock.getY() + 2); position.setZ((double) position.getBlockZ() + 0.5); return position.toLocation(world, getRandomYaw(), 0.0f); } return null; }
public boolean createRandomItemsContainer( Vector pos, Random random, RandomItemsContent content, DirectionalContainer container, int maxStacks) { Vector vec = translate(pos); if (boundingBox.isVectorInside(vec)) { BlockState state = world.getBlockAt(vec.getBlockX(), vec.getBlockY(), vec.getBlockZ()).getState(); delegate.backupBlockState(state.getBlock()); state.setType(container.getItemType()); state.setData(container); state.update(true); return content.fillContainer(random, container, state, maxStacks); } return false; }
public WorldEditSchematic(Object schematic) { weSchematic = (CuboidClipboard) schematic; // Center at the bottom X,Z center // This should be configurable, maybe? try { com.sk89q.worldedit.Vector weSize = weSchematic.getSize(); size = new Vector(weSize.getBlockX(), weSize.getBlockY(), weSize.getBlockZ()); center = new Vector(Math.floor(size.getBlockX() / 2), 0, Math.floor(size.getBlockZ() / 2)); } catch (Exception ex) { ex.printStackTrace(); } if (center == null) { center = new Vector(0, 0, 0); } if (size == null) { size = new Vector(0, 0, 0); } }
@SuppressWarnings({"deprecation"}) @Override public MaterialAndData getBlock(Vector v) { int x = v.getBlockX() + center.getBlockX(); int y = v.getBlockY() + center.getBlockY(); int z = v.getBlockZ() + center.getBlockZ(); try { if (x < 0 || x >= size.getBlockZ() || y < 0 || y >= size.getBlockY() || z < 0 || z >= size.getBlockZ()) { return null; } com.sk89q.worldedit.Vector vector = new com.sk89q.worldedit.Vector(x, y, z); BaseBlock baseBlock = weSchematic.getBlock(vector); Material material = Material.getMaterial(baseBlock.getId()); int materialData = baseBlock.getData(); MaterialAndData blockData = new MaterialAndData(material, (byte) materialData); // Note.. we don't actually get a SignBlock here, for some reason. // May have something to do with loading schematics not actually supporting sign // text, it doesn't work with //schematic and //paste, either. // It looks like //paste works in a dev build of WE, but it still doesn't give me the blocks // Looking at WE's code, it seems like the part that's needed is commented out... ?? if (material == Material.SIGN_POST || material == Material.WALL_SIGN) { try { if (baseBlock.hasNbtData()) { SignBlock signBlock = new SignBlock(material.getId(), materialData); CompoundTag nbtData = baseBlock.getNbtData(); signBlock.setNbtData(nbtData); blockData.setSignLines(signBlock.getText()); } } catch (Throwable ex) { ex.printStackTrace(); } } else if (material == Material.COMMAND) { try { if (baseBlock.hasNbtData()) { CompoundTag nbtRoot = baseBlock.getNbtData(); Map<String, Tag> rootValues = nbtRoot.getValue(); if (rootValues.containsKey("Command")) { Object commandValue = rootValues.get("Command").getValue(); blockData.setCommandLine((String) commandValue); } if (rootValues.containsKey("CustomName")) { Object nameValue = rootValues.get("CustomName").getValue(); blockData.setCustomName((String) nameValue); } } } catch (Throwable ex) { ex.printStackTrace(); } } else if (material == Material.CHEST) { try { if (baseBlock.hasNbtData()) { ChestBlock chestBlock = new ChestBlock(materialData); CompoundTag nbtRoot = baseBlock.getNbtData(); chestBlock.setNbtData(nbtRoot); BaseItemStack[] items = chestBlock.getItems(); if (items != null && items.length > 0) { ItemStack[] contents = new ItemStack[items.length]; for (int i = 0; i < items.length; i++) { if (items[i] != null) { Material itemMaterial = Material.getMaterial(items[i].getType()); // Bukkit.getLogger().info("Item from chest: " + itemMaterial + " at " + i + " / " // + contents.length); short itemData = items[i].getData(); int itemAmount = items[i].getAmount(); ItemStack newStack = new ItemStack(itemMaterial, itemAmount, itemData); Map<Integer, Integer> enchantments = items[i].getEnchantments(); if (enchantments != null && enchantments.size() > 0) { for (Entry<Integer, Integer> enchantment : enchantments.entrySet()) { try { Enchantment enchantmentType = Enchantment.getById(enchantment.getKey()); newStack.addEnchantment(enchantmentType, enchantment.getValue()); } catch (Exception ex) { // This seems to happen a lot .. like on potions especially. ex.printStackTrace(); } } } contents[i] = newStack; } } blockData.setInventoryContents(contents); } } } catch (Throwable ex) { ex.printStackTrace(); } } return blockData; } catch (ArrayIndexOutOfBoundsException ex) { // TODO: Figure out why this still happens, even with the size check } catch (Throwable ex) { ex.printStackTrace(); } return null; }
/** * Construct an immutable integer 3D vector from a mutable Bukkit vector. * * @param vector - the mutable real Bukkit vector to copy. */ public BlockPosition(Vector vector) { if (vector == null) throw new IllegalArgumentException("Vector cannot be NULL."); this.x = vector.getBlockX(); this.y = vector.getBlockY(); this.z = vector.getBlockZ(); }
@EventHandler public void onPlayerCommand(PlayerCommandPreprocessEvent evt) { Player p = evt.getPlayer(); String fCmd = evt.getMessage(); String cmd = fCmd.substring(1).split(" ", 1)[0]; String[] args = fCmd.substring(cmd.length() + 1).split(" "); if (cmd.equalsIgnoreCase("startexport")) { p.sendMessage("Starting export!"); bSets.put(p.getName(), new BlockSet()); evt.setCancelled(true); } else if (cmd.equalsIgnoreCase("export")) { BlockSet bs = bSets.get(p.getName()); if (bs == null) { p.sendMessage("You must first use the /startexport command!"); evt.setCancelled(true); return; } p.sendMessage("Exporting..."); List<NiceBlock> niceBlocks = new ArrayList<NiceBlock>(); Vector firstBlock = bs.getFirstBlock(); Vector secondBlock = bs.getSecondBlock(); World w = p.getWorld(); // http://forums.bukkit.org/threads/block-listed-within-cuboid-x-y-coodinates.103681/ int topBlockX = (firstBlock.getBlockX() < secondBlock.getBlockX() ? secondBlock.getBlockX() : firstBlock.getBlockX()); int bottomBlockX = (firstBlock.getBlockX() > secondBlock.getBlockX() ? secondBlock.getBlockX() : firstBlock.getBlockX()); int topBlockY = (firstBlock.getBlockY() < secondBlock.getBlockY() ? secondBlock.getBlockY() : firstBlock.getBlockY()); int bottomBlockY = (firstBlock.getBlockY() > secondBlock.getBlockY() ? secondBlock.getBlockY() : firstBlock.getBlockY()); int topBlockZ = (firstBlock.getBlockZ() < secondBlock.getBlockZ() ? secondBlock.getBlockZ() : firstBlock.getBlockZ()); int bottomBlockZ = (firstBlock.getBlockZ() > secondBlock.getBlockZ() ? secondBlock.getBlockZ() : firstBlock.getBlockZ()); int xCtr = 0; for (int x = bottomBlockX; x <= topBlockX; x++) { int zCtr = 0; for (int z = bottomBlockZ; z <= topBlockZ; z++) { int yCtr = 0; for (int y = bottomBlockY; y <= topBlockY; y++) { Block b = w.getBlockAt(x, y, z); MaterialData data = b.getState().getData(); NiceBlock nb = new NiceBlock(b.getTypeId(), xCtr, yCtr, zCtr, data.getData()); niceBlocks.add(nb); yCtr++; } zCtr++; } xCtr++; } File exportFolder = new File("block_exports"); if (!exportFolder.exists()) { exportFolder.mkdir(); } try { File datFile = new File(exportFolder, "export.dat"); if (!datFile.exists()) { datFile.createNewFile(); } FileOutputStream fos = new FileOutputStream(datFile); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(niceBlocks); oos.close(); } catch (Exception ex) { ex.printStackTrace(); } p.sendMessage("Done!"); bSets.remove(p.getName()); evt.setCancelled(true); } }
private boolean circleLineCollide( Location startLoc, Location endLoc, Location circleLoc, double radiusSquared) { Location lineStart = startLoc.clone(); Location lineEnd = endLoc.clone(); Location circleCenter = circleLoc.clone(); Vector direction = vectorFromLocations(lineStart, lineEnd); if (direction.getZ() == 0) { if (direction.getBlockX() == 0) { return false; } flipXZ(lineStart); flipXZ(lineEnd); flipXZ(circleCenter); flipXZ(direction); } Vector start = lineStart.toVector(); Vector end = lineEnd.toVector(); Vector circle = circleCenter.toVector(); double slope = direction.getZ() / direction.getX(); double perpSlope = -1 / slope; // This is the closest x if this line segment was extended for ever double closestX = (slope * start.getX() - perpSlope * circle.getX() + circle.getBlockZ() - start.getZ()) / (slope - perpSlope); // Getting the Z from the x is easy double closestZ = slope * (closestX - start.getX()) + start.getZ(); Vector closest = new Vector(closestX, 0, closestZ); double distanceSquared = closest.clone().subtract(circle).lengthSquared(); if (distanceSquared > radiusSquared) { return false; } if (((closest.getX() > lineStart.getX() && closest.getX() > lineEnd.getX()) || (closest.getZ() > lineStart.getZ() && closest.getZ() > lineEnd.getZ())) || ((closest.getX() < lineStart.getX() && closest.getX() < lineEnd.getX()) || (closest.getZ() < lineStart.getZ() && closest.getZ() < lineEnd.getZ()))) { if (closest.clone().subtract(end).lengthSquared() < closest.clone().subtract(start).lengthSquared()) { closest = end; } else { closest = start; } } distanceSquared = closest.subtract(circle).lengthSquared(); if (distanceSquared > radiusSquared) { return false; } return true; }
/** * Converts a Vector into a int[3] ([x;y;z]) * * @param vector Vector to convert * @return Converted Vector */ public static int[] toArray(Vector vector) { if (vector == null) return null; return new int[] {vector.getBlockX(), vector.getBlockY(), vector.getBlockZ()}; }
public BlockState getBlockState(Vector pos) { Vector vec = translate(pos); return delegate.getBlockState(world, vec.getBlockX(), vec.getBlockY(), vec.getBlockZ()); }