cn.nukkit.entity.Entity Java Examples

The following examples show how to use cn.nukkit.entity.Entity. 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: Level.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public boolean hasCollision(Entity entity, AxisAlignedBB bb, boolean entities) {
    int minX = NukkitMath.floorDouble(bb.getMinX());
    int minY = NukkitMath.floorDouble(bb.getMinY());
    int minZ = NukkitMath.floorDouble(bb.getMinZ());
    int maxX = NukkitMath.ceilDouble(bb.getMaxX());
    int maxY = NukkitMath.ceilDouble(bb.getMaxY());
    int maxZ = NukkitMath.ceilDouble(bb.getMaxZ());

    for (int z = minZ; z <= maxZ; ++z) {
        for (int x = minX; x <= maxX; ++x) {
            for (int y = minY; y <= maxY; ++y) {
                Block block = this.getBlock(this.temporalVector.setComponents(x, y, z));
                if (!block.canPassThrough() && block.collidesWithBB(bb)) {
                    return true;
                }
            }
        }
    }

    if (entities) {
        return this.getCollidingEntities(bb.grow(0.25f, 0.25f, 0.25f), entity).length > 0;
    }
    return false;
}
 
Example #2
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 #3
Source File: Effect.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void applyEffect(Entity entity) {
    switch (this.id) {
        case Effect.POISON: //POISON
            if (entity.getHealth() > 1) {
                entity.attack(new EntityDamageEvent(entity, DamageCause.MAGIC, 1));
            }
            break;
        case Effect.WITHER: //WITHER
            entity.attack(new EntityDamageEvent(entity, DamageCause.MAGIC, 1));
            break;
        case Effect.REGENERATION: //REGENERATION
            if (entity.getHealth() < entity.getMaxHealth()) {
                entity.heal(new EntityRegainHealthEvent(entity, 1, EntityRegainHealthEvent.CAUSE_MAGIC));
            }
            break;
    }
}
 
Example #4
Source File: DeathEventListener.java    From Plan with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerDeath(PlayerDeathEvent event) {
    long time = System.currentTimeMillis();
    Player dead = event.getEntity();
    SessionCache.getCachedSession(dead.getUniqueId()).ifPresent(Session::died);

    try {
        EntityDamageEvent entityDamageEvent = dead.getLastDamageCause();
        if (!(entityDamageEvent instanceof EntityDamageByEntityEvent)) {
            return;
        }

        EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entityDamageEvent;
        Entity killerEntity = entityDamageByEntityEvent.getDamager();

        UUID uuid = dead.getUniqueId();
        handleKill(time, uuid, killerEntity);
    } catch (Exception e) {
        errorLogger.log(L.ERROR, e, ErrorContext.builder().related(event, dead).build());
    }
}
 
Example #5
Source File: BlockEntityPistonArm.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private void pushEntities() {
    float lastProgress = this.getExtendedProgress(this.lastProgress);
    double x = (double) (lastProgress * (float) this.facing.getXOffset());
    double y = (double) (lastProgress * (float) this.facing.getYOffset());
    double z = (double) (lastProgress * (float) this.facing.getZOffset());
    AxisAlignedBB bb = new SimpleAxisAlignedBB(x, y, z, x + 1.0D, y + 1.0D, z + 1.0D);
    Entity[] entities = this.level.getCollidingEntities(bb);
    if (entities.length != 0) {

    }

}
 
Example #6
Source File: BlockEntityPistonArm.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private void pushEntities() {
    float lastProgress = this.getExtendedProgress(this.lastProgress);
    double x = (double) (lastProgress * (float) this.facing.getXOffset());
    double y = (double) (lastProgress * (float) this.facing.getYOffset());
    double z = (double) (lastProgress * (float) this.facing.getZOffset());
    AxisAlignedBB bb = new AxisAlignedBB(x, y, z, x + 1.0D, y + 1.0D, z + 1.0D);
    Entity[] entities = this.level.getCollidingEntities(bb);
    if (entities.length != 0) {
        ;
    }

}
 
