|
| 1 | +package org.skriptlang.skript.bukkit.misc.events; |
| 2 | + |
| 3 | +import ch.njol.skript.aliases.ItemType; |
| 4 | +import ch.njol.skript.classes.data.DefaultComparators; |
| 5 | +import ch.njol.skript.entity.EntityData; |
| 6 | +import ch.njol.skript.lang.Literal; |
| 7 | +import ch.njol.skript.lang.SkriptEvent; |
| 8 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 9 | +import ch.njol.skript.lang.SyntaxStringBuilder; |
| 10 | +import ch.njol.skript.registrations.EventConverter; |
| 11 | +import ch.njol.skript.registrations.EventValues; |
| 12 | +import ch.njol.skript.util.Patterns; |
| 13 | +import ch.njol.skript.util.slot.InventorySlot; |
| 14 | +import ch.njol.skript.util.slot.Slot; |
| 15 | +import ch.njol.util.coll.CollectionUtils; |
| 16 | +import io.papermc.paper.event.player.PlayerPickBlockEvent; |
| 17 | +import io.papermc.paper.event.player.PlayerPickEntityEvent; |
| 18 | +import io.papermc.paper.event.player.PlayerPickItemEvent; |
| 19 | +import org.bukkit.block.Block; |
| 20 | +import org.bukkit.block.data.BlockData; |
| 21 | +import org.bukkit.entity.Entity; |
| 22 | +import org.bukkit.event.Event; |
| 23 | +import org.jetbrains.annotations.Nullable; |
| 24 | +import org.skriptlang.skript.bukkit.registration.BukkitSyntaxInfos; |
| 25 | +import org.skriptlang.skript.docs.Origin; |
| 26 | +import org.skriptlang.skript.lang.comparator.Relation; |
| 27 | +import org.skriptlang.skript.registration.SyntaxRegistry; |
| 28 | + |
| 29 | +public class EvtPlayerPickItem extends SkriptEvent { |
| 30 | + |
| 31 | + private static final Patterns<PickType> PATTERNS = new Patterns<>(new Object[][]{ |
| 32 | + {"[player] pick[ing] [of] [an|any] item", PickType.ANY}, |
| 33 | + {"[player] pick[ing] [of] [a|any] block", PickType.BLOCK}, |
| 34 | + {"[player] pick[ing] [of] [an|any] entity", PickType.ENTITY}, |
| 35 | + {"[player] pick[ing] [of] %entitydata/itemtype/blockdata%", null} |
| 36 | + }); |
| 37 | + |
| 38 | + public static void register(SyntaxRegistry registry) { |
| 39 | + registry.register(BukkitSyntaxInfos.Event.KEY, BukkitSyntaxInfos.Event.builder(EvtPlayerPickItem.class, "Player Pick Item") |
| 40 | + .supplier(EvtPlayerPickItem::new) |
| 41 | + .addEvents(CollectionUtils.array(PlayerPickBlockEvent.class, PlayerPickEntityEvent.class)) |
| 42 | + .addPatterns(PATTERNS.getPatterns()) |
| 43 | + .addDescription("Called when a player picks an item, block or an entity" + |
| 44 | + " using the pick block key (default middle mouse button).", |
| 45 | + "The past event-slot represents the slot containing the item that will be put into the players hotbar," + |
| 46 | + " or nothing, if the item is not in the inventory.", |
| 47 | + "The event-slot represents the slot in the hotbar where the picked item will be placed.", |
| 48 | + "Both event-slots may be set to new slots.") |
| 49 | + .addExample(""" |
| 50 | + on player picking a diamond block: |
| 51 | + cancel event |
| 52 | + send "You cannot pick diamond blocks!" to the player |
| 53 | + """) |
| 54 | + .addSince("INSERT VERSION") |
| 55 | + .addRequiredPlugin("1.21.5+") |
| 56 | + .build()); |
| 57 | + |
| 58 | + EventValues.registerEventValue(PlayerPickItemEvent.class, Slot.class, new EventConverter<>() { |
| 59 | + @Override |
| 60 | + public void set(PlayerPickItemEvent event, @Nullable Slot slot) { |
| 61 | + if (!(slot instanceof InventorySlot inventorySlot) || inventorySlot.getInventory() != event.getPlayer().getInventory()) |
| 62 | + return; |
| 63 | + event.setSourceSlot(inventorySlot.getIndex()); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public @Nullable Slot convert(PlayerPickItemEvent event) { |
| 68 | + int source = event.getSourceSlot(); |
| 69 | + if (source == -1) |
| 70 | + return null; |
| 71 | + return new InventorySlot(event.getPlayer().getInventory(), source); |
| 72 | + } |
| 73 | + }, EventValues.TIME_PAST); |
| 74 | + EventValues.registerEventValue(PlayerPickItemEvent.class, Slot.class, new EventConverter<>() { |
| 75 | + @Override |
| 76 | + public void set(PlayerPickItemEvent event, @Nullable Slot slot) { |
| 77 | + if (!(slot instanceof InventorySlot inventorySlot) || inventorySlot.getInventory() != event.getPlayer().getInventory()) |
| 78 | + return; |
| 79 | + event.setTargetSlot(inventorySlot.getIndex()); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Slot convert(PlayerPickItemEvent event) { |
| 84 | + return new InventorySlot(event.getPlayer().getInventory(), event.getTargetSlot()); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + private @Nullable PickType pickType; |
| 90 | + private @Nullable Literal<?> type; |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult) { |
| 94 | + pickType = PATTERNS.getInfo(matchedPattern); |
| 95 | + if (pickType == null) |
| 96 | + type = args[0]; |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean check(Event event) { |
| 102 | + if (pickType != null) { |
| 103 | + return switch (pickType) { |
| 104 | + case ANY -> true; |
| 105 | + case BLOCK -> event instanceof PlayerPickBlockEvent; |
| 106 | + case ENTITY -> event instanceof PlayerPickEntityEvent; |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + Block pickedBlock; |
| 111 | + Entity pickedEntity; |
| 112 | + if (event instanceof PlayerPickBlockEvent pickBlockEvent) { |
| 113 | + pickedBlock = pickBlockEvent.getBlock(); |
| 114 | + pickedEntity = null; |
| 115 | + } else if (event instanceof PlayerPickEntityEvent pickEntityEvent) { |
| 116 | + pickedEntity = pickEntityEvent.getEntity(); |
| 117 | + pickedBlock = null; |
| 118 | + } else { |
| 119 | + assert false; |
| 120 | + return false; |
| 121 | + } |
| 122 | + assert type != null; |
| 123 | + return type.check(event, object -> switch (object) { |
| 124 | + case EntityData<?> entityData when pickedEntity != null -> entityData.isInstance(pickedEntity); |
| 125 | + case ItemType itemType when pickedEntity != null -> { |
| 126 | + Relation comparison = DefaultComparators.entityItemComparator.compare(EntityData.fromEntity(pickedEntity), itemType); |
| 127 | + yield Relation.EQUAL.isImpliedBy(comparison); |
| 128 | + } |
| 129 | + case ItemType itemType -> itemType.isOfType(pickedBlock); |
| 130 | + case BlockData blockData when pickedBlock != null -> pickedBlock.getBlockData().matches(blockData); |
| 131 | + default -> false; |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public String toString(@Nullable Event event, boolean debug) { |
| 137 | + SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); |
| 138 | + builder.append("player picking"); |
| 139 | + if (pickType != null) { |
| 140 | + switch (pickType) { |
| 141 | + case ANY -> builder.append("an item"); |
| 142 | + case BLOCK -> builder.append("a block"); |
| 143 | + case ENTITY -> builder.append("an entity"); |
| 144 | + } |
| 145 | + } else if (type != null) { |
| 146 | + builder.append(type); |
| 147 | + } |
| 148 | + return builder.toString(); |
| 149 | + } |
| 150 | + |
| 151 | + private enum PickType { |
| 152 | + ANY, |
| 153 | + BLOCK, |
| 154 | + ENTITY, |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments