Search in sources :

Example 1 with RequirementMissingException

use of net.citizensnpcs.api.command.exception.RequirementMissingException in project CitizensAPI by CitizensDev.

the class RequirementsProcessor method process.

@Override
public void process(CommandSender sender, CommandContext context, Annotation instance, Object[] methodArgs) throws CommandException {
    Requirements requirements = (Requirements) instance;
    NPC npc = (methodArgs.length >= 3 && methodArgs[2] instanceof NPC) ? (NPC) methodArgs[2] : null;
    boolean canRedefineSelected = context.hasValueFlag("id") && sender.hasPermission("npc.select");
    String error = Messaging.tr(CommandMessages.MUST_HAVE_SELECTED);
    if (canRedefineSelected) {
        npc = CitizensAPI.getNPCRegistry().getById(context.getFlagInteger("id"));
        if (methodArgs.length >= 3) {
            methodArgs[2] = npc;
        }
        if (npc == null) {
            error += ' ' + Messaging.tr(CommandMessages.ID_NOT_FOUND, context.getFlagInteger("id"));
        }
    }
    if (requirements.selected() && npc == null) {
        throw new RequirementMissingException(error);
    }
    if (requirements.ownership() && npc != null && !sender.hasPermission("citizens.admin") && !npc.getTrait(Owner.class).isOwnedBy(sender)) {
        throw new RequirementMissingException(Messaging.tr(CommandMessages.MUST_BE_OWNER));
    }
    if (npc == null)
        return;
    for (Class<? extends Trait> clazz : requirements.traits()) {
        if (!npc.hasTrait(clazz)) {
            throw new RequirementMissingException(Messaging.tr(CommandMessages.MISSING_TRAIT, clazz.getSimpleName()));
        }
    }
    Set<EntityType> types = Sets.newEnumSet(Arrays.asList(requirements.types()), EntityType.class);
    if (types.contains(EntityType.UNKNOWN)) {
        types = EnumSet.allOf(EntityType.class);
    }
    types.removeAll(Sets.newHashSet(requirements.excludedTypes()));
    EntityType type = npc.getTrait(MobType.class).getType();
    if (!types.contains(type)) {
        throw new RequirementMissingException(Messaging.tr(CommandMessages.REQUIREMENTS_INVALID_MOB_TYPE, type.name().toLowerCase().replace('_', ' ')));
    }
    if (requirements.livingEntity() && !type.isAlive()) {
        throw new RequirementMissingException(Messaging.tr(CommandMessages.REQUIREMENTS_MUST_BE_LIVING_ENTITY, methodArgs));
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) EntityType(org.bukkit.entity.EntityType) Owner(net.citizensnpcs.api.trait.trait.Owner) RequirementMissingException(net.citizensnpcs.api.command.exception.RequirementMissingException) MobType(net.citizensnpcs.api.trait.trait.MobType)

Aggregations

RequirementMissingException (net.citizensnpcs.api.command.exception.RequirementMissingException)1 NPC (net.citizensnpcs.api.npc.NPC)1 MobType (net.citizensnpcs.api.trait.trait.MobType)1 Owner (net.citizensnpcs.api.trait.trait.Owner)1 EntityType (org.bukkit.entity.EntityType)1