Skip to content

Add an AvailableEvent(s) annotation #7668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ed0a7c1
Add annotation
Nuutrai Mar 2, 2025
2857590
Add annotation to needed expressions
Nuutrai Mar 2, 2025
5e909b3
Add annotation
Nuutrai Mar 2, 2025
29f90ec
Add annotation to needed expressions
Nuutrai Mar 2, 2025
56d6871
Parsing stack (#5277) (#7534)
Moderocky Jan 27, 2025
2c9571e
String -> UUID (#7497)
Burbulinis Feb 23, 2025
d4f3564
More changes
Nuutrai Mar 2, 2025
4326b22
Merge remote-tracking branch 'origin/docs' into docs
Nuutrai Mar 2, 2025
09ca234
Revert "String -> UUID (#7497)"
Nuutrai Mar 2, 2025
2608e78
Revert "Parsing stack (#5277) (#7534)"
Nuutrai Mar 2, 2025
4d61948
Adds a little documentation to the annotations
Nuutrai Mar 2, 2025
79e4db5
Correct list annotations
Nuutrai Mar 2, 2025
cb92666
Fixing a fix
Nuutrai Mar 2, 2025
7ae9fe1
Deprecate Events
Nuutrai Mar 2, 2025
81d2c47
Revert stackable AvailableEvent change
Nuutrai Mar 3, 2025
d650ca3
Oops
Nuutrai Mar 3, 2025
f31560d
Update src/main/java/ch/njol/skript/doc/AvailableEvents.java
Nuutrai Mar 4, 2025
e3d0b86
Good stuff, mhm
Nuutrai Mar 8, 2025
019e4d3
More good stuff
Nuutrai Mar 8, 2025
18881ed
Documentation Id if it exists and is needed
Nuutrai Mar 8, 2025
e9a41b2
Update src/main/java/ch/njol/skript/doc/JSONGenerator.java
Nuutrai Mar 8, 2025
06f166f
Moved JsonArray generator to a helper
Nuutrai Mar 8, 2025
e27c96f
Merge remote-tracking branch 'origin/docs' into docs
Nuutrai Mar 8, 2025
8b8dc44
Apply suggestions from code review
Nuutrai Mar 11, 2025
c81f8cf
Update JSON_VERSION to 2.0
Nuutrai Mar 11, 2025
1465585
Correct AvailableEvents for ExprCommand and ExprCommandSender
Nuutrai Mar 11, 2025
f877e60
Merge branch 'dev/feature' into docs
Efnilite Mar 13, 2025
dd02e4c
Switch #cacheEvents() to static loading
Nuutrai Mar 13, 2025
f4a7e42
Apply suggestions from code review
Nuutrai Mar 13, 2025
7b3216c
Merge branch 'dev/feature' into docs
Efnilite Mar 22, 2025
7cc725f
Merge branch 'dev/feature' into docs
Nuutrai Mar 22, 2025
a985dda
Apply half suggestions from code review
Nuutrai Jun 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/ch/njol/skript/doc/AvailableEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ch.njol.skript.doc;

import org.bukkit.event.Event;

import java.lang.annotation.*;

/**
* Provides a list of {@link org.bukkit.event.Event}s that this syntax element can be used in.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AvailableEvents {

Class<? extends Event>[] value();

}
5 changes: 5 additions & 0 deletions src/main/java/ch/njol/skript/doc/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@

/**
* @author Peter Güttinger
*
* @deprecated
* This annotation is deprecated, please use {@link AvailableEvents} instead
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Deprecated(since = "INSERT VERSION")
public @interface Events {
/**
* A list of {@link SkriptEventInfo#getName() name(s)} of {@link SkriptEvent events} this expression is useful for.
Expand Down
38 changes: 35 additions & 3 deletions src/main/java/ch/njol/skript/doc/JSONGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,24 @@ public class JSONGenerator extends DocumentationGenerator {
/**
* The current version of the JSON generator
*/
public static final Version JSON_VERSION = new Version(1, 0);
public static final Version JSON_VERSION = new Version(2, 0);

private static final Gson GSON = new GsonBuilder()
.disableHtmlEscaping()
.setPrettyPrinting()
.serializeNulls()
.create();

private static final Map<Class<? extends Event>, List<SkriptEventInfo<?>>> events = new HashMap<>();

public JSONGenerator(File templateDir, File outputDir) {
super(templateDir, outputDir);
}

static {
cacheEvents();
}

/**
* @return The version of the JSON generator
*/
Expand Down Expand Up @@ -117,8 +123,9 @@ private static JsonObject generatedAnnotatedElement(SyntaxElementInfo<?> syntaxI
syntaxJsonObject.add("examples", null);
}

Events events = syntaxClass.getAnnotation(Events.class);
syntaxJsonObject.add("events", events == null ? null : convertToJsonArray(events.value()));
AvailableEvents availableEvents = syntaxClass.getAnnotation(AvailableEvents.class);
syntaxJsonObject.add("events", getSkriptEvents(availableEvents));


RequiredPlugins requirements = syntaxClass.getAnnotation(RequiredPlugins.class);
syntaxJsonObject.add("requirements", requirements == null ? null : convertToJsonArray(requirements.value()));
Expand All @@ -129,6 +136,23 @@ private static JsonObject generatedAnnotatedElement(SyntaxElementInfo<?> syntaxI
return syntaxJsonObject;
}

private static @Nullable JsonArray getSkriptEvents(@Nullable AvailableEvents availableEvents) {
if (availableEvents == null || availableEvents.value().length == 0)
return null;
JsonArray skriptEvents = new JsonArray();
for (Class<? extends Event> event : availableEvents.value()) {
if (events.get(event) == null)
continue;
for (SkriptEventInfo<?> skriptEvent : events.get(event)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible that two Bukkit events will resolve to the same SkriptEventInfo. Is that something we need to be checking?

JsonObject skriptEventJson = new JsonObject();
skriptEventJson.addProperty("id", skriptEvent.getDocumentationID() == null ? skriptEvent.getId() : skriptEvent.getDocumentationID());
skriptEventJson.addProperty("name", skriptEvent.getName());
skriptEvents.add(skriptEventJson);
}
}
return skriptEvents;
}

/**
* Generates the documentation JsonObject for an event
*
Expand Down Expand Up @@ -372,6 +396,14 @@ private static JsonArray cleanPatterns(String... strings) {
return convertToJsonArray(strings);
}

private static void cacheEvents() {
for (SkriptEventInfo<?> eventInfo : Skript.getEvents()) {
for (Class<? extends Event> event : eventInfo.events) {
events.computeIfAbsent(event, key -> new ArrayList<>()).add(eventInfo);
}
}
}

/**
* Writes the documentation JsonObject to an output path
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
import java.util.Iterator;
import java.util.List;

import ch.njol.skript.doc.*;
import org.bukkit.block.BlockState;
import org.bukkit.event.Event;
import org.bukkit.event.block.SpongeAbsorbEvent;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand All @@ -24,6 +20,7 @@

@Name("Absorbed blocks")
@Description("The blocks absorbed by a sponge block.")
@AvailableEvents(SpongeAbsorbEvent.class)
@Events("sponge absorb")
@Examples("the absorbed blocks")
@Since("2.5")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"\telse if applied effect = secondary effect:",
"\t\tbroadcast \"Is Secondary\""
})
@AvailableEvents(BeaconEffectEvent.class)
@Events("Beacon Effect")
@RequiredPlugins("Paper")
@Since("2.10")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package ch.njol.skript.expressions;

import ch.njol.skript.doc.*;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.Event;
import org.bukkit.event.enchantment.EnchantItemEvent;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand All @@ -26,6 +22,7 @@
" Deleting or removing the applied enchantments will prevent the item's enchantment."})
@Examples({"on enchant:",
"\tset the applied enchantments to sharpness 10 and fire aspect 5"})
@AvailableEvents(EnchantItemEvent.class)
@Events("enchant")
@Since("2.5")
public class ExprAppliedEnchantments extends SimpleExpression<EnchantmentType> {
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/ch/njol/skript/expressions/ExprAttacked.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Array;

import ch.njol.skript.doc.*;
import ch.njol.skript.lang.EventRestrictedSyntax;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.Entity;
Expand All @@ -16,11 +17,6 @@
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
Expand All @@ -37,6 +33,8 @@
"\tvictim is a creeper",
"\tdamage the attacked by 1 heart"})
@Since("1.3, 2.6.1 (projectile hit event)")
@AvailableEvents({EntityDamageEvent.class, EntityDeathEvent.class,
VehicleDamageEvent.class, VehicleDestroyEvent.class, ProjectileHitEvent.class})
@Events({"damage", "death", "projectile hit"})
public class ExprAttacked extends SimpleExpression<Entity> implements EventRestrictedSyntax {

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/ch/njol/skript/expressions/ExprAttacker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.njol.skript.expressions;

import ch.njol.skript.doc.*;
import ch.njol.skript.lang.EventRestrictedSyntax;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.Entity;
Expand All @@ -13,11 +14,6 @@
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand All @@ -37,6 +33,8 @@
" health of attacker is less than or equal to 2",
" damage victim by 1 heart"})
@Since("1.3")
@AvailableEvents({EntityDamageEvent.class, EntityDeathEvent.class,
VehicleDamageEvent.class, VehicleDestroyEvent.class})
@Events({"damage", "death", "destroy"})
public class ExprAttacker extends SimpleExpression<Entity> implements EventRestrictedSyntax {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"add 1 to range of {_block}"
})
@RequiredPlugins("Paper (range)")
@AvailableEvents({PlayerChangeBeaconEffectEvent.class, BeaconEffectEvent.class, BeaconActivatedEvent.class, BeaconDeactivatedEvent.class})
@Events({"Beacon Effect", "Beacon Toggle", "Beacon Change Effect"})
@Since("2.10")
public class ExprBeaconValues extends PropertyExpression<Block, Object> {
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/ch/njol/skript/expressions/ExprClicked.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Array;

import ch.njol.skript.doc.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
Expand All @@ -17,11 +18,6 @@

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
Expand All @@ -41,6 +37,7 @@
"\tshow the inventory of the clicked block to the player"
})
@Since("1.0, 2.2-dev35 (more clickable things)")
@AvailableEvents({PlayerInteractEntityEvent.class, PlayerInteractAtEntityEvent.class, InventoryClickEvent.class, EnchantItemEvent.class, PlayerInteractEvent.class})
@Events({"click", "inventory click"})
public class ExprClicked extends SimpleExpression<Object> {

Expand Down
7 changes: 2 additions & 5 deletions src/main/java/ch/njol/skript/expressions/ExprCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.njol.skript.expressions;

import ch.njol.skript.command.ScriptCommandEvent;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.EventRestrictedSyntax;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
Expand All @@ -9,11 +10,6 @@
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand All @@ -32,6 +28,7 @@
"\t\t\tmessage \"You're not allowed to use commands during the game\"",
"\t\t\tcancel the event"})
@Since("2.0, 2.7 (support for script commands)")
@AvailableEvents({PlayerCommandPreprocessEvent.class, ServerCommandEvent.class, ScriptCommandEvent.class})
@Events("command")
public class ExprCommand extends SimpleExpression<String> implements EventRestrictedSyntax {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package ch.njol.skript.expressions;

import ch.njol.skript.command.ScriptCommandEvent;
import ch.njol.skript.doc.*;
import org.bukkit.command.CommandSender;

import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.EventValueExpression;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;

@Name("Command Sender")
@Description({
Expand All @@ -21,6 +20,7 @@
"\tlog \"%executor% used command /%command% %arguments%\" to \"commands.log\""
})
@Since("2.0")
@AvailableEvents({PlayerCommandPreprocessEvent.class, ServerCommandEvent.class, ScriptCommandEvent.class})
@Events("command")
public class ExprCommandSender extends EventValueExpression<CommandSender> {

Expand Down
7 changes: 2 additions & 5 deletions src/main/java/ch/njol/skript/expressions/ExprDamage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.njol.skript.expressions;

import ch.njol.skript.doc.*;
import org.bukkit.event.Event;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
Expand All @@ -9,11 +10,6 @@
import ch.njol.skript.Skript;
import ch.njol.skript.bukkitutil.HealthUtils;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand All @@ -36,6 +32,7 @@
"\tincrease the damage by 2"
})
@Since("1.3.5, 2.8.0 (item damage event)")
@AvailableEvents({EntityDamageEvent.class, VehicleDamageEvent.class, PlayerItemDamageEvent.class})
@Events({"Damage", "Vehicle Damage", "Item Damage"})
public class ExprDamage extends SimpleExpression<Number> {

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/ch/njol/skript/expressions/ExprEgg.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package ch.njol.skript.expressions;

import ch.njol.skript.doc.*;
import org.bukkit.entity.Egg;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.EventValueExpression;

@Name("The Egg")
@Description("The egg thrown in a Player Egg Throw event.")
@Examples("spawn an egg at the egg")
@AvailableEvents(PlayerEggThrowEvent.class)
@Events("Egg Throw")
@Since("2.7")
public class ExprEgg extends EventValueExpression<Egg> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.njol.skript.expressions;

import ch.njol.skript.doc.*;
import org.bukkit.event.Event;
import org.bukkit.event.enchantment.EnchantItemEvent;
import org.bukkit.event.enchantment.PrepareItemEnchantEvent;
Expand All @@ -8,11 +9,6 @@
import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand All @@ -28,6 +24,7 @@
"\tset the enchanted item to a diamond chestplate",
"on enchant prepare:",
"\tset the enchant item to a wooden sword"})
@AvailableEvents({EnchantItemEvent.class, PrepareItemEnchantEvent.class})
@Events({"enchant prepare", "enchant"})
@Since("2.5")
public class ExprEnchantItem extends SimpleExpression<ItemType> {
Expand Down
Loading