Skip to content

Commit

Permalink
Repair BossBar, add old title methods for better compatibility (#19)
Browse files Browse the repository at this point in the history
* Make old plugins working on this Nukkit

* Forgot these imports

* Who did invent this?

* Back to how this should be

* Forgot this
  • Loading branch information
kvetinac97 authored and Creeperface01 committed Sep 26, 2017
1 parent 0c39fe6 commit 5e5f6ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 74 deletions.
36 changes: 27 additions & 9 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -4948,4 +4966,4 @@ public void notifyACK(int identification) {
public boolean isBreakingBlock() {
return this.breakingBlock != null;
}
}
}
67 changes: 2 additions & 65 deletions src/main/java/cn/nukkit/network/protocol/BossEventPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,89 +7,26 @@ 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;
}

@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
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;
}
}
}

4 comments on commit 5e5f6ce

@boybook
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

@boybook
Copy link
Collaborator

@boybook boybook commented on 5e5f6ce Sep 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why revert the BossEventPacket?

@boybook
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make it compatible with old plugins and add new features at the same time.

@Snake1999
Copy link

@Snake1999 Snake1999 commented on 5e5f6ce Sep 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for adapting 'old' interfaces, you are removing all the new features done with tons of work

@kvetinac97 do not do this next time :)

Please sign in to comment.