Updated events

Methods getPlayer() and getShop() don't need to be overridden,
as they are now declared in the ShopEvent superclass.
This commit is contained in:
Eric 2017-03-21 14:25:49 +01:00
parent bc13dd7a9d
commit 79660920d8
9 changed files with 68 additions and 106 deletions

View File

@ -1,10 +1,7 @@
package de.epiceric.shopchest; package de.epiceric.shopchest;
import de.epiceric.shopchest.config.Regex; import de.epiceric.shopchest.config.Regex;
import de.epiceric.shopchest.event.ShopPreCreateEvent; import de.epiceric.shopchest.event.*;
import de.epiceric.shopchest.event.ShopPreInfoEvent;
import de.epiceric.shopchest.event.ShopPreRemoveEvent;
import de.epiceric.shopchest.event.ShopReloadEvent;
import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.LocalizedMessage; import de.epiceric.shopchest.language.LocalizedMessage;
import de.epiceric.shopchest.nms.JsonBuilder; import de.epiceric.shopchest.nms.JsonBuilder;
@ -62,6 +59,7 @@ class ShopCommand extends BukkitCommand {
case "REMOVE": case "REMOVE":
case "INFO": case "INFO":
case "LIMITS": case "LIMITS":
case "OPEN":
sender.sendMessage(ChatColor.RED + "Only players can use this command."); sender.sendMessage(ChatColor.RED + "Only players can use this command.");
return true; return true;
} }
@ -104,6 +102,9 @@ class ShopCommand extends BukkitCommand {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OCCUPIED_SHOP_SLOTS, p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OCCUPIED_SHOP_SLOTS,
new LocalizedMessage.ReplacedRegex(Regex.LIMIT, (limit < 0 ? "" : String.valueOf(limit))), new LocalizedMessage.ReplacedRegex(Regex.LIMIT, (limit < 0 ? "" : String.valueOf(limit))),
new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(shopUtils.getShopAmount(p))))); new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(shopUtils.getShopAmount(p)))));
} else if (args[0].equalsIgnoreCase("open")) {
needsHelp = false;
open(p);
} }
} }
@ -474,6 +475,25 @@ class ShopCommand extends BukkitCommand {
ClickType.setPlayerClickType(p, new ClickType(EnumClickType.INFO)); ClickType.setPlayerClickType(p, new ClickType(EnumClickType.INFO));
} }
/**
* A given player opens a shop
* @param p The command executor
*/
private void open(Player p) {
plugin.debug(p.getName() + " wants to open a shop");
ShopPreOpenEvent event = new ShopPreOpenEvent(p);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
plugin.debug("Shop pre open event cancelled");
return;
}
plugin.debug(p.getName() + " can now click a chest");
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_OPEN));
ClickType.setPlayerClickType(p, new ClickType(EnumClickType.OPEN));
}
/** /**
* Sends the basic help message * Sends the basic help message
* @param sender {@link CommandSender} who will receive the message * @param sender {@link CommandSender} who will receive the message
@ -491,6 +511,7 @@ class ShopCommand extends BukkitCommand {
sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " remove - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_REMOVE)); sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " remove - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_REMOVE));
sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " info - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_INFO)); sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " info - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_INFO));
sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " limits - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_LIMITS)); sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " limits - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_LIMITS));
sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " open - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_OPEN));
} }
if (sender.hasPermission(Permissions.RELOAD)) { if (sender.hasPermission(Permissions.RELOAD)) {

View File

@ -8,26 +8,18 @@ import org.bukkit.event.Cancellable;
* Called when a player buys or sells something from or to a shop * Called when a player buys or sells something from or to a shop
*/ */
public class ShopBuySellEvent extends ShopEvent implements Cancellable { public class ShopBuySellEvent extends ShopEvent implements Cancellable {
private Player player;
private Shop shop;
private Type type; private Type type;
private int newAmount; private int newAmount;
private double newPrice; private double newPrice;
private boolean cancelled; private boolean cancelled;
public ShopBuySellEvent(Player player, Shop shop, Type type, int newAmount, double newPrice) { public ShopBuySellEvent(Player player, Shop shop, Type type, int newAmount, double newPrice) {
this.player = player; super(player, shop);
this.shop = shop;
this.type = type; this.type = type;
this.newAmount = newAmount; this.newAmount = newAmount;
this.newPrice = newPrice; this.newPrice = newPrice;
} }
@Override
public Shop getShop() {
return shop;
}
/** /**
* @return Whether the player buys or sells something * @return Whether the player buys or sells something
*/ */
@ -49,11 +41,6 @@ public class ShopBuySellEvent extends ShopEvent implements Cancellable {
return newPrice; return newPrice;
} }
@Override
public Player getPlayer() {
return player;
}
@Override @Override
public boolean isCancelled() { public boolean isCancelled() {
return cancelled; return cancelled;

View File

@ -8,27 +8,13 @@ import org.bukkit.event.Cancellable;
* Called when a player creates a shop (clicks on a chest) * Called when a player creates a shop (clicks on a chest)
*/ */
public class ShopCreateEvent extends ShopEvent implements Cancellable { public class ShopCreateEvent extends ShopEvent implements Cancellable {
private Player player;
private Shop shop;
private double creationPrice; private double creationPrice;
private boolean cancelled; private boolean cancelled;
public ShopCreateEvent(Player player, Shop shop, double creationPrice) { public ShopCreateEvent(Player player, Shop shop, double creationPrice) {
this.player = player; super(player, shop);
this.shop = shop;
this.creationPrice = creationPrice; this.creationPrice = creationPrice;
} }
@Override
public Shop getShop() {
return shop;
}
@Override
public Player getPlayer() {
return player;
}
/** /**
* @return The price the player has to pay in order to create the shop (only if the event is not cancelled) * @return The price the player has to pay in order to create the shop (only if the event is not cancelled)
*/ */

View File

@ -8,16 +8,27 @@ import org.bukkit.event.HandlerList;
public abstract class ShopEvent extends Event { public abstract class ShopEvent extends Event {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private Shop shop;
private Player player;
public ShopEvent(Player player, Shop shop) {
this.player = player;
this.shop = shop;
}
/** /**
* @return Shop which is involved in this event * @return Shop which is involved in this event
*/ */
public abstract Shop getShop(); public Shop getShop() {
return shop;
}
/** /**
* @return Player who is involved in this event * @return Player who is involved in this event
*/ */
public abstract Player getPlayer(); public Player getPlayer() {
return player;
}
public static HandlerList getHandlerList() { public static HandlerList getHandlerList() {
return handlers; return handlers;

View File

@ -8,23 +8,10 @@ import org.bukkit.event.Cancellable;
* Called when a player retrieves information about a shop (clicks on a chest) * Called when a player retrieves information about a shop (clicks on a chest)
*/ */
public class ShopInfoEvent extends ShopEvent implements Cancellable { public class ShopInfoEvent extends ShopEvent implements Cancellable {
private Player player;
private Shop shop;
private boolean cancelled; private boolean cancelled;
public ShopInfoEvent(Player player, Shop shop) { public ShopInfoEvent(Player player, Shop shop) {
this.player = player; super(player, shop);
this.shop = shop;
}
@Override
public Shop getShop() {
return shop;
}
@Override
public Player getPlayer() {
return player;
} }
@Override @Override

View File

@ -8,23 +8,10 @@ import org.bukkit.event.Cancellable;
* Called when a player wants to create a shop (enters the command) * Called when a player wants to create a shop (enters the command)
*/ */
public class ShopPreCreateEvent extends ShopEvent implements Cancellable { public class ShopPreCreateEvent extends ShopEvent implements Cancellable {
private Player player;
private Shop shop;
private boolean cancelled; private boolean cancelled;
public ShopPreCreateEvent(Player player, Shop shop) { public ShopPreCreateEvent(Player player, Shop shop) {
this.player = player; super(player, shop);
this.shop = shop;
}
@Override
public Player getPlayer() {
return player;
}
@Override
public Shop getShop() {
return shop;
} }
@Override @Override

View File

@ -8,23 +8,10 @@ import org.bukkit.event.Cancellable;
* Called when a player removes a shop (clicks on a chest) * Called when a player removes a shop (clicks on a chest)
*/ */
public class ShopRemoveEvent extends ShopEvent implements Cancellable { public class ShopRemoveEvent extends ShopEvent implements Cancellable {
private Player player;
private Shop shop;
private boolean cancelled; private boolean cancelled;
public ShopRemoveEvent(Player player, Shop shop) { public ShopRemoveEvent(Player player, Shop shop) {
this.player = player; super(player, shop);
this.shop = shop;
}
@Override
public Shop getShop() {
return shop;
}
@Override
public Player getPlayer() {
return player;
} }
@Override @Override

View File

@ -3,6 +3,10 @@ package de.epiceric.shopchest.event;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
/**
* Called when the shop updater runs <br/>
* It's not recommended to listen to this event!
*/
public class ShopUpdateEvent extends Event { public class ShopUpdateEvent extends Event {
public enum UpdateQuality { public enum UpdateQuality {

View File

@ -218,6 +218,27 @@ public class ShopInteractListener implements Listener {
ClickType.removePlayerClickType(p); ClickType.removePlayerClickType(p);
break; break;
case OPEN:
e.setCancelled(true);
if (shopUtils.isShop(b.getLocation())) {
Shop shop = shopUtils.getShop(b.getLocation());
if (p.getUniqueId().equals(shop.getVendor().getUniqueId()) || p.hasPermission(Permissions.OPEN_OTHER)) {
e.setCancelled(false);
if (!calledFromInteractEvent) {
p.openInventory(shop.getInventoryHolder().getInventory());
}
} else {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_OPEN_OTHERS));
plugin.debug(p.getName() + " is not permitted to open another player's shop");
}
} else {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_NO_SHOP));
plugin.debug("Chest is not a shop");
}
ClickType.removePlayerClickType(p);
break;
} }
} }
} else { } else {
@ -230,35 +251,6 @@ public class ShopInteractListener implements Listener {
} }
} }
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (p.isSneaking() || (shop.getShopType() != ShopType.ADMIN && shop.getVendor().getUniqueId().equals(p.getUniqueId()))) {
if (Utils.getPreferredItemInHand(p) == null) {
e.setCancelled(true);
if (!shop.getVendor().getUniqueId().equals(p.getUniqueId())) {
if (p.hasPermission(Permissions.OPEN_OTHER)) {
String vendorName = (shop.getVendor().getName() == null ? shop.getVendor().getUniqueId().toString() : shop.getVendor().getName());
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OPENED_SHOP, new LocalizedMessage.ReplacedRegex(Regex.VENDOR, vendorName)));
plugin.debug(p.getName() + " is opening " + vendorName + "'s shop (#" + shop.getID() + ")");
e.setCancelled(false);
if (!calledFromInteractEvent) {
p.openInventory(shop.getInventoryHolder().getInventory());
}
} else {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_OPEN_OTHERS));
plugin.debug(p.getName() + " is not permitted to open another player's shop");
}
} else {
e.setCancelled(false);
if (!calledFromInteractEvent) {
p.openInventory(shop.getInventoryHolder().getInventory());
}
}
}
return;
}
}
if ((e.getAction() == Action.RIGHT_CLICK_BLOCK && !inverted) || (e.getAction() == Action.LEFT_CLICK_BLOCK && inverted)) { if ((e.getAction() == Action.RIGHT_CLICK_BLOCK && !inverted) || (e.getAction() == Action.LEFT_CLICK_BLOCK && inverted)) {
e.setCancelled(true); e.setCancelled(true);