Skip to content
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

Module Mayhem #7421

Open
wants to merge 15 commits into
base: dev/feature
Choose a base branch
from
Open
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Update LootTableModule and it's syntax
cheeezburga committed Jan 12, 2025
commit afdd8a287836395c73d6bc1ffd0d7eeff605b00e
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
@@ -553,9 +553,9 @@ public void onEnable() {
new BreedingModule(),
new FurnaceModule(),
new DisplayModule(),
new TagModule()
new TagModule(),
new LootTableModule()
);
LootTableModule.load();
} catch (final Exception e) {
exception(e, "Could not load required .class files: " + e.getLocalizedMessage());
setEnabled(false);
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.skriptlang.skript.bukkit.loottables;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.expressions.base.EventValueExpression;
@@ -16,14 +15,21 @@
import org.bukkit.loot.LootContext;
import org.bukkit.loot.LootTable;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;

public class LootTableModule {

public static void load() throws IOException {

// --- CLASSES --- //
import org.skriptlang.skript.addon.AddonModule;
import org.skriptlang.skript.addon.SkriptAddon;
import org.skriptlang.skript.bukkit.loottables.elements.conditions.CondHasLootTable;
import org.skriptlang.skript.bukkit.loottables.elements.conditions.CondIsLootable;
import org.skriptlang.skript.bukkit.loottables.elements.effects.EffGenerateLoot;
import org.skriptlang.skript.bukkit.loottables.elements.expressions.*;
import org.skriptlang.skript.bukkit.registration.BukkitRegistryKeys;
import org.skriptlang.skript.bukkit.registration.BukkitSyntaxInfos;
import org.skriptlang.skript.registration.SyntaxRegistry;

public class LootTableModule implements AddonModule {

@Override
public void init(SkriptAddon addon) {
SyntaxRegistry registry = addon.syntaxRegistry();

Classes.registerClass(new ClassInfo<>(LootTable.class, "loottable")
.user("loot ?tables?")
@@ -97,31 +103,49 @@ public String toVariableNameString(LootContext context) {
})
);

Skript.getAddonInstance().loadClasses("org.skriptlang.skript.bukkit.loottables", "elements");

// --- SIMPLE EVENTS --- //

Skript.registerEvent("Loot Generate", SimpleEvent.class, LootGenerateEvent.class, "loot generat(e|ing)")
.description(
registry.register(BukkitRegistryKeys.EVENT, BukkitSyntaxInfos.Event
.builder(SimpleEvent.class, "Loot Generate")
.addEvent(LootGenerateEvent.class)
.addPattern("loot generat(e|ing)")
.addDescription(
"Called when a loot table of an inventory is generated in the world.",
"For example, when opening a shipwreck chest."
)
.examples(
.addExamples(
"on loot generate:",
"\tchance of 10%",
"\tadd 64 diamonds to the loot",
"\tsend \"You hit the jackpot at %event-location%!\""
"\tchance of 10%",
"\tadd 64 diamonds to the loot",
"\tsend \"You hit the jackpot at %event-location%!\""
)
.addRequiredPlugin("Minecraft 1.16+")
.since("2.7")
.requiredPlugins("MC 1.16+");

// --- EVENT VALUES --- //
.build()
);

// LootGenerateEvent
EventValues.registerEventValue(LootGenerateEvent.class, Entity.class, LootGenerateEvent::getEntity);
EventValues.registerEventValue(LootGenerateEvent.class, Location.class, event -> event.getLootContext().getLocation());
EventValues.registerEventValue(LootGenerateEvent.class, LootTable.class, LootGenerateEvent::getLootTable);
EventValues.registerEventValue(LootGenerateEvent.class, LootContext.class, LootGenerateEvent::getLootContext);
}

@Override
public void load(SkriptAddon addon) {
SyntaxRegistry registry = addon.syntaxRegistry();

CondHasLootTable.register(registry);
CondIsLootable.register(registry);
EffGenerateLoot.register(registry);
ExprLoot.register(registry);
ExprLootContext.register(registry);
ExprLootContextEntity.register(registry);
ExprLootContextLocation.register(registry);
ExprLootContextLooter.register(registry);
ExprLootContextLuck.register(registry);
ExprLootItems.register(registry);
ExprLootTable.register(registry);
ExprLootTableFromString.register(registry);
ExprLootTableSeed.register(registry);
ExprSecCreateLootContext.register(registry);
}

}
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import org.skriptlang.skript.bukkit.loottables.LootTableUtils;
import org.skriptlang.skript.registration.SyntaxRegistry;

@Name("Has Loot Table")
@Description(
@@ -25,8 +26,8 @@
@Since("2.10")
public class CondHasLootTable extends PropertyCondition<Object> {

static {
register(CondHasLootTable.class, PropertyType.HAVE, "[a] loot[ ]table", "blocks/entities");
public static void register(SyntaxRegistry registry) {
register(registry, CondHasLootTable.class, PropertyType.HAVE, "[a] loot[ ]table", "blocks/entities");
}

@Override
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import org.skriptlang.skript.bukkit.loottables.LootTableUtils;
import org.skriptlang.skript.registration.SyntaxRegistry;

@Name("Is Lootable")
@Description(
@@ -31,8 +32,8 @@
@Since("2.10")
public class CondIsLootable extends PropertyCondition<Object> {

static {
register(CondIsLootable.class, "lootable", "blocks/entities");
public static void register(SyntaxRegistry registry) {
register(registry, CondIsLootable.class, "lootable", "blocks/entities");
}

@Override
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.skriptlang.skript.bukkit.loottables.elements.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Node;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
@@ -17,6 +17,9 @@
import org.bukkit.loot.LootTable;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.bukkit.loottables.LootContextWrapper;
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer;
import org.skriptlang.skript.registration.SyntaxInfo;
import org.skriptlang.skript.registration.SyntaxRegistry;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
@@ -32,21 +35,24 @@
"generate loot using \"minecraft:chests/shipwreck_supply\" in {_inventory}"
})
@Since("2.10")
public class EffGenerateLoot extends Effect {
public class EffGenerateLoot extends Effect implements SyntaxRuntimeErrorProducer {

static {
Skript.registerEffect(EffGenerateLoot.class,
"generate [the] loot (of|using) %loottable% [(with|using) %-lootcontext%] in %inventories%"
public static void register(SyntaxRegistry registry) {
registry.register(SyntaxRegistry.EFFECT, SyntaxInfo.builder(EffGenerateLoot.class)
.addPattern("generate [the] loot (of|using) %loottable% [(with|using) %-lootcontext%] in %inventories%")
.build()
);
}

private Node node;
private Expression<LootTable> lootTable;
private Expression<LootContext> context;
private Expression<Inventory> inventories;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
node = getParser().getNode();
lootTable = (Expression<LootTable>) exprs[0];
context = (Expression<LootContext>) exprs[1];
inventories = (Expression<Inventory>) exprs[2];
@@ -60,24 +66,34 @@ protected void execute(Event event) {
LootContext context;
if (this.context != null) {
context = this.context.getSingle(event);
if (context == null)
if (context == null) {
error("The provided loot context was not set.", this.context.toString());
return;
}
} else {
context = new LootContextWrapper(Bukkit.getWorlds().get(0).getSpawnLocation()).getContext();
}

LootTable table = lootTable.getSingle(event);
if (table == null)
if (table == null) {
error("The provided loot table was not set.", this.lootTable.toString());
return;
}

for (Inventory inventory : inventories.getArray(event)) {
try {
// todo: perhaps runtime error in the future
table.fillInventory(inventory, random, context);
} catch (IllegalArgumentException ignore) {}
} catch (IllegalArgumentException ignored) {
error("Failed to fill an inventory with the provided loot table (and loot context).", inventories.toString());
}
}
}

@Override
public Node getNode() {
return node;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.config.Node;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
@@ -10,13 +11,15 @@
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.inventory.ItemStack;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.event.world.LootGenerateEvent;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer;
import org.skriptlang.skript.registration.SyntaxInfo;
import org.skriptlang.skript.registration.SyntaxRegistry;

import java.util.ArrayList;
import java.util.List;
@@ -30,27 +33,37 @@
"\tsend \"You hit the jackpot!!\""
})
@Since("2.7")
@RequiredPlugins("MC 1.16+")
public class ExprLoot extends SimpleExpression<ItemStack> {
@RequiredPlugins("Minecraft 1.16+")
public class ExprLoot extends SimpleExpression<ItemStack> implements SyntaxRuntimeErrorProducer {

static {
Skript.registerExpression(ExprLoot.class, ItemStack.class, ExpressionType.SIMPLE, "[the] loot");
public static void register(SyntaxRegistry registry) {
registry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression
.builder(ExprLoot.class, ItemStack.class)
.priority(SyntaxInfo.SIMPLE)
.addPattern("[the] loot")
.build()
);
}

private Node node;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!getParser().isCurrentEvent(LootGenerateEvent.class)) {
Skript.error("The 'loot' expression can only be used in a 'loot generate' event");
return false;
}
node = getParser().getNode();
return true;
}

@Override
@Nullable
protected ItemStack @Nullable [] get(Event event) {
if (!(event instanceof LootGenerateEvent lootEvent))
if (!(event instanceof LootGenerateEvent lootEvent)) {
error("The 'loot' expression can only be used in a 'loot generate' event.");
return new ItemStack[0];
}
return lootEvent.getLoot().toArray(new ItemStack[0]);
}

@@ -65,8 +78,10 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
if (!(event instanceof LootGenerateEvent lootEvent))
if (!(event instanceof LootGenerateEvent lootEvent)) {
error("The 'loot' expression can only be used in a 'loot generate' event.");
return;
}

List<ItemStack> items = null;
if (delta != null) {
@@ -93,6 +108,11 @@ public Class<? extends ItemStack> getReturnType() {
return ItemStack.class;
}

@Override
public Node getNode() {
return node;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the loot";
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.EventValueExpression;
import org.bukkit.loot.LootContext;
import org.skriptlang.skript.registration.SyntaxRegistry;

@Name("Loot Context")
@Description("The loot context involved in the context create section.")
@@ -16,8 +17,8 @@
@Since("2.10")
public class ExprLootContext extends EventValueExpression<LootContext> {

static {
register(ExprLootContext.class, LootContext.class, "loot[ ]context");
public static void register(SyntaxRegistry registry) {
register(registry, ExprLootContext.class, LootContext.class, "loot[ ]context");
}

public ExprLootContext() {
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import org.bukkit.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.bukkit.loottables.LootContextCreateEvent;
import org.skriptlang.skript.registration.SyntaxRegistry;

@Name("Looted Entity of Loot Context")
@Description("Returns the looted entity of a loot context.")
@@ -27,8 +28,8 @@
@Since("2.10")
public class ExprLootContextEntity extends SimplePropertyExpression<LootContext, Entity> {

static {
registerDefault(ExprLootContextEntity.class, Entity.class, "looted entity", "lootcontexts");
public static void register(SyntaxRegistry registry) {
registerDefault(registry, ExprLootContextEntity.class, Entity.class, "looted entity", "lootcontexts");
}

@Override
@@ -51,8 +52,10 @@ public class ExprLootContextEntity extends SimplePropertyExpression<LootContext,

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
if (!(event instanceof LootContextCreateEvent createEvent))
if (!(event instanceof LootContextCreateEvent createEvent)) {
error("The looted entity cannot be set outside of a LootContextCreateEvent.");
return;
}

Entity entity = delta != null ? (Entity) delta[0] : null;
createEvent.getContextWrapper().setEntity(entity);
Loading