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

Add experience point support to ExprTotalExperience #7581

Closed
Show file tree
Hide file tree
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
78 changes: 41 additions & 37 deletions src/main/java/ch/njol/skript/expressions/ExprTotalExperience.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import org.bukkit.entity.Entity;
import ch.njol.skript.util.Experience;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
Expand All @@ -25,44 +25,43 @@
"",
"if player's total experience is greater than 100:",
"\tset player's total experience to 0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"\tset player's total experience to 0",
"\tset player's total experience to 0",

"\tgive player 1 diamond"
"\tgive player 1 diamond",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"\tgive player 1 diamond",
"\tgive player 1 diamond",

"",
"on level progress change:",
"\tset {_xp} to event-experience",
"\tbroadcast experience of {_xp}"
Comment on lines +31 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"\tset {_xp} to event-experience",
"\tbroadcast experience of {_xp}"
"\tset {_xp} to event-experience",
"\tbroadcast experience of {_xp}"

})
@Since("2.7")
public class ExprTotalExperience extends SimplePropertyExpression<Entity, Integer> {
@Since("2.7, INSERT VERSION (experience point support)")
public class ExprTotalExperience extends SimplePropertyExpression<Object, Integer> {

static {
register(ExprTotalExperience.class, Integer.class, "[total] experience", "entities");
register(ExprTotalExperience.class, Integer.class, "[total] experience", "entities/experiences");
}

@Override
@Nullable
public Integer convert(Entity entity) {
public @Nullable Integer convert(Object object) {
// experience orbs
if (entity instanceof ExperienceOrb)
return ((ExperienceOrb) entity).getExperience();
if (object instanceof ExperienceOrb experienceOrb)
return experienceOrb.getExperience();

// players need special treatment
if (entity instanceof Player)
return PlayerUtils.getTotalXP(((Player) entity).getLevel(), ((Player) entity).getExp());
if (object instanceof Player player)
return PlayerUtils.getTotalXP(player.getLevel(), player.getExp());

// experiences
if (object instanceof Experience experience)
return experience.getXP();

// invalid entity type
return null;
}

@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
switch (mode) {
case ADD:
case REMOVE:
case SET:
case DELETE:
case RESET:
return new Class[]{Number.class};
case REMOVE_ALL:
default:
return null;
}
public @Nullable Class<?>[] acceptChange(ChangeMode mode) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public @Nullable Class<?>[] acceptChange(ChangeMode mode) {
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {

return switch (mode) {
case ADD, REMOVE, SET, DELETE, RESET -> new Class[]{ Integer.class };
default -> null;
};
}

@Override
Expand All @@ -75,11 +74,13 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
case SET:
if (change < 0)
change = 0;
for (Entity entity : getExpr().getArray(event)) {
if (entity instanceof ExperienceOrb) {
((ExperienceOrb) entity).setExperience(change);
} else if (entity instanceof Player) {
PlayerUtils.setTotalXP((Player) entity, change);
for (Object object : getExpr().getArray(event)) {
if (object instanceof ExperienceOrb experienceOrb) {
experienceOrb.setExperience(change);
} else if (object instanceof Player player) {
PlayerUtils.setTotalXP(player, change);
} else if (object instanceof Experience experience) {
experience.setXP(change);
}
}
break;
Expand All @@ -88,20 +89,23 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
// fall through to ADD
case ADD:
int xp;
for (Entity entity : getExpr().getArray(event)) {
if (entity instanceof ExperienceOrb) {
for (Object object : getExpr().getArray(event)) {
if (object instanceof ExperienceOrb experienceOrb) {
//ensure we don't go below 0
xp = ((ExperienceOrb) entity).getExperience() + change;
((ExperienceOrb) entity).setExperience(Math.max(xp, 0));
} else if (entity instanceof Player) {
xp = experienceOrb.getExperience() + change;
experienceOrb.setExperience(Math.max(xp, 0));
} else if (object instanceof Player player) {
// can only giveExp() positive experience
if (change < 0) {
// ensure we don't go below 0
xp = PlayerUtils.getTotalXP((Player) entity) + change;
PlayerUtils.setTotalXP((Player) entity, (Math.max(xp, 0)));
xp = PlayerUtils.getTotalXP(player) + change;
PlayerUtils.setTotalXP(player, (Math.max(xp, 0)));
} else {
((Player) entity).giveExp(change);
player.giveExp(change);
}
} else if (object instanceof Experience experience) {
xp = experience.getXP() + change;
experience.setXP(Math.max(xp, 0));
}
}
break;
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/ch/njol/skript/util/Experience.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@
*/
public class Experience implements YggdrasilSerializable {

private final int xp;
private int xp;

public Experience() {
xp = -1;
}

public Experience(final int xp) {
public Experience(int xp) {
this.xp = xp;
}

public int getXP() {
return xp == -1 ? 1 : xp;
}

public void setXP(int xp) {
this.xp = xp;
}

public int getInternalXP() {
return xp;
Expand All @@ -41,17 +45,14 @@ public int hashCode() {
}

@Override
public boolean equals(final @Nullable Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Experience))
return false;
final Experience other = (Experience) obj;
if (xp != other.xp)
if (!(obj instanceof Experience other))
return false;
return true;
return xp == other.xp;
}

}
12 changes: 12 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprTotalExperience.sk
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ test "total experience":
assert experience of entity is 0 with "remove too much experience from entity did not work"
assert experience of last spawned entity is 0 with "entity should have 0 experience after section"
delete last spawned entity

set {_xp} to 5 xp
assert experience of {_xp} is 5 with "getting xp of Experience doesn't work"
subtract 1 from experience of {_xp}
assert experience of {_xp} is 4 with "removing from xp of Experience doesn't work"
add 3 to experience of {_xp}
assert experience of {_xp} is 7 with "adding to xp of Experience doesn't work"
clear experience of {_xp}
assert experience of {_xp} is 0 with "clearing xp of Experience doesn't work"
set {_xp} to 5 xp
reset experience of {_xp}
assert experience of {_xp} is 0 with "resetting xp of Experience doesn't work"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert experience of {_xp} is 0 with "resetting xp of Experience doesn't work"
assert experience of {_xp} is 0 with "resetting xp of Experience doesn't work"