mirror of
https://github.com/RonanPlugins/BetterRTP.git
synced 2025-08-17 17:15:47 +00:00
Cooldowns database async saving/loading + cooldowns updated to new system
This commit is contained in:
parent
7f388e6c6b
commit
d3bb025068
2
pom.xml
2
pom.xml
@ -7,7 +7,7 @@
|
||||
<groupId>me.SuperRonanCraft</groupId>
|
||||
<artifactId>BetterRTP</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>3.1.1</version>
|
||||
<version>3.2.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
|
@ -6,6 +6,7 @@ import me.SuperRonanCraft.BetterRTP.player.commands.Commands;
|
||||
import me.SuperRonanCraft.BetterRTP.player.events.Listener;
|
||||
import me.SuperRonanCraft.BetterRTP.player.rtp.RTP;
|
||||
import me.SuperRonanCraft.BetterRTP.references.Permissions;
|
||||
import me.SuperRonanCraft.BetterRTP.references.Updater;
|
||||
import me.SuperRonanCraft.BetterRTP.references.database.DatabaseCooldowns;
|
||||
import me.SuperRonanCraft.BetterRTP.references.depends.DepEconomy;
|
||||
import me.SuperRonanCraft.BetterRTP.references.file.Files;
|
||||
@ -14,11 +15,8 @@ import me.SuperRonanCraft.BetterRTP.references.invs.RTPInventories;
|
||||
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.CooldownHandler;
|
||||
import me.SuperRonanCraft.BetterRTP.references.settings.Settings;
|
||||
import me.SuperRonanCraft.BetterRTP.references.web.Metrics;
|
||||
import me.SuperRonanCraft.BetterRTP.references.Updater;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.List;
|
||||
@ -35,7 +33,7 @@ public class BetterRTP extends JavaPlugin {
|
||||
private final RTPInventories invs = new RTPInventories();
|
||||
private final PlayerInfo pInfo = new PlayerInfo();
|
||||
private final Settings settings = new Settings();
|
||||
@Getter private final DatabaseCooldowns database = new DatabaseCooldowns();
|
||||
@Getter private final DatabaseCooldowns databaseCooldowns = new DatabaseCooldowns();
|
||||
@Getter private final CooldownHandler cooldowns = new CooldownHandler();
|
||||
|
||||
public void onEnable() {
|
||||
@ -114,6 +112,7 @@ public class BetterRTP extends JavaPlugin {
|
||||
|
||||
//(Re)Load all plugin systems/files/cache
|
||||
private void loadAll() {
|
||||
pInfo.unloadAll();
|
||||
files.loadAll();
|
||||
settings.load();
|
||||
invs.load();
|
||||
|
@ -9,6 +9,7 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerInfo {
|
||||
|
||||
@ -17,6 +18,7 @@ public class PlayerInfo {
|
||||
@Getter private final HashMap<Player, World> invWorld = new HashMap<>();
|
||||
@Getter private final HashMap<Player, RTP_INV_SETTINGS> invNextInv = new HashMap<>();
|
||||
@Getter private final HashMap<Player, CooldownData> cooldown = new HashMap<>();
|
||||
@Getter private final HashMap<Player, Boolean> rtping = new HashMap<>();
|
||||
//private final HashMap<Player, RTP_TYPE> rtpType = new HashMap<>();
|
||||
|
||||
public void setInv(Player p, Inventory inv) {
|
||||
@ -41,18 +43,25 @@ public class PlayerInfo {
|
||||
return invs.containsKey(p);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
public void unloadAll() {
|
||||
invs.clear();
|
||||
invType.clear();
|
||||
invWorld.clear();
|
||||
invNextInv.clear();
|
||||
cooldown.clear();
|
||||
rtping.clear();
|
||||
}
|
||||
|
||||
public void clear(Player p) {
|
||||
public void unload(Player p) {
|
||||
clearInvs(p);
|
||||
cooldown.remove(p);
|
||||
rtping.remove(p);
|
||||
}
|
||||
|
||||
public void clearInvs(Player p) {
|
||||
invs.remove(p);
|
||||
invType.remove(p);
|
||||
invWorld.remove(p);
|
||||
invNextInv.remove(p);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package me.SuperRonanCraft.BetterRTP.player.commands;
|
||||
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.CooldownData;
|
||||
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.CooldownHandler;
|
||||
import me.SuperRonanCraft.BetterRTP.player.rtp.RTPSetupInformation;
|
||||
import me.SuperRonanCraft.BetterRTP.player.rtp.RTP_TYPE;
|
||||
@ -13,14 +14,11 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Commands {
|
||||
|
||||
private final BetterRTP pl;
|
||||
public HashMap<UUID, Boolean> rtping = new HashMap<>();
|
||||
private int delayTimer;
|
||||
public List<RTPCommand> commands = new ArrayList<>();
|
||||
|
||||
@ -31,7 +29,6 @@ public class Commands {
|
||||
public void load() {
|
||||
FileBasics.FILETYPE config = FileBasics.FILETYPE.CONFIG;
|
||||
delayTimer = config.getInt("Settings.Delay.Time");
|
||||
rtping.clear();
|
||||
commands.clear();
|
||||
for (RTPCommandType cmd : RTPCommandType.values())
|
||||
registerCommand(cmd.getCmd(), false);
|
||||
@ -154,14 +151,16 @@ public class Commands {
|
||||
if (pl.getSettings().delayEnabled && delayTimer > 0) //Delay enabled?
|
||||
if (!pl.getPerms().getBypassDelay(player)) //Can bypass?
|
||||
delay = true;
|
||||
RTPSetupInformation setup_info = new RTPSetupInformation(world, sendi, player, true, biomes, delay, rtpType, locations);
|
||||
//player.sendMessage("Cooldown applies: " + cooldownApplies(sendi, player));
|
||||
RTPSetupInformation setup_info = new RTPSetupInformation(world, sendi, player, true,
|
||||
biomes, delay, rtpType, locations, !ignoreCooldown && cooldownApplies(sendi, player)); //ignore cooldown or else
|
||||
pl.getRTP().start(setup_info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkRTPing(Player player, CommandSender sendi) {
|
||||
if (rtping.containsKey(player.getUniqueId()) && rtping.get(player.getUniqueId())) {
|
||||
if (getPl().getpInfo().getRtping().containsKey(player) && getPl().getpInfo().getRtping().get(player)) {
|
||||
pl.getText().getAlready(sendi);
|
||||
return false;
|
||||
}
|
||||
@ -169,18 +168,20 @@ public class Commands {
|
||||
}
|
||||
|
||||
private boolean checkCooldown(CommandSender sendi, Player player) {
|
||||
if (sendi != player || pl.getPerms().getBypassCooldown(player)) { //Bypassing/Forced?
|
||||
return true;
|
||||
} else if (getPl().getCo.isEnabled()) { //Cooling down?
|
||||
if (!.database.isLoaded()) //Cooldowns have yet to download
|
||||
if (cooldownApplies(sendi, player)) { //Bypassing/Forced?
|
||||
CooldownHandler cooldownHandler = getPl().getCooldowns();
|
||||
if (!cooldownHandler.isLoaded() || !cooldownHandler.loadedPlayer(player)) { //Cooldowns have yet to download
|
||||
pl.getText().getCooldown(sendi, String.valueOf(-1L));
|
||||
return false;
|
||||
UUID id = player.getUniqueId();
|
||||
if (cooldowns.exists(id)) {
|
||||
if (cooldowns.locked(id)) { //Infinite cooldown (locked)
|
||||
}
|
||||
//Cooldown Data
|
||||
CooldownData cooldownData = getPl().getCooldowns().getPlayer(player);
|
||||
if (cooldownData != null) {
|
||||
if (cooldownHandler.locked(cooldownData)) { //Infinite cooldown (locked)
|
||||
pl.getText().getNoPermission(sendi);
|
||||
return false;
|
||||
} else { //Normal cooldown
|
||||
long Left = cooldowns.timeLeft(id);
|
||||
long Left = cooldownHandler.timeLeft(cooldownData);
|
||||
if (pl.getSettings().delayEnabled && !pl.getPerms().getBypassDelay(sendi))
|
||||
Left = Left + delayTimer;
|
||||
if (Left > 0) {
|
||||
@ -199,6 +200,18 @@ public class Commands {
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean cooldownOverride(CommandSender sendi, Player player) {
|
||||
return sendi != player || pl.getPerms().getBypassCooldown(player);
|
||||
}
|
||||
|
||||
private boolean cooldownEnabled() {
|
||||
return getPl().getCooldowns().isEnabled();
|
||||
}
|
||||
|
||||
private boolean cooldownApplies(CommandSender sendi, Player player) {
|
||||
return cooldownEnabled() && !cooldownOverride(sendi, player);
|
||||
}
|
||||
|
||||
private BetterRTP getPl() {
|
||||
return BetterRTP.getInstance();
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ public class Click {
|
||||
PlayerInfo pInfo = BetterRTP.getInstance().getpInfo();
|
||||
Player p = (Player) e.getWhoClicked();
|
||||
RTPInventories menu = BetterRTP.getInstance().getInvs();
|
||||
menu.getInv(pInfo.getInvType(p)).clickEvent(e);
|
||||
menu.getInv(pInfo.getInvType().get(p)).clickEvent(e);
|
||||
} catch (NullPointerException ex) {
|
||||
//ex.printStackTrace();
|
||||
}
|
||||
@ -36,10 +36,10 @@ public class Click {
|
||||
return false;
|
||||
else if (e.getWhoClicked() instanceof Player) {
|
||||
// Clicks the inventory
|
||||
if (!e.getInventory().equals(BetterRTP.getInstance().getpInfo().getInv((Player) e.getWhoClicked())))
|
||||
if (!e.getInventory().equals(BetterRTP.getInstance().getpInfo().getInvs().get((Player) e.getWhoClicked())))
|
||||
return false;
|
||||
// Clicks their own inventory
|
||||
else if (!e.getClickedInventory().equals(BetterRTP.getInstance().getpInfo().getInv((Player) e
|
||||
else if (!e.getClickedInventory().equals(BetterRTP.getInstance().getpInfo().getInvs().get((Player) e
|
||||
.getWhoClicked()))) {
|
||||
e.setCancelled(true);
|
||||
return false;
|
||||
|
@ -13,7 +13,9 @@ public class Join {
|
||||
void event(PlayerJoinEvent e) {
|
||||
Player p = e.getPlayer();
|
||||
updater(p);
|
||||
getPl().getCmd().cooldowns.loadPlayer(p.getUniqueId());
|
||||
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> {
|
||||
getPl().getCooldowns().loadPlayer(p);
|
||||
});
|
||||
rtpOnFirstJoin(p);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package me.SuperRonanCraft.BetterRTP.player.events;
|
||||
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.util.UUID;
|
||||
@ -9,8 +10,8 @@ class Leave {
|
||||
|
||||
void event(PlayerQuitEvent e) {
|
||||
BetterRTP pl = BetterRTP.getInstance();
|
||||
UUID id = e.getPlayer().getUniqueId();
|
||||
pl.getCmd().rtping.remove(id);
|
||||
pl.getCmd().cooldowns.unloadPlayer(id);
|
||||
Player p = e.getPlayer();
|
||||
pl.getpInfo().getRtping().remove(p);
|
||||
pl.getpInfo().unload(p);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
package me.SuperRonanCraft.BetterRTP.player.rtp;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.SuperRonanCraft.BetterRTP.references.file.FileBasics;
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import me.SuperRonanCraft.BetterRTP.references.file.FileBasics;
|
||||
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.*;
|
||||
import me.SuperRonanCraft.BetterRTP.references.worlds.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -156,14 +155,16 @@ public class RTP {
|
||||
// Economy
|
||||
if (!getPl().getEco().hasBalance(sendi, pWorld))
|
||||
return;
|
||||
rtp(sendi, pWorld, setup_info.delay, setup_info.rtp_type);
|
||||
rtp(sendi, pWorld, setup_info.delay, setup_info.rtp_type, setup_info.cooldown);
|
||||
}
|
||||
|
||||
private void rtp(CommandSender sendi, WorldPlayer pWorld, boolean delay, RTP_TYPE type) {
|
||||
private void rtp(CommandSender sendi, WorldPlayer pWorld, boolean delay, RTP_TYPE type, boolean cooldown) {
|
||||
//Cooldown
|
||||
Player p = pWorld.getPlayer();
|
||||
getPl().getCmd().cooldowns.add(p.getUniqueId());
|
||||
getPl().getCmd().rtping.put(p.getUniqueId(), true); //Cache player so they cant run '/rtp' again while rtp'ing
|
||||
//p.sendMessage("Cooling down: " + cooldown);
|
||||
if (cooldown)
|
||||
getPl().getCooldowns().add(p);
|
||||
getPl().getpInfo().getRtping().put(p, true); //Cache player so they cant run '/rtp' again while rtp'ing
|
||||
//Setup player rtp methods
|
||||
RTPPlayer rtpPlayer = new RTPPlayer(p, this, pWorld, type);
|
||||
// Delaying? Else, just go
|
||||
|
@ -61,7 +61,7 @@ class RTPDelay implements Listener {
|
||||
getPl().getRTP().getTeleport().cancelledTeleport(rtp.getPlayer());
|
||||
//getPl().getEco().unCharge(rtp.getPlayer(), rtp.pWorld);
|
||||
getPl().getCooldowns().removeCooldown(rtp.getPlayer());
|
||||
getPl().getCmd().rtping.put(rtp.getPlayer().getUniqueId(), false);
|
||||
getPl().getpInfo().getRtping().put(rtp.getPlayer(), false);
|
||||
Bukkit.getServer().getPluginManager().callEvent(new RTP_CancelledEvent(rtp.getPlayer()));
|
||||
}
|
||||
}
|
||||
@ -69,7 +69,7 @@ class RTPDelay implements Listener {
|
||||
private Runnable run(final CommandSender sendi, final RTPDelay cls) {
|
||||
return () -> {
|
||||
HandlerList.unregisterAll(cls);
|
||||
if (getPl().getCmd().rtping.containsKey(rtp.getPlayer().getUniqueId()))
|
||||
if (getPl().getpInfo().getRtping().containsKey(rtp.getPlayer()))
|
||||
rtp.randomlyTeleport(sendi);
|
||||
};
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package me.SuperRonanCraft.BetterRTP.player.rtp;
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import me.SuperRonanCraft.BetterRTP.references.file.FileBasics;
|
||||
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.*;
|
||||
import me.SuperRonanCraft.BetterRTP.references.worlds.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -68,7 +67,7 @@ public class RTPLoader {
|
||||
valids.append(type.name()).append(", ");
|
||||
valids.replace(valids.length() - 2, valids.length(), "");
|
||||
getPl().getLogger().severe("World Type for '" + entry.getKey() + "' is INVALID '" + entry.getValue() +
|
||||
"'. Valid ID's are: " + valids.toString());
|
||||
"'. Valid ID's are: " + valids);
|
||||
//Wrong rtp world type
|
||||
}
|
||||
}/* else {
|
||||
|
@ -79,9 +79,9 @@ public class RTPPlayer {
|
||||
getPl().getText().getFailedNotSafe(sendi, settings.maxAttempts);
|
||||
else
|
||||
getPl().getText().getOtherNotSafe(sendi, settings.maxAttempts, p.getName());*/
|
||||
getPl().getCmd().cooldowns.removeCooldown(p.getUniqueId());
|
||||
getPl().getCooldowns().removeCooldown(p);
|
||||
//getPl().getEco().unCharge(p, pWorld);
|
||||
getPl().getCmd().rtping.put(p.getUniqueId(), false);
|
||||
getPl().getpInfo().getRtping().put(p, false);
|
||||
}
|
||||
|
||||
private Location getLocAtNormal(int x, int z, World world, Float yaw, Float pitch, WorldPlayer pWorld) {
|
||||
|
@ -11,7 +11,7 @@ public class RTPSetupInformation {
|
||||
public String world;
|
||||
public CommandSender sender;
|
||||
public Player player;
|
||||
public boolean personalized;
|
||||
public boolean personalized, cooldown;
|
||||
public List<String> biomes;
|
||||
public WorldLocations location;
|
||||
public boolean delay;
|
||||
@ -22,7 +22,12 @@ public class RTPSetupInformation {
|
||||
}
|
||||
|
||||
public RTPSetupInformation(String world, CommandSender sender, Player player, boolean personalized, List<String> biomes,
|
||||
boolean delay, RTP_TYPE rtp_type, WorldLocations location) {
|
||||
boolean delay, RTP_TYPE rtp_type, WorldLocations location) {
|
||||
this(world, sender, player, personalized, biomes, delay, rtp_type, location, true);
|
||||
}
|
||||
|
||||
public RTPSetupInformation(String world, CommandSender sender, Player player, boolean personalized, List<String> biomes,
|
||||
boolean delay, RTP_TYPE rtp_type, WorldLocations location, boolean cooldown) {
|
||||
this.world = world;
|
||||
this.sender = sender;
|
||||
this.player = player;
|
||||
@ -37,5 +42,6 @@ public class RTPSetupInformation {
|
||||
else if (this.location != null)
|
||||
this.world = this.location.getWorld().getName();
|
||||
}
|
||||
this.cooldown = cooldown;
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ public class RTPTeleport {
|
||||
afterTeleport(p, loc, price, attempts, oldLoc, type);
|
||||
if (sendi != p) //Tell player who requested that the player rtp'd
|
||||
sendSuccessMsg(sendi, p.getName(), loc, price, false, attempts);
|
||||
getPl().getCmd().rtping.remove(p.getUniqueId()); //No longer rtp'ing
|
||||
getPl().getpInfo().getRtping().remove(p); //No longer rtp'ing
|
||||
//Save respawn location if first join
|
||||
if (type == RTP_TYPE.JOIN) //RTP Type was Join
|
||||
if (BetterRTP.getInstance().getSettings().rtpOnFirstJoin_SetAsRespawn) //Save as respawn is enabled
|
||||
@ -70,7 +70,7 @@ public class RTPTeleport {
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
getPl().getCmd().rtping.remove(p.getUniqueId()); //No longer rtp'ing (errored)
|
||||
getPl().getpInfo().getRtping().remove(p); //No longer rtp'ing (errored)
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ public class DatabaseCooldowns extends SQLite {
|
||||
return sqlUpdate(sql, params);
|
||||
}
|
||||
|
||||
public List<Object> getCooldown(UUID uuid) {
|
||||
public CooldownData getCooldown(UUID uuid) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@ -54,10 +54,9 @@ public class DatabaseCooldowns extends SQLite {
|
||||
|
||||
rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
List<Object> data = new ArrayList<>();
|
||||
data.add(rs.getLong(COLUMNS.COOLDOWN_DATE.name));
|
||||
data.add(rs.getInt(COLUMNS.USES.name));
|
||||
return data;
|
||||
Long time = rs.getLong(COLUMNS.COOLDOWN_DATE.name);
|
||||
int uses = rs.getInt(COLUMNS.USES.name);
|
||||
return new CooldownData(uuid, time, uses);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
BetterRTP.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
@ -93,9 +92,9 @@ public class DatabaseCooldowns extends SQLite {
|
||||
+ ") VALUES(?, ?, ?)";
|
||||
for (CooldownData data : cooldownData) {
|
||||
List<Object> param = new ArrayList<>() {{
|
||||
add(data.uuid.toString());
|
||||
add(data.time);
|
||||
add(data.uses);
|
||||
add(data.getUuid().toString());
|
||||
add(data.getTime());
|
||||
add(data.getUses());
|
||||
}};
|
||||
sqlUpdate(sql, param);
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class RTPInventories {
|
||||
for (Player p : Bukkit.getOnlinePlayers())
|
||||
if (main.getpInfo().playerExists(p)) {
|
||||
//main.getText().getReloadMenu(p);
|
||||
main.getpInfo().clear(p);
|
||||
main.getpInfo().clearInvs(p);
|
||||
p.closeInventory();
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import java.util.ArrayList;
|
||||
public class RTPInvCoordinates extends RTPInventory {
|
||||
|
||||
public void show(Player p) {
|
||||
if (BetterRTP.getInstance().getpInfo().getInvWorld(p) == null) {
|
||||
if (BetterRTP.getInstance().getpInfo().getInvWorld().get(p) == null) {
|
||||
BetterRTP.getInstance().getpInfo().setNextInv(p, this.type);
|
||||
BetterRTP.getInstance().getInvs().getInv(RTP_INV_SETTINGS.WORLDS).show(p);
|
||||
return;
|
||||
|
@ -53,7 +53,7 @@ public class RTPInvWorlds extends RTPInventory {
|
||||
for (World world : Bukkit.getWorlds()) {
|
||||
if (_index == slot) {
|
||||
BetterRTP.getInstance().getpInfo().setInvWorld(p, world);
|
||||
BetterRTP.getInstance().getInvs().getInv(BetterRTP.getInstance().getpInfo().getNextInv(p)).show(p);
|
||||
BetterRTP.getInstance().getInvs().getInv(BetterRTP.getInstance().getpInfo().getInvNextInv().get(p)).show(p);
|
||||
}
|
||||
_index ++;
|
||||
}
|
||||
|
@ -5,12 +5,12 @@ import me.SuperRonanCraft.BetterRTP.player.PlayerInfo;
|
||||
import me.SuperRonanCraft.BetterRTP.references.database.DatabaseCooldowns;
|
||||
import me.SuperRonanCraft.BetterRTP.references.file.FileBasics;
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.CooldownData;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@ -20,55 +20,60 @@ import java.util.UUID;
|
||||
|
||||
public class CooldownHandler {
|
||||
|
||||
//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
|
||||
@Getter boolean enabled;
|
||||
@Getter boolean enabled, loaded;
|
||||
private int
|
||||
timer, //Cooldown timer
|
||||
lockedAfter; //Rtp's before being locked
|
||||
private final List<Player> downloading = new ArrayList<>();
|
||||
|
||||
public void load() {
|
||||
//configfile = new File(BetterRTP.getInstance().getDataFolder(), "data/cooldowns.yml");
|
||||
getPInfo().getCooldown().clear();
|
||||
FileBasics.FILETYPE config = FileBasics.FILETYPE.CONFIG;
|
||||
enabled = config.getBoolean("Settings.Cooldown.Enabled");
|
||||
downloading.clear();
|
||||
loaded = false;
|
||||
if (enabled) {
|
||||
timer = config.getInt("Settings.Cooldown.Time");
|
||||
lockedAfter = config.getInt("Settings.Cooldown.LockAfter");
|
||||
//if (lockedAfter > 0)
|
||||
// uses = new HashMap<>();
|
||||
}
|
||||
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> {
|
||||
getDatabase().load();
|
||||
OldCooldownConverter.loadOldCooldowns();
|
||||
//Load any online players cooldowns (mostly after a reload)
|
||||
for (Player p : Bukkit.getOnlinePlayers())
|
||||
loadPlayer(p.getUniqueId());
|
||||
loadPlayer(p);
|
||||
loaded = true;
|
||||
});
|
||||
}
|
||||
|
||||
public void add(Player player) {
|
||||
if (!enabled) return;
|
||||
CooldownData data = getPInfo().getCooldown().getOrDefault(player,
|
||||
new CooldownData(player.getUniqueId(), System.currentTimeMillis(), 0));
|
||||
new CooldownData(player.getUniqueId(), 0L, 0));
|
||||
if (lockedAfter > 0)
|
||||
data.setUses(data.getUses() + 1);
|
||||
savePlayer(data);
|
||||
data.setTime(System.currentTimeMillis());
|
||||
getPInfo().getCooldown().put(player, data);
|
||||
savePlayer(data, false);
|
||||
}
|
||||
|
||||
public boolean exists(UUID id) {
|
||||
return cooldowns.containsKey(id);
|
||||
public boolean exists(Player p) {
|
||||
return getPInfo().getCooldown().containsKey(p);
|
||||
}
|
||||
|
||||
public long timeLeft(UUID id) {
|
||||
long cooldown = cooldowns.getOrDefault(id, 0L);
|
||||
@Nullable
|
||||
public CooldownData getPlayer(Player p) {
|
||||
return getPInfo().getCooldown().getOrDefault(p, null);
|
||||
}
|
||||
|
||||
public long timeLeft(CooldownData data) {
|
||||
long cooldown = data.getTime();
|
||||
return ((cooldown / 1000) + timer) - (System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
public boolean locked(UUID id) {
|
||||
if (uses != null && uses.containsKey(id))
|
||||
return uses.get(id) >= lockedAfter;
|
||||
return false;
|
||||
public boolean locked(CooldownData data) {
|
||||
return lockedAfter > 0 && data.uses >= lockedAfter;
|
||||
}
|
||||
|
||||
public void removeCooldown(Player player) {
|
||||
@ -90,82 +95,30 @@ public class CooldownHandler {
|
||||
}
|
||||
|
||||
private void savePlayer(CooldownData data, boolean remove) {
|
||||
if (!remove) {
|
||||
getDatabase().setCooldown(data);
|
||||
} else {
|
||||
getDatabase().removePlayer(data.getUuid());
|
||||
}
|
||||
/*YamlConfiguration config = getFile();
|
||||
if (config == null) {
|
||||
BetterRTP.getInstance().getLogger().severe("Unabled to save cooldown for the players UUID " + id
|
||||
+ ". Cooldown file might have been compromised!");
|
||||
return;
|
||||
}
|
||||
if (adding) { //Add player to file
|
||||
config.set(id.toString() + ".Time", time);
|
||||
if (attempts > 0)
|
||||
config.set(id.toString() + ".Attempts", attempts);
|
||||
} else { //Remove player from the file
|
||||
config.set(id.toString(), null);
|
||||
}
|
||||
try {
|
||||
config.save(configfile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}*/
|
||||
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> {
|
||||
if (!remove) {
|
||||
getDatabase().setCooldown(data);
|
||||
} else {
|
||||
getDatabase().removePlayer(data.getUuid());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*private File configfile;
|
||||
|
||||
/*private YamlConfiguration getFile() {
|
||||
if (!configfile.exists()) {
|
||||
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 void loadPlayer(UUID uuid) {
|
||||
public void loadPlayer(Player player) {
|
||||
downloading.add(player);
|
||||
if (isEnabled()) {
|
||||
List<Object> data = getDatabase().getCooldown(uuid);
|
||||
if (data != null) {
|
||||
cooldowns.put(uuid, (Long) data.get(0));
|
||||
uses.put(uuid, (int) data.get(1));
|
||||
}
|
||||
/*String id = uuid.toString();
|
||||
YamlConfiguration config = getFile();
|
||||
if (config != null && config.isConfigurationSection(id))
|
||||
try {
|
||||
Long time = config.getLong(id + ".Time");
|
||||
cooldowns.put(uuid, time);
|
||||
if (lockedAfter > 0) {
|
||||
int attempts = config.getInt(id + ".Attempts");
|
||||
uses.put(uuid, attempts);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
BetterRTP.getInstance().getLogger().info("UUID of `" + id + "` is invalid, please delete this!");
|
||||
//Bad uuid
|
||||
}*/
|
||||
CooldownData data = getDatabase().getCooldown(player.getUniqueId());
|
||||
if (data != null)
|
||||
getPInfo().getCooldown().put(player, data);
|
||||
}
|
||||
downloading.remove(player);
|
||||
}
|
||||
|
||||
public void unloadPlayer(UUID uuid) {
|
||||
cooldowns.remove(uuid);
|
||||
if (uses != null)
|
||||
uses.remove(uuid);
|
||||
public boolean loadedPlayer(Player player) {
|
||||
return !downloading.contains(player);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
static class OldCooldownConverter {
|
||||
|
||||
static void loadOldCooldowns() {
|
||||
@ -192,7 +145,7 @@ public class CooldownHandler {
|
||||
}
|
||||
BetterRTP.getInstance().getLogger().info("Cooldowns converting to new database...");
|
||||
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> {
|
||||
BetterRTP.getInstance().getDatabase().setCooldown(cooldownData);
|
||||
BetterRTP.getInstance().getDatabaseCooldowns().setCooldown(cooldownData);
|
||||
BetterRTP.getInstance().getLogger().info("Cooldowns have been converted to the new database!");
|
||||
});
|
||||
}
|
||||
@ -219,7 +172,7 @@ public class CooldownHandler {
|
||||
}
|
||||
|
||||
private DatabaseCooldowns getDatabase() {
|
||||
return BetterRTP.getInstance().getDatabase();
|
||||
return BetterRTP.getInstance().getDatabaseCooldowns();
|
||||
}
|
||||
|
||||
private PlayerInfo getPInfo() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user