mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-30 13:08:19 +01:00
Update title/actionbar methods
This commit is contained in:
parent
6c51631a29
commit
459b50863e
@ -14,8 +14,7 @@ import net.minestom.server.adventure.AdventurePacketConvertor;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.message.ChatPosition;
|
||||
import net.minestom.server.message.Messenger;
|
||||
import net.minestom.server.network.packet.server.play.PlayerListHeaderAndFooterPacket;
|
||||
import net.minestom.server.network.packet.server.play.TitlePacket;
|
||||
import net.minestom.server.network.packet.server.play.*;
|
||||
import net.minestom.server.utils.PacketUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@ -52,7 +51,7 @@ public interface PacketGroupingAudience extends ForwardingAudience {
|
||||
|
||||
@Override
|
||||
default void sendActionBar(@NotNull Component message) {
|
||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new TitlePacket(TitlePacket.Action.SET_ACTION_BAR, message));
|
||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new ActionBarPacket(message));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -62,18 +61,18 @@ public interface PacketGroupingAudience extends ForwardingAudience {
|
||||
|
||||
@Override
|
||||
default void showTitle(@NotNull Title title) {
|
||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new TitlePacket(TitlePacket.Action.SET_TITLE, title.title()));
|
||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new TitlePacket(TitlePacket.Action.SET_SUBTITLE, title.subtitle()));
|
||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new SetTitleTextPacket(title.title()));
|
||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new SetTitleSubTitlePacket(title.subtitle()));
|
||||
}
|
||||
|
||||
@Override
|
||||
default void clearTitle() {
|
||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new TitlePacket(TitlePacket.Action.HIDE));
|
||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new ClearTitlesPacket());
|
||||
}
|
||||
|
||||
@Override
|
||||
default void resetTitle() {
|
||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new TitlePacket(TitlePacket.Action.RESET));
|
||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new ClearTitlesPacket(true));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -904,20 +904,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
||||
playerConnection.sendPacket(packet);
|
||||
}
|
||||
|
||||
/**
|
||||
* Common method to send a title.
|
||||
*
|
||||
* @param text the text of the title
|
||||
* @param action the action of the title (where to show it)
|
||||
* @see #sendTitleTime(int, int, int) to specify the display time
|
||||
* @deprecated Use {@link #showTitle(Title)} and {@link #sendActionBar(Component)}
|
||||
*/
|
||||
@Deprecated
|
||||
private void sendTitle(@NotNull JsonMessage text, @NotNull TitlePacket.Action action) {
|
||||
TitlePacket titlePacket = new TitlePacket(action, text.asComponent());
|
||||
playerConnection.sendPacket(titlePacket);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a title and subtitle message.
|
||||
*
|
||||
@ -969,17 +955,20 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
||||
|
||||
@Override
|
||||
public void showTitle(@NotNull Title title) {
|
||||
Collection<TitlePacket> packet = TitlePacket.of(Title.title(title.title(), title.subtitle(), title.times()));
|
||||
|
||||
for (TitlePacket titlePacket : packet) {
|
||||
playerConnection.sendPacket(titlePacket);
|
||||
playerConnection.sendPacket(new SetTitleTextPacket(title.title()));
|
||||
playerConnection.sendPacket(new SetTitleSubTitlePacket(title.subtitle()));
|
||||
final var times = title.times();
|
||||
if (times != null) {
|
||||
playerConnection.sendPacket(new SetTitleTimePacket(
|
||||
TickUtils.fromDuration(times.fadeIn(), TickUtils.CLIENT_TICK_MS),
|
||||
TickUtils.fromDuration(times.stay(), TickUtils.CLIENT_TICK_MS),
|
||||
TickUtils.fromDuration(times.fadeOut(), TickUtils.CLIENT_TICK_MS)));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendActionBar(@NotNull Component message) {
|
||||
TitlePacket titlePacket = new TitlePacket(TitlePacket.Action.SET_ACTION_BAR, message);
|
||||
playerConnection.sendPacket(titlePacket);
|
||||
playerConnection.sendPacket(new ActionBarPacket(message));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -993,31 +982,17 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
||||
*/
|
||||
@Deprecated
|
||||
public void sendTitleTime(int fadeIn, int stay, int fadeOut) {
|
||||
TitlePacket titlePacket = new TitlePacket(fadeIn, stay, fadeOut);
|
||||
playerConnection.sendPacket(titlePacket);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides the previous title.
|
||||
*
|
||||
* @deprecated Use {@link #clearTitle()}
|
||||
*/
|
||||
@Deprecated
|
||||
public void hideTitle() {
|
||||
TitlePacket titlePacket = new TitlePacket(TitlePacket.Action.HIDE);
|
||||
playerConnection.sendPacket(titlePacket);
|
||||
playerConnection.sendPacket(new SetTitleTimePacket(fadeIn, stay, fadeOut));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetTitle() {
|
||||
TitlePacket titlePacket = new TitlePacket(TitlePacket.Action.RESET);
|
||||
playerConnection.sendPacket(titlePacket);
|
||||
playerConnection.sendPacket(new ClearTitlesPacket(true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearTitle() {
|
||||
TitlePacket titlePacket = new TitlePacket(TitlePacket.Action.HIDE);
|
||||
playerConnection.sendPacket(titlePacket);
|
||||
playerConnection.sendPacket(new ClearTitlesPacket());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -11,6 +11,13 @@ public class ActionBarPacket implements ServerPacket {
|
||||
|
||||
public Component actionBarText = Component.empty();
|
||||
|
||||
public ActionBarPacket() {
|
||||
}
|
||||
|
||||
public ActionBarPacket(Component actionBarText) {
|
||||
this.actionBarText = actionBarText;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.actionBarText = reader.readComponent();
|
||||
|
@ -10,6 +10,13 @@ public class ClearTitlesPacket implements ServerPacket {
|
||||
|
||||
public boolean reset;
|
||||
|
||||
public ClearTitlesPacket() {
|
||||
}
|
||||
|
||||
public ClearTitlesPacket(boolean reset) {
|
||||
this.reset = reset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.reset = reader.readBoolean();
|
||||
|
@ -11,6 +11,13 @@ public class SetTitleSubTitlePacket implements ServerPacket {
|
||||
|
||||
public Component subtitle = Component.empty();
|
||||
|
||||
public SetTitleSubTitlePacket() {
|
||||
}
|
||||
|
||||
public SetTitleSubTitlePacket(Component subtitle) {
|
||||
this.subtitle = subtitle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.subtitle = reader.readComponent();
|
||||
|
@ -7,10 +7,17 @@ import net.minestom.server.utils.binary.BinaryReader;
|
||||
import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SetTitleTextPacker implements ServerPacket {
|
||||
public class SetTitleTextPacket implements ServerPacket {
|
||||
|
||||
public Component title = Component.empty();
|
||||
|
||||
public SetTitleTextPacket() {
|
||||
}
|
||||
|
||||
public SetTitleTextPacket(Component title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.title = reader.readComponent();
|
@ -12,6 +12,15 @@ public class SetTitleTimePacket implements ServerPacket {
|
||||
public int stay;
|
||||
public int fadeOut;
|
||||
|
||||
public SetTitleTimePacket() {
|
||||
}
|
||||
|
||||
public SetTitleTimePacket(int fadeIn, int stay, int fadeOut) {
|
||||
this.fadeIn = fadeIn;
|
||||
this.stay = stay;
|
||||
this.fadeOut = fadeOut;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.fadeIn = reader.readInt();
|
||||
|
@ -1,172 +0,0 @@
|
||||
package net.minestom.server.network.packet.server.play;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import net.minestom.server.network.packet.server.ComponentHoldingServerPacket;
|
||||
import net.minestom.server.network.packet.server.ServerPacket;
|
||||
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
|
||||
import net.minestom.server.utils.TickUtils;
|
||||
import net.minestom.server.utils.binary.BinaryReader;
|
||||
import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import static net.minestom.server.network.packet.server.play.TitlePacket.Action.*;
|
||||
|
||||
public class TitlePacket implements ComponentHoldingServerPacket {
|
||||
|
||||
public Action action;
|
||||
|
||||
public Component payload;
|
||||
|
||||
public int fadeIn;
|
||||
public int stay;
|
||||
public int fadeOut;
|
||||
|
||||
/**
|
||||
* Constructs a new title packet from an action that can take a component argument.
|
||||
*
|
||||
* @param action the action
|
||||
* @param payload the payload
|
||||
* @throws IllegalArgumentException if the action is not {@link Action#SET_TITLE},
|
||||
* {@link Action#SET_SUBTITLE} or {@link Action#SET_ACTION_BAR}
|
||||
*/
|
||||
public TitlePacket(@NotNull Action action, @NotNull Component payload) {
|
||||
Check.argCondition(action != SET_TITLE && action != SET_SUBTITLE && action != SET_ACTION_BAR, "Invalid action type");
|
||||
this.action = action;
|
||||
this.payload = payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new title packet from a clear or reset action.
|
||||
*
|
||||
* @param action the action
|
||||
* @throws IllegalArgumentException if the action is not {@link Action#RESET},
|
||||
* or {@link Action#HIDE}
|
||||
*/
|
||||
public TitlePacket(@NotNull Action action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new title packet for {@link Action#SET_TIMES_AND_DISPLAY}.
|
||||
*
|
||||
* @param fadeIn the fade in time
|
||||
* @param stay the stay time
|
||||
* @param fadeOut the fade out time
|
||||
*/
|
||||
public TitlePacket(int fadeIn, int stay, int fadeOut) {
|
||||
this.action = SET_TIMES_AND_DISPLAY;
|
||||
this.fadeIn = fadeIn;
|
||||
this.stay = stay;
|
||||
this.fadeOut = fadeOut;
|
||||
}
|
||||
|
||||
public TitlePacket() {
|
||||
this(SET_TITLE, Component.empty());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeVarInt(action.ordinal());
|
||||
|
||||
switch (action) {
|
||||
case SET_TITLE:
|
||||
case SET_SUBTITLE:
|
||||
case SET_ACTION_BAR:
|
||||
writer.writeComponent(payload);
|
||||
break;
|
||||
case SET_TIMES_AND_DISPLAY:
|
||||
writer.writeInt(fadeIn);
|
||||
writer.writeInt(stay);
|
||||
writer.writeInt(fadeOut);
|
||||
break;
|
||||
case HIDE:
|
||||
case RESET:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
action = Action.values()[reader.readVarInt()];
|
||||
switch (action) {
|
||||
case SET_TITLE:
|
||||
case SET_SUBTITLE:
|
||||
case SET_ACTION_BAR:
|
||||
payload = reader.readComponent(Integer.MAX_VALUE);
|
||||
break;
|
||||
|
||||
case SET_TIMES_AND_DISPLAY:
|
||||
fadeIn = reader.readInt();
|
||||
stay = reader.readInt();
|
||||
fadeOut = reader.readInt();
|
||||
|
||||
case HIDE:
|
||||
case RESET:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return ServerPacketIdentifier.TITLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Collection<Component> components() {
|
||||
if (action == SET_TITLE || action == SET_SUBTITLE || action == SET_ACTION_BAR) {
|
||||
return Collections.singleton(payload);
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ServerPacket copyWithOperator(@NotNull UnaryOperator<Component> operator) {
|
||||
if (action == SET_TITLE || action == SET_SUBTITLE || action == SET_ACTION_BAR) {
|
||||
return new TitlePacket(action, operator.apply(payload));
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public enum Action {
|
||||
SET_TITLE,
|
||||
SET_SUBTITLE,
|
||||
SET_ACTION_BAR,
|
||||
SET_TIMES_AND_DISPLAY,
|
||||
HIDE,
|
||||
RESET
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a collection of title packets from an Adventure title.
|
||||
*
|
||||
* @param title the title
|
||||
* @return the packets
|
||||
*/
|
||||
public static Collection<TitlePacket> of(Title title) {
|
||||
List<TitlePacket> packets = new ArrayList<>(4);
|
||||
|
||||
// base packets
|
||||
packets.add(new TitlePacket(SET_TITLE, title.title()));
|
||||
packets.add(new TitlePacket(SET_SUBTITLE, title.subtitle()));
|
||||
|
||||
// times packet
|
||||
Title.Times times = title.times();
|
||||
if (times != null) {
|
||||
packets.add(new TitlePacket(TickUtils.fromDuration(times.fadeIn(), TickUtils.CLIENT_TICK_MS),
|
||||
TickUtils.fromDuration(times.stay(), TickUtils.CLIENT_TICK_MS),
|
||||
TickUtils.fromDuration(times.fadeOut(), TickUtils.CLIENT_TICK_MS)));
|
||||
}
|
||||
|
||||
return packets;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user