Skip to content
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

Feature/xp block break event #3589

Merged
merged 6 commits into from
Dec 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 51 additions & 31 deletions src/main/java/ch/njol/skript/expressions/ExprExperience.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,20 +42,25 @@
* @author Peter Güttinger
*/
@Name("Experience")
@Description("How much experience was spawned in an <a href='../events.html#experience_spawn'>experience spawn</a> 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<Experience> {
static {
Skript.registerExpression(ExprExperience.class, Experience.class, ExpressionType.SIMPLE, "[the] (spawned|dropped|) [e]xp[erience] [orb[s]]");
}

@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;
Expand All @@ -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
Expand All @@ -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:
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
d -= v;
break;
case RESET:
case DELETE:
APickledWalrus marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down