mirror of
https://github.com/BentoBoxWorld/Level.git
synced 2024-12-25 10:38:20 +01:00
Refactored to make code clearer and added admin command.
This commit is contained in:
parent
e47293a883
commit
393d4f7c83
103
Level/src/bskyblock/addin/level/CalculateLevel.java
Normal file
103
Level/src/bskyblock/addin/level/CalculateLevel.java
Normal file
@ -0,0 +1,103 @@
|
||||
package bskyblock.addin.level;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import us.tastybento.bskyblock.config.Settings;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
import us.tastybento.bskyblock.util.VaultHelper;
|
||||
|
||||
public class CalculateLevel extends LevelPlugin {
|
||||
|
||||
private int levelWait;
|
||||
// Level calc cool down
|
||||
private HashMap<UUID, Long> levelWaitTime = new HashMap<UUID, Long>();
|
||||
|
||||
public CalculateLevel(Level plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the island level
|
||||
*
|
||||
* @param sender
|
||||
* - Player object of player who is asking
|
||||
* @param targetPlayer
|
||||
* - UUID of the player's island that is being requested
|
||||
* @return - true if successful.
|
||||
*/
|
||||
public boolean calculateIslandLevel(final CommandSender sender, final UUID targetPlayer) {
|
||||
return calculateIslandLevel(sender, targetPlayer, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the island level
|
||||
* @param sender - asker of the level info
|
||||
* @param targetPlayer
|
||||
* @param report - if true, a detailed report will be provided
|
||||
* @return - false if this is cannot be done
|
||||
*/
|
||||
public boolean calculateIslandLevel(final CommandSender sender, final UUID targetPlayer, boolean report) {
|
||||
if (sender instanceof Player) {
|
||||
Player asker = (Player)sender;
|
||||
// Player asking for their own island calc
|
||||
if (asker.getUniqueId().equals(targetPlayer) || asker.isOp() || VaultHelper.hasPerm(asker, Settings.PERMPREFIX + "mod.info")) {
|
||||
// Newer better system - uses chunks
|
||||
if (!onLevelWaitTime(asker) || levelWait <= 0 || asker.isOp() || VaultHelper.hasPerm(asker, Settings.PERMPREFIX + "mod.info")) {
|
||||
Util.sendMessage(asker, ChatColor.GREEN + "Calculating level, please wait...");
|
||||
setLevelWaitTime(asker);
|
||||
new LevelCalcByChunk(plugin, bSkyBlock, targetPlayer, asker, report);
|
||||
} else {
|
||||
Util.sendMessage(asker, ChatColor.YELLOW + String.valueOf(getLevelWaitTime(asker)));
|
||||
}
|
||||
|
||||
} else {
|
||||
// Asking for the level of another player
|
||||
Util.sendMessage(asker, ChatColor.GREEN + plugin.getLocale(asker.getUniqueId()).get("island.islandLevelIs").replace("[level]", String.valueOf(plugin.getIslandLevel(targetPlayer))));
|
||||
}
|
||||
} else {
|
||||
// Console request
|
||||
//Util.sendMessage(sender, ChatColor.GREEN + bSkyBlock.myLocale().levelCalculating);
|
||||
new LevelCalcByChunk(plugin, bSkyBlock, targetPlayer, sender, report);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets cool down for the level command
|
||||
*
|
||||
* @param player
|
||||
*/
|
||||
private void setLevelWaitTime(final Player player) {
|
||||
levelWaitTime.put(player.getUniqueId(), Long.valueOf(Calendar.getInstance().getTimeInMillis() + levelWait * 1000));
|
||||
}
|
||||
|
||||
private boolean onLevelWaitTime(final Player player) {
|
||||
if (levelWaitTime.containsKey(player.getUniqueId())) {
|
||||
if (levelWaitTime.get(player.getUniqueId()).longValue() > Calendar.getInstance().getTimeInMillis()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private long getLevelWaitTime(final Player player) {
|
||||
if (levelWaitTime.containsKey(player.getUniqueId())) {
|
||||
if (levelWaitTime.get(player.getUniqueId()).longValue() > Calendar.getInstance().getTimeInMillis()) {
|
||||
return (levelWaitTime.get(player.getUniqueId()).longValue() - Calendar.getInstance().getTimeInMillis()) / 1000;
|
||||
}
|
||||
|
||||
return 0L;
|
||||
}
|
||||
|
||||
return 0L;
|
||||
}
|
||||
}
|
122
Level/src/bskyblock/addin/level/Commands.java
Normal file
122
Level/src/bskyblock/addin/level/Commands.java
Normal file
@ -0,0 +1,122 @@
|
||||
package bskyblock.addin.level;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.api.commands.ArgumentHandler;
|
||||
import us.tastybento.bskyblock.api.commands.CanUseResp;
|
||||
import us.tastybento.bskyblock.config.Settings;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
import us.tastybento.bskyblock.util.VaultHelper;
|
||||
|
||||
public class Commands extends CalculateLevel {
|
||||
|
||||
public Commands(Level plugin) {
|
||||
super(plugin);
|
||||
setupCommands();
|
||||
}
|
||||
|
||||
private void setupCommands() {
|
||||
BSkyBlock bSkyBlock = BSkyBlock.getPlugin();
|
||||
bSkyBlock.addSubCommand(new ArgumentHandler("island") {
|
||||
|
||||
@Override
|
||||
public CanUseResp canUse(CommandSender sender) {
|
||||
return new CanUseResp(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String[] args) {
|
||||
plugin.getLogger().info("DEBUG: " + args);
|
||||
if (args.length > 0) {
|
||||
// Asking for another player's level?
|
||||
// Convert name to a UUID
|
||||
final UUID playerUUID = bSkyBlock.getPlayers().getUUID(args[0], true);
|
||||
plugin.getLogger().info("DEBUG: console player info UUID = " + playerUUID);
|
||||
if (playerUUID == null) {
|
||||
Util.sendMessage(sender, ChatColor.RED + plugin.getLocale(sender).get("error.UnknownPlayer"));
|
||||
return;
|
||||
} else {
|
||||
Util.sendMessage(sender, ChatColor.GREEN + "Level is " + plugin.getIslandLevel(playerUUID));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player)sender;
|
||||
UUID playerUUID = player.getUniqueId();
|
||||
|
||||
if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "island.info")) {
|
||||
if (!bSkyBlock.getPlayers().inTeam(playerUUID) && !bSkyBlock.getPlayers().hasIsland(playerUUID)) {
|
||||
Util.sendMessage(player, ChatColor.RED + bSkyBlock.getLocale(sender).get("errors.no-island"));
|
||||
return;
|
||||
} else {
|
||||
calculateIslandLevel(player, playerUUID);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
Util.sendMessage(player, ChatColor.RED + bSkyBlock.getLocale(sender).get("errors.no-permission"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> tabComplete(CommandSender sender, String[] args) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] usage(CommandSender sender) {
|
||||
return new String[]{"[player]", "See your island's level or someone else's"};
|
||||
}
|
||||
}.alias("level"));
|
||||
|
||||
// Admin command
|
||||
bSkyBlock.addSubCommand(new ArgumentHandler("bsadmin") {
|
||||
|
||||
@Override
|
||||
public CanUseResp canUse(CommandSender sender) {
|
||||
return new CanUseResp(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String[] args) {
|
||||
if (args.length == 0) {
|
||||
|
||||
} else {
|
||||
// Convert name to a UUID
|
||||
final UUID playerUUID = bSkyBlock.getPlayers().getUUID(args[0], true);
|
||||
plugin.getLogger().info("DEBUG: console player info UUID = " + playerUUID);
|
||||
if (playerUUID == null) {
|
||||
Util.sendMessage(sender, ChatColor.RED + plugin.getLocale(sender).get("error.UnknownPlayer"));
|
||||
return;
|
||||
} else {
|
||||
if (sender instanceof Player) {
|
||||
calculateIslandLevel(sender, playerUUID, false);
|
||||
} else {
|
||||
calculateIslandLevel(sender, playerUUID, true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> tabComplete(CommandSender sender, String[] args) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] usage(CommandSender sender) {
|
||||
return new String[]{"[player]", "Calculate a player's island's level"};
|
||||
}
|
||||
}.alias("level"));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -3,14 +3,9 @@ package bskyblock.addin.level;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
@ -18,108 +13,65 @@ import bskyblock.addin.level.config.LocaleManager;
|
||||
import bskyblock.addin.level.config.PluginConfig;
|
||||
import bskyblock.addin.level.database.object.Levels;
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.api.commands.ArgumentHandler;
|
||||
import us.tastybento.bskyblock.api.commands.CanUseResp;
|
||||
import us.tastybento.bskyblock.config.BSBLocale;
|
||||
import us.tastybento.bskyblock.config.Settings;
|
||||
import us.tastybento.bskyblock.database.BSBDatabase;
|
||||
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
import us.tastybento.bskyblock.util.VaultHelper;
|
||||
|
||||
public class Level extends JavaPlugin {
|
||||
|
||||
private BSkyBlock bSkyBlock;
|
||||
|
||||
// Level calc cool down
|
||||
private HashMap<UUID, Long> levelWaitTime = new HashMap<UUID, Long>();
|
||||
|
||||
// Level calc checker
|
||||
BukkitTask checker = null;
|
||||
|
||||
private int levelWait;
|
||||
|
||||
private LocaleManager localeManager;
|
||||
|
||||
private HashMap<UUID, Long> islandLevel;
|
||||
|
||||
private AbstractDatabaseHandler<Levels> handler;
|
||||
|
||||
private BSBDatabase database;
|
||||
|
||||
private Levels levelsDatabase;
|
||||
|
||||
private Levels levelsCache;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Load the plugin's config
|
||||
new PluginConfig(this);
|
||||
// Get the BSkyBlock plugin
|
||||
bSkyBlock = BSkyBlock.getPlugin();
|
||||
islandLevel = new HashMap<>();
|
||||
if (!bSkyBlock.isEnabled()) {
|
||||
this.setEnabled(false);
|
||||
return;
|
||||
}
|
||||
// Set up database
|
||||
database = BSBDatabase.getDatabase();
|
||||
// Set up the database handler to store and retrieve Island classes
|
||||
// Note that these are saved by the BSkyBlock database
|
||||
handler = (AbstractDatabaseHandler<Levels>) database.getHandler(bSkyBlock, Levels.class);
|
||||
levelsDatabase = new Levels();
|
||||
// Load the levels to a cache
|
||||
levelsCache = new Levels();
|
||||
try {
|
||||
levelsDatabase = handler.loadObject("addon-levels");
|
||||
if (levelsDatabase == null) {
|
||||
levelsDatabase = new Levels();
|
||||
levelsCache = handler.loadObject("addon-levels");
|
||||
if (levelsCache == null) {
|
||||
levelsCache = new Levels();
|
||||
}
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException | SecurityException | ClassNotFoundException | IntrospectionException
|
||||
| SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
// Start the top ten
|
||||
new TopTen(this);
|
||||
// Local locales
|
||||
localeManager = new LocaleManager(this);
|
||||
bSkyBlock.getIslandCommand().addSubCommand(new ArgumentHandler("island") {
|
||||
|
||||
@Override
|
||||
public CanUseResp canUse(CommandSender sender) {
|
||||
return new CanUseResp(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player)sender;
|
||||
UUID playerUUID = player.getUniqueId();
|
||||
|
||||
if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "island.info")) {
|
||||
if (!bSkyBlock.getPlayers().inTeam(playerUUID) && !bSkyBlock.getPlayers().hasIsland(playerUUID)) {
|
||||
Util.sendMessage(player, ChatColor.RED + bSkyBlock.getLocale(sender).get("error.noisland"));
|
||||
return;
|
||||
} else {
|
||||
calculateIslandLevel(player, playerUUID);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
//Util.sendMessage(player, ChatColor.RED + bSkyBlock.myLocale(playerUUID).errorNoPermission);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> tabComplete(CommandSender sender, String[] args) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] usage(CommandSender sender) {
|
||||
return new String[]{null, "Calculate your island's level"};
|
||||
}
|
||||
}.alias("level"));
|
||||
|
||||
|
||||
// Register commands
|
||||
new Commands(this);
|
||||
// Done
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable(){
|
||||
if (levelsDatabase != null) {
|
||||
// Save the cache
|
||||
if (levelsCache != null) {
|
||||
save(false);
|
||||
}
|
||||
}
|
||||
@ -131,7 +83,7 @@ public class Level extends JavaPlugin {
|
||||
public void save(boolean async){
|
||||
Runnable save = () -> {
|
||||
try {
|
||||
handler.saveObject(levelsDatabase);
|
||||
handler.saveObject(levelsCache);
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
|
||||
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
@ -146,96 +98,28 @@ public class Level extends JavaPlugin {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the island level
|
||||
*
|
||||
* @param sender
|
||||
* - Player object of player who is asking
|
||||
* Get level from cache for a player
|
||||
* @param targetPlayer
|
||||
* - UUID of the player's island that is being requested
|
||||
* @return - true if successful.
|
||||
* @return Level of player
|
||||
*/
|
||||
public boolean calculateIslandLevel(final CommandSender sender, final UUID targetPlayer) {
|
||||
return calculateIslandLevel(sender, targetPlayer, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the island level
|
||||
* @param sender - asker of the level info
|
||||
* @param targetPlayer
|
||||
* @param report - if true, a detailed report will be provided
|
||||
* @return - false if this is cannot be done
|
||||
*/
|
||||
public boolean calculateIslandLevel(final CommandSender sender, final UUID targetPlayer, boolean report) {
|
||||
if (sender instanceof Player) {
|
||||
Player asker = (Player)sender;
|
||||
// Player asking for their own island calc
|
||||
if (asker.getUniqueId().equals(targetPlayer) || asker.isOp() || VaultHelper.hasPerm(asker, Settings.PERMPREFIX + "mod.info")) {
|
||||
// Newer better system - uses chunks
|
||||
if (!onLevelWaitTime(asker) || levelWait <= 0 || asker.isOp() || VaultHelper.hasPerm(asker, Settings.PERMPREFIX + "mod.info")) {
|
||||
Util.sendMessage(asker, ChatColor.GREEN + "Calculating level, please wait...");
|
||||
setLevelWaitTime(asker);
|
||||
new LevelCalcByChunk(this, bSkyBlock, targetPlayer, asker, report);
|
||||
} else {
|
||||
Util.sendMessage(asker, ChatColor.YELLOW + String.valueOf(getLevelWaitTime(asker)));
|
||||
}
|
||||
|
||||
} else {
|
||||
// Asking for the level of another player
|
||||
//Util.sendMessage(asker, ChatColor.GREEN + bSkyBlock.myLocale(asker.getUniqueId()).islandislandLevelis.replace("[level]", String.valueOf(bSkyBlock.getIslands().getIslandLevel(targetPlayer))));
|
||||
}
|
||||
} else {
|
||||
// Console request
|
||||
//Util.sendMessage(sender, ChatColor.GREEN + bSkyBlock.myLocale().levelCalculating);
|
||||
new LevelCalcByChunk(this, bSkyBlock, targetPlayer, sender, report);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets cool down for the level command
|
||||
*
|
||||
* @param player
|
||||
*/
|
||||
private void setLevelWaitTime(final Player player) {
|
||||
levelWaitTime.put(player.getUniqueId(), Long.valueOf(Calendar.getInstance().getTimeInMillis() + levelWait * 1000));
|
||||
}
|
||||
|
||||
public boolean onLevelWaitTime(final Player player) {
|
||||
if (levelWaitTime.containsKey(player.getUniqueId())) {
|
||||
if (levelWaitTime.get(player.getUniqueId()).longValue() > Calendar.getInstance().getTimeInMillis()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private long getLevelWaitTime(final Player player) {
|
||||
if (levelWaitTime.containsKey(player.getUniqueId())) {
|
||||
if (levelWaitTime.get(player.getUniqueId()).longValue() > Calendar.getInstance().getTimeInMillis()) {
|
||||
return (levelWaitTime.get(player.getUniqueId()).longValue() - Calendar.getInstance().getTimeInMillis()) / 1000;
|
||||
}
|
||||
|
||||
return 0L;
|
||||
}
|
||||
|
||||
return 0L;
|
||||
}
|
||||
|
||||
public Long getIslandLevel(UUID targetPlayer) {
|
||||
//getLogger().info("DEBUG: getting island level for " + bSkyBlock.getPlayers().getName(targetPlayer));
|
||||
return levelsDatabase.getLevel(targetPlayer);
|
||||
return levelsCache.getLevel(targetPlayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the player's level in the local cache
|
||||
* @param targetPlayer
|
||||
* @param level
|
||||
*/
|
||||
public void setIslandLevel(UUID targetPlayer, long level) {
|
||||
//getLogger().info("DEBUG: set island level to " + level + " for " + bSkyBlock.getPlayers().getName(targetPlayer));
|
||||
levelsDatabase.addLevel(targetPlayer, level);
|
||||
levelsCache.addLevel(targetPlayer, level);
|
||||
save(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the locale for this player
|
||||
* @param sender
|
||||
* @return Locale object for sender
|
||||
*/
|
||||
@ -244,6 +128,7 @@ public class Level extends JavaPlugin {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the locale for this UUID
|
||||
* @param uuid
|
||||
* @return Locale object for UUID
|
||||
*/
|
||||
|
20
Level/src/bskyblock/addin/level/LevelPlugin.java
Normal file
20
Level/src/bskyblock/addin/level/LevelPlugin.java
Normal file
@ -0,0 +1,20 @@
|
||||
package bskyblock.addin.level;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
|
||||
/**
|
||||
* Makes code look nicer
|
||||
* @author ben
|
||||
*
|
||||
*/
|
||||
public class LevelPlugin {
|
||||
protected Level plugin;
|
||||
protected BSkyBlock bSkyBlock;
|
||||
|
||||
public LevelPlugin(Level plugin) {
|
||||
super();
|
||||
this.plugin = plugin;
|
||||
this.bSkyBlock = BSkyBlock.getPlugin();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user