Java Code Examples for cn.nukkit.Player#isSurvival()

The following examples show how to use cn.nukkit.Player#isSurvival() . 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: ItemBucket.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onUse(Player player, int ticksUsed) {
    PlayerItemConsumeEvent consumeEvent = new PlayerItemConsumeEvent(player, this);

    player.getServer().getPluginManager().callEvent(consumeEvent);
    if (consumeEvent.isCancelled()) {
        player.getInventory().sendContents(player);
        return false;
    }

    if (player.isSurvival()) {
        this.count--;
        player.getInventory().setItemInHand(this);
        player.getInventory().addItem(new ItemBucket());
    }

    player.removeAllEffects();
    return true;
}
 
Example 2
Source File: ItemEdible.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onUse(Player player, int ticksUsed) {
    PlayerItemConsumeEvent consumeEvent = new PlayerItemConsumeEvent(player, this);

    player.getServer().getPluginManager().callEvent(consumeEvent);
    if (consumeEvent.isCancelled()) {
        player.getInventory().sendContents(player);
        return false;
    }

    Food food = Food.getByRelative(this);
    if (player.isSurvival() && food != null && food.eatenBy(player)) {
        --this.count;
        player.getInventory().setItemInHand(this);
    }
    return true;
}
 
Example 3
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 = (EntityMinecartEmpty) Entity.createEntity("MinecartRideable",
                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)))
        );

        if(minecart == null) {
            return false;
        }

        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }

        minecart.spawnToAll();
        return true;
    }
    return false;
}
 
Example 4
Source File: BlockCauldron.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected void replaceBucket(Item oldBucket, Player player, Item newBucket) {
    if (player.isSurvival() || player.isAdventure()) {
        if (oldBucket.getCount() == 1) {
            player.getInventory().setItemInHand(newBucket);
        } else {
            oldBucket.setCount(oldBucket.getCount() - 1);
            if (player.getInventory().canAddItem(newBucket)) {
                player.getInventory().addItem(newBucket);
            } else {
                player.getLevel().dropItem(player.add(0, 1.3, 0), newBucket, player.getDirectionVector().multiply(0.4));
            }
        }
    }
}
 
Example 5
Source File: ItemMinecartChest.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;
        }
        EntityMinecartChest minecart = (EntityMinecartChest) Entity.createEntity("MinecartChest",
                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)))
        );

        if(minecart == null) {
            return false;
        }

        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }

        minecart.spawnToAll();
        return true;
    }
    return false;
}
 
Example 6
Source File: ItemEndCrystal.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 (!(target instanceof BlockBedrock) && !(target instanceof BlockObsidian)) return false;
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);

    if (chunk == null) {
        return false;
    }

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", block.getX() + 0.5))
                    .add(new DoubleTag("", block.getY()))
                    .add(new DoubleTag("", block.getZ() + 0.5)))
            .putList(new ListTag<DoubleTag>("Motion")
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0)))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", new Random().nextFloat() * 360))
                    .add(new FloatTag("", 0)));

    if (this.hasCustomName()) {
        nbt.putString("CustomName", this.getCustomName());
    }

    Entity entity = Entity.createEntity("EndCrystal", chunk, nbt);

    if (entity != null) {
        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();
        return true;
    }
    return false;
}
 
Example 7
Source File: ItemBoat.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 (face != BlockFace.UP) return false;
    EntityBoat boat = (EntityBoat) Entity.createEntity("Boat",
            level.getChunk(block.getFloorX() >> 4, block.getFloorZ() >> 4), new CompoundTag("")
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", block.getX() + 0.5))
                    .add(new DoubleTag("", block.getY() - (target instanceof BlockWater ? 0.0625 : 0)))
                    .add(new DoubleTag("", block.getZ() + 0.5)))
            .putList(new ListTag<DoubleTag>("Motion")
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0)))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", (float) ((player.yaw + 90f) % 360)))
                    .add(new FloatTag("", 0)))
            .putByte("woodID", this.getDamage())
    );

    if (boat == null) {
        return false;
    }

    if (player.isSurvival()) {
        Item item = player.getInventory().getItemInHand();
        item.setCount(item.getCount() - 1);
        player.getInventory().setItemInHand(item);
    }

    boat.spawnToAll();
    return true;
}
 
Example 8
Source File: ItemBoat.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 (face != BlockFace.UP) return false;
    EntityBoat boat = new EntityBoat(
            level.getChunk(block.getFloorX() >> 4, block.getFloorZ() >> 4), new CompoundTag("")
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", block.getX() + 0.5))
                    .add(new DoubleTag("", block.getY() - 0.0625))
                    .add(new DoubleTag("", block.getZ() + 0.5)))
            .putList(new ListTag<DoubleTag>("Motion")
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0)))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", (float) ((player.yaw + 90f) % 360)))
                    .add(new FloatTag("", 0)))
            .putByte("woodID", this.getDamage())
    );

    if (player.isSurvival()) {
        Item item = player.getInventory().getItemInHand();
        item.setCount(item.getCount() - 1);
        player.getInventory().setItemInHand(item);
    }

    boat.spawnToAll();
    return true;
}
 
