From 8a517324a2c40813bd5f80cb41795cd4c7ece49d Mon Sep 17 00:00:00 2001 From: _tud <98935832+UnderscoreTud@users.noreply.github.com> Date: Tue, 3 Sep 2024 22:56:46 +0300 Subject: [PATCH] Use instanceof pattern matching --- .../java/ch/njol/skript/effects/EffExit.java | 6 +++--- .../ch/njol/skript/effects/EffReturn.java | 8 ++++---- .../ch/njol/skript/lang/ExecutionIntent.java | 3 ++- .../ch/njol/skript/lang/ExpressionList.java | 4 ++-- .../njol/skript/sections/SecConditional.java | 20 ++++++++----------- .../java/ch/njol/skript/sections/SecLoop.java | 6 +++--- 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffExit.java b/src/main/java/ch/njol/skript/effects/EffExit.java index b1d2be47d96..717bd13f526 100644 --- a/src/main/java/ch/njol/skript/effects/EffExit.java +++ b/src/main/java/ch/njol/skript/effects/EffExit.java @@ -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 diff --git a/src/main/java/ch/njol/skript/effects/EffReturn.java b/src/main/java/ch/njol/skript/effects/EffReturn.java index 0d358bec031..f2c8967dcb8 100644 --- a/src/main/java/ch/njol/skript/effects/EffReturn.java +++ b/src/main/java/ch/njol/skript/effects/EffReturn.java @@ -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; } diff --git a/src/main/java/ch/njol/skript/lang/ExecutionIntent.java b/src/main/java/ch/njol/skript/lang/ExecutionIntent.java index b92ee489e73..e9b7ceb90cf 100644 --- a/src/main/java/ch/njol/skript/lang/ExecutionIntent.java +++ b/src/main/java/ch/njol/skript/lang/ExecutionIntent.java @@ -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); } diff --git a/src/main/java/ch/njol/skript/lang/ExpressionList.java b/src/main/java/ch/njol/skript/lang/ExpressionList.java index cdf2932e90b..e4b68bd880c 100644 --- a/src/main/java/ch/njol/skript/lang/ExpressionList.java +++ b/src/main/java/ch/njol/skript/lang/ExpressionList.java @@ -307,8 +307,8 @@ public Expression[] getExpressions() { public List> getAllExpressions() { List> expressions = new ArrayList<>(); for (Expression expression : this.expressions) { - if (expression instanceof ExpressionList) { - expressions.addAll(((ExpressionList) expression).getAllExpressions()); + if (expression instanceof ExpressionList innerList) { + expressions.addAll(innerList.getAllExpressions()); continue; } expressions.add(expression); diff --git a/src/main/java/ch/njol/skript/sections/SecConditional.java b/src/main/java/ch/njol/skript/sections/SecConditional.java index 6f432fffd59..b57cdd962cd 100644 --- a/src/main/java/ch/njol/skript/sections/SecConditional.java +++ b/src/main/java/ch/njol/skript/sections/SecConditional.java @@ -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; } @@ -368,9 +368,7 @@ private static SecConditional getPrecedingConditional(List 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 @@ -391,10 +389,9 @@ private static List getPrecedingConditionals(List 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; @@ -407,13 +404,12 @@ private static List getElseIfs(List triggerItems) { List 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; } diff --git a/src/main/java/ch/njol/skript/sections/SecLoop.java b/src/main/java/ch/njol/skript/sections/SecLoop.java index d8538c275f1..c77b846bd31 100644 --- a/src/main/java/ch/njol/skript/sections/SecLoop.java +++ b/src/main/java/ch/njol/skript/sections/SecLoop.java @@ -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); @@ -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;