Refactoring the latest /cstoggle changes

This commit is contained in:
Andrzej Pomirski 2014-05-24 11:15:54 +02:00
parent c82d2b81c0
commit 39213b2be1
3 changed files with 31 additions and 20 deletions

View File

@ -20,22 +20,28 @@ public class Toggle implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (Permission.has(player, Permission.NOTIFY_TOGGLE)) {
if (args.length == 0) {
if (setIgnoring(player, !toggledPlayers.contains(player.getName()))) player.sendMessage(Messages.TOGGLE_MESSAGES_OFF);
else player.sendMessage(Messages.TOGGLE_MESSAGES_ON);
} else {
return false;
}
} else {
player.sendMessage(Messages.ACCESS_DENIED);
}
return true;
} else {
if (!(sender instanceof Player)) {
return false;
}
if (!Permission.has(sender, Permission.NOTIFY_TOGGLE)) {
sender.sendMessage(Messages.ACCESS_DENIED);
return true;
}
Player player = (Player) sender;
if (args.length != 0) {
return false;
}
if (setIgnoring(player, !toggledPlayers.contains(player.getName()))) {
player.sendMessage(Messages.TOGGLE_MESSAGES_OFF);
} else {
player.sendMessage(Messages.TOGGLE_MESSAGES_ON);
}
return true;
}
public static void clearToggledPlayers() {
@ -47,14 +53,18 @@ public class Toggle implements CommandExecutor {
}
public static boolean setIgnoring(Player player, boolean ignoring) {
Validate.notNull(player); // Make sure the player instance is not null. I believe this should be here instead of (object != null) because if the player is null, it shows there is an error with the code.
Validate.notNull(player); // Make sure the player instance is not null, in case there are any errors in the code
if (ignoring) {
if (!toggledPlayers.contains(player.getName()))
if (!toggledPlayers.contains(player.getName())) {
toggledPlayers.add(player.getName());
}
} else {
if (toggledPlayers.contains(player.getName()))
if (toggledPlayers.contains(player.getName())) {
toggledPlayers.remove(player.getName());
}
}
return ignoring;
}

View File

@ -69,7 +69,7 @@ public class Messages {
@PrecededBySpace
public static String TOGGLE_MESSAGES_OFF = "You will no longer receive messages from your shop(s).";
public static String TOGGLE_MESSAGES_ON = "You will now receive messages from your shop(s)..";
public static String TOGGLE_MESSAGES_ON = "You will now receive messages from your shop(s).";
public static String prefix(String message) {
return prefix + message;

View File

@ -3,7 +3,6 @@ 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.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
/**
@ -13,7 +12,9 @@ public class PlayerLeave implements Listener {
@EventHandler
public static void onPlayerLeave(PlayerQuitEvent event) {
if (Toggle.isIgnoring(event.getPlayer())) Toggle.setIgnoring(event.getPlayer(), false);
if (Toggle.isIgnoring(event.getPlayer())) {
Toggle.setIgnoring(event.getPlayer(), false);
}
}
}