cn.nukkit.item.Item Java Examples

The following examples show how to use cn.nukkit.item.Item. 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: BaseInventory.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void sendContents(Player... players) {
    InventoryContentPacket pk = new InventoryContentPacket();
    pk.slots = new Item[this.getSize()];
    for (int i = 0; i < this.getSize(); ++i) {
        pk.slots[i] = this.getItem(i);
    }

    for (Player player : players) {
        int id = player.getWindowId(this);
        if (id == -1 || !player.spawned) {
            this.close(player);
            continue;
        }
        pk.inventoryId = id;
        player.dataPacket(pk);
    }
}
 
Example #2
Source File: BlockOreDiamond.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_IRON) {
        int count = 1;
        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;

            if (i < 0) {
                i = 0;
            }

            count = i + 1;
        }

        return new Item[]{
                new ItemDiamond(0, count)
        };
    } else {
        return new Item[0];
    }
}
 
Example #3
Source File: BlockEntityBrewingStand.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 #4
Source File: BlockCactus.java    From Nukkit 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) {
    Block down = this.down();
    if (down.getId() == SAND || down.getId() == CACTUS) {
        Block block0 = north();
        Block block1 = south();
        Block block2 = west();
        Block block3 = east();
        if (block0.isTransparent() && block1.isTransparent() && block2.isTransparent() && block3.isTransparent()) {
            this.getLevel().setBlock(this, this, true);

            return true;
        }
    }
    return false;
}
 
Example #5
Source File: BlockOreRedstone.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_IRON) {
        int count = new Random().nextInt(2) + 4;

        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            count += new Random().nextInt(fortune.getLevel() + 1);
        }

        return new Item[]{
                new ItemRedstone(0, count)
        };
    } else {
        return new Item[0];
    }
}
 
Example #6
Source File: BlockJukebox.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
    if (blockEntity == null || !(blockEntity instanceof BlockEntityJukebox)) {
        blockEntity = this.createBlockEntity();
    }

    BlockEntityJukebox jukebox = (BlockEntityJukebox) blockEntity;
    if (jukebox.getRecordItem().getId() != 0) {
        jukebox.dropItem();
    } else if (item instanceof ItemRecord) {
        jukebox.setRecordItem(item);
        jukebox.play();
        player.getInventory().decreaseCount(player.getInventory().getHeldItemIndex());
    }

    return false;
}
 
Example #7
Source File: BlockConcretePowder.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block b, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    boolean concrete = false;

    for (int side = 1; side <= 5; side++) {
        Block block = this.getSide(BlockFace.fromIndex(side));
        if (block.getId() == Block.WATER || block.getId() == Block.STILL_WATER || block.getId() == Block.LAVA || block.getId() == Block.STILL_LAVA) {
            concrete = true;
            break;
        }
    }

    if (concrete) {
        this.level.setBlock(this, Block.get(Block.CONCRETE, this.getDamage()), true, true);
    } else {
        this.level.setBlock(this, this, true, true);
    }

    return true;
}
 
Example #8
Source File: BlockWallSign.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onUpdate(int type) {
    int[] faces = {
            3,
            2,
            5,
            4,
    };
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        if (this.getDamage() >= 2 && this.getDamage() <= 5) {
            if (this.getSide(BlockFace.fromIndex(faces[this.getDamage() - 2])).getId() == Item.AIR) {
                this.getLevel().useBreakOn(this);
            }
            return Level.BLOCK_UPDATE_NORMAL;
        }
    }
    return 0;
}
 
Example #9
Source File: CraftingManager.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private Item[][] cloneItemMap(Item[][] map) {
    Item[][] newMap = new Item[map.length][];
    for (int i = 0; i < newMap.length; i++) {
        Item[] old = map[i];
        Item[] n = new Item[old.length];

        System.arraycopy(old, 0, n, 0, n.length);
        newMap[i] = n;
    }

    for (int y = 0; y < newMap.length; y++) {
        Item[] row = newMap[y];
        for (int x = 0; x < row.length; x++) {
            Item item = newMap[y][x];
            newMap[y][x] = item.clone();
        }
    }
    return newMap;
}
 
