Changes in PlayerData for warp fonction and in WayPointViewer / gui/waypoints.yml

This commit is contained in:
Guillaume 2022-04-28 14:34:42 +02:00
parent 41720d9048
commit c8096765b4
4 changed files with 78 additions and 42 deletions

View File

@ -457,9 +457,9 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
* Teleports the player to a specific waypoint. This applies
* the stellium waypoint cost and plays the teleport animation.
*
* @param waypoint Target waypoint
* @param target Target waypoint
*/
public void warp(Waypoint waypoint, CostType costType) {
public void warp(Waypoint source, Waypoint target, CostType costType) {
/*
* This cooldown is only used internally to make sure the player is not
@ -467,8 +467,10 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
* player waypoints data
*/
setLastActivity(PlayerActivity.USE_WAYPOINT);
Validate.isTrue(source!=null||costType!=CostType.NORMAL_USE,"You must precise a source to use normal waypoint" );
final double cost = costType == CostType.DYNAMIC_USE ? target.getDynamicCost() : source.getCost(target);
final double cost = waypoint.getCost(costType);
giveStellium(-cost, PlayerResourceUpdateEvent.UpdateReason.USE_WAYPOINT);
new BukkitRunnable() {
@ -491,7 +493,7 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
MMOCore.plugin.configManager.getSimpleMessage("warping-comencing", "left", "" + ((120 - t) / 20)).send(getPlayer());
if (t++ >= 100) {
getPlayer().teleport(waypoint.getLocation());
getPlayer().teleport(target.getLocation());
getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 20, 1, false, false));
MMOCore.plugin.soundManager.getSound(SoundEvent.WARP_TELEPORT).playTo(getPlayer());
cancel();

View File

@ -22,7 +22,9 @@ import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WaypointViewer extends EditableInventory {
public WaypointViewer() {
@ -65,8 +67,8 @@ public class WaypointViewer extends EditableInventory {
}
public class WaypointItem extends SimplePlaceholderItem<WaypointViewerInventory> {
private final SimplePlaceholderItem noWaypoint, locked;
private final WaypointItemHandler availWaypoint, notLinked, notDynamic, noStellium;
private final SimplePlaceholderItem noWaypoint, locked,notLinked,notDynamic;
private final WaypointItemHandler availWaypoint,noStellium;
public WaypointItem(ConfigurationSection config) {
super(Material.BARRIER, config);
@ -80,8 +82,8 @@ public class WaypointViewer extends EditableInventory {
noWaypoint = new SimplePlaceholderItem(config.getConfigurationSection("no-waypoint"));
locked = new SimplePlaceholderItem(config.getConfigurationSection("locked"));
notLinked = new WaypointItemHandler(config.getConfigurationSection("not-a-destination"));
notDynamic = new WaypointItemHandler(config.getConfigurationSection("not-dynamic"));
notLinked = new SimplePlaceholderItem(config.getConfigurationSection("not-a-destination"));
notDynamic = new SimplePlaceholderItem(config.getConfigurationSection("not-dynamic"));
noStellium = new WaypointItemHandler(config.getConfigurationSection("not-enough-stellium"));
availWaypoint = new WaypointItemHandler(config.getConfigurationSection("display"));
}
@ -104,20 +106,23 @@ public class WaypointViewer extends EditableInventory {
return locked.display(inv, n);
// Waypoints are not linked
if (inv.current != null && inv.current.getPath(waypoint)==null)
if (inv.current != null && !inv.paths.containsKey(waypoint))
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())
//Dynamic waypoint
if (waypoint.hasOption(WaypointOption.DYNAMIC) && inv.current == null && waypoint.getDynamicCost() > inv.getPlayerData().getStellium())
return noStellium.display(inv, n);
//Normal cost
if (inv.current != null && inv.paths.get(waypoint).getCost() > inv.getPlayerData().getStellium())
return noStellium.display(inv, n);
return availWaypoint.display(inv, n);
}
}
@ -133,9 +138,9 @@ 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);
ItemMeta meta=disp.getItemMeta();
PersistentDataContainer container=meta.getPersistentDataContainer();
container.set(new NamespacedKey(MMOCore.plugin,"waypointId"),PersistentDataType.STRING,waypoint.getId());
ItemMeta meta = disp.getItemMeta();
PersistentDataContainer container = meta.getPersistentDataContainer();
container.set(new NamespacedKey(MMOCore.plugin, "waypointId"), PersistentDataType.STRING, waypoint.getId());
disp.setItemMeta(meta);
return disp;
}
@ -146,9 +151,10 @@ public class WaypointViewer extends EditableInventory {
Waypoint waypoint = inv.waypoints.get(inv.page * inv.getByFunction("waypoint").getSlots().size() + n);
holders.register("name", waypoint.getName());
holders.register("current_cost", decimal.format(waypoint.getCost(inv.waypointCostType)));
holders.register("normal_cost", decimal.format(waypoint.getCost(CostType.NORMAL_USE)));
holders.register("dynamic_cost", decimal.format(waypoint.getCost(CostType.DYNAMIC_USE)));
holders.register("current_cost", decimal.format(inv.waypointCostType.equals(CostType.DYNAMIC_USE) ? waypoint.getDynamicCost() : inv.paths.get(waypoint).getCost()));
holders.register("normal_cost", decimal.format(inv.paths.get(waypoint).getCost()));
holders.register("dynamic_cost", decimal.format(waypoint.getDynamicCost()));
holders.register("intermediary_waypoints", inv.paths.get(waypoint).displayIntermediaryWayPoints());
return holders;
}
@ -157,7 +163,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 Map<Waypoint, Waypoint.PathInfo> paths = new HashMap<>();
private final CostType waypointCostType;
private int page;
@ -166,7 +172,11 @@ public class WaypointViewer extends EditableInventory {
super(playerData, editable);
this.current = current;
paths=current!=null? current.getAllPath() : null;
if (current != null) {
for (Waypoint.PathInfo pathInfo : current.getAllPath())
paths.put(pathInfo.getFinalWaypoint(), pathInfo);
}
this.waypointCostType = current == null ? CostType.DYNAMIC_USE : CostType.NORMAL_USE;
}
@ -190,8 +200,8 @@ public class WaypointViewer extends EditableInventory {
}
if (item.getFunction().equals("waypoint")) {
PersistentDataContainer container =event.getCurrentItem().getItemMeta().getPersistentDataContainer();
String tag =container.get(new NamespacedKey(MMOCore.plugin,"wayPointId"), PersistentDataType.STRING);
PersistentDataContainer container = event.getCurrentItem().getItemMeta().getPersistentDataContainer();
String tag = container.get(new NamespacedKey(MMOCore.plugin, "wayPointId"), PersistentDataType.STRING);
if (tag.equals(""))
return;
@ -210,7 +220,7 @@ public class WaypointViewer extends EditableInventory {
}
// Waypoint does not have target as destination
if (current != null && !current.hasDestination(waypoint)) {
if (current != null && current.getPath(waypoint)==null) {
MMOCore.plugin.configManager.getSimpleMessage("cannot-teleport-to").send(player);
return;
}
@ -223,7 +233,8 @@ public class WaypointViewer extends EditableInventory {
// Stellium cost
CostType costType = current == null ? CostType.DYNAMIC_USE : CostType.NORMAL_USE;
double left = waypoint.getCost(costType) - playerData.getStellium();
double withdraw = current == null ? waypoint.getDynamicCost() : paths.get(waypoint).getCost();
double left = withdraw - playerData.getStellium();
if (left > 0) {
MMOCore.plugin.configManager.getSimpleMessage("not-enough-stellium", "more", decimal.format(left)).send(player);
return;
@ -233,7 +244,11 @@ public class WaypointViewer extends EditableInventory {
return;
player.closeInventory();
playerData.warp(waypoint, costType);
if (current == null)
playerData.warp(null, waypoint, costType);
else
playerData.warp(current, waypoint, costType);
}
}
}

View File

@ -33,8 +33,21 @@ public class Waypoint implements Unlockable {
/**
* Stellium cost for each action (0 being the default cost)
*/
private final double dynamicCost,setSpawnCost;
private final Map<CostType, Double> costs = new HashMap<>();
public double getDynamicCost() {
return dynamicCost;
}
public double getSetSpawnCost() {
return setSpawnCost;
}
public double getCost(Waypoint waypoint) {
return getPath(waypoint).cost;
}
public Waypoint(ConfigurationSection config) {
id = Objects.requireNonNull(config, "Could not load config section").getName();
name = Objects.requireNonNull(config.getString("name"), "Could not load waypoint name");
@ -42,8 +55,9 @@ public class Waypoint implements Unlockable {
loc = readLocation(Objects.requireNonNull(config.getString("location"), "Could not read location"));
radiusSquared = Math.pow(config.getDouble("radius"), 2);
for (CostType costType : CostType.values())
costs.put(costType, config.getDouble("cost." + costType.getPath()));
dynamicCost=config.getDouble("cost.dynamic-use");
setSpawnCost=config.getDouble("cost.set-spawnpoint");
for (WaypointOption option : WaypointOption.values())
options.put(option, config.getBoolean("option." + option.getPath(), option.getDefaultValue()));
@ -85,7 +99,7 @@ public class Waypoint implements Unlockable {
*
* @return Integer.POSITIVE_INFINITY if the way point is not linked
*/
public int getSimpleCostDestination(Waypoint waypoint) {
private int getSimpleCostDestination(Waypoint waypoint) {
if (!destinations.keySet().contains(waypoint.getId()))
return Integer.MAX_VALUE;
return destinations.get(waypoint.getId());
@ -153,11 +167,6 @@ public class Waypoint implements Unlockable {
}
public double getCost(CostType type) {
return costs.getOrDefault(type, 0d);
}
public boolean hasOption(WaypointOption option) {
return options.get(option);
}
@ -206,13 +215,13 @@ public class Waypoint implements Unlockable {
public class PathInfo {
private final ArrayList<Waypoint> waypoints;
private final int cost;
private final double cost;
public ArrayList<Waypoint> getWaypoints() {
return waypoints;
}
public int getCost() {
public double getCost() {
return cost;
}
@ -237,17 +246,27 @@ public class Waypoint implements Unlockable {
public PathInfo(ArrayList<Waypoint> waypoints, int cost) {
public PathInfo(ArrayList<Waypoint> waypoints, double cost) {
this.waypoints = waypoints;
this.cost = cost;
}
public String displayIntermediaryWayPoints() {
if(waypoints.size()<=2)
return "";
String result="";
for(int i=1;i<waypoints.size()-1;i++) {
result+=waypoints.get(i).name+(i!=waypoints.size()-2?",":"");
}
return result;
}
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);
double cost=this.cost+getFinalWaypoint().getSimpleCostDestination(waypoint);
return new PathInfo(newWaypoints,cost);
}

View File

@ -34,7 +34,7 @@ items:
lore:
- '&7You cannot teleport as the two waypoints are not linked.'
- '&7Teleporting costs &b&l{normal_cost}&7/&b&l{dynamic_cost} &7Stellium.'
# When you cannot teleport to a non dynamic waypoint
not-dynamic:
@ -43,9 +43,9 @@ items:
lore:
- '&7You cannot teleport as you are not standing on a waypoint.'
- '&7Teleporting costs &b&l{normal_cost}&7/&b&l{dynamic_cost} &7Stellium.'
# When you cannot teleport to a non dynamic waypoint
# When you don't have enough stellium
not-enough-stellium:
name: '&a{name}'
item: ENDER_PEARL