Java Code Examples for cn.nukkit.entity.Entity#createEntity()

The following examples show how to use cn.nukkit.entity.Entity#createEntity() . 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: ProjectileDispenseBehavior.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void dispense(BlockDispenser source, Item item) {
    Position dispensePos = Position.fromObject(source.getDispensePosition(), source.getLevel());
    CompoundTag nbt = Entity.getDefaultNBT(dispensePos);
    this.correctNBT(nbt);

    BlockFace face = source.getFacing();

    Entity projectile = Entity.createEntity(getEntityType(), dispensePos.getLevel().getChunk(dispensePos.getFloorX(), dispensePos.getFloorZ()), nbt);
    if (projectile == null) {
        return;
    }

    projectile.setMotion(new Vector3(face.getXOffset(), face.getYOffset() + 0.1f, face.getZOffset()).multiply(6));
    projectile.spawnToAll();
}
 
Example 2
Source File: ItemFirework.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private void spawnFirework(Level level, Vector3 pos) {
    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", pos.x + 0.5))
                    .add(new DoubleTag("", pos.y + 0.5))
                    .add(new DoubleTag("", pos.z + 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("", 0))
                    .add(new FloatTag("", 0)))
            .putCompound("FireworkItem", NBTIO.putItemHelper(this));

    EntityFirework entity = (EntityFirework) Entity.createEntity("Firework", level.getChunk(pos.getFloorX() >> 4, pos.getFloorZ() >> 4), nbt);
    if (entity != null) {
        entity.spawnToAll();
    }
}
 
Example 3
Source File: EntityThrownTrident.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public Entity create(Object type, Position source, Object... args) {
    FullChunk chunk = source.getLevel().getChunk((int) source.x >> 4, (int) source.z >> 4);
    if (chunk == null) return null;

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", source.x + 0.5))
                    .add(new DoubleTag("", source.y))
                    .add(new DoubleTag("", source.z + 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)));

    return Entity.createEntity(type.toString(), chunk, nbt, args);
}
 
Example 4
Source File: ProjectileDispenseBehavior.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void dispense(BlockDispenser source, Item item) {
    Position dispensePos = Position.fromObject(source.getDispensePosition(), source.getLevel());
    CompoundTag nbt = Entity.getDefaultNBT(dispensePos);
    this.correctNBT(nbt);

    BlockFace face = source.getFacing();

    Entity projectile = Entity.createEntity(getEntityType(), dispensePos.getLevel().getChunk(dispensePos.getFloorX(), dispensePos.getFloorZ()), nbt);
    if (projectile == null) {
        return;
    }

    projectile.setMotion(new Vector3(face.getXOffset(), face.getYOffset() + 0.1f, face.getZOffset()).multiply(6));
    projectile.spawnToAll();
}
 
Example 5
Source File: BlockTNT.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void prime(int fuse, Entity source) {
    this.getLevel().setBlock(this, Block.get(BlockID.AIR), true);
    double mot = (new NukkitRandom()).nextSignedFloat() * Math.PI * 2;
    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", this.x + 0.5))
                    .add(new DoubleTag("", this.y))
                    .add(new DoubleTag("", this.z + 0.5)))
            .putList(new ListTag<DoubleTag>("Motion")
                    .add(new DoubleTag("", -Math.sin(mot) * 0.02))
                    .add(new DoubleTag("", 0.2))
                    .add(new DoubleTag("", -Math.cos(mot) * 0.02)))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", 0))
                    .add(new FloatTag("", 0)))
            .putShort("Fuse", fuse);
    Entity tnt = Entity.createEntity("PrimedTnt",
            this.getLevel().getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4),
            nbt, source
    );
    if(tnt == null) {
        return;
    }
    tnt.spawnToAll();
    this.level.addSound(this, Sound.RANDOM_FUSE);
}
 
