Skip to content

Commit

Permalink
Improvements to Lobby teleportation on Void damage
Browse files Browse the repository at this point in the history
Fix for Placeholders every time cache expired
  • Loading branch information
A5H73Y committed Dec 30, 2020
1 parent ce15794 commit a3611f8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ParkourPlaceholders extends PlaceholderExpansion {
private final GenericCache<String, String> cache;

/**
* Contruct the Parkour Placeholders functionality.
* Construct the Parkour Placeholders functionality.
* A Cache is used for repeated expensive calls to the database.
* @param parkour plugin instance
*/
Expand Down Expand Up @@ -354,7 +354,8 @@ private String extractResultDetails(TimeEntry result, String key) {
}

private String getOrRetrieveCache(String key, Supplier<String> callback) {
if (!cache.containsKey(key)) {
// check if the key exists or its 'get' is about to expire.
if (!cache.containsKey(key) || !cache.get(key).isPresent()) {
cache.put(key, callback.get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected void initializeConfig() {
this.addDefault("Parkour.CheckpointCreated", "Checkpoint &3%CHECKPOINT% &fhas been set on &b%COURSE%&f!");
this.addDefault("Parkour.Selected", "Now editing &b%VALUE%");
this.addDefault("Parkour.Deselected", "Finish editing.");
this.addDefault("Parkour.WhenReady", "&7When you have finished creating &b%VALUE%&7, enter &b/pa ready");
this.addDefault("Parkour.WhenReady", "&7Once you have finished editing &b%VALUE%&7, enter &b/pa ready");
this.addDefault("Parkour.Delete", "&b%VALUE% &fhas been deleted!");
this.addDefault("Parkour.DeleteCheckpoint", "Checkpoint &b%CHECKPOINT% &fwas deleted on &b%COURSE%");
this.addDefault("Parkour.Reset", "&b%VALUE% &fhas been reset!");
Expand Down Expand Up @@ -148,7 +148,7 @@ protected void initializeConfig() {
this.addDefault("Other.PropertySet", "The &3%PROPERTY% &ffor &3%COURSE% &fwas set to &b%VALUE%&f!");

this.addDefault("Lobby.Created", "&b%VALUE% &flobby created.");
this.addDefault("Lobby.RequiredLevelSet", "The required ParkourLevel to join is &b%VALUE%");
this.addDefault("Lobby.RequiredLevelSet", "You have set the required ParkourLevel to &b%VALUE%&f.");

this.addDefault("Scoreboard.MainHeading", "&b&l== Parkour ==");
this.addDefault("Scoreboard.TitleFormat", "&b%VALUE%");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.github.a5h73y.parkour.enums.ParkourMode;
import io.github.a5h73y.parkour.other.AbstractPluginReceiver;
import io.github.a5h73y.parkour.type.player.ParkourSession;
import io.github.a5h73y.parkour.type.player.PlayerInfo;
import io.github.a5h73y.parkour.utility.TranslationUtils;
import org.bukkit.GameMode;
import org.bukkit.entity.Damageable;
Expand Down Expand Up @@ -79,17 +78,15 @@ public void onEntityDamage(EntityDamageEvent event) {
}

Player player = (Player) event.getEntity();

boolean playing = parkour.getPlayerManager().isPlaying(player);

if (event.getCause() == EntityDamageEvent.DamageCause.VOID) {
if (playing && parkour.getConfig().getBoolean("OnCourse.DieInVoid")) {
parkour.getPlayerManager().playerDie(player);
return;
} else if (!playing && parkour.getConfig().isVoidDetection()) {
parkour.getServer().getScheduler().runTaskLater(parkour, () -> {
parkour.getLobbyManager().teleportToNearestLobby(player);
}, 1L);
parkour.getServer().getScheduler().runTaskLater(parkour, () ->
parkour.getLobbyManager().teleportToNearestLobby(player),1L);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,37 @@ public static boolean canJoinLobby(Player player, String lobbyName) {
return true;
}

/**
* Validate Player joining Lobby Silently.
*
* @param player player
* @param lobbyName lobby name
* @return player can join lobby
*/
public static boolean canJoinLobbySilent(Player player, String lobbyName) {
if (!LobbyInfo.doesLobbyExist(lobbyName)) {
return false;
}

if (Bukkit.getWorld(Parkour.getDefaultConfig().getString("Lobby." + lobbyName + ".World")) == null) {
return false;
}

if (Parkour.getDefaultConfig().getBoolean("LobbySettings.EnforceWorld")
&& !player.getWorld().getName().equals(LobbyInfo.getLobbyLocation(lobbyName).getWorld().getName())) {
return false;
}

int level = LobbyInfo.getRequiredLevel(lobbyName);

if (level > 0 && PlayerInfo.getParkourLevel(player) < level
&& !PermissionUtils.hasPermission(player, Permission.ADMIN_LEVEL_BYPASS, false)) {
return false;
}

return true;
}

/**
* Validate Player joining Course.
*
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/io/github/a5h73y/parkour/type/lobby/Lobby.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@
*/
public class Lobby {

private final String name;

private final Location location;

private final int requiredLevel;

/**
* Construct a Lobby from the details.
*
* @param name lobby name
* @param location lobby {@link Location}
* @param requiredLevel required ParkourLevel
*/
public Lobby(Location location, int requiredLevel) {
public Lobby(String name, Location location, int requiredLevel) {
this.name = name;
this.location = location;
this.requiredLevel = requiredLevel;
}

public String getName() {
return name;
}

/**
* Get the {@link Location} of the Lobby.
* @return location
Expand Down
67 changes: 28 additions & 39 deletions src/main/java/io/github/a5h73y/parkour/type/lobby/LobbyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import io.github.a5h73y.parkour.utility.PluginUtils;
import io.github.a5h73y.parkour.utility.TranslationUtils;
import io.github.a5h73y.parkour.utility.ValidationUtils;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
Expand Down Expand Up @@ -103,44 +103,15 @@ public void justTeleportToDefaultLobby(Player player) {

/**
* Teleport the Player to the nearest Lobby available.
* @param player The player.
* @param player player
*/
public void teleportToNearestLobby(Player player) {
Lobby lobby = getNearestLobby(player.getLocation());
if (lobby == null) {
return;
}
player.teleport(lobby.getLocation());
}

/**
* Attempts to retrieve the nearest lobby for given location.
* If no nearest lobby was found, the default lobby will be returned.
* If no lobby was set, null is returned.
* @param location The location.
* @return The nearest lobby.
*/
private Lobby getNearestLobby(Location location) {
Lobby nearestLobby = null;
double nearestDistance = Double.MAX_VALUE;

for (Lobby lobby : lobbyCache.values()) {
Location lobbyLocation = lobby.getLocation();
if (!Objects.equals(location.getWorld(), lobbyLocation.getWorld())) {
continue;
}

double distance = lobbyLocation.distanceSquared(lobbyLocation);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestLobby = lobby;
}
Lobby lobby = getNearestLobby(player);
if (lobby != null) {
player.setFallDistance(0);
player.teleport(lobby.getLocation());
TranslationUtils.sendValueTranslation("Parkour.LobbyOther", lobby.getName(), player);
}

if (nearestLobby == null && LobbyInfo.doesLobbyExist()) {
return lobbyCache.getOrDefault(Constants.DEFAULT, populateLobby(Constants.DEFAULT));
}
return nearestLobby;
}

/**
Expand Down Expand Up @@ -211,8 +182,9 @@ public void clearCache(String lobbyName) {
* @param lobbyName lobby name
* @return populated Lobby
*/
private Lobby populateLobby(String lobbyName) {
Lobby lobby = new Lobby(LobbyInfo.getLobbyLocation(lobbyName), LobbyInfo.getRequiredLevel(lobbyName));
private Lobby populateLobby(@NotNull String lobbyName) {
Lobby lobby = new Lobby(lobbyName.toLowerCase(),
LobbyInfo.getLobbyLocation(lobbyName), LobbyInfo.getRequiredLevel(lobbyName));
lobbyCache.put(lobbyName.toLowerCase(), lobby);
return lobby;
}
Expand All @@ -227,4 +199,21 @@ private void setLobby(Player player, String lobbyName) {
LobbyInfo.setLobby(lobbyName, player.getLocation());
PluginUtils.logToFile(lobbyName + " lobby was set by " + player.getName());
}

/**
* Find the nearest valid Lobby to the Player.
* If no nearest lobby was found, the default lobby will be returned.
* If no lobby was set, null is returned.
*
* @param player player
* @return nearest lobby to Location
*/
@Nullable
private Lobby getNearestLobby(Player player) {
return lobbyCache.values().stream()
.filter(lobby -> lobby.getLocation().getWorld() == player.getWorld())
.filter(lobby -> ParkourValidation.canJoinLobbySilent(player, lobby.getName()))
.min(Comparator.comparingDouble(o -> player.getLocation().distanceSquared(o.getLocation())))
.orElse(lobbyCache.getOrDefault(Constants.DEFAULT, populateLobby(Constants.DEFAULT)));
}
}

0 comments on commit a3611f8

Please sign in to comment.