cn.nukkit.nbt.tag.CompoundTag Java Examples

The following examples show how to use cn.nukkit.nbt.tag.CompoundTag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlockUndyedShulkerBox.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    if (player != null) {
        BlockEntity t = this.getLevel().getBlockEntity(this);
        BlockEntityShulkerBox box;
        if (t instanceof BlockEntityShulkerBox) {
            box = (BlockEntityShulkerBox) t;
        } else {
            CompoundTag nbt = BlockEntity.getDefaultCompound(this, BlockEntity.SHULKER_BOX);
            box = (BlockEntityShulkerBox) BlockEntity.createBlockEntity(BlockEntity.SHULKER_BOX, this.getLevel().getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4), nbt);
            if (box == null) {
                return false;
            }
        }

        Block block = this.getSide(BlockFace.fromIndex(box.namedTag.getByte("facing")));
        if (!(block instanceof BlockAir) && !(block instanceof BlockLiquid) && !(block instanceof BlockFlowable)) {
            return true;
        }

        player.addWindow(box.getInventory());
    }

    return true;
}
 
Example #2
Source File: Item.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public Item clearCustomName() {
    if (!this.hasCompoundTag()) {
        return this;
    }

    CompoundTag tag = this.getNamedTag();

    if (tag.contains("display") && tag.get("display") instanceof CompoundTag) {
        tag.getCompound("display").remove("Name");
        if (tag.getCompound("display").isEmpty()) {
            tag.remove("display");
        }

        this.setNamedTag(tag);
    }

    return this;
}
 
Example #3
Source File: BlockEntityBrewingStand.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example #4
Source File: BlockRedstoneComparator.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (super.place(item, block, target, face, fx, fy, fz, player)) {
        CompoundTag nbt = new CompoundTag()
                .putList(new ListTag<>("Items"))
                .putString("id", BlockEntity.COMPARATOR)
                .putInt("x", (int) this.x)
                .putInt("y", (int) this.y)
                .putInt("z", (int) this.z);
        new BlockEntityComparator(this.level.getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);

        onUpdate(Level.BLOCK_UPDATE_REDSTONE);
        return true;
    }

    return false;
}
 
Example #5
Source File: BlockEntityChest.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void initBlockEntity() {
    this.inventory = new ChestInventory(this);

    if (!this.namedTag.contains("Items") || !(this.namedTag.get("Items") instanceof ListTag)) {
        this.namedTag.putList(new ListTag<CompoundTag>("Items"));
    }

    /* for (int i = 0; i < this.getSize(); i++) {
        this.inventory.setItem(i, this.getItem(i));
    } */

    ListTag<CompoundTag> list = (ListTag<CompoundTag>) this.namedTag.getList("Items");
    for (CompoundTag compound : list.getAll()) {
        Item item = NBTIO.getItemHelper(compound);
        this.inventory.slots.put(compound.getByte("Slot"), item);
    }

    super.initBlockEntity();
}
 
Example #6
Source File: BlockEntityBrewingStand.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public BlockEntityBrewingStand(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);
    inventory = new BrewingInventory(this);

    if (!namedTag.contains("Items") || !(namedTag.get("Items") instanceof ListTag)) {
        namedTag.putList(new ListTag<CompoundTag>("Items"));
    }

    for (int i = 0; i < getSize(); i++) {
        inventory.setItem(i, this.getItem(i));
    }

    if (!namedTag.contains("CookTime") || namedTag.getShort("CookTime") > MAX_BREW_TIME) {
        this.brewTime = MAX_BREW_TIME;
    } else {
        this.brewTime = namedTag.getShort("CookTime");
    }

    this.fuelAmount = namedTag.getShort("FuelAmount");
    this.fuelTotal = namedTag.getShort("FuelTotal");

    if (brewTime < MAX_BREW_TIME) {
        this.scheduleUpdate();
    }
}
 
Example #7
Source File: BlockEntityFurnace.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example #8
Source File: BlockEntityItemFrame.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public CompoundTag getSpawnCompound() {
    if (!this.namedTag.contains("Item")) {
        this.setItem(new ItemBlock(new BlockAir()), false);
    }
    CompoundTag NBTItem = namedTag.getCompound("Item").copy();
    NBTItem.setName("Item");
    boolean item = NBTItem.getShort("id") == Item.AIR;
    return new CompoundTag()
            .putString("id", BlockEntity.ITEM_FRAME)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z)
            .putCompound("Item", item ? NBTIO.putItemHelper(new ItemBlock(new BlockAir())) : NBTItem)
            .putByte("ItemRotation", item ? 0 : this.getItemRotation());
    // TODO: This crashes the client, why?
    // .putFloat("ItemDropChance", this.getItemDropChance());
}
 
Example #9
Source File: ItemBookWritten.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public Item writeBook(String author, String title, ListTag<CompoundTag> pages) {
    if (pages.size() > 50 || pages.size() <= 0) return this; //Minecraft does not support more than 50 pages
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();

    tag.putString("author", author);
    tag.putString("title", title);
    tag.putList(pages);

    tag.putInt("generation", GENERATION_ORIGINAL);
    tag.putString("xuid", "");

    return this.setNamedTag(tag);
}
 
Example #10
Source File: BlockShulkerBox.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    if (player != null) {
        Block top = up();
        if (!top.isTransparent()) {
            return true;
        }

        BlockEntity t = this.getLevel().getBlockEntity(this);
        BlockEntityShulkerBox shulkerBox;
        if (t instanceof BlockEntityShulkerBox) {
            shulkerBox = (BlockEntityShulkerBox) t;
        } else {
            CompoundTag nbt = new CompoundTag("")
                    .putString("id", BlockEntity.SHULKER_BOX)
                    .putInt("x", (int) this.x)
                    .putInt("y", (int) this.y)
                    .putInt("z", (int) this.z);
            shulkerBox = new BlockEntityShulkerBox(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
        }

        if (shulkerBox.namedTag.contains("Lock") && shulkerBox.namedTag.get("Lock") instanceof StringTag) {
            if (!shulkerBox.namedTag.getString("Lock").equals(item.getCustomName())) {
                return true;
            }
        }

        player.addWindow(shulkerBox.getInventory());
    }

    return true;
}
 
Example #11
Source File: Anvil.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/region").exists()) {
        new File(path + "/region").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("Data")
            .putCompound("GameRules", new CompoundTag())

            .putLong("DayTime", 0)
            .putInt("GameType", 0)
            .putString("generatorName", Generator.getGeneratorName(generator))
            .putString("generatorOptions", options.containsKey("preset") ? options.get("preset") : "")
            .putInt("generatorVersion", 1)
            .putBoolean("hardcore", false)
            .putBoolean("initialized", true)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putBoolean("raining", false)
            .putInt("rainTime", 0)
            .putLong("RandomSeed", seed)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putBoolean("thundering", false)
            .putInt("thunderTime", 0)
            .putInt("version", 19133)
            .putLong("Time", 0)
            .putLong("SizeOnDisk", 0);

    NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"), ByteOrder.BIG_ENDIAN);
}
 
Example #12
Source File: BlockEntityComparator.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public BlockEntityComparator(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);

    if (!nbt.contains("OutputSignal")) {
        nbt.putInt("OutputSignal", 0);
    }

    this.outputSignal = nbt.getInt("OutputSignal");
}
 
Example #13
Source File: BlockEntityFurnace.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public BlockEntityFurnace(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);
    this.inventory = new FurnaceInventory(this);

    if (!this.namedTag.contains("Items") || !(this.namedTag.get("Items") instanceof ListTag)) {
        this.namedTag.putList(new ListTag<CompoundTag>("Items"));
    }

    for (int i = 0; i < this.getSize(); i++) {
        this.inventory.setItem(i, this.getItem(i));
    }

    if (!this.namedTag.contains("BurnTime") || this.namedTag.getShort("BurnTime") < 0) {
        burnTime = 0;
    } else {
        burnTime = this.namedTag.getShort("BurnTime");
    }

    if (!this.namedTag.contains("CookTime") || this.namedTag.getShort("CookTime") < 0 || (this.namedTag.getShort("BurnTime") == 0 && this.namedTag.getShort("CookTime") > 0)) {
        cookTime = 0;
    } else {
        cookTime = this.namedTag.getShort("CookTime");
    }

    if (!this.namedTag.contains("MaxTime")) {
        maxTime = burnTime;
        burnDuration = 0;
    } else {
        maxTime = this.namedTag.getShort("MaxTime");
    }

    if (this.namedTag.contains("BurnTicks")) {
        burnDuration = this.namedTag.getShort("BurnTicks");
        this.namedTag.remove("BurnTicks");
    }

    if (burnTime > 0) {
        this.scheduleUpdate();
    }
}
 
