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();
}
Aggregations