Example 6
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 7
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void dropItem(Vector3 source, Item item, Vector3 motion, boolean dropAround, int delay) {
    if (motion == null) {
        if (dropAround) {
            float f = ThreadLocalRandom.current().nextFloat() * 0.5f;
            float f1 = ThreadLocalRandom.current().nextFloat() * ((float) Math.PI * 2);

            motion = new Vector3(-MathHelper.sin(f1) * f, 0.20000000298023224, MathHelper.cos(f1) * f);
        } else {
            motion = new Vector3(new java.util.Random().nextDouble() * 0.2 - 0.1, 0.2,
                    new java.util.Random().nextDouble() * 0.2 - 0.1);
        }
    }

    CompoundTag itemTag = NBTIO.putItemHelper(item);
    itemTag.setName("Item");

    if (item.getId() > 0 && item.getCount() > 0) {
        EntityItem itemEntity = (EntityItem) Entity.createEntity("Item",
                this.getChunk((int) source.getX() >> 4, (int) source.getZ() >> 4, true),
                new CompoundTag().putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("", source.getX()))
                        .add(new DoubleTag("", source.getY())).add(new DoubleTag("", source.getZ())))

                        .putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("", motion.x))
                                .add(new DoubleTag("", motion.y)).add(new DoubleTag("", motion.z)))

                        .putList(new ListTag<FloatTag>("Rotation")
                                .add(new FloatTag("", new Random().nextFloat() * 360))
                                .add(new FloatTag("", 0)))

                        .putShort("Health", 5).putCompound("Item", itemTag).putShort("PickupDelay", delay));

        if (itemEntity != null) {
            itemEntity.spawnToAll();
        }
    }
}
 
Example 8
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private void performThunder(long index, FullChunk chunk) {
    if (areNeighboringChunksLoaded(index)) return;
    if (ThreadLocalRandom.current().nextInt(10000) == 0) {
        int LCG = this.getUpdateLCG() >> 2;

        int chunkX = chunk.getX() * 16;
        int chunkZ = chunk.getZ() * 16;
        Vector3 vector = this.adjustPosToNearbyEntity(new Vector3(chunkX + (LCG & 0xf), 0, chunkZ + (LCG >> 8 & 0xf)));

        Biome biome = Biome.getBiome(this.getBiomeId(vector.getFloorX(), vector.getFloorZ()));
        if (!biome.canRain()) {
            return;
        }

        int bId = this.getBlockIdAt(vector.getFloorX(), vector.getFloorY(), vector.getFloorZ());
        if (bId != Block.TALL_GRASS && bId != Block.WATER)
            vector.y += 1;
        CompoundTag nbt = new CompoundTag()
                .putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("", vector.x))
                        .add(new DoubleTag("", vector.y)).add(new DoubleTag("", vector.z)))
                .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("", 0))
                        .add(new FloatTag("", 0)));

        EntityLightning bolt = (EntityLightning) Entity.createEntity("Lightning", chunk, nbt);
        if(bolt == null) return;
        LightningStrikeEvent ev = new LightningStrikeEvent(this, bolt);
        getServer().getPluginManager().callEvent(ev);
        if (!ev.isCancelled()) {
            bolt.spawnToAll();
        } else {
            bolt.setEffect(false);
        }

        this.addLevelSoundEvent(vector, LevelSoundEventPacket.SOUND_THUNDER, -1, EntityLightning.NETWORK_ID);
        this.addLevelSoundEvent(vector, LevelSoundEventPacket.SOUND_EXPLODE, -1, EntityLightning.NETWORK_ID);
    }
}
 
Example 9
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 10
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 11
Source File: ProjectileItem.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public boolean onClickAir(Player player, Vector3 directionVector) {
    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("", directionVector.x))
                    .add(new DoubleTag("", directionVector.y))
                    .add(new DoubleTag("", directionVector.z)))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", (float) player.yaw))
                    .add(new FloatTag("", (float) player.pitch)));

    this.correctNBT(nbt);

    Entity projectile = Entity.createEntity(this.getProjectileEntityType(), player.getLevel().getChunk(player.getFloorX() >> 4, player.getFloorZ() >> 4), nbt, player);
    if (projectile != null) {
        projectile.setMotion(projectile.getMotion().multiply(this.getThrowForce()));
        this.count--;

        if (projectile instanceof EntityProjectile) {
            ProjectileLaunchEvent ev = new ProjectileLaunchEvent((EntityProjectile) projectile);

            player.getServer().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                projectile.kill();
            } else {
                projectile.spawnToAll();
                player.getLevel().addSound(player, Sound.RANDOM_BOW, 1, 1, player.getViewers().values());
            }
        } else {
            projectile.spawnToAll();
        }
    } else {
        return false;
    }
    return true;
}
 
