|
1 | 1 | package dev.jsinco.brewery.bukkit.integration.event.skript; |
2 | 2 |
|
| 3 | +import ch.njol.skript.ScriptLoader; |
3 | 4 | import ch.njol.skript.Skript; |
| 5 | +import ch.njol.skript.config.SectionNode; |
4 | 6 | import ch.njol.skript.lang.Literal; |
5 | 7 | import ch.njol.skript.lang.SkriptParser; |
| 8 | +import ch.njol.skript.lang.TriggerItem; |
6 | 9 | import dev.jsinco.brewery.api.util.BreweryKey; |
7 | 10 | import net.kyori.adventure.text.Component; |
| 11 | +import net.kyori.adventure.text.minimessage.MiniMessage; |
8 | 12 | import org.bukkit.event.Event; |
9 | 13 | import org.jetbrains.annotations.Nullable; |
10 | 14 | import org.jetbrains.annotations.UnknownNullability; |
11 | 15 | import org.skriptlang.skript.lang.entry.EntryContainer; |
12 | 16 | import org.skriptlang.skript.lang.structure.Structure; |
| 17 | +import org.skriptlang.skript.registration.DefaultSyntaxInfos; |
| 18 | +import org.skriptlang.skript.registration.SyntaxRegistry; |
| 19 | + |
| 20 | +import java.util.List; |
13 | 21 |
|
14 | 22 | public class TbpSkriptEventStructure extends Structure { |
15 | 23 |
|
16 | 24 |
|
| 25 | + private BreweryKey key; |
| 26 | + private Component displayName; |
| 27 | + private SectionNode source; |
| 28 | + private List<TriggerItem> code; |
| 29 | + |
17 | 30 | @Override |
18 | 31 | public String toString(@Nullable Event event, boolean debug) { |
19 | | - return ""; |
| 32 | + if (debug) { |
| 33 | + return String.format("TbpSkriptEventStructure(key='%s', displayName='%s')", key.minimalized(SkriptIntegration.NAMESPACE), MiniMessage.miniMessage().serialize(displayName)); |
| 34 | + } |
| 35 | + return String.format("[tbp_]register event with key \"%s\" [and ][display ]name \"%s\"", key.minimalized(SkriptIntegration.NAMESPACE), MiniMessage.miniMessage().serialize(displayName)); |
20 | 36 | } |
21 | 37 |
|
22 | 38 | @Override |
23 | 39 | public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult, @UnknownNullability EntryContainer entryContainer) { |
24 | | - BreweryKey key = null; |
25 | | - Component displayName = null; |
26 | | - for (Literal<?> argument : args) { |
27 | | - Literal<? extends String> converted = argument.getConvertedExpression(String.class); |
28 | | - if (converted == null) { |
29 | | - Skript.error("[TBP] Could not parse argument of event; could not convert value to string."); |
30 | | - return false; |
31 | | - } |
| 40 | + this.key = BreweryKey.parse((String) args[0].getSingle()); |
| 41 | + this.displayName = (Component) args[1].getSingle(); |
| 42 | + if (entryContainer == null) { |
| 43 | + Skript.error("[TBP] Empty entry container, this event does nothing! "); |
| 44 | + return false; |
32 | 45 | } |
| 46 | + this.source = entryContainer.getSource(); |
| 47 | + return true; |
33 | 48 | } |
34 | 49 |
|
35 | 50 | @Override |
36 | 51 | public boolean load() { |
37 | | - return false; |
| 52 | + this.code = ScriptLoader.loadItems(source); |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + public void run() { |
| 57 | + // What to do here? |
| 58 | + } |
| 59 | + |
| 60 | + public static void register(SyntaxRegistry syntaxRegistry) { |
| 61 | + syntaxRegistry |
| 62 | + .register(SyntaxRegistry.Key.of("tbp_custom_event_structure"), DefaultSyntaxInfos.Structure.builder(TbpSkriptEventStructure.class) |
| 63 | + .addPattern("[tbp_]register event with key %text% [and ][display ]name %text component%") |
| 64 | + .supplier(TbpSkriptEventStructure::new) |
| 65 | + .build() |
| 66 | + ); |
38 | 67 | } |
39 | 68 | } |
0 commit comments