Search in sources :

Example 6 with BaseValue

use of org.spongepowered.api.data.value.BaseValue in project LanternServer by LanternPowered.

the class IValueContainer method getValues.

@SuppressWarnings("unchecked")
@Override
default Set<ImmutableValue<?>> getValues() {
    final ImmutableSet.Builder<ImmutableValue<?>> values = ImmutableSet.builder();
    // Check local registrations
    for (KeyRegistration<?, ?> entry : getValueCollection().getAll()) {
        final Key key = entry.getKey();
        final Optional<BaseValue> optValue = getValue(key);
        optValue.ifPresent(baseValue -> values.add(ValueHelper.toImmutable(baseValue)));
    }
    // Check for global registrations
    for (ValueProcessorKeyRegistration<?, ?> registration : LanternValueFactory.get().getKeyRegistrations()) {
        final Optional<BaseValue> optValue = ((Processor) registration).getValueFrom(this);
        optValue.ifPresent(baseValue -> values.add(ValueHelper.toImmutable(baseValue)));
    }
    // Check if custom data is supported by this container
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<?> containers = ((AdditionalContainerHolder<?>) this).getAdditionalContainers();
        containers.getAll().forEach(manipulator -> values.addAll(manipulator.getValues()));
    }
    return values.build();
}
Also used : ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) Processor(org.lanternpowered.server.data.processor.Processor) ImmutableSet(com.google.common.collect.ImmutableSet) BaseValue(org.spongepowered.api.data.value.BaseValue) Key(org.spongepowered.api.data.key.Key)

Example 7 with BaseValue

use of org.spongepowered.api.data.value.BaseValue in project Nucleus by NucleusPowered.

the class EntityInfoCommand method executeCommand.

@Override
public CommandResult executeCommand(Player player, CommandContext args) throws Exception {
    // Get all the entities in the world.
    Vector3i playerPos = player.getLocation().getBlockPosition();
    Collection<Entity> entities = player.getWorld().getEntities().stream().filter(// 11 blocks.
    x -> x.getLocation().getBlockPosition().distanceSquared(playerPos) < 121).collect(Collectors.toList());
    BlockRay<World> bl = BlockRay.from(player).distanceLimit(10).stopFilter(BlockRay.continueAfterFilter(x -> {
        Vector3i pt1 = x.getLocation().getBlockPosition();
        Vector3i pt2 = pt1.add(0, 1, 0);
        return entities.stream().allMatch(e -> {
            Vector3i current = e.getLocation().getBlockPosition();
            // We don't want it to stop until one of these are hit.
            return !(current.equals(pt1) || current.equals(pt2));
        });
    }, 1)).build();
    Optional<BlockRayHit<World>> ob = bl.end();
    if (ob.isPresent()) {
        BlockRayHit<World> brh = ob.get();
        Vector3d location = brh.getLocation().getPosition();
        Vector3d locationOneUp = location.add(0, 1, 0);
        Optional<Entity> entityOptional = entities.stream().filter(e -> {
            Vector3i current = e.getLocation().getBlockPosition();
            return current.equals(location.toInt()) || current.equals(locationOneUp.toInt());
        }).sorted(Comparator.comparingDouble(x -> x.getLocation().getPosition().distanceSquared(location))).findFirst();
        if (entityOptional.isPresent()) {
            // Display info about the entity
            Entity entity = entityOptional.get();
            EntityType type = entity.getType();
            List<Text> lt = new ArrayList<>();
            lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.entityinfo.id", type.getId(), Util.getTranslatableIfPresent(type)));
            lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.entityinfo.uuid", entity.getUniqueId().toString()));
            if (args.hasAny("e") || args.hasAny("extended")) {
                // For each key, see if the entity supports it. If so, get and print the value.
                DataScanner.getInstance().getKeysForHolder(entity).entrySet().stream().filter(x -> x.getValue() != null).filter(x -> {
                    // Work around a Sponge bug.
                    try {
                        return entity.supports(x.getValue());
                    } catch (Exception e) {
                        return false;
                    }
                }).forEach(x -> {
                    Key<? extends BaseValue<Object>> k = (Key<? extends BaseValue<Object>>) x.getValue();
                    if (entity.get(k).isPresent()) {
                        DataScanner.getText(player, "command.entityinfo.key", x.getKey(), entity.get(k).get()).ifPresent(lt::add);
                    }
                });
            }
            Sponge.getServiceManager().provideUnchecked(PaginationService.class).builder().contents(lt).padding(Text.of(TextColors.GREEN, "-")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.entityinfo.list.header", String.valueOf(brh.getBlockX()), String.valueOf(brh.getBlockY()), String.valueOf(brh.getBlockZ()))).sendTo(player);
            return CommandResult.success();
        }
    }
    player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.entityinfo.none"));
    return CommandResult.empty();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) DataScanner(io.github.nucleuspowered.nucleus.internal.DataScanner) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) Vector3d(com.flowpowered.math.vector.Vector3d) HashMap(java.util.HashMap) GenericArguments(org.spongepowered.api.command.args.GenericArguments) BlockRay(org.spongepowered.api.util.blockray.BlockRay) Key(org.spongepowered.api.data.key.Key) ArrayList(java.util.ArrayList) 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) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) CommandResult(org.spongepowered.api.command.CommandResult) Collection(java.util.Collection) BaseValue(org.spongepowered.api.data.value.BaseValue) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) Entity(org.spongepowered.api.entity.Entity) Collectors(java.util.stream.Collectors) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) Vector3i(com.flowpowered.math.vector.Vector3i) World(org.spongepowered.api.world.World) EntityType(org.spongepowered.api.entity.EntityType) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Comparator(java.util.Comparator) Entity(org.spongepowered.api.entity.Entity) BaseValue(org.spongepowered.api.data.value.BaseValue) ArrayList(java.util.ArrayList) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) CommandResult(org.spongepowered.api.command.CommandResult) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) EntityType(org.spongepowered.api.entity.EntityType) Vector3d(com.flowpowered.math.vector.Vector3d) Vector3i(com.flowpowered.math.vector.Vector3i) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Key(org.spongepowered.api.data.key.Key) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit)

Example 8 with BaseValue

use of org.spongepowered.api.data.value.BaseValue 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)

Aggregations

BaseValue (org.spongepowered.api.data.value.BaseValue)8 Key (org.spongepowered.api.data.key.Key)6 Optional (java.util.Optional)4 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Sponge (org.spongepowered.api.Sponge)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 DataScanner (io.github.nucleuspowered.nucleus.internal.DataScanner)2 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)2 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)2 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)2 PermissionInformation (io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation)2 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Processor (org.lanternpowered.server.data.processor.Processor)2 BlockState (org.spongepowered.api.block.BlockState)2 CommandResult (org.spongepowered.api.command.CommandResult)2