Example #10
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 #11
Source File: CraftingTransaction.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void setPrimaryOutput(Item item) {
    if (primaryOutput == null) {
        primaryOutput = item.clone();
    } else if (!primaryOutput.equals(item)) {
        throw new RuntimeException("Primary result item has already been set and does not match the current item (expected " + primaryOutput + ", got " + item + ")");
    }
}
 
Example #12
Source File: PlayerInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onSlotChange(int index, Item before, boolean send) {
    EntityHuman holder = this.getHolder();
    if (holder instanceof Player && !((Player) holder).spawned) {
        return;
    }

    if (index >= this.getSize()) {
        this.sendArmorSlot(index, this.getViewers());
        this.sendArmorSlot(index, this.getHolder().getViewers().values());
    } else {
        super.onSlotChange(index, before, send);
    }
}
 
Example #13
Source File: BlockIronBars.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_WOODEN) {
        return new Item[]{
                toItem()
        };
    } else {
        return new Item[0];
    }
}
 
Example #14
Source File: BlockTerracottaStained.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_WOODEN) {
        return new Item[]{toItem()};
    } else {
        return new Item[0];
    }
}
 
Example #15
Source File: BlockNoteblock.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) {
    this.level.setBlock(block, this, true, true);
    if (this.level.isBlockPowered(this)) {
    	this.isPowered = true;
    } else {
    	this.isPowered = false;
    }
	return true;
}
 
Example #16
Source File: BlockGlowstone.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isSilkTouch()){
        return new Item[]{
                this.toItem()
        };
    } else {
        return new Item[]{
                new ItemGlowstoneDust(0, new NukkitRandom().nextRange(2, 4))
        };
    }
}
 
Example #17
Source File: BlockSkull.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    BlockEntity blockEntity = getLevel().getBlockEntity(this);
    int dropMeta = 0;
    if (blockEntity != null) dropMeta = blockEntity.namedTag.getByte("SkullType");
    return new Item[]{
            new ItemSkull(dropMeta)
    };
}
 
Example #18
Source File: BlockLever.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    this.level.getServer().getPluginManager().callEvent(new BlockRedstoneEvent(this, isPowerOn() ? 15 : 0, isPowerOn() ? 0 : 15));
    this.setDamage(this.getDamage() ^ 0x08);

    this.getLevel().setBlock(this, this, false, true);
    this.getLevel().addSound(this, Sound.RANDOM_CLICK); //TODO: correct pitch

    LeverOrientation orientation = LeverOrientation.byMetadata(this.isPowerOn() ? this.getDamage() ^ 0x08 : this.getDamage());
    BlockFace face = orientation.getFacing();
    //this.level.updateAroundRedstone(this, null);
    this.level.updateAroundRedstone(this.getLocation().getSide(face.getOpposite()), isPowerOn() ? face : null);
    return true;
}
 
Example #19
Source File: BinaryStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void putRecipeIngredient(Item ingredient) {
    if (ingredient == null || ingredient.getId() == 0) {
        this.putVarInt(0);
        return;
    }
    this.putVarInt(ingredient.getId());
    int damage;
    if (ingredient.hasMeta()) {
        damage = ingredient.getDamage();
    } else {
        damage = 0x7fff;
    }
    this.putVarInt(damage);
    this.putVarInt(ingredient.getCount());
}
 
Example #20
Source File: EntitySheep.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item[] getDrops() {
    if (this.lastDamageCause instanceof EntityDamageByEntityEvent) {
        return new Item[]{Item.get(((this.isOnFire()) ? Item.COOKED_MUTTON : Item.RAW_MUTTON)), Item.get(Item.WOOL, getColor(), 1)};
    }
    return new Item[0];
}
 
