3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-26 08:10:09 +01:00

Abstract the BossBar for multi-platform support

Dieser Commit ist enthalten in:
Matsv 2016-09-24 19:31:41 +02:00
Ursprung dc01fb59b8
Commit b146257b1a
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 97CEC2A2EA31350F
7 geänderte Dateien mit 280 neuen und 252 gelöschten Zeilen

Datei anzeigen

@ -7,6 +7,7 @@ import us.myles.ViaVersion.api.boss.BossColor;
import us.myles.ViaVersion.api.boss.BossStyle;
import us.myles.ViaVersion.api.command.ViaVersionCommand;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import us.myles.ViaVersion.boss.ViaBossBar;
import java.util.SortedSet;
import java.util.UUID;
@ -80,7 +81,7 @@ public interface ViaVersionAPI {
* @param title The title
* @param color The color
* @param style The style
* @return Bossbar instance
* @return BossBar instance
*/
BossBar createBossBar(String title, BossColor color, BossStyle style);
@ -91,7 +92,7 @@ public interface ViaVersionAPI {
* @param health Number between 0 and 1
* @param color The color
* @param style The style
* @return Bossbar instance
* @return BossBar instance
*/
BossBar createBossBar(String title, float health, BossColor color, BossStyle style);

Datei anzeigen

@ -1,237 +1,42 @@
package us.myles.ViaVersion.boss;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.Validate;
import org.bukkit.entity.Player;
import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.boss.BossBar;
import us.myles.ViaVersion.api.boss.BossColor;
import us.myles.ViaVersion.api.boss.BossFlag;
import us.myles.ViaVersion.api.boss.BossStyle;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8;
import java.util.*;
@Getter
public class ViaBossBar implements BossBar {
private UUID uuid;
private String title;
private float health;
private BossColor color;
private BossStyle style;
private Set<UUID> players;
private boolean visible;
private Set<BossFlag> flags;
public class ViaBossBar extends CommonBoss {
public ViaBossBar(String title, float health, BossColor color, BossStyle style) {
Validate.notNull(title, "Title cannot be null");
Validate.isTrue((health >= 0 && health <= 1), "Health must be between 0 and 1");
this.uuid = UUID.randomUUID();
this.title = title;
this.health = health;
this.color = color == null ? BossColor.PURPLE : color;
this.style = style == null ? BossStyle.SOLID : style;
this.players = new HashSet<>();
this.flags = new HashSet<>();
visible = true;
super(title, health, color, style);
}
@Override
public BossBar setTitle(@NonNull String title) {
this.title = title;
sendPacket(UpdateAction.UPDATE_TITLE);
return this;
}
@Override
public BossBar setHealth(float health) {
Validate.isTrue((health >= 0 && health <= 1), "Health must be between 0 and 1");
this.health = health;
sendPacket(UpdateAction.UPDATE_HEALTH);
return this;
}
@Override
public BossColor getColor() {
return color;
}
@Override
public BossBar setColor(@NonNull BossColor color) {
this.color = color;
sendPacket(UpdateAction.UPDATE_STYLE);
return this;
}
@Override
public BossBar setStyle(@NonNull BossStyle style) {
this.style = style;
sendPacket(UpdateAction.UPDATE_STYLE);
return this;
}
@Override
public BossBar addPlayer(@NonNull Player player) {
return addPlayer(player.getUniqueId());
}
@Override
public BossBar addPlayer(UUID player) {
if (!players.contains(player)) {
players.add(player);
if (visible)
sendPacket(player, getPacket(UpdateAction.ADD));
public BossBar addPlayer(Object player) {
if (player instanceof Player){
addPlayer(((Player) player).getUniqueId());
} else {
throw new IllegalArgumentException("The addPlayer argument has to be a Bukkit player on this platform");
}
return this;
}
@Override
public BossBar removePlayer(@NonNull Player player) {
if (players.contains(player.getUniqueId())) {
players.remove(player.getUniqueId());
sendPacket(player.getUniqueId(), getPacket(UpdateAction.REMOVE));
}
return this;
}
@Override
public BossBar addPlayers(@NonNull Player... players) {
for (Player p : players)
public BossBar addPlayers(Object... players) {
for (Object p : players)
addPlayer(p);
return this;
}
@Override
public BossBar addFlag(@NonNull BossFlag flag) {
if (!hasFlag(flag))
flags.add(flag);
sendPacket(UpdateAction.UPDATE_FLAGS);
return this;
}
@Override
public BossBar removeFlag(@NonNull BossFlag flag) {
if (hasFlag(flag))
flags.remove(flag);
sendPacket(UpdateAction.UPDATE_FLAGS);
return this;
}
@Override
public boolean hasFlag(@NonNull BossFlag flag) {
return flags.contains(flag);
}
@Override
public Set<UUID> getPlayers() {
return Collections.unmodifiableSet(players);
}
@Override
public BossBar show() {
setVisible(true);
return this;
}
@Override
public BossBar hide() {
setVisible(false);
return this;
}
@Override
public boolean isVisible() {
return visible;
}
private void setVisible(boolean value) {
if (visible != value) {
visible = value;
sendPacket(value ? UpdateAction.ADD : UpdateAction.REMOVE);
public BossBar removePlayer(Object player) {
if (player instanceof Player){
removePlayer(((Player) player).getUniqueId());
} else {
throw new IllegalArgumentException("The removePlayer argument has to be a Bukkit player on this platform");
}
}
private void sendPacket(UpdateAction action) {
for (UUID uuid : new ArrayList<>(players)) {
ByteBuf buf = getPacket(action);
sendPacket(uuid, buf);
}
}
private void sendPacket(UUID uuid, ByteBuf buf) {
if (!ViaVersion.getInstance().isPorted(uuid) || !(ViaVersion.getInstance().getPlayerVersion(uuid) >= ProtocolVersion.v1_9.getId())) {
players.remove(uuid);
buf.release();
return;
}
ViaVersion.getInstance().sendRawPacket(uuid, buf);
}
private ByteBuf getPacket(UpdateAction action) {
try {
ByteBuf buf = Unpooled.buffer();
Type.VAR_INT.write(buf, 0x0C); // Boss bar packet
Type.UUID.write(buf, uuid);
Type.VAR_INT.write(buf, action.getId());
switch (action) {
case ADD:
Type.STRING.write(buf, fixJson(title));
buf.writeFloat(health);
Type.VAR_INT.write(buf, color.getId());
Type.VAR_INT.write(buf, style.getId());
buf.writeByte(flagToBytes());
break;
case REMOVE:
break;
case UPDATE_HEALTH:
buf.writeFloat(health);
break;
case UPDATE_TITLE:
Type.STRING.write(buf, fixJson(title));
break;
case UPDATE_STYLE:
Type.VAR_INT.write(buf, color.getId());
Type.VAR_INT.write(buf, style.getId());
break;
case UPDATE_FLAGS:
buf.writeByte(flagToBytes());
break;
}
return buf;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private int flagToBytes() {
int bitmask = 0;
for (BossFlag flag : flags)
bitmask |= flag.getId();
return bitmask;
}
private String fixJson(String text) {
return Protocol1_9TO1_8.fixJson(text);
}
@RequiredArgsConstructor
@Getter
private enum UpdateAction {
ADD(0),
REMOVE(1),
UPDATE_HEALTH(2),
UPDATE_TITLE(3),
UPDATE_STYLE(4),
UPDATE_FLAGS(5);
private final int id;
return this;
}
}

Datei anzeigen

@ -1,17 +1,18 @@
package us.myles.ViaVersion.api.boss;
import org.bukkit.entity.Player;
import org.apache.commons.lang.NotImplementedException;
import us.myles.ViaVersion.api.Via;
import java.util.Set;
import java.util.UUID;
public interface BossBar {
public abstract class BossBar {
/**
* Get the current title
*
* @return the title
*/
String getTitle();
public abstract String getTitle();
/**
* Change the title
@ -19,14 +20,14 @@ public interface BossBar {
* @param title Title can be in either JSON or just text
* @return The BossBar object
*/
BossBar setTitle(String title);
public abstract BossBar setTitle(String title);
/**
* Get the health
*
* @return float between 0F - 1F
*/
float getHealth();
public abstract float getHealth();
/**
* Change the health
@ -34,14 +35,14 @@ public interface BossBar {
* @param health this float has to be between 0F - 1F
* @return The BossBar object
*/
BossBar setHealth(float health);
public abstract BossBar setHealth(float health);
/**
* Get the bossbar color
*
* @return The colour
*/
BossColor getColor();
public abstract BossColor getColor();
/**
* Yay colors!
@ -49,14 +50,14 @@ public interface BossBar {
* @param color Whatever color you want!
* @return The BossBar object
*/
BossBar setColor(BossColor color);
public abstract BossBar setColor(BossColor color);
/**
* Get the bosbar style
*
* @return BossStyle
*/
BossStyle getStyle();
public abstract BossStyle getStyle();
/**
* Change the bosbar style
@ -64,7 +65,7 @@ public interface BossBar {
* @param style BossStyle
* @return The BossBar object
*/
BossBar setStyle(BossStyle style);
public abstract BossBar setStyle(BossStyle style);
/**
* Show the bossbar to a player.
@ -72,7 +73,9 @@ public interface BossBar {
* @param player The player
* @return The BossBar object
*/
BossBar addPlayer(Player player);
public BossBar addPlayer(Object player){
throw new NotImplementedException("This method is not implemented for the platform " + Via.getPlatform().getPlatformName());
}
/**
* Show the bossbar to a player (uuid)
@ -80,7 +83,7 @@ public interface BossBar {
* @param player uuid of the player
* @return The BossBar object
*/
BossBar addPlayer(UUID player);
public abstract BossBar addPlayer(UUID player);
/**
* add multiple players
@ -88,7 +91,9 @@ public interface BossBar {
* @param players list of players
* @return The BossBar object
*/
BossBar addPlayers(Player... players);
public BossBar addPlayers(Object... players){
throw new NotImplementedException("This method is not implemented for the platform " + Via.getPlatform().getPlatformName());
}
/**
* Remove the bossbar from a player
@ -96,7 +101,11 @@ public interface BossBar {
* @param player The player
* @return The BossBar object
*/
BossBar removePlayer(Player player);
public BossBar removePlayer(Object player){
throw new NotImplementedException("This method is not implemented for the platform " + Via.getPlatform().getPlatformName());
}
public abstract BossBar removePlayer(UUID uuid);
/**
* Add flags
@ -104,7 +113,7 @@ public interface BossBar {
* @param flag The flag to add
* @return The BossBar object
*/
BossBar addFlag(BossFlag flag);
public abstract BossBar addFlag(BossFlag flag);
/**
* Remove flags.
@ -112,39 +121,39 @@ public interface BossBar {
* @param flag The flag to remove
* @return The BossBar object
*/
BossBar removeFlag(BossFlag flag);
public abstract BossBar removeFlag(BossFlag flag);
/**
* @param flag The flag to check against
* @return True if it has the flag
*/
boolean hasFlag(BossFlag flag);
public abstract boolean hasFlag(BossFlag flag);
/**
* Get players
*
* @return UUIDS from players (sorry I lied)
*/
Set<UUID> getPlayers();
public abstract Set<UUID> getPlayers();
/**
* Show the bossbar to everyone (In the getPlayer set)
*
* @return The BossBar object
*/
BossBar show();
public abstract BossBar show();
/**
* Hide the bossbar from everyone (In the getPlayer set)
*
* @return The BossBar object
*/
BossBar hide();
public abstract BossBar hide();
/**
* Is it visible?
*
* @return visibility changable with show() and hide()
*/
boolean isVisible();
public abstract boolean isVisible();
}

Datei anzeigen

@ -6,6 +6,7 @@ import io.netty.channel.ChannelHandler;
import io.netty.channel.socket.SocketChannel;
import lombok.Data;
import net.md_5.bungee.api.ChatColor;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
@ -144,10 +145,10 @@ public class UserConnection {
pendingDisconnect = true;
if (get(ProtocolInfo.class).getUuid() != null) {
final UUID uuid = get(ProtocolInfo.class).getUuid();
ViaVersion.getPlatform().runSync(new Runnable() {
Via.getPlatform().runSync(new Runnable() {
@Override
public void run() {
if (!ViaVersion.getPlatform().kickPlayer(uuid, ChatColor.translateAlternateColorCodes('&', reason))) {
if (!Via.getPlatform().kickPlayer(uuid, ChatColor.translateAlternateColorCodes('&', reason))) {
getChannel().close(); // =)
}
}

Datei anzeigen

@ -1,26 +1,13 @@
package us.myles.ViaVersion.api.minecraft.metadata;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.*;
import us.myles.ViaVersion.api.type.Type;
@AllArgsConstructor
@Getter
@Setter
@Data
public class Metadata {
private int id;
private int typeID;
private Type type;
private Object value;
@Override
public String toString() {
return "Metadata{" +
"id=" + id +
", typeID=" + typeID +
", type=" + type +
", value=" + value +
'}';
}
}

Datei anzeigen

@ -0,0 +1,225 @@
package us.myles.ViaVersion.boss;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.Validate;
import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.boss.BossBar;
import us.myles.ViaVersion.api.boss.BossColor;
import us.myles.ViaVersion.api.boss.BossFlag;
import us.myles.ViaVersion.api.boss.BossStyle;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8;
import java.util.*;
@Getter
public abstract class CommonBoss extends BossBar {
private UUID uuid;
private String title;
private float health;
private BossColor color;
private BossStyle style;
private Set<UUID> players;
private boolean visible;
private Set<BossFlag> flags;
public CommonBoss(String title, float health, BossColor color, BossStyle style) {
Validate.notNull(title, "Title cannot be null");
Validate.isTrue((health >= 0 && health <= 1), "Health must be between 0 and 1");
this.uuid = UUID.randomUUID();
this.title = title;
this.health = health;
this.color = color == null ? BossColor.PURPLE : color;
this.style = style == null ? BossStyle.SOLID : style;
this.players = new HashSet<>();
this.flags = new HashSet<>();
visible = true;
}
@Override
public BossBar setTitle(@NonNull String title) {
this.title = title;
sendPacket(ViaBossBar.UpdateAction.UPDATE_TITLE);
return this;
}
@Override
public BossBar setHealth(float health) {
Validate.isTrue((health >= 0 && health <= 1), "Health must be between 0 and 1");
this.health = health;
sendPacket(ViaBossBar.UpdateAction.UPDATE_HEALTH);
return this;
}
@Override
public BossColor getColor() {
return color;
}
@Override
public BossBar setColor(@NonNull BossColor color) {
this.color = color;
sendPacket(ViaBossBar.UpdateAction.UPDATE_STYLE);
return this;
}
@Override
public BossBar setStyle(@NonNull BossStyle style) {
this.style = style;
sendPacket(ViaBossBar.UpdateAction.UPDATE_STYLE);
return this;
}
@Override
public BossBar addPlayer(UUID player) {
if (!players.contains(player)) {
players.add(player);
if (visible)
sendPacket(player, getPacket(ViaBossBar.UpdateAction.ADD));
}
return this;
}
@Override
public BossBar removePlayer(UUID uuid) {
if (players.contains(uuid)) {
players.remove(uuid);
sendPacket(uuid, getPacket(UpdateAction.REMOVE));
}
return this;
}
@Override
public BossBar addFlag(@NonNull BossFlag flag) {
if (!hasFlag(flag))
flags.add(flag);
sendPacket(ViaBossBar.UpdateAction.UPDATE_FLAGS);
return this;
}
@Override
public BossBar removeFlag(@NonNull BossFlag flag) {
if (hasFlag(flag))
flags.remove(flag);
sendPacket(ViaBossBar.UpdateAction.UPDATE_FLAGS);
return this;
}
@Override
public boolean hasFlag(@NonNull BossFlag flag) {
return flags.contains(flag);
}
@Override
public Set<UUID> getPlayers() {
return Collections.unmodifiableSet(players);
}
@Override
public BossBar show() {
setVisible(true);
return this;
}
@Override
public BossBar hide() {
setVisible(false);
return this;
}
@Override
public boolean isVisible() {
return visible;
}
private void setVisible(boolean value) {
if (visible != value) {
visible = value;
sendPacket(value ? ViaBossBar.UpdateAction.ADD : ViaBossBar.UpdateAction.REMOVE);
}
}
private void sendPacket(UpdateAction action) {
for (UUID uuid : new ArrayList<>(players)) {
ByteBuf buf = getPacket(action);
sendPacket(uuid, buf);
}
}
private void sendPacket(UUID uuid, ByteBuf buf) {
if (!ViaVersion.getInstance().isPorted(uuid) || !(ViaVersion.getInstance().getPlayerVersion(uuid) >= ProtocolVersion.v1_9.getId())) {
players.remove(uuid);
buf.release();
return;
}
ViaVersion.getInstance().sendRawPacket(uuid, buf);
}
private ByteBuf getPacket(UpdateAction action) {
try {
ByteBuf buf = Unpooled.buffer();
Type.VAR_INT.write(buf, 0x0C); // Boss bar packet
Type.UUID.write(buf, uuid);
Type.VAR_INT.write(buf, action.getId());
switch (action) {
case ADD:
Type.STRING.write(buf, fixJson(title));
buf.writeFloat(health);
Type.VAR_INT.write(buf, color.getId());
Type.VAR_INT.write(buf, style.getId());
buf.writeByte(flagToBytes());
break;
case REMOVE:
break;
case UPDATE_HEALTH:
buf.writeFloat(health);
break;
case UPDATE_TITLE:
Type.STRING.write(buf, fixJson(title));
break;
case UPDATE_STYLE:
Type.VAR_INT.write(buf, color.getId());
Type.VAR_INT.write(buf, style.getId());
break;
case UPDATE_FLAGS:
buf.writeByte(flagToBytes());
break;
}
return buf;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private int flagToBytes() {
int bitmask = 0;
for (BossFlag flag : flags)
bitmask |= flag.getId();
return bitmask;
}
private String fixJson(String text) {
return Protocol1_9TO1_8.fixJson(text);
}
@RequiredArgsConstructor
@Getter
private enum UpdateAction {
ADD(0),
REMOVE(1),
UPDATE_HEALTH(2),
UPDATE_TITLE(3),
UPDATE_STYLE(4),
UPDATE_FLAGS(5);
private final int id;
}
}

Datei anzeigen

@ -17,7 +17,7 @@
<defaultGoal>clean install</defaultGoal>
<resources>
<resource>
<targetPath>.</targetPath>
<targetPath>.</targetPath>https://github.com/MylesIsCool/ViaVersion/blob/master/ISSUE_TEMPLATE.md
<filtering>false</filtering>
<directory>.</directory>
<includes>