Search in sources :

Example 26 with BlockEntity

use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.

the class BlockChest method place.

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    BlockEntityChest chest = null;
    int[] faces = { 2, 5, 3, 4 };
    this.setDamage(faces[player != null ? player.getDirection().getHorizontalIndex() : 0]);
    for (int side = 2; side <= 5; ++side) {
        if ((this.getDamage() == 4 || this.getDamage() == 5) && (side == 4 || side == 5)) {
            continue;
        } else if ((this.getDamage() == 3 || this.getDamage() == 2) && (side == 2 || side == 3)) {
            continue;
        }
        Block c = this.getSide(BlockFace.fromIndex(side));
        if (c instanceof BlockChest && c.getDamage() == this.getDamage()) {
            BlockEntity blockEntity = this.getLevel().getBlockEntity(c);
            if (blockEntity instanceof BlockEntityChest && !((BlockEntityChest) blockEntity).isPaired()) {
                chest = (BlockEntityChest) blockEntity;
                break;
            }
        }
    }
    this.getLevel().setBlock(block, this, true, true);
    CompoundTag nbt = new CompoundTag("").putList(new ListTag<>("Items")).putString("id", BlockEntity.CHEST).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
    if (item.hasCustomName()) {
        nbt.putString("CustomName", item.getCustomName());
    }
    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }
    BlockEntity blockEntity = new BlockEntityChest(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
    if (chest != null) {
        chest.pairWith(((BlockEntityChest) blockEntity));
        ((BlockEntityChest) blockEntity).pairWith(chest);
    }
    return true;
}
Also used : BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) StringTag(cn.nukkit.nbt.tag.StringTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) ListTag(cn.nukkit.nbt.tag.ListTag) Map(java.util.Map) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 27 with BlockEntity

use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.

the class BlockEnchantingTable method onActivate.

@Override
public boolean onActivate(Item item, Player player) {
    if (player != null) {
        BlockEntity t = this.getLevel().getBlockEntity(this);
        BlockEntityEnchantTable enchantTable;
        if (t instanceof BlockEntityEnchantTable) {
            enchantTable = (BlockEntityEnchantTable) t;
        } else {
            CompoundTag nbt = new CompoundTag().putList(new ListTag<>("Items")).putString("id", BlockEntity.ENCHANT_TABLE).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
            enchantTable = new BlockEntityEnchantTable(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
        }
        if (enchantTable.namedTag.contains("Lock") && enchantTable.namedTag.get("Lock") instanceof StringTag) {
            if (!enchantTable.namedTag.getString("Lock").equals(item.getCustomName())) {
                return true;
            }
        }
        player.addWindow(new EnchantInventory(this.getLocation()), Player.ENCHANT_WINDOW_ID);
    }
    return true;
}
Also used : StringTag(cn.nukkit.nbt.tag.StringTag) BlockEntityEnchantTable(cn.nukkit.blockentity.BlockEntityEnchantTable) EnchantInventory(cn.nukkit.inventory.EnchantInventory) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 28 with BlockEntity

use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.

the class Level method doChunkGarbageCollection.

public void doChunkGarbageCollection() {
    this.timings.doChunkGC.startTiming();
    // remove all invaild block entities.
    List<BlockEntity> toClose = new ArrayList<>();
    if (!blockEntities.isEmpty()) {
        Iterator<Map.Entry<Long, BlockEntity>> iter = blockEntities.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<Long, BlockEntity> entry = iter.next();
            BlockEntity aBlockEntity = entry.getValue();
            if (aBlockEntity != null) {
                if (!aBlockEntity.isValid()) {
                    iter.remove();
                    aBlockEntity.close();
                }
            } else {
                iter.remove();
            }
        }
    }
    for (Map.Entry<Long, ? extends FullChunk> entry : provider.getLoadedChunks().entrySet()) {
        long index = entry.getKey();
        if (!this.unloadQueue.containsKey(index)) {
            FullChunk chunk = entry.getValue();
            int X = chunk.getX();
            int Z = chunk.getZ();
            if (!this.isSpawnChunk(X, Z)) {
                this.unloadChunkRequest(X, Z, true);
            }
        }
    }
    this.provider.doGarbageCollection();
    this.timings.doChunkGC.stopTiming();
}
Also used : BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 29 with BlockEntity

