Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import org.skriptlang.skript.bukkit.loottables.LootTableModule;
import org.skriptlang.skript.bukkit.registration.BukkitRegistryKeys;
import org.skriptlang.skript.bukkit.registration.BukkitSyntaxInfos;
import org.skriptlang.skript.bukkit.spawners.SpawnerModule;
import org.skriptlang.skript.bukkit.tags.TagModule;
import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.comparator.Comparators;
Expand Down Expand Up @@ -585,7 +586,7 @@ public void onEnable() {
TagModule.load();
FurnaceModule.load();
LootTableModule.load();
skript.loadModules(new DamageSourceModule());
skript.loadModules(new DamageSourceModule(), new SpawnerModule());
} catch (final Exception e) {
exception(e, "Could not load required .class files: " + e.getLocalizedMessage());
setEnabled(false);
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.function.DynamicFunctionReference;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.lang.util.common.AnyAmount;
import ch.njol.skript.lang.util.common.AnyContains;
import ch.njol.skript.lang.util.common.AnyNamed;
import ch.njol.skript.lang.util.common.AnyValued;
import ch.njol.skript.lang.util.common.*;
import ch.njol.skript.localization.Noun;
import ch.njol.skript.localization.RegexMessage;
import ch.njol.skript.registrations.Classes;
Expand Down Expand Up @@ -964,6 +961,14 @@ public String toVariableNameString(DynamicFunctionReference<?> function) {
.examples("{a} contains {b}")
.since("2.10")
);

Classes.registerClass(new AnyInfo<>(AnyWeighted.class, "weighted")
.name("Any Weighted Thing")
.description("Something that has a weight.")
.usage("")
.examples("the weight of {spawner entry}")
.since("INSERT VERSION")
);
}

}
118 changes: 0 additions & 118 deletions src/main/java/ch/njol/skript/expressions/ExprSpawnerType.java

This file was deleted.

74 changes: 74 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprWeight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ch.njol.skript.expressions;

import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Example;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.lang.util.common.AnyWeighted;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Weight")
@Description("""
Returns the weight of a weighted object. If supported, this weight can be modified.
""")
Comment on lines +15 to +17
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
@Description("""
Returns the weight of a weighted object. If supported, this weight can be modified.
""")
@Description("Returns the weight of a weighted object. If supported, this weight can be modified.")

@Example("""
broadcast weight of {_spawner entry}
set weight of {_spawner entry} to 5
add 5 to weight of {_spawner entry}
remove 2 from weight of {_spawner entry}
""")
@Since("INSERT VERSION")
public class ExprWeight extends SimplePropertyExpression<AnyWeighted, Number> {

static {
register(ExprWeight.class, Number.class, "weight[s]", "weighteds");
}

@Override
public @Nullable Number convert(AnyWeighted weighted) {
return weighted.weight();
}

@Override
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
return switch (mode) {
case SET, ADD, REMOVE -> CollectionUtils.array(Number.class);
default -> null;
};
}

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
assert delta != null;

Number deltaValue = (Number) delta[0];
for (AnyWeighted weighted : getExpr().getArray(event)) {
if (!weighted.supportsWeightChange())
error("This object does not support weight modification.");

Number newValue = switch (mode) {
case SET -> deltaValue;
case ADD -> weighted.weight().doubleValue() + deltaValue.doubleValue();
case REMOVE -> weighted.weight().doubleValue() - deltaValue.doubleValue();
default -> 0;
};

weighted.setWeight(newValue);
}
}

@Override
public Class<? extends Number> getReturnType() {
return Number.class;
}

@Override
protected String getPropertyName() {
return "weight";
}

}
42 changes: 42 additions & 0 deletions src/main/java/ch/njol/skript/lang/util/common/AnyWeighted.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ch.njol.skript.lang.util.common;

import org.jetbrains.annotations.UnknownNullability;

/**
* A provider for anything with a weight
* Anything implementing this (or convertible to this) can be used by the {@link ch.njol.skript.expressions.ExprWeight}
* property expression.
*
* @see AnyProvider
*/
@FunctionalInterface
public interface AnyWeighted extends AnyProvider {

/**
* @return This thing's weight
*/
@UnknownNullability Number weight();

/**
* This is called before {@link #setWeight(Number)}.
* If the result is false, setting the weight will never be attempted.
*
* @return Whether this supports being set
*/
default boolean supportsWeightChange() {
return false;
}

/**
* The behaviour for changing this thing's weight, if possible.
* If not possible, then {@link #supportsWeightChange()} should return false and this
* may throw an error.
*
* @param weight The weight to change
* @throws UnsupportedOperationException If this is impossible
*/
default void setWeight(Number weight) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}

}
Loading
Loading