Exemplo n.º 1
0
        public static void Init(IRegistryManager registryManager, ResourceManager resources, McResourcePack resourcePack, IProgressReceiver progressReceiver = null)
        {
            var blockRegistry = registryManager.GetRegistry <Block>();

            ResourceManager = resources;
            ResourcePack    = resourcePack;

            var otherRaw = ResourceManager.ReadStringResource("Alex.Resources.items3.json");

            SecItemEntries = JsonConvert.DeserializeObject <SecondItemEntry[]>(otherRaw);

            var raw = ResourceManager.ReadStringResource("Alex.Resources.items2.json");

            ItemEntries = JsonConvert.DeserializeObject <ItemEntry[]>(raw);


            var ii     = resources.Registries.Items.Entries;
            var blocks = resources.Registries.Blocks.Entries;

            LoadModels();

            Dictionary <ResourceLocation, Func <Item> > items = new Dictionary <ResourceLocation, Func <Item> >();

            for (int i = 0; i < blocks.Count; i++)
            {
                var entry = blocks.ElementAt(i);
                progressReceiver?.UpdateProgress((int)(100D * ((double)i / (double)blocks.Count)), $"Processing block items...", entry.Key);

                Item item;

                /*if (blockRegistry.TryGet(entry.Key, out var blockState))
                 * {
                 *      item = new ItemBlock(blockState.Value);
                 * }*/
                var bs = BlockFactory.GetBlockState(entry.Key);
                if (!(bs.Block is Air))
                {
                    item = new ItemBlock(bs);
                    //  Log.Info($"Registered block item: {entry.Key}");
                }
                else
                {
                    continue;
                }

                var minetItem = MiNET.Items.ItemFactory.GetItem(entry.Key.Replace("minecraft:", ""));
                if (minetItem != null)
                {
                    if (Enum.TryParse <ItemType>(minetItem.ItemType.ToString(), out ItemType t))
                    {
                        item.ItemType = t;
                    }

                    SetItemMaterial(item, minetItem.ItemMaterial);
                    // item.Material = minetItem.ItemMaterial;

                    item.Meta = minetItem.Metadata;
                    item.Id   = minetItem.Id;
                }

                item.Name        = entry.Key;
                item.DisplayName = entry.Key;

                var data = ItemEntries.FirstOrDefault(x =>
                                                      x.name.Equals(entry.Key.Substring(10), StringComparison.InvariantCultureIgnoreCase));
                if (data != null)
                {
                    item.MaxStackSize = data.stackSize;
                    item.DisplayName  = data.displayName;
                }

                string ns   = ResourceLocation.DefaultNamespace;
                string path = entry.Key;
                if (entry.Key.Contains(':'))
                {
                    var index = entry.Key.IndexOf(':');
                    ns   = entry.Key.Substring(0, index);
                    path = entry.Key.Substring(index + 1);
                }

                var key = new ResourceLocation(ns, $"block/{path}");

                ResourcePackModelBase model;
                if (!(ResourcePack.ItemModels.TryGetValue(key, out model)))
                {
                    foreach (var it in ResourcePack.ItemModels)
                    {
                        if (it.Key.Path.Equals(key.Path, StringComparison.InvariantCultureIgnoreCase))
                        {
                            model = it.Value;
                            break;
                        }
                    }
                }

                if (model != null)
                {
                    item.Renderer = new ItemBlockModelRenderer(bs, model, resourcePack, resources);
                    item.Renderer.Cache(resourcePack);
                }
                else
                {
                    Log.Warn($"Could not find block model renderer for: {key.ToString()}");
                }

                items.TryAdd(entry.Key, () =>
                {
                    return(item.Clone());
                });
            }

            for (int i = 0; i < ii.Count; i++)
            {
                var entry = ii.ElementAt(i);
                progressReceiver?.UpdateProgress(i * (100 / ii.Count), $"Processing items...", entry.Key);

                Item item;

                /*if (blockRegistry.TryGet(entry.Key, out var blockState))
                 * {
                 *      item = new ItemBlock(blockState.Value);
                 * }*/
                /*   if (blocks.ContainsKey(entry.Key) && blockRegistry.TryGet(entry.Key, out var registryEntry))
                 * {
                 *         item = new ItemBlock(registryEntry.Value);
                 * }
                 * else
                 * {*/
                item = new Item();
                // }

                var minetItem = MiNET.Items.ItemFactory.GetItem(entry.Key.Replace("minecraft:", ""));
                if (minetItem != null)
                {
                    if (Enum.TryParse <ItemType>(minetItem.ItemType.ToString(), out ItemType t))
                    {
                        item.ItemType = t;
                    }

                    SetItemMaterial(item, minetItem.ItemMaterial);

                    // item.Material = minetItem.ItemMaterial;
                    item.Meta = minetItem.Metadata;
                    item.Id   = minetItem.Id;
                }

                item.Name        = entry.Key;
                item.DisplayName = entry.Key;

                var data = ItemEntries.FirstOrDefault(x =>
                                                      x.name.Equals(entry.Key.Substring(10), StringComparison.InvariantCultureIgnoreCase));
                if (data != null)
                {
                    item.MaxStackSize = data.stackSize;
                    item.DisplayName  = data.displayName;
                }

                string ns   = ResourceLocation.DefaultNamespace;
                string path = entry.Key;
                if (entry.Key.Contains(':'))
                {
                    var index = entry.Key.IndexOf(':');
                    ns   = entry.Key.Substring(0, index);
                    path = entry.Key.Substring(index + 1);
                }

                var key = new ResourceLocation(ns, $"item/{path}");

                foreach (var it in ResourcePack.ItemModels)
                {
                    if (it.Key.Path.Equals(key.Path, StringComparison.InvariantCultureIgnoreCase))
                    {
                        //Log.Info($"Model found: {entry.Key} = {it.Key}");
                        ItemModelRenderer renderer;
                        if (ItemRenderers.TryGetValue(it.Key, out renderer))
                        {
                        }
                        else if (ItemRenderers.TryGetValue(key, out renderer))

                        {
                        }

                        if (renderer != null)
                        {
                            //Log.Debug($"Found renderer for {entry.Key}, textures: {it.Value.Textures.Count}");
                            item.Renderer = renderer;
                            break;
                        }
                    }
                }

                if (item.Renderer == null)
                {
                    Log.Warn($"Could not find item model renderer for: {key.ToString()}");
                }

                items.TryAdd(key, () => { return(item.Clone()); });
            }

            Items = new ReadOnlyDictionary <ResourceLocation, Func <Item> >(items);
        }