Example 12
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 13
Source File: BlockFallable.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        Block down = this.down();
        if (down.getId() == AIR || down instanceof BlockLiquid || down instanceof BlockFire) {
            BlockFallEvent event = new BlockFallEvent(this);
            this.level.getServer().getPluginManager().callEvent(event);
            if (event.isCancelled()) {
                return type;
            }

            this.level.setBlock(this, Block.get(Block.AIR), true, true);
            CompoundTag nbt = new CompoundTag()
                    .putList(new ListTag<DoubleTag>("Pos")
                            .add(new DoubleTag("", this.x + 0.5))
                            .add(new DoubleTag("", this.y))
                            .add(new DoubleTag("", this.z + 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("", 0))
                            .add(new FloatTag("", 0)))
                    .putInt("TileID", this.getId())
                    .putByte("Data", this.getDamage());

            EntityFallingBlock fall = (EntityFallingBlock) Entity.createEntity("FallingSand", this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);

            if (fall != null) {
                fall.spawnToAll();
            }
        }
    }
    return type;
}
 
Example 14
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 15
Source File: SummonCommand.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 (sender instanceof ConsoleCommandSender) {
        sender.sendMessage(new TranslationContainer("commands.generic.ingame"));
        return true;
    }

    if (args.length < 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return false;
    }

    Position pos = new Position(Double.parseDouble(args[1]), Double.parseDouble(args[2]), Double.parseDouble(args[3]), ((Player) sender).getLevel());

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", pos.getX() + 0.5))
                    .add(new DoubleTag("", pos.getY()))
                    .add(new DoubleTag("", pos.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)));

    Entity entity = Entity.createEntity(args[0], pos.getLevel().getChunk((int) pos.getX() >> 4, (int) pos.getZ() >> 4, true), nbt);

    entity.spawnToAll();
    return true;
}
 
Example 16
Source File: ItemSpawnEgg.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) {
    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 17
Source File: ProjectileItem.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public boolean onClickAir(Player player, Vector3 directionVector) {
    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("", directionVector.x))
                    .add(new DoubleTag("", directionVector.y))
                    .add(new DoubleTag("", directionVector.z)))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", (float) player.yaw))
                    .add(new FloatTag("", (float) player.pitch)));

    this.correctNBT(nbt);
    Entity projectile = Entity.createEntity(this.getProjectileEntityType(), player.getLevel().getChunk(player.getFloorX() >> 4, player.getFloorZ() >> 4), nbt, player);
    if (projectile != null) {
        projectile.setMotion(projectile.getMotion().multiply(this.getThrowForce()));
        this.count--;

        if (projectile instanceof EntityProjectile) {
            ProjectileLaunchEvent ev = new ProjectileLaunchEvent((EntityProjectile) projectile);

            player.getServer().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                projectile.kill();
            } else {
                projectile.spawnToAll();
                player.getLevel().addSound(new LaunchSound(player), player.getViewers().values());
            }
        } else {
            projectile.spawnToAll();
        }
    } else {
        return false;
    }
    return true;
}
 
