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

Make ItemInfoEvent cancellable (to cancel /iteminfo command) #349

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
23 changes: 1 addition & 22 deletions src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,9 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
item = parseEvent.getItem();
}

if (MaterialUtil.isEmpty(item)) {
return false;
}

iteminfo.send(sender);
try {
iteminfo_fullname.send(sender, "item", MaterialUtil.getName(item));
} catch (IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + "Error while generating full name. Please contact an admin or take a look at the console/log!");
ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating full item name", e);
return true;
}

try {
iteminfo_shopname.send(sender, "item", MaterialUtil.getSignName(item));
} catch (IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + "Error while generating shop sign name. Please contact an admin or take a look at the console/log!");
ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating shop sign item name", e);
return true;
}

ItemInfoEvent event = new ItemInfoEvent(sender, item);
ChestShop.callEvent(event);

return true;
return !MaterialUtil.isEmpty(item);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure any other event listener doesn't throw an error when the item is null/empty? (As you moved the check from before calling the event to after it and don't check that again anywhere)

}
}
15 changes: 14 additions & 1 deletion src/main/java/com/Acrobot/ChestShop/Events/ItemInfoEvent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.Acrobot.ChestShop.Events;

import org.bukkit.command.CommandSender;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
Expand All @@ -10,15 +11,17 @@
*
* @author Acrobot
*/
public class ItemInfoEvent extends Event {
public class ItemInfoEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();

private CommandSender sender;
private ItemStack item;
private boolean cancelled;

public ItemInfoEvent(CommandSender sender, ItemStack item) {
this.sender = sender;
this.item = item;
this.cancelled = false;
}

/**
Expand All @@ -42,4 +45,14 @@ public HandlerList getHandlers() {
public static HandlerList getHandlerList() {
return handlers;
}

@Override
public boolean isCancelled () {
return cancelled;
}

@Override
public void setCancelled (boolean b) {
cancelled = b;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,36 @@
*/
public class ItemInfoListener implements Listener {

@EventHandler
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public static void messageHandler(ItemInfoEvent event) {
CommandSender sender = event.getSender();
ItemStack item = event.getItem();
iteminfo.send(sender);
try {
iteminfo_fullname.send(sender, "item", MaterialUtil.getName(item));
} catch (IllegalArgumentException e) {
event.getSender().sendMessage(ChatColor.RED + "Error while generating full name. Please contact an admin or take a look at the console/log!");
ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating full item name", e);
return;
}

try {
iteminfo_shopname.send(sender, "item", MaterialUtil.getSignName(item));
} catch (IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + "Error while generating shop sign name. Please contact an admin or take a look at the console/log!");
ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating shop sign item name", e);
}
}

@EventHandler(ignoreCancelled = true)
public static void addRepairCost(ItemInfoEvent event) {
ItemMeta meta = event.getItem().getItemMeta();
if (meta instanceof Repairable && ((Repairable) meta).getRepairCost() > 0) {
iteminfo_repaircost.send(event.getSender(), "cost", String.valueOf(((Repairable) meta).getRepairCost()));
}
}

@EventHandler
@EventHandler(ignoreCancelled = true)
public static void addEnchantment(ItemInfoEvent event) {
ItemStack item = event.getItem();
ItemMeta meta = item.getItemMeta();
Expand All @@ -56,7 +77,7 @@ public static void addEnchantment(ItemInfoEvent event) {
}
}

@EventHandler
@EventHandler(ignoreCancelled = true)
public static void addPotionInfo(ItemInfoEvent event) {
ItemStack item = event.getItem();

Expand Down Expand Up @@ -97,7 +118,7 @@ public static void addPotionInfo(ItemInfoEvent event) {
}
}

@EventHandler
@EventHandler(ignoreCancelled = true)
public static void addBookInfo(ItemInfoEvent event) {
ItemMeta meta = event.getItem().getItemMeta();
if (meta instanceof BookMeta) {
Expand All @@ -115,7 +136,7 @@ public static void addBookInfo(ItemInfoEvent event) {
}
}

@EventHandler
@EventHandler(ignoreCancelled = true)
public static void addLoreInfo(ItemInfoEvent event) {
ItemMeta meta = event.getItem().getItemMeta();
if (meta.hasLore()) {
Expand Down