Make the entity counting code use the new entity categories, remove usage of magic numbers and add /plot caps

This commit is contained in:
Alexander Söderberg
2020-04-12 19:30:50 +02:00
parent 5772af37fe
commit 79bd69e599
8 changed files with 157 additions and 145 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <T extends PlotFlag<Integer, T>> void sendFormatted(final Plot plot,
final PlotPlayer player, final Class<T> 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));
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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"),
//</editor-fold>
//<editor-fold desc="Caps">
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"),
//</editor-fold>
/**
* Legacy Configuration Conversion
*/

View File

@ -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<br>
@ -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;
}

View File

@ -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;
}

View File

@ -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");