Skip to content

Commit

Permalink
Handle drag events
Browse files Browse the repository at this point in the history
  • Loading branch information
stefvanschie committed Jun 30, 2020
1 parent 9c8c36f commit 8d3478e
Showing 1 changed file with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.*;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

/**
* Listens to events for {@link Gui}s. Only one instance of this class gets constructed.
Expand Down Expand Up @@ -76,6 +71,43 @@ public void onInventoryClick(@NotNull InventoryClickEvent event) {
}
}

/**
* Handles small drag events which are likely clicks instead. These small drags will be interpreted as clicks and
* will fire a click event.
*
* @param event the event fired
* @since 0.6.1
*/
@EventHandler
public void onInventoryDrag(@NotNull InventoryDragEvent event) {
if (!(event.getInventory().getHolder() instanceof Gui)) {
return;
}

Set<Integer> inventorySlots = event.getInventorySlots();

if (inventorySlots.size() > 1) {
return;
}

InventoryView view = event.getView();
int index = inventorySlots.toArray(new Integer[0])[0];
InventoryType.SlotType slotType = view.getSlotType(index);

boolean even = event.getType() == DragType.EVEN;

ClickType clickType = even ? ClickType.LEFT : ClickType.RIGHT;
InventoryAction inventoryAction = even ? InventoryAction.PLACE_SOME : InventoryAction.PLACE_ONE;

//this is a fake click event, firing this may cause other plugins to function incorrectly, so keep it local
InventoryClickEvent inventoryClickEvent = new InventoryClickEvent(view, slotType, index, clickType,
inventoryAction);

onInventoryClick(inventoryClickEvent);

event.setCancelled(inventoryClickEvent.isCancelled());
}

/**
* Handles closing in inventories
*
Expand Down

0 comments on commit 8d3478e

Please sign in to comment.