Repository navigation
Expand file tree
/
Copy pathCondChance.java
More file actions
60 lines (51 loc) · 1.67 KB
/
Copy pathCondChance.java
File metadata and controls
60 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package ch.njol.skript.conditions;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import ch.njol.skript.Skript;
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.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
/**
* @author Peter Güttinger
*/
@Name("Chance")
@Description({"A condition that randomly succeeds or fails.",
"Valid values are between 0% and 100%, or if the percent sign is omitted between 0 and 1."})
@Example("""
chance of 50%:
drop a diamond
""")
@Example("chance of {chance}% # {chance} between 0 and 100")
@Example("chance of {chance} # {chance} between 0 and 1")
@Since("1.0")
public class CondChance extends Condition {
static {
Skript.registerCondition(CondChance.class, "chance of %number%(1¦\\%|)");
}
@SuppressWarnings("null")
private Expression<Number> chance;
private boolean percent;
@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
chance = (Expression<Number>) exprs[0];
percent = parser.mark == 1;
return true;
}
@Override
public boolean check(final Event e) {
final Number n = chance.getSingle(e);
if (n == null)
return false;
return Math.random() < (percent ? n.doubleValue() / 100 : n.doubleValue());
}
@Override
public String toString(final @Nullable Event e, final boolean debug) {
return "chance of " + chance.toString(e, debug) + (percent ? "%" : "");
}
}