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

Add InventoryDragEvent #5474

Merged
merged 6 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@
import org.bukkit.event.hanging.HangingPlaceEvent;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.DragType;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
Expand Down Expand Up @@ -145,13 +147,17 @@
import org.bukkit.event.world.WorldEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.Recipe;
import org.bukkit.potion.PotionEffectType;
import org.eclipse.jdt.annotation.Nullable;

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

/**
* @author Peter Güttinger
Expand Down Expand Up @@ -1087,6 +1093,75 @@ public Inventory get(final InventoryClickEvent e) {
return e.getClickedInventory();
}
}, 0);
// InventoryDragEvent
EventValues.registerEventValue(InventoryDragEvent.class, Player.class, new Getter<Player, InventoryDragEvent>() {
@Override
public @Nullable Player get(InventoryDragEvent event) {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
return event.getWhoClicked() instanceof Player ? (Player) event.getWhoClicked() : null;
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(InventoryDragEvent.class, World.class, new Getter<World, InventoryDragEvent>() {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
@Override
public @Nullable World get(InventoryDragEvent event) {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
return event.getWhoClicked().getWorld();
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(InventoryDragEvent.class, ItemStack.class, new Getter<ItemStack, InventoryDragEvent>() {
@Override
public @Nullable ItemStack get(InventoryDragEvent event) {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
return event.getOldCursor();
}
}, EventValues.TIME_PAST);
EventValues.registerEventValue(InventoryDragEvent.class, ItemStack.class, new Getter<ItemStack, InventoryDragEvent>() {
@Override
public @Nullable ItemStack get(InventoryDragEvent event) {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
return event.getCursor();
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(InventoryDragEvent.class, ItemStack[].class, new Getter<ItemStack[], InventoryDragEvent>() {
Copy link
Member

@AyhamAl-Ali AyhamAl-Ali Mar 23, 2023

Choose a reason for hiding this comment

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

Is there a use for getOldCursor() here? Could it be the past timestate of event-itemstack?

@Override
public @Nullable ItemStack[] get(InventoryDragEvent event) {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
return event.getNewItems().values().toArray(new ItemStack[0]);
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(InventoryDragEvent.class, Slot[].class, new Getter<Slot[], InventoryDragEvent>() {
@Override
public @Nullable Slot[] get(InventoryDragEvent event) {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
List<Slot> slots = new ArrayList<>(event.getRawSlots().size());
InventoryView view = event.getView();
for (Integer rawSlot : event.getRawSlots()) {
Inventory inventory = view.getInventory(rawSlot);
int slot = view.convertSlot(rawSlot);
if (inventory == null)
continue;
// Not all indices point to inventory slots. Equipment, for example
if (inventory instanceof PlayerInventory && slot >= 36) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add notes why using the inventory size (36) for future contributions

slots.add(new ch.njol.skript.util.slot.EquipmentSlot(((PlayerInventory) view.getBottomInventory()).getHolder(), slot));
} else {
slots.add(new InventorySlot(inventory, view.convertSlot(rawSlot)));
}
}
return slots.toArray(new Slot[0]);
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(InventoryDragEvent.class, ClickType.class, new Getter<ClickType, InventoryDragEvent>() {
@Override
public @Nullable ClickType get(InventoryDragEvent event) {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
return event.getType() == DragType.EVEN ? ClickType.LEFT : ClickType.RIGHT;
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(InventoryDragEvent.class, Inventory[].class, new Getter<Inventory[], InventoryDragEvent>() {
@Override
@Nullable
public Inventory[] get(InventoryDragEvent event) {
Set<Inventory> inventories = new HashSet<>();
InventoryView view = event.getView();
for (Integer rawSlot : event.getRawSlots()) {
inventories.add(view.getInventory(rawSlot));
}
return inventories.toArray(new Inventory[0]);
}
}, EventValues.TIME_NOW);
// PrepareAnvilEvent
EventValues.registerEventValue(PrepareAnvilEvent.class, ItemStack.class, new Getter<ItemStack, PrepareAnvilEvent>() {
@Override
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.bukkit.event.inventory.FurnaceBurnEvent;
import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
Expand Down Expand Up @@ -670,7 +671,7 @@ public class SimpleEvents {
"Use <a href='./expressions.html#ExprChatFormat'>chat format</a> to change message format.",
"Use <a href='./expressions.html#ExprChatRecipients'>chat recipients</a> to edit chat recipients."
)
.examples(
.examples(
"on chat:",
"\tif player has permission \"owner\":",
"\t\tset chat format to \"&lt;red&gt;[player]&lt;light gray&gt;: &lt;light red&gt;[message]\"",
Expand All @@ -679,7 +680,7 @@ public class SimpleEvents {
"\telse: #default message format",
"\t\tset chat format to \"&lt;orange&gt;[player]&lt;light gray&gt;: &lt;white&gt;[message]\""
)
.since("1.4.1");
.since("1.4.1");
if (Skript.classExists("org.bukkit.event.world.LootGenerateEvent")) {
Skript.registerEvent("Loot Generate", SimpleEvent.class, LootGenerateEvent.class, "loot generat(e|ing)")
.description(
Expand Down Expand Up @@ -709,6 +710,16 @@ public class SimpleEvents {
.requiredPlugins("Paper 1.16+");
}

Skript.registerEvent("Inventory Drag", SimpleEvent.class, InventoryDragEvent.class, "inventory drag[ging]")
.description("Called when a player drags an item in their cursor across the inventory.")
.examples(
"on inventory drag:",
"\tif player's current inventory is {_gui}:",
"\t\tsend \"You can't drag your items here!\" to player",
"\t\tcancel event"
)
.since("INSERT VERSION");

}

}