Search in sources :

Example 1 with GameInitializationEvent

use of org.spongepowered.api.event.game.state.GameInitializationEvent 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)

Example 2 with GameInitializationEvent

use of org.spongepowered.api.event.game.state.GameInitializationEvent in project SpongeCommon by SpongePowered.

the class InventoryQueryTest method onInitialization.

@Listener
public void onInitialization(GameInitializationEvent e) {
    CommandSpec inventoryType = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory hotbar = inventory.query(QueryOperationTypes.INVENTORY_TYPE.of(Hotbar.class));
        src.sendMessage(Text.of("You have ", hotbar.totalItems(), " items in your hotbar."));
        return CommandResult.success();
    }).build();
    CommandSpec itemType = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory sticks = inventory.query(QueryOperationTypes.ITEM_TYPE.of(ItemTypes.STICK));
        src.sendMessage(Text.of("You have ", sticks.totalItems(), " sticks in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec itemStackGeneral = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        ItemStack lapis = ItemStack.of(ItemTypes.DYE, 4);
        lapis.offer(Keys.DYE_COLOR, DyeColors.BLUE);
        Inventory lapisItems = inventory.query(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(lapis));
        src.sendMessage(Text.of("You have ", lapisItems.totalItems(), " lapis lazuli in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec itemStackSpecific = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        ItemStack lapis = ItemStack.of(ItemTypes.DYE, 4);
        lapis.offer(Keys.DYE_COLOR, DyeColors.BLUE);
        Inventory lapisItems = inventory.query(QueryOperationTypes.ITEM_STACK_EXACT.of(lapis));
        src.sendMessage(Text.of("You have ", lapisItems.size(), " stacks of 4 lapis lazuli in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec itemStackCustom = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory evenCountStacks = inventory.query(QueryOperationTypes.ITEM_STACK_CUSTOM.of(x -> x.getQuantity() > 0 && x.getQuantity() % 2 == 0));
        src.sendMessage(Text.of("You have ", evenCountStacks.size(), " stacks with an even number of items in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec inventoryProperty = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory slots = ((PlayerInventory) inventory).getHotbar().query(QueryOperationTypes.INVENTORY_PROPERTY.of(new SlotIndex(3, Property.Operator.LESS)));
        src.sendMessage(Text.of("You have ", slots.totalItems(), " items in the first 3 slots of your hotbar."));
        return CommandResult.success();
    }).build();
    CommandSpec inventoryTranslation = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory slots = ((PlayerInventory) inventory).getHotbar().query(QueryOperationTypes.INVENTORY_TRANSLATION.of(Sponge.getRegistry().getTranslationById("slot.name").get()));
        src.sendMessage(Text.of("You have ", slots.totalItems(), " items in your hotbar."));
        return CommandResult.success();
    }).build();
    Sponge.getCommandManager().register(this, CommandSpec.builder().child(inventoryType, "inventorytype").child(itemType, "itemtype").child(itemStackGeneral, "itemstackgeneral").child(itemStackSpecific, "itemstackspecific").child(itemStackCustom, "itemstackcustom").child(inventoryProperty, "inventoryproperty").child(inventoryTranslation, "inventorytranslation").build(), "invquery");
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) CommandResult(org.spongepowered.api.command.CommandResult) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) Keys(org.spongepowered.api.data.key.Keys) CommandSource(org.spongepowered.api.command.CommandSource) Hotbar(org.spongepowered.api.item.inventory.entity.Hotbar) GameInitializationEvent(org.spongepowered.api.event.game.state.GameInitializationEvent) ItemTypes(org.spongepowered.api.item.ItemTypes) Sponge(org.spongepowered.api.Sponge) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) Plugin(org.spongepowered.api.plugin.Plugin) ItemStack(org.spongepowered.api.item.inventory.ItemStack) DyeColors(org.spongepowered.api.data.type.DyeColors) QueryOperationTypes(org.spongepowered.api.item.inventory.query.QueryOperationTypes) Text(org.spongepowered.api.text.Text) Property(org.spongepowered.api.data.Property) Player(org.spongepowered.api.entity.living.player.Player) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) Listener(org.spongepowered.api.event.Listener) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) Listener(org.spongepowered.api.event.Listener)

