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

Adds InventoryMoveItemEvent #5462

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
21 changes: 21 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 @@ -104,6 +104,7 @@
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
Expand Down Expand Up @@ -1562,5 +1563,25 @@ public Location get(LootGenerateEvent event) {
}
}, EventValues.TIME_NOW);
}

// InventoryItemMoveEvent - does not extend InventoryEvent
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
EventValues.registerEventValue(InventoryMoveItemEvent.class, Inventory.class, new Getter<Inventory, InventoryMoveItemEvent>() {
@Override
public Inventory get(InventoryMoveItemEvent event) {
return event.getSource();
}
}, EventValues.TIME_PAST);
EventValues.registerEventValue(InventoryMoveItemEvent.class, Inventory.class, new Getter<Inventory, InventoryMoveItemEvent>() {
@Override
public Inventory get(InventoryMoveItemEvent event) {
return event.getDestination();
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(InventoryMoveItemEvent.class, ItemStack.class, new Getter<ItemStack, InventoryMoveItemEvent>() {
@Override
public ItemStack get(InventoryMoveItemEvent event) {
return event.getItem();
}
}, EventValues.TIME_NOW);
}
}
19 changes: 17 additions & 2 deletions src/main/java/ch/njol/skript/events/EvtItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package ch.njol.skript.events;

import ch.njol.skript.lang.util.SimpleEvent;
import ch.njol.skript.sections.EffSecSpawn;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockDispenseEvent;
Expand All @@ -28,6 +29,7 @@
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
Expand Down Expand Up @@ -127,6 +129,17 @@ public class EvtItem extends SkriptEvent {
.examples("on item merge of gold blocks:",
" cancel event")
.since("2.2-dev35");
Skript.registerEvent("Inventory Item Move", SimpleEvent.class, InventoryMoveItemEvent.class, "inventory item (move|transport)")
.description(
"Called when an entity or block (e.g. hopper) tries to move items directly from one inventory to another.",
"When this event is called, the initiator may have already removed the item from the source inventory and is ready to move it into the destination inventory.",
"If this event is cancelled, the items will be returned to the source inventory."
)
.examples(
"on inventory item move:",
"\tbroadcast \"%holder of past event-inventory% is transporting %event-item% to %holder of event-inventory%!\""
)
.since("INSERT VERSION");
}

@Nullable
Expand Down Expand Up @@ -184,6 +197,8 @@ public boolean check(final Event event) {
is = ((ItemDespawnEvent) event).getEntity().getItemStack();
} else if (event instanceof ItemMergeEvent) {
is = ((ItemMergeEvent) event).getTarget().getItemStack();
} else if (event instanceof InventoryMoveItemEvent) {
is = ((InventoryMoveItemEvent) event).getItem();
} else {
assert false;
return false;
Expand All @@ -201,8 +216,8 @@ public boolean check(final ItemType t) {
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return "dispense/spawn/drop/craft/pickup/consume/break/despawn/merge" + (types == null ? "" : " of " + types);
public String toString(@Nullable Event event, boolean debug) {
return "dispense/spawn/drop/craft/pickup/consume/break/despawn/merge/move item" + (types == null ? "" : " of " + types);
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
}

}
69 changes: 69 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprEvtInitiator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;

import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.inventory.Inventory;

@Name("Initiator Inventory")
@Description("Returns the initiator inventory in an on <a href=\"./events.html?search=#inventory_item_move\">inventory item move</a> event.")
@Examples({
"on inventory item move:",
"\tif holder of event-initiator-inventory is a chest:",
"broadcast \"Item transport requested at %location at holder of event-initiator-inventory%...\""
})
@Events("Inventory Item Move")
@Since("INSERT VERSION")
public class ExprEvtInitiator extends EventValueExpression<Inventory> {

public ExprEvtInitiator() {
super(Inventory.class);
}

static {
Skript.registerExpression(ExprEvtInitiator.class, Inventory.class, ExpressionType.SIMPLE, "[the] [event-]initiator[( |-)inventory]");
}
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) {
if (!getParser().isCurrentEvent(InventoryMoveItemEvent.class)) {
Skript.error("'event-initiator' can only be used in 'inventory item move' events.");
return false;
}
return super.init(exprs, matchedPattern, isDelayed, parser);
}

@Override
public String toString() {
return "the event-initiator-inventory in 'inventory item move' event";
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
}

}