-
-
Notifications
You must be signed in to change notification settings - Fork 414
Add support for a negation/fail check on CondChance #8320
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
Open
Fusezion
wants to merge
6
commits into
SkriptLang:dev/feature
Choose a base branch
from
Fusezion:enhancement/cond-chance-negation
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f15d78e
CondChance.java - Add support for fail checks
Fusezion 9dddb4c
Remove debug import
Fusezion 23d1658
Add new line in toString's negate check
Fusezion 1a822e5
Apply suggestions from code review
Fusezion afb5e27
Smurfy Request - Implement SimplifiedCondition support
Fusezion 95dc157
Apply code suggestions
Fusezion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,58 +1,81 @@ | ||
| 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.Examples; | ||
| 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.Literal; | ||
| import ch.njol.skript.lang.SimplifiedCondition; | ||
| import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
| import ch.njol.util.Kleenean; | ||
| import org.bukkit.event.Event; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** | ||
| * @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."}) | ||
| @Examples({"chance of 50%:", | ||
| "\tdrop a diamond", | ||
| "chance of {chance}% # {chance} between 0 and 100", | ||
| "chance of {chance} # {chance} between 0 and 1"}) | ||
| @Since("1.0") | ||
| @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 at location(100, 100, 100, "world') | ||
| """) | ||
| @Example("chance of {chance}% # {chance} between 0 and 100") | ||
| @Example("chance of {chance} # {chance} between 0 and 1") | ||
| @Example(""" | ||
| if chance of 99% fails: | ||
| broadcast "Haha loser! *points and laughs*" | ||
| """) | ||
| @Since("1.0, INSERT VERSION (chance fails)") | ||
| public class CondChance extends Condition { | ||
|
|
||
| static { | ||
| Skript.registerCondition(CondChance.class, "chance of %number%(1¦\\%|)"); | ||
| Skript.registerCondition(CondChance.class, "chance of %number%(1:\\%|) [fail:(fails|failed)]"); | ||
| } | ||
|
|
||
| @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) { | ||
| public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
| //noinspection unchecked | ||
| chance = (Expression<Number>) exprs[0]; | ||
| percent = parser.mark == 1; | ||
| percent = parseResult.mark == 1; | ||
| setNegated(parseResult.hasTag("fail")); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean check(final Event e) { | ||
| final Number n = chance.getSingle(e); | ||
| if (n == null) | ||
| public boolean check(Event event) { | ||
| Number chance = this.chance.getSingle(event); | ||
| if (chance == null) | ||
| return false; | ||
| return Math.random() < (percent ? n.doubleValue() / 100 : n.doubleValue()); | ||
| boolean result = Math.random() < (percent ? chance.doubleValue() / 100 : chance.doubleValue()); | ||
| return result == !isNegated(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(final @Nullable Event e, final boolean debug) { | ||
| return "chance of " + chance.toString(e, debug) + (percent ? "%" : ""); | ||
| public Condition simplify() { | ||
| if (chance instanceof Literal<Number> litChance) { | ||
| Number chance = litChance.getSingle(); | ||
| // We can only simplify if the provided value <= 0% or >= 100% (or 1) | ||
| // as this eliminates the random element and will always evaluate the same. | ||
| double maxValue = percent ? 100 : 1; | ||
| if (chance.doubleValue() >= maxValue || chance.doubleValue() <= 0) | ||
| return SimplifiedCondition.fromCondition(this); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public String toString(@Nullable Event event, boolean debug) { | ||
| String baseString = "chance of " + chance.toString(event, debug) + (percent ? "%" : ""); | ||
| if (isNegated()) | ||
| baseString += " failed"; | ||
| return baseString; | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can leave this as a suppression on the method if you'd like