From a4a923c23c7c30d6210589b5701fadb59c05976c Mon Sep 17 00:00:00 2001 From: sauilitired Date: Thu, 15 Nov 2018 00:08:32 +0100 Subject: [PATCH] Extract duplicated switch statement in EntityUtil --- .../plotsquared/plot/util/EntityUtil.java | 76 ++++++++----------- 1 file changed, 30 insertions(+), 46 deletions(-) diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/EntityUtil.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/EntityUtil.java index e6cb63285..c62b226d4 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/EntityUtil.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/EntityUtil.java @@ -4,6 +4,7 @@ import com.github.intellectualsites.plotsquared.plot.config.Settings; import com.github.intellectualsites.plotsquared.plot.flag.Flags; import com.github.intellectualsites.plotsquared.plot.flag.IntegerFlag; import com.github.intellectualsites.plotsquared.plot.object.Plot; +import lombok.NonNull; import lombok.experimental.UtilityClass; /** @@ -12,35 +13,40 @@ import lombok.experimental.UtilityClass; @UtilityClass public final class EntityUtil { + private static int capNumeral(@NonNull final String flagName) { + int i; + switch (flagName) { + case "entity-cap": + i = 0; + break; + case "mob-cap": + i = 3; + break; + case "hostile-cap": + i = 2; + break; + case "animal-cap": + i = 1; + break; + case "vehicle-cap": + i = 4; + break; + case "misc-cap": + i = 5; + break; + default: + i = 0; + } + return i; + } + public static boolean checkEntity(Plot plot, IntegerFlag... flags) { if (Settings.Done.RESTRICT_BUILDING && Flags.DONE.isSet(plot)) { return true; } int[] mobs = null; for (IntegerFlag flag : flags) { - int i; - switch (flag.getName()) { - case "entity-cap": - i = 0; - break; - case "mob-cap": - i = 3; - break; - case "hostile-cap": - i = 2; - break; - case "animal-cap": - i = 1; - break; - case "vehicle-cap": - i = 4; - break; - case "misc-cap": - i = 5; - break; - default: - i = 0; - } + final int i = capNumeral(flag.getName()); int cap = plot.getFlag(flag, Integer.MAX_VALUE); if (cap == Integer.MAX_VALUE) { continue; @@ -59,29 +65,7 @@ public final class EntityUtil { } if (mobs != null) { for (IntegerFlag flag : flags) { - int i; - switch (flag.getName()) { - case "entity-cap": - i = 0; - break; - case "mob-cap": - i = 3; - break; - case "hostile-cap": - i = 2; - break; - case "animal-cap": - i = 1; - break; - case "vehicle-cap": - i = 4; - break; - case "misc-cap": - i = 5; - break; - default: - i = 0; - } + final int i = capNumeral(flag.getName()); mobs[i]++; } plot.setMeta("EntityCount", mobs);