Cooldowns save and lock

- Cooldowns work on first rtp after start-up
- Cooldowns save after restart/reload
- Cannot rtp (locked) after certain amount of rtps
- Added Rtp on first join
This commit is contained in:
SuperRonanCraft 2020-08-10 17:56:03 -04:00
parent b394f37f93
commit 2ffe2b5cb3
11 changed files with 151 additions and 30 deletions

View File

@ -129,17 +129,20 @@ public class Commands {
} }
private boolean checkDelay(CommandSender sendi, Player player) { private boolean checkDelay(CommandSender sendi, Player player) {
if (rtping.containsKey(player.getUniqueId())) //Already rtp'ing if (rtping.containsKey(player.getUniqueId()) && rtping.get(player.getUniqueId())) {
if (rtping.get(player.getUniqueId())) {
pl.getText().getAlready(player); pl.getText().getAlready(player);
return false; return false;
} } else if (sendi != player || pl.getPerms().getBypassCooldown(player)) { //Bypassing/Forced?
else if (sendi != player || pl.getPerms().getBypassCooldown(player)) //Bypassing/Forced?
return true; return true;
else if (cooldowns.enabled) { //Cooling down? } else if (cooldowns.enabled) { //Cooling down?
Player p = (Player) sendi; Player p = (Player) sendi;
if (cooldowns.exists(p.getUniqueId())) { UUID id = p.getUniqueId();
long Left = cooldowns.timeLeft(p.getUniqueId()); if (cooldowns.exists(id)) {
if (cooldowns.locked(id)) { //Infinite cooldown
pl.getText().getNoPermission(sendi);
return false;
} else { //Normal cooldown
long Left = cooldowns.timeLeft(id);
if (!pl.getPerms().getBypassDelay(p)) if (!pl.getPerms().getBypassDelay(p))
Left = Left + delayTimer; Left = Left + delayTimer;
if (Left > 0) { if (Left > 0) {
@ -148,11 +151,12 @@ public class Commands {
return false; return false;
} else { } else {
//Reset timer, but allow them to tp //Reset timer, but allow them to tp
cooldowns.add(p.getUniqueId()); cooldowns.add(id);
return true; return true;
} }
}
} else } else
cooldowns.add(p.getUniqueId()); cooldowns.add(id);
} }
return true; return true;
} }

View File

@ -3,6 +3,7 @@ package me.SuperRonanCraft.BetterRTP.player.events;
import me.SuperRonanCraft.BetterRTP.references.Updater; import me.SuperRonanCraft.BetterRTP.references.Updater;
import me.SuperRonanCraft.BetterRTP.references.file.FileBasics; import me.SuperRonanCraft.BetterRTP.references.file.FileBasics;
import me.SuperRonanCraft.BetterRTP.Main; import me.SuperRonanCraft.BetterRTP.Main;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
@ -24,9 +25,8 @@ public class Join {
//RTP on first join //RTP on first join
private void rtpOnFirstJoin(Player p) { private void rtpOnFirstJoin(Player p) {
if (getPl().getSettings().firstJoinRtp && !p.hasPlayedBefore()) { if (getPl().getSettings().rtpOnFirstJoin && !p.hasPlayedBefore())
getPl().getCmd().tp(p, Bukkit.getConsoleSender(), getPl().getSettings().rtpOnFirstJoinWorld, null); //Console is sender to override delays
}
} }
private Main getPl() { private Main getPl() {

View File

@ -1,35 +1,142 @@
package me.SuperRonanCraft.BetterRTP.player.rtp; package me.SuperRonanCraft.BetterRTP.player.rtp;
import me.SuperRonanCraft.BetterRTP.Main;
import me.SuperRonanCraft.BetterRTP.references.file.FileBasics; import me.SuperRonanCraft.BetterRTP.references.file.FileBasics;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
public class RTPCooldown { public class RTPCooldown {
public HashMap<UUID, Long> cooldowns = new HashMap<>(); private HashMap<UUID, Long> cooldowns = new HashMap<>(); //Cooldown timer for each player
private HashMap<UUID, Integer> locked = null; //Players locked from rtp'ing ever again
public boolean enabled; public boolean enabled;
private int timer; private int
timer, //Cooldown timer
lockedAfter; //Rtp's before being locked
public void load() { public void load() {
cooldowns.clear();
if (locked != null)
locked.clear();
FileBasics.FILETYPE config = FileBasics.FILETYPE.CONFIG; FileBasics.FILETYPE config = FileBasics.FILETYPE.CONFIG;
enabled = config.getBoolean("Settings.Cooldown.Enabled"); enabled = config.getBoolean("Settings.Cooldown.Enabled");
if (enabled) {
timer = config.getInt("Settings.Cooldown.Time"); timer = config.getInt("Settings.Cooldown.Time");
lockedAfter = config.getInt("Settings.Cooldown.LockAfter");
if (lockedAfter > 0)
locked = new HashMap<>();
loadFile();
}
} }
public void add(UUID id) { public void add(UUID id) {
cooldowns.put(id, System.currentTimeMillis()); cooldowns.put(id, System.currentTimeMillis());
if (lockedAfter > 0) {
if (locked.containsKey(id))
locked.put(id, locked.get(id) + 1);
else
locked.put(id, 1);
savePlayer(id, true, cooldowns.get(id), locked.get(id));
} else
savePlayer(id, true, cooldowns.get(id), 0);
} }
public boolean exists(UUID id) { public boolean exists(UUID id) {
System.out.println("Exists " + id + " " + cooldowns.containsKey(id));
return cooldowns.containsKey(id); return cooldowns.containsKey(id);
} }
public long timeLeft(UUID id) { public long timeLeft(UUID id) {
return ((cooldowns.getOrDefault(id, (long) 0) / 1000) + timer) - (System.currentTimeMillis() / 1000); long cooldown = cooldowns.getOrDefault(id, 0L);
return ((cooldown / 1000) + timer) - (System.currentTimeMillis() / 1000);
}
public boolean locked(UUID id) {
if (locked != null && locked.containsKey(id))
return locked.get(id) >= lockedAfter;
return false;
} }
public void remove(UUID id) { public void remove(UUID id) {
if (lockedAfter > 0) {
locked.put(id, locked.getOrDefault(id, 1) - 1);
if (locked.get(id) <= 0) { //Remove completely
cooldowns.remove(id); cooldowns.remove(id);
savePlayer(id, false, 0L, 0);
} else { //Keep the player cached
savePlayer(id, false, cooldowns.get(id), locked.get(id));
}
} else { //Remove completely
cooldowns.remove(id);
savePlayer(id, false, 0L, 0);
}
}
private void savePlayer(UUID id, boolean adding, long time, int attempts) {
YamlConfiguration config = getFile();
assert config != null;
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();
}
}
private YamlConfiguration config;
private File configfile;
private YamlConfiguration getFile() {
if (config != null) {
return config;
} else {
if (!configfile.exists()) {
try {
configfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
config = new YamlConfiguration();
config.load(configfile);
return config;
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
return null;
}
}
private void loadFile() {
config = null;
configfile = new File(Main.getInstance().getDataFolder(), "cooldowns.yml");
YamlConfiguration config = getFile();
assert config != null;
for (String id : config.getKeys(false)) {
try {
UUID uuid = UUID.fromString(id);
Long time = config.getLong(id + ".Time");
cooldowns.put(uuid, time);
if (lockedAfter > 0) {
int attempts = config.getInt(id + ".Attempts");
locked.put(uuid, attempts);
}
} catch (IllegalArgumentException e) {
Main.getInstance().getLogger().info("UUID of `" + id + "` is invalid, please delete this!");
//Bad uuid
}
}
} }
} }

View File

@ -7,7 +7,8 @@ public class Settings {
public boolean debug; public boolean debug;
public boolean delayEnabled; public boolean delayEnabled;
public boolean firstJoinRtp; public boolean rtpOnFirstJoin;
public String rtpOnFirstJoinWorld;
//Dependencies //Dependencies
private SoftDepends depends = new SoftDepends(); private SoftDepends depends = new SoftDepends();
@ -16,7 +17,8 @@ public class Settings {
FileBasics.FILETYPE config = getPl().getFiles().getType(FileBasics.FILETYPE.CONFIG); FileBasics.FILETYPE config = getPl().getFiles().getType(FileBasics.FILETYPE.CONFIG);
debug = config.getBoolean("Settings.Debugger"); debug = config.getBoolean("Settings.Debugger");
delayEnabled = config.getBoolean("Settings.Delay.Enabled"); delayEnabled = config.getBoolean("Settings.Delay.Enabled");
firstJoinRtp = config.getBoolean("Settings.FirstJoinRtp"); rtpOnFirstJoin = config.getBoolean("Settings.RtpOnFirstJoin.Enabled");
rtpOnFirstJoinWorld = config.getString("Settings.RtpOnFirstJoin.World");
} }
public SoftDepends getsDepends() { public SoftDepends getsDepends() {

View File

@ -16,7 +16,9 @@ Settings:
MinRadius: 25 MinRadius: 25
## Maximum amount of tries before BetterRTP gives up and sends a NotSafeMessage # ## Maximum amount of tries before BetterRTP gives up and sends a NotSafeMessage #
MaxAttempts: 15 MaxAttempts: 15
FirstJoinRtp: false # Make the player rtp when joining the server for the first time RtpOnFirstJoin: # Will execute as console to override delays
Enabled: false # Make the player rtp when joining the server for the first time
World: 'world' # World to first rtp in
Cooldown: Cooldown:
Enabled: true # Enabled or disabled cooldown timer Enabled: true # Enabled or disabled cooldown timer
LockAfter: 0 # Lock the player in an infinite cooldown after # rtp's (0 to disable) LockAfter: 0 # Lock the player in an infinite cooldown after # rtp's (0 to disable)

View File

@ -18,6 +18,7 @@ Messages:
## %world% is the world the player is in that is disabled to rtp in! # ## %world% is the world the player is in that is disabled to rtp in! #
DisabledWorld: '&c%world%世界已被禁止使用此命令,&7无法传送' DisabledWorld: '&c%world%世界已被禁止使用此命令,&7无法传送'
Cooldown: '&c对不起&7你需要在&c%time%&7秒后才能传送' Cooldown: '&c对不起&7你需要在&c%time%&7秒后才能传送'
Locked: '&cSorry! &7You''ve used up all your RTP''s!'
Invalid: '&c无效的参数使用''/%command% help''查看帮助。' Invalid: '&c无效的参数使用''/%command% help''查看帮助。'
NotOnline: '&c玩家&7%player%&c不在线' NotOnline: '&c玩家&7%player%&c不在线'
Delay: '&a即将在&f%time%&a秒后传送乖乖站好' Delay: '&a即将在&f%time%&a秒后传送乖乖站好'

View File

@ -18,6 +18,7 @@ Messages:
## %world% 是玩家嘗試傳送到但設定已禁用的世界! # ## %world% 是玩家嘗試傳送到但設定已禁用的世界! #
DisabledWorld: '&c%world%世界已被禁止使用此指令,&7您無法傳送' DisabledWorld: '&c%world%世界已被禁止使用此指令,&7您無法傳送'
Cooldown: '&c抱歉&7您需要在&c%time%&7秒後才能嘗試傳送' Cooldown: '&c抱歉&7您需要在&c%time%&7秒後才能嘗試傳送'
Locked: '&cSorry! &7You''ve used up all your RTP''s!'
Invalid: '&c無效的參數請使用''/%command% help''查看幫助。' Invalid: '&c無效的參數請使用''/%command% help''查看幫助。'
NotOnline: '&c玩家&7%player%&c不在線' NotOnline: '&c玩家&7%player%&c不在線'
Delay: '&a即將在&f%time%&a秒後傳送請站好' Delay: '&a即將在&f%time%&a秒後傳送請站好'

View File

@ -18,6 +18,7 @@ Messages:
## %world% is the world the player is in that is disabled to rtp in! # ## %world% is the world the player is in that is disabled to rtp in! #
DisabledWorld: '&cDisabled World %world%! &7Could not RTP!' DisabledWorld: '&cDisabled World %world%! &7Could not RTP!'
Cooldown: '&cSorry! &7You can''t rtp for another &c%time% &7seconds!' Cooldown: '&cSorry! &7You can''t rtp for another &c%time% &7seconds!'
Locked: '&cSorry! &7You''ve used up all your RTP''s!'
Invalid: '&cInvalid argument. Try ''/%command% help''' Invalid: '&cInvalid argument. Try ''/%command% help'''
NotOnline: '&cThe player &7%player% &cis not online!' NotOnline: '&cThe player &7%player% &cis not online!'
Delay: '&aTeleporting in &f%time% &aseconds! Don''t move!' Delay: '&aTeleporting in &f%time% &aseconds! Don''t move!'

View File

@ -18,6 +18,7 @@ Messages:
## %world% Est le monde dans lequel le joueur est téléporter ! # ## %world% Est le monde dans lequel le joueur est téléporter ! #
DisabledWorld: '&cLe monde %world% est désactivé! &7Impossible de se téléporter!' DisabledWorld: '&cLe monde %world% est désactivé! &7Impossible de se téléporter!'
Cooldown: '&cDésoler! &7Tu ne peut pas te téléporter pour encore &c%time% &7secondes!' Cooldown: '&cDésoler! &7Tu ne peut pas te téléporter pour encore &c%time% &7secondes!'
Locked: '&cSorry! &7You''ve used up all your RTP''s!'
Invalid: '&cArgument invalide. Essaye ''/%command% help''' Invalid: '&cArgument invalide. Essaye ''/%command% help'''
NotOnline: '&cLe joueur &7%player% &cn''est pas en ligne!' NotOnline: '&cLe joueur &7%player% &cn''est pas en ligne!'
Delay: '&aTéléportation dans &f%time% &asecondes! Ne bouge pas!' Delay: '&aTéléportation dans &f%time% &asecondes! Ne bouge pas!'

View File

@ -18,6 +18,7 @@ Messages:
## ##
DisabledWorld: '&cワールド%world%は無効です! &7RTPできませんでした' DisabledWorld: '&cワールド%world%は無効です! &7RTPできませんでした'
Cooldown: '&cごめんなさい &7あなたは&c%time%&7秒間RTPできません' Cooldown: '&cごめんなさい &7あなたは&c%time%&7秒間RTPできません'
Locked: '&cSorry! &7You''ve used up all your RTP''s!'
Invalid: '&c無効な引数。 ''/%command% help''を試してください。' Invalid: '&c無効な引数。 ''/%command% help''を試してください。'
NotOnline: '&cプレイヤー&7%player%&cはオンラインではありません' NotOnline: '&cプレイヤー&7%player%&cはオンラインではありません'
Delay: '&f%time%&a秒でテレポートします 動かないで!' Delay: '&f%time%&a秒でテレポートします 動かないで!'

View File

@ -18,6 +18,7 @@ Messages:
## %world% is the world the player is in that is disabled to rtp in! # ## %world% is the world the player is in that is disabled to rtp in! #
DisabledWorld: '&cОтключенный мир %world%! &7Не удалось телепортироваться!' DisabledWorld: '&cОтключенный мир %world%! &7Не удалось телепортироваться!'
Cooldown: '&cИзвините! &7Вы не сможете использовать rtp ближайшие &c%time% &7сек.!' Cooldown: '&cИзвините! &7Вы не сможете использовать rtp ближайшие &c%time% &7сек.!'
Locked: '&cSorry! &7You''ve used up all your RTP''s!'
Invalid: '&cНеправильные параметры. Попробуйте ''/%command% help''' Invalid: '&cНеправильные параметры. Попробуйте ''/%command% help'''
NotOnline: '&cИгрок &7%player% &cне онлайн!' NotOnline: '&cИгрок &7%player% &cне онлайн!'
Delay: '&aТелепортация через &f%time% &aсек.! Не двигайтесь!' Delay: '&aТелепортация через &f%time% &aсек.! Не двигайтесь!'