Skip to content

Commit 6be278e

Browse files
authored
Type Properties/Attributes (#8165)
* basic implementations for Contains and Named properties * fix rogue find+replace * cleanup and move lookupPropertyInfo to override of get(Class) * Remove necessary usage of toHandlerType * add base handler class * Improve property resolution, add back-compat for AnyNamed, AnyContains, start parse-time checks for Contains * cleaning and reorganization * implement common module * Cleanup, fully implement ExprName, many docs, some other improvements - allows stateful property handlers - removes use of AnyNamed - removes NameHandler in favor of just using ExpressionPropertyHandler - Avoid exposing silly cast to users when possible in PropertyBaseExpression - fix issue with handling DELETE and RESET changers in PropertyBaseExpression - add support for default toString impl in PropertyBaseExpression * re-add type in register() and Script#name() * implement documents plus a bit more cleanup * cleanup entity/player changer usage * add property info itself to related syntaxes docs * Update name's description * update docs, add since * small fixes * Replace AnyAmount/ExprAmount first pass breaking change, removes `size of itemstack`. * Finish replacing AnyAmount, add Empty property, add PropertyBaseSyntax, add condition base class * banish utils class * AnyValued replacement * better errors * some docs * fix PropCondContains with somewhat horrible code that works * final cleanup * final cleanup 2 * Apply suggestions from code review more cleanup Co-authored-by: SirSmurfy2 <[email protected]> * reorder methods + missing continue * Update PropExprValueOf.java * Update JSONGenerator.java * Apply suggestions from code review Co-authored-by: SirSmurfy2 <[email protected]> * requested changes * add config option to enable type properties * Update ExprName.java * Use ChangeInPlace when changing property expressions * Update JSONGenerator.java * Update JSONGenerator.java * better CIP requirement detection * improve error message for missing properties * requested changes
1 parent c22ee9e commit 6be278e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5061
-1331
lines changed

src/main/java/ch/njol/skript/Skript.java

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,30 @@
33
import ch.njol.skript.aliases.Aliases;
44
import ch.njol.skript.bukkitutil.BurgerHelper;
55
import ch.njol.skript.classes.ClassInfo;
6-
import ch.njol.skript.classes.data.BukkitClasses;
7-
import ch.njol.skript.classes.data.BukkitEventValues;
8-
import ch.njol.skript.classes.data.DefaultComparators;
9-
import ch.njol.skript.classes.data.DefaultConverters;
10-
import ch.njol.skript.classes.data.DefaultFunctions;
11-
import ch.njol.skript.classes.data.DefaultOperations;
12-
import ch.njol.skript.classes.data.JavaClasses;
13-
import ch.njol.skript.classes.data.SkriptClasses;
6+
import ch.njol.skript.classes.data.*;
147
import ch.njol.skript.command.Commands;
158
import ch.njol.skript.doc.Documentation;
169
import ch.njol.skript.events.EvtSkript;
1710
import ch.njol.skript.expressions.arithmetic.ExprArithmetic;
1811
import ch.njol.skript.hooks.Hook;
1912
import ch.njol.skript.lang.*;
13+
import ch.njol.skript.lang.Effect;
2014
import ch.njol.skript.lang.Condition.ConditionType;
2115
import ch.njol.skript.lang.util.SimpleExpression;
2216
import ch.njol.skript.localization.Language;
2317
import ch.njol.skript.localization.Message;
2418
import ch.njol.skript.localization.PluralizingArgsMessage;
25-
import ch.njol.skript.log.BukkitLoggerFilter;
26-
import ch.njol.skript.log.CountingLogHandler;
27-
import ch.njol.skript.log.ErrorDescLogHandler;
28-
import ch.njol.skript.log.ErrorQuality;
29-
import ch.njol.skript.log.LogEntry;
30-
import ch.njol.skript.log.LogHandler;
31-
import ch.njol.skript.log.SkriptLogger;
32-
import ch.njol.skript.log.TestingLogHandler;
33-
import ch.njol.skript.log.Verbosity;
19+
import ch.njol.skript.log.*;
3420
import ch.njol.skript.registrations.Classes;
3521
import ch.njol.skript.registrations.EventValues;
3622
import ch.njol.skript.registrations.Feature;
37-
import ch.njol.skript.test.runner.EffObjectives;
38-
import ch.njol.skript.test.runner.SkriptAsyncJUnitTest;
39-
import ch.njol.skript.test.runner.SkriptJUnitTest;
40-
import ch.njol.skript.test.runner.SkriptTestEvent;
41-
import ch.njol.skript.test.runner.TestMode;
42-
import ch.njol.skript.test.runner.TestTracker;
23+
import ch.njol.skript.test.runner.*;
4324
import ch.njol.skript.timings.SkriptTimings;
4425
import ch.njol.skript.update.ReleaseManifest;
4526
import ch.njol.skript.update.ReleaseStatus;
4627
import ch.njol.skript.update.UpdateManifest;
4728
import ch.njol.skript.util.Date;
48-
import ch.njol.skript.util.EmptyStacktraceException;
49-
import ch.njol.skript.util.ExceptionUtils;
50-
import ch.njol.skript.util.FileUtils;
51-
import ch.njol.skript.util.Task;
52-
import ch.njol.skript.util.Utils;
53-
import ch.njol.skript.util.Version;
29+
import ch.njol.skript.util.*;
5430
import ch.njol.skript.util.chat.BungeeConverter;
5531
import ch.njol.skript.util.chat.ChatMessages;
5632
import ch.njol.skript.variables.Variables;
@@ -63,11 +39,7 @@
6339
import com.google.gson.GsonBuilder;
6440
import io.papermc.lib.PaperLib;
6541
import org.bstats.bukkit.Metrics;
66-
import org.bukkit.Bukkit;
67-
import org.bukkit.ChatColor;
68-
import org.bukkit.Material;
69-
import org.bukkit.OfflinePlayer;
70-
import org.bukkit.Server;
42+
import org.bukkit.*;
7143
import org.bukkit.command.CommandSender;
7244
import org.bukkit.command.PluginCommand;
7345
import org.bukkit.entity.Player;
@@ -104,12 +76,15 @@
10476
import org.skriptlang.skript.bukkit.registration.BukkitRegistryKeys;
10577
import org.skriptlang.skript.bukkit.registration.BukkitSyntaxInfos;
10678
import org.skriptlang.skript.bukkit.tags.TagModule;
79+
import org.skriptlang.skript.common.CommonModule;
10780
import org.skriptlang.skript.lang.comparator.Comparator;
10881
import org.skriptlang.skript.lang.comparator.Comparators;
10982
import org.skriptlang.skript.lang.converter.Converter;
11083
import org.skriptlang.skript.lang.converter.Converters;
11184
import org.skriptlang.skript.lang.entry.EntryValidator;
11285
import org.skriptlang.skript.lang.experiment.ExperimentRegistry;
86+
import org.skriptlang.skript.lang.properties.Property;
87+
import org.skriptlang.skript.lang.properties.PropertyRegistry;
11388
import org.skriptlang.skript.lang.script.Script;
11489
import org.skriptlang.skript.lang.structure.Structure;
11590
import org.skriptlang.skript.lang.structure.StructureInfo;
@@ -502,6 +477,9 @@ public void onEnable() {
502477
experimentRegistry = new ExperimentRegistry(this);
503478
Feature.registerAll(getAddonInstance(), experimentRegistry);
504479

480+
skript.storeRegistry(PropertyRegistry.class, new PropertyRegistry(this));
481+
Property.registerDefaultProperties();
482+
505483
// Load classes which are always safe to use
506484
new JavaClasses(); // These may be needed in configuration
507485

@@ -590,10 +568,11 @@ public void onEnable() {
590568
FurnaceModule.load();
591569
LootTableModule.load();
592570
skript.loadModules(
593-
new DamageSourceModule(),
594-
new ItemComponentModule(),
595-
new BrewingModule()
596-
);
571+
new DamageSourceModule(),
572+
new ItemComponentModule(),
573+
new BrewingModule(),
574+
new CommonModule()
575+
);
597576
} catch (final Exception e) {
598577
exception(e, "Could not load required .class files: " + e.getLocalizedMessage());
599578
setEnabled(false);

src/main/java/ch/njol/skript/SkriptConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ public static String formatDate(final long timestamp) {
294294
})
295295
.optional(true);
296296

297+
public static final Option<Boolean> useTypeProperties = new Option<>("use type properties", false)
298+
.optional(true);
299+
297300
public static final Option<Boolean> allowUnsafePlatforms = new Option<>("allow unsafe platforms", false)
298301
.optional(true);
299302

src/main/java/ch/njol/skript/classes/AnyInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
* These auto-generate their user patterns (e.g. {@code named} -> {@code any named thing}).
1313
*
1414
* @see AnyProvider
15+
* @deprecated Use {@link org.skriptlang.skript.lang.properties.Property} instead.
1516
*/
17+
@Deprecated(since="INSERT VERSION", forRemoval = true)
1618
public class AnyInfo<Type extends AnyProvider> extends ClassInfo<Type> {
1719

1820
/**

src/main/java/ch/njol/skript/classes/ClassInfo.java

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
import ch.njol.skript.lang.DefaultExpression;
77
import ch.njol.skript.lang.util.SimpleLiteral;
88
import ch.njol.skript.localization.Noun;
9+
import ch.njol.skript.registrations.Classes;
910
import ch.njol.util.coll.iterator.ArrayIterator;
1011
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1112
import org.bukkit.event.Event;
13+
import org.jetbrains.annotations.ApiStatus;
1214
import org.jetbrains.annotations.NotNull;
1315
import org.jetbrains.annotations.Nullable;
16+
import org.jetbrains.annotations.Unmodifiable;
17+
import org.skriptlang.skript.addon.SkriptAddon;
18+
import org.skriptlang.skript.lang.properties.Property;
19+
import org.skriptlang.skript.lang.properties.Property.PropertyInfo;
20+
import org.skriptlang.skript.lang.properties.PropertyHandler;
1421

15-
import java.util.Arrays;
16-
import java.util.HashSet;
17-
import java.util.Iterator;
18-
import java.util.Set;
22+
import java.util.*;
1923
import java.util.function.Supplier;
2024
import java.util.regex.Pattern;
2125
import java.util.regex.PatternSyntaxException;
@@ -476,4 +480,74 @@ public String toString(final @Nullable Event event, final boolean debug) {
476480
return getName().getSingular();
477481
}
478482

483+
private final Map<Property<?>, PropertyInfo<?>> propertyInfos = new HashMap<>();
484+
private final Map<Property<?>, PropertyDocs> propertyDocumentation = new HashMap<>();
485+
486+
@ApiStatus.Experimental
487+
public record PropertyDocs(Property<?> property, String description, SkriptAddon provider) {}
488+
489+
/**
490+
* Registers this class as having the given property, using the given property handler.
491+
* @param property The property this class should have
492+
* @param description A short description of the property for documentation
493+
* @param handler The handler for this property
494+
* @return This ClassInfo object
495+
* @param <Handler> The type of the property handler
496+
* @throws IllegalStateException If this property is already registered for this class
497+
*/
498+
@ApiStatus.Experimental
499+
public <Handler extends PropertyHandler<T>> ClassInfo<T> property(Property<? super Handler> property, String description, SkriptAddon addon, @NotNull Handler handler) {
500+
if (propertyInfos.containsKey(property)) {
501+
throw new IllegalStateException("Property " + property.name() + " is already registered for the " + c.getName() + " type.");
502+
}
503+
propertyInfos.put(property, new PropertyInfo<>(property, handler));
504+
Classes.hasProperty(property, this);
505+
propertyDocumentation.put(property, new PropertyDocs(property, description, addon));
506+
return this;
507+
}
508+
509+
/**
510+
* Checks whether this class already has the given property registered.
511+
* @param property The property to check
512+
* @return True if this class has the property, false otherwise
513+
*/
514+
@ApiStatus.Experimental
515+
public boolean hasProperty(Property<?> property) {
516+
return propertyInfos.containsKey(property);
517+
}
518+
519+
/**
520+
* @return An unmodifiable collection of all the properties this class has.
521+
*/
522+
@ApiStatus.Experimental
523+
public @Unmodifiable Collection<Property<?>> getAllProperties() {
524+
return Collections.unmodifiableCollection(propertyInfos.keySet());
525+
}
526+
527+
/**
528+
* Gets the property info for the given property, or null if this class does not have the property.
529+
* @param property The property to get the info for
530+
* @return The property info, or null if this class does not have the property
531+
* @param <Handler> The type of the property handler
532+
*/
533+
@ApiStatus.Experimental
534+
public <Handler extends PropertyHandler<?>> @Nullable PropertyInfo<Handler> getPropertyInfo(Property<Handler> property) {
535+
if (!propertyInfos.containsKey(property)) {
536+
return null;
537+
}
538+
//noinspection unchecked
539+
return (PropertyInfo<Handler>) propertyInfos.get(property);
540+
}
541+
542+
/**
543+
* Gets the type-specific documentation for the given property, or null if this type does not have the property.
544+
* Meant to be used for documentation.
545+
* @param property The property to get the documentation for
546+
* @return The documentation, or null if this type does not have the property
547+
*/
548+
@ApiStatus.Experimental
549+
public PropertyDocs getPropertyDocumentation(Property<?> property) {
550+
return propertyDocumentation.get(property);
551+
}
552+
479553
}

0 commit comments

Comments
 (0)