Fix notifications

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-12-01 22:41:59 +01:00
parent 9083f87153
commit 22a8ccabfa
6 changed files with 47 additions and 98 deletions

View File

@ -9,7 +9,6 @@ import net.minestom.server.utils.PacketUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Date;
import java.util.List;
import java.util.Set;
@ -305,7 +304,7 @@ public class Advancement {
final String parentIdentifier = parent != null ? parent.getIdentifier() : null;
AdvancementsPacket.Advancement adv = new AdvancementsPacket.Advancement(parentIdentifier, toDisplayData(),
List.of(criteria.criterionIdentifier()),
List.of(criteria.criterionIdentifier()));
List.of(new AdvancementsPacket.Requirement(List.of(criteria.criterionIdentifier()))));
return new AdvancementsPacket.AdvancementMapping(getIdentifier(), adv);
}
@ -335,27 +334,16 @@ public class Advancement {
}
protected void updateCriteria() {
Long achievedDate = achieved ? new Date(System.currentTimeMillis()).getTime() : null;
final Long achievedDate = achieved ? System.currentTimeMillis() : null;
final var progress = new AdvancementsPacket.CriterionProgress(achievedDate);
this.criteria = new AdvancementsPacket.Criteria(identifier, progress);
}
private int getFlags() {
byte result = 0;
if (background != null) {
result |= 0x1;
}
if (hasToast()) {
result |= 0x2;
}
if (isHidden()) {
result |= 0x4;
}
if (background != null) result |= 0x1;
if (hasToast()) result |= 0x2;
if (isHidden()) result |= 0x4;
return result;
}
}

View File

