Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiplier and UpdateRealTimePlayedTask #727

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions Settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ interval check: 5
# This is the interval between checks of Autorank (in minutes).
# Lowering this number will increase accuracy but will also increase server lag.

use more accurate timings: false
#if it is "true", the plugin will compute the time between every update to add the time elapsed

enable multiplier command: false
#this will enable the multiplier command. It is useful if there is an event or a special day/hour.
#Usage in game: if you want players to get x2 time, use "/ar multiplier 2".
#If the server is restarted the multiplier is lost.

leaderboard layout: '&6&r | &b&p - &7&d %day%, &h %hour% and &m %minute%.'
leaderboard length: 5
# changes the appearance of the /ar leaderboard command
Expand Down
69 changes: 69 additions & 0 deletions src/me/armar/plugins/autorank/commands/MultiplierCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.armar.plugins.autorank.commands;

import me.armar.plugins.autorank.Autorank;
import me.armar.plugins.autorank.commands.manager.AutorankCommand;
import me.armar.plugins.autorank.language.Lang;
import me.armar.plugins.autorank.permissions.AutorankPermission;
import me.armar.plugins.autorank.util.AutorankTools;
import me.armar.plugins.autorank.playtimes.PlayTimeManager;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

/**
* The command delegator for the '/ar set' command.
*/
public class MultiplierCommand extends AutorankCommand {

private final Autorank plugin;

public MultiplierCommand(final Autorank instance) {
plugin = instance;
}

@Override
public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {


if (args.length < 2) {
sender.sendMessage(Lang.INVALID_FORMAT.getConfigValue(this.getUsage()));
return true;
}

int value = AutorankTools.readTimeInput(args, 1);

if (value >= 1) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiplier value can't be less than 1


if (!this.hasPermission(AutorankPermission.SET_LOCAL_TIME, sender)) {
return true;
}

if(plugin.getSettingsConfig().multiplierCommandIsEnabled()){
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks if the command is enable

PlayTimeManager.MULTIPLIER=value;
sender.sendMessage(ChatColor.AQUA + "MULTIPLIER = " + PlayTimeManager.MULTIPLIER);
}else{
sender.sendMessage(ChatColor.AQUA + "Multipler command is disabled in settings config.");
}

} else {
AutorankTools.sendColoredMessage(sender, Lang.INVALID_FORMAT.getConfigValue(this.getUsage()));
}

return true;
}

@Override
public String getDescription() {
return "Set multiplier's time to [value].";
}

@Override
public String getPermission() {
return AutorankPermission.SET_LOCAL_TIME;
}

@Override
public String getUsage() {
return "/ar multiplier [value]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public CommandsManager(final Autorank plugin) {
registeredCommands.put(Arrays.asList("info"), new InfoCommand(plugin));
registeredCommands.put(Arrays.asList("editor"), new EditorCommand(plugin));
registeredCommands.put(Arrays.asList("migrate"), new MigrateCommand(plugin));
registeredCommands.put(Arrays.asList("multiplier"), new MultiplierCommand(plugin));
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/me/armar/plugins/autorank/config/SettingsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public int getIntervalTime() {
return this.getConfig().getInt("interval check", 5);
}

public boolean useMoreAccurateTimings() {
return this.getConfig().getBoolean("use more accurate timings", false);
}

public boolean multiplierCommandIsEnabled() {
return this.getConfig().getBoolean("enable multiplier command", false);
}

/**
* Get the layout of the leaderboards.
*
Expand Down
2 changes: 2 additions & 0 deletions src/me/armar/plugins/autorank/playtimes/PlayTimeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class PlayTimeManager {

// How often do we check whether a player is still online? (in minutes)
public static int INTERVAL_MINUTES = 5;
//Mutiplier, can be changed in game by command if "enable multiplier command" is true
public static int MULTIPLIER = 1;

private final Autorank plugin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public UpdateTimePlayedTask(Autorank instance, UUID uuid) {
public void run() {

Player player = plugin.getServer().getPlayer(uuid);
long lastPlayTimeUpdate=plugin.getTaskManager().getLastPlayTimeUpdate(uuid);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the value is overwritten, it is saved in this variable

int timeToAdd;

// Cancel task as player is not online anymore.
if (player == null || !player.isOnline()) {
Expand Down Expand Up @@ -55,9 +57,18 @@ public void run() {
return;
}


if(plugin.getSettingsConfig().useMoreAccurateTimings()){
//This compute the time elapsed between the last update and the current update.
timeToAdd=(int)Math.round((System.currentTimeMillis()-lastPlayTimeUpdate)/60000f*PlayTimeManager.MULTIPLIER);
}else{
//Multiply by 1 doesn't affect the performance
timeToAdd=PlayTimeManager.INTERVAL_MINUTES*PlayTimeManager.MULTIPLIER;
}

// Add time to a player's current time for all storage providers.
for (final TimeType type : TimeType.values()) {
plugin.getPlayTimeStorageManager().addPlayerTime(type, uuid, PlayTimeManager.INTERVAL_MINUTES);
plugin.getPlayTimeStorageManager().addPlayerTime(type, uuid, timeToAdd);
}

// Auto assign path (if possible)
Expand Down
1 change: 1 addition & 0 deletions src/me/armar/plugins/autorank/tasks/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void startUpdatePlayTimeTask(UUID uuid) {
new UpdateTimePlayedTask(plugin, uuid)
, PlayTimeManager.INTERVAL_MINUTES * AutorankTools.TICKS_PER_MINUTE, PlayTimeManager
.INTERVAL_MINUTES * AutorankTools.TICKS_PER_MINUTE);


// Store taskID so we can refer to it later.
updatePlayTimeTaskIds.put(uuid, task.getTaskId());
Expand Down