Search in sources :

Example 1 with BlockSpawnsConfig

use of io.github.nucleuspowered.nucleus.modules.mob.config.BlockSpawnsConfig in project Nucleus by NucleusPowered.

the class SpawnMobCommand method executeWithPlayer.

@Override
public CommandResult executeWithPlayer(CommandSource src, Player pl, CommandContext args, boolean isSelf) throws Exception {
    // Get the amount
    int amount = args.<Integer>getOne(amountKey).get();
    EntityType et = args.<EntityType>getOne(mobTypeKey).get();
    if (!Living.class.isAssignableFrom(et.getEntityClass())) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnmob.livingonly", et.getTranslation().get()));
    }
    String id = et.getId().toLowerCase();
    if (this.mobConfig.isPerMobPermission() && !permissions.testSuffix(src, "mob." + id.replace(":", "."))) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnmob.mobnoperm", et.getTranslation().get()));
    }
    Optional<BlockSpawnsConfig> config = this.mobConfig.getBlockSpawnsConfigForWorld(pl.getWorld());
    if (config.isPresent() && (config.get().isBlockVanillaMobs() && id.startsWith("minecraft:") || config.get().getIdsToBlock().contains(id))) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnmob.blockedinconfig", et.getTranslation().get()));
    }
    Location<World> loc = pl.getLocation();
    World w = loc.getExtent();
    MessageProvider mp = plugin.getMessageProvider();
    // Count the number of entities spawned.
    int i = 0;
    Entity entityone = null;
    do {
        Entity e = w.createEntity(et, loc.getPosition());
        if (!w.spawnEntity(e)) {
            throw ReturnMessageException.fromKeyText("command.spawnmob.fail", Text.of(e));
        }
        if (entityone == null) {
            entityone = e;
        }
        i++;
    } while (i < Math.min(amount, this.mobConfig.getMaxMobsToSpawn()));
    if (amount > this.mobConfig.getMaxMobsToSpawn()) {
        src.sendMessage(mp.getTextMessageWithFormat("command.spawnmob.limit", String.valueOf(this.mobConfig.getMaxMobsToSpawn())));
    }
    if (i == 0) {
        throw ReturnMessageException.fromKey("command.spawnmob.fail", et.getTranslation().get());
    }
    if (i == 1) {
        src.sendMessage(mp.getTextMessageWithTextFormat("command.spawnmob.success.singular", Text.of(i), Text.of(entityone)));
    } else {
        src.sendMessage(mp.getTextMessageWithTextFormat("command.spawnmob.success.plural", Text.of(i), Text.of(entityone)));
    }
    return CommandResult.success();
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) Entity(org.spongepowered.api.entity.Entity) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) Living(org.spongepowered.api.entity.living.Living) BlockSpawnsConfig(io.github.nucleuspowered.nucleus.modules.mob.config.BlockSpawnsConfig) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World)

Aggregations

ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)1 MessageProvider (io.github.nucleuspowered.nucleus.internal.messages.MessageProvider)1 BlockSpawnsConfig (io.github.nucleuspowered.nucleus.modules.mob.config.BlockSpawnsConfig)1 Entity (org.spongepowered.api.entity.Entity)1 EntityType (org.spongepowered.api.entity.EntityType)1 Living (org.spongepowered.api.entity.living.Living)1 World (org.spongepowered.api.world.World)1