Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
cheeezburga authored and Moderocky committed Aug 17, 2024
1 parent 3a2668f commit bf6a659
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/main/java/ch/njol/skript/expressions/ExprDomestication.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
case SET:
case ADD:
case REMOVE:
case RESET:
return CollectionUtils.array(Number.class);
default:
return null;
}
}

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
assert mode != ChangeMode.REMOVE_ALL && mode != ChangeMode.DELETE && mode != ChangeMode.RESET;
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
assert mode != ChangeMode.REMOVE_ALL && mode != ChangeMode.DELETE;

int change = delta == null ? 0 : ((Number) delta[0]).intValue();

for (LivingEntity entity : getExpr().getArray(event)) {
if (entity instanceof AbstractHorse) {
AbstractHorse horse = (AbstractHorse) entity;
Expand All @@ -84,13 +86,18 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
case REMOVE:
level -= change;
break;
case RESET:
level = 1;
break;
default:
assert false;
return;
}
level = max ? Math.max(level, 1) : Math2.fit(1, level, horse.getMaxDomestication());
if (max) {
horse.setMaxDomestication(level);
if (horse.getDomestication() > level)
horse.setDomestication(level);
} else {
horse.setDomestication(level);
}
Expand All @@ -99,13 +106,13 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
}

@Override
protected String getPropertyName() {
return (max ? "max " : "") + "domestication level";
public Class<? extends Number> getReturnType() {
return Number.class;
}

@Override
public Class<? extends Number> getReturnType() {
return Number.class;
protected String getPropertyName() {
return (max ? "max " : "") + "domestication level";
}

}
27 changes: 23 additions & 4 deletions src/test/skript/tests/syntaxes/expressions/ExprDomestication.sk
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,37 @@ test "domestication":
assert max domestication level of {_e} is 100 with "removing from the max domestication of a horse should do exactly that"
assert domestication level of {_e} is 50 with "removing from the domestication of a horse should do exactly that"

set {_max} to max domestication level of {_e}
set domestication level of {_e} to {_max} + 1
subtract 60 from max domestication level of {_e}
assert domestication level of {_e} is 40 with "removing from the max domestication level of a horse should also clamp the domestication level if need be"

reset max domestication level of {_e}
reset domestication level of {_e}
assert max domestication level of {_e} is 1 with "resetting the max domestication of a horse should do exactly that"
assert domestication level of {_e} is 1 with "resetting the domestication of a horse should do exactly that"

set domestication level of {_e} to (max domestication level of {_e}) + 1
add 100 to domestication level of {_e}
assert domestication level of {_e} is not greater than {_max} with "the domestication level of a horse should never exceed it's max domestication level"
assert domestication level of {_e} is (max domestication level of {_e}) with "the domestication level of a horse should never exceed it's max domestication level"

set domestication level of {_e} to 0
subtract 100 from domestication level of {_e}
assert domestication level of {_e} is 1 with "the domestication level of a horse should never be less than 1"

set max domestication level of {_e} to 0
subtract 100 from max domestication level of {_e}
assert max domestication level of {_e} is 1 with "the domestication level of a horse should never be less than 1"
assert max domestication level of {_e} is 1 with "the max domestication level of a horse should never be less than 1"

add infinity value to max domestication level of {_e}
set max domestication level of {_e} to infinity value
add NaN value to max domestication level of {_e}
set max domestication level of {_e} to NaN value
assert max domestication level of {_e} is 1 with "infinity and NaN values affected the max domestication level of a horse, but shouldn't have"

add infinity value to domestication level of {_e}
set domestication level of {_e} to infinity value
add NaN value to domestication level of {_e}
set domestication level of {_e} to NaN value
assert domestication level of {_e} is 1 with "infinity and NaN values affected the domestication level of a horse, but shouldn't have"

assert max domestication level of {_z} is not set with "the max domestication of a non-horse should be null"
assert domestication level of {_z} is not set with "the domestication of a non-horse should be null"
Expand Down

0 comments on commit bf6a659

Please sign in to comment.