From 79bd69e599b6482c7bea57e79032319c9696c8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sun, 12 Apr 2020 19:30:50 +0200 Subject: [PATCH] Make the entity counting code use the new entity categories, remove usage of magic numbers and add `/plot caps` --- .../bukkit/util/BukkitChunkManager.java | 158 ++++-------------- .../plotsquared/plot/commands/Caps.java | 85 ++++++++++ .../plot/commands/MainCommand.java | 1 + .../plotsquared/plot/config/Caption.java | 7 +- .../plotsquared/plot/config/Captions.java | 7 + .../plotsquared/plot/object/Plot.java | 18 +- .../plotsquared/plot/util/EntityUtil.java | 19 ++- .../plot/util/entity/EntityCategories.java | 7 + 8 files changed, 157 insertions(+), 145 deletions(-) create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Caps.java diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java index 4258246ca..4e6085b71 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java @@ -41,7 +41,9 @@ import com.github.intellectualsites.plotsquared.plot.util.TaskManager; import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue; import com.github.intellectualsites.plotsquared.plot.util.block.LocalBlockQueue; import com.github.intellectualsites.plotsquared.plot.util.block.ScopedLocalBlockQueue; +import com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories; import com.github.intellectualsites.plotsquared.plot.util.world.RegionUtil; +import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.math.BlockVector3; @@ -54,8 +56,6 @@ import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.data.BlockData; -import org.bukkit.entity.Animals; -import org.bukkit.entity.Creature; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -71,6 +71,12 @@ import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Semaphore; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_ANIMAL; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_ENTITY; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MISC; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MOB; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MONSTER; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_VEHICLE; import static com.google.common.base.Preconditions.checkNotNull; public class BukkitChunkManager extends ChunkManager { @@ -514,135 +520,27 @@ public class BukkitChunkManager extends ChunkManager { } private void count(int[] count, Entity entity) { - switch (entity.getType()) { - case PLAYER: - // not valid - return; - case SMALL_FIREBALL: - case FIREBALL: - case DROPPED_ITEM: - case EGG: - case THROWN_EXP_BOTTLE: - case SPLASH_POTION: - case SNOWBALL: - case ENDER_PEARL: - case ARROW: - case TRIDENT: - case SHULKER_BULLET: - case SPECTRAL_ARROW: - case DRAGON_FIREBALL: - case LLAMA_SPIT: - // projectile - case PRIMED_TNT: - case FALLING_BLOCK: - // Block entities - case ENDER_CRYSTAL: - case FISHING_HOOK: - case ENDER_SIGNAL: - case EXPERIENCE_ORB: - case LEASH_HITCH: - case FIREWORK: - case LIGHTNING: - case WITHER_SKULL: - case UNKNOWN: - case AREA_EFFECT_CLOUD: - case EVOKER_FANGS: - // non moving / unremovable - break; - case ITEM_FRAME: - case PAINTING: - case ARMOR_STAND: - count[5]++; - break; - // misc - case MINECART: - case MINECART_CHEST: - case MINECART_COMMAND: - case MINECART_FURNACE: - case MINECART_HOPPER: - case MINECART_MOB_SPAWNER: - case MINECART_TNT: - case BOAT: - count[4]++; - break; - case POLAR_BEAR: - case RABBIT: - case SHEEP: - case MUSHROOM_COW: - case OCELOT: - case PIG: - case HORSE: - case SQUID: - case VILLAGER: - case IRON_GOLEM: - case WOLF: - case CHICKEN: - case COW: - case SNOWMAN: - case BAT: - case DONKEY: - case LLAMA: - case SKELETON_HORSE: - case ZOMBIE_HORSE: - case MULE: - case DOLPHIN: - case TURTLE: - case COD: - case PARROT: - case SALMON: - case PUFFERFISH: - case TROPICAL_FISH: - case CAT: - case FOX: - case PANDA: - // animal - count[3]++; - count[1]++; - break; - case BLAZE: - case CAVE_SPIDER: - case CREEPER: - case ENDERMAN: - case ENDERMITE: - case ENDER_DRAGON: - case GHAST: - case GIANT: - case GUARDIAN: - case MAGMA_CUBE: - case PIG_ZOMBIE: - case SILVERFISH: - case SKELETON: - case SLIME: - case SPIDER: - case WITCH: - case WITHER: - case ZOMBIE: - case SHULKER: - case ELDER_GUARDIAN: - case STRAY: - case HUSK: - case EVOKER: - case VEX: - case WITHER_SKELETON: - case ZOMBIE_VILLAGER: - case VINDICATOR: - // monster - count[3]++; - count[2]++; - break; - default: - if (entity instanceof Creature) { - count[3]++; - if (entity instanceof Animals) { - count[1]++; - } else { - count[2]++; - } - } else { - count[4]++; - } + final com.sk89q.worldedit.world.entity.EntityType entityType = + BukkitAdapter.adapt(entity.getType()); + + if (EntityCategories.PLAYER.contains(entityType)) { + return; + } else if (EntityCategories.PROJECTILE.contains(entityType) || + EntityCategories.OTHER.contains(entityType) || + EntityCategories.HANGING.contains(entityType)) { + count[CAP_MISC]++; + } else if (EntityCategories.ANIMAL.contains(entityType) || + EntityCategories.VILLAGER.contains(entityType) || + EntityCategories.TAMEABLE.contains(entityType)) { + count[CAP_MOB]++; + count[CAP_ANIMAL]++; + } else if (EntityCategories.VEHICLE.contains(entityType)) { + count[CAP_VEHICLE]++; + } else if (EntityCategories.HOSTILE.contains(entityType)) { + count[CAP_MOB]++; + count[CAP_MONSTER]++; } - count[0]++; + count[CAP_ENTITY]++; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Caps.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Caps.java new file mode 100644 index 000000000..bc47d3237 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Caps.java @@ -0,0 +1,85 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.github.intellectualsites.plotsquared.plot.commands; + +import com.github.intellectualsites.plotsquared.commands.CommandDeclaration; +import com.github.intellectualsites.plotsquared.plot.config.Captions; +import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.AnimalCapFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.EntityCapFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.HostileCapFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.MiscCapFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.MobCapFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.VehicleCapFlag; +import com.github.intellectualsites.plotsquared.plot.object.Plot; +import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; +import com.github.intellectualsites.plotsquared.plot.util.Permissions; + +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_ANIMAL; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_ENTITY; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MISC; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MOB; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MONSTER; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_VEHICLE; + +@CommandDeclaration(command = "caps", + category = CommandCategory.INFO, + description = "Show plot mob caps", + usage = "/plot caps") +public class Caps extends SubCommand { + + @Override public boolean onCommand(final PlotPlayer player, final String[] args) { + final Plot plot = player.getCurrentPlot(); + if (plot == null) { + return Captions.NOT_IN_PLOT.send(player); + } + if (!plot.isAdded(player.getUUID()) && !Permissions + .hasPermission(player, Captions.PERMISSION_ADMIN_CAPS_OTHER)) { + return Captions.NO_PERMISSION.send(player, Captions.PERMISSION_ADMIN_CAPS_OTHER); + } + Captions.PLOT_CAPS_HEADER.send(player); + final int[] countedEntities = plot.countEntities(); + sendFormatted(plot, player, MobCapFlag.class, countedEntities, "mobs", CAP_MOB); + sendFormatted(plot, player, HostileCapFlag.class, countedEntities, "hostile", CAP_MONSTER); + sendFormatted(plot, player, AnimalCapFlag.class, countedEntities, "animals", CAP_ANIMAL); + sendFormatted(plot, player, VehicleCapFlag.class, countedEntities, "vehicle", CAP_VEHICLE); + sendFormatted(plot, player, MiscCapFlag.class, countedEntities, "misc", CAP_MISC); + sendFormatted(plot, player, EntityCapFlag.class, countedEntities, "entities", CAP_ENTITY); + return true; + } + + private > void sendFormatted(final Plot plot, + final PlotPlayer player, final Class capFlag, final int[] countedEntities, + final String name, final int type) { + final int current = countedEntities[type]; + final int max = plot.getFlag(capFlag); + final String percentage = String.format("%.1f", 100 * ((float) current / max)); + player.sendMessage(Captions.PLOT_CAPS_FORMAT.getTranslated().replace("%cap%", name) + .replace("%current%", Integer.toString(current)) + .replace("%limit%", Integer.toString(max)).replace("%percentage%", percentage)); + } + +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/MainCommand.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/MainCommand.java index eb2b9f004..1b84f5000 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/MainCommand.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/MainCommand.java @@ -63,6 +63,7 @@ public class MainCommand extends Command { public static MainCommand getInstance() { if (instance == null) { instance = new MainCommand(); + new Caps(); new Buy(); new Save(); new Load(); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Caption.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Caption.java index d85d31696..9b73590a9 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Caption.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Caption.java @@ -37,17 +37,18 @@ public interface Caption { return StringMan.replaceFromMap(getTranslated(), Captions.replacements); } - default void send(PlotPlayer caller, String... args) { - send(caller, (Object[]) args); + default boolean send(PlotPlayer caller, String... args) { + return send(caller, (Object[]) args); } - default void send(PlotPlayer caller, Object... args) { + default boolean send(PlotPlayer caller, Object... args) { String msg = CaptionUtility.format(caller, this, args); if (caller == null) { PlotSquared.log(msg); } else { caller.sendMessage(msg); } + return true; } boolean usePrefix(); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java index d29ebd668..d3358fdaf 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java @@ -83,6 +83,7 @@ public enum Captions implements Caption { PERMISSION_COMMANDS_CHAT("plots.admin.command.chat", "static.permissions"), PERMISSION_MERGE_OTHER("plots.merge.other", "static.permissions"), PERMISSION_MERGE_KEEP_ROAD("plots.merge.keeproad", "static.permissions"), + PERMISSION_ADMIN_CAPS_OTHER("plots.admin.caps.other", "static.permissions"), PERMISSION_ADMIN_DESTROY_UNOWNED("plots.admin.destroy.unowned", "static.permissions"), PERMISSION_ADMIN_DESTROY_GROUNDLEVEL("plots.admin.destroy.groundlevel", "static.permissions"), PERMISSION_ADMIN_DESTROY_OTHER("plots.admin.destroy.other", "static.permissions"), @@ -749,6 +750,12 @@ public enum Captions implements Caption { EVENT_DENIED("$1%s $2Cancelled by external plugin.", "Events"), // + // + PLOT_CAPS_HEADER("$3&m---------&r $1CAPS $3&m---------", false, "Info"), + PLOT_CAPS_FORMAT("$2- Cap Type: $1%cap% $2| Status: $1%current%$2/$1%limit% $2($1%percentage%%$2)", + false, "Info"), + // + /** * Legacy Configuration Conversion */ diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index 8467ef0b2..7fe50edf6 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -95,6 +95,12 @@ import java.util.function.Consumer; import java.util.stream.Collectors; import static com.github.intellectualsites.plotsquared.plot.commands.SubCommand.sendMessage; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_ANIMAL; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_VEHICLE; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_ENTITY; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MISC; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MOB; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MONSTER; /** * The plot class
@@ -1278,12 +1284,12 @@ public class Plot { int[] count = new int[6]; for (Plot current : this.getConnectedPlots()) { int[] result = ChunkManager.manager.countEntities(current); - count[0] += result[0]; - count[1] += result[1]; - count[2] += result[2]; - count[3] += result[3]; - count[4] += result[4]; - count[5] += result[5]; + count[CAP_ENTITY] += result[CAP_ENTITY]; + count[CAP_ANIMAL] += result[CAP_ANIMAL]; + count[CAP_MONSTER] += result[CAP_MONSTER]; + count[CAP_MOB] += result[CAP_MOB]; + count[CAP_VEHICLE] += result[CAP_VEHICLE]; + count[CAP_MISC] += result[CAP_MISC]; } return count; } 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 7b654100c..90c841c68 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 @@ -32,6 +32,13 @@ import com.github.intellectualsites.plotsquared.plot.object.Plot; import lombok.NonNull; import lombok.experimental.UtilityClass; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_ANIMAL; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_ENTITY; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MISC; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MOB; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_MONSTER; +import static com.github.intellectualsites.plotsquared.plot.util.entity.EntityCategories.CAP_VEHICLE; + /** * Entity related general utility methods */ @@ -41,23 +48,23 @@ import lombok.experimental.UtilityClass; int i; switch (flagName) { case "mob-cap": - i = 3; + i = CAP_MOB; break; case "hostile-cap": - i = 2; + i = CAP_MONSTER; break; case "animal-cap": - i = 1; + i = CAP_ANIMAL; break; case "vehicle-cap": - i = 4; + i = CAP_VEHICLE; break; case "misc-cap": - i = 5; + i = CAP_MISC; break; case "entity-cap": default: - i = 0; + i = CAP_ENTITY; } return i; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategories.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategories.java index f40854cea..36ec700cd 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategories.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategories.java @@ -30,6 +30,13 @@ package com.github.intellectualsites.plotsquared.plot.util.entity; */ public class EntityCategories { + public static final int CAP_ENTITY = 0; + public static final int CAP_ANIMAL = 1; + public static final int CAP_MONSTER = 2; + public static final int CAP_MOB = 3; + public static final int CAP_VEHICLE = 4; + public static final int CAP_MISC = 5; + public static final EntityCategory ANIMAL = register("animal"); public static final EntityCategory TAMEABLE = register("tameable"); public static final EntityCategory VEHICLE = register("vehicle");