Search in sources :

Example 1 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class BlockRail method place.

// Information from http://minecraft.gamepedia.com/Rail
@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 == null || down.isTransparent()) {
        return false;
    }
    Map<BlockRail, BlockFace> railsAround = this.checkRailsAroundAffected();
    List<BlockRail> rails = new ArrayList<>(railsAround.keySet());
    List<BlockFace> faces = new ArrayList<>(railsAround.values());
    if (railsAround.size() == 1) {
        BlockRail other = rails.get(0);
        this.setDamage(this.connect(other, railsAround.get(other)).metadata());
    } else if (railsAround.size() == 4) {
        if (this.isAbstract()) {
            this.setDamage(this.connect(rails.get(faces.indexOf(SOUTH)), SOUTH, rails.get(faces.indexOf(EAST)), EAST).metadata());
        } else {
            this.setDamage(this.connect(rails.get(faces.indexOf(EAST)), EAST, rails.get(faces.indexOf(WEST)), WEST).metadata());
        }
    } else if (!railsAround.isEmpty()) {
        if (this.isAbstract()) {
            if (railsAround.size() == 2) {
                BlockRail rail1 = rails.get(0);
                BlockRail rail2 = rails.get(1);
                this.setDamage(this.connect(rail1, railsAround.get(rail1), rail2, railsAround.get(rail2)).metadata());
            } else {
                List<BlockFace> cd = Stream.of(CURVED_SOUTH_EAST, CURVED_NORTH_EAST, CURVED_SOUTH_WEST).filter(o -> o.connectingDirections().stream().allMatch(faces::contains)).findFirst().get().connectingDirections();
                BlockFace f1 = cd.get(0);
                BlockFace f2 = cd.get(1);
                this.setDamage(this.connect(rails.get(faces.indexOf(f1)), f1, rails.get(faces.indexOf(f2)), f2).metadata());
            }
        } else {
            BlockFace f = faces.stream().sorted((f1, f2) -> (f1.getIndex() < f2.getIndex()) ? 1 : ((x == y) ? 0 : -1)).findFirst().get();
            BlockFace fo = f.getOpposite();
            if (faces.contains(fo)) {
                // Opposite connectable
                this.setDamage(this.connect(rails.get(faces.indexOf(f)), f, rails.get(faces.indexOf(fo)), fo).metadata());
            } else {
                this.setDamage(this.connect(rails.get(faces.indexOf(f)), f).metadata());
            }
        }
    }
    this.level.setBlock(this, this, true, true);
    if (!isAbstract()) {
        level.scheduleUpdate(this, this, 0);
    }
    return true;
}
Also used : SOUTH(cn.nukkit.math.BlockFace.SOUTH) ItemTool(cn.nukkit.item.ItemTool) java.util(java.util) BlockFace(cn.nukkit.math.BlockFace) NORTH(cn.nukkit.math.BlockFace.NORTH) Level(cn.nukkit.level.Level) AxisAlignedBB(cn.nukkit.math.AxisAlignedBB) EAST(cn.nukkit.math.BlockFace.EAST) BlockColor(cn.nukkit.utils.BlockColor) Rail(cn.nukkit.utils.Rail) Collectors(java.util.stream.Collectors) WEST(cn.nukkit.math.BlockFace.WEST) Orientation(cn.nukkit.utils.Rail.Orientation) Stream(java.util.stream.Stream) Player(cn.nukkit.Player) Item(cn.nukkit.item.Item) BlockFace(cn.nukkit.math.BlockFace)

Example 2 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class BlockFlowerPot method getDrops.

@Override
public Item[] getDrops(Item item) {
    boolean dropInside = false;
    int insideID = 0;
    int insideMeta = 0;
    BlockEntity blockEntity = getLevel().getBlockEntity(this);
    if (blockEntity instanceof BlockEntityFlowerPot) {
        dropInside = true;
        insideID = blockEntity.namedTag.getShort("item");
        insideMeta = blockEntity.namedTag.getInt("data");
    }
    if (dropInside) {
        return new Item[] { new ItemFlowerPot(), Item.get(insideID, insideMeta, 1) };
    } else {
        return new Item[] { new ItemFlowerPot() };
    }
}
Also used : Item(cn.nukkit.item.Item) ItemFlowerPot(cn.nukkit.item.ItemFlowerPot) BlockEntity(cn.nukkit.blockentity.BlockEntity) BlockEntityFlowerPot(cn.nukkit.blockentity.BlockEntityFlowerPot)

