Fix BossBars

This commit is contained in:
Marco 2020-04-18 15:48:21 +02:00
parent 672024c718
commit 7cd941e5d6
4 changed files with 130 additions and 62 deletions

View file

@ -0,0 +1,104 @@
package org.geysermc.connector.network.session.cache;
import com.github.steveice10.mc.protocol.data.message.Message;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.EntityData;
import com.nukkitx.protocol.bedrock.packet.AddEntityPacket;
import com.nukkitx.protocol.bedrock.packet.BossEventPacket;
import com.nukkitx.protocol.bedrock.packet.RemoveEntityPacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.utils.MessageUtils;
public class BossBar {
private GeyserSession session;
private long entityId;
private Message title;
private float health;
private int color;
private int overlay;
private int darkenSky;
public BossBar(GeyserSession session, Message title, float health, int color, int overlay, int darkenSky) {
this.session = session;
this.entityId = session.getEntityCache().getNextEntityId().incrementAndGet();
this.title = title;
this.health = health;
this.color = color;
this.overlay = overlay;
this.darkenSky = darkenSky;
}
public void addBossBar() {
addBossEntity();
updateBossBar();
}
public void updateBossBar() {
BossEventPacket bossEventPacket = new BossEventPacket();
bossEventPacket.setBossUniqueEntityId(entityId);
bossEventPacket.setAction(BossEventPacket.Action.SHOW);
bossEventPacket.setTitle(MessageUtils.getTranslatedBedrockMessage(title, session.getClientData().getLanguageCode()));
bossEventPacket.setHealthPercentage(health);
bossEventPacket.setColor(color); //ignored by client
bossEventPacket.setOverlay(overlay);
bossEventPacket.setDarkenSky(darkenSky);
session.getUpstream().sendPacket(bossEventPacket);
}
public void updateTitle(Message title) {
this.title = title;
BossEventPacket bossEventPacket = new BossEventPacket();
bossEventPacket.setBossUniqueEntityId(entityId);
bossEventPacket.setAction(BossEventPacket.Action.TITLE);
bossEventPacket.setTitle(MessageUtils.getTranslatedBedrockMessage(title, session.getClientData().getLanguageCode()));
session.getUpstream().sendPacket(bossEventPacket);
}
public void updateHealth(float health) {
this.health = health;
BossEventPacket bossEventPacket = new BossEventPacket();
bossEventPacket.setBossUniqueEntityId(entityId);
bossEventPacket.setAction(BossEventPacket.Action.HEALTH_PERCENTAGE);
bossEventPacket.setHealthPercentage(health);
session.getUpstream().sendPacket(bossEventPacket);
}
public void removeBossBar() {
BossEventPacket bossEventPacket = new BossEventPacket();
bossEventPacket.setBossUniqueEntityId(entityId);
bossEventPacket.setAction(BossEventPacket.Action.HIDE);
session.getUpstream().sendPacket(bossEventPacket);
removeBossEntity();
}
/**
* Bedrock still needs an entity to display the BossBar.<br>
* Just like 1.8 but it doesn't care about which entity
*/
private void addBossEntity() {
AddEntityPacket addEntityPacket = new AddEntityPacket();
addEntityPacket.setUniqueEntityId(entityId);
addEntityPacket.setRuntimeEntityId(entityId);
addEntityPacket.setIdentifier("minecraft:creeper");
addEntityPacket.setEntityType(33);
addEntityPacket.setPosition(session.getPlayerEntity().getPosition());
addEntityPacket.setRotation(Vector3f.ZERO);
addEntityPacket.setMotion(Vector3f.ZERO);
addEntityPacket.getMetadata().put(EntityData.SCALE, 0.01F); // scale = 0 doesn't work?
session.getUpstream().sendPacket(addEntityPacket);
}
private void removeBossEntity() {
RemoveEntityPacket removeEntityPacket = new RemoveEntityPacket();
removeEntityPacket.setUniqueEntityId(entityId);
session.getUpstream().sendPacket(removeEntityPacket);
}
}

View file

@ -48,7 +48,7 @@ public class EntityCache {
private Long2ObjectMap<Entity> entities = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>()); private Long2ObjectMap<Entity> entities = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>());
private Long2LongMap entityIdTranslations = Long2LongMaps.synchronize(new Long2LongOpenHashMap()); private Long2LongMap entityIdTranslations = Long2LongMaps.synchronize(new Long2LongOpenHashMap());
private Map<UUID, PlayerEntity> playerEntities = Collections.synchronizedMap(new HashMap<>()); private Map<UUID, PlayerEntity> playerEntities = Collections.synchronizedMap(new HashMap<>());
private Object2LongMap<UUID> bossbars = new Object2LongOpenHashMap<>(); private Map<UUID, BossBar> bossBars = Collections.synchronizedMap(new HashMap<>());
@Getter @Getter
private AtomicLong nextEntityId = new AtomicLong(2L); private AtomicLong nextEntityId = new AtomicLong(2L);
@ -116,24 +116,30 @@ public class EntityCache {
playerEntities.remove(uuid); playerEntities.remove(uuid);
} }
public long addBossBar(UUID uuid) { public void addBossBar(UUID uuid, BossBar bossBar) {
long entityId = getNextEntityId().incrementAndGet(); bossBars.put(uuid, bossBar);
bossbars.put(uuid, entityId); bossBar.addBossBar();
return entityId;
} }
public long getBossBar(UUID uuid) { public BossBar getBossBar(UUID uuid) {
return bossbars.containsKey(uuid) ? bossbars.get(uuid) : -1; return bossBars.get(uuid);
} }
public long removeBossBar(UUID uuid) { public void removeBossBar(UUID uuid) {
return bossbars.remove(uuid); BossBar bossBar = bossBars.remove(uuid);
if (bossBar != null) {
bossBar.removeBossBar();
}
}
public void updateBossBars() {
bossBars.values().forEach(BossBar::updateBossBar);
} }
public void clear() { public void clear() {
entities = null; entities = null;
entityIdTranslations = null; entityIdTranslations = null;
playerEntities = null; playerEntities = null;
bossbars = null; bossBars = null;
} }
} }