Example 18
Source File: ItemSpawnEgg.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;
    }

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", block.getX() + 0.5))
                    .add(new DoubleTag("", target.getBoundingBox() == null ? block.getY() : target.getBoundingBox().getMaxY() + 0.0001f))
                    .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());
    }

    CreatureSpawnEvent ev = new CreatureSpawnEvent(this.meta, block, nbt, SpawnReason.SPAWN_EGG);
    level.getServer().getPluginManager().callEvent(ev);

    if (ev.isCancelled()) {
        return false;
    }

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

    if (entity != null) {
        if (player.isSurvival()) {
            player.getInventory().decreaseCount(player.getInventory().getHeldItemIndex());
        }
        entity.spawnToAll();
        return true;
    }

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

    Inventory inventory = player.getOffhandInventory();

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

    double damage = 2;

    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);
    boolean 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", flame ? 45 * 60 : 0)
            .putDouble("damage", damage);

    double p = (double) ticksUsed / 20;
    double f = Math.min((p * p + p * 2) / 3, 1) * 2;

    EntityArrow arrow = (EntityArrow) Entity.createEntity("Arrow", player.chunk, nbt, player, f == 2);

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

    EntityShootBowEvent entityShootBowEvent = new EntityShootBowEvent(player, this, arrow, f);

    if (f < 0.1 || ticksUsed < 3) {
        entityShootBowEvent.setCancelled();
    }

    Server.getInstance().getPluginManager().callEvent(entityShootBowEvent);
    if (entityShootBowEvent.isCancelled()) {
        entityShootBowEvent.getProjectile().kill();
        player.getInventory().sendContents(player);
        player.getOffhandInventory().sendContents(player);
    } else {
        entityShootBowEvent.getProjectile().setMotion(entityShootBowEvent.getProjectile().getMotion().multiply(entityShootBowEvent.getForce()));
        Enchantment infinityEnchant = this.getEnchantment(Enchantment.ID_BOW_INFINITY);
        boolean infinity = infinityEnchant != null && infinityEnchant.getLevel() > 0;
        EntityProjectile projectile;
        if (infinity && (projectile = entityShootBowEvent.getProjectile()) instanceof EntityArrow) {
            ((EntityArrow) projectile).setPickupMode(EntityArrow.PICKUP_CREATIVE);
        }
        if (player.isSurvival()) {
            if (!infinity) {
                inventory.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() >= getMaxDurability()) {
                        this.count--;
                    }
                    player.getInventory().setItemInHand(this);
                }
            }
        }
        if (entityShootBowEvent.getProjectile() != null) {
            ProjectileLaunchEvent projectev = new ProjectileLaunchEvent(entityShootBowEvent.getProjectile());
            Server.getInstance().getPluginManager().callEvent(projectev);
            if (projectev.isCancelled()) {
                entityShootBowEvent.getProjectile().kill();
            } else {
                entityShootBowEvent.getProjectile().spawnToAll();
                player.getLevel().addLevelSoundEvent(player, LevelSoundEventPacket.SOUND_BOW);
            }
        }
    }

    return true;
}
 
Example 20
Source File: BlockEntityMobSpawner.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onUpdate() {
    if (this.closed) {
        return false;
    }

    this.timing.startTiming();

    if (!(this.chunk instanceof Chunk)) {
        return false;
    }

    if (!(this.canUpdate())) {
        return false;
    }

    if (this.namedTag.getInt("Delay") <= 0) {
        int success = 0;
        NukkitRandom random = new NukkitRandom();
        for (int i = 0; i < this.namedTag.getInt("SpawnCount"); ++i) {
            Vector3 pos = this.add(
                    this.getRandomSpawn(), 
                    NukkitMath.randomRange(random, -1, 1), 
                    this.getRandomSpawn());
            Block target = this.getLevel().getBlock(pos);
            Block ground = target.down();
            if (target.getId() == Block.AIR && ground.isSolid()) {
                ++success;
                CompoundTag nbt = new CompoundTag()
                        .putList(new ListTag<DoubleTag>("Pos")
                                .add(new DoubleTag("", pos.x))
                                .add(new DoubleTag("", pos.y))
                                .add(new DoubleTag("", pos.z)))
                        .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) Math.random() * 360))
                                .add(new FloatTag("", (float) 0)));

                Entity entity = Entity.createEntity(this.getNetworkId(), this.chunk, nbt);
                entity.spawnToAll();
            }
        }

        if (success > 0) {
            this.namedTag.putInt("Delay", NukkitMath.randomRange(random, this.namedTag.getInt("MinSpawnDelay"), this.namedTag.getInt("MaxSpawnDelay")));
        }

    } else {
        this.namedTag.putInt("Delay", this.namedTag.getInt("Delay") - 1);
    }

    this.timing.stopTiming();

    return true;
}