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 ignore messages option to Account database (Resolves #575) #602

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/main/java/com/Acrobot/ChestShop/ChestShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
32 changes: 9 additions & 23 deletions src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
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;
import org.bukkit.command.CommandExecutor;
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<UUID> toggledPlayers = new HashSet<>();

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Expand All @@ -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);
Expand All @@ -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();
}

/**
Expand All @@ -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;
}

}
11 changes: 11 additions & 0 deletions src/main/java/com/Acrobot/ChestShop/Database/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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;
}
}
20 changes: 18 additions & 2 deletions src/main/java/com/Acrobot/ChestShop/Database/Migrations.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -147,4 +152,15 @@ private static boolean migrateTo4() {
return false;
}
}

private static boolean migrateTo5() {
try {
Dao<Account, String> 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;
}
}
}

This file was deleted.