use of io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule in project Nucleus by NucleusPowered.
the class KitResetUsageCommand method executeCommand.
@Override
public CommandResult executeCommand(final CommandSource player, CommandContext args) throws Exception {
Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
User u = args.<User>getOne(user).get();
KitUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(u).get(KitUserDataModule.class);
if (Util.getKeyIgnoreCase(inu.getKitLastUsedTime(), kitInfo.getName()).isPresent()) {
// Remove the key.
inu.removeKitLastUsedTime(kitInfo.getName().toLowerCase());
player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.resetuser.success", u.getName(), kitInfo.getName()));
return CommandResult.success();
}
throw ReturnMessageException.fromKey("command.kit.resetuser.empty", u.getName(), kitInfo.getName());
}
use of io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule in project Nucleus by NucleusPowered.
the class KitHandler method redeemKit.
public RedeemResult redeemKit(Kit kit, Player player, boolean performChecks, boolean isMustGetAll, boolean isFirstJoin) throws KitRedeemException {
KitUserDataModule user = Nucleus.getNucleus().getUserDataManager().get(player.getUniqueId()).get().get(KitUserDataModule.class);
Optional<Instant> oi = Util.getValueIgnoreCase(user.getKitLastUsedTime(), kit.getName());
Instant now = Instant.now();
if (performChecks) {
// If the kit was used before...
if (oi.isPresent()) {
// if it's one time only and the user does not have an exemption...
if (kit.isOneTime() && !player.hasPermission(cph.getPermissionWithSuffix("exempt.onetime"))) {
throw new KitRedeemException("Already redeemed", KitRedeemException.Reason.ALREADY_REDEEMED);
}
// bypass it...
if (!cph.testCooldownExempt(player) && kit.getCooldown().map(Duration::getSeconds).orElse(0L) > 0) {
// ...and we haven't reached the cooldown point yet...
Instant timeForNextUse = oi.get().plus(kit.getCooldown().get());
if (timeForNextUse.isAfter(now)) {
throw new KitRedeemException.Cooldown("Cooldown not expired", Duration.between(now, timeForNextUse));
}
}
}
}
// Kit pre redeem
Cause cause = CauseStackHelper.createCause(player);
NucleusKitEvent.Redeem.Pre preEvent = new KitEvent.PreRedeem(cause, oi.orElse(null), kit, player);
if (Sponge.getEventManager().post(preEvent)) {
throw new KitRedeemException.PreCancelled(preEvent.getCancelMessage().orElse(null));
}
List<Optional<ItemStackSnapshot>> slotList = Lists.newArrayList();
Util.getStandardInventory(player).slots().forEach(x -> slotList.add(x.peek().map(ItemStack::createSnapshot)));
InventoryTransactionResult inventoryTransactionResult = EMPTY_ITR;
if (!kit.getStacks().isEmpty()) {
inventoryTransactionResult = addToStandardInventory(player, kit.getStacks(), isProcessTokens);
if (!isFirstJoin && !inventoryTransactionResult.getRejectedItems().isEmpty() && isMustGetAll) {
Inventory inventory = Util.getStandardInventory(player);
// Slots
Iterator<Inventory> slot = inventory.slots().iterator();
// Slots to restore
slotList.forEach(x -> {
Inventory i = slot.next();
i.clear();
x.ifPresent(y -> i.offer(y.createStack()));
});
// My friend was playing No Man's Sky, I almost wrote "No free slots in suit inventory".
throw new KitRedeemException("No free slots in player inventory", KitRedeemException.Reason.NO_SPACE);
}
}
// If something was consumed, consider a success.
if (inventoryTransactionResult.getType() == InventoryTransactionResult.Type.SUCCESS) {
kit.redeemKitCommands(player);
// permissions or cooldowns change later
if (performChecks) {
user.addKitLastUsedTime(kit.getName(), now);
}
Sponge.getEventManager().post(new KitEvent.PostRedeem(cause, oi.orElse(null), kit, player));
return new KitRedeemResult(inventoryTransactionResult.getRejectedItems(), slotList.stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList()));
} else {
// Failed.
Sponge.getEventManager().post(new KitEvent.FailedRedeem(cause, oi.orElse(null), kit, player));
throw new KitRedeemException("No items were redeemed", KitRedeemException.Reason.UNKNOWN);
}
}
use of io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule in project Nucleus by NucleusPowered.
the class KitListener method onPlayerJoin.
@Listener
public void onPlayerJoin(ClientConnectionEvent.Join event, @Root Player player) {
loader.get(player).ifPresent(p -> {
KitUserDataModule user = loader.get(player.getUniqueId()).get().get(KitUserDataModule.class);
gds.getAutoRedeemable().stream().filter(k -> k.ignoresPermission() || (!this.isSepratePermissions && !player.hasPermission(PermissionRegistry.PERMISSIONS_PREFIX + "kits." + k.getName().toLowerCase()))).forEach(k -> {
try {
handler.redeemKit(k, player, true, this.mustGetAll);
} catch (KitRedeemException e) {
// player.sendMessage(e.getText());
}
});
});
}
use of io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule in project Nucleus by NucleusPowered.
the class KitListCommand method executeCommand.
@Override
public CommandResult executeCommand(final CommandSource src, CommandContext args) throws Exception {
Set<String> kits = KIT_HANDLER.getKitNames();
if (kits.isEmpty()) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.list.empty"));
return CommandResult.empty();
}
PaginationService paginationService = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
ArrayList<Text> kitText = Lists.newArrayList();
final KitUserDataModule user = src instanceof Player ? Nucleus.getNucleus().getUserDataManager().getUnchecked(((Player) src).getUniqueId()).get(KitUserDataModule.class) : null;
final boolean showHidden = kitPermissionHandler.testSuffix(src, "showhidden");
Stream<String> kitStream = KIT_HANDLER.getKitNames(showHidden).stream();
if (this.isSeparatePermissions) {
kitStream = kitStream.filter(kit -> src.hasPermission(KitHandler.getPermissionForKit(kit.toLowerCase())));
}
kitStream.forEach(kit -> kitText.add(createKit(src, user, kit, KIT_HANDLER.getKit(kit).get())));
PaginationList.Builder paginationBuilder = paginationService.builder().contents(kitText).title(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.list.kits")).padding(Text.of(TextColors.GREEN, "-"));
paginationBuilder.sendTo(src);
return CommandResult.success();
}
Aggregations