use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class BlockEntityHopper method pickupDroppedItems.
public boolean pickupDroppedItems() {
if (this.inventory.isFull()) {
return false;
}
boolean update = false;
for (Entity entity : this.level.getCollidingEntities(this.pickupArea)) {
if (!(entity instanceof EntityItem)) {
continue;
}
EntityItem itemEntity = (EntityItem) entity;
Item item = itemEntity.getItem();
if (item.getId() == 0 || item.getCount() < 1) {
continue;
}
int originalCount = item.getCount();
Item[] items = this.inventory.addItem(item);
if (items.length == 0) {
entity.close();
update = true;
continue;
}
if (items[0].getCount() != originalCount) {
update = true;
}
}
BlockEntity blockEntity = this.level.getBlockEntity(this.up());
if (blockEntity instanceof InventoryHolder) {
Inventory inv = ((InventoryHolder) blockEntity).getInventory();
for (int i = 0; i < inv.getSize(); i++) {
Item item = inv.getItem(i);
if (item.getId() != 0 && item.getCount() > 0) {
Item itemToAdd = item.clone();
itemToAdd.count = 1;
Item[] items = this.inventory.addItem(itemToAdd);
if (items.length >= 1) {
continue;
}
item.count--;
if (item.count <= 0) {
item = Item.get(0);
}
inv.setItem(i, item);
update = true;
break;
}
}
}
// TODO: check for minecart
return update;
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class BlockTNT method prime.
public void prime(int fuse) {
this.getLevel().setBlock(this, new BlockAir(), 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 = new EntityPrimedTNT(this.getLevel().getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4), nbt);
tnt.spawnToAll();
this.level.addSound(this, Sound.RANDOM_FUSE);
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class EntityItem method onUpdate.
@Override
public boolean onUpdate(int currentTick) {
if (this.closed) {
return false;
}
int tickDiff = currentTick - this.lastUpdate;
if (tickDiff <= 0 && !this.justCreated) {
return true;
}
this.lastUpdate = currentTick;
this.timing.startTiming();
boolean hasUpdate = this.entityBaseTick(tickDiff);
if (this.isAlive()) {
if (this.pickupDelay > 0 && this.pickupDelay < 32767) {
this.pickupDelay -= tickDiff;
if (this.pickupDelay < 0) {
this.pickupDelay = 0;
}
} else {
for (Entity entity : this.level.getNearbyEntities(this.boundingBox.grow(1, 0.5, 1), this)) {
if (entity instanceof Player) {
if (((Player) entity).pickupEntity(this, true)) {
return true;
}
}
}
}
this.motionY -= this.getGravity();
if (this.checkObstruction(this.x, this.y, this.z)) {
hasUpdate = true;
}
this.move(this.motionX, this.motionY, this.motionZ);
double friction = 1 - this.getDrag();
if (this.onGround && (Math.abs(this.motionX) > 0.00001 || Math.abs(this.motionZ) > 0.00001)) {
friction *= this.getLevel().getBlock(this.temporalVector.setComponents((int) Math.floor(this.x), (int) Math.floor(this.y - 1), (int) Math.floor(this.z) - 1)).getFrictionFactor();
}
this.motionX *= friction;
this.motionY *= 1 - this.getDrag();
this.motionZ *= friction;
if (this.onGround) {
this.motionY *= -0.5;
}
this.updateMovement();
if (this.age > 6000) {
ItemDespawnEvent ev = new ItemDespawnEvent(this);
this.server.getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
this.age = 0;
} else {
this.kill();
hasUpdate = true;
}
}
}
this.timing.stopTiming();
return hasUpdate || !this.onGround || Math.abs(this.motionX) > 0.00001 || Math.abs(this.motionY) > 0.00001 || Math.abs(this.motionZ) > 0.00001;
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class EntityMinecartAbstract method onUpdate.
@Override
public boolean onUpdate(int currentTick) {
if (this.closed) {
return false;
}
if (!this.isAlive()) {
++this.deadTicks;
if (this.deadTicks >= 10) {
this.despawnFromAll();
this.close();
}
return this.deadTicks < 10;
}
int tickDiff = currentTick - this.lastUpdate;
if (tickDiff <= 0) {
return false;
}
this.lastUpdate = currentTick;
if (isAlive()) {
super.onUpdate(currentTick);
// Entity variables
lastX = x;
lastY = y;
lastZ = z;
motionY -= 0.03999999910593033D;
int dx = MathHelper.floor(x);
int dy = MathHelper.floor(y);
int dz = MathHelper.floor(z);
// Some hack to check rails
if (Rail.isRailBlock(level.getBlockIdAt(dx, dy - 1, dz))) {
--dy;
}
Block block = level.getBlock(new Vector3(dx, dy, dz));
// Ensure that the block is a rail
if (Rail.isRailBlock(block)) {
processMovement(dx, dy, dz, (BlockRail) block);
if (block instanceof BlockRailActivator) {
// Activate the minecart/TNT
activate(dx, dy, dz, (block.getDamage() & 0x8) != 0);
}
} else {
setFalling();
}
checkBlockCollision();
// Minecart head
pitch = 0;
double diffX = this.lastX - this.x;
double diffZ = this.lastZ - this.z;
double yawToChange = yaw;
if (diffX * diffX + diffZ * diffZ > 0.001D) {
yawToChange = (Math.atan2(diffZ, diffX) * 180 / 3.141592653589793D);
}
// Reverse yaw if yaw is below 0
if (yawToChange < 0) {
// -90-(-90)-(-90) = 90
yawToChange -= yawToChange - yawToChange;
}
setRotation(yawToChange, pitch);
Location from = new Location(lastX, lastY, lastZ, lastYaw, lastPitch, level);
Location to = new Location(this.x, this.y, this.z, this.yaw, this.pitch, level);
this.getServer().getPluginManager().callEvent(new VehicleUpdateEvent(this));
if (!from.equals(to)) {
this.getServer().getPluginManager().callEvent(new VehicleMoveEvent(this, from, to));
}
// Collisions
for (Entity entity : level.getNearbyEntities(boundingBox.grow(0.2D, 0, 0.2D), this)) {
if (entity != linkedEntity && entity instanceof EntityMinecartAbstract) {
entity.applyEntityCollision(this);
}
}
// Easier
if ((linkedEntity != null) && (!linkedEntity.isAlive())) {
if (linkedEntity.riding == this) {
linkedEntity.riding = null;
}
linkedEntity = null;
}
// No need to onGround or Motion diff! This always have an update
return true;
}
return false;
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class ProjectileDispenseBehavior method dispense.
@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();
}
Aggregations