Exemplo n.º 2
0
        public static void Init(IRegistryManager registryManager, ResourceManager resources, IProgressReceiver progressReceiver = null)
        {
            ResourceManager = resources;
            // ResourcePack = resourcePack;

            var otherRaw = ResourceManager.ReadStringResource("Alex.Resources.items3.json");

            SecItemEntries = JsonConvert.DeserializeObject <SecondItemEntry[]>(otherRaw);

            var raw = ResourceManager.ReadStringResource("Alex.Resources.items2.json");

            ItemEntries = JsonConvert.DeserializeObject <ItemEntry[]>(raw);


            var ii     = resources.Registries.Items.Entries;
            var blocks = resources.Registries.Blocks.Entries;

            // LoadModels();

            ConcurrentDictionary <ResourceLocation, Func <Item> > items = new ConcurrentDictionary <ResourceLocation, Func <Item> >();

            // for(int i = 0; i < blocks.Count; i++)
            // List<ResourceLocation> addedCurrently = n
            int done = 0;

            Parallel.ForEach(
                blocks, e =>
            {
                try
                {
                    var entry = e;
                    progressReceiver?.UpdateProgress(done, blocks.Count, $"Processing block items...", entry.Key);

                    Item item;

                    /*if (blockRegistry.TryGet(entry.Key, out var blockState))
                     * {
                     *      item = new ItemBlock(blockState.Value);
                     * }*/
                    var bs = BlockFactory.GetBlockState(entry.Key);

                    if (!(bs.Block is Air) && bs != null)
                    {
                        item = new ItemBlock(bs);
                        //  Log.Info($"Registered block item: {entry.Key}");
                    }
                    else
                    {
                        return;
                    }

                    /*var minetItem = MiNET.Items.ItemFactory.GetItem(entry.Key.Replace("minecraft:", ""));
                     *
                     * if (minetItem != null)
                     * {
                     *      if (Enum.TryParse<ItemType>(minetItem.ItemType.ToString(), out ItemType t))
                     *      {
                     *              item.ItemType = t;
                     *      }
                     *
                     *      SetItemMaterial(item, minetItem.ItemMaterial);
                     *      // item.Material = minetItem.ItemMaterial;
                     *
                     *      item.Meta = minetItem.Metadata;
                     *      item.Id = minetItem.Id;
                     * }*/

                    item.Name        = entry.Key;
                    item.DisplayName = entry.Key;

                    var data = ItemEntries.FirstOrDefault(
                        x => x.name.Equals(entry.Key.Substring(10), StringComparison.OrdinalIgnoreCase));

                    if (data != null)
                    {
                        item.MaxStackSize = data.stackSize;
                        item.DisplayName  = data.displayName;
                    }


                    string ns   = ResourceLocation.DefaultNamespace;
                    string path = entry.Key;

                    if (entry.Key.Contains(':'))
                    {
                        var index = entry.Key.IndexOf(':');
                        ns        = entry.Key.Substring(0, index);
                        path      = entry.Key.Substring(index + 1);
                    }


                    var resourceLocation = new ResourceLocation(ns, $"block/{path}");

                    ResourcePackModelBase model = null;

                    if (!ResourceManager.TryGetBlockModel(resourceLocation, out model))
                    {
                        /*foreach (var it in ResourcePack.ItemModels)
                         * {
                         *      if (it.Key.Path.Equals(key.Path, StringComparison.OrdinalIgnoreCase))
                         *      {
                         *              model = it.Value;
                         *
                         *              break;
                         *      }
                         * }*/
                    }

                    if (model == null)
                    {
                        Log.Debug($"Missing item render definition for block {entry.Key}, using default.");
                        //  model = new ResourcePackItem() {Display = _defaultDisplayElements};
                    }
                    else
                    {
                        item.Renderer = new ItemBlockModelRenderer(bs, model, bs.Block.Animated ? resources.Atlas.GetAtlas(0) : resources.Atlas.GetStillAtlas());
                        item.Renderer.Cache(resources);


                        if (!items.TryAdd(entry.Key, () => { return(item.Clone()); }))
                        {
                            // items[entry.Key] = () => { return item.Clone(); };
                        }
                    }
                }
                finally
                {
                    done++;
                }
            });

            int i = 0;

            Parallel.ForEach(
                ii, (entry) =>
            {
                // var entry = ii.ElementAt(i);
                progressReceiver?.UpdateProgress(i++, ii.Count, $"Processing items...", entry.Key);
                var resourceLocation = new ResourceLocation(entry.Key);
                resourceLocation     = new ResourceLocation(resourceLocation.Namespace, $"item/{resourceLocation.Path}");

                if (items.ContainsKey(resourceLocation))
                {
                    return;
                }

                Item item;

                /*if (blockRegistry.TryGet(entry.Key, out var blockState))
                 * {
                 *      item = new ItemBlock(blockState.Value);
                 * }*/
                /*   if (blocks.ContainsKey(entry.Key) && blockRegistry.TryGet(entry.Key, out var registryEntry))
                 * {
                 *         item = new ItemBlock(registryEntry.Value);
                 * }
                 * else
                 * {*/
                item = new Item();
                // }

                var minetItem = MiNET.Items.ItemFactory.GetItem(resourceLocation.Path);

                if (minetItem != null)
                {
                    if (Enum.TryParse <ItemType>(minetItem.ItemType.ToString(), out ItemType t))
                    {
                        item.ItemType = t;
                    }

                    SetItemMaterial(item, minetItem.ItemMaterial);

                    // item.Material = minetItem.ItemMaterial;
                    item.Meta = minetItem.Metadata;
                    item.Id   = minetItem.Id;
                }

                item.Name        = entry.Key;
                item.DisplayName = entry.Key;

                var data = ItemEntries.FirstOrDefault(
                    x => x.name.Equals(resourceLocation.Path, StringComparison.OrdinalIgnoreCase));

                if (data != null)
                {
                    item.MaxStackSize = data.stackSize;
                    item.DisplayName  = data.displayName;
                }

                ItemModelRenderer renderer;
                if (!ItemRenderers.TryGetValue(resourceLocation, out renderer))
                {
                    if (ResourceManager.TryGetItemModel(resourceLocation, out var model))
                    {
                        renderer = new ItemModelRenderer(model);
                        renderer.Cache(ResourceManager);

                        ItemRenderers.TryAdd(resourceLocation, renderer);
                    }

                    if (renderer == null)
                    {
                        var r = ItemRenderers.FirstOrDefault(
                            x => x.Key.Path.Equals(resourceLocation.Path, StringComparison.OrdinalIgnoreCase));

                        if (r.Value != null)
                        {
                            renderer = r.Value;
                        }
                    }

                    //  if (ResourcePack.ItemModels.TryGetValue(resourceLocation, out var itemModel)) { }
                }

                if (renderer != null)
                {
                    item.Renderer = renderer;
                }

                if (item.Renderer == null)
                {
                    Log.Warn($"Could not find item model renderer for: {resourceLocation}");
                }

                if (!items.TryAdd(resourceLocation, () => { return(item.Clone()); }))
                {
                    //var oldItem = items[resourceLocation];
                    //  items[resourceLocation] = () => { return item.Clone(); };
                }
            });

            Items = new ReadOnlyDictionary <ResourceLocation, Func <Item> >(items);
        }