Added Events for new API

Javadoc will be added later
This commit is contained in:
Eric 2016-07-05 18:41:31 +02:00
parent 3a50a20bf4
commit 0b3f45ccd1
12 changed files with 413 additions and 2 deletions

View File

@ -2,9 +2,14 @@ package de.epiceric.shopchest;
import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.config.Regex; import de.epiceric.shopchest.config.Regex;
import de.epiceric.shopchest.event.ShopPreCreateEvent;
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.IJsonBuilder; import de.epiceric.shopchest.nms.IJsonBuilder;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.shop.Shop.ShopType;
import de.epiceric.shopchest.utils.ClickType; import de.epiceric.shopchest.utils.ClickType;
import de.epiceric.shopchest.utils.ClickType.EnumClickType; import de.epiceric.shopchest.utils.ClickType.EnumClickType;
@ -204,6 +209,10 @@ public class Commands extends BukkitCommand {
* @param player The command executor * @param player The command executor
*/ */
private void reload(Player player) { private void reload(Player player) {
ShopReloadEvent event = new ShopReloadEvent(player);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return;
player.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(ShopUtils.reloadShops())))); player.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(ShopUtils.reloadShops()))));
} }
@ -320,9 +329,13 @@ public class Commands extends BukkitCommand {
} }
} }
ClickType.setPlayerClickType(p, new ClickType(EnumClickType.CREATE, itemStack, buyPrice, sellPrice, shopType)); ShopPreCreateEvent event = new ShopPreCreateEvent(p, Shop.createImaginaryShop(p, itemStack, buyPrice, sellPrice, shopType));
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_CREATE)); Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
ClickType.setPlayerClickType(p, new ClickType(EnumClickType.CREATE, itemStack, buyPrice, sellPrice, shopType));
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_CREATE));
}
} }
/** /**
@ -330,6 +343,10 @@ public class Commands extends BukkitCommand {
* @param p The command executor * @param p The command executor
*/ */
private void remove(Player p) { private void remove(Player p) {
ShopPreRemoveEvent event = new ShopPreRemoveEvent(p);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return;
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_REMOVE)); p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_REMOVE));
ClickType.setPlayerClickType(p, new ClickType(EnumClickType.REMOVE)); ClickType.setPlayerClickType(p, new ClickType(EnumClickType.REMOVE));
} }
@ -339,6 +356,10 @@ public class Commands extends BukkitCommand {
* @param p The command executor * @param p The command executor
*/ */
private void info(Player p) { private void info(Player p) {
ShopPreInfoEvent event = new ShopPreInfoEvent(p);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return;
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_INFO)); p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_INFO));
ClickType.setPlayerClickType(p, new ClickType(EnumClickType.INFO)); ClickType.setPlayerClickType(p, new ClickType(EnumClickType.INFO));
} }

View File

@ -0,0 +1,46 @@
package de.epiceric.shopchest.event;
import de.epiceric.shopchest.shop.Shop;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
public class ShopBuySellEvent extends ShopEvent implements Cancellable {
private Player player;
private Shop shop;
private Type type;
private boolean cancelled;
public ShopBuySellEvent(Player player, Shop shop, Type type) {
this.player = player;
this.shop = shop;
}
@Override
public Shop getShop() {
return shop;
}
public Type getType() {
return type;
}
@Override
public Player getPlayer() {
return player;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
cancelled = b;
}
public enum Type {
BUY,
SELL;
}
}

View File

@ -0,0 +1,43 @@
package de.epiceric.shopchest.event;
import de.epiceric.shopchest.shop.Shop;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
public class ShopCreateEvent extends ShopEvent implements Cancellable{
private Player player;
private Shop shop;
private double creationPrice;
private boolean cancelled;
public ShopCreateEvent(Player player, Shop shop, double creationPrice) {
this.player = player;
this.shop = shop;
this.creationPrice = creationPrice;
}
@Override
public Shop getShop() {
return shop;
}
@Override
public Player getPlayer() {
return player;
}
public double getCreationPrice() {
return creationPrice;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
cancelled = b;
}
}

View File