Example #21
Source File: BlockOreIron.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_STONE) {
        return new Item[]{
                Item.get(IRON_ORE)
        };
    } else {
        return new Item[0];
    }
}
 
Example #22
Source File: BlockMobSpawner.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 (item.getId() == Item.SPAWN_EGG) {
        BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
        if (blockEntity instanceof BlockEntityMobSpawner) {
            this.setDamage(item.getDamage());
            ((BlockEntityMobSpawner) blockEntity).setNetworkId(item.getDamage());
        }

        return true;
    }

    return false;
}
 
Example #23
Source File: ShapelessRecipe.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public ShapelessRecipe addIngredient(Item item) {
    if (this.ingredients.size() > 9) {
        throw new IllegalArgumentException("Shapeless recipes cannot have more than 9 ingredients");
    }

    Item it = item.clone();
    it.setCount(1);

    while (item.getCount() > 0) {
        this.ingredients.add(it.clone());
        item.setCount(item.getCount() - 1);
    }

    return this;
}
 
Example #24
Source File: BlockButton.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 (target.isTransparent()) {
        return false;
    }

    this.meta = face.getIndex();
    this.level.setBlock(block, this, true, true);
    return true;
}
 
Example #25
Source File: BlockRedstoneRepeaterUnpowered.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    this.meta += 4;
    if (this.meta > 15) this.meta = this.meta % 4;

    this.level.setBlock(this, this, true, false);
    return true;
}
 
Example #26
Source File: BlockFenceGate.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) {
        return false;
    }

    if (!this.toggle(player)) {
        return false;
    }

    this.getLevel().setBlock(this, this, true);
    this.getLevel().addSound(new DoorSound(this));

    return true;
}
 
Example #27
Source File: BaseInventory.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean clear(int index, boolean send) {
    if (this.slots.containsKey(index)) {
        Item item = new ItemBlock(new BlockAir(), null, 0);
        Item old = this.slots.get(index);
        InventoryHolder holder = this.getHolder();
        if (holder instanceof Entity) {
            EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent((Entity) holder, this, old, item, index);
            Server.getInstance().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                this.sendSlot(index, this.getViewers());
                return false;
            }
            item = ev.getNewItem();
        }

        if (item.getId() != Item.AIR) {
            this.slots.put(index, item.clone());
        } else {
            this.slots.remove(index);
        }

        this.onSlotChange(index, old, send);
    }

    return true;
}
 
Example #28
Source File: BaseInventory.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int firstEmpty(Item item) {
    for (int i = 0; i < this.size; ++i) {
        if (this.getItem(i).getId() == Item.AIR) {
            return i;
        }
    }

    return -1;
}
 
Example #29
Source File: PlayerBucketEvent.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public PlayerBucketEvent(Player who, Block blockClicked, BlockFace blockFace, Item bucket, Item itemInHand) {
    this.player = who;
    this.blockClicked = blockClicked;
    this.blockFace = blockFace;
    this.item = itemInHand;
    this.bucket = bucket;
}
 
Example #30
Source File: EnchantCommand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length < 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Player player = sender.getServer().getPlayer(args[0]);
    if (player == null) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
        return true;
    }
    int enchantId;
    int enchantLevel;
    try {
        enchantId = getIdByName(args[1]);
        enchantLevel = args.length == 3 ? Integer.parseInt(args[2]) : 1;
    } catch (NumberFormatException e) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Enchantment enchantment = Enchantment.getEnchantment(enchantId);
    if (enchantment == null) {
        sender.sendMessage(new TranslationContainer("commands.enchant.notFound", String.valueOf(enchantId)));
        return true;
    }
    enchantment.setLevel(enchantLevel);
    Item item = player.getInventory().getItemInHand();
    if (item.getId() <= 0) {
        sender.sendMessage(new TranslationContainer("commands.enchant.noItem"));
        return true;
    }
    item.addEnchantment(enchantment);
    player.getInventory().setItemInHand(item);
    Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.enchant.success"));
    return true;
}