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.Component;
033import net.kyori.adventure.text.ComponentLike;
034import net.kyori.adventure.text.minimessage.tag.Tag;
035import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
036
037import static com.plotsquared.core.util.entity.EntityCategories.CAP_ANIMAL;
038import static com.plotsquared.core.util.entity.EntityCategories.CAP_ENTITY;
039import static com.plotsquared.core.util.entity.EntityCategories.CAP_MISC;
040import static com.plotsquared.core.util.entity.EntityCategories.CAP_MOB;
041import static com.plotsquared.core.util.entity.EntityCategories.CAP_MONSTER;
042import static com.plotsquared.core.util.entity.EntityCategories.CAP_VEHICLE;
043
044@CommandDeclaration(command = "caps",
045        category = CommandCategory.INFO,
046        usage = "/plot caps")
047public class Caps extends SubCommand {
048
049    @Override
050    public boolean onCommand(final PlotPlayer<?> player, final String[] args) {
051        final Plot plot = player.getCurrentPlot();
052        if (plot == null) {
053            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
054            return false;
055        }
056        if (!plot.isAdded(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_CAPS_OTHER)) {
057            player.sendMessage(
058                    TranslatableCaption.of("permission.no_permission"),
059                    TagResolver.resolver("node", Tag.inserting(Permission.PERMISSION_ADMIN_CAPS_OTHER))
060            );
061            return false;
062        }
063        if (plot.getVolume() > Integer.MAX_VALUE) {
064            player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
065            return false;
066        }
067        player.sendMessage(TranslatableCaption.of("info.plot_caps_header"));
068        final int[] countedEntities = plot.countEntities();
069        sendFormatted(plot, player, MobCapFlag.class, countedEntities, "mobs", CAP_MOB);
070        sendFormatted(plot, player, HostileCapFlag.class, countedEntities, "hostile", CAP_MONSTER);
071        sendFormatted(plot, player, AnimalCapFlag.class, countedEntities, "animals", CAP_ANIMAL);
072        sendFormatted(plot, player, VehicleCapFlag.class, countedEntities, "vehicle", CAP_VEHICLE);
073        sendFormatted(plot, player, MiscCapFlag.class, countedEntities, "misc", CAP_MISC);
074        sendFormatted(plot, player, EntityCapFlag.class, countedEntities, "entities", CAP_ENTITY);
075        return true;
076    }
077
078    private <T extends PlotFlag<Integer, T>> void sendFormatted(
079            final Plot plot,
080            final PlotPlayer<?> player, final Class<T> capFlag, final int[] countedEntities,
081            final String name, final int type
082    ) {
083        final int current = countedEntities[type];
084        final int max = plot.getFlag(capFlag);
085        final String percentage = String.format("%.1f", 100 * ((float) current / max));
086        ComponentLike maxBeautified = max >= Integer.MAX_VALUE
087                ? TranslatableCaption.of("info.infinite").toComponent(player)
088                : Component.text(max);
089        player.sendMessage(
090                TranslatableCaption.of("info.plot_caps_format"),
091                TagResolver.builder()
092                        .tag("cap", Tag.inserting(Component.text(name)))
093                        .tag("current", Tag.inserting(Component.text(current)))
094                        .tag("limit", Tag.inserting(maxBeautified))
095                        .tag("percentage", Tag.inserting(Component.text(percentage)))
096                        .build()
097        );
098    }
099
100}