Skip to content

Commit

Permalink
Fix eventhandler, fixes #22 and fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
dentych committed Jan 27, 2016
1 parent e020b63 commit 4a8bcbf
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/me/tychsen/enchantgui/Event/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

import java.util.logging.Logger;

public class EventManager implements Listener, CommandExecutor {
private MenuSystem system;

Expand All @@ -26,13 +29,29 @@ public EventManager(MenuSystem system) {

@EventHandler
public void onInventoryClickEvent(InventoryClickEvent e) {
if (e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR)
String inventoryName = e.getInventory().getName().toLowerCase();
String configInventoryName = EshopConfig.getInstance().getMenuName().toLowerCase();
boolean correctEvent = inventoryName.startsWith(configInventoryName);

if (!correctEvent) {
return;
try {
handleInventoryClickEvent(e);
} catch (Exception exc) {
// TODO: Fix this shit with main getinstance.. it's ugly.
Main.getInstance().getLogger().severe("EXCEPTION: " + exc.getMessage() + "\n" + exc.getStackTrace()[0].toString() + "\n\n");
} else {
e.setCancelled(true);
if (!(e.getWhoClicked() instanceof Player)) return;
try {
handleInventoryClickEvent(e);
} catch (Exception ex) {
Logger logger = Main.getInstance().getLogger();
logger.severe("EXCEPTION: " + ex.getMessage());
for (StackTraceElement element : ex.getStackTrace()) {
final String exceptionMsg =
"Exception thrown from " + element.getMethodName() +
" in class " + element.getClassName() +
" [on line number " + element.getLineNumber() +
" of file " + element.getFileName() + "]";
logger.severe(exceptionMsg);
}
}
}
}

Expand All @@ -48,17 +67,11 @@ public void onPlayerInteractEvent(PlayerInteractEvent e) {
}

private void handleInventoryClickEvent(InventoryClickEvent e) {
boolean correctEvent = e.getInventory().getName().toLowerCase().startsWith(EshopConfig.getInstance().getMenuName().toLowerCase());
Player p;

if (correctEvent && e.getWhoClicked() instanceof Player) {
e.setCancelled(true); // Cancel whatever default action might occur
p = (Player) e.getWhoClicked();
if (e.getCurrentItem().getType() == Material.AIR) return;
if (e.getClickedInventory().getType() != InventoryType.CHEST) return;

system.handleMenuClick(p, e);
} else {
return;
}
Player p = (Player) e.getWhoClicked();
system.handleMenuClick(p, e);
}

private void handlePlayerInteractEvent(PlayerInteractEvent e) {
Expand Down Expand Up @@ -89,8 +102,7 @@ private boolean handleCommand(CommandSender sender, Command cmd, String[] args)
if (sender instanceof Player) {
Player p = (Player) sender;
system.showMainMenu(p);
}
else {
} else {
sender.sendMessage("Can't use this command from console...");
}
}
Expand Down

0 comments on commit 4a8bcbf

Please sign in to comment.