Added AdvancementTab#getTabs

This commit is contained in:
Felix Cravic 2020-08-09 14:13:01 +02:00
parent d3d06e907c
commit 1f2451f0b2
3 changed files with 52 additions and 9 deletions

View File

@ -6,7 +6,6 @@ import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material; import net.minestom.server.item.Material;
import net.minestom.server.network.packet.server.play.AdvancementsPacket; import net.minestom.server.network.packet.server.play.AdvancementsPacket;
import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.PacketUtils;
import java.util.Date; import java.util.Date;
@ -336,7 +335,8 @@ public class Advancement {
updateCriteria(); updateCriteria();
if (tab != null) { if (tab != null) {
tab.createBuffer = PacketUtils.writePacket(tab.createPacket()); // Update the tab cached packet
tab.updatePacket();
final ByteBuf createBuffer = tab.createBuffer; final ByteBuf createBuffer = tab.createBuffer;
final ByteBuf removeBuffer = tab.removeBuffer; final ByteBuf removeBuffer = tab.removeBuffer;

View File

@ -16,6 +16,8 @@ import java.util.*;
*/ */
public class AdvancementTab implements Viewable { public class AdvancementTab implements Viewable {
private static Map<Player, Set<AdvancementTab>> playerTabMap = new HashMap<>();
private Set<Player> viewers = new HashSet<>(); private Set<Player> viewers = new HashSet<>();
private AdvancementRoot root; private AdvancementRoot root;
@ -38,6 +40,16 @@ public class AdvancementTab implements Viewable {
this.removeBuffer = PacketUtils.writePacket(removePacket); this.removeBuffer = PacketUtils.writePacket(removePacket);
} }
/**
* Get all the tabs of a viewer
*
* @param player the player to get the tabs from
* @return all the advancement tabs that the player sees
*/
public static Set<AdvancementTab> getTabs(Player player) {
return playerTabMap.getOrDefault(player, null);
}
/** /**
* Get the root advancement of this tab * Get the root advancement of this tab
* *
@ -116,7 +128,7 @@ public class AdvancementTab implements Viewable {
} }
@Override @Override
public boolean addViewer(Player player) { public synchronized boolean addViewer(Player player) {
final boolean result = viewers.add(player); final boolean result = viewers.add(player);
if (!result) { if (!result) {
return false; return false;
@ -127,11 +139,13 @@ public class AdvancementTab implements Viewable {
// Send the tab to the player // Send the tab to the player
playerConnection.sendPacket(createBuffer, true); playerConnection.sendPacket(createBuffer, true);
addPlayer(player);
return true; return true;
} }
@Override @Override
public boolean removeViewer(Player player) { public synchronized boolean removeViewer(Player player) {
if (!isViewer(player)) { if (!isViewer(player)) {
return false; return false;
} }
@ -141,6 +155,8 @@ public class AdvancementTab implements Viewable {
// Remove the tab // Remove the tab
playerConnection.sendPacket(removeBuffer, true); playerConnection.sendPacket(removeBuffer, true);
removePlayer(player);
return viewers.remove(player); return viewers.remove(player);
} }
@ -149,4 +165,30 @@ public class AdvancementTab implements Viewable {
return viewers; return viewers;
} }
/**
* Add the tab to the player set
*
* @param player the player
*/
private void addPlayer(Player player) {
Set<AdvancementTab> tabs = playerTabMap.computeIfAbsent(player, p -> new HashSet<>());
tabs.add(this);
}
/**
* Remove the tab from the player set
*
* @param player the player
*/
private void removePlayer(Player player) {
if (!playerTabMap.containsKey(player)) {
return;
}
Set<AdvancementTab> tabs = playerTabMap.get(player);
tabs.remove(this);
if (tabs.isEmpty()) {
playerTabMap.remove(player);
}
}
} }

View File

@ -76,6 +76,7 @@ public abstract class EntityCreature extends LivingEntity {
return; return;
} }
// Goal selectors
{ {
// Supplier used to get the next goal selector which should start // Supplier used to get the next goal selector which should start
// (null if not found) // (null if not found)
@ -111,7 +112,7 @@ public abstract class EntityCreature extends LivingEntity {
this.currentGoalSelector.start(); this.currentGoalSelector.start();
} }
// Execute tick for the goal selector // Execute tick for the current goal selector
if (currentGoalSelector != null) { if (currentGoalSelector != null) {
currentGoalSelector.tick(time); currentGoalSelector.tick(time);
} }
@ -120,7 +121,7 @@ public abstract class EntityCreature extends LivingEntity {
// Path finding // Path finding
{ {
pathLock.lock(); this.pathLock.lock();
path = pathFinder.updatePathFor(pathingEntity); path = pathFinder.updatePathFor(pathingEntity);
if (path != null) { if (path != null) {
final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED); final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED);
@ -132,7 +133,7 @@ public abstract class EntityCreature extends LivingEntity {
this.pathFinder.reset(); this.pathFinder.reset();
} }
} }
pathLock.unlock(); this.pathLock.unlock();
} }
super.update(time); super.update(time);
@ -337,7 +338,7 @@ public abstract class EntityCreature extends LivingEntity {
return false; return false;
} }
pathLock.lock(); this.pathLock.lock();
this.pathFinder.reset(); this.pathFinder.reset();
if (position == null) { if (position == null) {
return false; return false;
@ -362,7 +363,7 @@ public abstract class EntityCreature extends LivingEntity {
} catch (NullPointerException | IndexOutOfBoundsException e) { } catch (NullPointerException | IndexOutOfBoundsException e) {
this.path = null; this.path = null;
} }
pathLock.unlock(); this.pathLock.unlock();
final boolean success = path != null; final boolean success = path != null;