From bf857f7b6db0b2b728773abdc3c9aa1416b7aa9b Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Tue, 30 May 2023 19:31:04 -0600 Subject: [PATCH 1/7] Add custom events (cherry picked from commit 07e1700e360f37f274fda475059cf091554beb9f) --- .../java/ch/njol/skript/CustomEvents.java | 148 ++++++++++++++++++ src/main/java/ch/njol/skript/Skript.java | 3 + src/main/resources/custom-events.sk | 41 +++++ 3 files changed, 192 insertions(+) create mode 100644 src/main/java/ch/njol/skript/CustomEvents.java create mode 100644 src/main/resources/custom-events.sk diff --git a/src/main/java/ch/njol/skript/CustomEvents.java b/src/main/java/ch/njol/skript/CustomEvents.java new file mode 100644 index 00000000000..77ffad3379f --- /dev/null +++ b/src/main/java/ch/njol/skript/CustomEvents.java @@ -0,0 +1,148 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.bukkit.event.Event; + +import ch.njol.skript.config.Config; +import ch.njol.skript.config.Node; +import ch.njol.skript.config.OptionSection; +import ch.njol.skript.config.SectionNode; +import ch.njol.skript.lang.util.SimpleEvent; +import ch.njol.skript.log.ParseLogHandler; +import ch.njol.skript.log.RetainingLogHandler; +import ch.njol.skript.log.SkriptLogger; +import ch.njol.skript.util.FileUtils; + +public class CustomEvents { + + public static final OptionSection events = new OptionSection("events"); + + private static boolean loaded; + + @SuppressWarnings("unchecked") + public static void load(File file) { + if (loaded) + return; + loaded = true; + File eventsFile = new File(Skript.getInstance().getDataFolder(), "custom-events.sk"); + Config configuration = null; + if (eventsFile.exists()) { + try { + configuration = new Config(eventsFile, false, false, ":"); + } catch (IOException e) { + Skript.exception(e, "Failed to load custom-events.sk"); + } + } else { + try (ZipFile zip = new ZipFile(file)) { + File saveTo = null; + ZipEntry entry = zip.getEntry("custom-events.sk"); + if (entry != null) { + File destination = new File(Skript.getInstance().getDataFolder(), entry.getName()); + if (!destination.exists()) + saveTo = destination; + } if (saveTo != null) { + try (InputStream in = zip.getInputStream(entry)) { + FileUtils.save(in, saveTo); + } + } + } catch (IOException e) { + if (Skript.debug()) + Skript.exception(e); + } finally { + eventsFile = new File(Skript.getInstance().getDataFolder(), "custom-events.sk"); + try { + configuration = new Config(eventsFile, false, false, ":"); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + if (configuration != null) { + configuration.load(CustomEvents.class); + // When we need to change values in the future of this file, bump the version node. + if (configuration.get("version").isEmpty() || !configuration.get("version").equalsIgnoreCase("1")) { + Skript.warning("Your custom-events.sk config file is outdated. " + + "Backup any changes you've made, and delete your custom-events.sk in the Skript folder to update it. " + + "After, re-add any nodes you've changed."); + } + SectionNode section = (SectionNode) configuration.getMainNode().get("events"); + if (section != null && !section.isEmpty()) { + for (Node node : section) { + if (!(node instanceof SectionNode)) { + Skript.warning("Event node '" + node.getKey() + "' was not a section, ignoring."); + continue; + } + SectionNode eventSection = (SectionNode) node; + String name = node.getKey(); + String enabled = eventSection.get("enabled", "true"); + if (enabled.equalsIgnoreCase("false")) + continue; + String description = eventSection.get("description", "custom event '" + name + "'"); + String pattern = eventSection.getValue("pattern"); + if (pattern == null) { + Skript.warning("Event node '" + name + "' did not contain a 'pattern' value."); + continue; + } + if (pattern.contains("%")) { + SkriptLogger.setNode(eventSection.get("pattern")); + Skript.warning("Event node '" + name + "' cannot contain any expressions in the pattern '" + pattern + "'"); + continue; + } + String eventClass = eventSection.getValue("class"); + if (eventClass == null) { + Skript.warning("Event node '" + name + "' did not contain a 'class' value."); + continue; + } + + // Everything below this should be dedicated to the class node. + SkriptLogger.setNode(eventSection.get("class")); + Class event; + try { + event = Class.forName(eventClass); + } catch (Exception e) { + if (Skript.logVeryHigh() || Skript.debug()) { + Skript.exception(e, "Class '" + eventClass + "' for event node '" + name + "' had an exception loading."); + } else { + Skript.error("Failed to find class '" + eventClass + "' for event node '" + name + "'"); + } + continue; + } + if (event == null) + continue; + if (!(Event.class.isAssignableFrom(event))) { + Skript.error("Class '" + eventClass + "' for event node '" + name + "' was not a valid Event class!"); + continue; + } + Skript.registerEvent(name, SimpleEvent.class, (Class) event, pattern) + .requiredPlugins("Custom Skript Event") + .description(description); + } + } + } + } + +} diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index dab73c2b5a6..99228328579 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -548,6 +548,9 @@ public void onEnable() { return; } + // Must load after loadClasses + CustomEvents.load(getFile()); + Commands.registerListeners(); if (logNormal()) diff --git a/src/main/resources/custom-events.sk b/src/main/resources/custom-events.sk new file mode 100644 index 00000000000..0f0248dbf04 --- /dev/null +++ b/src/main/resources/custom-events.sk @@ -0,0 +1,41 @@ + +# This file allows you to register custom events that aren't already registered under Skript. +# The event classes you want to register must exist in the server class loader. So events from Paper/Spigot. +# If you want to register an event from a plugin API, you must edit the plugin.yml in the jar of Skript to include: +# +# softdepend: [YourPlugin, SQLibrary, Vault, WorldGuard, Residence, PreciousStones, GriefPrevention] +# +# Reminder to maintain any existing defined plugins in this array as this example should not be directly copied. +#! Additions or changes to this file requires a server restart. + +#! Internal version of this configuration. Don't modify. +version: 1 + +# The format must include a section where the section name is the event name. +# The event section must include: +# description - describing the event +# class - the class name to search for +# pattern - the syntax pattern of the event +# The optional pattern [on] gets prefixed on every Skript event. +# All event-values will be automatically applied. +# If you need more info, make an issue report at https://github.com/SkriptLang/Skript/issues + +# Examples are placed below. The 'enabled' node is not required, it's just to disable examples. +events: + # [on] beacon effect apply + beacon effect: + enabled: false + class: com.destroystokyo.paper.event.block.BeaconEffectEvent + pattern: beacon effect apply [to player[s]] + + # [on] [player] enter bed + player enter bed: + enabled: false + class: org.bukkit.event.player.PlayerBedEnterEvent + pattern: [player] enter bed + + # [on] campfire lit + campfire lit: + enabled: false + class: org.bukkit.event.block.CampfireStartEvent + pattern: campfire (lit|start [burning]) From 1f3c2ec9e55d965596a476ef18ba058b2d90fa1e Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Tue, 30 May 2023 19:42:29 -0600 Subject: [PATCH 2/7] Add to configuration loading junit --- .../org/skriptlang/skript/test/tests/files/FilesGenerate.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/skriptlang/skript/test/tests/files/FilesGenerate.java b/src/test/java/org/skriptlang/skript/test/tests/files/FilesGenerate.java index a6bcabcea71..2cb83e29ae1 100644 --- a/src/test/java/org/skriptlang/skript/test/tests/files/FilesGenerate.java +++ b/src/test/java/org/skriptlang/skript/test/tests/files/FilesGenerate.java @@ -41,6 +41,7 @@ public void checkFiles() { assertTrue(skript.getScriptsFolder().isDirectory()); assertTrue(new File(dataFolder, "config.sk").exists()); assertTrue(new File(dataFolder, "features.sk").exists()); + assertTrue(new File(dataFolder, "custom-events.sk").exists()); File lang = new File(dataFolder, "lang"); assertTrue(lang.exists()); assertTrue(lang.isDirectory()); From b352f68baa40ea052b33ed2f75cc7f1092672b1a Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Tue, 30 May 2023 19:51:03 -0600 Subject: [PATCH 3/7] Remove un-needed example --- src/main/resources/custom-events.sk | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/resources/custom-events.sk b/src/main/resources/custom-events.sk index 0f0248dbf04..5b1761a5f11 100644 --- a/src/main/resources/custom-events.sk +++ b/src/main/resources/custom-events.sk @@ -28,12 +28,6 @@ events: class: com.destroystokyo.paper.event.block.BeaconEffectEvent pattern: beacon effect apply [to player[s]] - # [on] [player] enter bed - player enter bed: - enabled: false - class: org.bukkit.event.player.PlayerBedEnterEvent - pattern: [player] enter bed - # [on] campfire lit campfire lit: enabled: false From 9a0eddff4d9862f28d87e854fb5aea8159a5592c Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Tue, 30 May 2023 19:52:18 -0600 Subject: [PATCH 4/7] Explain expressions --- src/main/resources/custom-events.sk | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/custom-events.sk b/src/main/resources/custom-events.sk index 5b1761a5f11..81a7d2c9c87 100644 --- a/src/main/resources/custom-events.sk +++ b/src/main/resources/custom-events.sk @@ -17,6 +17,7 @@ version: 1 # class - the class name to search for # pattern - the syntax pattern of the event # The optional pattern [on] gets prefixed on every Skript event. +# Pattern cannot have expressions inside currently. # All event-values will be automatically applied. # If you need more info, make an issue report at https://github.com/SkriptLang/Skript/issues From 0b9bc0c11fb37de129bc2d9dd478ec30c05b1417 Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Mon, 21 Aug 2023 14:44:20 -0600 Subject: [PATCH 5/7] Apply changes --- src/main/java/ch/njol/skript/CustomEvents.java | 4 +--- src/main/resources/custom-events.sk | 6 ++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/CustomEvents.java b/src/main/java/ch/njol/skript/CustomEvents.java index 77ffad3379f..e63e558e57a 100644 --- a/src/main/java/ch/njol/skript/CustomEvents.java +++ b/src/main/java/ch/njol/skript/CustomEvents.java @@ -84,7 +84,7 @@ public static void load(File file) { if (configuration != null) { configuration.load(CustomEvents.class); // When we need to change values in the future of this file, bump the version node. - if (configuration.get("version").isEmpty() || !configuration.get("version").equalsIgnoreCase("1")) { + if (configuration.get("version", "").isEmpty() || !configuration.get("version", "").equalsIgnoreCase("1")) { Skript.warning("Your custom-events.sk config file is outdated. " + "Backup any changes you've made, and delete your custom-events.sk in the Skript folder to update it. " + "After, re-add any nodes you've changed."); @@ -131,8 +131,6 @@ public static void load(File file) { } continue; } - if (event == null) - continue; if (!(Event.class.isAssignableFrom(event))) { Skript.error("Class '" + eventClass + "' for event node '" + name + "' was not a valid Event class!"); continue; diff --git a/src/main/resources/custom-events.sk b/src/main/resources/custom-events.sk index 81a7d2c9c87..33a27c226b2 100644 --- a/src/main/resources/custom-events.sk +++ b/src/main/resources/custom-events.sk @@ -18,7 +18,7 @@ version: 1 # pattern - the syntax pattern of the event # The optional pattern [on] gets prefixed on every Skript event. # Pattern cannot have expressions inside currently. -# All event-values will be automatically applied. +# event-values of subclass events will be automatically applied. # If you need more info, make an issue report at https://github.com/SkriptLang/Skript/issues # Examples are placed below. The 'enabled' node is not required, it's just to disable examples. @@ -26,11 +26,13 @@ events: # [on] beacon effect apply beacon effect: enabled: false + description: "Called when entities receive potion effects from a beacon" class: com.destroystokyo.paper.event.block.BeaconEffectEvent pattern: beacon effect apply [to player[s]] # [on] campfire lit campfire lit: enabled: false + description: "Called when a campfire starts cooking" class: org.bukkit.event.block.CampfireStartEvent - pattern: campfire (lit|start [burning]) + pattern: campfire start[ing] cooking From 9ebb99c31ebfe9b3214542cc7aabcbb8ef841670 Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Mon, 21 Aug 2023 16:18:51 -0600 Subject: [PATCH 6/7] Apply changes --- src/main/java/ch/njol/skript/CustomEvents.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/njol/skript/CustomEvents.java b/src/main/java/ch/njol/skript/CustomEvents.java index e63e558e57a..3963721e923 100644 --- a/src/main/java/ch/njol/skript/CustomEvents.java +++ b/src/main/java/ch/njol/skript/CustomEvents.java @@ -31,8 +31,6 @@ import ch.njol.skript.config.OptionSection; import ch.njol.skript.config.SectionNode; import ch.njol.skript.lang.util.SimpleEvent; -import ch.njol.skript.log.ParseLogHandler; -import ch.njol.skript.log.RetainingLogHandler; import ch.njol.skript.log.SkriptLogger; import ch.njol.skript.util.FileUtils; @@ -84,7 +82,8 @@ public static void load(File file) { if (configuration != null) { configuration.load(CustomEvents.class); // When we need to change values in the future of this file, bump the version node. - if (configuration.get("version", "").isEmpty() || !configuration.get("version", "").equalsIgnoreCase("1")) { + String version = configuration.get("version"); + if (version != null && version.isEmpty() || !version.equalsIgnoreCase("1")) { Skript.warning("Your custom-events.sk config file is outdated. " + "Backup any changes you've made, and delete your custom-events.sk in the Skript folder to update it. " + "After, re-add any nodes you've changed."); @@ -93,11 +92,15 @@ public static void load(File file) { if (section != null && !section.isEmpty()) { for (Node node : section) { if (!(node instanceof SectionNode)) { - Skript.warning("Event node '" + node.getKey() + "' was not a section, ignoring."); + Skript.error("Event node '" + node.getKey() + "' was not a section, ignoring."); continue; } SectionNode eventSection = (SectionNode) node; String name = node.getKey(); + if (name.isEmpty()) { + Skript.error("Event node '" + node.getKey() + "' is empty/blank."); + continue; + } String enabled = eventSection.get("enabled", "true"); if (enabled.equalsIgnoreCase("false")) continue; From cbf6a6f9a456be86e7e6fc73a3933f4ae44e0982 Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Sat, 9 Sep 2023 04:42:56 -0600 Subject: [PATCH 7/7] Apply changes --- src/main/resources/custom-events.sk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/custom-events.sk b/src/main/resources/custom-events.sk index 33a27c226b2..f617e9a8ade 100644 --- a/src/main/resources/custom-events.sk +++ b/src/main/resources/custom-events.sk @@ -30,8 +30,8 @@ events: class: com.destroystokyo.paper.event.block.BeaconEffectEvent pattern: beacon effect apply [to player[s]] - # [on] campfire lit - campfire lit: + # [on] campfire start cooking + campfire cooking: enabled: false description: "Called when a campfire starts cooking" class: org.bukkit.event.block.CampfireStartEvent