Skip to content

Commit

Permalink
Hotfix 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Efnilite committed Jan 10, 2021
1 parent 2a97555 commit 3af5f06
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 92 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.efnilite</groupId>
<artifactId>witp</artifactId>
<version>1.9</version>
<version>1.9a</version>
<build>
<plugins>
<plugin>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/efnilite/witp/WITP.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public void onEnable() {
divider = new SubareaDivider();

checker = new UpdateChecker();

ParkourUser.initHighScores();
Tasks.syncRepeat(new BukkitRunnable() {
@Override
public void run() {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/dev/efnilite/witp/command/MainCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.efnilite.witp.util.Util;
import dev.efnilite.witp.util.Verbose;
import dev.efnilite.witp.util.wrapper.BukkitCommand;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -40,6 +41,10 @@ public boolean execute(CommandSender sender, String[] args) {
WITP.getConfiguration().reload();
ParkourGenerator.Configurable.reload();
sender.sendMessage(Util.color("&a&l(!) &7The lang.yml file has been reloaded"));
} else if (args[0].equalsIgnoreCase("test")) {
for (int i = 1; i <= 5; i++) {
sender.sendMessage(PlaceholderAPI.setPlaceholders(player, "#" + i + " / %witp_player_rank_" + i + "% -- " + "%witp_score_rank_" + i + "%"));
}
}
if (player == null) {
return true;
Expand Down Expand Up @@ -79,15 +84,15 @@ public boolean execute(CommandSender sender, String[] args) {
} else if (args[0].equalsIgnoreCase("leaderboard")) {
ParkourUser user = ParkourUser.getUser(player);
if (user != null) {
user.scoreboard(1);
user.leaderboard(1);
}
}
} else if (args.length == 2) {
if (args[0].equalsIgnoreCase("leaderboard") && args[1] != null && player != null) {
int page = Integer.parseInt(args[1]);
ParkourPlayer pp = ParkourPlayer.getPlayer(player);
if (pp != null) {
pp.scoreboard(page);
pp.leaderboard(page);
}
} else if (args[0].equalsIgnoreCase("join") && args[1] != null) {
if (sender.isOp()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import dev.efnilite.witp.util.Verbose;
import dev.efnilite.witp.util.VoidGenerator;
import dev.efnilite.witp.util.inventory.ItemBuilder;
import dev.efnilite.witp.util.task.Tasks;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.craftbukkit.libs.org.apache.commons.io.FileUtils;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -218,7 +216,6 @@ private void fetchPossibleInLayer() {
world.setWeatherDuration(1000);
world.setDifficulty(Difficulty.PEACEFUL);
world.setAutoSave(false);
world.save();
return world;
}

Expand Down
89 changes: 54 additions & 35 deletions src/main/java/dev/efnilite/witp/hook/PlaceholderHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public class PlaceholderHook extends PlaceholderExpansion {

@Override
Expand Down Expand Up @@ -41,52 +43,69 @@ public String onPlaceholderRequest(Player player, @NotNull String params) {
return "player doesn't exist";
}
ParkourPlayer pp = ParkourPlayer.getPlayer(player);
if (pp == null) {
return "player is not registered";
if (pp != null) {
switch (params) {
case "highscore":
case "high_score":
return Integer.toString(pp.highScore);
case "score":
case "current_score":
return Integer.toString(pp.getGenerator().score);
case "time":
case "current_time":
return pp.getGenerator().time;
case "blocklead":
case "lead":
return Integer.toString(pp.blockLead);
case "style":
return pp.style;
case "time_pref":
case "time_preference":
return pp.time;
}
}

switch (params) {
case "highscore":
case "high_score":
return Integer.toString(pp.highScore);
case "score":
case "current_score":
return Integer.toString(pp.getGenerator().score);
case "time":
case "current_time":
return pp.getGenerator().time;
case "version":
case "ver":
return WITP.getInstance().getDescription().getVersion();
case "blocklead":
case "lead":
return Integer.toString(pp.blockLead);
case "style":
return pp.style;
case "time_pref":
case "time_preference":
return pp.time;
case "leader":
case "record_player":
return Bukkit.getOfflinePlayer(ParkourPlayer.getAtPlace(1)).getName();
UUID recordPlayer = ParkourPlayer.getAtPlace(1);
return recordPlayer == null ? "N/A" : Bukkit.getOfflinePlayer(recordPlayer).getName();
case "leader_score":
case "record_score":
case "record":
return Integer.toString(ParkourPlayer.getHighScore(ParkourPlayer.getAtPlace(1)));
}
if (params.contains("player_rank_")) {
String replaced = params.replaceAll("player_rank_", "");
int rank = Integer.parseInt(replaced);
if (rank > 0) {
String name = Bukkit.getOfflinePlayer(ParkourPlayer.getAtPlace(rank)).getName();
return name == null ? "N/A" : name;
}
} else if (params.contains("score_rank_")) {
String replaced = params.replaceAll("score_rank_", "");
int rank = Integer.parseInt(replaced);
if (rank > 0) {
return Integer.toString(ParkourPlayer.getHighScore(ParkourPlayer.getAtPlace(rank)));
}
UUID uuid = ParkourPlayer.getAtPlace(1);
if (uuid == null) {
return "N/A";
}
Integer score = ParkourPlayer.getHighScore(uuid);
return score == null ? "N/A" : Integer.toString(score);
default:
if (params.contains("player_rank_")) {
String replaced = params.replaceAll("player_rank_", "");
int rank = Integer.parseInt(replaced);
if (rank > 0) {
UUID uuidRank = ParkourPlayer.getAtPlace(rank);
return uuidRank == null ? "N/A" : Bukkit.getOfflinePlayer(uuidRank).getName();
} else {
return "N/A";
}
} else if (params.contains("score_rank_")) {
String replaced = params.replaceAll("score_rank_", "");
int rank = Integer.parseInt(replaced);
if (rank > 0) {
UUID uuidRank1 = ParkourPlayer.getAtPlace(rank);
if (uuidRank1 == null) {
return "N/A";
}
Integer score1 = ParkourPlayer.getHighScore(uuidRank1);
return score1 == null ? "N/A" : Integer.toString(score1);
} else {
return "N/A";
}
}
}

return null;
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/dev/efnilite/witp/player/ParkourPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class ParkourPlayer extends ParkourUser {
public @Expose String time;
public @Expose String style;
public @Expose String lang;
public @Expose String name; // for fixing null in leaderboard

private ParkourGenerator generator;
private List<Material> possibleStyle;
Expand All @@ -55,7 +56,7 @@ public class ParkourPlayer extends ParkourUser {
* Creates a new instance of a ParkourPlayer<br>
* If you are using the API, please use {@link WITPAPI#registerPlayer(Player)} instead
*/
public ParkourPlayer(@NotNull Player player, int highScore, String time, String style, int blockLead, boolean useParticles,
public ParkourPlayer(@NotNull Player player, int highScore, String time, String style, String name, int blockLead, boolean useParticles,
boolean useDifficulty, boolean useStructure, boolean useSpecial, boolean showDeathMsg, boolean showScoreboard) {
super(player);
Verbose.verbose("Init of Player " + player.getName());
Expand All @@ -70,6 +71,7 @@ public ParkourPlayer(@NotNull Player player, int highScore, String time, String
this.useStructure = useStructure;
this.showScoreboard = showScoreboard;
this.spectators = new HashMap<>();
this.name = name;

this.file = new File(WITP.getInstance().getDataFolder() + "/players/" + player.getUniqueId().toString() + ".json");
this.possibleStyle = new ArrayList<>();
Expand All @@ -83,15 +85,6 @@ public ParkourPlayer(@NotNull Player player, int highScore, String time, String
if (player.isOp() && WITP.isOutdated) {
send("&4&l!!! &fThe WITP plugin version you are using is outdated. Please check the Spigot page for updates.");
}
if (highScores.size() == 0) {
try {
fetchHighScores();
} catch (IOException ex) {
ex.printStackTrace();
Verbose.error("Error while trying to fetch the high scores!");
}
highScores = Util.sortByValue(highScores);
}
}

public void setGenerator(ParkourGenerator generator) {
Expand Down Expand Up @@ -171,6 +164,7 @@ public void setStyle(String style) {
public void setHighScore(int score) {
this.highScore = score;
highScores.put(player.getUniqueId(), score);
highScores = Util.sortByValue(highScores);
saveStats();
}

Expand Down Expand Up @@ -350,7 +344,7 @@ public void menu() {
.setLore(getTranslated("your-rank", Integer.toString(getRank(player.getUniqueId())), Integer.toString(score == null ? 0 : score)))
.build(), (t2, e2) -> {
if (checkPermission("witp.leaderboard")) {
scoreboard(1);
leaderboard(1);
player.closeInventory();
}
});
Expand Down Expand Up @@ -439,7 +433,7 @@ public void save() throws IOException {
*
* @return the high score of the player
*/
public static int getHighScore(@NotNull UUID player) {
public static Integer getHighScore(@NotNull UUID player) {
return highScores.get(player);
}

Expand All @@ -452,8 +446,13 @@ public static int getHighScore(@NotNull UUID player) {
*
* @return the player at that place
*/
public static UUID getAtPlace(int place) {
return new ArrayList<>(highScores.keySet()).get(place);
public static @Nullable UUID getAtPlace(int place) {
List<UUID> scores = new ArrayList<>(highScores.keySet());
place--;
if (scores.size() > place) {
return scores.get(place);
}
return null;
}

/**
Expand Down Expand Up @@ -487,17 +486,18 @@ public static UUID getAtPlace(int place) {
if (from.showScoreboard == null) {
from.showScoreboard = true;
}
ParkourPlayer pp = new ParkourPlayer(player, from.highScore, from.time, from.style, from.blockLead,
if (from.name == null || !from.name.equals(player.getName())) {
from.name = player.getName();
}
ParkourPlayer pp = new ParkourPlayer(player, from.highScore, from.time, from.style, from.name, from.blockLead,
from.useParticles, from.useDifficulty, from.useStructure, from.useSpecial, from.showDeathMsg, from.showScoreboard);
pp.save();
players.put(player, pp);
reader.close();
return pp;
} else {
ParkourPlayer pp = new ParkourPlayer(player, 0, "Day",
WITP.getConfiguration().getString("config", "styles.default"),
4, true, true, true, true,
true, true);
ParkourPlayer pp = new ParkourPlayer(player, 0, "Day", WITP.getConfiguration().getString("config", "styles.default"),
player.getName(), 4, true, true, true, true, true, true);
players.put(player, pp);
pp.save();
return pp;
Expand Down
50 changes: 35 additions & 15 deletions src/main/java/dev/efnilite/witp/player/ParkourUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public abstract class ParkourUser {
protected static final HashMap<String, ParkourUser> users = new HashMap<>();
protected static final HashMap<Player, ParkourPlayer> players = new HashMap<>();
protected static HashMap<UUID, Integer> highScores = new LinkedHashMap<>();
protected static HashMap<UUID, String> nameMap = new LinkedHashMap<>();
protected static final Gson gson = new GsonBuilder().disableHtmlEscaping().excludeFieldsWithoutExposeAnnotation().create();

public ParkourUser(@NotNull Player player) {
Expand Down Expand Up @@ -81,6 +82,7 @@ public static void unregister(@NotNull ParkourUser player, boolean sendBack) thr
try {
ParkourPlayer.register(spectator.getPlayer());
} catch (IOException ex) {
ex.printStackTrace();
ex.printStackTrace();
Verbose.error("Error while trying to register player" + player.getPlayer().getName());
}
Expand Down Expand Up @@ -167,7 +169,21 @@ public static void fetchHighScores() throws IOException {
FileReader reader = new FileReader(file);
ParkourPlayer from = gson.fromJson(reader, ParkourPlayer.class);
String name = file.getName();
highScores.put(UUID.fromString(name.substring(0, name.lastIndexOf('.'))), from.highScore);
UUID uuid = UUID.fromString(name.substring(0, name.lastIndexOf('.')));
highScores.put(uuid, from.highScore);
nameMap.put(uuid, from.name);
}
}

public static void initHighScores() {
if (highScores.isEmpty()) {
try {
fetchHighScores();
} catch (IOException ex) {
ex.printStackTrace();
Verbose.error("Error while trying to fetch the high scores!");
}
highScores = Util.sortByValue(highScores);
}
}

Expand Down Expand Up @@ -241,17 +257,10 @@ public void gamemode() {
}

/**
* Shows the scoreboard (as a chat message)
* Shows the leaderboard (as a chat message)
*/
public void scoreboard(int page) {
if (highScores.size() == 0) {
try {
fetchHighScores();
} catch (IOException ex) {
ex.printStackTrace();
Verbose.error("Error while trying to fetch the high scores!");
}
}
public void leaderboard(int page) {
initHighScores();

int lowest = page * 10;
int highest = (page - 1) * 10;
Expand All @@ -276,7 +285,13 @@ public void scoreboard(int page) {
if (uuid == null) {
continue;
}
String name = Bukkit.getOfflinePlayer(uuid).getName();
String name = nameMap.get(uuid);
if (name == null || name.equals("null")) {
name = Bukkit.getOfflinePlayer(uuid).getName();
if (name == null || name.equals("null")) {
continue;
}
}
int rank = i + 1;
send("&a#" + rank + ". &7" + name + " &f- " + highScores.get(uuid));
}
Expand Down Expand Up @@ -334,9 +349,14 @@ public void sendTranslated(String path, String... replaceable) {

/**
* Same as {@link #sendTranslated(String, String...)}, but without sending the text (used in GUIs)
* @param path
* @param replaceable
* @return
*
* @param path
* The path
*
* @param replaceable
* Things that can be replaced
*
* @return the coloured and replaced string
*/
public String getTranslated(String path, String... replaceable) {
path = "messages.en." + path;
Expand Down
Loading

0 comments on commit 3af5f06

Please sign in to comment.