001 package org.bukkit.event.inventory; 002 003 import org.bukkit.entity.HumanEntity; 004 import org.bukkit.event.Cancellable; 005 import org.bukkit.event.Event.Result; 006 import org.bukkit.inventory.InventoryView; 007 import org.bukkit.inventory.ItemStack; 008 009 /** 010 * An abstract base class for events that describe an interaction between a 011 * HumanEntity and the contents of an Inventory. 012 */ 013 public abstract class InventoryInteractEvent extends InventoryEvent implements Cancellable { 014 private Result result = Result.DEFAULT; 015 016 public InventoryInteractEvent(InventoryView transaction) { 017 super(transaction); 018 } 019 020 /** 021 * Gets the player who performed the click. 022 * 023 * @return The clicking player. 024 */ 025 public HumanEntity getWhoClicked() { 026 return getView().getPlayer(); 027 } 028 029 /** 030 * Sets the result of this event. This will change whether or not this 031 * event is considered cancelled. 032 * 033 * @see #isCancelled() 034 * @param newResult the new {@link Result} for this event 035 */ 036 public void setResult(Result newResult) { 037 result = newResult; 038 } 039 040 /** 041 * Gets the {@link Result} of this event. The Result describes the 042 * behavior that will be applied to the inventory in relation to this 043 * event. 044 * 045 * @return the Result of this event. 046 */ 047 public Result getResult() { 048 return result; 049 } 050 051 /** 052 * Gets whether or not this event is cancelled. This is based off of the 053 * Result value returned by {@link #getResult()}. Result.ALLOW and 054 * Result.DEFAULT will result in a returned value of false, but 055 * Result.DENY will result in a returned value of true. 056 * <p> 057 * {@inheritDoc} 058 * 059 * @return whether the event is cancelled 060 */ 061 public boolean isCancelled() { 062 return getResult() == Result.DENY; 063 } 064 065 /** 066 * Proxy method to {@link #setResult(Event.Result)} for the Cancellable 067 * interface. {@link #setResult(Event.Result)} is preferred, as it allows 068 * you to specify the Result beyond Result.DENY and Result.ALLOW. 069 * <p> 070 * {@inheritDoc} 071 * 072 * @param toCancel result becomes DENY if true, ALLOW if false 073 */ 074 public void setCancelled(boolean toCancel) { 075 setResult(toCancel ? Result.DENY : Result.ALLOW); 076 } 077 078 }