Search in sources :

Example 1 with CompoundTag

use of com.github.steveice10.opennbt.tag.builtin.CompoundTag in project DragonProxy by DragonetMC.

the class ItemList method tryToRemove.

// public
public boolean tryToRemove(ItemStack item) {
    ArrayList<ItemStack> original = this.cloneList();
    if (item == null || item.getId() == 0) {
        return true;
    }
    int toRemove = item.getAmount();
    for (int i = 0; i < items.size(); i++) {
        if (toRemove == 0) {
            break;
        }
        if (items.get(i) == null) {
            continue;
        }
        int typeID = items.get(i).getId();
        int damage = items.get(i).getData();
        int amount = items.get(i).getAmount();
        CompoundTag nbt = items.get(i).getNBT();
        if (typeID == item.getId() && damage == item.getData()) {
            // We found the item
            if (amount > toRemove) {
                // SUCCESS
                items.set(i, new ItemStack(typeID, amount - toRemove, damage, nbt));
                return true;
            } else {
                items.set(i, null);
                toRemove -= amount;
            }
        }
    }
    if (toRemove <= 0) {
        return true;
    } else {
        this.items = original;
        return false;
    }
}
Also used : ItemStack(com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 2 with CompoundTag

use of com.github.steveice10.opennbt.tag.builtin.CompoundTag in project DragonProxy by DragonetMC.

the class ItemBlockTranslator method newTileTag.

public static CompoundTag newTileTag(String id, int x, int y, int z) {
    CompoundTag t = new CompoundTag(null);
    t.put(new StringTag("id", id));
    t.put(new IntTag("x", x));
    t.put(new IntTag("y", y));
    t.put(new IntTag("z", z));
    return t;
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag)

Example 3 with CompoundTag

use of com.github.steveice10.opennbt.tag.builtin.CompoundTag in project DragonProxy by DragonetMC.

the class ChunkCache method translateChunk.

public final ChunkData translateChunk(int columnX, int columnZ) {
    ChunkPos columnPos = new ChunkPos(columnX, columnZ);
    if (chunkCache.containsKey(columnPos)) {
        Column column = chunkCache.get(columnPos);
        ChunkData chunk = new ChunkData();
        chunk.sections = new Section[16];
        for (int i = 0; i < 16; i++) chunk.sections[i] = new Section();
        // Blocks
        for (int y = 0; y < 256; y++) {
            int cy = y >> 4;
            Chunk c = null;
            try {
                c = column.getChunks()[cy];
            } catch (Exception ex) {
                DragonProxy.getInstance().getLogger().info("Chunk " + columnX + ", " + cy + ", " + columnZ + " not exist !");
            }
            if (c == null || c.isEmpty())
                continue;
            BlockStorage blocks = c.getBlocks();
            for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) {
                BlockState block = blocks.get(x, y & 0xF, z);
                ItemEntry entry = ItemBlockTranslator.translateToPE(block.getId(), block.getData());
                Section section = chunk.sections[cy];
                // Block id
                section.blockIds[index(x, y, z)] = (byte) (entry.getId() & 0xFF);
                // Data value
                int i = dataIndex(x, y, z);
                byte data = section.blockMetas[i];
                int newValue = entry.getPEDamage().byteValue();
                if ((y & 1) == 0)
                    data = (byte) ((data & 0xf0) | (newValue & 0x0f));
                else
                    data = (byte) (((newValue & 0x0f) << 4) | (data & 0x0f));
                section.blockMetas[i] = data;
            }
        }
        // Blocks entities
        try {
            List<CompoundTag> blockEntities = new ArrayList<>();
            for (int i = 0; i < column.getTileEntities().length; i++) {
                CompoundTag peTag = ItemBlockTranslator.translateBlockEntityToPE(column.getTileEntities()[i]);
                if (// filter non handled blocks entities
                peTag != null)
                    blockEntities.add(peTag);
            // else // debug
            // DragonProxy.getInstance().getLogger().debug("NBT null for " + pc.getTileEntities()[i].toString());
            }
            chunk.blockEntities = NBTIO.write(blockEntities, ByteOrder.LITTLE_ENDIAN, true);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        chunk.encode();
        return chunk;
    }
    // System.out.println("Chunk " + columnX + ", " + columnZ + " not in cache !!!!!!!!!!!!!");
    return null;
}
Also used : ChunkData(org.dragonet.protocol.type.chunk.ChunkData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Chunk(com.github.steveice10.mc.protocol.data.game.chunk.Chunk) Section(org.dragonet.protocol.type.chunk.Section) IOException(java.io.IOException) BlockStorage(com.github.steveice10.mc.protocol.data.game.chunk.BlockStorage) BlockState(com.github.steveice10.mc.protocol.data.game.world.block.BlockState) Column(com.github.steveice10.mc.protocol.data.game.chunk.Column) ChunkPos(org.dragonet.common.maths.ChunkPos) ItemEntry(org.dragonet.common.data.itemsblocks.ItemEntry) CompoundTag(org.dragonet.common.data.nbt.tag.CompoundTag)

Example 4 with CompoundTag

use of com.github.steveice10.opennbt.tag.builtin.CompoundTag in project DragonProxy by DragonetMC.

the class ItemBlockTranslator method translateRawNBT.

@SuppressWarnings("unchecked")
public static org.dragonet.common.data.nbt.tag.CompoundTag translateRawNBT(int id, Tag pcTag, org.dragonet.common.data.nbt.tag.CompoundTag target) {
    if (pcTag != null) {
        String name = pcTag.getName() != null ? pcTag.getName() : "";
        if (target == null)
            target = new org.dragonet.common.data.nbt.tag.CompoundTag(name);
        switch(pcTag.getClass().getSimpleName()) {
            case "ByteArrayTag":
                target.putByteArray(name, (byte[]) pcTag.getValue());
                break;
            case "ByteTag":
                target.putByte(name, (byte) pcTag.getValue());
                break;
            case "DoubleTag":
                target.putDouble(name, (double) pcTag.getValue());
                break;
            case "FloatTag":
                target.putFloat(name, (float) pcTag.getValue());
                break;
            case "IntArrayTag":
                target.putIntArray(name, (int[]) pcTag.getValue());
                break;
            case "IntTag":
                target.putInt(name, (int) pcTag.getValue());
                break;
            case "LongTag":
                target.putLong(name, (long) pcTag.getValue());
                break;
            case "ShortTag":
                target.putShort(name, (short) pcTag.getValue());
                break;
            case "StringTag":
                target.putString(name, (String) pcTag.getValue());
                break;
            case "CompoundTag":
                for (String subName : ((CompoundTag) pcTag).getValue().keySet()) translateRawNBT(0, ((CompoundTag) pcTag).getValue().get(subName), target);
                break;
            case "ListTag":
                ListTag listTag = new ListTag();
                for (Tag subTag : (List<Tag>) pcTag.getValue()) listTag.add(translateRawNBT(0, subTag, new org.dragonet.common.data.nbt.tag.CompoundTag()));
                target.putList(listTag);
                break;
            default:
                System.out.println("TAG not implemented : " + pcTag.getClass().getSimpleName());
                break;
        }
    }
    return target;
}
Also used : List(java.util.List) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ListTag(org.dragonet.common.data.nbt.tag.ListTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(org.dragonet.common.data.nbt.tag.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Aggregations

CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)3 IntTag (com.github.steveice10.opennbt.tag.builtin.IntTag)2 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)2 BlockStorage (com.github.steveice10.mc.protocol.data.game.chunk.BlockStorage)1 Chunk (com.github.steveice10.mc.protocol.data.game.chunk.Chunk)1 Column (com.github.steveice10.mc.protocol.data.game.chunk.Column)1 ItemStack (com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack)1 BlockState (com.github.steveice10.mc.protocol.data.game.world.block.BlockState)1 Tag (com.github.steveice10.opennbt.tag.builtin.Tag)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ItemEntry (org.dragonet.common.data.itemsblocks.ItemEntry)1 CompoundTag (org.dragonet.common.data.nbt.tag.CompoundTag)1 ListTag (org.dragonet.common.data.nbt.tag.ListTag)1 ChunkPos (org.dragonet.common.maths.ChunkPos)1 ChunkData (org.dragonet.protocol.type.chunk.ChunkData)1 Section (org.dragonet.protocol.type.chunk.Section)1