-
-
Notifications
You must be signed in to change notification settings - Fork 386
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||||
|
@@ -25,44 +25,43 @@ | |||||||||
"", | ||||||||||
"if player's total experience is greater than 100:", | ||||||||||
"\tset player's total experience to 0", | ||||||||||
"\tgive player 1 diamond" | ||||||||||
"\tgive player 1 diamond", | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
"", | ||||||||||
"on level progress change:", | ||||||||||
"\tset {_xp} to event-experience", | ||||||||||
"\tbroadcast experience of {_xp}" | ||||||||||
Comment on lines
+31
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
}) | ||||||||||
@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) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
return switch (mode) { | ||||||||||
case ADD, REMOVE, SET, DELETE, RESET -> new Class[]{ Integer.class }; | ||||||||||
default -> null; | ||||||||||
}; | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
|
@@ -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; | ||||||||||
|
@@ -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; | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
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.