Example #14
Source File: BlockEntityShulkerBox.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item getItem(int index) {
    int i = this.getSlotIndex(index);
    if (i < 0) {
        return new ItemBlock(new BlockAir(), 0, 0);
    } else {
        CompoundTag data = (CompoundTag) this.namedTag.getList("Items").get(i);
        return NBTIO.getItemHelper(data);
    }
}
 
Example #15
Source File: BlockEntityDropper.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item getItem(int index) {
    int i = this.getSlotIndex(index);
    if (i < 0) {
        return new ItemBlock(new BlockAir(), 0, 0);
    } else {
        CompoundTag data = (CompoundTag) this.namedTag.getList("Items").get(i);
        return NBTIO.getItemHelper(data);
    }
}
 
Example #16
Source File: ItemBookWritable.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Deletes an existing page with the given page ID.
 * @return boolean indicating success
 */
public boolean deletePage(int pageId) {
    Preconditions.checkArgument(pageId >= 0 && pageId < 50, "Page number " + pageId + " is out of range");
    if (this.hasCompoundTag()) {
        CompoundTag tag = this.getNamedTag();
        if (tag.contains("pages") && tag.get("pages") instanceof ListTag) {
            ListTag<CompoundTag> pages = tag.getList("pages", CompoundTag.class);
            if (pages.size() > pageId) {
                pages.remove(pageId);
                this.setCompoundTag(tag);
            }
        }
    }
    return true;
}
 
Example #17
Source File: Item.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public byte[] writeCompoundTag(CompoundTag tag) {
    try {
        tag.setName("");
        return NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #18
Source File: BlockEntitySign.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CompoundTag getSpawnCompound() {
    return new CompoundTag()
            .putString("id", BlockEntity.SIGN)
            .putString("Text", this.namedTag.getString("Text"))
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z);

}
 
Example #19
Source File: BlockEntityChest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected int getSlotIndex(int index) {
    ListTag<CompoundTag> list = this.namedTag.getList("Items", CompoundTag.class);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getByte("Slot") == index) {
            return i;
        }
    }

    return -1;
}
 
