-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paper only
player changes framed <item>
event (#2615)
* `player changes framed <item>` event * Slightly update meta * Use `frame` variable instead of getting it from `event`. * Make fields `public` and store `ItemTag` as a field as well. * Update field * Update `ElementTag` constructor and de-cache `item` for `getContext` * Fix typo
- Loading branch information
1 parent
a063ef4
commit 9325f13
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
.../main/java/com/denizenscript/denizen/paper/events/PlayerChangesFramedItemScriptEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.denizenscript.denizen.paper.events; | ||
|
||
import com.denizenscript.denizen.events.BukkitScriptEvent; | ||
import com.denizenscript.denizen.objects.EntityTag; | ||
import com.denizenscript.denizen.objects.ItemTag; | ||
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; | ||
import com.denizenscript.denizencore.objects.ObjectTag; | ||
import com.denizenscript.denizencore.objects.core.ElementTag; | ||
import com.denizenscript.denizencore.scripts.ScriptEntryData; | ||
import io.papermc.paper.event.player.PlayerItemFrameChangeEvent; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
|
||
public class PlayerChangesFramedItemScriptEvent extends BukkitScriptEvent implements Listener { | ||
|
||
// <--[event] | ||
// @Events | ||
// player changes framed <item> | ||
// | ||
// @Location true | ||
// | ||
// @Plugin Paper | ||
// | ||
// @Group Paper | ||
// | ||
// @Cancellable true | ||
// | ||
// @Triggers when a player interacts with an item frame by adding, removing, or rotating the item held in it. | ||
// | ||
// @Switch frame:<entity> to only process the event if the item frame entity being matches the input. | ||
// @Switch action:<action> to only process the event if the change matches the input. | ||
// | ||
// @Context | ||
// <context.frame> returns the EntityTag of the item frame. | ||
// <context.item> returns the ItemTag of the item held in the item frame. | ||
// <context.action> returns the ElementTag of the action being performed, based on <@link url https://jd.papermc.io/paper/1.20/io/papermc/paper/event/player/PlayerItemFrameChangeEvent.ItemFrameChangeAction.html> | ||
// | ||
// @Determine | ||
// "ITEM:<ItemTag>" to change the item held by the item frame. If there is an item already in the frame, it will be replaced. To remove the item, set it to air. | ||
// | ||
// @Player Always. | ||
// --> | ||
|
||
public PlayerChangesFramedItemScriptEvent() { | ||
registerCouldMatcher("player changes framed <item>"); | ||
registerSwitches("frame", "action"); | ||
this.<PlayerChangesFramedItemScriptEvent, ItemTag>registerDetermination("item", ItemTag.class, (evt, context, item) -> { | ||
evt.event.setItemStack(item.getItemStack()); | ||
}); | ||
} | ||
|
||
public ItemTag item; | ||
public EntityTag frame; | ||
public ElementTag action; | ||
public PlayerItemFrameChangeEvent event; | ||
|
||
@Override | ||
public boolean matches(ScriptPath path) { | ||
if (!path.tryArgObject(3, item)) { | ||
return false; | ||
} | ||
if (!path.tryObjectSwitch("frame", frame)) { | ||
return false; | ||
} | ||
if (!path.tryObjectSwitch("action", action)) { | ||
return false; | ||
} | ||
if (!runInCheck(path, frame.getLocation())) { | ||
return false; | ||
} | ||
return super.matches(path); | ||
} | ||
|
||
@Override | ||
public ObjectTag getContext(String name) { | ||
return switch (name) { | ||
case "frame" -> frame; | ||
case "item" -> new ItemTag(event.getItemStack()); | ||
case "action" -> action; | ||
default -> super.getContext(name); | ||
}; | ||
} | ||
|
||
@Override | ||
public ScriptEntryData getScriptEntryData() { | ||
return new BukkitScriptEntryData(event.getPlayer()); | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerChangesFramedItem(PlayerItemFrameChangeEvent event) { | ||
item = new ItemTag(event.getItemStack()); | ||
frame = new EntityTag(event.getItemFrame()); | ||
action = new ElementTag(event.getAction()); | ||
this.event = event; | ||
fire(event); | ||
} | ||
} |