Merge pull request #34 from KingFaris10/master

Added /cstoggle command
This commit is contained in:
Andrzej Pomirski 2014-05-21 14:36:41 +02:00
commit c82d2b81c0
7 changed files with 102 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package com.Acrobot.ChestShop;
import com.Acrobot.Breeze.Configuration.Configuration;
import com.Acrobot.ChestShop.Commands.Give;
import com.Acrobot.ChestShop.Commands.ItemInfo;
import com.Acrobot.ChestShop.Commands.Toggle;
import com.Acrobot.ChestShop.Commands.Version;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
@ -19,10 +20,7 @@ import com.Acrobot.ChestShop.Listeners.Item.ItemMoveListener;
import com.Acrobot.ChestShop.Listeners.ItemInfoListener;
import com.Acrobot.ChestShop.Listeners.Modules.DiscountModule;
import com.Acrobot.ChestShop.Listeners.Modules.PriceRestrictionModule;
import com.Acrobot.ChestShop.Listeners.Player.PlayerConnect;
import com.Acrobot.ChestShop.Listeners.Player.PlayerInteract;
import com.Acrobot.ChestShop.Listeners.Player.PlayerInventory;
import com.Acrobot.ChestShop.Listeners.Player.PlayerTeleport;
import com.Acrobot.ChestShop.Listeners.Player.*;
import com.Acrobot.ChestShop.Listeners.PostShopCreation.CreationFeeGetter;
import com.Acrobot.ChestShop.Listeners.PostShopCreation.MessageSender;
import com.Acrobot.ChestShop.Listeners.PostShopCreation.ShopCreationLogger;
@ -133,6 +131,7 @@ public class ChestShop extends JavaPlugin {
getCommand("iteminfo").setExecutor(new ItemInfo());
getCommand("csVersion").setExecutor(new Version());
getCommand("csGive").setExecutor(new Give());
getCommand("cstoggle").setExecutor(new Toggle());
startStatistics();
startUpdater();
@ -240,6 +239,8 @@ public class ChestShop extends JavaPlugin {
public void onDisable() {
getServer().getScheduler().cancelTasks(this);
Toggle.clearToggledPlayers();
if (handler != null) {
handler.close();
getLogger().removeHandler(handler);
@ -266,6 +267,7 @@ public class ChestShop extends JavaPlugin {
registerEvent(new PlayerConnect());
registerEvent(new PlayerInteract());
registerEvent(new PlayerInventory());
registerEvent(new PlayerLeave());
registerEvent(new PlayerTeleport());
registerEvent(new ItemInfoListener());

View File

@ -0,0 +1,61 @@
package com.Acrobot.ChestShop.Commands;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Permission;
import org.apache.commons.lang.Validate;
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.ArrayList;
import java.util.List;
/**
* @author KingFaris10
*/
public class Toggle implements CommandExecutor {
private static final List<String> toggledPlayers = new ArrayList<String>();
@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 {
return false;
}
}
public static void clearToggledPlayers() {
toggledPlayers.clear();
}
public static boolean isIgnoring(OfflinePlayer player) {
return player != null && toggledPlayers.contains(player.getName());
}
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.
if (ignoring) {
if (!toggledPlayers.contains(player.getName()))
toggledPlayers.add(player.getName());
} else {
if (toggledPlayers.contains(player.getName()))
toggledPlayers.remove(player.getName());
}
return ignoring;
}
}

View File

@ -67,6 +67,10 @@ public class Messages {
@PrecededBySpace
public static String CANNOT_CREATE_SHOP_HERE = "You can't create shop here!";
@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 prefix(String message) {
return prefix + message;
}

View File

@ -0,0 +1,19 @@
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;
/**
* @author KingFaris10
*/
public class PlayerLeave implements Listener {
@EventHandler
public static void onPlayerLeave(PlayerQuitEvent event) {
if (Toggle.isIgnoring(event.getPlayer())) Toggle.setIgnoring(event.getPlayer(), false);
}
}

View File

@ -2,6 +2,7 @@ package com.Acrobot.ChestShop.Listeners.PostTransaction;
import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.Commands.Toggle;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Economy.Economy;
@ -45,7 +46,7 @@ public class TransactionMessageSender implements Listener {
player.sendMessage(message);
}
if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER) {
if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwner())) {
String message = formatMessage(Messages.SOMEBODY_BOUGHT_FROM_YOUR_SHOP, itemName, price);
message = message.replace("%buyer", player.getName());
@ -68,7 +69,7 @@ public class TransactionMessageSender implements Listener {
player.sendMessage(message);
}
if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER) {
if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwner())) {
String message = formatMessage(Messages.SOMEBODY_SOLD_TO_YOUR_SHOP, itemName, price);
message = message.replace("%seller", player.getName());

View File

@ -24,7 +24,9 @@ public enum Permission {
GROUP("ChestShop.group."),
NOFEE("ChestShop.nofee"),
DISCOUNT("ChestShop.discount.");
DISCOUNT("ChestShop.discount."),
NOTIFY_TOGGLE("ChestShop.toggle");
private final String permission;

View File

@ -31,6 +31,9 @@ commands:
aliases: [chestshop]
description: Shows the ChestShop's version
usage: /<command>
cstoggle:
description: Toggle messages to the owner of a shop
usage: /<command>
permissions:
ChestShop.*:
@ -67,6 +70,9 @@ permissions:
description: Allows user to sell to a shop
ChestShop.nofee:
description: User doesn't have to pay the shop creation fee.
ChestShop.toggle:
description: Allows user to toggle messages.
default: true
ChestShop.admin:
description: Allows user to modify/destroy other stores and create an Admin Shops
default: op