Use PersistentDataCContainer instead of NBT, change the system of WayPoints with Djikstra Algorithm

This commit is contained in:
Guillaume 2022-04-28 13:43:33 +02:00
parent b92125c039
commit 41720d9048
3 changed files with 164 additions and 9 deletions

View File

@ -1,7 +1,5 @@
package net.Indyuce.mmocore.gui;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.NBTItem;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerActivity;
import net.Indyuce.mmocore.api.player.PlayerData;
@ -15,13 +13,16 @@ import net.Indyuce.mmocore.waypoint.Waypoint;
import net.Indyuce.mmocore.waypoint.WaypointOption;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class WaypointViewer extends EditableInventory {
public WaypointViewer() {
@ -103,13 +104,16 @@ public class WaypointViewer extends EditableInventory {
return locked.display(inv, n);
// Waypoints are not linked
if (inv.current != null && !inv.current.hasDestination(waypoint))
if (inv.current != null && inv.current.getPath(waypoint)==null)
return notLinked.display(inv, n);
// Not dynamic waypoint
if (inv.current == null && !waypoint.hasOption(WaypointOption.DYNAMIC))
return notDynamic.display(inv, n);
//Dynamic waypoint
if(waypoint.hasOption(WaypointOption.DYNAMIC)&&inv.current==null)
// Stellium cost
if (waypoint.getCost(inv.current == null ? CostType.DYNAMIC_USE : CostType.NORMAL_USE) > inv.getPlayerData().getStellium())
return noStellium.display(inv, n);
@ -129,7 +133,11 @@ public class WaypointViewer extends EditableInventory {
// If a player can teleport to another waypoint given his location
Waypoint waypoint = inv.waypoints.get(inv.page * inv.getEditable().getByFunction("waypoint").getSlots().size() + n);
return NBTItem.get(disp).addTag(new ItemTag("waypointId", waypoint.getId())).toItem();
ItemMeta meta=disp.getItemMeta();
PersistentDataContainer container=meta.getPersistentDataContainer();
container.set(new NamespacedKey(MMOCore.plugin,"waypointId"),PersistentDataType.STRING,waypoint.getId());
disp.setItemMeta(meta);
return disp;
}
@Override
@ -149,6 +157,7 @@ public class WaypointViewer extends EditableInventory {
public class WaypointViewerInventory extends GeneratedInventory {
private final List<Waypoint> waypoints = new ArrayList<>(MMOCore.plugin.waypointManager.getAll());
private final Waypoint current;
private final ArrayList<Waypoint.PathInfo> paths;
private final CostType waypointCostType;
private int page;
@ -157,6 +166,7 @@ public class WaypointViewer extends EditableInventory {
super(playerData, editable);
this.current = current;
paths=current!=null? current.getAllPath() : null;
this.waypointCostType = current == null ? CostType.DYNAMIC_USE : CostType.NORMAL_USE;
}
@ -180,7 +190,9 @@ public class WaypointViewer extends EditableInventory {
}
if (item.getFunction().equals("waypoint")) {
String tag = NBTItem.get(event.getCurrentItem()).getString("waypointId");
PersistentDataContainer container =event.getCurrentItem().getItemMeta().getPersistentDataContainer();
String tag =container.get(new NamespacedKey(MMOCore.plugin,"wayPointId"), PersistentDataType.STRING);
if (tag.equals(""))
return;

View File

@ -1,11 +1,13 @@
package net.Indyuce.mmocore.waypoint;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.player.Unlockable;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.craftbukkit.libs.jline.internal.Nullable;
import org.bukkit.entity.Player;
import java.util.*;
@ -21,7 +23,7 @@ public class Waypoint implements Unlockable {
* <p>
* If it's empty it can access any waypoint.
*/
private final Set<String> destinations = new HashSet<>();
private final Map<String, Integer> destinations = new HashMap<>();
/**
* Waypoint options saved here.
@ -46,7 +48,14 @@ public class Waypoint implements Unlockable {
for (WaypointOption option : WaypointOption.values())
options.put(option, config.getBoolean("option." + option.getPath(), option.getDefaultValue()));
destinations.addAll(config.getStringList("linked"));
//We load all the linked WayPoints
ConfigurationSection section=config.getConfigurationSection("linked");
for(String key: section.getKeys(false)) {
destinations.put(key,config.getInt(key));
}
//destinations.addAll(config.getStringList("linked"));
}
public String getId() {
@ -61,14 +70,90 @@ public class Waypoint implements Unlockable {
return loc;
}
/**
* @param other Another waypoint
* @return If any player standing on that waypoint can teleport to given waypoint
*/
@Deprecated
public boolean hasDestination(Waypoint other) {
return destinations.isEmpty() || destinations.contains(other.getId());
return destinations.isEmpty() || destinations.keySet().contains(other.getId());
}
/**
* Checks directly if the waypoint is directly linked to the current one
*
* @return Integer.POSITIVE_INFINITY if the way point is not linked
*/
public int getSimpleCostDestination(Waypoint waypoint) {
if (!destinations.keySet().contains(waypoint.getId()))
return Integer.MAX_VALUE;
return destinations.get(waypoint.getId());
}
public ArrayList<PathInfo> getAllPath() {
//All the WayPoints that have been registered
ArrayList<Waypoint> checkedPoints = new ArrayList<>();
//All the path
ArrayList<PathInfo> paths = new ArrayList();
ArrayList<PathInfo> pointsToCheck = new ArrayList<>();
pointsToCheck.add(new PathInfo(this));
while (pointsToCheck.size() != 0) {
PathInfo checked = pointsToCheck.get(0);
pointsToCheck.remove(0);
paths.add(checked);
checkedPoints.add(checked.getFinalWaypoint());
for (String wayPointId : checked.getFinalWaypoint().destinations.keySet()) {
Waypoint toCheck = MMOCore.plugin.waypointManager.get(wayPointId);
if (!checkedPoints.contains(toCheck)) {
PathInfo toCheckInfo = checked.addWayPoint(toCheck);
//We keep pointsToCheck ordered
pointsToCheck = toCheckInfo.addInOrder(pointsToCheck);
}
}
}
return paths;
}
@Nullable
public PathInfo getPath(Waypoint targetWaypoint) {
//All the WayPoints that have been registered
ArrayList<Waypoint> checkedPoints = new ArrayList<>();
//All the path
ArrayList<PathInfo> paths = new ArrayList();
ArrayList<PathInfo> pointsToCheck = new ArrayList<>();
pointsToCheck.add(new PathInfo(this));
while (pointsToCheck.size() != 0) {
PathInfo checked = pointsToCheck.get(0);
pointsToCheck.remove(0);
paths.add(checked);
checkedPoints.add(checked.getFinalWaypoint());
if(checked.getFinalWaypoint().equals(targetWaypoint))
return checked;
for (String wayPointId : checked.getFinalWaypoint().destinations.keySet()) {
Waypoint toCheck = MMOCore.plugin.waypointManager.get(wayPointId);
if (!checkedPoints.contains(toCheck)) {
PathInfo toCheckInfo = checked.addWayPoint(toCheck);
//We keep pointsToCheck ordered
pointsToCheck = toCheckInfo.addInOrder(pointsToCheck);
}
}
}
//If no path has been found we return null
return null;
}
public double getCost(CostType type) {
return costs.getOrDefault(type, 0d);
}
@ -118,4 +203,61 @@ public class Waypoint implements Unlockable {
return new Location(world, x, y, z, yaw, pitch);
}
public class PathInfo {
private final ArrayList<Waypoint> waypoints;
private final int cost;
public ArrayList<Waypoint> getWaypoints() {
return waypoints;
}
public int getCost() {
return cost;
}
public PathInfo(Waypoint waypoint) {
this.waypoints = (ArrayList<Waypoint>) Arrays.asList(waypoint);
cost = 0;
}
public ArrayList<PathInfo> addInOrder(ArrayList<PathInfo> pathInfos) {
int index = 0;
while (index < pathInfos.size()) {
if (cost < pathInfos.get(index).cost) {
pathInfos.set(index, this);
return pathInfos;
}
}
//If index==pathInfos.size() we add the waypoint at the end
pathInfos.add(this);
return pathInfos;
}
public PathInfo(ArrayList<Waypoint> waypoints, int cost) {
this.waypoints = waypoints;
this.cost = cost;
}
public PathInfo addWayPoint(Waypoint waypoint) {
Validate.isTrue(!waypoints.contains(waypoint), "You can't create cyclic path");
ArrayList<Waypoint> newWaypoints = new ArrayList<>();
newWaypoints.addAll(waypoints);
newWaypoints.add(waypoint);
int cost=this.cost+getFinalWaypoint().getSimpleCostDestination(waypoint);
return new PathInfo(newWaypoints,cost);
}
public Waypoint getInitialWaypoint() {
return waypoints.get(0);
}
public Waypoint getFinalWaypoint() {
return waypoints.get(waypoints.size() - 1);
}
}
}

View File

@ -61,6 +61,7 @@ items:
lore:
- '&7You can teleport to this waypoint.'
- '&7 Intermediary waypoints : {intermediary_waypoints}'
- '&7Click to teleport for &b{current_cost} &7Stellium.'
next: