Search in sources :

Example 6 with Key

use of org.spongepowered.api.data.key.Key in project SpongeCommon by SpongePowered.

the class SpongeItemStackBuilder method build.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ItemStack build() throws IllegalStateException {
    checkState(this.type != null, "Item type has not been set");
    if (this.type == ItemTypes.NONE || this.quantity <= 0) {
        // If either type is none(air) or quantity is 0 return the vanilla EMPTY item
        return ((ItemStack) net.minecraft.item.ItemStack.EMPTY);
    }
    final ItemStack stack = (ItemStack) new net.minecraft.item.ItemStack((Item) this.type, this.quantity, this.damageValue);
    if (this.compound != null) {
        ((net.minecraft.item.ItemStack) stack).setTagCompound(this.compound.copy());
    }
    if (this.itemDataSet != null) {
        this.itemDataSet.forEach(stack::offer);
    }
    if (this.keyValues != null) {
        this.keyValues.entrySet().forEach(entry -> stack.offer((Key) entry.getKey(), entry.getValue()));
    }
    return stack;
}
Also used : Item(net.minecraft.item.Item) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Key(org.spongepowered.api.data.key.Key)

Example 7 with Key

use of org.spongepowered.api.data.key.Key in project core by CubeEngine.

the class MaterialMatcher method buildVariantMap.

private Map<BlockType, Map<String, BlockState>> buildVariantMap() {
    Map<BlockType, Map<String, BlockState>> blockStateItemsByType = new HashMap<>();
    for (Entry<String, BlockState> entry : blockStateItems.entrySet()) {
        BlockType itemType = entry.getValue().getType();
        Map<String, BlockState> itemTypes = blockStateItemsByType.computeIfAbsent(itemType, k -> new HashMap<>());
        itemTypes.put(entry.getKey(), entry.getValue());
    }
    Map<BlockType, Map<String, BlockState>> variants = new HashMap<>();
    blockStateItemsByType.entrySet().stream().filter(e -> e.getValue().size() != 1).forEach(e -> {
        Map<String, Set<String>> variantNames = new HashMap<>();
        Map<List<String>, BlockState> fullVariant = new HashMap<>();
        for (Entry<String, BlockState> entry : e.getValue().entrySet()) {
            String variant = entry.getKey();
            variant = variant.substring(variant.indexOf("[") + 1, variant.indexOf("]"));
            // multiple variants
            String[] split = variant.split(",");
            fullVariant.put(Arrays.asList(split), entry.getValue());
            for (String variantEntry : split) {
                String[] variantEntryPart = variantEntry.split("=");
                Set<String> variantValues = variantNames.computeIfAbsent(variantEntryPart[0], k -> new HashSet<>());
                variantValues.add(variantEntryPart[1]);
            }
        }
        for (Entry<String, Set<String>> entry : variantNames.entrySet()) {
            if (entry.getKey().equals("axis") || entry.getKey().equals("facing") || entry.getKey().equals("half") || entry.getKey().equals("shape") || entry.getKey().equals("open") || entry.getKey().equals("powered") || entry.getKey().equals("stage") || entry.getKey().equals("decayable")) {
                Map<List<String>, BlockState> filtered = new HashMap<>();
                for (Entry<List<String>, BlockState> offender : fullVariant.entrySet()) {
                    List<String> key = new ArrayList<>(offender.getKey());
                    for (String fv : entry.getValue()) {
                        key.remove(entry.getKey() + "=" + fv);
                    }
                    if (!key.isEmpty()) {
                        filtered.put(key, offender.getValue());
                    }
                }
                fullVariant = filtered;
            }
            if (entry.getValue().size() == 1) {
                String singleVariant = entry.getKey() + "=" + entry.getValue().iterator().next();
                fullVariant = fullVariant.entrySet().stream().collect(Collectors.toMap(fv -> {
                    List<String> split = new ArrayList<>(fv.getKey());
                    split.remove(singleVariant);
                    return split;
                }, Entry::getValue));
            }
        }
        for (Entry<List<String>, BlockState> variant : fullVariant.entrySet()) {
            if (variant.getKey().size() > 1) {
                System.out.print(e.getKey().getName() + " Has multiple Variants:");
                for (String s : variant.getKey()) {
                    System.out.print(" " + s);
                }
                System.out.print("\n");
            }
        }
        variants.put(e.getKey(), fullVariant.entrySet().stream().collect(Collectors.toMap(en -> String.join(" ", en.getKey().stream().map(s -> s.split("=")[1]).collect(toList())), Entry::getValue)));
    });
    /*
        for (Entry<ItemType, Map<String, ItemStack>> variant : variants.entrySet())
        {
            System.out.print(variant.getKey().getName() + ":\n");
            for (Entry<String, ItemStack> entry : variant.getValue().entrySet())
            {
                System.out.print("  " + entry.getKey() + ": " + entry.getValue().getTranslation().get() + "\n");
            }
        }
        */
    return variants;
}
Also used : Arrays(java.util.Arrays) Keys(org.spongepowered.api.data.key.Keys) Reflector(org.cubeengine.reflect.Reflector) ItemTypes(org.spongepowered.api.item.ItemTypes) HashMap(java.util.HashMap) LibCube(org.cubeengine.libcube.LibCube) Singleton(javax.inject.Singleton) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Key(org.spongepowered.api.data.key.Key) HashSet(java.util.HashSet) Inject(javax.inject.Inject) SimpleItemStackConverter(org.cubeengine.libcube.service.config.SimpleItemStackConverter) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemTypeConverter(org.cubeengine.libcube.service.config.ItemTypeConverter) ModuleManager(org.cubeengine.libcube.ModuleManager) Locale(java.util.Locale) Map(java.util.Map) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ItemStackConverter(org.cubeengine.libcube.service.config.ItemStackConverter) Builder(org.spongepowered.api.block.BlockState.Builder) GameDictionary(org.spongepowered.api.GameDictionary) BlockTypeConverter(org.cubeengine.libcube.service.config.BlockTypeConverter) Collection(java.util.Collection) DataRegistrationNotFoundException(org.spongepowered.api.data.DataRegistrationNotFoundException) Set(java.util.Set) Sponge(org.spongepowered.api.Sponge) Collectors(java.util.stream.Collectors) SetMultimap(com.google.common.collect.SetMultimap) BlockState(org.spongepowered.api.block.BlockState) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) TreeMap(java.util.TreeMap) Entry(java.util.Map.Entry) BlockType(org.spongepowered.api.block.BlockType) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) ItemType(org.spongepowered.api.item.ItemType) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Entry(java.util.Map.Entry) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 8 with Key

use of org.spongepowered.api.data.key.Key in project LanternServer by LanternPowered.

the class CompositeValueStoreHelper method offerFast.

protected static <H extends ValueContainer<?>> boolean offerFast(ICompositeValueStore<?, H> store, Iterable<H> valueContainers, MergeFunction function) {
    // Leave this, the compiler complains
    final CompositeValueStore store1 = store;
    if (store1 instanceof DataHolder) {
        final Set<Key<?>> keys = new HashSet<>();
        for (H valueContainer : valueContainers) {
            keys.addAll(valueContainer.getKeys());
        }
        final boolean hasListeners = hasListeners(store, keys);
        if (hasListeners) {
            return offer(store, valueContainers, function, () -> true).isSuccessful();
        }
    }
    return store.offerFastNoEvents(valueContainers, function);
}
Also used : DataHolder(org.spongepowered.api.data.DataHolder) CompositeValueStore(org.spongepowered.api.data.value.mutable.CompositeValueStore) LanternKey(org.lanternpowered.server.data.key.LanternKey) Key(org.spongepowered.api.data.key.Key) HashSet(java.util.HashSet)

Example 9 with Key

use of org.spongepowered.api.data.key.Key in project LanternServer by LanternPowered.

the class IValueContainer method matchContents.

/**
 * Matches the contents of the two {@link ValueContainer}s.
 *
 * @param valueContainerA The first value container
 * @param valueContainerB The second value container
 * @return Whether the contents match
 */
