From 3d14b614ce18ca3e0477b40780aa603867f3902e Mon Sep 17 00:00:00 2001 From: Konstantin Shandurenko Date: Mon, 22 Feb 2021 09:05:54 +0300 Subject: [PATCH] EntityArrow and EntitySpectralArrow --- .../type/projectile/EntityAbstractArrow.java | 78 +++++++++++++++++++ .../entity/type/projectile/EntityArrow.java | 27 +++++++ .../type/projectile/EntitySpectralArrow.java | 18 +++++ 3 files changed, 123 insertions(+) create mode 100644 src/main/java/net/minestom/server/entity/type/projectile/EntityAbstractArrow.java create mode 100644 src/main/java/net/minestom/server/entity/type/projectile/EntityArrow.java create mode 100644 src/main/java/net/minestom/server/entity/type/projectile/EntitySpectralArrow.java diff --git a/src/main/java/net/minestom/server/entity/type/projectile/EntityAbstractArrow.java b/src/main/java/net/minestom/server/entity/type/projectile/EntityAbstractArrow.java new file mode 100644 index 000000000..deaa7a06b --- /dev/null +++ b/src/main/java/net/minestom/server/entity/type/projectile/EntityAbstractArrow.java @@ -0,0 +1,78 @@ +package net.minestom.server.entity.type.projectile; + +import net.minestom.server.entity.Entity; +import net.minestom.server.entity.EntityType; +import net.minestom.server.entity.Metadata; +import net.minestom.server.entity.ObjectEntity; +import net.minestom.server.entity.type.Projectile; +import net.minestom.server.utils.Position; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Created by k.shandurenko on 22.02.2021 + */ +public class EntityAbstractArrow extends ObjectEntity implements Projectile { + + private final static byte CRITICAL_BIT = 0x01; + private final static byte NO_CLIP_BIT = 0x02; + + private final int shooterID; + + EntityAbstractArrow(@Nullable Entity shooter, @NotNull EntityType entityType, @NotNull Position spawnPosition) { + super(entityType, spawnPosition); + this.shooterID = shooter == null ? 0 : shooter.getEntityId(); + } + + public void setCritical(boolean value) { + modifyMask(CRITICAL_BIT, value); + } + + public boolean isCritical() { + return (getMask() & CRITICAL_BIT) != 0; + } + + public void setNoClip(boolean value) { + modifyMask(NO_CLIP_BIT, value); + } + + public boolean isNoClip() { + return (getMask() & NO_CLIP_BIT) != 0; + } + + public void setPiercingLevel(byte value) { + this.metadata.setIndex((byte) 8, Metadata.Byte(value)); + } + + public byte getPiercingLevel() { + return this.metadata.getIndex((byte) 8, (byte) 0); + } + + private byte getMask() { + return this.metadata.getIndex((byte) 7, (byte) 0); + } + + private void setMask(byte mask) { + this.metadata.setIndex((byte) 7, Metadata.Byte(mask)); + } + + private void modifyMask(byte bit, boolean value) { + byte mask = getMask(); + boolean isPresent = (mask & bit) == bit; + if (isPresent == value) { + return; + } + if (value) { + mask |= bit; + } else { + mask &= ~bit; + } + setMask(mask); + } + + @Override + public int getObjectData() { + return this.shooterID + 1; + } + +} diff --git a/src/main/java/net/minestom/server/entity/type/projectile/EntityArrow.java b/src/main/java/net/minestom/server/entity/type/projectile/EntityArrow.java new file mode 100644 index 000000000..85098370b --- /dev/null +++ b/src/main/java/net/minestom/server/entity/type/projectile/EntityArrow.java @@ -0,0 +1,27 @@ +package net.minestom.server.entity.type.projectile; + +import net.minestom.server.entity.Entity; +import net.minestom.server.entity.EntityType; +import net.minestom.server.entity.Metadata; +import net.minestom.server.utils.Position; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Created by k.shandurenko on 22.02.2021 + */ +public class EntityArrow extends EntityAbstractArrow { + + public EntityArrow(@Nullable Entity shooter, @NotNull Position spawnPosition) { + super(shooter, EntityType.ARROW, spawnPosition); + } + + public void setColor(int value) { + this.metadata.setIndex((byte) 9, Metadata.VarInt(value)); + } + + public int getColor() { + return this.metadata.getIndex((byte) 9, -1); + } + +} diff --git a/src/main/java/net/minestom/server/entity/type/projectile/EntitySpectralArrow.java b/src/main/java/net/minestom/server/entity/type/projectile/EntitySpectralArrow.java new file mode 100644 index 000000000..a3f2af688 --- /dev/null +++ b/src/main/java/net/minestom/server/entity/type/projectile/EntitySpectralArrow.java @@ -0,0 +1,18 @@ +package net.minestom.server.entity.type.projectile; + +import net.minestom.server.entity.Entity; +import net.minestom.server.entity.EntityType; +import net.minestom.server.utils.Position; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Created by k.shandurenko on 22.02.2021 + */ +public class EntitySpectralArrow extends EntityAbstractArrow { + + public EntitySpectralArrow(@Nullable Entity shooter, @NotNull Position spawnPosition) { + super(shooter, EntityType.SPECTRAL_ARROW, spawnPosition); + } + +}