diff --git a/src/main/java/ch/njol/skript/expressions/ExprExperience.java b/src/main/java/ch/njol/skript/expressions/ExprExperience.java index 4b204113c3d..f83f99bdd0b 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprExperience.java +++ b/src/main/java/ch/njol/skript/expressions/ExprExperience.java @@ -19,6 +19,7 @@ package ch.njol.skript.expressions; import org.bukkit.event.Event; +import org.bukkit.event.block.BlockBreakEvent; import org.eclipse.jdt.annotation.Nullable; import ch.njol.skript.ScriptLoader; @@ -41,11 +42,16 @@ * @author Peter Güttinger */ @Name("Experience") -@Description("How much experience was spawned in an experience spawn event. Can be changed.") +@Description("How much experience was spawned in an experience spawn or block break event. Can be changed.") @Examples({"on experience spawn:", - " add 5 to the spawned experience"}) -@Since("2.1") -@Events("experience spawn") + "\tadd 5 to the spawned experience", + "on break of coal ore:", + "\tclear dropped experience", + "on break of diamond ore:", + "\tif tool of player = diamond pickaxe:", + "\t\tadd 100 to dropped experience"}) +@Since("2.1, INSERT VERSION (block break event)") +@Events({"experience spawn", "break / mine"}) public class ExprExperience extends SimpleExpression { static { Skript.registerExpression(ExprExperience.class, Experience.class, ExpressionType.SIMPLE, "[the] (spawned|dropped|) [e]xp[erience] [orb[s]]"); @@ -53,8 +59,8 @@ public class ExprExperience extends SimpleExpression { @Override public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { - if (!ScriptLoader.isCurrentEvent(ExperienceSpawnEvent.class)) { - Skript.error("The experience expression can only be used in experience spawn events"); + if (!ScriptLoader.isCurrentEvent(ExperienceSpawnEvent.class, BlockBreakEvent.class)) { + Skript.error("The experience expression can only be used in experience spawn and block break events"); return false; } return true; @@ -63,9 +69,12 @@ public boolean init(final Expression[] exprs, final int matchedPattern, final @Override @Nullable protected Experience[] get(final Event e) { - if (!(e instanceof ExperienceSpawnEvent)) + if (e instanceof ExperienceSpawnEvent) + return new Experience[] {new Experience(((ExperienceSpawnEvent) e).getSpawnedXP())}; + else if (e instanceof BlockBreakEvent) + return new Experience[] {new Experience(((BlockBreakEvent) e).getExpToDrop())}; + else return new Experience[0]; - return new Experience[] {new Experience(((ExperienceSpawnEvent) e).getSpawnedXP())}; } @Override @@ -87,31 +96,42 @@ public Class[] acceptChange(final ChangeMode mode) { @Override public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) { - if (!(e instanceof ExperienceSpawnEvent)) + double d; + if (e instanceof ExperienceSpawnEvent) + d = ((ExperienceSpawnEvent) e).getSpawnedXP(); + else if (e instanceof BlockBreakEvent) + d = ((BlockBreakEvent) e).getExpToDrop(); + else return; - if (delta == null) { - ((ExperienceSpawnEvent) e).setSpawnedXP(0); - return; - } - double d = 0; - for (final Object o : delta) { - final double v = o instanceof Experience ? ((Experience) o).getXP() : ((Number) o).doubleValue(); - switch (mode) { - case ADD: - case SET: - d += v; - break; - case REMOVE: - case REMOVE_ALL: - d -= v; - break; - case RESET: - case DELETE: - assert false; - break; + + if (delta != null) + for (final Object o : delta) { + final double v = o instanceof Experience ? ((Experience) o).getXP() : ((Number) o).doubleValue(); + switch (mode) { + case ADD: + d += v; + break; + case SET: + d = v; + break; + case REMOVE: + case REMOVE_ALL: + d -= v; + break; + case RESET: + case DELETE: + assert false; + break; + } } - } - ((ExperienceSpawnEvent) e).setSpawnedXP(Math.max(0, (int) Math.round(((ExperienceSpawnEvent) e).getSpawnedXP() + d))); + else + d = 0; + + d = Math.max(0, Math.round(d)); + if (e instanceof ExperienceSpawnEvent) + ((ExperienceSpawnEvent) e).setSpawnedXP((int) d); + else + ((BlockBreakEvent) e).setExpToDrop((int) d); } @Override