Example 9
Source File: ItemMinecartHopper.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;
        }
        EntityMinecartHopper minecart = (EntityMinecartHopper) Entity.createEntity("MinecartHopper",
                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)))
        );

        if(minecart == null) {
            return false;
        }

        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }

        minecart.spawnToAll();
        return true;
    }
    return false;
}
 
Example 10
Source File: BlockFlowerPot.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    BlockEntity blockEntity = getLevel().getBlockEntity(this);
    if (!(blockEntity instanceof BlockEntityFlowerPot)) return false;
    if (blockEntity.namedTag.getShort("item") != 0 || blockEntity.namedTag.getInt("mData") != 0) return false;
    int itemID;
    int itemMeta;
    if (!canPlaceIntoFlowerPot(item.getId())) {
        if (!canPlaceIntoFlowerPot(item.getBlock().getId())) {
            return true;
        }
        itemID = item.getBlock().getId();
        itemMeta = item.getDamage();
    } else {
        itemID = item.getId();
        itemMeta = item.getDamage();
    }
    blockEntity.namedTag.putShort("item", itemID);
    blockEntity.namedTag.putInt("data", itemMeta);

    this.setDamage(1);
    this.getLevel().setBlock(this, this, true);
    ((BlockEntityFlowerPot) blockEntity).spawnToAll();

    if (player.isSurvival()) {
        item.setCount(item.getCount() - 1);
        player.getInventory().setItemInHand(item.getCount() > 0 ? item : Item.get(Item.AIR));
    }
    return true;
}
 
Example 11
Source File: ItemMinecartTNT.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;
        }
        EntityMinecartTNT minecart = (EntityMinecartTNT) Entity.createEntity("MinecartTnt",
                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)))
        );

        if(minecart == null) {
            return false;
        }

        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }

        minecart.spawnToAll();
        return true;
    }
    return false;
}
 
Example 12
Source File: ItemSpawnEgg.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) {
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);

    if (chunk == null) {
        return false;
    }

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", block.getX() + 0.5))
                    .add(new DoubleTag("", block.getY()))
                    .add(new DoubleTag("", block.getZ() + 0.5)))
            .putList(new ListTag<DoubleTag>("Motion")
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0)))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", new Random().nextFloat() * 360))
                    .add(new FloatTag("", 0)));

    if (this.hasCustomName()) {
        nbt.putString("CustomName", this.getCustomName());
    }

    Entity entity = Entity.createEntity(this.meta, chunk, nbt);

    if (entity != null) {
        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();
        return true;
    }

    return false;
}
 
Example 13
Source File: BlockItemFrame.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@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
        Item itemOnFrame = item.clone(); // So we clone the item
        itemOnFrame.setCount(1); // Change it to only one item (if we keep +1, visual glitches will happen)
        itemFrame.setItem(itemOnFrame); // And then we set it on the item frame
        // The item will be removed from the player's hand a few lines ahead
        this.getLevel().addSound(new ItemFrameItemAddedSound(this));
        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(new ItemFrameItemRotated(this));
    }
    return true;
}
 
Example 14
Source File: BlockFlowerPot.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    BlockEntity blockEntity = getLevel().getBlockEntity(this);
    if (!(blockEntity instanceof BlockEntityFlowerPot)) return false;
    if (blockEntity.namedTag.getShort("item") != 0 || blockEntity.namedTag.getInt("mData") != 0) return false;
    int itemID;
    int itemMeta;
    if (!canPlaceIntoFlowerPot(item.getId())) {
        if (!canPlaceIntoFlowerPot(item.getBlock().getId())) {
            return true;
        }
        itemID = item.getBlock().getId();
        itemMeta = item.getDamage();
    } else {
        itemID = item.getId();
        itemMeta = item.getDamage();
    }
    blockEntity.namedTag.putShort("item", itemID);
    blockEntity.namedTag.putInt("data", itemMeta);

    this.meta = 1;
    this.getLevel().setBlock(this, this, true);
    ((BlockEntityFlowerPot) blockEntity).spawnToAll();

    if (player.isSurvival()) {
        item.setCount(item.getCount() - 1);
        player.getInventory().setItemInHand(item.getCount() > 0 ? item : Item.get(Item.AIR));
    }
    return true;
}
 