@ -4,7 +4,6 @@ import net.minestom.server.Viewable;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.play.AdvancementsPacket;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.advancement.AdvancementUtils;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -37,10 +36,8 @@ public class AdvancementTab implements Viewable {
protected AdvancementTab(@NotNull String rootIdentifier, @NotNull AdvancementRoot root) {
this.root = root;
cacheAdvancement(rootIdentifier, root, null);
this.removePacket = AdvancementUtils.getRemovePacket(new String[]{rootIdentifier});
this.removePacket = new AdvancementsPacket(false, List.of(), List.of(rootIdentifier), List.of());
}
/**

View File

@ -7,48 +7,20 @@ import net.minestom.server.item.Material;
import org.jetbrains.annotations.NotNull;
/**
* Represents a message which can be send using the {@link NotificationCenter}.
* Represents a message which can be sent using the {@link NotificationCenter}.
*/
public class Notification {
private final Component title;
private final FrameType frameType;
private final ItemStack icon;
public Notification(@NotNull Component title, @NotNull FrameType frameType, @NotNull ItemStack icon) {
this.title = title;
this.frameType = frameType;
this.icon = icon;
}
public record Notification(@NotNull Component title, @NotNull FrameType frameType, @NotNull ItemStack icon) {
public Notification(@NotNull Component title, @NotNull FrameType frameType, @NotNull Material icon) {
this(title, frameType, ItemStack.of(icon));
}
/**
* Gets the title of the notification.
*
* @return the notification title
*/
public Component getTitle() {
@Deprecated
public @NotNull Component getTitle() {
return title;
}
/**
* Gets the {@link FrameType} of the notification.
*
* @return the notification frame type
*/
@Deprecated
public @NotNull FrameType getFrameType() {
return frameType;
}
/**
* Gets the icon of the notification.
*
* @return the notification icon
*/
protected @NotNull ItemStack getIcon() {
return icon;
}
}

View File

@ -3,10 +3,8 @@ package net.minestom.server.advancements.notifications;
import net.kyori.adventure.text.Component;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.play.AdvancementsPacket;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.advancement.AdvancementUtils;
import org.jetbrains.annotations.NotNull;
import java.sql.Date;
import java.util.Collection;
import java.util.List;
@ -17,9 +15,9 @@ import java.util.List;
* <p>
* You can simply create a {@link Notification} object and call {@link #send(Notification, Player)}.
*/
public class NotificationCenter {
public final class NotificationCenter {
private static final String IDENTIFIER = "minestom:notification";
private static final AdvancementsPacket REMOVE_PACKET = new AdvancementsPacket(false, List.of(), List.of(IDENTIFIER), List.of());
/**
* Can't create an instance, use the static methods instead.
@ -33,12 +31,9 @@ public class NotificationCenter {
* @param notification the {@link Notification} to send
* @param player the player to send the notification to
*/
public static void send(Notification notification, Player player) {
final PlayerConnection playerConnection = player.getPlayerConnection();
playerConnection.sendPacket(getCreatePacket(notification));
playerConnection.sendPacket(AdvancementUtils.getRemovePacket(new String[]{IDENTIFIER}));
public static void send(@NotNull Notification notification, @NotNull Player player) {
player.sendPacket(createPacket(notification));
player.sendPacket(REMOVE_PACKET);
}
/**
@ -47,7 +42,7 @@ public class NotificationCenter {
* @param notification the {@link Notification} to send
* @param players the collection of players to send the notification to
*/
public static void send(Notification notification, Collection<Player> players) {
public static void send(@NotNull Notification notification, @NotNull Collection<Player> players) {
// Can't use PacketWriterUtils because we need the packets to come in the correct order
players.forEach(player -> send(notification, player));
}
@ -58,19 +53,20 @@ public class NotificationCenter {
* @param notification the notification
* @return the packet used to show the Toast
*/
private static AdvancementsPacket getCreatePacket(Notification notification) {
private static AdvancementsPacket createPacket(Notification notification) {
// For An advancement to be shown, it must have all of its criteria achieved (progress 100%)
// Create a Criteria that we can set to 100% achieved.
final var displayData = new AdvancementsPacket.DisplayData(
notification.getTitle(), Component.text("Articdive was here. #Minestom"),
notification.getIcon(), notification.getFrameType(),
notification.title(), Component.text("Articdive was here. #Minestom"),
notification.icon(), notification.frameType(),
0x6, null, 0f, 0f);
final var criteria = new AdvancementsPacket.Criteria("minestom:some_criteria",
new AdvancementsPacket.CriterionProgress(new Date(System.currentTimeMillis()).getTime()));
new AdvancementsPacket.CriterionProgress(System.currentTimeMillis()));
final var advancement = new AdvancementsPacket.Advancement(null, displayData,
List.of(criteria.criterionIdentifier()), List.of(criteria.criterionIdentifier()));
List.of(criteria.criterionIdentifier()),
List.of(new AdvancementsPacket.Requirement(List.of(criteria.criterionIdentifier()))));
final var mapping = new AdvancementsPacket.AdvancementMapping(IDENTIFIER, advancement);
final var progressMapping = new AdvancementsPacket.ProgressMapping(IDENTIFIER,

View File

@ -57,24 +57,42 @@ public record AdvancementsPacket(boolean reset, @NotNull List<AdvancementMapping
}
public record Advancement(@Nullable String parentIdentifier, @Nullable DisplayData displayData,
@NotNull List<String> criterions,
@NotNull List<String> requirements) implements Writeable {
@NotNull List<String> criteria,
@NotNull List<Requirement> requirements) implements Writeable {
public Advancement {
criteria = List.copyOf(criteria);
requirements = List.copyOf(requirements);
}
public Advancement(BinaryReader reader) {
this(reader.readBoolean() ? reader.readSizedString() : null,
reader.readBoolean() ? new DisplayData(reader) : null,
reader.readVarIntList(BinaryReader::readSizedString),
reader.readVarIntList(BinaryReader::readSizedString));
reader.readVarIntList(Requirement::new));
}
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeBoolean(parentIdentifier != null);
if (parentIdentifier != null) writer.writeSizedString(parentIdentifier);
writer.writeBoolean(displayData != null);
if (displayData != null) writer.write(displayData);
writer.writeVarIntList(criteria, BinaryWriter::writeSizedString);
writer.writeVarIntList(requirements, BinaryWriter::write);
}
}
writer.writeVarIntList(criterions, BinaryWriter::writeSizedString);
public record Requirement(@NotNull List<String> requirements) implements Writeable {
public Requirement {
requirements = List.copyOf(requirements);
}
public Requirement(BinaryReader reader) {
this(reader.readVarIntList(BinaryReader::readSizedString));
}
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeVarIntList(requirements, BinaryWriter::writeSizedString);
}
}

View File

@ -1,22 +0,0 @@
package net.minestom.server.utils.advancement;
import net.minestom.server.network.packet.server.play.AdvancementsPacket;
import java.util.List;
public final class AdvancementUtils {
private AdvancementUtils() {
}
/**
* Gets an {@link AdvancementsPacket} which remove the specified identifiers.
*
* @param identifiers the identifiers to remove
* @return the packet to remove all the identifiers
*/
public static AdvancementsPacket getRemovePacket(String[] identifiers) {
return new AdvancementsPacket(false, List.of(), List.of(identifiers), List.of());
}
}