static boolean matchContents(IValueContainer<?> valueContainerA, IValueContainer<?> valueContainerB) {
    final boolean additional = valueContainerA instanceof IAdditionalCompositeValueStore;
    if (additional != valueContainerB instanceof IAdditionalCompositeValueStore) {
        return false;
    }
    final ValueCollection valueCollectionA = valueContainerA.getValueCollection();
    final ValueCollection valueCollectionB = valueContainerB.getValueCollection();
    final Collection<Key<?>> keysA = valueCollectionA.getKeys();
    final Collection<Key<?>> keysB = valueCollectionB.getKeys();
    // The same keys have to be present in both of the containers
    if (keysA.size() != keysB.size() || !keysA.containsAll(keysB)) {
        return false;
    }
    for (KeyRegistration<?, ?> registration1 : valueCollectionA.getAll()) {
        final KeyRegistration registration2 = (KeyRegistration) valueCollectionB.get((Key) registration1.getKey()).get();
        // Get the values from both of the containers and match them
        final Object value1 = ((Processor) registration1).getFrom(valueContainerA).orElse(null);
        final Object value2 = ((Processor) registration2).getFrom(valueContainerB).orElse(null);
        if (!Objects.equals(value1, value2)) {
            return false;
        }
    }
    // Match additional containers
    if (additional) {
        final Map<Class<?>, ValueContainer<?>> mapA = ((IAdditionalCompositeValueStore) valueContainerA).getAdditionalContainers().getMap();
        final Map<Class<?>, ValueContainer<?>> mapB = ((IAdditionalCompositeValueStore) valueContainerB).getAdditionalContainers().getMap();
        if (mapA.size() != mapB.size() || !mapA.keySet().containsAll(mapB.keySet())) {
            return false;
        }
        for (Map.Entry<Class<?>, ValueContainer<?>> entry : mapA.entrySet()) {
            final ValueContainer<?> containerA = entry.getValue();
            final ValueContainer<?> containerB = mapB.get(entry.getKey());
            if (!Objects.equals(containerA, containerB)) {
                return false;
            }
        }
    }
    return true;
}
Also used : ValueContainer(org.spongepowered.api.data.value.ValueContainer) ValueProcessorKeyRegistration(org.lanternpowered.server.data.processor.ValueProcessorKeyRegistration) Map(java.util.Map) Key(org.spongepowered.api.data.key.Key)

Example 10 with Key

use of org.spongepowered.api.data.key.Key in project LanternServer by LanternPowered.

the class IDataHolder method supports.

@Override
default boolean supports(Class<? extends DataManipulator<?, ?>> containerClass) {
    checkNotNull(containerClass, "containerClass");
    // Offer all the default key values as long if they are supported
    final Optional<DataManipulatorRegistration> optRegistration = DataManipulatorRegistry.get().getBy(containerClass);
    if (optRegistration.isPresent()) {
        final DataManipulatorRegistration registration = optRegistration.get();
        for (Key key : (Set<Key>) registration.getRequiredKeys()) {
            if (!supports(key)) {
                return false;
            }
        }
        return true;
    }
    // Support all the additional manipulators
    return this instanceof AdditionalContainerHolder;
}
Also used : Set(java.util.Set) DataManipulatorRegistration(org.lanternpowered.server.data.manipulator.DataManipulatorRegistration) Key(org.spongepowered.api.data.key.Key)

Aggregations

Key (org.spongepowered.api.data.key.Key)28 HashMap (java.util.HashMap)8 Map (java.util.Map)8 ArrayList (java.util.ArrayList)7 BlockState (org.spongepowered.api.block.BlockState)7 BaseValue (org.spongepowered.api.data.value.BaseValue)7 List (java.util.List)6 Optional (java.util.Optional)6 Sponge (org.spongepowered.api.Sponge)5 Text (org.spongepowered.api.text.Text)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Collection (java.util.Collection)4 BlockType (org.spongepowered.api.block.BlockType)4 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)4 World (org.spongepowered.api.world.World)4 Set (java.util.Set)3 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)3 CommandContext (org.spongepowered.api.command.args.CommandContext)3 CommandElement (org.spongepowered.api.command.args.CommandElement)3 DataView (org.spongepowered.api.data.DataView)3