cn.nukkit.entity.projectile.EntityEnderPearl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powernukkit Show documentation
Show all versions of powernukkit Show documentation
A Minecraft Bedrock Edition server software implementation made in Java from scratch which supports all new features.
package cn.nukkit.entity.projectile;
import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.entity.Entity;
import cn.nukkit.event.entity.EntityDamageByEntityEvent;
import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.event.player.PlayerTeleportEvent.TeleportCause;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.math.NukkitMath;
import cn.nukkit.math.Vector3;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.network.protocol.LevelEventPacket;
public class EntityEnderPearl extends EntityProjectile {
public static final int NETWORK_ID = 87;
@Override
public int getNetworkId() {
return NETWORK_ID;
}
@Override
public float getWidth() {
return 0.25f;
}
@Override
public float getLength() {
return 0.25f;
}
@Override
public float getHeight() {
return 0.25f;
}
@Override
protected float getGravity() {
return 0.03f;
}
@Override
protected float getDrag() {
return 0.01f;
}
public EntityEnderPearl(FullChunk chunk, CompoundTag nbt) {
this(chunk, nbt, null);
}
public EntityEnderPearl(FullChunk chunk, CompoundTag nbt, Entity shootingEntity) {
super(chunk, nbt, shootingEntity);
}
@Override
public boolean onUpdate(int currentTick) {
if (this.closed) {
return false;
}
this.timing.startTiming();
boolean hasUpdate = super.onUpdate(currentTick);
if (this.isCollided && this.shootingEntity instanceof Player) {
boolean portal = false;
for (Block collided : this.getCollisionBlocks()) {
if (collided.getId() == Block.NETHER_PORTAL) {
portal = true;
}
}
if (!portal) {
teleport();
}
}
if (this.age > 1200 || this.isCollided) {
this.kill();
hasUpdate = true;
}
this.timing.stopTiming();
return hasUpdate;
}
@Override
public void onCollideWithEntity(Entity entity) {
if (this.shootingEntity instanceof Player) {
teleport();
}
super.onCollideWithEntity(entity);
}
private void teleport() {
if (!this.level.equals(this.shootingEntity.getLevel())) {
return;
}
this.level.addLevelEvent(this.shootingEntity.add(0.5, 0.5, 0.5), LevelEventPacket.EVENT_SOUND_PORTAL);
this.shootingEntity.teleport(new Vector3(NukkitMath.floorDouble(this.x) + 0.5, this.y, NukkitMath.floorDouble(this.z) + 0.5), TeleportCause.ENDER_PEARL);
if ((((Player) this.shootingEntity).getGamemode() & 0x01) == 0) {
this.shootingEntity.attack(new EntityDamageByEntityEvent(this, shootingEntity, EntityDamageEvent.DamageCause.PROJECTILE, 5f, 0f));
}
this.level.addLevelEvent(this, LevelEventPacket.EVENT_PARTICLE_ENDERMAN_TELEPORT);
this.level.addLevelEvent(this.shootingEntity.add(0.5, 0.5, 0.5), LevelEventPacket.EVENT_SOUND_PORTAL);
}
}