@Override public int hashCode() { int ret = toImmutableState().hashCode() << 3; if (hasNbtData()) { ret += getNbtData().hashCode(); } return ret; }
BlockState(BlockType blockType) { this.blockType = blockType; this.values = new LinkedHashMap<>(); this.emptyBaseBlock = new BaseBlock(this); }
@Override public boolean test(BlockVector3 vector) { BlockState block = getExtent().getBlock(vector); for (BaseBlock testBlock : blocks) { if (testBlock.equalsFuzzy(block)) { return true; } } return false; }
if (block instanceof BaseBlock && ((BaseBlock) block).hasNbtData()) { NBTTagCompound nativeTag = NBTConverter.toNative(((BaseBlock) block).getNbtData()); nativeTag.setString("id", ((BaseBlock) block).getNbtId()); TileEntityUtils.setTileEntity(world, position, nativeTag);
@Override public <B extends BlockStateHolder<B>> void sendFakeBlock(BlockVector3 pos, B block) { BlockPos loc = ForgeAdapter.toBlockPos(pos); if (block == null) { // TODO // player.sendBlockChange(loc, player.getWorld().getBlockAt(loc).getBlockData()); } else { // TODO // player.sendBlockChange(loc, BukkitAdapter.adapt(block)); if (block instanceof BaseBlock && ((BaseBlock) block).hasNbtData()) { player.connection.sendPacket(new SPacketUpdateTileEntity( new BlockPos(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()), 7, NBTConverter.toNative(((BaseBlock) block).getNbtData())) ); } } }
BlockVector3 point = BlockVector3.at(x0, y0, z0); BaseBlock block = clipboard.getFullBlock(point); if (block.getNbtData() != null) { Map<String, Tag> values = new HashMap<>(); for (Map.Entry<String, Tag> entry : block.getNbtData().getValue().entrySet()) { values.put(entry.getKey(), entry.getValue()); values.remove("z"); values.put("Id", new StringTag(block.getNbtId())); values.put("Pos", new IntArrayTag(new int[]{ x, String blockKey = block.toImmutableState().getAsString(); int blockId; if (palette.containsKey(blockKey)) {
@Override public boolean hasNbtData() { return getNbtData() != null; }
@Override public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block, boolean notifyAndLight) throws WorldEditException { checkNotNull(position); checkNotNull(block); World world = getWorldChecked(); // First set the block Vector3i pos = new Vector3i(position.getX(), position.getY(), position.getZ()); BlockState newState = getBlockState(block); BlockSnapshot snapshot = builder.reset() .blockState(newState) .position(pos) .world(world.getProperties()) .build(); snapshot.restore(true, notifyAndLight ? BlockChangeFlags.ALL : BlockChangeFlags.NONE); // Create the TileEntity if (block instanceof BaseBlock && ((BaseBlock) block).hasNbtData()) { // Kill the old TileEntity world.getTileEntity(pos).ifPresent(tileEntity -> applyTileEntityData(tileEntity, (BaseBlock) block)); } return true; }
/** * Transform NBT data in the given block state and return a new instance * if the NBT data needs to be transformed. * * @param state the existing state * @return a new state or the existing one */ private BaseBlock transformNbtData(BaseBlock state) { CompoundTag tag = state.getNbtData(); if (tag != null) { // Handle blocks which store their rotation in NBT if (tag.containsKey("Rot")) { int rot = tag.asInt("Rot"); Direction direction = MCDirections.fromRotation(rot); if (direction != null) { Vector3 vector = transform.apply(direction.toVector()).subtract(transform.apply(Vector3.ZERO)).normalize(); Direction newDirection = Direction.findClosest(vector, Flag.CARDINAL | Flag.ORDINAL | Flag.SECONDARY_ORDINAL); if (newDirection != null) { CompoundTagBuilder builder = tag.createBuilder(); builder.putByte("Rot", (byte) MCDirections.toRotation(newDirection)); return state.toBaseBlock(builder.build()); } } } } return state; }
@Nullable @Override public BlockState apply(@Nullable String input) { try { return WorldEdit.getInstance().getBlockFactory().parseFromInput(input, TO_BLOCK_CONTEXT).toImmutableState(); } catch (InputParseException e) { e.printStackTrace(); return null; } } });
@Override public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) { World world = (World) clicked.getExtent(); EditSession editSession = session.createEditSession(player); BlockVector3 blockPoint = clicked.toVector().toBlockPoint(); BaseBlock block = editSession.getFullBlock(blockPoint); player.print("\u00A79@" + clicked.toVector() + ": " + "\u00A7e" + block.getBlockType().getName() + "\u00A77" + " (" + block.toString() + ") " + "\u00A7f" + " (" + world.getBlockLightLevel(blockPoint) + "/" + world.getBlockLightLevel(blockPoint.add(0, 1, 0)) + ")"); if (block instanceof MobSpawnerBlock) { player.printRaw("\u00A7e" + "Mob Type: " + ((MobSpawnerBlock) block).getMobType()); } return true; }
blockType = blockInHand.getBlockType(); blockStates = blockInHand.getStates(); } else if ("offhand".equalsIgnoreCase(typeString)) { blockType = blockInHand.getBlockType(); blockStates = blockInHand.getStates(); } else if ("pos1".equalsIgnoreCase(typeString)) {
@Command( aliases = { "lrbuild", "/lrbuild" }, usage = "<leftclick block> <rightclick block>", desc = "Long-range building tool", min = 2, max = 2 ) @CommandPermissions("worldedit.tool.lrbuild") public void longrangebuildtool(Player player, LocalSession session, Pattern secondary, Pattern primary) throws WorldEditException { BaseItemStack itemStack = player.getItemInHand(HandSide.MAIN_HAND); session.setTool(itemStack.getType(), new LongRangeBuildTool(primary, secondary)); player.print("Long-range building tool bound to " + itemStack.getType().getName() + "."); String primaryName = "pattern"; String secondaryName = "pattern"; if (primary instanceof BlockPattern) { primaryName = ((BlockPattern) primary).getBlock().getBlockType().getName(); } if (secondary instanceof BlockPattern) { secondaryName = ((BlockPattern) secondary).getBlock().getBlockType().getName(); } player.print("Left-click set to " + secondaryName + "; right-click set to " + primaryName + "."); } }
@Override public <B extends BlockStateHolder<B>> void sendFakeBlock(BlockVector3 pos, B block) { Location loc = new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ()); if (block == null) { player.sendBlockChange(loc, player.getWorld().getBlockAt(loc).getBlockData()); } else { player.sendBlockChange(loc, BukkitAdapter.adapt(block)); if (block instanceof BaseBlock && ((BaseBlock) block).hasNbtData()) { BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); if (adapter != null) { adapter.sendFakeNBT(player, pos, ((BaseBlock) block).getNbtData()); if (block.getBlockType() == BlockTypes.STRUCTURE_BLOCK) { adapter.sendFakeOP(player); } } } } } }
@Override public String getNbtId() { CompoundTag nbtData = getNbtData(); if (nbtData == null) { return ""; } Tag idTag = nbtData.getValue().get("id"); if (idTag instanceof StringTag) { return ((StringTag) idTag).getValue(); } else { return ""; } }
try { String id = blockEntry.getKey(); BlockState state = WorldEdit.getInstance().getBlockFactory().parseFromInput(blockEntry.getValue(), parserContext).toImmutableState(); blockToStringMap.put(state, id); stringToBlockMap.put(id, state);
@Override protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial) { final Vector3 current = Vector3.at(x, y, z); environment.setCurrentBlock(current); final Vector3 scaled = current.subtract(zero).divide(unit); try { if (expression.evaluate(scaled.getX(), scaled.getY(), scaled.getZ(), defaultMaterial.getBlockType().getLegacyId(), 0) <= 0) { // TODO data return null; } return LegacyMapper.getInstance().getBlockFromLegacy((int) typeVariable.getValue(), (int) dataVariable.getValue()).toBaseBlock(); } catch (Exception e) { log.log(Level.WARNING, "Failed to create shape", e); return null; } } };
/** * Checks whether the type ID and data value are equal. */ @Override public boolean equals(Object o) { if (!(o instanceof BaseBlock)) { if (!hasNbtData() && o instanceof BlockStateHolder) { return Objects.equals(toImmutableState(), ((BlockStateHolder<?>) o).toImmutableState()); } return false; } final BaseBlock otherBlock = (BaseBlock) o; return this.blockState.equalsFuzzy(otherBlock.blockState) && Objects.equals(getNbtData(), otherBlock.getNbtData()); }
@Override public <V> BaseBlock with(Property<V> property, V value) { return this.blockState.with(property, value).toBaseBlock(getNbtData()); }
@Override public BlockState getBlock(BlockVector3 position) { if (region.contains(position)) { BlockVector3 v = position.subtract(region.getMinimumPoint()); BaseBlock block = blocks[v.getBlockX()][v.getBlockY()][v.getBlockZ()]; if (block != null) { return block.toImmutableState(); } } return BlockTypes.AIR.getDefaultState(); }