|
| 1 | +package com.github.skriptdev.skript.plugin.elements.expressions.entity; |
| 2 | + |
| 3 | +import com.github.skriptdev.skript.api.skript.registration.SkriptRegistration; |
| 4 | +import com.hypixel.hytale.component.Ref; |
| 5 | +import com.hypixel.hytale.component.Store; |
| 6 | +import com.hypixel.hytale.server.core.entity.Entity; |
| 7 | +import com.hypixel.hytale.server.core.entity.EntityUtils; |
| 8 | +import com.hypixel.hytale.server.core.universe.world.World; |
| 9 | +import com.hypixel.hytale.server.core.universe.world.storage.EntityStore; |
| 10 | +import com.hypixel.hytale.server.core.util.TargetUtil; |
| 11 | +import io.github.syst3ms.skriptparser.lang.Expression; |
| 12 | +import io.github.syst3ms.skriptparser.lang.TriggerContext; |
| 13 | +import io.github.syst3ms.skriptparser.parsing.ParseContext; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | + |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +public class ExprTargetEntityOfEntity implements Expression<Entity> { |
| 19 | + |
| 20 | + public static void register(SkriptRegistration reg) { |
| 21 | + reg.newExpression(ExprTargetEntityOfEntity.class, Entity.class, true, |
| 22 | + "target entity of %entity%") |
| 23 | + .name("Target Entity of Entity") |
| 24 | + .description("Returns the target entity of the given entity.") |
| 25 | + .examples("set {_target} to target entity of player") |
| 26 | + .since("INSERT VERSION") |
| 27 | + .register(); |
| 28 | + } |
| 29 | + |
| 30 | + private Expression<Entity> entity; |
| 31 | + |
| 32 | + @SuppressWarnings("unchecked") |
| 33 | + @Override |
| 34 | + public boolean init(Expression<?>[] expressions, int matchedPattern, @NotNull ParseContext parseContext) { |
| 35 | + this.entity = (Expression<Entity>) expressions[0]; |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + @SuppressWarnings("deprecation") |
| 40 | + @Override |
| 41 | + public Entity[] getValues(@NotNull TriggerContext ctx) { |
| 42 | + Optional<? extends Entity> single = this.entity.getSingle(ctx); |
| 43 | + if (single.isEmpty()) return null; |
| 44 | + |
| 45 | + Entity entity = single.get(); |
| 46 | + Ref<EntityStore> ref = entity.getReference(); |
| 47 | + World world = entity.getWorld(); |
| 48 | + if (world == null || ref == null) return null; |
| 49 | + |
| 50 | + Store<EntityStore> store = world.getEntityStore().getStore(); |
| 51 | + Ref<EntityStore> targetEntity = TargetUtil.getTargetEntity(ref, store); |
| 52 | + if (targetEntity == null || !targetEntity.isValid()) return null; |
| 53 | + |
| 54 | + // TODO better handling of this deprecation |
| 55 | + Entity target = EntityUtils.getEntity(targetEntity, store); |
| 56 | + |
| 57 | + return new Entity[]{target}; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String toString(@NotNull TriggerContext ctx, boolean debug) { |
| 62 | + return "target entity of " + this.entity.toString(ctx, debug); |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments