|
| 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.iterator.LineIterator; |
| 6 | +import com.hypixel.hytale.math.vector.Location; |
| 7 | +import com.hypixel.hytale.math.vector.Vector3i; |
| 8 | +import com.hypixel.hytale.server.core.universe.Universe; |
| 9 | +import com.hypixel.hytale.server.core.universe.world.World; |
| 10 | +import io.github.syst3ms.skriptparser.lang.Expression; |
| 11 | +import io.github.syst3ms.skriptparser.lang.TriggerContext; |
| 12 | +import io.github.syst3ms.skriptparser.parsing.ParseContext; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Iterator; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class ExprBlockIterator implements Expression<Block> { |
| 20 | + |
| 21 | + public static void register(SkriptRegistration reg) { |
| 22 | + reg.newExpression(ExprBlockIterator.class, Block.class, false, |
| 23 | + "blocks within %location% and %location%", |
| 24 | + "blocks between %location% and %location%") |
| 25 | + .name("Block Iterator") |
| 26 | + .description("Get all blocks within (cuboid) or between (straight line) two locations.") |
| 27 | + .examples("loop blocks within {_loc1} and {_loc2}:", |
| 28 | + "\tset blocktype of loop-value to rock_stone") |
| 29 | + .since("INSERT VERSION") |
| 30 | + .register(); |
| 31 | + } |
| 32 | + |
| 33 | + private boolean within; |
| 34 | + private Expression<Location> loc1, loc2; |
| 35 | + |
| 36 | + @SuppressWarnings("unchecked") |
| 37 | + @Override |
| 38 | + public boolean init(Expression<?>[] expressions, int matchedPattern, @NotNull ParseContext parseContext) { |
| 39 | + this.within = matchedPattern == 0; |
| 40 | + this.loc1 = (Expression<Location>) expressions[0]; |
| 41 | + this.loc2 = (Expression<Location>) expressions[1]; |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Block[] getValues(@NotNull TriggerContext ctx) { |
| 47 | + List<Block> blocks = new ArrayList<>(); |
| 48 | + iterator(ctx).forEachRemaining(blocks::add); |
| 49 | + return blocks.toArray(Block[]::new); |
| 50 | + } |
| 51 | + |
| 52 | + @SuppressWarnings("RedundantOperationOnEmptyContainer") |
| 53 | + @Override |
| 54 | + public Iterator<? extends Block> iterator(@NotNull TriggerContext ctx) { |
| 55 | + List<Block> blocks = new ArrayList<>(); |
| 56 | + |
| 57 | + Location loc1 = this.loc1.getSingle(ctx).orElse(null); |
| 58 | + Location loc2 = this.loc2.getSingle(ctx).orElse(null); |
| 59 | + if (loc1 == null || loc2 == null) return blocks.iterator(); |
| 60 | + |
| 61 | + String worldString = loc1.getWorld(); |
| 62 | + if (worldString == null) return blocks.iterator(); |
| 63 | + |
| 64 | + World world = Universe.get().getWorld(worldString); |
| 65 | + if (world == null) return blocks.iterator(); |
| 66 | + |
| 67 | + Vector3i pos1 = loc1.getPosition().toVector3i(); |
| 68 | + Vector3i pos2 = loc2.getPosition().toVector3i(); |
| 69 | + |
| 70 | + if (this.within) { |
| 71 | + Vector3i min = Vector3i.min(pos1, pos2); |
| 72 | + Vector3i max = Vector3i.max(pos1, pos2); |
| 73 | + for (int x = min.getX(); x <= max.getX(); x++) { |
| 74 | + for (int y = min.getY(); y <= max.getY(); y++) { |
| 75 | + for (int z = min.getZ(); z <= max.getZ(); z++) { |
| 76 | + Block block = new Block(world, new Vector3i(x, y, z)); |
| 77 | + blocks.add(block); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return blocks.iterator(); |
| 82 | + } else { |
| 83 | + return new BlockLineIterator(world, pos1, pos2); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String toString(@NotNull TriggerContext ctx, boolean debug) { |
| 89 | + String type = this.within ? " within " : " between "; |
| 90 | + return "Blocks" + type + this.loc1.toString(ctx, debug) + " and " + this.loc2.toString(ctx, debug); |
| 91 | + } |
| 92 | + |
| 93 | + private static class BlockLineIterator implements Iterator<Block> { |
| 94 | + |
| 95 | + private final World world; |
| 96 | + private final LineIterator lineIterator; |
| 97 | + |
| 98 | + public BlockLineIterator(World world, Vector3i pos1, Vector3i pos2) { |
| 99 | + this.world = world; |
| 100 | + this.lineIterator = new LineIterator(pos1.getX(), pos1.getY(), pos1.getZ(), pos2.getX(), pos2.getY(), pos2.getZ()); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public boolean hasNext() { |
| 105 | + return this.lineIterator.hasNext(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Block next() { |
| 110 | + Vector3i pos = this.lineIterator.next(); |
| 111 | + return new Block(world, pos); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments