Skip to content

Commit

Permalink
Adds InventoryMoveItemEvent (SkriptLang#5462)
Browse files Browse the repository at this point in the history
  • Loading branch information
NotSoDelayed authored and Moderocky committed Sep 16, 2023
1 parent ca68e1f commit 2100459
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 26 deletions.
46 changes: 22 additions & 24 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@
import java.util.List;
import java.util.Set;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.command.CommandEvent;
import ch.njol.skript.events.bukkit.ScriptEvent;
import ch.njol.skript.events.bukkit.SkriptStartEvent;
import ch.njol.skript.events.bukkit.SkriptStopEvent;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.BlockStateBlock;
import ch.njol.skript.util.BlockUtils;
import ch.njol.skript.util.DelayedChangeBlock;
import ch.njol.skript.util.Direction;
import ch.njol.skript.util.EnchantmentType;
import ch.njol.skript.util.Getter;
import ch.njol.skript.util.slot.InventorySlot;
import ch.njol.skript.util.slot.Slot;
import com.destroystokyo.paper.event.block.AnvilDamagedEvent;
import com.destroystokyo.paper.event.entity.ProjectileCollideEvent;
import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;
import io.papermc.paper.event.entity.EntityMoveEvent;
import io.papermc.paper.event.player.PlayerInventorySlotChangeEvent;
import io.papermc.paper.event.player.PlayerTradeEvent;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.FireworkEffect;
Expand Down Expand Up @@ -143,30 +165,6 @@
import org.bukkit.potion.PotionEffectType;
import org.eclipse.jdt.annotation.Nullable;

import com.destroystokyo.paper.event.block.AnvilDamagedEvent;
import com.destroystokyo.paper.event.entity.ProjectileCollideEvent;
import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.command.CommandEvent;
import ch.njol.skript.events.bukkit.ScriptEvent;
import ch.njol.skript.events.bukkit.SkriptStartEvent;
import ch.njol.skript.events.bukkit.SkriptStopEvent;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.BlockStateBlock;
import ch.njol.skript.util.BlockUtils;
import ch.njol.skript.util.DelayedChangeBlock;
import ch.njol.skript.util.Direction;
import ch.njol.skript.util.EnchantmentType;
import ch.njol.skript.util.Getter;
import ch.njol.skript.util.slot.InventorySlot;
import ch.njol.skript.util.slot.Slot;
import io.papermc.paper.event.entity.EntityMoveEvent;
import io.papermc.paper.event.player.PlayerInventorySlotChangeEvent;
import io.papermc.paper.event.player.PlayerTradeEvent;

/**
* @author Peter Güttinger
*/
Expand Down
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" + (types == null ? "" : " of " + types);
}

}
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> {

static {
Skript.registerExpression(ExprEvtInitiator.class, Inventory.class, ExpressionType.SIMPLE, "[the] [event-]initiator[( |-)inventory]");
}

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

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

@Override
public String toString() {
return "event-initiator-inventory";
}

}

0 comments on commit 2100459

Please sign in to comment.