use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class BaseInventory method setItem.
@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);
}
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();
}
Item old = this.getItem(index);
this.slots.put(index, item.clone());
this.onSlotChange(index, old, send);
return true;
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class BaseInventory method clear.
@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;
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class EntityPotion method onUpdate.
@Override
public boolean onUpdate(int currentTick) {
if (this.closed) {
return false;
}
this.timing.startTiming();
int tickDiff = currentTick - this.lastUpdate;
boolean hasUpdate = super.onUpdate(currentTick);
if (this.age > 1200) {
this.kill();
hasUpdate = true;
}
if (this.isCollided) {
this.kill();
Potion potion = Potion.getPotion(this.potionId);
PotionCollideEvent event = new PotionCollideEvent(potion, this);
this.server.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return false;
}
potion = event.getPotion();
if (potion == null) {
return false;
}
potion.setSplash(true);
Particle particle;
int r;
int g;
int b;
Effect effect = Potion.getEffect(potion.getId(), true);
if (effect == null) {
r = 40;
g = 40;
b = 255;
} else {
int[] colors = effect.getColor();
r = colors[0];
g = colors[1];
b = colors[2];
}
if (Potion.isInstant(potion.getId())) {
particle = new InstantSpellParticle(this, r, g, b);
} else {
particle = new SpellParticle(this, r, g, b);
}
this.getLevel().addParticle(particle);
hasUpdate = true;
Entity[] entities = this.getLevel().getNearbyEntities(this.getBoundingBox().grow(8.25, 4.24, 8.25));
for (Entity anEntity : entities) {
double distance = anEntity.distanceSquared(this);
if (distance < 16) {
double d = 1 - Math.sqrt(distance) / 4;
potion.applyPotion(anEntity, d);
}
}
}
this.timing.stopTiming();
return hasUpdate;
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class EntityXPOrb 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;
boolean hasUpdate = entityBaseTick(tickDiff);
if (this.isAlive()) {
if (this.pickupDelay > 0 && this.pickupDelay < 32767) {
// Infinite delay
this.pickupDelay -= tickDiff;
if (this.pickupDelay < 0) {
this.pickupDelay = 0;
}
} else {
for (Entity entity : this.level.getCollidingEntities(this.boundingBox, this)) {
if (entity instanceof Player) {
if (((Player) entity).pickupEntity(this, false)) {
return true;
}
}
}
}
this.motionY -= this.getGravity();
if (this.checkObstruction(this.x, this.y, this.z)) {
hasUpdate = true;
}
if (this.closestPlayer == null || this.closestPlayer.distanceSquared(this) > 64.0D) {
for (Player p : level.getPlayers().values()) {
if (!p.isSpectator() && p.distance(this) <= 8) {
this.closestPlayer = p;
break;
}
}
}
if (this.closestPlayer != null && this.closestPlayer.isSpectator()) {
this.closestPlayer = null;
}
if (this.closestPlayer != null) {
double dX = (this.closestPlayer.x - this.x) / 8.0D;
double dY = (this.closestPlayer.y + (double) this.closestPlayer.getEyeHeight() / 2.0D - this.y) / 8.0D;
double dZ = (this.closestPlayer.z - this.z) / 8.0D;
double d = Math.sqrt(dX * dX + dY * dY + dZ * dZ);
double diff = 1.0D - d;
if (diff > 0.0D) {
diff = diff * diff;
this.motionX += dX / d * diff * 0.1D;
this.motionY += dY / d * diff * 0.1D;
this.motionZ += dZ / d * diff * 0.1D;
}
}
this.move(this.motionX, this.motionY, this.motionZ);
double friction = 1d - 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() * friction;
}
this.motionX *= friction;
this.motionY *= 1 - this.getDrag();
this.motionZ *= friction;
if (this.onGround) {
this.motionY *= -0.5;
}
this.updateMovement();
if (this.age > 6000) {
this.kill();
hasUpdate = true;
}
}
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 EntityLightning 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.entityBaseTick(tickDiff);
if (this.state == 2) {
this.level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_THUNDER, 93, -1);
this.level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_EXPLODE, 93, -1);
}
this.state--;
if (this.state < 0) {
if (this.liveTime == 0) {
this.close();
return false;
} else if (this.state < -ThreadLocalRandom.current().nextInt(10)) {
this.liveTime--;
this.state = 1;
if (this.isEffect && this.level.gameRules.getBoolean(GameRule.DO_FIRE_TICK)) {
Block block = this.getLevelBlock();
if (block.getId() == Block.AIR || block.getId() == Block.TALL_GRASS) {
BlockIgniteEvent e = new BlockIgniteEvent(block, null, this, BlockIgniteEvent.BlockIgniteCause.LIGHTNING);
getServer().getPluginManager().callEvent(e);
if (!e.isCancelled()) {
Block fire = new BlockFire();
this.level.setBlock(block, fire);
this.getLevel().scheduleUpdate(fire, fire.tickRate());
}
}
}
}
}
if (this.state >= 0) {
if (this.isEffect) {
AxisAlignedBB bb = getBoundingBox().grow(3, 3, 3);
bb.setMaxX(bb.getMaxX() + 6);
for (Entity entity : this.level.getCollidingEntities(bb, this)) {
entity.onStruckByLightning(this);
}
}
}
return true;
}
Aggregations