Example #7
Source File: BaseFullChunk.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void addEntity(Entity entity) {
    if (this.entities == null) {
        this.entities = new Long2ObjectOpenHashMap<>();
    }
    this.entities.put(entity.getId(), entity);
    if (!(entity instanceof Player) && this.isInit) {
        this.setChanged();
    }
}
 
Example #8
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 #9
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public Entity[] getNearbyEntities(AxisAlignedBB bb, Entity entity, boolean loadChunks) {
    int index = 0;

    int minX = NukkitMath.floorDouble((bb.getMinX() - 2) * 0.0625);
    int maxX = NukkitMath.ceilDouble((bb.getMaxX() + 2) * 0.0625);
    int minZ = NukkitMath.floorDouble((bb.getMinZ() - 2) * 0.0625);
    int maxZ = NukkitMath.ceilDouble((bb.getMaxZ() + 2) * 0.0625);

    ArrayList<Entity> overflow = null;

    for (int x = minX; x <= maxX; ++x) {
        for (int z = minZ; z <= maxZ; ++z) {
            for (Entity ent : this.getChunkEntities(x, z, loadChunks).values()) {
                if (ent != entity && ent.boundingBox.intersectsWith(bb)) {
                    if (index < ENTITY_BUFFER.length) {
                        ENTITY_BUFFER[index] = ent;
                    } else {
                        if (overflow == null) overflow = new ArrayList<>(1024);
                        overflow.add(ent);
                    }
                    index++;
                }
            }
        }
    }

    if (index == 0) return EMPTY_ENTITY_ARR;
    Entity[] copy;
    if (overflow == null) {
        copy = Arrays.copyOfRange(ENTITY_BUFFER, 0, index);
        Arrays.fill(ENTITY_BUFFER, 0, index, null);
    } else {
        copy = new Entity[ENTITY_BUFFER.length + overflow.size()];
        System.arraycopy(ENTITY_BUFFER, 0, copy, 0, ENTITY_BUFFER.length);
        for (int i = 0; i < overflow.size(); i++) {
            copy[ENTITY_BUFFER.length + i] = overflow.get(i);
        }
    }
    return copy;
}
 
Example #10
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void addPlayerMovement(Entity entity, double x, double y, double z, double yaw, double pitch, double headYaw) {
    MovePlayerPacket pk = new MovePlayerPacket();
    pk.eid = entity.getId();
    pk.x = (float) x;
    pk.y = (float) y;
    pk.z = (float) z;
    pk.yaw = (float) yaw;
    pk.headYaw = (float) headYaw;
    pk.pitch = (float) pitch;

    Server.broadcastPacket(entity.getViewers().values(), pk);
}
 
Example #11
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean setItem(int index, Item item, boolean send) {
    item = item.clone();
    if (index < 0 || index >= this.size) {
        return false;
    } else if (item.getId() == 0 || item.getCount() <= 0) {
        return this.clear(index, send);
    }

    InventoryHolder holder = this.getHolder();
    if (holder instanceof Entity) {
        EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent((Entity) holder, this.getItem(index), item, index);
        Server.getInstance().getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            this.sendSlot(index, this.getViewers());
            return false;
        }

        item = ev.getNewItem();
    }

    if (holder instanceof BlockEntity) {
        ((BlockEntity) holder).setDirty();
    }

    Item old = this.getItem(index);
    this.slots.put(index, item.clone());
    this.onSlotChange(index, old, send);

    return true;
}
 