Example 15
Source File: ItemBoat.java    From Jupiter 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 (face != BlockFace.UP) return false;
    EntityBoat boat = new EntityBoat(
            level.getChunk(block.getFloorX() >> 4, block.getFloorZ() >> 4), new CompoundTag("")
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", block.getX() + 0.5))
                    .add(new DoubleTag("", block.getY() - 0.0625))
                    .add(new DoubleTag("", block.getZ() + 0.5)))
            .putList(new ListTag<DoubleTag>("Motion")
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0)))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", (float) ((player.yaw + 90f) % 360)))
                    .add(new FloatTag("", 0)))
            .putByte("woodID", this.getDamage())
    );

    if (player.isSurvival()) {
        Item item = player.getInventory().getItemInHand();
        item.setCount(item.getCount() - 1);
        player.getInventory().setItemInHand(item);
    }

    boat.spawnToAll();
    return true;
}
 
Example 16
Source File: BlockItemFrame.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@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
        Item itemOnFrame = item.clone(); // So we clone the item
        itemOnFrame.setCount(1); // Change it to only one item (if we keep +1, visual glitches will happen)
        itemFrame.setItem(itemOnFrame); // And then we set it on the item frame
        // 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;
}
 
Example 17
Source File: EntityArrow.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCollideWithPlayer(Player player) {
    if(!this.hadCollision) {
        return;
    }

    Item item = new Item(Item.ARROW, 0, 1);
    PlayerInventory playerInventory = player.getInventory();

    if(player.isSurvival() && playerInventory.canAddItem(item)) {
        return;
    }

    InventoryPickupArrowEvent ev;
    this.server.getPluginManager().callEvent(ev = new InventoryPickupArrowEvent(playerInventory, this));
    if(ev.isCancelled()) {
        return;
    }

    TakeItemEntityPacket pk = new TakeItemEntityPacket();
    pk.entityRuntimeId = player.getId();
    pk.target = this.getId();
    Server.broadcastPacket(this.getViewers().values(), pk);

    playerInventory.addItem(item);
    this.kill();
}
 
Example 18
Source File: ItemPainting.java    From Jupiter with GNU General Public License v3.0 4 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) {
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);

    if (chunk == null) {
        return false;
    }

    if (!target.isTransparent() && face.getIndex() > 1 && !block.isSolid()) {
        int[] direction = {2, 0, 1, 3};
        int[] right = {4, 5, 3, 2};

        List<EntityPainting.Motive> validMotives = new ArrayList<>();
        for (EntityPainting.Motive motive : EntityPainting.motives) {
            boolean valid = true;
            for (int x = 0; x < motive.width && valid; x++) {
                for (int z = 0; z < motive.height && valid; z++) {
                    if (target.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isTransparent() ||
                            target.up(z).isTransparent() ||
                            block.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isSolid() ||
                            block.up(z).isSolid()) {
                        valid = false;
                    }
                }
            }

            if (valid) {
                validMotives.add(motive);
            }
        }

        CompoundTag nbt = new CompoundTag()
                .putByte("Direction", direction[face.getIndex() - 2])
                .putString("Motive", validMotives.get(ThreadLocalRandom.current().nextInt(validMotives.size())).title)
                .putList(new ListTag<DoubleTag>("Pos")
                        .add(new DoubleTag("0", target.x))
                        .add(new DoubleTag("1", target.y))
                        .add(new DoubleTag("2", target.z)))
                .putList(new ListTag<DoubleTag>("Motion")
                        .add(new DoubleTag("0", 0))
                        .add(new DoubleTag("1", 0))
                        .add(new DoubleTag("2", 0)))
                .putList(new ListTag<FloatTag>("Rotation")
                        .add(new FloatTag("0", direction[face.getIndex() - 2] * 90))
                        .add(new FloatTag("1", 0)));

        EntityPainting entity = new EntityPainting(chunk, nbt);

        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();

        return true;
    }

    return false;
}
 
