|
| 1 | +/** |
| 2 | + * This file is part of Skript. |
| 3 | + * |
| 4 | + * Skript is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * Skript is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with Skript. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * Copyright Peter Güttinger, SkriptLang team and contributors |
| 18 | + */ |
| 19 | +package ch.njol.skript.effects; |
| 20 | + |
| 21 | +import ch.njol.skript.Skript; |
| 22 | +import ch.njol.skript.doc.Description; |
| 23 | +import ch.njol.skript.doc.Examples; |
| 24 | +import ch.njol.skript.doc.Name; |
| 25 | +import ch.njol.skript.doc.RequiredPlugins; |
| 26 | +import ch.njol.skript.doc.Since; |
| 27 | +import ch.njol.skript.lang.Effect; |
| 28 | +import ch.njol.skript.lang.Expression; |
| 29 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 30 | +import ch.njol.skript.util.Direction; |
| 31 | +import ch.njol.util.Kleenean; |
| 32 | +import org.bukkit.block.Bell; |
| 33 | +import org.bukkit.block.Block; |
| 34 | +import org.bukkit.block.BlockFace; |
| 35 | +import org.bukkit.block.BlockState; |
| 36 | +import org.bukkit.entity.Entity; |
| 37 | +import org.bukkit.event.Event; |
| 38 | +import org.eclipse.jdt.annotation.Nullable; |
| 39 | + |
| 40 | +@Name("Ring Bell") |
| 41 | +@Description({ |
| 42 | + "Causes a bell to ring.", |
| 43 | + "Optionally, the entity that rang the bell and the direction the bell should ring can be specified.", |
| 44 | + "A bell can only ring in two directions, and the direction is determined by which way the bell is facing.", |
| 45 | + "By default, the bell will ring in the direction it is facing.", |
| 46 | +}) |
| 47 | +@Examples({"make player ring target-block"}) |
| 48 | +@RequiredPlugins("Spigot 1.19.4+") |
| 49 | +@Since("INSERT VERSION") |
| 50 | +public class EffRing extends Effect { |
| 51 | + |
| 52 | + static { |
| 53 | + if (Skript.classExists("org.bukkit.block.Bell") && Skript.methodExists(Bell.class, "ring", Entity.class, BlockFace.class)) |
| 54 | + Skript.registerEffect(EffRing.class, |
| 55 | + "ring %blocks% [from [the]] [%-direction%]", |
| 56 | + "(make|let) %entity% ring %blocks% [from [the]] [%-direction%]" |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + @Nullable |
| 61 | + private Expression<Entity> entity; |
| 62 | + |
| 63 | + @SuppressWarnings("NotNullFieldNotInitialized") |
| 64 | + private Expression<Block> blocks; |
| 65 | + |
| 66 | + @Nullable |
| 67 | + private Expression<Direction> direction; |
| 68 | + |
| 69 | + @Override |
| 70 | + @SuppressWarnings("unchecked") |
| 71 | + public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
| 72 | + entity = matchedPattern == 0 ? null : (Expression<Entity>) exprs[0]; |
| 73 | + blocks = (Expression<Block>) exprs[matchedPattern]; |
| 74 | + direction = (Expression<Direction>) exprs[matchedPattern + 1]; |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + @Nullable |
| 79 | + private BlockFace getBlockFace(Event event) { |
| 80 | + if (this.direction == null) |
| 81 | + return null; |
| 82 | + |
| 83 | + Direction direction = this.direction.getSingle(event); |
| 84 | + if (direction == null) |
| 85 | + return null; |
| 86 | + |
| 87 | + return Direction.getFacing(direction.getDirection(), true); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected void execute(Event event) { |
| 92 | + BlockFace blockFace = getBlockFace(event); |
| 93 | + Entity actualEntity = entity == null ? null : entity.getSingle(event); |
| 94 | + |
| 95 | + for (Block block : blocks.getArray(event)) { |
| 96 | + BlockState state = block.getState(false); |
| 97 | + if (state instanceof Bell) { |
| 98 | + Bell bell = (Bell) state; |
| 99 | + bell.ring(actualEntity, blockFace); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String toString(@Nullable Event event, boolean debug) { |
| 106 | + return (entity != null ? "make " + entity.toString(event, debug) + " " : "") + |
| 107 | + "ring " + blocks.toString(event, debug) + " from " + (direction != null ? direction.toString(event, debug) : ""); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments