diff --git a/src/main/java/com/Acrobot/ChestShop/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/ChestShop.java index 5f968146d..bf420acc8 100644 --- a/src/main/java/com/Acrobot/ChestShop/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/ChestShop.java @@ -322,8 +322,6 @@ public void onDisable() { executorService.awaitTermination(15, TimeUnit.SECONDS); } catch (InterruptedException ignored) {} - Toggle.clearToggledPlayers(); - if (handler != null) { handler.close(); getLogger().removeHandler(handler); @@ -354,7 +352,6 @@ private void registerEvents() { registerEvent(new PlayerConnect()); registerEvent(new PlayerInteract()); registerEvent(new PlayerInventory()); - registerEvent(new PlayerLeave()); registerEvent(new PlayerTeleport()); registerEvent(new SignParseListener()); diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java b/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java index 70d8284c0..804958290 100644 --- a/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java +++ b/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java @@ -1,7 +1,8 @@ package com.Acrobot.ChestShop.Commands; import com.Acrobot.ChestShop.Configuration.Messages; -import com.google.common.base.Preconditions; +import com.Acrobot.ChestShop.Database.Account; +import com.Acrobot.ChestShop.UUIDs.NameManager; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; @@ -9,15 +10,12 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import java.util.HashSet; -import java.util.Set; import java.util.UUID; /** * @author KingFaris10 */ public class Toggle implements CommandExecutor { - private static final Set toggledPlayers = new HashSet<>(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -31,7 +29,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - if (setIgnoring(player, !isIgnoring(player))) { + Account account = NameManager.getOrCreateAccount(player); + account.setIgnoreMessages(!account.isIgnoringMessages()); + + if (account.isIgnoringMessages()) { Messages.TOGGLE_MESSAGES_OFF.sendWithPrefix(player); } else { Messages.TOGGLE_MESSAGES_ON.sendWithPrefix(player); @@ -40,16 +41,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } - public static void clearToggledPlayers() { - toggledPlayers.clear(); - } - public static boolean isIgnoring(OfflinePlayer player) { - return player != null && isIgnoring(player.getUniqueId()); + return player != null && NameManager.getOrCreateAccount(player).isIgnoringMessages(); } public static boolean isIgnoring(UUID playerId) { - return toggledPlayers.contains(playerId); + Account account = NameManager.getAccount(playerId); + return account != null && account.isIgnoringMessages(); } /** @@ -60,16 +58,4 @@ public static boolean isIgnoring(String playerName) { return isIgnoring(Bukkit.getOfflinePlayer(playerName)); } - public static boolean setIgnoring(Player player, boolean ignoring) { - Preconditions.checkNotNull(player); // Make sure the player instance is not null, in case there are any errors in the code - - if (ignoring) { - toggledPlayers.add(player.getUniqueId()); - } else { - toggledPlayers.remove(player.getUniqueId()); - } - - return ignoring; - } - } diff --git a/src/main/java/com/Acrobot/ChestShop/Database/Account.java b/src/main/java/com/Acrobot/ChestShop/Database/Account.java index 5af9c8227..c6773692a 100644 --- a/src/main/java/com/Acrobot/ChestShop/Database/Account.java +++ b/src/main/java/com/Acrobot/ChestShop/Database/Account.java @@ -28,6 +28,9 @@ public class Account { @DatabaseField(canBeNull = false, dataType = DataType.DATE_LONG, defaultValue = "0") private Date lastSeen; + @DatabaseField(canBeNull = false, dataType = DataType.BOOLEAN, defaultValue = "0") + private boolean ignoreMessages; + public Account() { //empty constructor, needed for ORMLite } @@ -73,4 +76,12 @@ public Date getLastSeen() { public void setLastSeen(Date lastSeen) { this.lastSeen = lastSeen; } + + public boolean isIgnoringMessages() { + return ignoreMessages; + } + + public void setIgnoreMessages(boolean ignoreMessages) { + this.ignoreMessages = ignoreMessages; + } } diff --git a/src/main/java/com/Acrobot/ChestShop/Database/Migrations.java b/src/main/java/com/Acrobot/ChestShop/Database/Migrations.java index f5a2a2ccc..5d8e32217 100644 --- a/src/main/java/com/Acrobot/ChestShop/Database/Migrations.java +++ b/src/main/java/com/Acrobot/ChestShop/Database/Migrations.java @@ -5,7 +5,6 @@ import com.j256.ormlite.dao.Dao; import com.j256.ormlite.dao.GenericRawResults; -import java.io.IOException; import java.sql.SQLException; import java.util.Date; import java.util.UUID; @@ -17,7 +16,7 @@ * @author Andrzej Pomirski */ public class Migrations { - public static final int CURRENT_DATABASE_VERSION = 4; + public static final int CURRENT_DATABASE_VERSION = 5; /** * Migrates a database from the given version @@ -50,6 +49,12 @@ public static int migrate(int currentVersion) { return -1; } case 4: + if (migrateTo5()) { + currentVersion++; + } else { + return -1; + } + case 5: default: break; //do nothing @@ -147,4 +152,15 @@ private static boolean migrateTo4() { return false; } } + + private static boolean migrateTo5() { + try { + Dao accounts = DaoCreator.getDao(Account.class); + accounts.executeRaw("ALTER TABLE `accounts` ADD COLUMN ignoreMessages BOOLEAN"); + return true; + } catch (SQLException e) { + ChestShop.getBukkitLogger().log(Level.SEVERE, "Error while migrating database to v5", e); + return false; + } + } } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerLeave.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerLeave.java deleted file mode 100644 index bf5bb2fc4..000000000 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerLeave.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.Acrobot.ChestShop.Listeners.Player; - -import com.Acrobot.ChestShop.Commands.Toggle; -import com.Acrobot.ChestShop.UUIDs.NameManager; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerQuitEvent; - -/** - * @author KingFaris10 - */ -public class PlayerLeave implements Listener { - - @EventHandler - public static void onPlayerLeave(PlayerQuitEvent event) { - if (Toggle.isIgnoring(event.getPlayer())) { - Toggle.setIgnoring(event.getPlayer(), false); - } - } - -}