use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.

the class Level method useBreakOn.

public Item useBreakOn(Vector3 vector, Item item, Player player, boolean createParticles) {
    if (player != null && player.getGamemode() > 1) {
        return null;
    }
    Block target = this.getBlock(vector);
    Item[] drops;
    if (item == null) {
        item = new ItemBlock(new BlockAir(), 0, 0);
    }
    if (player != null) {
        double breakTime = target.getBreakTime(item, player);
        if (player.isCreative() && breakTime > 0.15) {
            breakTime = 0.15;
        }
        if (player.hasEffect(Effect.SWIFTNESS)) {
            breakTime *= 1 - (0.2 * (player.getEffect(Effect.SWIFTNESS).getAmplifier() + 1));
        }
        if (player.hasEffect(Effect.MINING_FATIGUE)) {
            breakTime *= 1 - (0.3 * (player.getEffect(Effect.MINING_FATIGUE).getAmplifier() + 1));
        }
        Enchantment eff = item.getEnchantment(Enchantment.ID_EFFICIENCY);
        if (eff != null && eff.getLevel() > 0) {
            breakTime *= 1 - (0.3 * eff.getLevel());
        }
        breakTime -= 0.15;
        BlockBreakEvent ev = new BlockBreakEvent(player, target, item, player.isCreative(), (player.lastBreak + breakTime * 1000) > System.currentTimeMillis());
        double distance;
        if (player.isSurvival() && !target.isBreakable(item)) {
            ev.setCancelled();
        } else if (!player.isOp() && (distance = this.server.getSpawnRadius()) > -1) {
            Vector2 t = new Vector2(target.x, target.z);
            Vector2 s = new Vector2(this.getSpawnLocation().x, this.getSpawnLocation().z);
            if (!this.server.getOps().getAll().isEmpty() && t.distance(s) <= distance) {
                ev.setCancelled();
            }
        }
        this.server.getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            return null;
        }
        if (!ev.getInstaBreak() && ev.isFastBreak()) {
            return null;
        }
        player.lastBreak = System.currentTimeMillis();
        drops = ev.getDrops();
    } else if (!target.isBreakable(item)) {
        return null;
    } else {
        drops = target.getDrops(item);
    }
    Block above = this.getBlock(new Vector3(target.x, target.y + 1, target.z));
    if (above != null) {
        if (above.getId() == Item.FIRE) {
            this.setBlock(above, new BlockAir(), true);
        }
    }
    Tag tag = item.getNamedTagEntry("CanDestroy");
    if (tag instanceof ListTag) {
        boolean canBreak = false;
        for (Tag v : ((ListTag<Tag>) tag).getAll()) {
            if (v instanceof StringTag) {
                Item entry = Item.fromString(((StringTag) v).data);
                if (entry.getId() > 0 && entry.getBlock() != null && entry.getBlock().getId() == target.getId()) {
                    canBreak = true;
                    break;
                }
            }
        }
        if (!canBreak) {
            return null;
        }
    }
    if (createParticles) {
        Map<Integer, Player> players = this.getChunkPlayers((int) target.x >> 4, (int) target.z >> 4);
        this.addParticle(new DestroyBlockParticle(target.add(0.5), target), players.values());
        if (player != null) {
            players.remove(player.getLoaderId());
        }
    }
    target.onBreak(item);
    BlockEntity blockEntity = this.getBlockEntity(target);
    if (blockEntity != null) {
        if (blockEntity instanceof InventoryHolder) {
            if (blockEntity instanceof BlockEntityChest) {
                ((BlockEntityChest) blockEntity).unpair();
            }
            for (Item chestItem : ((InventoryHolder) blockEntity).getInventory().getContents().values()) {
                this.dropItem(target, chestItem);
            }
        }
        blockEntity.close();
        this.updateComparatorOutputLevel(target);
    }
    item.useOn(target);
    if (item.isTool() && item.getDamage() >= item.getMaxDurability()) {
        item = new ItemBlock(new BlockAir(), 0, 0);
    }
    if (this.gameRules.getBoolean(GameRule.DO_TILE_DROPS)) {
        int dropExp = target.getDropExp();
        if (player != null) {
            player.addExperience(dropExp);
            if (player.isSurvival()) {
                for (int ii = 1; ii <= dropExp; ii++) {
                    this.dropExpOrb(target, 1);
                }
            }
        }
        if (player == null || player.isSurvival()) {
            for (Item drop : drops) {
                if (drop.getCount() > 0) {
                    this.dropItem(vector.add(0.5, 0.5, 0.5), drop);
                }
            }
        }
    }
    return item;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Player(cn.nukkit.Player) DestroyBlockParticle(cn.nukkit.level.particle.DestroyBlockParticle) ItemBlock(cn.nukkit.item.ItemBlock) EntityItem(cn.nukkit.entity.item.EntityItem) Item(cn.nukkit.item.Item) BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block) BlockBreakEvent(cn.nukkit.event.block.BlockBreakEvent) Enchantment(cn.nukkit.item.enchantment.Enchantment) InventoryHolder(cn.nukkit.inventory.InventoryHolder) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 30 with BlockEntity

