|
| 1 | +package ch.njol.skript.expressions; |
| 2 | + |
| 3 | +import ch.njol.skript.Skript; |
| 4 | +import ch.njol.skript.classes.Changer.ChangeMode; |
| 5 | +import ch.njol.skript.doc.*; |
| 6 | +import ch.njol.skript.expressions.base.PropertyExpression; |
| 7 | +import ch.njol.skript.lang.Expression; |
| 8 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 9 | +import ch.njol.util.Kleenean; |
| 10 | +import ch.njol.util.coll.CollectionUtils; |
| 11 | +import io.papermc.paper.event.player.PlayerChangeBeaconEffectEvent; |
| 12 | +import org.bukkit.block.Beacon; |
| 13 | +import org.bukkit.block.Block; |
| 14 | +import org.bukkit.event.Event; |
| 15 | +import org.bukkit.potion.PotionEffect; |
| 16 | +import org.bukkit.potion.PotionEffectType; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | + |
| 19 | +import java.util.function.BiConsumer; |
| 20 | + |
| 21 | +@Name("Beacon Effects") |
| 22 | +@Description({ |
| 23 | + "The active effects of a beacon.", |
| 24 | + "The secondary effect can be set to anything, but the icon in the GUI will not display correctly.", |
| 25 | + "The secondary effect can only be set when the beacon is at max tier.", |
| 26 | + "The primary and secondary effect can not be the same, primary will always retain the potion type and secondary will be cleared." |
| 27 | +}) |
| 28 | +@Example(""" |
| 29 | + set primary beacon effect of {_block} to haste |
| 30 | + set secondary effect of {_block} to resistance |
| 31 | + """ |
| 32 | +) |
| 33 | +@Events({"Beacon Effect", "Beacon Toggle", "Beacon Change Effect"}) |
| 34 | +@Since("2.10") |
| 35 | +public class ExprBeaconEffects extends PropertyExpression<Block, PotionEffectType> { |
| 36 | + |
| 37 | + private static final boolean SUPPORTS_CHANGE_EVENT = Skript.classExists("io.papermc.paper.event.player.PlayerChangeBeaconEffectEvent"); |
| 38 | + |
| 39 | + static { |
| 40 | + registerDefault(ExprBeaconEffects.class, PotionEffectType.class, "(:primary|secondary) [beacon] effect", "blocks"); |
| 41 | + } |
| 42 | + |
| 43 | + private boolean primary; |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
| 47 | + //noinspection unchecked |
| 48 | + setExpr((Expression<? extends Block>) expressions[0]); |
| 49 | + primary = parseResult.hasTag("primary"); |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected PotionEffectType[] get(Event event, Block[] blocks) { |
| 55 | + return get(blocks, block -> { |
| 56 | + if (!(block.getState() instanceof Beacon beacon)) |
| 57 | + return null; |
| 58 | + |
| 59 | + if (SUPPORTS_CHANGE_EVENT |
| 60 | + && event instanceof PlayerChangeBeaconEffectEvent changeEvent |
| 61 | + && block.equals(changeEvent.getBeacon())) |
| 62 | + return primary ? changeEvent.getPrimary() : changeEvent.getSecondary(); |
| 63 | + |
| 64 | + PotionEffect effect = primary ? beacon.getPrimaryEffect() : beacon.getSecondaryEffect(); |
| 65 | + if (effect == null) |
| 66 | + return null; |
| 67 | + return effect.getType(); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Class<?> @Nullable [] acceptChange(ChangeMode mode) { |
| 73 | + return switch (mode) { |
| 74 | + case SET, RESET, DELETE -> CollectionUtils.array(PotionEffectType.class); |
| 75 | + default -> null; |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { |
| 81 | + PotionEffectType type = delta == null ? null : (PotionEffectType) delta[0]; |
| 82 | + BiConsumer<Beacon, PotionEffectType> changer = primary ? Beacon::setPrimaryEffect : Beacon::setSecondaryEffect; |
| 83 | + for (Block block : getExpr().getArray(event)) { |
| 84 | + if (!(block.getState() instanceof Beacon beacon)) |
| 85 | + continue; |
| 86 | + changer.accept(beacon, type); |
| 87 | + beacon.update(true); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Class<PotionEffectType> getReturnType() { |
| 93 | + return PotionEffectType.class; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public String toString(@Nullable Event event, boolean debug) { |
| 98 | + return (primary ? "primary" : "secondary") + " beacon effect of " + getExpr().toString(event, debug); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments