package com.btk5h.skriptmirror.skript.custom;

import org.bukkit.event.Event;

import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;

public class CustomSyntaxExpression extends SimpleExpression<Object> {
  private Expression<?> source;
  private Event realEvent;

  public CustomSyntaxExpression(Expression<?> source, Event realEvent) {
    this.source = source;
    this.realEvent = realEvent;
  }

  public static CustomSyntaxExpression wrap(Expression<?> source, Event realEvent) {
    if (source instanceof CustomSyntaxExpression) {
      return (CustomSyntaxExpression) source;
    }
    return new CustomSyntaxExpression(source, realEvent);
  }

  @Override
  protected Object[] get(Event e) {
    return source == null ? new Object[0] : source.getAll(realEvent);
  }

  @Override
  public boolean isSingle() {
    return source == null || source.isSingle();
  }

  @Override
  public Class<?> getReturnType() {
    return source == null ? Object.class : source.getReturnType();
  }

  @Override
  public String toString(Event e, boolean debug) {
    return source == null ? "" : source.toString(realEvent, debug);
  }

  @Override
  public Expression<?> getSource() {
    return source;
  }

  @Override
  public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
    throw new UnsupportedOperationException();
  }
}

@@ -44,40 +44,8 @@
 m;%0A%0A
-import org.bukkit.event.Event;%0A%0A
 impo
@@ -71,32 +71,32 @@
 ang.Expression;%0A
+
 import ch.njol.s
@@ -199,16 +199,47 @@
 leenean;
+%0Aimport org.bukkit.event.Event;
 %0A%0Apublic
@@ -361,16 +361,42 @@
 alEvent;
+%0A  private Object%5B%5D value;
 %0A%0A  publ
@@ -520,16 +520,144 @@
 lEvent;%0A
+    // TODO store more information from the source?%0A    this.value = source == null ? new Object%5B0%5D : source.getAll(realEvent);%0A
   %7D%0A%0A  p
@@ -968,65 +968,13 @@
 urn 
-source == null ? new Object%5B0%5D : source.getAll(realEvent)
+value
 ;%0A  

Store value of expressions used in custom syntaxes (fixes #61)