Skip to content

Commit

Permalink
move old zaps/cooldowns to flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 5, 2020
1 parent 6318899 commit 159e6c5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 175 deletions.
40 changes: 13 additions & 27 deletions plugin/src/main/java/com/denizenscript/denizen/Denizen.java
Expand Up @@ -71,6 +71,12 @@

public class Denizen extends JavaPlugin {

private static Denizen instance;

public static Denizen getInstance() {
return instance;
}

public static String versionTag = null;
private boolean startedSuccessful = false;

Expand Down Expand Up @@ -123,6 +129,7 @@ public NotableManager notableManager() {
*/
@Override
public void onEnable() {
instance = this;
try {
versionTag = this.getDescription().getVersion();

Expand Down Expand Up @@ -417,6 +424,12 @@ public void run() {
}
}
}.runTaskTimer(this, 100, 20 * 60 * 60);
new BukkitRunnable() {
@Override
public void run() {
PlayerTag.cleanCache();
}
}.runTaskTimer(this, 100, 20 * 60 * 2);
new BukkitRunnable() {
@Override
public void run() {
Expand Down Expand Up @@ -490,21 +503,10 @@ public void reloadConfig() {
SlowWarning.WARNING_RATE = Settings.warningRate();
}

/*
* Reloads, retrieves and saves progress information in
* Denizen/saves.yml and Denizen/scoreboards.yml
*/
private FileConfiguration savesConfig = null;
private File savesConfigFile = null;
private FileConfiguration scoreboardsConfig = null;
private File scoreboardsConfigFile = null;

public void reloadSaves() {
if (savesConfigFile == null) {
savesConfigFile = new File(getDataFolder(), "saves.yml");
}
savesConfig = YamlConfiguration.loadConfiguration(savesConfigFile);

if (scoreboardsConfigFile == null) {
scoreboardsConfigFile = new File(getDataFolder(), "scoreboards.yml");
}
Expand All @@ -529,13 +531,6 @@ public void reloadSaves() {
Bukkit.getServer().getPluginManager().callEvent(new SavesReloadEvent());
}

public FileConfiguration getSaves() {
if (savesConfig == null) {
reloadSaves();
}
return savesConfig;
}

public FileConfiguration getScoreboards() {
if (scoreboardsConfig == null) {
reloadSaves();
Expand All @@ -544,9 +539,6 @@ public FileConfiguration getScoreboards() {
}

public void saveSaves() {
if (savesConfig == null || savesConfigFile == null) {
return;
}
// Save notables
notableManager.saveNotables();
// Save scoreboards to scoreboards.yml
Expand All @@ -572,12 +564,6 @@ public void saveSaves() {
catch (Throwable ex) {
Debug.echoError(ex);
}
try {
savesConfig.save(savesConfigFile);
}
catch (IOException ex) {
Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Could not save to " + savesConfigFile, ex);
}
try {
scoreboardsConfig.save(scoreboardsConfigFile);
}
Expand Down
@@ -1,6 +1,6 @@
package com.denizenscript.denizen.scripts.commands.core;

import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.PlayerTag;
Expand All @@ -9,6 +9,7 @@
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.objects.core.TimeTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;

Expand Down Expand Up @@ -142,42 +143,18 @@ public void execute(ScriptEntry scriptEntry) {
* @return a DurationTag of the time remaining
*/
public static DurationTag getCooldownDuration(PlayerTag player, String scriptName) {

// Change to UPPERCASE so there's no case-sensitivity.
scriptName = scriptName.toUpperCase();

DurationTag duration = new DurationTag(0);

// Check current entry GLOBALLY, reset it if necessary
if (DenizenAPI.getSaves().contains("Global.Scripts." + scriptName + ".Cooldown Time")) {
if (System.currentTimeMillis()
< DenizenAPI.getSaves().getLong("Global.Scripts." + scriptName + ".Cooldown Time")) {
duration = new DurationTag((double) (DenizenAPI.getSaves().getLong("Global.Scripts." + scriptName
+ ".Cooldown Time") - System.currentTimeMillis()) / 1000);
}
TimeTag expires = Denizen.getInstance().serverFlagMap.getFlagExpirationTime("__interact_cooldown." + scriptName);
if (expires != null) {
return new DurationTag((TimeTag.now().millis() - expires.millis()) / 1000.0);
}

// No player specified? No need to check any further...
if (player == null) {
return duration;
}

// If no entry for the script, return true
if (!DenizenAPI.getSaves().contains("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time")) {
return duration;
return new DurationTag(0);
}

// If there is an entry, check against the time
if (System.currentTimeMillis()
<= DenizenAPI.getSaves().getLong("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time")) {
DurationTag player_dur = new DurationTag((double) (DenizenAPI.getSaves().getLong("Players." + player.getSaveName() + ".Scripts."
+ scriptName + ".Cooldown Time") - System.currentTimeMillis()) / 1000);
if (player_dur.getSeconds() > duration.getSeconds()) {
return player_dur;
}
expires = player.getFlagTracker().getFlagExpirationTime("__interact_cooldown." + scriptName);
if (expires != null) {
return new DurationTag((TimeTag.now().millis() - expires.millis()) / 1000.0);
}

return duration;
return new DurationTag(0);
}

/**
Expand All @@ -190,39 +167,11 @@ public static DurationTag getCooldownDuration(PlayerTag player, String scriptNam
* @return true if the script is cool
*/
public static boolean checkCooldown(PlayerTag player, String scriptName) {

// Change to UPPERCASE so there's no case-sensitivity.
scriptName = scriptName.toUpperCase();

// Check current entry GLOBALLY, reset it if necessary
if (DenizenAPI.getSaves().contains("Global.Scripts." + scriptName + ".Cooldown Time")) {
if (System.currentTimeMillis()
< DenizenAPI.getSaves().getLong("Global.Scripts." + scriptName + ".Cooldown Time")) {
return false;
}
else {
DenizenAPI.getSaves().set("Global.Scripts." + scriptName + ".Cooldown Time", null);
}
}

// No player specified? No need to check any further...
if (player == null) {
return true;
DurationTag cooldown = getCooldownDuration(player, scriptName);
if (cooldown.getSeconds() <= 0) {
return false;
}

// If no entry for the script, return true
if (!DenizenAPI.getSaves().contains("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time")) {
return true;
}

// If there is an entry, check against the time
if (System.currentTimeMillis()
>= DenizenAPI.getSaves().getLong("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time")) {
DenizenAPI.getSaves().set("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time", null);
return true;
}

return false;
return true;
}

/**
Expand All @@ -234,19 +183,12 @@ public static boolean checkCooldown(PlayerTag player, String scriptName) {
* @param global whether the script should be cooled down globally
*/
public static void setCooldown(PlayerTag player, DurationTag duration, String scriptName, boolean global) {
scriptName = scriptName.toUpperCase();
// Set global cooldown
TimeTag cooldownTime = new TimeTag(TimeTag.now().millis() + duration.getMillis());
if (global) {
DenizenAPI.getSaves().set("Global.Scripts." + scriptName + ".Cooldown Time",
System.currentTimeMillis()
+ (duration.getSecondsAsInt() * 1000));

// or set Player cooldown
Denizen.getInstance().serverFlagMap.setFlag("__interact_cooldown." + scriptName, cooldownTime, cooldownTime);
}
else {
DenizenAPI.getSaves().set("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time",
System.currentTimeMillis()
+ (duration.getSecondsAsInt() * 1000));
player.getFlagTracker().setFlag("__interact_cooldown." + scriptName, cooldownTime, cooldownTime);
}
}
}
@@ -1,6 +1,5 @@
package com.denizenscript.denizen.scripts.commands.core;

import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.*;
Expand All @@ -23,10 +22,10 @@ public ResetCommand() {

// <--[command]
// @Name Reset
// @Syntax reset (<player>|...) [cooldown/saves/global_cooldown] (<script>)
// @Syntax reset (<player>|...) [cooldown/global_cooldown] (<script>)
// @Required 1
// @Maximum 3
// @Short Resets various parts of Denizen's save data, including a script's cooldowns or general player saves.
// @Short Resets various parts of Denizen's interact save data, including a script's cooldowns.
// @Group core
//
// @Description
Expand All @@ -37,28 +36,20 @@ public ResetCommand() {
//
// The "global_cooldown" argument removes all cooldowns for the specified script (not player-specific).
//
// The "saves" argument removes all permanent save data related to the player (most notably: all flags).
//
// @Tags
// None
//
// @Usage
// Use to forget all data about the linked player.
// - reset saves
//
// @Usage
// Use to reset all cooldowns for a script when an event that limits usage completes.
// - reset global_cooldown MyScriptName
//
// -->

private enum Type {PLAYER_COOLDOWN, GLOBAL_COOLDOWN, SAVES}
private enum Type {PLAYER_COOLDOWN, GLOBAL_COOLDOWN}

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (Argument arg : scriptEntry.getProcessedArgs()) {

if (arg.matches("cooldown")
&& !scriptEntry.hasObject("type")) {
scriptEntry.addObject("type", Type.PLAYER_COOLDOWN);
Expand All @@ -67,9 +58,6 @@ else if (arg.matches("global_cooldown")
&& !scriptEntry.hasObject("type")) {
scriptEntry.addObject("type", Type.GLOBAL_COOLDOWN);
}
else if (arg.matches("saves") && !scriptEntry.hasObject("type")) {
scriptEntry.addObject("type", Type.SAVES);
}
else if (arg.matchesArgumentType(ScriptTag.class)) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
Expand All @@ -82,21 +70,16 @@ else if (arg.matchesArgumentList(PlayerTag.class)) {
arg.reportUnhandled();
}
}

// Use attached player if none is specified, and we're not resetting GLOBAL_COOLDOWN
if (!scriptEntry.getObject("type").equals(Type.GLOBAL_COOLDOWN)) {
scriptEntry.defaultObject("players", Utilities.getEntryPlayer(scriptEntry));
}

// Must specify a script unless resetting SAVES
if (!scriptEntry.hasObject("script") && !scriptEntry.getObject("type").equals(Type.SAVES)) {
if (!scriptEntry.hasObject("script")) {
throw new InvalidArgumentsException("Must specify a script!");
}
}

@Override
public void execute(ScriptEntry scriptEntry) {

// We allow players to be a single player or multiple players
ObjectTag player = scriptEntry.getObjectTag("players");
ListTag players;
Expand All @@ -106,41 +89,29 @@ public void execute(ScriptEntry scriptEntry) {
else {
players = scriptEntry.getObjectTag("players");
}

Type type = (Type) scriptEntry.getObject("type");
ScriptTag script = scriptEntry.getObjectTag("script");

if (scriptEntry.dbCallShouldDebug()) {

Debug.report(scriptEntry, getName(),
(players != null ? players.debug() : "")
+ ArgumentHelper.debugObj("type", type)
+ (script != null ? script.debug() : ""));

}

// Deal with GLOBAL_COOLDOWN reset first, since there's no player/players involved
if (type == Type.GLOBAL_COOLDOWN) {
CooldownCommand.setCooldown(null, new DurationTag(0), script.getName(), true);
return;
}

// Now deal with the rest
for (String object : players) {

PlayerTag resettable = PlayerTag.valueOf(object, scriptEntry.context);
if (resettable.isValid()) {

switch (type) {
case PLAYER_COOLDOWN:
CooldownCommand.setCooldown(resettable, new DurationTag(0), script.getName(), false);
return;

case SAVES:
DenizenAPI.getCurrentInstance().getSaves().set("Players." + resettable.getSaveName(), null);
}
}

}
}
}

0 comments on commit 159e6c5

Please sign in to comment.