Advancements packet are now cached

This commit is contained in:
Felix Cravic 2020-08-05 14:03:52 +02:00
parent 2ab7427b94
commit b066e1b78f
5 changed files with 53 additions and 70 deletions

View File

@ -237,7 +237,7 @@ public class PlayerInit {
tab.addViewer(player);
advancement.setTitle(ColoredText.of("test ttlechange"));
root.setTitle(ColoredText.of("test ttlechange"));
//player.getPlayerConnection().sendPacket(tab.removePacket());
}
});

View File

@ -1,9 +1,12 @@
package net.minestom.server.advancements;
import io.netty.buffer.ByteBuf;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.network.packet.server.play.AdvancementsPacket;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.PacketUtils;
public class Advancement {
@ -149,42 +152,20 @@ public class Advancement {
return displayData;
}
protected AdvancementsPacket getUpdatePacket() {
AdvancementsPacket advancementsPacket = new AdvancementsPacket();
advancementsPacket.resetAdvancements = false;
AdvancementsPacket.AdvancementMapping mapping = new AdvancementsPacket.AdvancementMapping();
{
AdvancementsPacket.Advancement adv = new AdvancementsPacket.Advancement();
mapping.key = getIdentifier();
mapping.value = adv;
final Advancement parent = getParent();
if (parent != null) {
final String parentIdentifier = parent.getIdentifier();
adv.parentIdentifier = parentIdentifier;
}
adv.displayData = toDisplayData();
adv.criterions = new String[]{};
adv.requirements = new AdvancementsPacket.Requirement[]{};
}
advancementsPacket.identifiersToRemove = new String[]{};
advancementsPacket.advancementMappings = new AdvancementsPacket.AdvancementMapping[]{mapping};
advancementsPacket.progressMappings = new AdvancementsPacket.ProgressMapping[]{};
return advancementsPacket;
}
/**
* Update this advancement value when a field is modified
*/
protected void update() {
if (tab != null) {
// TODO: how to update an advancement without clearing everything
//final AdvancementsPacket packet = getUpdatePacket();
//tab.sendPacketToViewers(packet);
tab.createBuffer = PacketUtils.writePacket(tab.createPacket());
final ByteBuf createBuffer = tab.createBuffer;
final ByteBuf removeBuffer = tab.removeBuffer;
tab.getViewers().forEach(player -> {
final PlayerConnection playerConnection = player.getPlayerConnection();
playerConnection.sendPacket(removeBuffer, true);
playerConnection.sendPacket(createBuffer, true);
});
}
}

View File

@ -1,9 +1,12 @@
package net.minestom.server.advancements;
import io.netty.buffer.ByteBuf;
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.PacketUtils;
import net.minestom.server.utils.advancement.AdvancementUtils;
import net.minestom.server.utils.validate.Check;
import java.util.*;
@ -12,14 +15,20 @@ public class AdvancementTab implements Viewable {
private Set<Player> viewers = new HashSet<>();
private Advancement root;
// Advancement -> its parent
private Map<Advancement, Advancement> advancementMap = new HashMap<>();
// Packet cache, updated every time the tab changes
protected ByteBuf createBuffer;
// the packet used to clear the tab (used to remove it and to update an advancement)
// will never change (since the root identifier is always the same)
protected ByteBuf removeBuffer;
protected AdvancementTab(String rootIdentifier, Advancement root) {
this.root = root;
cacheAdvancement(rootIdentifier, root, null);
final AdvancementsPacket removePacket = AdvancementUtils.getRemovePacket(new String[]{rootIdentifier});
this.removeBuffer = PacketUtils.writePacket(removePacket);
}
public void createAdvancement(String identifier, Advancement advancement, Advancement parent) {
@ -33,7 +42,7 @@ public class AdvancementTab implements Viewable {
*
* @return the packet adding this advancement tab and all its advancements
*/
public AdvancementsPacket createPacket() {
protected AdvancementsPacket createPacket() {
AdvancementsPacket advancementsPacket = new AdvancementsPacket();
advancementsPacket.resetAdvancements = false;
@ -66,23 +75,6 @@ public class AdvancementTab implements Viewable {
return advancementsPacket;
}
/**
* Create a packet which remove the root advancement
* <p>
* This does in fact remove the whole advancement tab
*
* @return the packet which remove the root advancement
*/
public AdvancementsPacket removePacket() {
AdvancementsPacket advancementsPacket = new AdvancementsPacket();
advancementsPacket.resetAdvancements = false;
advancementsPacket.identifiersToRemove = new String[]{root.getIdentifier()};
advancementsPacket.advancementMappings = new AdvancementsPacket.AdvancementMapping[]{};
advancementsPacket.progressMappings = new AdvancementsPacket.ProgressMapping[]{};
return advancementsPacket;
}
private void cacheAdvancement(String identifier, Advancement advancement, Advancement parent) {
Check.stateCondition(advancement.getTab() != null,
"You tried to add an advancement already linked to a tab");
@ -116,7 +108,7 @@ public class AdvancementTab implements Viewable {
final PlayerConnection playerConnection = player.getPlayerConnection();
// Remove the tab
playerConnection.sendPacket(removePacket());
playerConnection.sendPacket(removeBuffer, true);
return viewers.remove(player);
}
@ -125,4 +117,5 @@ public class AdvancementTab implements Viewable {
public Set<Player> getViewers() {
return viewers;
}
}

View File

@ -4,6 +4,7 @@ import net.minestom.server.chat.ColoredText;
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 java.sql.Date;
import java.util.Collection;
@ -26,7 +27,7 @@ public class NotificationCenter {
playerConnection.sendPacket(getCreatePacket(notification));
playerConnection.sendPacket(getRemovePacket());
playerConnection.sendPacket(AdvancementUtils.getRemovePacket(new String[]{IDENTIFIER}));
}
public static void send(Notification notification, Collection<Player> players) {
@ -111,19 +112,4 @@ public class NotificationCenter {
return advancementsPacket;
}
/**
* Create the packet responsive for removing the advancement identifier
*
* @return the packet to remove the identifier
*/
private static AdvancementsPacket getRemovePacket() {
AdvancementsPacket advancementsPacket = new AdvancementsPacket();
advancementsPacket.resetAdvancements = false;
advancementsPacket.identifiersToRemove = new String[]{IDENTIFIER};
advancementsPacket.advancementMappings = new AdvancementsPacket.AdvancementMapping[]{};
advancementsPacket.progressMappings = new AdvancementsPacket.ProgressMapping[]{};
return advancementsPacket;
}
}

View File

@ -0,0 +1,23 @@
package net.minestom.server.utils.advancement;
import net.minestom.server.network.packet.server.play.AdvancementsPacket;
public class AdvancementUtils {
/**
* Get 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) {
AdvancementsPacket advancementsPacket = new AdvancementsPacket();
advancementsPacket.resetAdvancements = false;
advancementsPacket.identifiersToRemove = identifiers;
advancementsPacket.advancementMappings = new AdvancementsPacket.AdvancementMapping[]{};
advancementsPacket.progressMappings = new AdvancementsPacket.ProgressMapping[]{};
return advancementsPacket;
}
}