Example #20
Source File: Item.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public static CompoundTag parseCompoundTag(byte[] tag) {
    try {
        return NBTIO.read(tag, ByteOrder.LITTLE_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #21
Source File: ItemMinecart.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    if (Rail.isRailBlock(target)) {
        Rail.Orientation type = ((BlockRail) target).getOrientation();
        double adjacent = 0.0D;
        if (type.isAscending()) {
            adjacent = 0.5D;
        }
        EntityMinecartEmpty minecart = new EntityMinecartEmpty(
                level.getChunk(target.getFloorX() >> 4, target.getFloorZ() >> 4), new CompoundTag("")
                .putList(new ListTag<>("Pos")
                        .add(new DoubleTag("", target.getX() + 0.5))
                        .add(new DoubleTag("", target.getY() + 0.0625D + adjacent))
                        .add(new DoubleTag("", target.getZ() + 0.5)))
                .putList(new ListTag<>("Motion")
                        .add(new DoubleTag("", 0))
                        .add(new DoubleTag("", 0))
                        .add(new DoubleTag("", 0)))
                .putList(new ListTag<>("Rotation")
                        .add(new FloatTag("", 0))
                        .add(new FloatTag("", 0)))
        );
        minecart.spawnToAll();
        count -= 1;
        return true;
    }
    return false;
}
 
Example #22
Source File: ItemFirework.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void addExplosion(FireworkExplosion explosion) {
    List<DyeColor> colors = explosion.getColors();
    List<DyeColor> fades = explosion.getFades();

    if (colors.isEmpty()) {
        return;
    }
    byte[] clrs = new byte[colors.size()];
    for (int i = 0; i < clrs.length; i++) {
        clrs[i] = (byte) colors.get(i).getDyeData();
    }

    byte[] fds = new byte[fades.size()];
    for (int i = 0; i < fds.length; i++) {
        fds[i] = (byte) fades.get(i).getDyeData();
    }

    ListTag<CompoundTag> explosions = this.getNamedTag().getCompound("Fireworks").getList("Explosions", CompoundTag.class);
    CompoundTag tag = new CompoundTag()
            .putByteArray("FireworkColor", clrs)
            .putByteArray("FireworkFade", fds)
            .putBoolean("FireworkFlicker", explosion.flicker)
            .putBoolean("FireworkTrail", explosion.trail)
            .putByte("FireworkType", explosion.type.ordinal());

    explosions.add(tag);
}
 
Example #23
Source File: BlockSignPost.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (face != BlockFace.DOWN) {
        CompoundTag nbt = new CompoundTag()
                .putString("id", BlockEntity.SIGN)
                .putInt("x", (int) block.x)
                .putInt("y", (int) block.y)
                .putInt("z", (int) block.z)
                .putString("Text1", "")
                .putString("Text2", "")
                .putString("Text3", "")
                .putString("Text4", "");

        if (face == BlockFace.UP) {
            meta = (int) Math.floor(((player.yaw + 180) * 16 / 360) + 0.5) & 0x0f;
            getLevel().setBlock(block, new BlockSignPost(meta), true);
        } else {
            meta = face.getIndex();
            getLevel().setBlock(block, new BlockWallSign(meta), true);
        }

        if (player != null) {
            nbt.putString("Creator", player.getUniqueId().toString());
        }

        if (item.hasCustomBlockData()) {
            for (Tag aTag : item.getCustomBlockData().getAllTags()) {
                nbt.put(aTag.getName(), aTag);
            }
        }

        new BlockEntitySign(getLevel().getChunk((int) block.x >> 4, (int) block.z >> 4), nbt);

        return true;
    }

    return false;
}
 
Example #24
Source File: ItemBookWritten.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public Item writeBook(String author, String title, String[] pages) {
    ListTag<CompoundTag> pageList = new ListTag<>("pages");
    for (String page : pages) {
        pageList.add(new CompoundTag().putString("photoname", "").putString("text", page));
    }
    return writeBook(author, title, pageList);
}
 
Example #25
Source File: ChunkSection.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public ChunkSection(CompoundTag nbt) {
    this.y = nbt.getByte("Y");
    this.blocks = nbt.getByteArray("Blocks");
    this.data = nbt.getByteArray("Data");
    this.blockLight = nbt.getByteArray("BlockLight");
    this.skyLight = nbt.getByteArray("SkyLight");
}
 
Example #26
Source File: BlockBrewingStand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (!block.down().isTransparent()) {
        getLevel().setBlock(block, this, true, true);

        CompoundTag nbt = new CompoundTag()
                .putList(new ListTag<>("Items"))
                .putString("id", BlockEntity.BREWING_STAND)
                .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());
            }
        }

        new BlockEntityBrewingStand(getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);

        return true;
    }
    return false;
}
 
Example #27
Source File: ItemBookWritten.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public String[] getPages(){
    if (!this.isWritten) return new String[0];
    ListTag<CompoundTag> tag = (ListTag<CompoundTag>) this.getNamedTag().getList("pages");
    String[] pages = new String[tag.size()];
    int i = 0;
    for (CompoundTag pageCompound : tag.getAll()) {
        pages[i] = pageCompound.getString("text");
        i++;
    }
    return pages;
}
 
Example #28
Source File: BlockEntityBanner.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public BlockEntityBanner(FullChunk chunk, CompoundTag nbt) {
	super(chunk, nbt);

	if (!nbt.contains("Base")) {
		nbt.putInt("Base", 0);
	}
	if (!nbt.contains("Patterns")) {
		nbt.putList(new ListTag<>("Patterns"));
	}
}
 
Example #29
Source File: BlockEntityCauldron.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CompoundTag getSpawnCompound() {
    return new CompoundTag()
            .putString("id", BlockEntity.CAULDRON)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z)
            .putShort("PotionId", namedTag.getShort("PotionId"))
            .putByte("SplashPotion", namedTag.getByte("SplashPotion"));
}
 
Example #30
Source File: Item.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public CompoundTag getNamedTag() {
    if (!this.hasCompoundTag()) {
        return null;
    }

    if (this.cachedNBT == null) {
        this.cachedNBT = parseCompoundTag(this.tags);
    }

    if (this.cachedNBT != null) {
        this.cachedNBT.setName("");
    }

    return this.cachedNBT;
}