View file

@ -123,6 +123,7 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
spawnPacket.setStatus(PlayStatusPacket.Status.PLAYER_SPAWN); spawnPacket.setStatus(PlayStatusPacket.Status.PLAYER_SPAWN);
session.getUpstream().sendPacket(spawnPacket); session.getUpstream().sendPacket(spawnPacket);
entity.updateBedrockAttributes(session); entity.updateBedrockAttributes(session);
session.getEntityCache().updateBossBars();
} }
break; break;
case JUMP: case JUMP:

View file

@ -26,80 +26,37 @@
package org.geysermc.connector.network.translators.java; package org.geysermc.connector.network.translators.java;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.session.cache.BossBar;
import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator; import org.geysermc.connector.network.translators.Translator;
import org.geysermc.connector.utils.MessageUtils;
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerBossBarPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerBossBarPacket;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.EntityData; import java.awt.*;
import com.nukkitx.protocol.bedrock.packet.AddEntityPacket;
import com.nukkitx.protocol.bedrock.packet.BossEventPacket;
import com.nukkitx.protocol.bedrock.packet.RemoveEntityPacket;
@Translator(packet = ServerBossBarPacket.class) @Translator(packet = ServerBossBarPacket.class)
public class JavaBossBarTranslator extends PacketTranslator<ServerBossBarPacket> { public class JavaBossBarTranslator extends PacketTranslator<ServerBossBarPacket> {
@Override @Override
public void translate(ServerBossBarPacket packet, GeyserSession session) { public void translate(ServerBossBarPacket packet, GeyserSession session) {
BossEventPacket bossEventPacket = new BossEventPacket(); BossBar bossBar = session.getEntityCache().getBossBar(packet.getUuid());
bossEventPacket.setBossUniqueEntityId(session.getEntityCache().getBossBar(packet.getUuid()));
switch (packet.getAction()) { switch (packet.getAction()) {
case ADD: case ADD:
long entityId = session.getEntityCache().addBossBar(packet.getUuid()); bossBar = new BossBar(session, packet.getTitle(), packet.getHealth(), Color.RED.getRGB(), 1, 0);
addBossEntity(session, entityId); session.getEntityCache().addBossBar(packet.getUuid(), bossBar);
bossEventPacket.setAction(BossEventPacket.Action.SHOW);
bossEventPacket.setBossUniqueEntityId(entityId);
bossEventPacket.setTitle(MessageUtils.getTranslatedBedrockMessage(packet.getTitle(), session.getClientData().getLanguageCode()));
bossEventPacket.setHealthPercentage(packet.getHealth());
bossEventPacket.setColor(0); //ignored by client
bossEventPacket.setOverlay(1);
bossEventPacket.setDarkenSky(0);
break; break;
case UPDATE_TITLE: case UPDATE_TITLE:
bossEventPacket.setAction(BossEventPacket.Action.TITLE); if (bossBar != null) bossBar.updateTitle(packet.getTitle());
bossEventPacket.setTitle(MessageUtils.getTranslatedBedrockMessage(packet.getTitle(), session.getClientData().getLanguageCode()));
break; break;
case UPDATE_HEALTH: case UPDATE_HEALTH:
bossEventPacket.setAction(BossEventPacket.Action.HEALTH_PERCENTAGE); if (bossBar != null) bossBar.updateHealth(packet.getHealth());
bossEventPacket.setHealthPercentage(packet.getHealth());
break; break;
case REMOVE: case REMOVE:
bossEventPacket.setAction(BossEventPacket.Action.HIDE); session.getEntityCache().removeBossBar(packet.getUuid());
removeBossEntity(session, session.getEntityCache().removeBossBar(packet.getUuid()));
break; break;
case UPDATE_STYLE: case UPDATE_STYLE:
case UPDATE_FLAGS: case UPDATE_FLAGS:
//todo //todo
return; return;
} }
session.getUpstream().sendPacket(bossEventPacket);
}
/**
* Bedrock still needs an entity to display the BossBar.<br>
* Just like 1.8 but it doesn't care about which entity
*/
private void addBossEntity(GeyserSession session, long entityId) {
AddEntityPacket addEntityPacket = new AddEntityPacket();
addEntityPacket.setUniqueEntityId(entityId);
addEntityPacket.setRuntimeEntityId(entityId);
addEntityPacket.setIdentifier("minecraft:creeper");
addEntityPacket.setEntityType(33);
addEntityPacket.setPosition(session.getPlayerEntity().getPosition());
addEntityPacket.setRotation(Vector3f.ZERO);
addEntityPacket.setMotion(Vector3f.ZERO);
addEntityPacket.getMetadata().put(EntityData.SCALE, 0.01F); // scale = 0 doesn't work?
session.getUpstream().sendPacket(addEntityPacket);
}
private void removeBossEntity(GeyserSession session, long entityId) {
RemoveEntityPacket removeEntityPacket = new RemoveEntityPacket();
removeEntityPacket.setUniqueEntityId(entityId);
session.getUpstream().sendPacket(removeEntityPacket);
} }
} }