PokeRemovalCommand.java

mail@pastecode.io avatar
unknown
java
2 years ago
6.4 kB
9
Indexable
Never
public class PokeRemovalCommand {

    @CommandProcessor
    public void onCommand(@Sender ICommandSender sender, String[] args) {
        if(args.length == 1) {
            String input = args[0];
            if(input.equalsIgnoreCase("reload")) {
                PokeRemovalForge.getInstance().reloadConfig();
                sender.sendMessage(new TextComponentString("Reloaded config!"));
            }
            if(input.equalsIgnoreCase("clear")) {
                PokeRemovalConfig.RemovalSetting removalSetting = PokeRemovalForge.getInstance()
                        .getConfig().getRemovalSettings().get("clear_command");
                if(removalSetting.isLocked()) {
                    sender.sendMessage(new TextComponentString("§cThe removal is executing, please wait till it done!"));
                    return;
                }
                removalSetting.setLocked(true);
                int pokemonRemoved = 0;
                int itemRemoved = 0;
                for (WorldServer world : FMLCommonHandler.instance().getMinecraftServerInstance().worlds) {
                    if (removalSetting.getBlacklistedWorlds().contains(world.getWorldInfo().getWorldName()))
                        continue;
                    for (Entity entity : world.loadedEntityList) {
                        if (removalSetting.ignoreEntitiesYoungerThan > 0 && entity.ticksExisted <= removalSetting.ignoreEntitiesYoungerThan)
                            continue;
                        if (!removalSetting.getMode().shouldRemoveEntity(removalSetting.getRemovedEntities(), entity))
                            continue;
                        if (entity instanceof EntityPixelmon) {
                            EntityPixelmon pixelmon = (EntityPixelmon) entity;
                            if (pixelmon.battleController != null)
                                continue;
                            if (pixelmon.hasOwner())
                                continue;
                            if (!removalSetting.getMode().shouldRemovePokemon(removalSetting.getMatchingRequirements(), pixelmon.getPokemonData()))
                                continue;
                            ++pokemonRemoved;
                            continue;
                        }
                        entity.setDead();
                        ++itemRemoved;
                    }
                }
                if (!removalSetting.isBroadcastRemoval())
                    return;
                for (String s : removalSetting.getRemovalBroadcast()) {
                    FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().sendMessage(
                            new TextComponentString(
                                    s.replace("&", "§")
                                            .replace("%pokemon_amount%", pokemonRemoved + "")
                                            .replace("%item_amount%", itemRemoved + "")
                            ));
                }
                removalSetting.setLocked(false);
            }
        }

        if(args.length == 2) {
            String input = args[0];
            if(input.equalsIgnoreCase("clear")) {
                WorldServer world = getWorld(args[1]);
                if(world == null) {
                    sender.sendMessage(new TextComponentString("§cCould not find world with name §6" + args[1]));
                    return;
                }
                PokeRemovalConfig.RemovalSetting removalSetting = PokeRemovalForge.getInstance()
                        .getConfig().getRemovalSettings().get("clear_command");
                if(removalSetting.isLocked()) {
                    sender.sendMessage(new TextComponentString("§cThe removal is executing, please wait till it done!"));
                    return;
                }
                removalSetting.setLocked(true);
                int pokemonRemoved = 0;
                int itemRemoved = 0;
                if (removalSetting.getBlacklistedWorlds().contains(world.getWorldInfo().getWorldName())) {
                    sender.sendMessage(new TextComponentString("§cThis world is blacklisted and can't be cleared!"));
                    return;
                }
                for (Entity entity : world.loadedEntityList) {
                    if (removalSetting.ignoreEntitiesYoungerThan > 0 && entity.ticksExisted <= removalSetting.ignoreEntitiesYoungerThan)
                        continue;
                    if (!removalSetting.getMode().shouldRemoveEntity(removalSetting.getRemovedEntities(), entity))
                        continue;
                    if (entity instanceof EntityPixelmon) {
                        EntityPixelmon pixelmon = (EntityPixelmon) entity;
                        if (pixelmon.battleController != null)
                            continue;
                        if (pixelmon.hasOwner())
                            continue;
                        if (!removalSetting.getMode().shouldRemovePokemon(removalSetting.getMatchingRequirements(), pixelmon.getPokemonData()))
                            continue;
                        ++pokemonRemoved;
                        continue;
                    }
                    entity.setDead();
                    ++itemRemoved;
                }
                if (!removalSetting.isBroadcastRemoval())
                    return;
                for (String s : removalSetting.getRemovalBroadcast()) {
                    FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().sendMessage(
                            new TextComponentString(
                                    s.replace("&", "§")
                                            .replace("%pokemon_amount%", pokemonRemoved + "")
                                            .replace("%item_amount%", itemRemoved + "")
                            ));
                }
                removalSetting.setLocked(false);
            }
        }
    }

    public WorldServer getWorld(String name) {
        return Arrays.stream(FMLCommonHandler.instance().getMinecraftServerInstance().worlds)
                .filter(w -> w.getWorldInfo().getWorldName().equals(name))
                .findAny().orElse(null);
    }
}