001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.command;
020
021import com.plotsquared.core.configuration.caption.TranslatableCaption;
022import com.plotsquared.core.permissions.Permission;
023import com.plotsquared.core.player.PlotPlayer;
024import com.plotsquared.core.plot.Plot;
025import com.plotsquared.core.plot.flag.PlotFlag;
026import com.plotsquared.core.plot.flag.implementations.AnimalCapFlag;
027import com.plotsquared.core.plot.flag.implementations.EntityCapFlag;
028import com.plotsquared.core.plot.flag.implementations.HostileCapFlag;
029import com.plotsquared.core.plot.flag.implementations.MiscCapFlag;
030import com.plotsquared.core.plot.flag.implementations.MobCapFlag;
031import com.plotsquared.core.plot.flag.implementations.VehicleCapFlag;
032import net.kyori.adventure.text.minimessage.Template;
033
034import static com.plotsquared.core.util.entity.EntityCategories.CAP_ANIMAL;
035import static com.plotsquared.core.util.entity.EntityCategories.CAP_ENTITY;
036import static com.plotsquared.core.util.entity.EntityCategories.CAP_MISC;
037import static com.plotsquared.core.util.entity.EntityCategories.CAP_MOB;
038import static com.plotsquared.core.util.entity.EntityCategories.CAP_MONSTER;
039import static com.plotsquared.core.util.entity.EntityCategories.CAP_VEHICLE;
040
041@CommandDeclaration(command = "caps",
042        category = CommandCategory.INFO,
043        usage = "/plot caps")
044public class Caps extends SubCommand {
045
046    @Override
047    public boolean onCommand(final PlotPlayer<?> player, final String[] args) {
048        final Plot plot = player.getCurrentPlot();
049        if (plot == null) {
050            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
051            return false;
052        }
053        if (!plot.isAdded(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_CAPS_OTHER)) {
054            player.sendMessage(
055                    TranslatableCaption.of("permission.no_permission"),
056                    Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_CAPS_OTHER))
057            );
058            return false;
059        }
060        if (plot.getVolume() > Integer.MAX_VALUE) {
061            player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
062            return false;
063        }
064        player.sendMessage(TranslatableCaption.of("info.plot_caps_header"));
065        final int[] countedEntities = plot.countEntities();
066        sendFormatted(plot, player, MobCapFlag.class, countedEntities, "mobs", CAP_MOB);
067        sendFormatted(plot, player, HostileCapFlag.class, countedEntities, "hostile", CAP_MONSTER);
068        sendFormatted(plot, player, AnimalCapFlag.class, countedEntities, "animals", CAP_ANIMAL);
069        sendFormatted(plot, player, VehicleCapFlag.class, countedEntities, "vehicle", CAP_VEHICLE);
070        sendFormatted(plot, player, MiscCapFlag.class, countedEntities, "misc", CAP_MISC);
071        sendFormatted(plot, player, EntityCapFlag.class, countedEntities, "entities", CAP_ENTITY);
072        return true;
073    }
074
075    private <T extends PlotFlag<Integer, T>> void sendFormatted(
076            final Plot plot,
077            final PlotPlayer<?> player, final Class<T> capFlag, final int[] countedEntities,
078            final String name, final int type
079    ) {
080        final int current = countedEntities[type];
081        final int max = plot.getFlag(capFlag);
082        final String percentage = String.format("%.1f", 100 * ((float) current / max));
083        String maxBeautified = max >= Integer.MAX_VALUE
084                ? TranslatableCaption.of("info.infinite").getComponent(player)
085                : String.valueOf(max);
086        player.sendMessage(
087                TranslatableCaption.of("info.plot_caps_format"),
088                Template.of("cap", name),
089                Template.of("current", String.valueOf(current)),
090                Template.of("limit", maxBeautified),
091                Template.of("percentage", percentage)
092        );
093    }
094
095}