|
| 1 | +package fr.maxlego08.essentials.commands.commands.utils.lag; |
| 2 | + |
| 3 | +import fr.maxlego08.essentials.api.EssentialsPlugin; |
| 4 | +import fr.maxlego08.essentials.api.commands.CommandResultType; |
| 5 | +import fr.maxlego08.essentials.api.commands.Permission; |
| 6 | +import fr.maxlego08.essentials.api.messages.Message; |
| 7 | +import fr.maxlego08.essentials.zutils.utils.commands.VCommand; |
| 8 | +import org.bukkit.Bukkit; |
| 9 | +import org.bukkit.World; |
| 10 | +import org.bukkit.entity.Entity; |
| 11 | +import org.bukkit.entity.EntityType; |
| 12 | +import org.bukkit.entity.Player; |
| 13 | + |
| 14 | +import java.util.Comparator; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +public class CommandLagWorld extends VCommand { |
| 19 | + |
| 20 | + public CommandLagWorld(EssentialsPlugin plugin) { |
| 21 | + super(plugin); |
| 22 | + this.addSubCommand("world"); |
| 23 | + this.setPermission(Permission.ESSENTIALS_LAG); |
| 24 | + this.setDescription(Message.DESCRIPTION_LAG_WORLD); |
| 25 | + this.addRequireArg("world", (a, b) -> Bukkit.getWorlds().stream().map(World::getName).toList()); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + protected CommandResultType perform(EssentialsPlugin plugin) { |
| 30 | + |
| 31 | + World world = this.argAsWorld(0); |
| 32 | + if (world == null) { |
| 33 | + return CommandResultType.SYNTAX_ERROR; |
| 34 | + } |
| 35 | + |
| 36 | + message(this.sender, Message.COMMAND_LAG_WORLD_HEADER, "%world%", world.getName()); |
| 37 | + Map<EntityType, Long> counts = world.getEntities().stream() |
| 38 | + .filter(entity -> !(entity instanceof Player)) |
| 39 | + .collect(Collectors.groupingBy(Entity::getType, Collectors.counting())); |
| 40 | + |
| 41 | + counts.entrySet().stream() |
| 42 | + .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) |
| 43 | + .forEach(entry -> message(this.sender, Message.COMMAND_LAG_WORLD_ENTRY, |
| 44 | + "%type%", entry.getKey().name().toLowerCase(), |
| 45 | + "%amount%", entry.getValue())); |
| 46 | + |
| 47 | + return CommandResultType.SUCCESS; |
| 48 | + } |
| 49 | +} |
0 commit comments