Skip to content

Commit 746475a

Browse files
committed
Init commit
1 parent db61a08 commit 746475a

56 files changed

Lines changed: 5594 additions & 1 deletion

File tree

Some content is hidden

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
import org.skriptlang.skript.bukkit.loottables.LootTableModule;
101101
import org.skriptlang.skript.bukkit.registration.BukkitRegistryKeys;
102102
import org.skriptlang.skript.bukkit.registration.BukkitSyntaxInfos;
103+
import org.skriptlang.skript.bukkit.spawners.SpawnerModule;
103104
import org.skriptlang.skript.bukkit.tags.TagModule;
104105
import org.skriptlang.skript.lang.comparator.Comparator;
105106
import org.skriptlang.skript.lang.comparator.Comparators;
@@ -585,7 +586,7 @@ public void onEnable() {
585586
TagModule.load();
586587
FurnaceModule.load();
587588
LootTableModule.load();
588-
skript.loadModules(new DamageSourceModule());
589+
skript.loadModules(new DamageSourceModule(), new SpawnerModule());
589590
} catch (final Exception e) {
590591
exception(e, "Could not load required .class files: " + e.getLocalizedMessage());
591592
setEnabled(false);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package ch.njol.skript.expressions;
2+
3+
import ch.njol.skript.classes.Changer.ChangeMode;
4+
import ch.njol.skript.doc.Description;
5+
import ch.njol.skript.doc.Example;
6+
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.Since;
8+
import ch.njol.skript.expressions.base.SimplePropertyExpression;
9+
import ch.njol.skript.lang.util.common.AnyWeighted;
10+
import ch.njol.util.coll.CollectionUtils;
11+
import org.bukkit.event.Event;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
@Name("Weight")
15+
@Description("""
16+
Returns the weight of a weighted object. If supported, this weight can be modified.
17+
""")
18+
@Example("""
19+
broadcast weight of {_spawner entry}
20+
set weight of {_spawner entry} to 5
21+
add 5 to weight of {_spawner entry}
22+
remove 2 from weight of {_spawner entry}
23+
""")
24+
@Since("INSERT VERSION")
25+
public class ExprWeight extends SimplePropertyExpression<AnyWeighted, Number> {
26+
27+
static {
28+
register(ExprWeight.class, Number.class, "weight[s]", "weighteds");
29+
}
30+
31+
@Override
32+
public @Nullable Number convert(AnyWeighted weighted) {
33+
return weighted.weight();
34+
}
35+
36+
@Override
37+
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
38+
return switch (mode) {
39+
case SET, ADD, REMOVE -> CollectionUtils.array(Number.class);
40+
default -> null;
41+
};
42+
}
43+
44+
@Override
45+
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
46+
assert delta != null;
47+
48+
Number deltaValue = (Number) delta[0];
49+
for (AnyWeighted weighted : getExpr().getArray(event)) {
50+
if (!weighted.supportsWeightChange())
51+
error("This object does not support weight modification.");
52+
53+
Number newValue = switch (mode) {
54+
case SET -> deltaValue;
55+
case ADD -> weighted.weight().doubleValue() + deltaValue.doubleValue();
56+
case REMOVE -> weighted.weight().doubleValue() - deltaValue.doubleValue();
57+
default -> 0;
58+
};
59+
60+
weighted.setWeight(newValue);
61+
}
62+
}
63+
64+
@Override
65+
public Class<? extends Number> getReturnType() {
66+
return Number.class;
67+
}
68+
69+
@Override
70+
protected String getPropertyName() {
71+
return "weight";
72+
}
73+
74+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ch.njol.skript.lang.util.common;
2+
3+
import org.jetbrains.annotations.UnknownNullability;
4+
5+
/**
6+
* A provider for anything with a weight
7+
* Anything implementing this (or convertible to this) can be used by the {@link ch.njol.skript.expressions.ExprWeight}
8+
* property expression.
9+
*
10+
* @see AnyProvider
11+
*/
12+
@FunctionalInterface
13+
public interface AnyWeighted extends AnyProvider {
14+
15+
/**
16+
* @return This thing's weight
17+
*/
18+
@UnknownNullability Number weight();
19+
20+
/**
21+
* This is called before {@link #setWeight(Number)}.
22+
* If the result is false, setting the weight will never be attempted.
23+
*
24+
* @return Whether this supports being set
25+
*/
26+
default boolean supportsWeightChange() {
27+
return false;
28+
}
29+
30+
/**
31+
* The behaviour for changing this thing's weight, if possible.
32+
* If not possible, then {@link #supportsWeightChange()} should return false and this
33+
* may throw an error.
34+
*
35+
* @param weight The weight to change
36+
* @throws UnsupportedOperationException If this is impossible
37+
*/
38+
default void setWeight(Number weight) throws UnsupportedOperationException {
39+
throw new UnsupportedOperationException();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)