Search in sources :

Example 21 with CatalogType

use of org.spongepowered.api.CatalogType in project LanternServer by LanternPowered.

the class LanternGameRegistry method registerModulePhase.

@SuppressWarnings("unchecked")
private void registerModulePhase() {
    syncModules();
    for (Class<? extends RegistryModule> moduleClass : this.orderedModules) {
        if (!this.classMap.containsKey(moduleClass)) {
            throw new IllegalStateException("Something funky happened! The module " + moduleClass + " is required but seems to be missing.");
        }
        tryModulePhaseRegistration(this.classMap.get(moduleClass));
        if (this.phase == RegistrationPhase.INIT) {
            Map.Entry<Class<? extends CatalogType>, CatalogRegistryModule<?>> selectedEntry = null;
            for (Map.Entry<Class<? extends CatalogType>, CatalogRegistryModule<?>> entry : this.catalogRegistryMap.entrySet()) {
                if (entry.getValue().getClass() == moduleClass) {
                    selectedEntry = entry;
                    break;
                }
            }
            if (selectedEntry == null) {
                continue;
            }
            final CatalogRegistryModule module = selectedEntry.getValue();
            if (module instanceof AdditionalCatalogRegistryModule && module.getClass().getAnnotation(CustomRegistrationPhase.class) == null) {
                this.game.getEventManager().post(new LanternGameRegistryRegisterEvent(CauseStack.current().getCurrentCause(), selectedEntry.getKey(), (AdditionalCatalogRegistryModule) module));
            }
        }
    }
    registerAdditionalPhase();
}
Also used : PluginCatalogType(org.lanternpowered.server.catalog.PluginCatalogType) CatalogType(org.spongepowered.api.CatalogType) AdditionalCatalogRegistryModule(org.spongepowered.api.registry.AdditionalCatalogRegistryModule) AlternateCatalogRegistryModule(org.spongepowered.api.registry.AlternateCatalogRegistryModule) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) LanternGameRegistryRegisterEvent(org.lanternpowered.server.event.registry.LanternGameRegistryRegisterEvent) AdditionalCatalogRegistryModule(org.spongepowered.api.registry.AdditionalCatalogRegistryModule) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 22 with CatalogType

use of org.spongepowered.api.CatalogType in project Nucleus by NucleusPowered.

the class ItemInfoCommand method executeCommand.

@Override
public CommandResult executeCommand(Player player, CommandContext args) throws Exception {
    Optional<CatalogType> catalogTypeOptional = args.getOne(key);
    ItemStack it;
    if (catalogTypeOptional.isPresent()) {
        CatalogType ct = catalogTypeOptional.get();
        if (ct instanceof ItemType) {
            it = ((ItemType) ct).getTemplate().createStack();
        } else {
            BlockState bs = ((BlockState) ct);
            it = bs.getType().getItem().orElseThrow(() -> new CommandException(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.invalidblockstate"))).getTemplate().createStack();
            it.offer(Keys.ITEM_BLOCKSTATE, bs);
        }
    } else if (player.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        it = player.getItemInHand(HandTypes.MAIN_HAND).get();
    } else {
        player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.none"));
        return CommandResult.empty();
    }
    final List<Text> lt = new ArrayList<>();
    String id = it.getType().getId().toLowerCase();
    lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.id", it.getType().getId(), it.getTranslation().get()));
    Optional<BlockState> obs = it.get(Keys.ITEM_BLOCKSTATE);
    if (obs.isPresent()) {
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.extendedid", obs.get().getId()));
        id = obs.get().getId().toLowerCase();
    }
    if (args.hasAny("e") || args.hasAny("extended")) {
        // For each key, see if the item supports it. If so, get and
        // print the value.
        DataScanner.getInstance().getKeysForHolder(it).entrySet().stream().filter(x -> x.getValue() != null).filter(x -> {
            // Work around a Sponge bug.
            try {
                return it.supports(x.getValue());
            } catch (Exception e) {
                return false;
            }
        }).forEach(x -> {
            Key<? extends BaseValue<Object>> k = (Key<? extends BaseValue<Object>>) x.getValue();
            if (it.get(k).isPresent()) {
                DataScanner.getText(player, "command.iteminfo.key", x.getKey(), it.get(k).get()).ifPresent(lt::add);
            }
        });
    }
    ItemDataNode itemDataNode = itemDataService.getDataForItem(id);
    // /buy and /sell prices
    if (econHelper.economyServiceExists() && plugin.getModuleContainer().isModuleLoaded(ServerShopModule.ID)) {
        boolean space = false;
        double buyPrice = itemDataNode.getServerBuyPrice();
        if (buyPrice >= 0) {
            lt.add(Text.EMPTY);
            lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.buyprice", econHelper.getCurrencySymbol(buyPrice)));
            space = true;
        }
        double sellPrice = itemDataNode.getServerSellPrice();
        if (sellPrice >= 0) {
            if (!space) {
                lt.add(Text.EMPTY);
            }
            lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.sellprice", econHelper.getCurrencySymbol(sellPrice)));
        }
    }
    List<String> aliases = itemDataNode.getAliases();
    if (!aliases.isEmpty()) {
        lt.add(Text.EMPTY);
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.list.aliases"));
        Text.Builder tb = Text.builder();
        Iterator<String> iterator = aliases.iterator();
        tb.append(Text.of(TextColors.YELLOW, iterator.next()));
        while (iterator.hasNext()) {
            tb.append(comma, Text.of(TextColors.YELLOW, iterator.next()));
        }
        lt.add(tb.build());
    }
    Sponge.getServiceManager().provideUnchecked(PaginationService.class).builder().contents(lt).padding(Text.of(TextColors.GREEN, "-")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.list.header")).sendTo(player);
    return CommandResult.success();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Keys(org.spongepowered.api.data.key.Keys) DataScanner(io.github.nucleuspowered.nucleus.internal.DataScanner) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) EconHelper(io.github.nucleuspowered.nucleus.internal.EconHelper) HashMap(java.util.HashMap) ServerShopModule(io.github.nucleuspowered.nucleus.modules.servershop.ServerShopModule) GenericArguments(org.spongepowered.api.command.args.GenericArguments) Key(org.spongepowered.api.data.key.Key) ArrayList(java.util.ArrayList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemDataService(io.github.nucleuspowered.nucleus.dataservices.ItemDataService) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) Iterator(java.util.Iterator) BaseValue(org.spongepowered.api.data.value.BaseValue) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) ItemAliasArgument(io.github.nucleuspowered.nucleus.argumentparsers.ItemAliasArgument) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode) BlockState(org.spongepowered.api.block.BlockState) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) ItemType(org.spongepowered.api.item.ItemType) ItemType(org.spongepowered.api.item.ItemType) ArrayList(java.util.ArrayList) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) CommandResult(org.spongepowered.api.command.CommandResult) BaseValue(org.spongepowered.api.data.value.BaseValue) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) CommandException(org.spongepowered.api.command.CommandException) CatalogType(org.spongepowered.api.CatalogType) BlockState(org.spongepowered.api.block.BlockState) PaginationService(org.spongepowered.api.service.pagination.PaginationService) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Key(org.spongepowered.api.data.key.Key) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Example 23 with CatalogType

