|
| 1 | +/** |
| 2 | + * This file is part of Skript. |
| 3 | + * |
| 4 | + * Skript is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * Skript is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with Skript. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * Copyright Peter Güttinger, SkriptLang team and contributors |
| 18 | + */ |
| 19 | +package ch.njol.skript.lang; |
| 20 | + |
| 21 | +import ch.njol.skript.ScriptLoader; |
| 22 | +import ch.njol.skript.SkriptAPIException; |
| 23 | +import ch.njol.skript.config.SectionNode; |
| 24 | +import ch.njol.skript.lang.parser.ParserInstance; |
| 25 | +import org.bukkit.event.Event; |
| 26 | +import org.jetbrains.annotations.ApiStatus.NonExtendable; |
| 27 | +import org.jetbrains.annotations.Nullable; |
| 28 | + |
| 29 | +import java.util.Deque; |
| 30 | +import java.util.LinkedList; |
| 31 | + |
| 32 | +public interface ReturnHandler<T> { |
| 33 | + |
| 34 | + /** |
| 35 | + * Loads the code in the given {@link SectionNode} using the same logic as |
| 36 | + * {@link Section#loadCode(SectionNode)} and pushes the section onto the |
| 37 | + * return handler stack |
| 38 | + * <br> |
| 39 | + * <b>This method may only be called by a {@link Section}</b> |
| 40 | + * @throws SkriptAPIException if this return handler is not a {@link Section} |
| 41 | + */ |
| 42 | + @NonExtendable |
| 43 | + default void loadReturnableSectionCode(SectionNode node) { |
| 44 | + if (!(this instanceof Section)) |
| 45 | + throw new SkriptAPIException("loadReturnableSectionCode called on a non-section object"); |
| 46 | + ParserInstance parser = ParserInstance.get(); |
| 47 | + ReturnHandlerStack stack = parser.getData(ReturnHandlerStack.class); |
| 48 | + stack.push(this); |
| 49 | + Section section = (Section) this; |
| 50 | + try { |
| 51 | + section.loadCode(node); |
| 52 | + } finally { |
| 53 | + stack.pop(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Loads the code in the given {@link SectionNode} using the same logic as |
| 59 | + * {@link Section#loadCode(SectionNode, String, Class[])} and pushes the section onto the |
| 60 | + * return handler stack |
| 61 | + * <br> |
| 62 | + * <b>This method may only be called by a {@link Section}</b> |
| 63 | + * @param node the section node |
| 64 | + * @param name the name of the event(s) being used |
| 65 | + * @param events the event(s) during the section's execution |
| 66 | + * @return a returnable trigger containing the loaded section. |
| 67 | + * This should be stored and used to run the section one or more times |
| 68 | + * @throws SkriptAPIException if this return handler is not a {@link Section} |
| 69 | + */ |
| 70 | + @NonExtendable |
| 71 | + default ReturnableTrigger<T> loadReturnableSectionCode(SectionNode node, String name, Class<? extends Event>[] events) { |
| 72 | + if (!(this instanceof Section)) |
| 73 | + throw new SkriptAPIException("loadReturnableSectionCode called on a non-section object"); |
| 74 | + ParserInstance parser = ParserInstance.get(); |
| 75 | + ParserInstance.Backup parserBackup = parser.backup(); |
| 76 | + parser.reset(); |
| 77 | + |
| 78 | + parser.setCurrentEvent(name, events); |
| 79 | + SkriptEvent skriptEvent = new SectionSkriptEvent(name, (Section) this); |
| 80 | + parser.setCurrentStructure(skriptEvent); |
| 81 | + ReturnHandlerStack stack = parser.getData(ReturnHandlerStack.class); |
| 82 | + |
| 83 | + try { |
| 84 | + return new ReturnableTrigger<>( |
| 85 | + this, |
| 86 | + parser.getCurrentScript(), |
| 87 | + name, |
| 88 | + skriptEvent, |
| 89 | + trigger -> { |
| 90 | + stack.push(trigger); |
| 91 | + return ScriptLoader.loadItems(node); |
| 92 | + } |
| 93 | + ); |
| 94 | + } finally { |
| 95 | + stack.pop(); |
| 96 | + parser.restoreBackup(parserBackup); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Loads the code in the given {@link SectionNode} into a {@link ReturnableTrigger}. |
| 102 | + * <br> |
| 103 | + * This is a general method to load a section node without extra logic |
| 104 | + * done to the {@link ParserInstance}. |
| 105 | + * The calling code is expected to manage the {@code ParserInstance} accordingly, which may vary depending on |
| 106 | + * where the code being loaded is located and what state the {@code ParserInstance} is in. |
| 107 | + * @param node the section node to load |
| 108 | + * @param name the name of the trigger |
| 109 | + * @param event the {@link SkriptEvent} of the trigger |
| 110 | + * @return a returnable trigger containing the loaded section node |
| 111 | + */ |
| 112 | + @NonExtendable |
| 113 | + default ReturnableTrigger<T> loadReturnableTrigger(SectionNode node, String name, SkriptEvent event) { |
| 114 | + ParserInstance parser = ParserInstance.get(); |
| 115 | + ReturnHandlerStack stack = parser.getData(ReturnHandlerStack.class); |
| 116 | + try { |
| 117 | + return new ReturnableTrigger<T>( |
| 118 | + this, |
| 119 | + parser.getCurrentScript(), |
| 120 | + name, |
| 121 | + event, |
| 122 | + trigger -> { |
| 123 | + stack.push(trigger); |
| 124 | + return ScriptLoader.loadItems(node); |
| 125 | + } |
| 126 | + ); |
| 127 | + } finally { |
| 128 | + stack.pop(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @param values the values to return |
| 134 | + */ |
| 135 | + void returnValues(T @Nullable [] values); |
| 136 | + |
| 137 | + /** |
| 138 | + * @return whether this return handler may accept multiple return values |
| 139 | + */ |
| 140 | + boolean isSingleReturnValue(); |
| 141 | + |
| 142 | + /** |
| 143 | + * The return type of this return handler, or null if it can't |
| 144 | + * accept return values in this context (e.g. a function without a return type). |
| 145 | + * |
| 146 | + * @return the return type |
| 147 | + */ |
| 148 | + @Nullable Class<? extends T> returnValueType(); |
| 149 | + |
| 150 | + class ReturnHandlerStack extends ParserInstance.Data { |
| 151 | + |
| 152 | + private final Deque<ReturnHandler<?>> stack = new LinkedList<>(); |
| 153 | + |
| 154 | + public ReturnHandlerStack(ParserInstance parserInstance) { |
| 155 | + super(parserInstance); |
| 156 | + } |
| 157 | + |
| 158 | + public Deque<ReturnHandler<?>> getStack() { |
| 159 | + return stack; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Retrieves the current {@link ReturnHandler} |
| 164 | + * @return the return data |
| 165 | + */ |
| 166 | + public @Nullable ReturnHandler<?> getCurrentHandler() { |
| 167 | + return stack.peek(); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Pushes the current return handler onto the return stack. |
| 172 | + * <br> |
| 173 | + * <b>Note: After the trigger finished loading, |
| 174 | + * {@link ReturnHandlerStack#pop()} <u>MUST</u> be called</b> |
| 175 | + * @param handler the return handler |
| 176 | + * @see ReturnHandlerStack#pop() |
| 177 | + */ |
| 178 | + public void push(ReturnHandler<?> handler) { |
| 179 | + stack.push(handler); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Pops the current handler off the return stack. |
| 184 | + * Should be called after the trigger has finished loading. |
| 185 | + * @return the popped return data |
| 186 | + * @see ReturnHandlerStack#push(ReturnHandler) |
| 187 | + */ |
| 188 | + public ReturnHandler<?> pop() { |
| 189 | + return stack.pop(); |
| 190 | + } |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments