cooldowns convert + hClaims fix

This commit is contained in:
SuperRonanCraft 2021-09-24 23:39:37 -04:00
parent 86bf26535e
commit 141e857ed8
6 changed files with 98 additions and 7 deletions

View File

@ -97,9 +97,6 @@ public class BetterRTP extends JavaPlugin {
invs.closeAll();
loadAll();
text.getReload(sendi);
//Reload all players cooldowns
for (Player p : Bukkit.getOnlinePlayers())
getCmd().cooldowns.loadPlayer(p.getUniqueId());
}
public RTPInventories getInvs() {

View File

@ -174,6 +174,8 @@ public class Commands {
if (sendi != player || pl.getPerms().getBypassCooldown(player)) { //Bypassing/Forced?
return true;
} else if (cooldowns.isEnabled()) { //Cooling down?
if (!cooldowns.database.isLoaded()) //Cooldowns have yet to download
return false;
UUID id = player.getUniqueId();
if (cooldowns.exists(id)) {
if (cooldowns.locked(id)) { //Infinite cooldown (locked)

View File

@ -337,7 +337,7 @@ public class CmdEdit implements RTPCommand, RTPCommandHelpable { //Edit a worlds
list.add(block.name());
}
} else if (args[2].equalsIgnoreCase("remove")) {
for (String block : BetterRTP.getInstance().getRTP().blockList) {
for (String block : BetterRTP.getInstance().getRTP().getBlockList()) {
if (block.startsWith(args[3]))
list.add(block);
}

View File

@ -4,11 +4,14 @@ import lombok.Getter;
import me.SuperRonanCraft.BetterRTP.references.database.DatabaseCooldowns;
import me.SuperRonanCraft.BetterRTP.references.file.FileBasics;
import me.SuperRonanCraft.BetterRTP.BetterRTP;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
@ -17,14 +20,13 @@ public class RTPCooldown {
private final HashMap<UUID, Long> cooldowns = new HashMap<>(); //Cooldown timer for each player
private HashMap<UUID, Integer> uses = null; //Players locked from rtp'ing ever again
private final DatabaseCooldowns database = new DatabaseCooldowns();
public final DatabaseCooldowns database = new DatabaseCooldowns();
@Getter boolean enabled;
private int
timer, //Cooldown timer
lockedAfter; //Rtp's before being locked
public void load() {
database.load();
//configfile = new File(BetterRTP.getInstance().getDataFolder(), "data/cooldowns.yml");
cooldowns.clear();
if (uses != null)
@ -37,6 +39,13 @@ public class RTPCooldown {
if (lockedAfter > 0)
uses = new HashMap<>();
}
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> {
database.load();
OldCooldownConverter.loadOldCooldowns(database);
//Load any online players cooldowns (mostly after a reload)
for (Player p : Bukkit.getOnlinePlayers())
loadPlayer(p.getUniqueId());
});
}
public void add(UUID id) {
@ -159,4 +168,68 @@ public class RTPCooldown {
if (uses != null)
uses.remove(uuid);
}
static class OldCooldownConverter {
static void loadOldCooldowns(DatabaseCooldowns database) {
File file = new File(BetterRTP.getInstance().getDataFolder(), "data/cooldowns.yml");
YamlConfiguration config = getFile(file);
if (config == null) return;
if (config.getBoolean("Converted")) return;
List<CooldownData> cooldownData = new ArrayList<>();
for (String id : config.getConfigurationSection("").getKeys(false)) {
try {
Long time = config.getLong(id + ".Time");
UUID uuid = UUID.fromString(id);
int uses = config.getInt(id + ".Attempts");
cooldownData.add(new CooldownData(uuid, time, uses));
} catch (IllegalArgumentException e) {
//Invalid UUID
}
}
config.set("Converted", true);
try {
config.save(file);
} catch (IOException e) {
e.printStackTrace();
}
BetterRTP.getInstance().getLogger().info("Cooldowns converting to new database...");
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> {
database.setCooldown(cooldownData);
BetterRTP.getInstance().getLogger().info("Cooldowns have been converted to the new database!");
});
}
private static YamlConfiguration getFile(File configfile) {
if (!configfile.exists()) {
return null;
/*try {
configfile.getParentFile().mkdir();
configfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}*/
}
try {
YamlConfiguration config = new YamlConfiguration();
config.load(configfile);
return config;
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
return null;
}
}
public static class CooldownData {
public UUID uuid;
public Long time;
public int uses;
CooldownData(UUID uuid, Long time, int uses) {
this.uuid = uuid;
this.time = time;
this.uses = uses;
}
}
}

View File

@ -178,7 +178,7 @@ public class RTPPluginValidation { //Safe locations depending on enabled depende
boolean result = true;
if (getDepends().ishClaims())
try {
result = ClaimAPI.isClaimed(loc);
result = ClaimAPI.getInstance().isClaimed(loc);
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -1,6 +1,7 @@
package me.SuperRonanCraft.BetterRTP.references.database;
import me.SuperRonanCraft.BetterRTP.BetterRTP;
import me.SuperRonanCraft.BetterRTP.player.rtp.RTPCooldown;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -81,4 +82,22 @@ public class DatabaseCooldowns extends SQLite {
}};
sqlUpdate(sql, params);
}
//Update multiple players cooldowns
public void setCooldown(List<RTPCooldown.CooldownData> cooldownData) {
String pre = "INSERT OR REPLACE INTO ";
String sql = pre + table + " ("
+ COLUMNS.UUID.name + ", "
+ COLUMNS.COOLDOWN_DATE.name + ", "
+ COLUMNS.USES.name + " "
+ ") VALUES(?, ?, ?)";
for (RTPCooldown.CooldownData data : cooldownData) {
List<Object> param = new ArrayList<>() {{
add(data.uuid.toString());
add(data.time);
add(data.uses);
}};
sqlUpdate(sql, param);
}
}
}