Skip to content

Commit

Permalink
Update task & perm system
Browse files Browse the repository at this point in the history
  • Loading branch information
Efnilite committed Feb 15, 2021
1 parent 32ee83c commit 099ccbc
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 120 deletions.
7 changes: 1 addition & 6 deletions src/main/java/dev/efnilite/witp/WITP.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,7 @@ public void onEnable() {
new InventoryBuilder.ClickHandler(this);

UpdateChecker checker = new UpdateChecker();
Tasks.syncRepeat(new BukkitRunnable() {
@Override
public void run() {
checker.check();
}
}, 30 * 60 * 20);
Tasks.syncRepeat(checker::check, 30 * 60 * 20);
}

@Override
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/dev/efnilite/witp/command/MainCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,15 @@ public boolean execute(CommandSender sender, String[] args) {
} else if (args[0].equalsIgnoreCase("gamemode") || args[0].equalsIgnoreCase("gm")) {
ParkourUser user = ParkourUser.getUser(player);
if (user != null) {
if (player.hasPermission("witp.gamemode")) {
if (user.checkPermission("witp.gamemode")) {
user.gamemode();
} else {
user.sendTranslated("cant-do");
}
}
} else if (args[0].equalsIgnoreCase("leaderboard")) {
ParkourUser user = ParkourUser.getUser(player);
if (user != null) {
if (player.hasPermission("witp.leaderboard")) {
if (user.checkPermission("witp.leaderboard")) {
user.leaderboard(1);
} else {
user.sendTranslated("cant-do");
}
}
}
Expand All @@ -139,10 +135,8 @@ public boolean execute(CommandSender sender, String[] args) {
int page = Integer.parseInt(args[1]);
ParkourPlayer pp = ParkourPlayer.getPlayer(player);
if (pp != null) {
if (player.hasPermission("witp.leaderboard")) {
if (pp.checkPermission("witp.leaderboard")) {
pp.leaderboard(page);
} else {
pp.sendTranslated("cant-do");
}
}
} else if (args[0].equalsIgnoreCase("join") && args[1] != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public DefaultGenerator(@NotNull ParkourPlayer player) {
*/
@Override
public void start() {
Tasks.syncRepeat(new BukkitRunnable() {
Tasks.defaultSyncRepeat(new BukkitRunnable() {
@Override
public void run() {
if (stopped) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,11 @@ private void createIsland(ParkourPlayer pp, SubareaPoint point) {

// todo fix this check
Location finalTo = to;
BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
Tasks.syncDelay(() -> {
if (!player.getWorld().getUID().equals(world.getUID())) {
player.teleport(finalTo, PlayerTeleportEvent.TeleportCause.PLUGIN);
}
}
};
Tasks.syncDelay(runnable, 10);
}, 10);
setBorder(pp, point);
}
}
77 changes: 36 additions & 41 deletions src/main/java/dev/efnilite/witp/player/ParkourPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import dev.efnilite.witp.util.sql.InvalidStatementException;
import dev.efnilite.witp.util.sql.SelectStatement;
import dev.efnilite.witp.util.sql.UpdertStatement;
import dev.efnilite.witp.util.task.Tasks;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
Expand All @@ -28,6 +30,7 @@
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;

/**
Expand Down Expand Up @@ -393,17 +396,6 @@ public void menu() {
builder.build();
}

private boolean checkPermission(String perm) {
if (Option.PERMISSIONS) {
boolean check = player.hasPermission(perm);
if (!check) {
sendTranslated("cant-do");
}
return check;
}
return true;
}

private @Nullable List<Material> getPossibleMaterials(String style) {
List<Material> possibleStyles = new ArrayList<>();
String possible = WITP.getConfiguration().getFile("config").getString("styles.list." + style);
Expand Down Expand Up @@ -431,39 +423,41 @@ private void saveStats() {
* Saves the player's data to their file
*/
public void save() {
try {
if (Option.SQL) {
Verbose.verbose("Writing player's data to SQL server");
UpdertStatement statement = new UpdertStatement(WITP.getDatabase(), "players")
.setDefault("uuid", uuid.toString()).setDefault("name", name)
.setDefault("highscore", highScore).setDefault("hstime", highScoreTime)
.setCondition("`uuid` = '" + uuid.toString() + "'");
statement.query();
statement = new UpdertStatement(WITP.getDatabase(), "options")
.setDefault("uuid", uuid.toString()).setDefault("time", time)
.setDefault("style", style).setDefault("blockLead", blockLead)
.setDefault("useParticles", useParticles).setDefault("useDifficulty", useDifficulty)
.setDefault("useStructure", useStructure).setDefault("useSpecial", useSpecial)
.setDefault("showFallMsg", showDeathMsg).setDefault("showScoreboard", showScoreboard)
.setCondition("`uuid` = '" + uuid.toString() + "'"); // saves all options
statement.query();
} else {
if (!file.exists()) {
File folder = new File(WITP.getInstance().getDataFolder() + "/players");
if (!folder.exists()) {
folder.mkdirs();
Tasks.asyncTask(() -> {
try {
if (Option.SQL) {
Verbose.verbose("Writing player's data to SQL server");
UpdertStatement statement = new UpdertStatement(WITP.getDatabase(), "players")
.setDefault("uuid", uuid.toString()).setDefault("name", name)
.setDefault("highscore", highScore).setDefault("hstime", highScoreTime)
.setCondition("`uuid` = '" + uuid.toString() + "'");
statement.query();
statement = new UpdertStatement(WITP.getDatabase(), "options")
.setDefault("uuid", uuid.toString()).setDefault("time", time)
.setDefault("style", style).setDefault("blockLead", blockLead)
.setDefault("useParticles", useParticles).setDefault("useDifficulty", useDifficulty)
.setDefault("useStructure", useStructure).setDefault("useSpecial", useSpecial)
.setDefault("showFallMsg", showDeathMsg).setDefault("showScoreboard", showScoreboard)
.setCondition("`uuid` = '" + uuid.toString() + "'"); // saves all options
statement.query();
} else {
if (!file.exists()) {
File folder = new File(WITP.getInstance().getDataFolder() + "/players");
if (!folder.exists()) {
folder.mkdirs();
}
file.createNewFile();
}
file.createNewFile();
FileWriter writer = new FileWriter(file);
gson.toJson(this, writer);
writer.flush();
writer.close();
}
FileWriter writer = new FileWriter(file);
gson.toJson(this, writer);
writer.flush();
writer.close();
} catch (IOException | InvalidStatementException ex) {
ex.printStackTrace();
Verbose.error("Error while trying to save the player's data..");
}
} catch (IOException | InvalidStatementException ex) {
ex.printStackTrace();
Verbose.error("Error while trying to save the player's data..");
}
});
}

/**
Expand Down Expand Up @@ -660,3 +654,4 @@ public ParkourGenerator getGenerator() {
return generator;
}
}

11 changes: 11 additions & 0 deletions src/main/java/dev/efnilite/witp/player/ParkourUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ public static void unregister(@NotNull ParkourUser player, boolean sendBack, boo
}
}

public boolean checkPermission(String perm) {
if (Option.PERMISSIONS) {
boolean check = player.hasPermission(perm);
if (!check) {
sendTranslated("cant-do");
}
return check;
}
return true;
}

/**
* Teleports the player asynchronously, which helps with unloaded chunks (?)
*
Expand Down
43 changes: 19 additions & 24 deletions src/main/java/dev/efnilite/witp/util/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,36 +73,31 @@ public void reload() {
*/
private void structures() {
if (!(new File(plugin.getDataFolder().toString() + "/structures/parkour-1.nbt").exists())) {
String[] schematics = new String[] {"spawn-island.nbt"};
String[] schematics = new String[]{"spawn-island.nbt"};
File folder = new File(plugin.getDataFolder().toString() + "/structures");
folder.mkdirs();
Verbose.info("Downloading all structures...");
int structureCount = 18;
BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
try {
for (String schematic : schematics) {
InputStream stream = new URL("https://github.com/Efnilite/Walk-in-the-Park/raw/main/structures/" + schematic).openStream();
Files.copy(stream, Paths.get(folder + "/" + schematic));
Verbose.verbose("Downloaded " + schematic);
stream.close();
}
for (int i = 1; i <= structureCount; i++) {
InputStream stream = new URL("https://github.com/Efnilite/Walk-in-the-Park/raw/main/structures/parkour-" + i + ".nbt").openStream();
Files.copy(stream, Paths.get(folder + "/parkour-" + i + ".nbt"));
Verbose.verbose("Downloaded parkour-" + i);
stream.close();
}
Verbose.info("Downloaded all structures");
} catch (IOException ex) {
ex.printStackTrace();
Verbose.error("Stopped download - please delete all the structures that have been downloaded and restart the server");
Tasks.asyncTask(() -> {
try {
for (String schematic : schematics) {
InputStream stream = new URL("https://github.com/Efnilite/Walk-in-the-Park/raw/main/structures/" + schematic).openStream();
Files.copy(stream, Paths.get(folder + "/" + schematic));
Verbose.verbose("Downloaded " + schematic);
stream.close();
}
this.cancel();
for (int i = 1; i <= structureCount; i++) {
InputStream stream = new URL("https://github.com/Efnilite/Walk-in-the-Park/raw/main/structures/parkour-" + i + ".nbt").openStream();
Files.copy(stream, Paths.get(folder + "/parkour-" + i + ".nbt"));
Verbose.verbose("Downloaded parkour-" + i);
stream.close();
}
Verbose.info("Downloaded all structures");
} catch (IOException ex) {
ex.printStackTrace();
Verbose.error("Stopped download - please delete all the structures that have been downloaded and restart the server");
}
};
Tasks.asyncTask(runnable);
});
}
}

Expand Down
30 changes: 18 additions & 12 deletions src/main/java/dev/efnilite/witp/util/task/Tasks.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.efnilite.witp.util.task;

import dev.efnilite.witp.WITP;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

/**
* Utilities for Runnables
Expand All @@ -15,27 +17,31 @@ public class Tasks {
plugin = WITP.getInstance();
}

public static int syncRepeat(BukkitRunnable runnable, int interval) {
return runnable.runTaskTimer(plugin, 0L, interval).getTaskId();
public static BukkitTask defaultSyncRepeat(BukkitRunnable runnable, int interval) {
return runnable.runTaskTimer(plugin, 0L, interval);
}

public static int asyncRepeat(BukkitRunnable runnable, int interval) {
return runnable.runTaskTimerAsynchronously(plugin, 0L, interval).getTaskId();
public static BukkitTask syncRepeat(Runnable runnable, int interval) {
return Bukkit.getScheduler().runTaskTimer(plugin, runnable, 0L, interval);
}

public static int syncTask(BukkitRunnable runnable) {
return runnable.runTask(plugin).getTaskId();
public static BukkitTask asyncRepeat(Runnable runnable, int interval) {
return Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, 0L, interval);
}

public static int asyncTask(BukkitRunnable runnable) {
return runnable.runTaskAsynchronously(plugin).getTaskId();
public static BukkitTask syncTask(Runnable runnable) {
return Bukkit.getScheduler().runTask(plugin, runnable);
}

public static int asyncDelay(BukkitRunnable runnable, int delay) {
return runnable.runTaskLaterAsynchronously(plugin, delay).getTaskId();
public static BukkitTask asyncTask(Runnable runnable) {
return Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable);
}

public static int syncDelay(BukkitRunnable runnable, int delay) {
return runnable.runTaskLater(plugin, delay).getTaskId();
public static BukkitTask asyncDelay(Runnable runnable, int delay) {
return Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, delay);
}

public static BukkitTask syncDelay(Runnable runnable, int delay) {
return Bukkit.getScheduler().runTaskLater(plugin, runnable, delay);
}
}
36 changes: 16 additions & 20 deletions src/main/java/dev/efnilite/witp/util/web/UpdateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,23 @@
public class UpdateChecker {

public void check() {
BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
String latest;
try {
latest = getLatestVersion();
} catch (IOException ex) {
ex.printStackTrace();
Verbose.error("Error while trying to fetch latest version!");
return;
}
if (!WITP.getInstance().getDescription().getVersion().equals(latest)) {
Verbose.info("A new version of WITP is available to download!");
Verbose.info("Newest version: " + latest);
WITP.OUTDATED = true;
} else {
Verbose.info("WITP is currently up-to-date!");
}
Tasks.asyncTask(() -> {
String latest;
try {
latest = getLatestVersion();
} catch (IOException ex) {
ex.printStackTrace();
Verbose.error("Error while trying to fetch latest version!");
return;
}
};
Tasks.asyncTask(runnable);
if (!WITP.getInstance().getDescription().getVersion().equals(latest)) {
Verbose.info("A new version of WITP is available to download!");
Verbose.info("Newest version: " + latest);
WITP.OUTDATED = true;
} else {
Verbose.info("WITP is currently up-to-date!");
}
});
}

private String getLatestVersion() throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: 'WITP'
description: 'Automatically generating, infinitely long parkour plugin. The sky, and your time, is the limit. The further you go, the harder it gets. Should be a walk in the park, right?'
author: Efnilite, Ice_Pancake
version: 2.0
version: 2.1
api-version: 1.16
main: dev.efnilite.witp.WITP
softdepend: [Vault, PlaceholderAPI]
Expand Down

0 comments on commit 099ccbc

Please sign in to comment.