Search in sources :

Example 1 with CooldownTracker

use of org.spongepowered.api.entity.living.player.CooldownTracker in project SpongeCommon by SpongePowered.

the class CooldownTrackerTest method onInit.

@Listener
public void onInit(GameInitializationEvent event) {
    final CommandSpec test = CommandSpec.builder().executor((src, args) -> {
        if (!(src instanceof Player)) {
            throw new CommandException(Text.of(TextColors.RED, "You must be a player to execute this command!"));
        }
        final Player player = (Player) src;
        final CooldownTracker cooldownTracker = player.getCooldownTracker();
        final ItemType itemType = player.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.empty()).getType();
        if (!cooldownTracker.hasCooldown(itemType)) {
            player.sendMessage(Text.of(TextColors.GRAY, "The item type in your hand is not on cooldown!"));
        } else {
            player.sendMessage(Text.of(TextColors.GRAY, "The cooldown remaining for the item type in your hand is ", TextColors.GOLD, cooldownTracker.getCooldown(itemType).orElse(0), TextColors.GRAY, " tick(s)."));
            player.sendMessage(Text.of(TextColors.GRAY, "This item type has ", TextColors.GOLD, new DecimalFormat("#.00").format(cooldownTracker.getFractionRemaining(itemType).orElse(0.0) * 100) + "%", TextColors.GRAY, " of its cooldown remaining."));
        }
        return CommandResult.success();
    }).build();
    final CommandSpec set = CommandSpec.builder().executor((src, args) -> {
        if (!(src instanceof Player)) {
            throw new CommandException(Text.of(TextColors.RED, "You must be a player to execute this command!"));
        }
        final Player player = (Player) src;
        final int cooldown = args.<Integer>getOne("cooldown").orElse(10);
        player.getCooldownTracker().setCooldown(player.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.empty()).getType(), cooldown);
        player.sendMessage(Text.of(TextColors.GRAY, "You have given the item type in your hand a cooldown of ", TextColors.GOLD, cooldown, TextColors.GRAY, " tick(s)."));
        return CommandResult.success();
    }).arguments(GenericArguments.integer(Text.of("cooldown"))).build();
    final CommandSpec enable = CommandSpec.builder().executor(((src, args) -> {
        if (!(src instanceof Player)) {
            throw new CommandException(Text.of(TextColors.RED, "You must be a player to execute this command!"));
        }
        final Player player = (Player) src;
        if (!this.enabled.remove(player.getUniqueId())) {
            this.enabled.add(player.getUniqueId());
            src.sendMessage(Text.of(TextColors.GOLD, "You have enabled the cooldown listeners!"));
        } else {
            src.sendMessage(Text.of(TextColors.GOLD, "You have disabled the cooldown listeners!"));
        }
        return CommandResult.success();
    })).build();
    Sponge.getCommandManager().register(this, CommandSpec.builder().executor(((src, args) -> {
        src.sendMessage(Text.of(TextColors.GOLD, "Use cooldown set|test|enable"));
        return CommandResult.success();
    })).child(test, "test").child(set, "set").child(enable, "enable", "disable").build(), "cooldowns");
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) GameInitializationEvent(org.spongepowered.api.event.game.state.GameInitializationEvent) DecimalFormat(java.text.DecimalFormat) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) UUID(java.util.UUID) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) HashSet(java.util.HashSet) CooldownTracker(org.spongepowered.api.entity.living.player.CooldownTracker) CooldownEvent(org.spongepowered.api.event.entity.living.humanoid.player.CooldownEvent) Plugin(org.spongepowered.api.plugin.Plugin) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Text(org.spongepowered.api.text.Text) Order(org.spongepowered.api.event.Order) HandTypes(org.spongepowered.api.data.type.HandTypes) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) ItemType(org.spongepowered.api.item.ItemType) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) ItemType(org.spongepowered.api.item.ItemType) DecimalFormat(java.text.DecimalFormat) CommandException(org.spongepowered.api.command.CommandException) CooldownTracker(org.spongepowered.api.entity.living.player.CooldownTracker) Listener(org.spongepowered.api.event.Listener)

Aggregations

DecimalFormat (java.text.DecimalFormat)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Sponge (org.spongepowered.api.Sponge)1 CommandException (org.spongepowered.api.command.CommandException)1 CommandResult (org.spongepowered.api.command.CommandResult)1 GenericArguments (org.spongepowered.api.command.args.GenericArguments)1 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)1 HandTypes (org.spongepowered.api.data.type.HandTypes)1 CooldownTracker (org.spongepowered.api.entity.living.player.CooldownTracker)1 Player (org.spongepowered.api.entity.living.player.Player)1 Listener (org.spongepowered.api.event.Listener)1 Order (org.spongepowered.api.event.Order)1 CooldownEvent (org.spongepowered.api.event.entity.living.humanoid.player.CooldownEvent)1 GameInitializationEvent (org.spongepowered.api.event.game.state.GameInitializationEvent)1 ItemType (org.spongepowered.api.item.ItemType)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1 Plugin (org.spongepowered.api.plugin.Plugin)1 Text (org.spongepowered.api.text.Text)1