Search in sources :

Example 1 with KitData

use of org.cubeengine.module.module.kits.data.KitData in project modules-extra by CubeEngine.

the class Kit method give.

public boolean give(CommandSource sender, Player player, boolean force) {
    if (!force && this.getPermission() != null) {
        if (!sender.hasPermission(getPermission().getId())) {
            throw new PermissionDeniedException(getPermission());
        }
    }
    if (!force) {
        if (limitUsagePerPlayer > 0) {
            boolean reached = false;
            Optional<KitData> kitData = player.get(KitData.class);
            if (kitData.isPresent()) {
                reached = limitUsagePerPlayer <= kitData.get().getTimes().getOrDefault(this.name, 0);
            }
            if (reached) {
                // TODO this messages are not displayed & translated
                throw new IncorrectUsageException(false, "Kit-limit reached.");
            }
        }
        if (limitUsageDelay > 0) {
            boolean inDelay = false;
            // System.currentTimeMillis() - lastUsage < limitUsageDelay)
            Optional<KitData> kitData = player.get(KitData.class);
            if (kitData.isPresent()) {
                inDelay = limitUsageDelay <= System.currentTimeMillis() - kitData.get().getTime().getOrDefault(this.name, System.currentTimeMillis());
            }
            if (inDelay) {
                throw new IncorrectUsageException(false, "This kit isn't available at the moment. Try again later!");
            }
        }
    }
    // TODO what if not enough place
    items.forEach(i -> player.getInventory().offer(i.copy()));
    KitData kitData = player.get(KitData.class).orElse(null);
    if (kitData == null) {
        kitData = new KitData();
    }
    kitData.getTime().put(name, System.currentTimeMillis());
    kitData.getTimes().put(name, kitData.getTimes().getOrDefault(name, 0) + 1);
    player.offer(kitData);
    this.executeCommands(player);
    return true;
}
Also used : KitData(org.cubeengine.module.module.kits.data.KitData) PermissionDeniedException(org.cubeengine.libcube.service.command.exception.PermissionDeniedException) IncorrectUsageException(org.cubeengine.butler.parameter.IncorrectUsageException)

Aggregations

IncorrectUsageException (org.cubeengine.butler.parameter.IncorrectUsageException)1 PermissionDeniedException (org.cubeengine.libcube.service.command.exception.PermissionDeniedException)1 KitData (org.cubeengine.module.module.kits.data.KitData)1