Search in sources :

Example 1 with ItemTypes

use of org.spongepowered.api.item.ItemTypes 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)

Aggregations

SetMultimap (com.google.common.collect.SetMultimap)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Inject (javax.inject.Inject)1 Singleton (javax.inject.Singleton)1