Example #12
Source File: EntityBoat.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    if (invulnerable) {
        return false;
    } else {
        performHurtAnimation((int) source.getFinalDamage());

        Entity damager = ((EntityDamageByEntityEvent) source).getDamager();
        boolean instantKill = damager instanceof Player && ((Player) damager).isCreative();
        if (instantKill || getDamage() > 40) {
            if (linkedEntity != null) {
                mountEntity(linkedEntity);
            }

            if (instantKill && (!hasCustomName())) {
                kill();
            } else {
                if (level.getGameRules().getBoolean("doentitydrops")) {
                    this.level.dropItem(this, new ItemBoat());
                }
                close();
            }
        }
    }
    
    return true;
}
 
Example #13
Source File: BaseFullChunk.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void removeEntity(Entity entity) {
    this.entities.remove(entity.getId());
    if (!(entity instanceof Player) && this.isInit) {
        this.setChanged();
    }
}
 
Example #14
Source File: ItemArmorStand.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("", this.getDirection(player.yaw)))
                    .add(new FloatTag("", 0)));

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

    Entity entity = Entity.createEntity("ArmorStand", 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 #15
Source File: EnchantmentDamageSmite.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public double getDamageBonus(Entity entity) {
    if(entity instanceof EntitySmite) {
        return getLevel() * 2.5;
    }

    return 0;
}
 
Example #16
Source File: BlockLava.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onEntityCollide(Entity entity) {
    entity.highestPosition -= (entity.highestPosition - entity.y) * 0.5;
    if (!entity.hasEffect(Effect.FIRE_RESISTANCE)) {
        entity.attack(new EntityDamageByBlockEvent(this, entity, DamageCause.LAVA, 4));
    }

    EntityCombustByBlockEvent ev = new EntityCombustByBlockEvent(this, entity, 15);
    Server.getInstance().getPluginManager().callEvent(ev);
    if (!ev.isCancelled()) {
        entity.setOnFire(ev.getDuration());
    }

    super.onEntityCollide(entity);
}
 
Example #17
Source File: BlockMagma.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onEntityCollide(Entity entity) {
    if (!entity.hasEffect(Effect.FIRE_RESISTANCE)) {
        if (entity instanceof Player) {
            Player p = (Player) entity;
            if (!p.isCreative() && !p.isSpectator() && !p.isSneaking()) {
                entity.attack(new EntityDamageByBlockEvent(this, entity, EntityDamageEvent.DamageCause.LAVA, 1));
            }
        } else {
            entity.attack(new EntityDamageByBlockEvent(this, entity, EntityDamageEvent.DamageCause.LAVA, 1));
        }
    }
}
 
Example #18
Source File: BaseInventory.java    From Nukkit 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, 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 #19
Source File: BlockRailDetector.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected void updateState() {
    boolean wasPowered = isActive();
    boolean isPowered = false;

    for (Entity entity : level.getNearbyEntities(new SimpleAxisAlignedBB(
            getFloorX() + 0.125D,
            getFloorY(),
            getFloorZ() + 0.125D,
            getFloorX() + 0.875D,
            getFloorY() + 0.525D,
            getFloorZ() + 0.875D))) {
        if (entity instanceof EntityMinecartAbstract) {
            isPowered = true;
        }
    }

    if (isPowered && !wasPowered) {
        setActive(true);
        level.scheduleUpdate(this, this, 0);
        level.scheduleUpdate(this, this.down(), 0);
    }

    if (!isPowered && wasPowered) {
        setActive(false);
        level.scheduleUpdate(this, this, 0);
        level.scheduleUpdate(this, this.down(), 0);
    }

    level.updateComparatorOutputLevel(this);
}
 
Example #20
Source File: EnchantmentThorns.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void doPostAttack(Entity attacker, Entity entity) {
    if (!(entity instanceof EntityHumanType)) {
        return;
    }

    EntityHumanType human = (EntityHumanType) entity;

    int thornsDamage = 0;
    Random rnd = new Random();

    for (Item armor : human.getInventory().getArmorContents()) {
        Enchantment thorns = armor.getEnchantment(Enchantment.ID_THORNS);

        if (thorns != null && thorns.getLevel() > 0) {
            int chance = thorns.getLevel() * 15;

            if (chance > 90) {
                chance = 90;
            }

            if (rnd.nextInt(100) + 1 <= chance) {
                thornsDamage += rnd.nextInt(4) + 1;
            }
        }
    }

    if (thornsDamage > 0) {
        attacker.attack(new EntityDamageEvent(attacker, DamageCause.MAGIC, rnd.nextInt(4) + 1));
    }
}
 
Example #21
Source File: NukkitWorld.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<com.sk89q.worldedit.entity.Entity> getEntities(Region region) {
    Level world = getLevel();
    cn.nukkit.entity.Entity[] ents = world.getEntities();
    List<com.sk89q.worldedit.entity.Entity> entities = new ArrayList<com.sk89q.worldedit.entity.Entity>();
    for (cn.nukkit.entity.Entity ent : ents) {
        if (region.contains(NukkitUtil.toVector(ent.getLocation()))) {
            entities.add(new NukkitEntity(ent));
        }
    }
    return entities;
}
 
Example #22
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 #23
Source File: EntityMetadataStore.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected String disambiguate(Metadatable entity, String metadataKey) {
    if (!(entity instanceof Entity)) {
        throw new IllegalArgumentException("Argument must be an Entity instance");
    }
    return ((Entity) entity).getId() + ":" + metadataKey;
}
 
Example #24
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 #25
Source File: EnchantmentDamageAll.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public double getDamageBonus(Entity entity) {
    if (this.getLevel() <= 0) {
        return 0;
    }

    return 0.5 + getLevel() * 0.5;
}
 
Example #26
Source File: EntityDamageEvent.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public EntityDamageEvent(Entity entity, DamageCause cause, Map<DamageModifier, Float> modifiers) {
    this.entity = entity;
    this.cause = cause;
    this.modifiers = new EnumMap<>(modifiers);

    this.originals = ImmutableMap.copyOf(this.modifiers);

    if (!this.modifiers.containsKey(DamageModifier.BASE)) {
        throw new EventException("BASE Damage modifier missing");
    }

    if (entity.hasEffect(Effect.DAMAGE_RESISTANCE)) {
        this.setDamage((float) -(this.getDamage(DamageModifier.BASE) * 0.20 * (entity.getEffect(Effect.DAMAGE_RESISTANCE).getAmplifier() + 1)), DamageModifier.RESISTANCE);
    }
}
 
Example #27
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void addEntityMovement(Entity entity, double x, double y, double z, double yaw, double pitch, double headYaw) {
    MoveEntityAbsolutePacket pk = new MoveEntityAbsolutePacket();
    pk.eid = entity.getId();
    pk.x = (float) x;
    pk.y = (float) y;
    pk.z = (float) z;
    pk.yaw = (float) yaw;
    pk.headYaw = (float) headYaw;
    pk.pitch = (float) pitch;
    pk.onGround = entity.onGround;

    Server.broadcastPacket(entity.getViewers().values(), pk);
}
 
Example #28
Source File: EntityMetadataStore.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected String disambiguate(Metadatable entity, String metadataKey) {
    if (!(entity instanceof Entity)) {
        throw new IllegalArgumentException("Argument must be an Entity instance");
    }
    return ((Entity) entity).getId() + ":" + metadataKey;
}
 
Example #29
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 #30
Source File: BlockTripWire.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_SCHEDULED) {
        if (!isPowered()) {
            return type;
        }

        boolean found = false;
        for (Entity entity : this.level.getCollidingEntities(this.getCollisionBoundingBox())) {
            if (!entity.doesTriggerPressurePlate()) {
                continue;
            }

            found = true;
        }

        if (found) {
            this.level.scheduleUpdate(this, 10);
        } else {
            this.setPowered(false);
            this.level.setBlock(this, this, true, false);
            this.updateHook(false);
        }
        return type;
    }

    return 0;
}