diff --git a/pom.xml b/pom.xml index da79813..2793be1 100644 --- a/pom.xml +++ b/pom.xml @@ -6,15 +6,16 @@ com.mstiles92.plugins HardcoreDeathBan - 1.2.1-SNAPSHOT + 1.3.0-SNAPSHOT UTF-8 - 1.7.10-R0.1-SNAPSHOT + 1.7.9-R0.2 clean package + HardcoreDeathBan @@ -91,13 +92,6 @@ ${bukkitVersion} - - org.bukkit - craftbukkit - ${bukkitVersion} - runtime - - org.mcstats.bukkit metrics @@ -137,4 +131,4 @@ 1.0.6-SNAPSHOT - \ No newline at end of file + diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/HardcoreDeathBan.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/HardcoreDeathBan.java index 7bcdaed..f4359c9 100644 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/HardcoreDeathBan.java +++ b/src/main/java/com/mstiles92/plugins/hardcoredeathban/HardcoreDeathBan.java @@ -23,23 +23,23 @@ package com.mstiles92.plugins.hardcoredeathban; -import com.mstiles92.plugins.hardcoredeathban.config.Config; -import com.mstiles92.plugins.hardcoredeathban.util.Log; -import com.mstiles92.plugins.stileslib.calendar.CalendarUtils; -import com.mstiles92.plugins.stileslib.commands.CommandRegistry; -import com.mstiles92.plugins.stileslib.updates.UpdateChecker; -import com.mstiles92.plugins.hardcoredeathban.commands.Credits; -import com.mstiles92.plugins.hardcoredeathban.commands.Deathban; -import com.mstiles92.plugins.hardcoredeathban.listeners.PlayerListener; -import com.mstiles92.plugins.hardcoredeathban.util.Bans; -import com.mstiles92.plugins.hardcoredeathban.util.RevivalCredits; +import java.io.File; +import java.io.IOException; + import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.plugin.java.JavaPlugin; import org.mcstats.Metrics; -import java.io.IOException; +import com.mstiles92.plugins.hardcoredeathban.commands.Credits; +import com.mstiles92.plugins.hardcoredeathban.commands.Deathban; +import com.mstiles92.plugins.hardcoredeathban.config.Config; +import com.mstiles92.plugins.hardcoredeathban.data.PlayerData; +import com.mstiles92.plugins.hardcoredeathban.listeners.PlayerListener; +import com.mstiles92.plugins.hardcoredeathban.util.Log; +import com.mstiles92.plugins.stileslib.commands.CommandRegistry; +import com.mstiles92.plugins.stileslib.updates.UpdateChecker; /** * HardcoreDeathBan is the main class of this Bukkit plugin. @@ -49,26 +49,24 @@ * @author mstiles92 */ public class HardcoreDeathBan extends JavaPlugin { + + public final File PLAYERDATA_JSON_FILE = new File(this.getDataFolder(), "PlayerData.json"); + private static HardcoreDeathBan instance; private static Config config; private UpdateChecker updateChecker; private CommandRegistry commandRegistry; - public RevivalCredits credits = null; - public Bans bans = null; - @Override public void onEnable() { instance = this; config = new Config(); - try { - credits = new RevivalCredits(this, "credits.yml"); - bans = new Bans(this, "bans.yml"); - } catch (Exception e) { - Log.warning(ChatColor.RED + "Error opening a config file. Plugin will now be disabled."); - getPluginLoader().disablePlugin(this); - } + PlayerData.init(PLAYERDATA_JSON_FILE); + // Start autosave task (if enabled) + if (this.getConfigObject().playerDataAutosaveEnabled()) { + PlayerData.startAutosaveTask(); + } commandRegistry = new CommandRegistry(this); commandRegistry.registerCommands(new Deathban()); @@ -91,8 +89,7 @@ public void onEnable() { @Override public void onDisable() { - credits.save(); - bans.save(); + PlayerData.save(); config.save(); } diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/commands/Credits.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/commands/Credits.java index 4030f54..1c7150b 100644 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/commands/Credits.java +++ b/src/main/java/com/mstiles92/plugins/hardcoredeathban/commands/Credits.java @@ -23,11 +23,14 @@ package com.mstiles92.plugins.hardcoredeathban.commands; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; + import com.mstiles92.plugins.hardcoredeathban.data.PlayerData; import com.mstiles92.plugins.stileslib.commands.Arguments; import com.mstiles92.plugins.stileslib.commands.CommandHandler; import com.mstiles92.plugins.stileslib.commands.annotations.Command; -import org.bukkit.ChatColor; /** * Credits is the CommandExecutor that handles all commands dealing @@ -104,7 +107,15 @@ public void give(Arguments args) { return; } - PlayerData otherPlayerData = PlayerData.get(args.getArgs()[0]); + PlayerData otherPlayerData = PlayerData.get(args.getArgs()[0]); + + if (otherPlayerData == null) { + OfflinePlayer otherOfflinePlayer = Bukkit.getOfflinePlayer(args.getArgs()[0]); + + if (otherOfflinePlayer.getPlayer() != null) { + otherPlayerData = PlayerData.get(otherOfflinePlayer.getPlayer()); + } + } if (otherPlayerData == null) { args.getSender().sendMessage(tag + ChatColor.RED + "The specified player could not be found."); diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/commands/Deathban.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/commands/Deathban.java index 749dda9..34f0189 100644 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/commands/Deathban.java +++ b/src/main/java/com/mstiles92/plugins/hardcoredeathban/commands/Deathban.java @@ -23,14 +23,16 @@ package com.mstiles92.plugins.hardcoredeathban.commands; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; + +import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; +import com.mstiles92.plugins.hardcoredeathban.data.PlayerData; import com.mstiles92.plugins.hardcoredeathban.util.Utils; import com.mstiles92.plugins.stileslib.commands.Arguments; import com.mstiles92.plugins.stileslib.commands.CommandHandler; import com.mstiles92.plugins.stileslib.commands.annotations.Command; -import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.entity.Player; /** * Deathban is the CommandExecutor that handles all commands dealing @@ -110,18 +112,18 @@ public void unban(Arguments args) { return; } - Player player = Bukkit.getPlayer(args.getArgs()[0]); + PlayerData playerData = PlayerData.get(args.getArgs()[0]); - if (player == null) { + if (playerData == null) { args.getSender().sendMessage(tag + ChatColor.RED + "The specified player could not be found."); return; } - if (Utils.checkPlayerBanned(player.getUniqueId())) { - Utils.unbanPlayer(player); - args.getSender().sendMessage(tag + player.getName() + " has been unbanned."); + if (Utils.checkPlayerBanned(playerData.getPlayerUUID())) { + Utils.unbanPlayer(playerData.getPlayerUUID()); + args.getSender().sendMessage(tag + playerData.getLastSeenName() + " has been unbanned."); } else { - args.getSender().sendMessage(tag + player.getName() + " is not currently banned."); + args.getSender().sendMessage(tag + playerData.getLastSeenName() + " is not currently banned."); } } @@ -132,17 +134,17 @@ public void status(Arguments args) { return; } - Player player = Bukkit.getPlayer(args.getArgs()[0]); + PlayerData playerData = PlayerData.get(args.getArgs()[0]); - if (player == null) { + if (playerData == null) { args.getSender().sendMessage(tag + ChatColor.RED + "The specified player could not be found."); return; } - if (Utils.checkPlayerBanned(player.getUniqueId())) { - args.getSender().sendMessage(tag + Utils.replaceMessageVariables("%player% is banned until %unbantime% %unbandate%", player.getUniqueId())); + if (Utils.checkPlayerBanned(playerData.getPlayerUUID())) { + args.getSender().sendMessage(tag + Utils.replaceMessageVariables("%player% is banned until %unbantime% %unbandate%", playerData.getPlayerUUID())); } else { - args.getSender().sendMessage(tag + player.getName() + " is not currently banned."); + args.getSender().sendMessage(tag + playerData.getLastSeenName() + " is not currently banned."); } } } diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/config/Config.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/config/Config.java index 424c1c4..e4063c9 100644 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/config/Config.java +++ b/src/main/java/com/mstiles92/plugins/hardcoredeathban/config/Config.java @@ -23,13 +23,14 @@ package com.mstiles92.plugins.hardcoredeathban.config; -import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; -import com.mstiles92.plugins.hardcoredeathban.data.DeathClass; +import java.util.ArrayList; +import java.util.List; + import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; -import java.util.ArrayList; -import java.util.List; +import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; +import com.mstiles92.plugins.hardcoredeathban.data.DeathClass; public class Config { private FileConfiguration config; @@ -41,6 +42,8 @@ public class Config { private int startingCredits; private boolean verboseLoggingEnabled; private boolean updateCheckingEnabled; + private boolean playerDataAutosaveEnabled; + private int playerDataAutosaveSeconds; private List deathClasses = new ArrayList<>(); public Config() { @@ -59,6 +62,8 @@ public void load() { startingCredits = config.getInt("Starting-Credits", 0); verboseLoggingEnabled = config.getBoolean("Verbose", false); updateCheckingEnabled = config.getBoolean("Check-for-Updates", true); + playerDataAutosaveEnabled = config.getBoolean("PlayerData-Autosave-Enabled", true); + playerDataAutosaveSeconds = config.getInt("PlayerData-Autosave-Seconds", 300); ConfigurationSection section = config.getConfigurationSection("Death-Classes"); if (section != null) { @@ -111,6 +116,14 @@ public boolean shouldCheckForUpdates() { return updateCheckingEnabled; } + public boolean playerDataAutosaveEnabled() { + return playerDataAutosaveEnabled; + } + + public int playerDataAutosaveSeconds() { + return playerDataAutosaveSeconds; + } + public List getDeathClasses() { return deathClasses; } diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/data/PlayerData.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/data/PlayerData.java index d51292d..77e12f6 100644 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/data/PlayerData.java +++ b/src/main/java/com/mstiles92/plugins/hardcoredeathban/data/PlayerData.java @@ -23,22 +23,36 @@ package com.mstiles92.plugins.hardcoredeathban.data; -import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; -import org.bukkit.entity.Player; - -import javax.json.*; -import javax.json.stream.JsonGenerator; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigDecimal; -import java.util.*; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import javax.json.JsonReader; +import javax.json.JsonValue; +import javax.json.JsonWriter; +import javax.json.stream.JsonGenerator; + +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; +import com.mstiles92.plugins.hardcoredeathban.util.Log; public class PlayerData { private static File file; private static Map instances = new HashMap<>(); + private transient UUID uuid; private String lastSeenName; private long unbanTimeInMillis; private int revivalCredits; @@ -86,11 +100,23 @@ public static void save() { } } + public static void startAutosaveTask() { + final int delay = HardcoreDeathBan.getConfigObject().playerDataAutosaveSeconds() * 20; + new BukkitRunnable() { + @Override + public void run() { + save(); + Log.info("Player data has been automatically saved."); + } + }.runTaskTimer(HardcoreDeathBan.getInstance(), delay, delay); + } + private static void deserialize(JsonObject json) { instances.clear(); for (Map.Entry entry : json.entrySet()) { - instances.put(UUID.fromString(entry.getKey()), new PlayerData((JsonObject) entry.getValue())); + UUID playerUuid = UUID.fromString(entry.getKey()); + instances.put(playerUuid, new PlayerData((JsonObject) entry.getValue(), playerUuid)); } } @@ -102,13 +128,15 @@ private static JsonObject serialize() { return builder.build(); } - private PlayerData(JsonObject json) { + private PlayerData(JsonObject json, UUID playerUuid) { + this.uuid = playerUuid; lastSeenName = json.getString("lastSeenName"); unbanTimeInMillis = json.getJsonNumber("unbanTimeInMillis").longValueExact(); revivalCredits = json.getInt("revivalCredits"); } private PlayerData(Player player) { + this.uuid = player.getUniqueId(); lastSeenName = player.getName(); unbanTimeInMillis = -1; revivalCredits = HardcoreDeathBan.getConfigObject().getStartingCredits(); //TODO: check for death classes as well @@ -158,7 +186,7 @@ public static PlayerData get(UUID playerUUID) { @Deprecated public static PlayerData get(String playerName) { for (PlayerData playerData : instances.values()) { - if (playerData.getLastSeenName().equals(playerName)) { + if (playerData.getLastSeenName().equalsIgnoreCase(playerName)) { return playerData; } } @@ -218,4 +246,8 @@ public boolean removeRevivalCredits(int amount) { return false; } } + + public UUID getPlayerUUID() { + return this.uuid; + } } diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/listeners/PlayerListener.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/listeners/PlayerListener.java index 282bfcb..6d59dd9 100644 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/listeners/PlayerListener.java +++ b/src/main/java/com/mstiles92/plugins/hardcoredeathban/listeners/PlayerListener.java @@ -23,10 +23,7 @@ package com.mstiles92.plugins.hardcoredeathban.listeners; -import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; -import com.mstiles92.plugins.hardcoredeathban.data.PlayerData; -import com.mstiles92.plugins.hardcoredeathban.util.Log; -import com.mstiles92.plugins.hardcoredeathban.util.Utils; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -35,12 +32,17 @@ import org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result; import org.bukkit.event.player.PlayerJoinEvent; +import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; +import com.mstiles92.plugins.hardcoredeathban.data.PlayerData; +import com.mstiles92.plugins.hardcoredeathban.util.Log; +import com.mstiles92.plugins.hardcoredeathban.util.Utils; + /** * PlayerListener is the class used to register the event handlers needed for this plugin's operation. */ public class PlayerListener implements Listener { - @EventHandler(ignoreCancelled = true) + @EventHandler public void onPlayerDeath(PlayerDeathEvent event) { if (HardcoreDeathBan.getConfigObject().isEnabled() && !event.getEntity().hasPermission("deathban.ban.exempt")) { Log.verbose("Player death: " + event.getEntity().getName()); @@ -48,7 +50,7 @@ public void onPlayerDeath(PlayerDeathEvent event) { } } - @EventHandler(ignoreCancelled = true) + @EventHandler public void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) { PlayerData playerData = PlayerData.get(event.getUniqueId()); diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/tasks/KickRunnable.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/tasks/KickRunnable.java index 1261185..1a74dc5 100644 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/tasks/KickRunnable.java +++ b/src/main/java/com/mstiles92/plugins/hardcoredeathban/tasks/KickRunnable.java @@ -23,15 +23,16 @@ package com.mstiles92.plugins.hardcoredeathban.tasks; -import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; -import com.mstiles92.plugins.hardcoredeathban.data.DeathClass; -import com.mstiles92.plugins.hardcoredeathban.util.Log; -import com.mstiles92.plugins.hardcoredeathban.util.Utils; +import java.util.UUID; + import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; -import java.util.UUID; +import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; +import com.mstiles92.plugins.hardcoredeathban.data.DeathClass; +import com.mstiles92.plugins.hardcoredeathban.util.Log; +import com.mstiles92.plugins.hardcoredeathban.util.Utils; /** * KickRunnable is a BukkitRunnable used to kick a Player after a short delay when getting banned after death. diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Bans.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Bans.java deleted file mode 100644 index 498dcde..0000000 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Bans.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * This document is a part of the source code and related artifacts for - * HardcoreDeathBan, an open source Bukkit plugin for hardcore-type servers - * where players are temporarily banned upon death. - * - * http://dev.bukkit.org/bukkit-plugins/hardcoredeathban/ - * http://github.com/mstiles92/HardcoreDeathBan - * - * Copyright (c) 2014 Matthew Stiles (mstiles92) - * - * Licensed under the Common Development and Distribution License Version 1.0 - * You may not use this file except in compliance with this License. - * - * You may obtain a copy of the CDDL-1.0 License at - * http://opensource.org/licenses/CDDL-1.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the license. - */ - -package com.mstiles92.plugins.hardcoredeathban.util; - -import com.mstiles92.plugins.hardcoredeathban.data.DeathClass; -import com.mstiles92.plugins.stileslib.calendar.CalendarUtils; -import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; -import com.mstiles92.plugins.hardcoredeathban.tasks.KickRunnable; -import org.bukkit.ChatColor; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.permissions.Permission; -import org.bukkit.permissions.PermissionDefault; - -import java.io.File; -import java.io.IOException; -import java.util.Calendar; -import java.util.Set; - -/** - * Bans is a class used to store and modify the ban length of each player. - * - * @author mstiles92 - */ -public class Bans { - private final HardcoreDeathBan plugin; - private YamlConfiguration config; - private File file; - - /** - * The main constructor to be used with this class. - * - * @param plugin the instance of the plugin - * @param filename name of the file to save to disk - * @throws Exception if there is an error while opening or creating the file - */ - public Bans(HardcoreDeathBan plugin, String filename) throws Exception { - this.plugin = plugin; - load(filename); - - if (HardcoreDeathBan.getConfigObject().getDeathClasses().size() == 0) { - Log.verbose("No death classes found."); - } else { - for (DeathClass deathClass : HardcoreDeathBan.getConfigObject().getDeathClasses()) { - Log.verbose("Death class loaded: " + deathClass.getName()); - } - } - } - - private void load(String filename) throws Exception { - file = new File(plugin.getDataFolder(), filename); - config = new YamlConfiguration(); - - if (!file.exists()) { - file.createNewFile(); - } - config.load(file); - } - - /** - * Save the config to a file. - */ - public void save() { - try { - config.save(file); - } catch (IOException e) { - Log.warning(ChatColor.RED + "Error occurred while saving bans config file."); - } - } - - /** - * Get the date and time that the specified player is unbanned after. - * - * @param player name of the player to check - * @return a Calendar object that specifies the date and time when the - * player's ban is over, or null if the player is not banned - */ - public Calendar getUnbanCalendar(String player) { - if (player == null) { - return null; - } - Calendar calendar = Calendar.getInstance(); - final long ms = config.getLong(player.toLowerCase(), 0); - if (ms == 0) { - return null; - } - calendar.setTimeInMillis(ms); - return calendar; - } - - /** - * Check if the specified player is currently banned. - * - * @param player the name of the player to check - * @return true if the player is currently banned, false otherwise - */ - public boolean checkPlayerIsBanned(String player) { - final Calendar unban = getUnbanCalendar(player); - final Calendar now = Calendar.getInstance(); - if (unban != null) { - if (unban.after(now)) { - return true; - } - unbanPlayer(player); - } - return false; - } - - /** - * Unban the specified player. - * - * @param player name of the player to be unbanned - */ - public void unbanPlayer(String player) { - Log.verbose("Player unbanned: " + player); - config.set(player.toLowerCase(), null); - } - - /** - * Ban a player for their default time, taking possible death classes into account. - * - * @param player the player to ban - */ - public void banPlayer(String player) { - final Player p = plugin.getServer().getPlayerExact(player); - if (p != null) { - for (DeathClass deathClass : HardcoreDeathBan.getConfigObject().getDeathClasses()) { - if (p.hasPermission(deathClass.getPermission())) { - banPlayer(player, deathClass.getBanTime()); - Log.verbose("Death class " + deathClass.getName() + " detected for " + player); - return; - } - } - } - - Log.verbose("No death class detected for " + player); - banPlayer(player, HardcoreDeathBan.getConfigObject().getBanTime()); - } - - /** - * Ban a player for a specified time. - * - * @param player the player to ban - * @param time the amount of time the player will be banned - */ - public void banPlayer(String player, String time) { - final Player p = plugin.getServer().getPlayerExact(player); - try { - final Calendar unbanDate = CalendarUtils.parseTimeDifference(time); - - if (p != null) { // Player is online - if (!p.hasPermission("deathban.ban.exempt")) { - config.set(player.toLowerCase(), unbanDate.getTimeInMillis()); - save(); - Log.verbose("Player added to ban list: " + player); - plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new KickRunnable(p.getUniqueId()), HardcoreDeathBan.getConfigObject().getTickDelay()); - } - } else { // Player is offline - config.set(player.toLowerCase(), unbanDate.getTimeInMillis()); - save(); - Log.verbose("Offline player added to ban list: " + player); - } - } catch (Exception e) { - Log.verbose("Error occurred while banning player: " + player); - } - } -} diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Log.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Log.java index 5bd885b..1a32c0f 100644 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Log.java +++ b/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Log.java @@ -23,10 +23,10 @@ package com.mstiles92.plugins.hardcoredeathban.util; -import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; - import java.util.logging.Level; +import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; + public class Log { public static void log(Level level, String message) { HardcoreDeathBan.getInstance().getLogger().log(level, message); diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/RevivalCredits.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/RevivalCredits.java deleted file mode 100644 index 7e22a14..0000000 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/RevivalCredits.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * This document is a part of the source code and related artifacts for - * HardcoreDeathBan, an open source Bukkit plugin for hardcore-type servers - * where players are temporarily banned upon death. - * - * http://dev.bukkit.org/bukkit-plugins/hardcoredeathban/ - * http://github.com/mstiles92/HardcoreDeathBan - * - * Copyright (c) 2014 Matthew Stiles (mstiles92) - * - * Licensed under the Common Development and Distribution License Version 1.0 - * You may not use this file except in compliance with this License. - * - * You may obtain a copy of the CDDL-1.0 License at - * http://opensource.org/licenses/CDDL-1.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the license. - */ - -package com.mstiles92.plugins.hardcoredeathban.util; - -import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; -import org.bukkit.ChatColor; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.File; -import java.io.IOException; - -/** - * RevivalCredits is the class used to store the amount of credits each player - * currently holds, as well as methods to modify the credits of each player. - * - * @author mstiles92 - */ -public class RevivalCredits { - - private final HardcoreDeathBan plugin; - private YamlConfiguration config; - private File file; - - /** - * The main constructor to be used with this class. - * - * @param plugin instance of the plugin - * @param filename name of the file to save to disk - * @throws Exception if there is an error while opening or creating the file - */ - public RevivalCredits(HardcoreDeathBan plugin, String filename) throws Exception { - this.plugin = plugin; - load(filename); - } - - private void load(String filename) throws Exception { - file = new File(plugin.getDataFolder(), filename); - config = new YamlConfiguration(); - - if (!file.exists()) { - file.createNewFile(); - } - config.load(file); - } - - /** - * Save the config to a file. - */ - public void save() { - try { - config.save(file); - } catch (IOException e) { - Log.warning(ChatColor.RED + "Error occurred while saving credits config file."); - } - } - - /** - * Get the number of credits a player currently holds. - * - * @param player the name of the player to check - * @return the number of credits the player holds - */ - public int getPlayerCredits(String player) { - if (!config.contains(player.toLowerCase())) { - config.set(player.toLowerCase(), plugin.getConfig().getInt("Starting-Credits")); - save(); - } - return config.getInt(player.toLowerCase()); - } - - /** - * Give credits to a player. - * - * @param player the player to give the credits to - * @param amount the amount of credits to give - */ - public void givePlayerCredits(String player, int amount) { - if (!config.contains(player.toLowerCase())) { - config.set(player.toLowerCase(), HardcoreDeathBan.getConfigObject().getStartingCredits()); - } - config.set(player.toLowerCase(), amount + config.getInt(player.toLowerCase())); - save(); - } - - /** - * Give credits to all registered players. - * - * @param amount the amount of credits to give - */ - public void giveAllPlayersCredits(int amount) { - for (String player : config.getKeys(false)) { - givePlayerCredits(player, amount); - } - } - - /** - * Set the amount of credits a player currently holds. - * - * @param player the player to set the credits for - * @param amount the amount of credits the player should have - */ - public void setPlayerCredits(String player, int amount) { - config.set(player.toLowerCase(), amount); - save(); - } - - /** - * Reset all players' credits to the amount recieved when starting out. - */ - public void resetAllPlayersCredits() { - final int startingAmount = HardcoreDeathBan.getConfigObject().getStartingCredits(); - for (String player : config.getKeys(false)) { - setPlayerCredits(player, startingAmount); - } - } - - /** - * Check if a player has been seen by this plugin before. - * - * @param player the name of the player to check - * @return true if they are registered with this plugin, false otherwise - */ - public boolean checkPlayerHasPlayedBefore(String player) { - return config.contains(player.toLowerCase()); - } -} diff --git a/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Utils.java b/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Utils.java index 6e691bc..4ac38a2 100644 --- a/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Utils.java +++ b/src/main/java/com/mstiles92/plugins/hardcoredeathban/util/Utils.java @@ -23,19 +23,19 @@ package com.mstiles92.plugins.hardcoredeathban.util; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.List; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + import com.mstiles92.plugins.hardcoredeathban.HardcoreDeathBan; import com.mstiles92.plugins.hardcoredeathban.data.DeathClass; import com.mstiles92.plugins.hardcoredeathban.data.PlayerData; import com.mstiles92.plugins.hardcoredeathban.tasks.KickRunnable; import com.mstiles92.plugins.stileslib.calendar.CalendarUtils; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.GregorianCalendar; -import java.util.List; -import java.util.UUID; public class Utils { private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("hh:mm a z"); @@ -72,6 +72,22 @@ public static String replaceMessageVariables(String message, UUID playerUUID) { return message; } + /** + * Ban the specified Player from the server. They will be banned for the amount of time specified by their death + * class if they have one, or for the amount of time specified in the plugin's config if they do not. + * + * @param playerUuid the UUID of the player who should be banned + */ + public static void banPlayer(UUID playerUuid) { + DeathClass deathClass = getDeathClass(playerUuid); + + if (deathClass == null) { + banPlayer(playerUuid, HardcoreDeathBan.getConfigObject().getBanTime()); + } else { + banPlayer(playerUuid, deathClass.getBanTime()); + } + } + /** * Ban the specified Player from the server. They will be banned for the amount of time specified by their death * class if they have one, or for the amount of time specified in the plugin's config if they do not. @@ -79,12 +95,33 @@ public static String replaceMessageVariables(String message, UUID playerUUID) { * @param player the Player who should be banned */ public static void banPlayer(Player player) { - DeathClass deathClass = getDeathClass(player); + banPlayer(player.getUniqueId()); + } - if (deathClass == null) { - banPlayer(player, HardcoreDeathBan.getConfigObject().getBanTime()); - } else { - banPlayer(player, deathClass.getBanTime()); + /** + * Ban the specified Player from the server for a specific amount of time. + * + * @param playerUuid the UUID of the player who should be banned + * @param banTime the amount of time the Player should be banned for + */ + public static void banPlayer(UUID playerUuid, String banTime) { + Player player = Bukkit.getPlayer(playerUuid); + + if (player != null && player.hasPermission("deathban.ban.exempt")) { + return; + } + + PlayerData playerData = PlayerData.get(playerUuid); + if (playerData == null && player != null && player.isOnline()) { + playerData = PlayerData.get(player); + } + + Calendar unbanDate = CalendarUtils.parseTimeDifference(banTime); + playerData.setUnbanTimeInMillis(unbanDate.getTimeInMillis()); + + if (player != null && player.isOnline()) { + KickRunnable runnable = new KickRunnable(player.getUniqueId()); + runnable.runTaskLater(HardcoreDeathBan.getInstance(), HardcoreDeathBan.getConfigObject().getTickDelay()); } } @@ -95,15 +132,7 @@ public static void banPlayer(Player player) { * @param banTime the amount of time the Player should be banned for */ public static void banPlayer(Player player, String banTime) { - if (!player.hasPermission("deathban.ban.exempt")) { - Calendar unbanDate = CalendarUtils.parseTimeDifference(banTime); - PlayerData.get(player).setUnbanTimeInMillis(unbanDate.getTimeInMillis()); - - if (player.isOnline()) { - KickRunnable runnable = new KickRunnable(player.getUniqueId()); - runnable.runTaskLater(HardcoreDeathBan.getInstance(), HardcoreDeathBan.getConfigObject().getTickDelay()); - } - } + banPlayer(player.getUniqueId(), banTime); } /** @@ -176,4 +205,14 @@ public static boolean checkPlayerBanned(UUID playerUUID) { return false; } + + /** + * Check if the specified Player is currently banned. + * + * @param player the Player whose ban status should be checked + * @return true if the Player is banned, false if they are not + */ + public static boolean checkPlayerBanned(Player player) { + return checkPlayerBanned(player.getUniqueId()); + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 80a39ac..7c116b5 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -6,10 +6,12 @@ Tick-Delay: 15 Starting-Credits: 0 Verbose: false Check-for-Updates: true +PlayerData-Autosave-Enabled: true +PlayerData-Autosave-Seconds: 300 Death-Classes: Donator: Ban-Time: 1h Death-Message: Thank you for donating! You are now banned for a reduced time of %bantimeleft%. VIP: Ban-Time: 15m - Death-Message: Thank you for being awesome! You are only banned for %bantimeleft%. \ No newline at end of file + Death-Message: Thank you for being awesome! You are only banned for %bantimeleft%.