Example 3 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class BlockMelon method getDrops.

@Override
public Item[] getDrops(Item item) {
    Random random = new Random();
    int count = 3 + random.nextInt(5);
    Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
    if (fortune != null && fortune.getLevel() >= 1) {
        count += random.nextInt(fortune.getLevel() + 1);
    }
    return new Item[] { new ItemMelon(0, Math.min(9, count)) };
}
Also used : Item(cn.nukkit.item.Item) Random(java.util.Random) Enchantment(cn.nukkit.item.enchantment.Enchantment) ItemMelon(cn.nukkit.item.ItemMelon)

Example 4 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class BlockItemFrame method onActivate.

@Override
public boolean onActivate(Item item, Player player) {
    BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
    BlockEntityItemFrame itemFrame = (BlockEntityItemFrame) blockEntity;
    if (itemFrame.getItem().getId() == Item.AIR) {
        // We can't use Item.get(item.getId(), item.getDamage(), 1) because
        // we need to keep the item's NBT tags
        // So we clone the item
        Item itemOnFrame = item.clone();
        // Change it to only one item (if we keep +1, visual glitches will happen)
        itemOnFrame.setCount(1);
        // And then we set it on the item frame
        itemFrame.setItem(itemOnFrame);
        // The item will be removed from the player's hand a few lines ahead
        this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_ADD_ITEM);
        if (player != null && player.isSurvival()) {
            int count = item.getCount();
            if (count-- <= 0) {
                player.getInventory().setItemInHand(new ItemBlock(new BlockAir(), 0, 0));
                return true;
            }
            item.setCount(count);
            player.getInventory().setItemInHand(item);
        }
    } else {
        int itemRot = itemFrame.getItemRotation();
        if (itemRot >= 7) {
            itemRot = 0;
        } else {
            itemRot++;
        }
        itemFrame.setItemRotation(itemRot);
        this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_ROTATE_ITEM);
    }
    return true;
}
Also used : Item(cn.nukkit.item.Item) BlockEntityItemFrame(cn.nukkit.blockentity.BlockEntityItemFrame) ItemBlock(cn.nukkit.item.ItemBlock) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 5 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class BlockOreLapis method getDrops.

@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_STONE) {
        int count = 4 + ThreadLocalRandom.current().nextInt(5);
        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 ItemDye(4, new Random().nextInt(4) + 4) };
    } else {
        return new Item[0];
    }
}
Also used : Item(cn.nukkit.item.Item) NukkitRandom(cn.nukkit.math.NukkitRandom) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Random(java.util.Random) ItemDye(cn.nukkit.item.ItemDye) Enchantment(cn.nukkit.item.enchantment.Enchantment)

Aggregations

Item (cn.nukkit.item.Item)64 Enchantment (cn.nukkit.item.enchantment.Enchantment)13 Player (cn.nukkit.Player)10 ItemBlock (cn.nukkit.item.ItemBlock)10 BlockAir (cn.nukkit.block.BlockAir)8 Random (java.util.Random)7 BlockEntity (cn.nukkit.blockentity.BlockEntity)6 Entity (cn.nukkit.entity.Entity)5 Block (cn.nukkit.block.Block)4 EntityItem (cn.nukkit.entity.item.EntityItem)4 EntityInventoryChangeEvent (cn.nukkit.event.entity.EntityInventoryChangeEvent)4 Inventory (cn.nukkit.inventory.Inventory)3 InventoryHolder (cn.nukkit.inventory.InventoryHolder)3 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)2 EntityArmorChangeEvent (cn.nukkit.event.entity.EntityArmorChangeEvent)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 EntityDamageEvent (cn.nukkit.event.entity.EntityDamageEvent)2 HopperInventory (cn.nukkit.inventory.HopperInventory)2 InventoryAction (cn.nukkit.inventory.transaction.action.InventoryAction)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2