-
-
Notifications
You must be signed in to change notification settings - Fork 414
Refactor ExprBeaconValues into multiple expressions, fix bugs. #7729
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
Merged
APickledWalrus
merged 8 commits into
SkriptLang:dev/feature
from
sovdeeth:cleanup/exprbeacon
Jul 1, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
40ef2be
Clean up ExprBeaconValues, split into multiple classes, fix various bugs
sovdeeth 2153e8b
Update src/main/java/ch/njol/skript/expressions/ExprBeaconRange.java
sovdeeth 2d19276
Merge branch 'dev/feature' into cleanup/exprbeacon
sovdeeth 001e82f
Update ExprBeaconEffects.java
sovdeeth 4ceaa16
Merge branch 'dev/feature' into cleanup/exprbeacon
sovdeeth 6e353b4
Requested Changes
sovdeeth 81bb427
Merge branch 'dev/feature' into cleanup/exprbeacon
sovdeeth e26bc47
Merge branch 'dev/feature' into cleanup/exprbeacon
APickledWalrus 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
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
src/main/java/ch/njol/skript/expressions/ExprBeaconEffects.java
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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package ch.njol.skript.expressions; | ||
|
|
||
| import ch.njol.skript.Skript; | ||
| import ch.njol.skript.classes.Changer.ChangeMode; | ||
| import ch.njol.skript.doc.*; | ||
| import ch.njol.skript.expressions.base.PropertyExpression; | ||
| import ch.njol.skript.lang.Expression; | ||
| import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
| import ch.njol.util.Kleenean; | ||
| import ch.njol.util.coll.CollectionUtils; | ||
| import io.papermc.paper.event.player.PlayerChangeBeaconEffectEvent; | ||
| import org.bukkit.block.Beacon; | ||
| import org.bukkit.block.Block; | ||
| import org.bukkit.event.Event; | ||
| import org.bukkit.potion.PotionEffect; | ||
| import org.bukkit.potion.PotionEffectType; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.util.function.BiConsumer; | ||
|
|
||
| @Name("Beacon Effects") | ||
| @Description({ | ||
| "The active effects of a beacon.", | ||
| "The secondary effect can be set to anything, but the icon in the GUI will not display correctly.", | ||
| "The secondary effect can only be set when the beacon is at max tier.", | ||
| "The primary and secondary effect can not be the same, primary will always retain the potion type and secondary will be cleared." | ||
| }) | ||
| @Example(""" | ||
| set primary beacon effect of {_block} to haste | ||
| set secondary effect of {_block} to resistance | ||
| """ | ||
| ) | ||
| @Events({"Beacon Effect", "Beacon Toggle", "Beacon Change Effect"}) | ||
| @Since("2.10") | ||
| public class ExprBeaconEffects extends PropertyExpression<Block, PotionEffectType> { | ||
|
|
||
| private static final boolean SUPPORTS_CHANGE_EVENT = Skript.classExists("io.papermc.paper.event.player.PlayerChangeBeaconEffectEvent"); | ||
|
|
||
| static { | ||
| register(ExprBeaconEffects.class, PotionEffectType.class, "(:primary|secondary) [beacon] effect", "blocks"); | ||
| } | ||
|
|
||
| private boolean primary; | ||
|
|
||
| @Override | ||
| public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
| //noinspection unchecked | ||
| setExpr((Expression<? extends Block>) expressions[0]); | ||
| primary = parseResult.hasTag("primary"); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected PotionEffectType[] get(Event event, Block[] source) { | ||
| return get(source, block -> { | ||
| if (!(block.getState() instanceof Beacon beacon)) | ||
| return null; | ||
|
|
||
| if (SUPPORTS_CHANGE_EVENT && event instanceof PlayerChangeBeaconEffectEvent changeEvent) | ||
| return primary ? changeEvent.getPrimary() : changeEvent.getSecondary(); | ||
|
|
||
| PotionEffect effect = primary ? beacon.getPrimaryEffect() : beacon.getSecondaryEffect(); | ||
| if (effect == null) | ||
| return null; | ||
| return effect.getType(); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public Class<?> @Nullable [] acceptChange(ChangeMode mode) { | ||
| return switch (mode) { | ||
| case SET, RESET, DELETE -> CollectionUtils.array(PotionEffectType.class); | ||
| default -> null; | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { | ||
| PotionEffectType type = delta == null ? null : (PotionEffectType) delta[0]; | ||
| BiConsumer<Beacon, PotionEffectType> changer = primary ? Beacon::setPrimaryEffect : Beacon::setSecondaryEffect; | ||
| for (Block block : getExpr().getArray(event)) { | ||
| if (!(block.getState() instanceof Beacon beacon)) | ||
| continue; | ||
| changer.accept(beacon, type); | ||
| beacon.update(true); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Class<PotionEffectType> getReturnType() { | ||
| return PotionEffectType.class; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(@Nullable Event event, boolean debug) { | ||
| if (primary) | ||
| return "primary beacon effect of " + getExpr().toString(event, debug); | ||
| return "secondary beacon effect of " + getExpr().toString(event, debug); | ||
| } | ||
|
|
||
| } | ||
86 changes: 86 additions & 0 deletions
86
src/main/java/ch/njol/skript/expressions/ExprBeaconRange.java
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package ch.njol.skript.expressions; | ||
|
|
||
| import ch.njol.skript.Skript; | ||
| import ch.njol.skript.classes.Changer.ChangeMode; | ||
| import ch.njol.skript.doc.*; | ||
| import ch.njol.skript.expressions.base.SimplePropertyExpression; | ||
| import ch.njol.util.Math2; | ||
| import ch.njol.util.coll.CollectionUtils; | ||
| import org.bukkit.block.Beacon; | ||
| import org.bukkit.block.Block; | ||
| import org.bukkit.event.Event; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @Name("Beacon Range") | ||
| @Description({ | ||
| "The range of a beacon's effects, in blocks." | ||
| }) | ||
| @Example(""" | ||
| if the beacon tier of the clicked block is 4: | ||
| set the beacon effect range of the clicked block to 100 | ||
| """ | ||
| ) | ||
| @RequiredPlugins("Paper") | ||
| @Since("2.10") | ||
| public class ExprBeaconRange extends SimplePropertyExpression<Block, Double> { | ||
|
|
||
| private static final boolean SUPPORTS_RANGE = Skript.methodExists(Beacon.class, "getEffectRange"); | ||
|
|
||
| static { | ||
| if (SUPPORTS_RANGE) | ||
| register(ExprBeaconRange.class, Double.class, "beacon [effect] range", "blocks"); | ||
| } | ||
sovdeeth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public @Nullable Double convert(Block from) { | ||
| if (from.getState() instanceof Beacon beacon) | ||
sovdeeth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return beacon.getEffectRange(); | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<?> @Nullable [] acceptChange(ChangeMode mode) { | ||
| return switch (mode) { | ||
| case SET, ADD, REMOVE, RESET -> CollectionUtils.array(Double.class); | ||
| default -> null; | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { | ||
| if (mode == ChangeMode.RESET) { | ||
| for (Block block : getExpr().getArray(event)) { | ||
| if (block.getState() instanceof Beacon beacon) { | ||
| beacon.resetEffectRange(); | ||
| beacon.update(true); | ||
| } | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| assert delta != null; | ||
| double range = ((Number) delta[0]).doubleValue(); | ||
| for (Block block : getExpr().getArray(event)) { | ||
| if (block.getState() instanceof Beacon beacon) { | ||
| switch (mode) { | ||
| case SET -> beacon.setEffectRange(Math2.fit(0, range, Double.MAX_VALUE)); | ||
| case ADD -> beacon.setEffectRange(Math2.fit(0, beacon.getEffectRange() + range, Double.MAX_VALUE)); | ||
| case REMOVE -> beacon.setEffectRange(Math2.fit(0, beacon.getEffectRange() - range, Double.MAX_VALUE)); | ||
| default -> throw new IllegalStateException(); | ||
| } | ||
| beacon.update(true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Class<? extends Double> getReturnType() { | ||
| return Double.class; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getPropertyName() { | ||
| return "beacon [effect] range"; | ||
sovdeeth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } | ||
45 changes: 45 additions & 0 deletions
45
src/main/java/ch/njol/skript/expressions/ExprBeaconTier.java
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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package ch.njol.skript.expressions; | ||
|
|
||
| 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 org.bukkit.block.Beacon; | ||
| import org.bukkit.block.Block; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @Name("Beacon Tier") | ||
| @Description({ | ||
| "The tier of a beacon. Ranges from 0 to 4." | ||
| }) | ||
| @Example(""" | ||
| if the beacon tier of the clicked block is 4: | ||
| send "This is a max tier beacon!" | ||
| """ | ||
| ) | ||
| @Since("2.10") | ||
| public class ExprBeaconTier extends SimplePropertyExpression<Block, Integer> { | ||
|
|
||
| static { | ||
| register(ExprBeaconTier.class, Integer.class, "beacon tier", "blocks"); | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable Integer convert(Block from) { | ||
| if (from.getState() instanceof Beacon beacon) | ||
sovdeeth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return beacon.getTier(); | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<Integer> getReturnType() { | ||
| return Integer.class; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getPropertyName() { | ||
| return "beacon tier"; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.