@ -0,0 +1,21 @@
package de.epiceric.shopchest.event;
import de.epiceric.shopchest.shop.Shop;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public abstract class ShopEvent extends Event {
private static final HandlerList handlers = new HandlerList();
public abstract Shop getShop();
public abstract Player getPlayer();
@Override
public HandlerList getHandlers() {
return handlers;
}
}

View File

@ -0,0 +1,36 @@
package de.epiceric.shopchest.event;
import de.epiceric.shopchest.shop.Shop;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
public class ShopInfoEvent extends ShopEvent implements Cancellable {
private Player player;
private Shop shop;
private boolean cancelled;
public ShopInfoEvent(Player player, Shop shop) {
this.player = player;
this.shop = shop;
}
@Override
public Shop getShop() {
return shop;
}
@Override
public Player getPlayer() {
return player;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
cancelled = b;
}
}

View File

@ -0,0 +1,35 @@
package de.epiceric.shopchest.event;
import de.epiceric.shopchest.shop.Shop;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
public class ShopPreCreateEvent extends ShopEvent implements Cancellable {
private Player player;
private Shop shop;
private boolean cancelled;
public ShopPreCreateEvent(Player player, Shop shop) {
this.player = player;
this.shop = shop;
}
public Player getPlayer() {
return player;
}
@Override
public Shop getShop() {
return shop;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
cancelled = b;
}
}

View File

@ -0,0 +1,32 @@
package de.epiceric.shopchest.event;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class ShopPreInfoEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private Player player;
private boolean cancelled;
public ShopPreInfoEvent(Player player) {
this.player = player;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
}

View File

@ -0,0 +1,36 @@
package de.epiceric.shopchest.event;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class ShopPreRemoveEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private Player player;
private boolean cancelled;
public ShopPreRemoveEvent(Player player) {
this.player = player;
}
public Player getPlayer() {
return player;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
}

View File

@ -0,0 +1,36 @@
package de.epiceric.shopchest.event;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class ShopReloadEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private Player player;
private boolean cancelled;
public ShopReloadEvent(Player player) {
this.player = player;
}
public Player getPlayer() {
return player;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
cancelled = b;
}
}

View File

@ -0,0 +1,36 @@
package de.epiceric.shopchest.event;
import de.epiceric.shopchest.shop.Shop;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
public class ShopRemoveEvent extends ShopEvent implements Cancellable {
private Player player;
private Shop shop;
private boolean cancelled;
public ShopRemoveEvent(Player player, Shop shop) {
this.player = player;
this.shop = shop;
}
@Override
public Shop getShop() {
return shop;
}
@Override
public Player getPlayer() {
return player;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
cancelled = b;
}
}

View File