Example 3 with GameInitializationEvent

use of org.spongepowered.api.event.game.state.GameInitializationEvent in project SpongeCommon by SpongePowered.

the class DamageSourceTest method onInit.

@Listener
public void onInit(GameInitializationEvent event) {
    Sponge.getCommandManager().register(this, CommandSpec.builder().executor((src, args) -> {
        if (src instanceof Player) {
            final UUID uuid = ((Player) src).getUniqueId();
            if (this.activated.contains(uuid)) {
                this.activated.remove(uuid);
                src.sendMessage(Text.of("You have deactivated damage source analysis."));
            } else {
                this.activated.add(uuid);
                src.sendMessage(Text.of("You have activated damage source analysis."));
            }
            return CommandResult.success();
        }
        throw new CommandException(Text.of(TextColors.RED, "You must be a player to execute this command!"));
    }).build(), "dstest");
    final DamageSource damageSource = DamageSource.builder().type(DamageTypes.CUSTOM).exhaustion(5).scalesWithDifficulty().build();
    Sponge.getCommandManager().register(this, CommandSpec.builder().executor((src, args) -> {
        if (src instanceof Player) {
            final Player player = (Player) src;
            player.damage(args.<Double>getOne("damage").orElse(2.0), damageSource);
            player.sendMessage(Text.of("You have damaged yourself with the custom damage source."));
            return CommandResult.success();
        }
        throw new CommandException(Text.of(TextColors.RED, "You must be a player to execute this command!"));
    }).arguments(GenericArguments.doubleNum(Text.of("damage"))).build(), "dsdamage");
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) Getter(org.spongepowered.api.event.filter.Getter) GameInitializationEvent(org.spongepowered.api.event.game.state.GameInitializationEvent) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) DamageTypes(org.spongepowered.api.event.cause.entity.damage.DamageTypes) UUID(java.util.UUID) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) Root(org.spongepowered.api.event.filter.cause.Root) HashSet(java.util.HashSet) Plugin(org.spongepowered.api.plugin.Plugin) Text(org.spongepowered.api.text.Text) Order(org.spongepowered.api.event.Order) DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) CommandException(org.spongepowered.api.command.CommandException) UUID(java.util.UUID) Listener(org.spongepowered.api.event.Listener)

Example 4 with GameInitializationEvent

use of org.spongepowered.api.event.game.state.GameInitializationEvent in project PokexpMultiplier by happyzleaf.

the class PokexpMultiplier method init.

