Improved the GUI API.

* BUG: Fixed players unable to shift-click an item from the player inventory to the GUI.
* NEW: Added a direct access to the InventoryAction in the methods.
* OPT: Code style, avoiding too many nested blocs.
* OPT: Raw types in the class attributes (meh @IamBlueSlime).
This commit is contained in:
Amaury Carrade 2015-07-10 21:44:45 +02:00
parent 9084d28401
commit c738359d2e
4 changed files with 47 additions and 25 deletions

View File

@ -40,7 +40,12 @@ public abstract class AbstractGui {
public void onClose(Player player) {}
public void onClick(Player player, ItemStack stack, String action, ClickType clickType, InventoryClickEvent event)
public void onClick(Player player, ItemStack stack, String action, ClickType clickType, InventoryAction invAction, InventoryClickEvent event)
{
this.onClick(player, stack, action, clickType, invAction);
}
public void onClick(Player player, ItemStack stack, String action, ClickType clickType, InventoryAction invAction)
{
this.onClick(player, stack, action, clickType);
}
@ -53,7 +58,12 @@ public abstract class AbstractGui {
public void onClick(Player player, ItemStack stack, String action) {}
public void onItemDeposit(Player player, ItemStack stack, ClickType clickType, InventoryClickEvent event)
public void onItemDeposit(Player player, ItemStack stack, ClickType clickType, InventoryAction invAction, InventoryClickEvent event)
{
onItemDeposit(player, stack, clickType, invAction);
}
public void onItemDeposit(Player player, ItemStack stack, ClickType clickType, InventoryAction invAction)
{
onItemDeposit(player, stack, clickType);
}

View File

@ -46,30 +46,41 @@ public class GuiListener implements Listener {
Player player = (Player) event.getWhoClicked();
AbstractGui gui = GuiManager.getPlayerGui(player);
if (gui != null)
if(gui == null)
return;
if (event.getInventory() instanceof PlayerInventory)
return;
/* *** Click from player inventory (with shift) *** */
if(event.getRawSlot() != event.getSlot())
{
if (event.getInventory() instanceof PlayerInventory)
return;
if(event.getRawSlot() == event.getSlot()) // Chest inventory, not player one
if(event.isShiftClick())
{
if(event.getCursor() != null && event.getCursor().getType() != Material.AIR)
{
gui.onItemDeposit(player, event.getCursor(), event.getClick(), event);
}
else
{
String action = gui.getAction(event.getSlot());
if (action != null)
gui.onClick(player, event.getCurrentItem(), action, event.getClick(), event);
}
event.setCancelled(true);
gui.onItemDeposit(player, event.getCurrentItem(), event.getClick(), event.getAction(), event);
}
return;
}
/* *** Click on the “chest” *** */
if(event.getCursor() != null && event.getCursor().getType() != Material.AIR)
{
gui.onItemDeposit(player, event.getCursor(), event.getClick(), event.getAction(), event);
}
else
{
String action = gui.getAction(event.getSlot());
if (action != null)
gui.onClick(player, event.getCurrentItem(), action, event.getClick(), event.getAction(), event);
}
event.setCancelled(true);
}
}

View File

@ -31,7 +31,8 @@ import java.util.concurrent.*;
* Changes by Amaury Carrade to use statics (beh, code style, these things).
*/
public class GuiManager {
protected static ConcurrentHashMap<UUID, AbstractGui> currentGUIs;
protected static Map<UUID, AbstractGui> currentGUIs;
public static void init(ImageOnMap plugin)
{
@ -72,7 +73,7 @@ public class GuiManager {
return null;
}
public static ConcurrentHashMap<UUID, AbstractGui> getPlayersGui()
public static Map<UUID, AbstractGui> getPlayersGui()
{
return currentGUIs;
}

View File

@ -189,7 +189,7 @@ public class MapListGui extends AbstractGui {
}
@Override
public void onItemDeposit(Player player, ItemStack stack, ClickType clickType, InventoryClickEvent ev) {
public void onItemDeposit(Player player, ItemStack stack, ClickType clickType, InventoryAction invAction, InventoryClickEvent ev) {
ev.setCancelled(true);
if (stack.getType() == Material.MAP && MapManager.managesMap(stack))