use of org.spongepowered.api.CatalogType in project Nucleus by NucleusPowered.

the class ClearItemAliasesCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    CatalogType al = args.<CatalogType>getOne(item).get();
    String id = al.getId().toLowerCase();
    ItemDataNode node = itemDataService.getDataForItem(id);
    node.clearAliases();
    itemDataService.setDataForItem(id, node);
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.removeitemalias.cleared", id));
    return CommandResult.success();
}
Also used : CatalogType(org.spongepowered.api.CatalogType) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Example 24 with CatalogType

use of org.spongepowered.api.CatalogType in project Nucleus by NucleusPowered.

the class SetItemAliasCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    // Do we have an item or blockstate?
    String a = args.<String>getOne(alias).get().toLowerCase();
    if (itemDataService.getIdFromAlias(a).isPresent()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.setitemalias.inuse", a));
        return CommandResult.empty();
    }
    if (!ItemDataNode.ALIAS_PATTERN.matcher(a).matches()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.setitemalias.notvalid", a));
        return CommandResult.empty();
    }
    CatalogType type = getCatalogTypeFromHandOrArgs(src, item, args);
    // Set the alias.
    String id = type.getId().toLowerCase();
    ItemDataNode idn = itemDataService.getDataForItem(id);
    idn.addAlias(a);
    itemDataService.setDataForItem(id, idn);
    // Tell the user
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.setitemalias.success", a, id));
    return CommandResult.success();
}
Also used : CatalogType(org.spongepowered.api.CatalogType) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Example 25 with CatalogType

use of org.spongepowered.api.CatalogType in project Nucleus by NucleusPowered.

the class BuyCommand method executeCommand.

@Override
public CommandResult executeCommand(final Player src, CommandContext args) throws Exception {
    CatalogType ct = args.<CatalogType>getOne(itemKey).get();
    int amount = args.<Integer>getOne(amountKey).get();
    ItemDataNode node = itemDataService.getDataForItem(ct.getId());
    if (node.getServerBuyPrice() < 0) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itembuy.notforsale"));
        return CommandResult.empty();
    }
    if (amount > max) {
        amount = max;
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itembuy.maximum", String.valueOf(amount)));
    }
    final ItemStack created;
    if (ct instanceof ItemType) {
        created = ItemStack.of((ItemType) ct, amount);
    } else {
        created = ItemStack.builder().fromBlockState((BlockState) ct).quantity(amount).build();
    }
    // Get the cost.
    final double perUnitCost = node.getServerBuyPrice();
    final int unitCount = amount;
    final double overallCost = perUnitCost * unitCount;
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itembuy.summary", Text.of(String.valueOf(amount)), Text.of(created), Text.of(econHelper.getCurrencySymbol(overallCost))));
    if (args.hasAny("y")) {
        new BuyCallback(src, overallCost, created, unitCount, perUnitCost).accept(src);
    } else {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itembuy.clickhere").toBuilder().onClick(TextActions.executeCallback(new BuyCallback(src, overallCost, created, unitCount, perUnitCost))).build());
    }
    return CommandResult.success();
}
Also used : CatalogType(org.spongepowered.api.CatalogType) BlockState(org.spongepowered.api.block.BlockState) ItemType(org.spongepowered.api.item.ItemType) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Aggregations

CatalogType (org.spongepowered.api.CatalogType)26 Map (java.util.Map)12 ArrayList (java.util.ArrayList)8 ItemDataNode (io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)7 DataView (org.spongepowered.api.data.DataView)6 Nullable (javax.annotation.Nullable)5 CatalogRegistryModule (org.spongepowered.api.registry.CatalogRegistryModule)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Collection (java.util.Collection)4 List (java.util.List)4 Optional (java.util.Optional)4 Sponge (org.spongepowered.api.Sponge)4 DataContainer (org.spongepowered.api.data.DataContainer)4 Text (org.spongepowered.api.text.Text)4 ImmutableList (com.google.common.collect.ImmutableList)3 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 IdentityHashMap (java.util.IdentityHashMap)3 BlockState (org.spongepowered.api.block.BlockState)3 DataSerializable (org.spongepowered.api.data.DataSerializable)3