@Listener
public void init(GameInitializationEvent event) {
    PlaceholderUtility.init();
    PokexpConfig.getInstance().setup(configFile, configLoader);
    Pixelmon.EVENT_BUS.register(this);
    CommandSpec reload = CommandSpec.builder().executor((src, args) -> {
        PokexpConfig.getInstance().loadConfig();
        src.sendMessage(Text.of(TextColors.DARK_GREEN, "[" + PLUGIN_NAME + "]", TextColors.GREEN, " Config(s) reloaded!"));
        return CommandResult.success();
    }).description(Text.of("Reload configs.")).permission(PLUGIN_ID + ".admin.reload").build();
    CommandSpec info = CommandSpec.builder().arguments(GenericArguments.optional(GenericArguments.requiringPermission(GenericArguments.onlyOne(GenericArguments.player(Text.of("player"))), PLUGIN_ID + ".info.others"))).executor((src, args) -> {
        if (args.hasAny("player")) {
            Player player = (Player) args.getOne("player").get();
            src.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(AlgorithmUtilities.parseInfoWithValues(player, AlgorithmUtilities.algorithmPerUser(player))));
            return CommandResult.success();
        } else {
            if (src instanceof Player) {
                src.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(AlgorithmUtilities.parseInfoWithValues((Player) src, AlgorithmUtilities.algorithmPerUser((Player) src))));
                return CommandResult.success();
            } else {
                src.sendMessage(Text.of(TextColors.DARK_RED, "[" + PLUGIN_NAME + "]", TextColors.RED, " Your MUST be in-game in order to execute this command."));
                return CommandResult.successCount(0);
            }
        }
    }).permission(PLUGIN_ID + ".info.me").description(Text.of("Get the player's experience algorithm info.")).build();
    CommandSpec pokexp = CommandSpec.builder().child(reload, "reload").child(info, "info").build();
    Sponge.getGame().getCommandManager().register(this, pokexp, "pokexp", "pkexp");
    // Just to check if the method works well
    /*Sponge.getGame().getCommandManager().register(this, CommandSpec.builder()
				.arguments(GenericArguments.onlyOne(GenericArguments.remainingJoinedStrings(Text.of("operation"))))
				.executor(((src, args) -> {
					src.sendMessage(Text.of(TextColors.GREEN, "result: " + AlgorithmUtilities.eval((String) args.getOne("operation").get())));
					return CommandResult.success();
				}))
				.build(), "math");*/
    LOGGER.info("Loaded!");
}
Also used : ExperienceGainEvent(com.pixelmonmod.pixelmon.api.events.ExperienceGainEvent) PlaceholderUtility(com.github.happyzleaf.pokexpmultiplier.placeholder.PlaceholderUtility) DefaultConfig(org.spongepowered.api.config.DefaultConfig) Inject(com.google.inject.Inject) GameInitializationEvent(org.spongepowered.api.event.game.state.GameInitializationEvent) LoggerFactory(org.slf4j.LoggerFactory) GenericArguments(org.spongepowered.api.command.args.GenericArguments) ConfigurationLoader(ninja.leaping.configurate.loader.ConfigurationLoader) Pixelmon(com.pixelmonmod.pixelmon.Pixelmon) PlaceholderHelper(com.github.happyzleaf.pokexpmultiplier.placeholder.PlaceholderHelper) Text(org.spongepowered.api.text.Text) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) Dependency(org.spongepowered.api.plugin.Dependency) TextColors(org.spongepowered.api.text.format.TextColors) CommandResult(org.spongepowered.api.command.CommandResult) Logger(org.slf4j.Logger) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) GamePreInitializationEvent(org.spongepowered.api.event.game.state.GamePreInitializationEvent) Sponge(org.spongepowered.api.Sponge) File(java.io.File) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) Plugin(org.spongepowered.api.plugin.Plugin) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) EntityPixelmon(com.pixelmonmod.pixelmon.entities.pixelmon.EntityPixelmon) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) PixelmonConfig(com.pixelmonmod.pixelmon.config.PixelmonConfig) Player(org.spongepowered.api.entity.living.player.Player) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) Listener(org.spongepowered.api.event.Listener)

Aggregations

Sponge (org.spongepowered.api.Sponge)4 CommandResult (org.spongepowered.api.command.CommandResult)4 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)4 Player (org.spongepowered.api.entity.living.player.Player)4 Listener (org.spongepowered.api.event.Listener)4 GameInitializationEvent (org.spongepowered.api.event.game.state.GameInitializationEvent)4 Plugin (org.spongepowered.api.plugin.Plugin)4 Text (org.spongepowered.api.text.Text)4 CommandException (org.spongepowered.api.command.CommandException)3 GenericArguments (org.spongepowered.api.command.args.GenericArguments)3 TextColors (org.spongepowered.api.text.format.TextColors)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 UUID (java.util.UUID)2 Order (org.spongepowered.api.event.Order)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 PlaceholderHelper (com.github.happyzleaf.pokexpmultiplier.placeholder.PlaceholderHelper)1 PlaceholderUtility (com.github.happyzleaf.pokexpmultiplier.placeholder.PlaceholderUtility)1 Inject (com.google.inject.Inject)1 Pixelmon (com.pixelmonmod.pixelmon.Pixelmon)1