Skip to content

Commit

Permalink
✨ Introducing Mongo database and collection drops
Browse files Browse the repository at this point in the history
  • Loading branch information
Romitou committed Apr 2, 2021
1 parent 4ca89cc commit ee18121
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fr.romitou.mongosk.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import fr.romitou.mongosk.MongoSK;
import fr.romitou.mongosk.SubscriberHelpers;
import fr.romitou.mongosk.elements.MongoSKCollection;
import org.bukkit.event.Event;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class EffDropMongoCollection extends Effect {

private final static Boolean CAN_DROP = MongoSK.getConfiguration().getBoolean("allow-drop.collection", false);

static {
if (CAN_DROP)
Skript.registerEffect(
EffDropMongoCollection.class,
"drop [the] mongo[(sk|db)] collection %mongoskcollection%"
);
}

private Expression<MongoSKCollection> exprMongoSKCollection;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, @Nonnull Kleenean isDelayed, @Nonnull SkriptParser.ParseResult parseResult) {
exprMongoSKCollection = (Expression<MongoSKCollection>) exprs[0];
return true;
}

@Override
protected void execute(@Nonnull Event e) {
MongoSKCollection mongoSKCollection = exprMongoSKCollection.getSingle(e);
if (mongoSKCollection == null)
return;
SubscriberHelpers.ObservableSubscriber<Void> voidSubscriber = new SubscriberHelpers.OperationSubscriber<>();
mongoSKCollection.getMongoCollection().drop().subscribe(voidSubscriber);
voidSubscriber.await();
}

@Nonnull
@Override
public String toString(@Nullable Event e, boolean debug) {
return "drop the mongo collection " + exprMongoSKCollection.toString(e, debug);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fr.romitou.mongosk.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import fr.romitou.mongosk.MongoSK;
import fr.romitou.mongosk.SubscriberHelpers;
import fr.romitou.mongosk.elements.MongoSKDatabase;
import org.bukkit.event.Event;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class EffDropMongoDatabase extends Effect {

private final static Boolean CAN_DROP = MongoSK.getConfiguration().getBoolean("allow-drop.database", false);

static {
if (CAN_DROP)
Skript.registerEffect(
EffDropMongoDatabase.class,
"drop [the] mongo[(sk|db)] database %mongoskdatabase%"
);
}

private Expression<MongoSKDatabase> exprMongoSKDatabase;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, @Nonnull Kleenean isDelayed, @Nonnull SkriptParser.ParseResult parseResult) {
exprMongoSKDatabase = (Expression<MongoSKDatabase>) exprs[0];
return true;
}

@Override
protected void execute(@Nonnull Event e) {
MongoSKDatabase mongoSKDatabase = exprMongoSKDatabase.getSingle(e);
if (mongoSKDatabase == null)
return;
SubscriberHelpers.ObservableSubscriber<Void> voidSubscriber = new SubscriberHelpers.OperationSubscriber<>();
mongoSKDatabase.getMongoDatabase().drop().subscribe(voidSubscriber);
voidSubscriber.await();
}

@Nonnull
@Override
public String toString(@Nullable Event e, boolean debug) {
return "drop the mongo database " + exprMongoSKDatabase.toString(e, debug);
}
}
8 changes: 8 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ skript-adapters:
# The prefix and the field used by MongoSK in the storage of your documents.
# This field must be unique and not conflict with existing entries in your documents.
document-field: "__MongoSK__"

# When you activate these settings, the relevant drop effects will be registered by Skript and can be used.
# By default, these are disabled to avoid any problems, because any drop action is irreversible!
allow-drop:
# Activates the 'drop [the] mongo[(sk|db)] database %mongoskdatabase%' effect.
database: false
# Activates the 'drop [the] mongo[(sk|db)] collection %mongoskcollection%' effect.
collection: false

0 comments on commit ee18121

Please sign in to comment.