Skip to content
This repository was archived by the owner on Feb 23, 2025. It is now read-only.

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
Efnilite committed Jan 16, 2022
1 parent 487c2f1 commit 135f5bb
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 135 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>2.8g</version>
<version>2.9</version>

<properties>
<java.version>1.8</java.version>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/dev/efnilite/witp/WITP.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.efnilite.witp;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import dev.efnilite.witp.api.Registry;
import dev.efnilite.witp.api.gamemode.DefaultGamemode;
import dev.efnilite.witp.api.gamemode.SpectatorGamemode;
Expand Down Expand Up @@ -43,6 +45,7 @@ public final class WITP extends JavaPlugin {
private static Configuration configuration;
private static SubareaDivider divider;
private static Registry registry;
private static Gson gson;
private static @Nullable MultiverseHook multiverseHook;
private static @Nullable ProtocolHook protocolHook;
private static @Nullable PlaceholderHook placeholderHook;
Expand Down Expand Up @@ -188,6 +191,8 @@ public void onEnable() {
}));
long time = Tasks.end("load");

gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().disableHtmlEscaping().setLenient().create();

Logging.info("Loaded WITP in " + time + "ms!");
}

Expand Down Expand Up @@ -282,6 +287,10 @@ public static Configuration getConfiguration() {
return configuration;
}

public static Gson getGson() {
return gson;
}

