Skip to content

Commit

Permalink
✨ Introduce sections support with new Skript's API and support legacy…
Browse files Browse the repository at this point in the history
… sections
  • Loading branch information
Romitou committed Mar 5, 2022
1 parent 7867179 commit 395509c
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.skript.log.*;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nonnull;
import java.lang.reflect.Field;
Expand All @@ -20,9 +21,9 @@
* @author Tuke_Nuke on 29/03/2017
* @author Romitou on 25/04/2021
*/
public abstract class EffectSection extends Condition {
public abstract class LegacyEffectSection extends Condition {

public static final HashMap<Class<? extends EffectSection>, EffectSection> effectSections = new HashMap<>();
public static final HashMap<Class<? extends LegacyEffectSection>, LegacyEffectSection> effectSections = new HashMap<>();
private static final Boolean IS_USING_NEW_PARSER = isUsingNewParser();
private final Node node;
private SectionNode sectionNode;
Expand All @@ -34,7 +35,7 @@ public abstract class EffectSection extends Condition {
* To use sections, your class must extend EffectSection.
*/
@SuppressWarnings("unchecked")
public EffectSection() {
public LegacyEffectSection() {
// Get the current node.
this.node = SkriptLogger.getNode();

Expand Down Expand Up @@ -82,7 +83,7 @@ public EffectSection() {
*/
@Nonnull
@SafeVarargs
public static Boolean isCurrentSection(Class<? extends EffectSection>... classes) {
public static Boolean isCurrentSection(Class<? extends LegacyEffectSection>... classes) {
return getCurrentSection(classes).isPresent();
}

Expand All @@ -94,25 +95,20 @@ public static Boolean isCurrentSection(Class<? extends EffectSection>... classes
*/
@Nonnull
@SafeVarargs
public static Optional<EffectSection> getCurrentSection(Class<? extends EffectSection>... classes) {
public static Optional<LegacyEffectSection> getCurrentSection(Class<? extends LegacyEffectSection>... classes) {
return Arrays.stream(classes)
.filter(EffectSection.effectSections::containsKey)
.map(EffectSection.effectSections::get)
.filter(LegacyEffectSection.effectSections::containsKey)
.map(LegacyEffectSection.effectSections::get)
.findFirst();
}

public static Boolean isUsingNewParser() {
try {
ParserInstance.class.getDeclaredMethod("get");
return true;
} catch (NoSuchMethodException ignored) {
return false;
}
public static @NotNull Boolean isUsingNewParser() {
return MongoSK.isUsingNewParser();
}

/**
* Initializes the section's trigger, so that the section can then be executed.
* It is mandatory to initialise it first before using {@link EffectSection#runSection(Event)}.
* It is mandatory to initialise it first before using {@link LegacyEffectSection#runSection(Event)}.
*/
protected void loadSection() {
// If section node is null, this means that a trigger has already been created. Aborting.
Expand All @@ -137,7 +133,7 @@ protected TriggerItem walk(@Nonnull Event e) {
@Override
public String toString(Event e, boolean debug) {
// Call the Debuggable#toString method of EffectSection (and not of the TriggerSection).
return EffectSection.this.toString(e, debug);
return LegacyEffectSection.this.toString(e, debug);
}
};

Expand Down Expand Up @@ -235,7 +231,7 @@ public boolean check(@Nonnull Event e) {

/**
* Executes the section, with the event passed as a parameter.
* It is mandatory to have initialized the section first, via {@link EffectSection#loadSection()}.
* It is mandatory to have initialized the section first, via {@link LegacyEffectSection#loadSection()}.
*
* @param e The event
*/
Expand Down Expand Up @@ -271,7 +267,7 @@ protected Boolean hasSection() {
}

/**
* Checks that the {@link EffectSection#loadSection()} method has been called beforehand.
* Checks that the {@link LegacyEffectSection#loadSection()} method has been called beforehand.
*
* @return Whether the section is initialized
*/
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/fr/romitou/mongosk/MongoSK.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon;
import ch.njol.skript.lang.parser.ParserInstance;
import com.mongodb.reactivestreams.client.MongoClient;
import fr.romitou.mongosk.adapters.MongoSKAdapter;
import fr.romitou.mongosk.elements.MongoSKServer;
import fr.romitou.mongosk.skript.SkriptTypes;
import org.bukkit.event.Event;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -46,7 +49,11 @@ public void onEnable() {
LoggerHelper.info("Registration of the MongoSK syntaxes...");
try {
SkriptAddon skriptAddon = Skript.registerAddon(this);
skriptAddon.loadClasses("fr.romitou.mongosk.skript");
skriptAddon.loadClasses(
"fr.romitou.mongosk.skript",
"conditions", "effects", "events", "expressions", (isUsingNewSections() ? "sections" : "legacySections")
);
new SkriptTypes();
} catch (IOException e) {
LoggerHelper.severe("MongoSK could not load and register some syntax elements.",
"Try to update your version of Skript and MongoSK, and try again only with these two plugins.",
Expand Down Expand Up @@ -107,6 +114,24 @@ public static void registerServer(MongoSKServer mongoSKServer) {
mongoSKServers.add(mongoSKServer);
}

public static @NotNull Boolean isUsingNewParser() {
try {
ParserInstance.class.getDeclaredMethod("get");
return true;
} catch (NoSuchMethodException ignored) {
return false;
}
}

public static @NotNull Boolean isUsingNewSections() {
try {
Class.forName("ch.njol.skript.lang.Section");
return true;
} catch (ClassNotFoundException ignored) {
return false;
}
}

public static MongoSK getInstance() {
return instance;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/romitou/mongosk/MongoSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @param <T> The value associated with this section
*/
public abstract class MongoSection<T> extends EffectSection {
public abstract class MongoSection<T> extends LegacyEffectSection {

private Variable<?> variable;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package fr.romitou.mongosk.skript.legacySections;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
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.SkriptParser;
import ch.njol.skript.lang.Variable;
import ch.njol.util.Kleenean;
import fr.romitou.mongosk.LoggerHelper;
import fr.romitou.mongosk.MongoSK;
import fr.romitou.mongosk.MongoSection;
import fr.romitou.mongosk.elements.MongoSKDocument;
import org.bukkit.event.Event;

import javax.annotation.Nonnull;

@Name("Mongo section document")
@Description("This section can be used to create documents quickly and easily. " +
"To know how to use this section completely, see the \"Mongo section document value\" effect. " +
"Please note: you must specify a variable that will be affected by this section, as in the example.")
@Examples({"set {_doc} to a new mongo document with:",
"\tmongo \"key\": \"value\"",
"send {_doc}"})
@Since("2.1.0")
public class SectLegacyMongoDocument extends MongoSection<MongoSKDocument> {

public static MongoSKDocument mongoSKDocument = new MongoSKDocument();

static {
if (!MongoSK.isUsingNewSections())
Skript.registerCondition(
SectLegacyMongoDocument.class,
"set %object% to [a] new mongo[(db|sk)] document with"
);
}

@Override
public boolean init(@Nonnull Expression<?>[] exprs, int matchedPattern, @Nonnull Kleenean isDelayed, @Nonnull SkriptParser.ParseResult parseResult) {

if (isConditional()) {
LoggerHelper.severe("You cannot use conditions with this section.",
"Use the section like this: set {_variable} to a new mongo document with:");
return false;
}

Expression<?> variable = exprs[0];
if (!(variable instanceof Variable<?>)) {
LoggerHelper.severe("In order to use this section, you must provide a variable as the first argument.",
"Use the section like this: set {_variable} to a new mongo document with:");
return false;
}

loadSection();
setVariable((Variable<?>) variable);
return true;
}

@Override
protected MongoSKDocument provideValue() {
return mongoSKDocument;
}

@Override
protected void endSection() {
mongoSKDocument = new MongoSKDocument();
}


@Nonnull
@Override
public String toString(Event e, boolean debug) {
return "set " + getVariable().toString(e, debug) + " to a new mongo document with";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.romitou.mongosk.skript.effects;
package fr.romitou.mongosk.skript.legacySections;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
Expand All @@ -9,13 +9,12 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import fr.romitou.mongosk.EffectSection;
import fr.romitou.mongosk.LegacyEffectSection;
import fr.romitou.mongosk.MongoSK;
import fr.romitou.mongosk.adapters.MongoSKAdapter;
import fr.romitou.mongosk.skript.sections.SectMongoDocument;
import org.bukkit.event.Event;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand All @@ -31,10 +30,15 @@
"\tmongo \"nestedObject\": {_nested}",
"broadcast {_doc}'s mongo json"})
@Since("2.1.0")
public class EffMongoValue extends Effect {
public class SectLegacyMongoValue extends Effect {

static {
Skript.registerEffect(EffMongoValue.class, "mongo[(sk|db)] [(1¦(field|value)|2¦(array|list))] %string%: %objects%");
if (!MongoSK.isUsingNewSections()) {
Skript.registerEffect(
SectLegacyMongoValue.class,
"mongo[(sk|db)] [(1¦(field|value)|2¦(array|list))] %string%: %objects%"
);
}
}

private Expression<String> exprKey;
Expand All @@ -44,7 +48,7 @@ public class EffMongoValue extends Effect {
@SuppressWarnings("unchecked")
@Override
public boolean init(@Nonnull Expression<?>[] exprs, int matchedPattern, @Nonnull Kleenean isDelayed, @Nonnull SkriptParser.ParseResult parseResult) {
if (!EffectSection.isCurrentSection(SectMongoDocument.class))
if (!LegacyEffectSection.isCurrentSection(SectLegacyMongoDocument.class))
return false;
isSingle = parseResult.mark == 1
? Kleenean.TRUE
Expand All @@ -62,11 +66,11 @@ protected void execute(@Nonnull Event e) {
List<?> omega = Arrays.asList(MongoSKAdapter.serializeArray(exprValue.getArray(e)));
switch (isSingle) {
case UNKNOWN:
SectMongoDocument.mongoSKDocument.getBsonDocument().put(key, omega.size() == 1 ? omega.get(0) : omega);
SectLegacyMongoDocument.mongoSKDocument.getBsonDocument().put(key, omega.size() == 1 ? omega.get(0) : omega);
break;
case TRUE:
case FALSE:
SectMongoDocument.mongoSKDocument.getBsonDocument().put(key, isSingle == Kleenean.TRUE ? omega.get(0) : omega);
SectLegacyMongoDocument.mongoSKDocument.getBsonDocument().put(key, isSingle == Kleenean.TRUE ? omega.get(0) : omega);
break;
}
}
Expand Down
Loading

0 comments on commit 395509c

Please sign in to comment.