|
| 1 | +package com.github.skriptdev.skript.plugin.elements.expressions.block; |
| 2 | + |
| 3 | +import com.github.skriptdev.skript.api.hytale.Block; |
| 4 | +import com.github.skriptdev.skript.api.skript.registration.SkriptRegistration; |
| 5 | +import com.hypixel.hytale.math.vector.Vector3i; |
| 6 | +import io.github.syst3ms.skriptparser.lang.Expression; |
| 7 | +import io.github.syst3ms.skriptparser.lang.TriggerContext; |
| 8 | +import io.github.syst3ms.skriptparser.parsing.ParseContext; |
| 9 | +import io.github.syst3ms.skriptparser.types.changers.ChangeMode; |
| 10 | +import io.github.syst3ms.skriptparser.util.CollectionUtils; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | + |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +public class ExprBlockRotation implements Expression<Vector3i> { |
| 16 | + |
| 17 | + public static void register(SkriptRegistration reg) { |
| 18 | + reg.newExpression(ExprBlockRotation.class, Vector3i.class, true, |
| 19 | + "block rotation of %block%") |
| 20 | + .name("Block Rotation") |
| 21 | + .description("Get/set the rotation of a block.", |
| 22 | + "Represented as a vector3i(pitch, yaw, roll).", |
| 23 | + "Do note that only increments of 90 degrees are supported (0, 90, 180, 270).", |
| 24 | + "Caution: This allows you to rotate blocks in ways they're not meant to... have fun, be careful!") |
| 25 | + .examples("set block rotation of block at player to vector3i(0, 180, 0)") |
| 26 | + .since("INSERT VERSION") |
| 27 | + .register(); |
| 28 | + } |
| 29 | + |
| 30 | + private Expression<Block> block; |
| 31 | + |
| 32 | + @SuppressWarnings("unchecked") |
| 33 | + @Override |
| 34 | + public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, @NotNull ParseContext parseContext) { |
| 35 | + this.block = (Expression<Block>) expressions[0]; |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Vector3i[] getValues(@NotNull TriggerContext ctx) { |
| 41 | + Block block = this.block.getSingle(ctx).orElse(null); |
| 42 | + if (block == null) return null; |
| 43 | + |
| 44 | + return new Vector3i[]{block.getRotation()}; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Optional<Class<?>[]> acceptsChange(@NotNull ChangeMode mode) { |
| 49 | + if (mode == ChangeMode.SET) return CollectionUtils.optionalArrayOf(Vector3i.class); |
| 50 | + return Optional.empty(); |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("ConstantValue") |
| 54 | + @Override |
| 55 | + public void change(@NotNull TriggerContext ctx, @NotNull ChangeMode changeMode, Object @NotNull [] changeWith) { |
| 56 | + if (changeWith == null || changeWith.length == 0 || !(changeWith[0] instanceof Vector3i rotation)) return; |
| 57 | + |
| 58 | + this.block.getSingle(ctx).ifPresent(block -> block.setRotation(rotation)); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String toString(@NotNull TriggerContext ctx, boolean debug) { |
| 63 | + return "block rotation of " + this.block.toString(ctx, debug); |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments