From cb3e1d5c711d36b8a259fe2a6256725fadf36077 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 14:08:08 +0100 Subject: [PATCH] Optimize CodeStyle --- .../misslewars/scripts/RunnableScript.java | 7 +++++++ src/de/steamwar/misslewars/scripts/Script.java | 3 +-- .../scripts/implemented/FilterScript.java | 3 +-- .../scripts/implemented/LaunchScript.java | 4 +--- .../scripts/implemented/LocationScript.java | 3 +-- .../scripts/implemented/SoundScript.java | 14 +++++++------- .../scripts/implemented/SummonScript.java | 4 +--- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/de/steamwar/misslewars/scripts/RunnableScript.java b/src/de/steamwar/misslewars/scripts/RunnableScript.java index 4f9f688..5328bc3 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScript.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -19,10 +19,17 @@ package de.steamwar.misslewars.scripts; +import java.util.function.UnaryOperator; + public interface RunnableScript { boolean execute(RunnableScriptEvent runnableScriptEvent); interface ScriptFunction { boolean execute(RunnableScriptEvent runnableScriptEvent, double... doubles); } + + default boolean defaultExecution(ScriptFunction scriptFunction, boolean nullReturn, UnaryOperator returnValue, RunnableScriptEvent runnableScriptEvent, double... doubles) { + if (scriptFunction == null) return nullReturn; + return returnValue.apply(scriptFunction.execute(runnableScriptEvent, doubles)); + } } diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java index 8534c34..0b8ac11 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -64,8 +64,7 @@ public class Script { } private static RunnableScript parseScriptSnippet(JsonObject jsonObject) { - if (!jsonObject.has("type")) - return null; + if (!jsonObject.has("type")) return null; switch (jsonObject.getAsJsonPrimitive("type").getAsString().toLowerCase()) { case "delay": return new DelayScript(jsonObject); diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index 85a6de2..4cf250c 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -62,8 +62,7 @@ public class FilterScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - if (filter == null) return true; - return filter.execute(runnableScriptEvent) ^ inverted; + return defaultExecution(filter, true, b -> b ^ inverted, runnableScriptEvent); } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index 24481a0..83b44ed 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -44,10 +44,8 @@ public class LaunchScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - if (launch == null) return false; if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true; - launch.execute(runnableScriptEvent); - return true; + return defaultExecution(launch, false, b -> true, runnableScriptEvent); } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java index 68959c8..1fb272e 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -83,8 +83,7 @@ public class LocationScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { runnableScriptEvent.setLocationType(locationType); - if (locationExecutor != null) locationExecutor.execute(runnableScriptEvent, x, y, z); - return true; + return defaultExecution(locationExecutor, true, b -> true, runnableScriptEvent, x, y, z); } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java index f7f9455..32e40bb 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java @@ -23,9 +23,10 @@ import com.google.gson.JsonObject; import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.ScriptedItem; -import de.steamwar.misslewars.scripts.utils.JsonUtils; import org.bukkit.Sound; -import org.bukkit.entity.Player; + +import static de.steamwar.misslewars.scripts.utils.JsonUtils.getFloat; +import static de.steamwar.misslewars.scripts.utils.JsonUtils.getString; public class SoundScript implements RunnableScript { @@ -34,17 +35,16 @@ public class SoundScript implements RunnableScript { private float pitch; public SoundScript(JsonObject sound) { - JsonUtils.getString(sound, "sound", value -> this.sound = Sound.valueOf(value)); - volume = JsonUtils.getFloat(sound, "volume", 100); - pitch = JsonUtils.getFloat(sound, "pitch", 1); + getString(sound, "sound", value -> this.sound = Sound.valueOf(value)); + volume = getFloat(sound, "volume", 100); + pitch = getFloat(sound, "pitch", 1); } @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { if (sound == null) return false; if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true; - Player player = runnableScriptEvent.getPlayer(); - player.playSound(player.getLocation(), sound, volume, pitch); + runnableScriptEvent.getPlayer().playSound(runnableScriptEvent.getPlayer().getLocation(), sound, volume, pitch); return true; } diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java index 84cbe13..a85ea3a 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -43,9 +43,7 @@ public class SummonScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - if (summon == null) return false; - summon.execute(runnableScriptEvent); - return true; + return defaultExecution(summon, false, b -> true, runnableScriptEvent); } }