mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 14:38:26 +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.entity.Player;
|
||||||
import net.minestom.server.message.ChatPosition;
|
import net.minestom.server.message.ChatPosition;
|
||||||
import net.minestom.server.message.Messenger;
|
import net.minestom.server.message.Messenger;
|
||||||
import net.minestom.server.network.packet.server.play.PlayerListHeaderAndFooterPacket;
|
import net.minestom.server.network.packet.server.play.*;
|
||||||
import net.minestom.server.network.packet.server.play.TitlePacket;
|
|
||||||
import net.minestom.server.utils.PacketUtils;
|
import net.minestom.server.utils.PacketUtils;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@ -52,7 +51,7 @@ public interface PacketGroupingAudience extends ForwardingAudience {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
default void sendActionBar(@NotNull Component message) {
|
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
|
@Override
|
||||||
@ -62,18 +61,18 @@ public interface PacketGroupingAudience extends ForwardingAudience {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
default void showTitle(@NotNull Title title) {
|
default void showTitle(@NotNull Title title) {
|
||||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new TitlePacket(TitlePacket.Action.SET_TITLE, title.title()));
|
PacketUtils.sendGroupedPacket(this.getPlayers(), new SetTitleTextPacket(title.title()));
|
||||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new TitlePacket(TitlePacket.Action.SET_SUBTITLE, title.subtitle()));
|
PacketUtils.sendGroupedPacket(this.getPlayers(), new SetTitleSubTitlePacket(title.subtitle()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default void clearTitle() {
|
default void clearTitle() {
|
||||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new TitlePacket(TitlePacket.Action.HIDE));
|
PacketUtils.sendGroupedPacket(this.getPlayers(), new ClearTitlesPacket());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default void resetTitle() {
|
default void resetTitle() {
|
||||||
PacketUtils.sendGroupedPacket(this.getPlayers(), new TitlePacket(TitlePacket.Action.RESET));
|
PacketUtils.sendGroupedPacket(this.getPlayers(), new ClearTitlesPacket(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -904,20 +904,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
playerConnection.sendPacket(packet);
|
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.
|
* Sends a title and subtitle message.
|
||||||
*
|
*
|
||||||
@ -969,17 +955,20 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showTitle(@NotNull Title title) {
|
public void showTitle(@NotNull Title title) {
|
||||||
Collection<TitlePacket> packet = TitlePacket.of(Title.title(title.title(), title.subtitle(), title.times()));
|
playerConnection.sendPacket(new SetTitleTextPacket(title.title()));
|
||||||
|
playerConnection.sendPacket(new SetTitleSubTitlePacket(title.subtitle()));
|
||||||
for (TitlePacket titlePacket : packet) {
|
final var times = title.times();
|
||||||
playerConnection.sendPacket(titlePacket);
|
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
|
@Override
|
||||||
public void sendActionBar(@NotNull Component message) {
|
public void sendActionBar(@NotNull Component message) {
|
||||||
TitlePacket titlePacket = new TitlePacket(TitlePacket.Action.SET_ACTION_BAR, message);
|
playerConnection.sendPacket(new ActionBarPacket(message));
|
||||||
playerConnection.sendPacket(titlePacket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -993,31 +982,17 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public void sendTitleTime(int fadeIn, int stay, int fadeOut) {
|
public void sendTitleTime(int fadeIn, int stay, int fadeOut) {
|
||||||
TitlePacket titlePacket = new TitlePacket(fadeIn, stay, fadeOut);
|
playerConnection.sendPacket(new SetTitleTimePacket(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void resetTitle() {
|
public void resetTitle() {
|
||||||
TitlePacket titlePacket = new TitlePacket(TitlePacket.Action.RESET);
|
playerConnection.sendPacket(new ClearTitlesPacket(true));
|
||||||
playerConnection.sendPacket(titlePacket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearTitle() {
|
public void clearTitle() {
|
||||||
TitlePacket titlePacket = new TitlePacket(TitlePacket.Action.HIDE);
|
playerConnection.sendPacket(new ClearTitlesPacket());
|
||||||
playerConnection.sendPacket(titlePacket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -11,6 +11,13 @@ public class ActionBarPacket implements ServerPacket {
|
|||||||
|
|
||||||
public Component actionBarText = Component.empty();
|
public Component actionBarText = Component.empty();
|
||||||
|
|
||||||
|
public ActionBarPacket() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ActionBarPacket(Component actionBarText) {
|
||||||
|
this.actionBarText = actionBarText;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(@NotNull BinaryReader reader) {
|
public void read(@NotNull BinaryReader reader) {
|
||||||
this.actionBarText = reader.readComponent();
|
this.actionBarText = reader.readComponent();
|
||||||
|
@ -10,6 +10,13 @@ public class ClearTitlesPacket implements ServerPacket {
|
|||||||
|
|
||||||
public boolean reset;
|
public boolean reset;
|
||||||
|
|
||||||
|
public ClearTitlesPacket() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClearTitlesPacket(boolean reset) {
|
||||||
|
this.reset = reset;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(@NotNull BinaryReader reader) {
|
public void read(@NotNull BinaryReader reader) {
|
||||||
this.reset = reader.readBoolean();
|
this.reset = reader.readBoolean();
|
||||||
|
@ -11,6 +11,13 @@ public class SetTitleSubTitlePacket implements ServerPacket {
|
|||||||
|
|
||||||
public Component subtitle = Component.empty();
|
public Component subtitle = Component.empty();
|
||||||
|
|
||||||
|
public SetTitleSubTitlePacket() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public SetTitleSubTitlePacket(Component subtitle) {
|
||||||
|
this.subtitle = subtitle;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(@NotNull BinaryReader reader) {
|
public void read(@NotNull BinaryReader reader) {
|
||||||
this.subtitle = reader.readComponent();
|
this.subtitle = reader.readComponent();
|
||||||
|
@ -7,10 +7,17 @@ import net.minestom.server.utils.binary.BinaryReader;
|
|||||||
import net.minestom.server.utils.binary.BinaryWriter;
|
import net.minestom.server.utils.binary.BinaryWriter;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class SetTitleTextPacker implements ServerPacket {
|
public class SetTitleTextPacket implements ServerPacket {
|
||||||
|
|
||||||
public Component title = Component.empty();
|
public Component title = Component.empty();
|
||||||
|
|
||||||
|
public SetTitleTextPacket() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public SetTitleTextPacket(Component title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(@NotNull BinaryReader reader) {
|
public void read(@NotNull BinaryReader reader) {
|
||||||
this.title = reader.readComponent();
|
this.title = reader.readComponent();
|
@ -12,6 +12,15 @@ public class SetTitleTimePacket implements ServerPacket {
|
|||||||
public int stay;
|
public int stay;
|
||||||
public int fadeOut;
|
public int fadeOut;
|
||||||
|
|
||||||
|
public SetTitleTimePacket() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public SetTitleTimePacket(int fadeIn, int stay, int fadeOut) {
|
||||||
|
this.fadeIn = fadeIn;
|
||||||
|
this.stay = stay;
|
||||||
|
this.fadeOut = fadeOut;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(@NotNull BinaryReader reader) {
|
public void read(@NotNull BinaryReader reader) {
|
||||||
this.fadeIn = reader.readInt();
|
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