public static WITP getInstance() {
return instance;
}
Expand Down
146 changes: 102 additions & 44 deletions src/main/java/dev/efnilite/witp/command/MainCommand.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package dev.efnilite.witp.command;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import dev.efnilite.witp.WITP;
import dev.efnilite.witp.generator.DefaultGenerator;
import dev.efnilite.witp.player.ParkourPlayer;
import dev.efnilite.witp.player.ParkourSpectator;
import dev.efnilite.witp.player.ParkourUser;
import dev.efnilite.witp.player.data.InventoryData;
import dev.efnilite.witp.schematic.Schematic;
import dev.efnilite.witp.schematic.selection.Selection;
import dev.efnilite.witp.util.Logging;
Expand Down Expand Up @@ -58,25 +57,37 @@ public boolean execute(CommandSender sender, String[] args) {
}
if (args.length == 0) {
// Help menu
send(sender, "&7--------------- &aWITP &7---------------");
send(sender, "&a/witp &f- &7Main command");
send(sender, "&a/witp join [player] &f- &7Join the game on this server or make another player join");
send(sender, "&a/witp leave &f- &7Leave the game on this server");
send(sender, "&a/witp menu &f- &7Open the customization menu");
send(sender, "&a/witp gamemode &f- &7Open the gamemode menu");
send(sender, "&a/witp leaderboard &f- &7Open the leaderboard");
send(sender, "&7--------------- &4WITP &7---------------");
send(sender, "&4/witp &8- &7Main command");
if (sender.hasPermission("witp.join")) {
send(sender, "&4/witp join [player] &8- &7Join the game on this server or make another player join");
send(sender, "&4/witp leave &8- &7Leave the game on this server");
}
if (sender.hasPermission("witp.menu")) {
send(sender, "&4/witp menu &8- &7Open the customization menu");
}
if (sender.hasPermission("witp.gamemode")) {
send(sender, "&4/witp gamemode &8- &7Open the gamemode menu");
}
if (sender.hasPermission("witp.leaderboard")) {
send(sender, "&4/witp leaderboard &8- &7Open the leaderboard");
}

// Advanced settings based per permission
if (sender.hasPermission("witp.reload") || sender.hasPermission("witp.schematic") || sender.isOp()) {
send(sender, "&7------------ &aAdvanced &7-------------");
send(sender, "&7------------ &4&lAdvanced &7-------------");
}
if (sender.hasPermission("witp.schematic")) {
send(sender, "&a/witp schematic &f- &7Create a schematic");
send(sender, "&4/witp schematic &f- &7Create a schematic");
}
if (sender.hasPermission("witp.reload")) {
send(sender, "&a/witp reload &f- &7Reloads the lang.yml file");
send(sender, "&a/witp migrate &f- &7Migrate your Json files to MySQL");
send(sender, "&a/witp reset &f- &7Resets all highscores. &cBe careful when using!");
send(sender, "&4&l/witp reload &8- &7Reloads the lang.yml file");
send(sender, "&4&l/witp migrate &8- &7Migrate your Json files to MySQL");
send(sender, "&4&l/witp reset &8- &7Resets all highscores. &cBe careful when using!");
}
if (sender.hasPermission("witp.recoverinventory")) {
send(sender, "&4&l/witp recoverinventory <player> &8- &7Recover a player's saved inventory." +
" &cUseful for recovering data after server crashes or errors when leaving.");
}
return true;
} else if (args.length == 1) {
Expand Down Expand Up @@ -119,7 +130,6 @@ public boolean execute(CommandSender sender, String[] args) {
folder.mkdirs();
return true;
}
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().disableHtmlEscaping().create();
for (File file : folder.listFiles()) {
FileReader reader;
try {
Expand All @@ -129,7 +139,7 @@ public boolean execute(CommandSender sender, String[] args) {
send(sender, "&4&l> &cError while trying to read file, check your console");
return true;
}
ParkourPlayer from = gson.fromJson(reader, ParkourPlayer.class);
ParkourPlayer from = WITP.getGson().fromJson(reader, ParkourPlayer.class);
String name = file.getName();
from.uuid = UUID.fromString(name.substring(0, name.lastIndexOf('.')));
from.save(true);
Expand Down Expand Up @@ -288,23 +298,21 @@ public boolean execute(CommandSender sender, String[] args) {
return true;
}
ParkourUser.leaderboard(ParkourUser.getUser(player), player, page);
} else if (args[0].equalsIgnoreCase("join") && args[1] != null) {
if (sender.isOp()) {
Player join = Bukkit.getPlayer(args[1]);
if (join == null) {
Logging.error("Player " + args[1] + " doesn't exist!");
return true;
}
try {
ParkourPlayer.register(join);
ParkourPlayer pp = ParkourPlayer.getPlayer(join);
if (pp != null) {
pp.sendTranslated("joined");
}
} catch (IOException | SQLException ex) {
ex.printStackTrace();
Logging.error("Error while joining");
} else if (args[0].equalsIgnoreCase("join") && args[1] != null && sender.hasPermission("witp.join.other")) {
Player join = Bukkit.getPlayer(args[1]);
if (join == null) {
send(sender, "&4&l> &7That player isn't online!");
return true;
}
try {
ParkourPlayer.register(join);
ParkourPlayer pp = ParkourPlayer.getPlayer(join);
if (pp != null) {
pp.sendTranslated("joined");
}
} catch (IOException | SQLException ex) {
ex.printStackTrace();
Logging.error("Error while joining");
}
} else if (args[0].equalsIgnoreCase("search") && player != null) {
ParkourUser user = ParkourUser.getUser(player);
Expand All @@ -322,6 +330,24 @@ public boolean execute(CommandSender sender, String[] args) {
}
}
}
} else if (args[0].equalsIgnoreCase("recoverinventory") && sender.hasPermission("witp.recoverinventory")) {
Player arg1 = Bukkit.getPlayer(args[1]);
if (arg1 == null) {
send(sender, "&4&l> &7That player isn't online!");
return true;
}

InventoryData data = new InventoryData(arg1);
data.readFile(success -> {
if (success) {
send(sender, "&4&l> &7Successfully recovered the inventory of " + arg1.getName() + " from their file");
send(sender, "&4&l> &7Giving " + arg1.getName() + " their items now...");
data.apply();
} else {
send(sender, "&4&l> &cThere was an error recovering the inventory of " + arg1.getName() + " from their file");
send(sender, "&4&l> &7" + arg1.getName() + " has no saved inventory or there was an error. Check the console.");
}
});
}
} else if (args.length == 3) {
if (args[0].equalsIgnoreCase("askreset") && player != null && args[2] != null) {
Expand All @@ -345,7 +371,34 @@ public static void send(CommandSender sender, String message) {

@Override
public List<String> tabComplete(Player player, String[] args) {
if (args.length == 2) {
List<String> completions = new ArrayList<>();
if (args.length == 1) {
if (player.hasPermission("witp.join")) {
completions.add("join");
completions.add("leave");
}
if (player.hasPermission("witp.menu")) {
completions.add("menu");
}
if (player.hasPermission("witp.gamemode")) {
completions.add("gamemode");
}
if (player.hasPermission("witp.leaderboard")) {
completions.add("leaderboard");
}
if (player.hasPermission("witp.schematic")) {
completions.add("schematic");
}
if (player.hasPermission("witp.reload")) {
completions.add("reload");
completions.add("migrate");
completions.add("reset");
}
if (player.hasPermission("witp.recoverinventory")) {
completions.add("recoverinventory");
}
return completions(args[0], completions);
} else if (args.length == 2) {
if (args[0].equalsIgnoreCase("search")) {
List<String> names = new ArrayList<>();
for (ParkourPlayer pp : ParkourUser.getActivePlayers()) {
Expand All @@ -355,19 +408,24 @@ public List<String> tabComplete(Player player, String[] args) {
}
names.add(name);
}
return names;
completions.addAll(names);
} else if (args[0].equalsIgnoreCase("schematic") && player.hasPermission("witp.schematic")) {
return Arrays.asList("wand", "pos1", "pos2", "save");
completions.addAll(Arrays.asList("wand", "pos1", "pos2", "save"));
} else if ((args[0].equalsIgnoreCase("join") && player.hasPermission("witp.join.other")) ||
(args[0].equalsIgnoreCase("recoverinventory") && player.hasPermission("witp.recoverinventory"))) {
List<String> names = new ArrayList<>();
for (Player pl : Bukkit.getOnlinePlayers()) {
String name = pl.getName();
if (player.getName().equals(name)) {
continue;
}
names.add(name);
}
completions.addAll(names);
}
return completions(args[1], completions);
} else {
return Collections.emptyList();
}
List<String> suggestions = new ArrayList<>(Arrays.asList("join", "leave", "menu", "leaderboard", "gamemode"));
if (player.hasPermission("witp.reload")) {
suggestions.add("reload");
suggestions.add("migrate");
}
if (player.hasPermission("witp.schematic")) {
suggestions.add("schematic");
}
return suggestions;
}
}
6 changes: 3 additions & 3 deletions src/main/java/dev/efnilite/witp/player/ParkourPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void save(boolean async) {
file.createNewFile();
}
FileWriter writer = new FileWriter(file);
gson.toJson(ParkourPlayer.this, writer);
WITP.getGson().toJson(ParkourPlayer.this, writer);
writer.flush();
writer.close();
}
Expand Down Expand Up @@ -402,7 +402,7 @@ public static ParkourPlayer register(@NotNull ParkourPlayer pp) throws IOExcepti
if (data.exists()) {
Logging.verbose("Reading player data..");
FileReader reader = new FileReader(data);
ParkourPlayer from = gson.fromJson(reader, ParkourPlayer.class);
ParkourPlayer from = WITP.getGson().fromJson(reader, ParkourPlayer.class);

pp.setDefaults(from.highScore, from.time, from.style, from.highScoreTime, from.lang, from.blockLead,
from.useParticles == null || from.useParticles,
Expand All @@ -422,7 +422,7 @@ public static ParkourPlayer register(@NotNull ParkourPlayer pp) throws IOExcepti
pp.saveStats();
}
} else {
SelectStatement select = new SelectStatement(WITP.getDatabase(),Option.SQL_PREFIX.get() + "players")
SelectStatement select = new SelectStatement(WITP.getDatabase(), Option.SQL_PREFIX.get() + "players")
.addColumns("`uuid`", "`name`", "`highscore`", "`hstime`", "`hsdiff`").addCondition("`uuid` = '" + uuid + "'");
HashMap<String, List<Object>> map = select.fetch();
List<Object> objects = map != null ? map.get(uuid.toString()) : null;
Expand Down
Loading

0 comments on commit 135f5bb

Please sign in to comment.