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

Toggle Ignore Persistent #612

Open
wants to merge 3 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
4 changes: 1 addition & 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,7 @@ private void registerEvents() {
registerEvent(new PlayerConnect());
registerEvent(new PlayerInteract());
registerEvent(new PlayerInventory());
registerEvent(new PlayerLeave());
registerEvent(new PlayerJoin());
registerEvent(new PlayerTeleport());

registerEvent(new SignParseListener());
Expand Down
97 changes: 69 additions & 28 deletions src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
// Toggle.java
package com.Acrobot.ChestShop.Commands;

import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Database.DaoCreator;
import com.google.common.base.Preconditions;
import org.bukkit.Bukkit;
import com.j256.ormlite.dao.Dao;
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.sql.SQLException;
import java.util.UUID;

/**
* @author KingFaris10
* Command executor class for toggling the ignoring state of players.
*/
public class Toggle implements CommandExecutor {
private static final Set<UUID> toggledPlayers = new HashSet<>();
private static Dao<Account, UUID> accountDao;

// Static block to initialize the DAO for Account
static {
try {
accountDao = DaoCreator.getDaoAndCreateTable(Account.class);
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* Handles the command execution to toggle the ignoring state.
*
* @param sender The command sender.
* @param command The command.
* @param label The command label.
* @param args The command arguments.
* @return true if the command was successful, false otherwise.
*/
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
Expand All @@ -31,45 +51,66 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return false;
}

if (setIgnoring(player, !isIgnoring(player))) {
Messages.TOGGLE_MESSAGES_OFF.sendWithPrefix(player);
} else {
Messages.TOGGLE_MESSAGES_ON.sendWithPrefix(player);
try {
if (setIgnoring(player, !isIgnoring(player))) {
Messages.TOGGLE_MESSAGES_OFF.sendWithPrefix(player);
} else {
Messages.TOGGLE_MESSAGES_ON.sendWithPrefix(player);
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}

return true;
}

public static void clearToggledPlayers() {
toggledPlayers.clear();
}

public static boolean isIgnoring(OfflinePlayer player) {
/**
* Checks if the player is in the ignoring state.
*
* @param player The player to check.
* @return true if the player is ignoring, false otherwise.
* @throws SQLException if a database access error occurs.
*/
public static boolean isIgnoring(OfflinePlayer player) throws SQLException {
return player != null && isIgnoring(player.getUniqueId());
}

public static boolean isIgnoring(UUID playerId) {
return toggledPlayers.contains(playerId);
/**
* Checks if the player with the given UUID is in the ignoring state.
*
* @param playerId The UUID of the player to check.
* @return true if the player is ignoring, false otherwise.
* @throws SQLException if a database access error occurs.
*/
public static boolean isIgnoring(UUID playerId) throws SQLException {
Account account = accountDao.queryForId(playerId);
return account != null && account.isToggled();
}

/**
* @deprecated Use {@link #isIgnoring(UUID)}
* Sets the ignoring state for the player.
*
* @param player The player to set the state for.
* @param ignoring The new ignoring state.
* @return true if the state was successfully set, false otherwise.
* @throws SQLException if a database access error occurs.
*/
@Deprecated
public static boolean isIgnoring(String playerName) {
return isIgnoring(Bukkit.getOfflinePlayer(playerName));
}
public static boolean setIgnoring(Player player, boolean ignoring) throws SQLException {
Preconditions.checkNotNull(player);

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
UUID playerId = player.getUniqueId();
Account account = accountDao.queryForId(playerId);

if (ignoring) {
toggledPlayers.add(player.getUniqueId());
if (account == null) {
account = new Account(player.getName(), playerId);
account.setToggled(ignoring);
accountDao.create(account);
} else {
toggledPlayers.remove(player.getUniqueId());
account.setToggled(ignoring);
accountDao.update(account);
}

return ignoring;
}

}
}
16 changes: 14 additions & 2 deletions src/main/java/com/Acrobot/ChestShop/Database/Account.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Account.java
package com.Acrobot.ChestShop.Database;

import com.Acrobot.Breeze.Utils.NameUtil;
Expand All @@ -10,7 +11,6 @@

/**
* A mapping for an account
* @author Andrzej Pomirski (Acrobot)
*/
@DatabaseTable(tableName = "accounts")
@DatabaseFileName("users.db")
Expand All @@ -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 = "false")
private boolean toggled;

public Account() {
//empty constructor, needed for ORMLite
}
Expand All @@ -40,6 +43,7 @@ public Account(String name, String shortName, UUID uuid) {
this.name = name;
this.shortName = shortName;
this.uuid = uuid;
this.toggled = false;
}

public String getName() {
Expand Down Expand Up @@ -73,4 +77,12 @@ public Date getLastSeen() {
public void setLastSeen(Date lastSeen) {
this.lastSeen = lastSeen;
}
}

public boolean isToggled() {
return toggled;
}

public void setToggled(boolean toggled) {
this.toggled = toggled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// PlayerJoin.java
package com.Acrobot.ChestShop.Listeners.Player;

import com.Acrobot.ChestShop.Commands.Toggle;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import java.sql.SQLException;

public class PlayerJoin implements Listener {

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
boolean ignoring;

try {
if (!event.getPlayer().hasPlayedBefore()) {
ignoring = false; // Disable ignoring state for first-time players
} else {
ignoring = Toggle.isIgnoring(event.getPlayer());
}

Toggle.setIgnoring(event.getPlayer(), ignoring);
} catch (SQLException e) {
e.printStackTrace();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.event.Listener;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

Expand All @@ -25,7 +26,7 @@
*/
public class TransactionMessageSender implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public static void onCurrencyTransfer(CurrencyTransferEvent event) {
public static void onCurrencyTransfer(CurrencyTransferEvent event) throws SQLException {
if (event.getTransactionEvent() == null) {
return;
}
Expand All @@ -36,7 +37,7 @@ public static void onCurrencyTransfer(CurrencyTransferEvent event) {
}
}

protected static void sendBuyMessage(CurrencyTransferEvent event) {
protected static void sendBuyMessage(CurrencyTransferEvent event) throws SQLException {
TransactionEvent transactionEvent = event.getTransactionEvent();
Player player = transactionEvent.getClient();

Expand All @@ -50,7 +51,7 @@ protected static void sendBuyMessage(CurrencyTransferEvent event) {
}
}

protected static void sendSellMessage(CurrencyTransferEvent event) {
protected static void sendSellMessage(CurrencyTransferEvent event) throws SQLException {
TransactionEvent transactionEvent = event.getTransactionEvent();
Player player = transactionEvent.getClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;

import java.sql.SQLException;
import java.util.Collections;
import java.util.UUID;

Expand All @@ -43,7 +44,7 @@ public static void onQuit(PlayerQuitEvent event) {
}

@EventHandler(priority = EventPriority.MONITOR)
public static void onMessage(PreTransactionEvent event) {
public static void onMessage(PreTransactionEvent event) throws SQLException {
if (!event.isCancelled()) {
return;
}
Expand Down