use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.

the class Chunk method toBinary.

@Override
public byte[] toBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.putInt("xPos", this.getX());
    nbt.putInt("zPos", this.getZ());
    ListTag<CompoundTag> sectionList = new ListTag<>("Sections");
    for (cn.nukkit.level.format.ChunkSection section : this.getSections()) {
        if (section instanceof EmptyChunkSection) {
            continue;
        }
        CompoundTag s = new CompoundTag(null);
        s.putByte("Y", (section.getY()));
        s.putByteArray("Blocks", section.getIdArray());
        s.putByteArray("Data", section.getDataArray());
        s.putByteArray("BlockLight", section.getLightArray());
        s.putByteArray("SkyLight", section.getSkyLightArray());
        sectionList.add(s);
    }
    nbt.putList(sectionList);
    nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
    int[] heightInts = new int[256];
    byte[] heightBytes = this.getHeightMapArray();
    for (int i = 0; i < heightInts.length; i++) {
        heightInts[i] = heightBytes[i] & 0xFF;
    }
    nbt.putIntArray("HeightMap", heightInts);
    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);
    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);
    Set<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this);
    if (entries != null) {
        ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks");
        long totalTime = this.provider.getLevel().getCurrentTick();
        for (BlockUpdateEntry entry : entries) {
            CompoundTag entryNBT = new CompoundTag().putString("i", entry.block.getSaveId()).putInt("x", entry.pos.getFloorX()).putInt("y", entry.pos.getFloorY()).putInt("z", entry.pos.getFloorZ()).putInt("t", (int) (entry.delay - totalTime)).putInt("p", entry.priority);
            tileTickTag.add(entryNBT);
        }
        nbt.putList(tileTickTag);
    }
    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }
    nbt.putByteArray("ExtraData", extraData.getBuffer());
    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);
    try {
        return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) IOException(java.io.IOException) EmptyChunkSection(cn.nukkit.level.format.generic.EmptyChunkSection) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

BlockEntity (cn.nukkit.blockentity.BlockEntity)32 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)13 Entity (cn.nukkit.entity.Entity)7 Player (cn.nukkit.Player)6 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)6 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)6 StringTag (cn.nukkit.nbt.tag.StringTag)6 IOException (java.io.IOException)6 Item (cn.nukkit.item.Item)5 FullChunk (cn.nukkit.level.format.FullChunk)5 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)4 ListTag (cn.nukkit.nbt.tag.ListTag)4 BinaryStream (cn.nukkit.utils.BinaryStream)4 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)3 ArrayList (java.util.ArrayList)3 BlockEntityComparator (cn.nukkit.blockentity.BlockEntityComparator)2 BlockEntityFlowerPot (cn.nukkit.blockentity.BlockEntityFlowerPot)2 Tag (cn.nukkit.nbt.tag.Tag)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 Long2ObjectMap (it.unimi.dsi.fastutil.longs.Long2ObjectMap)2