Example 19
Source File: ItemBow.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public boolean onReleaseUsing(Player player) {
    Item itemArrow = Item.get(Item.ARROW, 0, 1);

    if (player.isSurvival() && !player.getInventory().contains(itemArrow)) {
        player.getInventory().sendContents(player);
        return false;
    }

    double damage = 2;
    boolean flame = false;

    if (this.hasEnchantments()) {
        Enchantment bowDamage = this.getEnchantment(Enchantment.ID_BOW_POWER);

        if (bowDamage != null && bowDamage.getLevel() > 0) {
            damage += 0.25 * (bowDamage.getLevel() + 1);
        }

        Enchantment flameEnchant = this.getEnchantment(Enchantment.ID_BOW_FLAME);
        flame = flameEnchant != null && flameEnchant.getLevel() > 0;
    }

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", player.x))
                    .add(new DoubleTag("", player.y + player.getEyeHeight()))
                    .add(new DoubleTag("", player.z)))
            .putList(new ListTag<DoubleTag>("Motion")
                    .add(new DoubleTag("", -Math.sin(player.yaw / 180 * Math.PI) * Math.cos(player.pitch / 180 * Math.PI)))
                    .add(new DoubleTag("", -Math.sin(player.pitch / 180 * Math.PI)))
                    .add(new DoubleTag("", Math.cos(player.yaw / 180 * Math.PI) * Math.cos(player.pitch / 180 * Math.PI))))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", (player.yaw > 180 ? 360 : 0) - (float) player.yaw))
                    .add(new FloatTag("", (float) -player.pitch)))
            .putShort("Fire", player.isOnFire() || flame ? 45 * 60 : 0)
            .putDouble("damage", damage);

    int diff = (Server.getInstance().getTick() - player.getStartActionTick());
    double p = (double) diff / 20;

    double f = Math.min((p * p + p * 2) / 3, 1) * 2;
    EntityShootBowEvent entityShootBowEvent = new EntityShootBowEvent(player, this, new EntityArrow(player.chunk, nbt, player, f == 2), f);

    if (f < 0.1 || diff < 5) {
        entityShootBowEvent.setCancelled();
    }

    Server.getInstance().getPluginManager().callEvent(entityShootBowEvent);
    if (entityShootBowEvent.isCancelled()) {
        entityShootBowEvent.getProjectile().kill();
        player.getInventory().sendContents(player);
    } else {
        entityShootBowEvent.getProjectile().setMotion(entityShootBowEvent.getProjectile().getMotion().multiply(entityShootBowEvent.getForce()));
        if (player.isSurvival()) {
            Enchantment infinity;

            if (!this.hasEnchantments() || (infinity = this.getEnchantment(Enchantment.ID_BOW_INFINITY)) == null || infinity.getLevel() <= 0)
                player.getInventory().removeItem(itemArrow);
            if (!this.isUnbreakable()) {
                Enchantment durability = this.getEnchantment(Enchantment.ID_DURABILITY);
                if (!(durability != null && durability.getLevel() > 0 && (100 / (durability.getLevel() + 1)) <= new Random().nextInt(100))) {
                    this.setDamage(this.getDamage() + 1);
                    if (this.getDamage() >= 385) {
                        player.getInventory().setItemInHand(new ItemBlock(new BlockAir(), 0, 0));
                    } else {
                        player.getInventory().setItemInHand(this);
                    }
                }
            }
        }
        if (entityShootBowEvent.getProjectile() instanceof EntityProjectile) {
            ProjectileLaunchEvent projectev = new ProjectileLaunchEvent(entityShootBowEvent.getProjectile());
            Server.getInstance().getPluginManager().callEvent(projectev);
            if (projectev.isCancelled()) {
                entityShootBowEvent.getProjectile().kill();
            } else {
                entityShootBowEvent.getProjectile().spawnToAll();
                player.level.addSound(new LaunchSound(player), player.getViewers().values());
            }
        } else {
            entityShootBowEvent.getProjectile().spawnToAll();
        }
    }

    return true;
}
 
Example 20
Source File: ItemPainting.java    From Nukkit with GNU General Public License v3.0 4 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) {
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);

    if (chunk == null) {
        return false;
    }

    if (!target.isTransparent() && face.getIndex() > 1 && !block.isSolid()) {
        int[] direction = {2, 0, 1, 3};
        int[] right = {4, 5, 3, 2};

        List<EntityPainting.Motive> validMotives = new ArrayList<>();
        for (EntityPainting.Motive motive : EntityPainting.motives) {
            boolean valid = true;
            for (int x = 0; x < motive.width && valid; x++) {
                for (int z = 0; z < motive.height && valid; z++) {
                    if (target.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isTransparent() ||
                            target.up(z).isTransparent() ||
                            block.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isSolid() ||
                            block.up(z).isSolid()) {
                        valid = false;
                    }
                }
            }

            if (valid) {
                validMotives.add(motive);
            }
        }

        CompoundTag nbt = new CompoundTag()
                .putByte("Direction", direction[face.getIndex() - 2])
                .putString("Motive", validMotives.get(ThreadLocalRandom.current().nextInt(validMotives.size())).title)
                .putList(new ListTag<DoubleTag>("Pos")
                        .add(new DoubleTag("0", target.x))
                        .add(new DoubleTag("1", target.y))
                        .add(new DoubleTag("2", target.z)))
                .putList(new ListTag<DoubleTag>("Motion")
                        .add(new DoubleTag("0", 0))
                        .add(new DoubleTag("1", 0))
                        .add(new DoubleTag("2", 0)))
                .putList(new ListTag<FloatTag>("Rotation")
                        .add(new FloatTag("0", direction[face.getIndex() - 2] * 90))
                        .add(new FloatTag("1", 0)));

        EntityPainting entity = new EntityPainting(chunk, nbt);

        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();

        return true;
    }

    return false;
}