@ -5,6 +5,10 @@ import com.griefcraft.model.Protection;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.config.Regex; import de.epiceric.shopchest.config.Regex;
import de.epiceric.shopchest.event.ShopBuySellEvent;
import de.epiceric.shopchest.event.ShopCreateEvent;
import de.epiceric.shopchest.event.ShopInfoEvent;
import de.epiceric.shopchest.event.ShopRemoveEvent;
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.shop.Shop; import de.epiceric.shopchest.shop.Shop;
@ -248,6 +252,12 @@ public class ShopInteractListener implements Listener {
} }
double creationPrice = (shopType == ShopType.NORMAL) ? Config.shop_creation_price_normal : Config.shop_creation_price_admin; double creationPrice = (shopType == ShopType.NORMAL) ? Config.shop_creation_price_normal : Config.shop_creation_price_admin;
ShopCreateEvent event = new ShopCreateEvent(executor, Shop.createImaginaryShop(executor, product, buyPrice, sellPrice,shopType), creationPrice);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return;
EconomyResponse r = plugin.getEconomy().withdrawPlayer(executor, creationPrice); EconomyResponse r = plugin.getEconomy().withdrawPlayer(executor, creationPrice);
if (!r.transactionSuccess()) { if (!r.transactionSuccess()) {
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r.errorMessage))); executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r.errorMessage)));
@ -271,6 +281,10 @@ public class ShopInteractListener implements Listener {
* @param shop Shop to be removed * @param shop Shop to be removed
*/ */
private void remove(Player executor, Shop shop) { private void remove(Player executor, Shop shop) {
ShopRemoveEvent event = new ShopRemoveEvent(executor, shop);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return;
ShopUtils.removeShop(shop, true); ShopUtils.removeShop(shop, true);
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_REMOVED)); executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_REMOVED));
} }
@ -281,6 +295,10 @@ public class ShopInteractListener implements Listener {
* @param shop Shop from which the information will be retrieved * @param shop Shop from which the information will be retrieved
*/ */
private void info(Player executor, Shop shop) { private void info(Player executor, Shop shop) {
ShopInfoEvent event = new ShopInfoEvent(executor, shop);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return;
Chest c = (Chest) shop.getLocation().getBlock().getState(); Chest c = (Chest) shop.getLocation().getBlock().getState();
int amount = Utils.getAmount(c.getInventory(), shop.getProduct()); int amount = Utils.getAmount(c.getInventory(), shop.getProduct());
@ -402,6 +420,15 @@ public class ShopInteractListener implements Listener {
if (r.transactionSuccess()) { if (r.transactionSuccess()) {
if (r2 != null) { if (r2 != null) {
if (r2.transactionSuccess()) { if (r2.transactionSuccess()) {
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.BUY);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
econ.depositPlayer(executor, shop.getBuyPrice());
econ.withdrawPlayer(shop.getVendor(), shop.getBuyPrice());
return;
}
addToInventory(inventory, product); addToInventory(inventory, product);
removeFromInventory(c.getInventory(), product); removeFromInventory(c.getInventory(), product);
executor.updateInventory(); executor.updateInventory();
@ -419,6 +446,14 @@ public class ShopInteractListener implements Listener {
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r2.errorMessage))); executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r2.errorMessage)));
} }
} else { } else {
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.BUY);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
econ.depositPlayer(executor, shop.getBuyPrice());
return;
}
addToInventory(inventory, product); addToInventory(inventory, product);
executor.updateInventory(); executor.updateInventory();
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_SUCESS_ADMIN, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())), executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_SUCESS_ADMIN, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())),
@ -476,6 +511,15 @@ public class ShopInteractListener implements Listener {
if (r.transactionSuccess()) { if (r.transactionSuccess()) {
if (r2 != null) { if (r2 != null) {
if (r2.transactionSuccess()) { if (r2.transactionSuccess()) {
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.SELL);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
econ.withdrawPlayer(executor, shop.getBuyPrice());
econ.depositPlayer(shop.getVendor(), shop.getBuyPrice());
return;
}
addToInventory(inventory, product); addToInventory(inventory, product);
removeFromInventory(executor.getInventory(), product); removeFromInventory(executor.getInventory(), product);
executor.updateInventory(); executor.updateInventory();
@ -494,6 +538,14 @@ public class ShopInteractListener implements Listener {
} }
} else { } else {
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.SELL);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
econ.withdrawPlayer(executor, shop.getBuyPrice());
return;
}
removeFromInventory(executor.getInventory(), product); removeFromInventory(executor.getInventory(), product);
executor.updateInventory(); executor.updateInventory();
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELL_SUCESS_ADMIN, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())), executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELL_SUCESS_ADMIN, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())),

View File

@ -63,6 +63,16 @@ public class Shop {
if (item == null || item.isDead()) createItem(); if (item == null || item.isDead()) createItem();
} }
private Shop(OfflinePlayer vendor, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) {
this.id = 0;
this.vendor = vendor;
this.product = product;
this.location = null;
this.buyPrice = buyPrice;
this.sellPrice = sellPrice;
this.shopType = shopType;
}
/** /**
* Creates the hologram of the shop if it doesn't exist * Creates the hologram of the shop if it doesn't exist
*/ */
@ -262,6 +272,13 @@ public class Shop {
return chest; return chest;
} }
/**
* @return A shop, which is not really a shop. It's just for "storing" the data (used in some events).
*/
public static Shop createImaginaryShop(OfflinePlayer vendor, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) {
return new Shop(vendor, product, buyPrice, sellPrice, shopType);
}
public enum ShopType { public enum ShopType {
NORMAL, NORMAL,
ADMIN ADMIN