From 5e5f6ce1c6c6954cc939639954680cfef479c8d8 Mon Sep 17 00:00:00 2001 From: kvetinac97 Date: Tue, 26 Sep 2017 07:17:33 +0200 Subject: [PATCH] Repair BossBar, add old title methods for better compatibility (#19) * Make old plugins working on this Nukkit * Forgot these imports * Who did invent this? * Back to how this should be * Forgot this --- src/main/java/cn/nukkit/Player.java | 36 +++++++--- .../network/protocol/BossEventPacket.java | 67 +------------------ 2 files changed, 29 insertions(+), 74 deletions(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index 1d5c57e528..b38ab1b39a 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -12,6 +12,7 @@ import cn.nukkit.entity.*; import cn.nukkit.entity.data.*; import cn.nukkit.entity.item.*; +import cn.nukkit.entity.mob.EntityCreeper; import cn.nukkit.entity.projectile.EntityArrow; import cn.nukkit.event.block.ItemFrameDropItemEvent; import cn.nukkit.event.block.SignChangeEvent; @@ -79,6 +80,7 @@ import java.util.*; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.ThreadLocalRandom; /** * author: MagicDroidX & Box @@ -3635,6 +3637,22 @@ public void resetTitleSettings() { pk.type = SetTitlePacket.TYPE_RESET; this.dataPacket(pk); } + + public void setSubtitle(String subtitle){ + SetTitlePacket pk = new SetTitlePacket(); + pk.type = SetTitlePacket.TYPE_SUBTITLE; + pk.text = subtitle; + this.dataPacket(pk); + } + + public void setTitleAnimationTimes(int fadein, int duration, int fadeout){ + SetTitlePacket pk = new SetTitlePacket(); + pk.type = SetTitlePacket.TYPE_ANIMATION_TIMES; + pk.fadeInTime = fadein; + pk.stayTime = duration; + pk.fadeOutTime = fadeout; + this.dataPacket(pk); + } public void sendTitle(String title) { this.sendTitle(title, "", 20, 20, 5); @@ -4484,7 +4502,7 @@ public int addServerSettings(FormWindow window) { * @return bossBarId The BossBar ID, you should store it if you want to remove or update the BossBar later */ - /*public long createBossBar(String text, int length) { + public long createBossBar(String text, int length) { // First we spawn a entity long bossBarId = 1095216660480L + ThreadLocalRandom.current().nextLong(0, 0x7fffffffL); AddEntityPacket pkAdd = new AddEntityPacket(); @@ -4521,11 +4539,11 @@ public int addServerSettings(FormWindow window) { // And now we send the bossbar packet BossEventPacket pkBoss = new BossEventPacket(); - pkBoss.eid = bossBarId; - pkBoss.type = BossEventPacket.ADD; + pkBoss.bossEid = bossBarId; + pkBoss.type = BossEventPacket.TYPE_SHOW; this.dataPacket(pkBoss); return bossBarId; - }*/ + } /** * Updates a BossBar @@ -4534,7 +4552,7 @@ public int addServerSettings(FormWindow window) { * @param length The new BossBar length * @param bossBarId The BossBar ID */ - /*public void updateBossBar(String text, int length, long bossBarId) { + public void updateBossBar(String text, int length, long bossBarId) { // First we update the boss bar length UpdateAttributesPacket pkAttributes = new UpdateAttributesPacket(); pkAttributes.entityId = bossBarId; @@ -4559,11 +4577,11 @@ public int addServerSettings(FormWindow window) { // And now we send the bossbar packet BossEventPacket pkBoss = new BossEventPacket(); - pkBoss.eid = bossBarId; - pkBoss.type = BossEventPacket.UPDATE; + pkBoss.bossEid = bossBarId; + pkBoss.type = BossEventPacket.TYPE_UPDATE; this.dataPacket(pkBoss); return; - }*/ + } /** * Removes a BossBar @@ -4948,4 +4966,4 @@ public void notifyACK(int identification) { public boolean isBreakingBlock() { return this.breakingBlock != null; } -} \ No newline at end of file +} diff --git a/src/main/java/cn/nukkit/network/protocol/BossEventPacket.java b/src/main/java/cn/nukkit/network/protocol/BossEventPacket.java index 1b52ddc078..ef0675f1a3 100644 --- a/src/main/java/cn/nukkit/network/protocol/BossEventPacket.java +++ b/src/main/java/cn/nukkit/network/protocol/BossEventPacket.java @@ -7,32 +7,13 @@ public class BossEventPacket extends DataPacket { public static final byte NETWORK_ID = ProtocolInfo.BOSS_EVENT_PACKET; - /* S2C: Shows the boss-bar to the player. */ public static final int TYPE_SHOW = 0; - /* C2S: Registers a player to a boss fight. */ - public static final int TYPE_REGISTER_PLAYER = 1; - /* S2C: Removes the boss-bar from the client. */ + public static final int TYPE_UPDATE = 1; public static final int TYPE_HIDE = 2; - /* C2S: Unregisters a player from a boss fight. */ - public static final int TYPE_UNREGISTER_PLAYER = 3; - /* S2C: Appears not to be implemented. Currently bar percentage only appears to change in response to the target entity's health. */ - public static final int TYPE_HEALTH_PERCENT = 4; - /* S2C: Also appears to not be implemented. Title client-side sticks as the target entity's nametag, or their entity type name if not set. */ - public static final int TYPE_TITLE = 5; - /* S2C: Not sure on this. Includes color and overlay fields, plus an unknown short. TODO: check this */ - public static final int TYPE_UNKNOWN_6 = 6; - /* S2C: Not implemented :( Intended to alter bar appearance, but these currently produce no effect on client-side whatsoever. */ - public static final int TYPE_TEXTURE = 7; public long bossEid; public int type; - public long playerEid; - public float healthPercent; - public String title; - public short unknown; - public int color; - public int overlay; - + @Override public byte pid() { return NETWORK_ID; @@ -40,29 +21,6 @@ public byte pid() { @Override public void decode() { - this.bossEid = this.getEntityUniqueId(); - this.type = (int) this.getUnsignedVarInt(); - switch (this.type) { - case TYPE_REGISTER_PLAYER: - case TYPE_UNREGISTER_PLAYER: - this.playerEid = this.getEntityUniqueId(); - break; - case TYPE_SHOW: - this.title = this.getString(); - this.healthPercent = this.getLFloat(); - case TYPE_UNKNOWN_6: - this.unknown = (short) this.getShort(); - case TYPE_TEXTURE: - this.color = (int) this.getUnsignedVarInt(); - this.overlay = (int) this.getUnsignedVarInt(); - break; - case TYPE_HEALTH_PERCENT: - this.healthPercent = this.getLFloat(); - break; - case TYPE_TITLE: - this.title = this.getString(); - break; - } } @Override @@ -70,26 +28,5 @@ public void encode() { this.reset(); this.putEntityUniqueId(this.bossEid); this.putUnsignedVarInt(this.type); - switch (this.type) { - case TYPE_REGISTER_PLAYER: - case TYPE_UNREGISTER_PLAYER: - this.putEntityUniqueId(this.playerEid); - break; - case TYPE_SHOW: - this.putString(this.title); - this.putLFloat(this.healthPercent); - case TYPE_UNKNOWN_6: - this.putShort(this.unknown); - case TYPE_TEXTURE: - this.putUnsignedVarInt(this.color); - this.putUnsignedVarInt(this.overlay); - break; - case TYPE_HEALTH_PERCENT: - this.putLFloat(this.healthPercent); - break; - case TYPE_TITLE: - this.putString(this.title); - break; - } } }