Skip to content

Commit

Permalink
Use instanceof pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderscoreTud committed Sep 3, 2024
1 parent c1bf355 commit 8a51732
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/main/java/ch/njol/skript/effects/EffExit.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ protected TriggerItem walk(Event event) {
assert false : this;
return null;
}
if (node instanceof SectionExitHandler)
((SectionExitHandler) node).exit(event);
if (node instanceof SectionExitHandler exitHandler)
exitHandler.exit(event);

if (type == EVERYTHING || type == CONDITIONALS && node instanceof SecConditional || type == LOOPS && (node instanceof LoopSection))
i--;
}
return node instanceof LoopSection ? ((LoopSection) node).getActualNext() : node.getNext();
return node instanceof LoopSection ? node.getActualNext() : node.getNext();
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/ch/njol/skript/effects/EffReturn.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ protected TriggerItem walk(Event event) {

TriggerSection parent = getParent();
while (parent != null && parent != handler) {
if (parent instanceof SectionExitHandler)
((SectionExitHandler) parent).exit(event);
if (parent instanceof SectionExitHandler exitHandler)
exitHandler.exit(event);

parent = parent.getParent();
}

if (handler instanceof SectionExitHandler)
((SectionExitHandler) handler).exit(event);
if (handler instanceof SectionExitHandler exitHandler)
exitHandler.exit(event);

return null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ch/njol/skript/lang/ExecutionIntent.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ private StopSections(int levels) {

@Override
public int compareTo(@NotNull ExecutionIntent other) {
if (!(other instanceof StopSections)) {return other.compareTo(this) * -1;}
if (!(other instanceof StopSections))
return other.compareTo(this) * -1;
int levels = ((StopSections) other).levels;
return Integer.compare(this.levels, levels);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/lang/ExpressionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ public Expression<? extends T>[] getExpressions() {
public List<Expression<? extends T>> getAllExpressions() {
List<Expression<? extends T>> expressions = new ArrayList<>();
for (Expression<? extends T> expression : this.expressions) {
if (expression instanceof ExpressionList<?>) {
expressions.addAll(((ExpressionList<? extends T>) expression).getAllExpressions());
if (expression instanceof ExpressionList<? extends T> innerList) {
expressions.addAll(innerList.getAllExpressions());
continue;
}
expressions.add(expression);
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/ch/njol/skript/sections/SecConditional.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public ExecutionIntent triggerExecutionIntent() {
private TriggerItem getSkippedNext() {
TriggerItem next = getActualNext();
while (next instanceof SecConditional && ((SecConditional) next).type != ConditionalType.IF)
next = ((SecConditional) next).getActualNext();
next = next.getActualNext();
return next;
}

Expand Down Expand Up @@ -368,9 +368,7 @@ private static SecConditional getPrecedingConditional(List<TriggerItem> triggerI
// loop through the triggerItems in reverse order so that we find the most recent items first
for (int i = triggerItems.size() - 1; i >= 0; i--) {
TriggerItem triggerItem = triggerItems.get(i);
if (triggerItem instanceof SecConditional) {
SecConditional conditionalSection = (SecConditional) triggerItem;

if (triggerItem instanceof SecConditional conditionalSection) {
if (conditionalSection.type == ConditionalType.ELSE) {
// if the conditional is an else, return null because it belongs to a different condition and ends
// this one
Expand All @@ -391,10 +389,9 @@ private static List<SecConditional> getPrecedingConditionals(List<TriggerItem> t
// loop through the triggerItems in reverse order so that we find the most recent items first
for (int i = triggerItems.size() - 1; i >= 0; i--) {
TriggerItem triggerItem = triggerItems.get(i);
if (!(triggerItem instanceof SecConditional))
if (!(triggerItem instanceof SecConditional conditional))
break;
SecConditional conditional = (SecConditional) triggerItem;
if (conditional.type == ConditionalType.ELSE)
if (conditional.type == ConditionalType.ELSE)
// if the conditional is an else, break because it belongs to a different condition and ends
// this one
break;
Expand All @@ -407,13 +404,12 @@ private static List<SecConditional> getElseIfs(List<TriggerItem> triggerItems) {
List<SecConditional> list = new ArrayList<>();
for (int i = triggerItems.size() - 1; i >= 0; i--) {
TriggerItem triggerItem = triggerItems.get(i);
if (triggerItem instanceof SecConditional) {
SecConditional secConditional = (SecConditional) triggerItem;

if (secConditional.type == ConditionalType.ELSE_IF)
if (triggerItem instanceof SecConditional secConditional) {
if (secConditional.type == ConditionalType.ELSE_IF) {
list.add(secConditional);
else
} else {
break;
}
} else {
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ch/njol/skript/sections/SecLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public boolean init(Expression<?>[] exprs,
protected TriggerItem walk(Event event) {
Iterator<?> iter = currentIter.get(event);
if (iter == null) {
iter = expr instanceof Variable ? ((Variable<?>) expr).variablesIterator(event) : expr.iterator(event);
iter = expr instanceof Variable<?> variable ? variable.variablesIterator(event) : expr.iterator(event);
if (iter != null) {
if (iter.hasNext())
currentIter.put(event, iter);
Expand Down Expand Up @@ -188,8 +188,8 @@ public void exit(Event event) {
}

private static boolean guaranteedToLoop(Expression<?> expression) {
if (expression instanceof Literal<?>)
return ((Literal<?>) expression).getAll().length > 0;
if (expression instanceof Literal<?> literal)
return literal.getAll().length > 0;
if (!(expression instanceof ExpressionList<?> list))
return false;

Expand Down

0 comments on commit 8a51732

Please sign in to comment.