private static void LoadModels() { foreach (var model in ResourcePack.ItemModels) { if (model.Value == null || model.Value.Textures == null || model.Value.Textures.Count == 0) { continue; } ItemRenderers.AddOrUpdate(model.Key, (a) => { var render = new ItemModelRenderer(model.Value, ResourcePack); render.Cache(ResourcePack); return(render); }, (s, renderer) => { var render = new ItemModelRenderer(model.Value, ResourcePack); render.Cache(ResourcePack); return(render); }); } }
private static void LoadModels() { Parallel.ForEach( ResourcePack.ItemModels, model => { if (model.Value == null || model.Value.Textures == null || model.Value.Textures.Count == 0) { return; } ItemRenderers.AddOrUpdate( model.Key, (a) => { var render = new ItemModelRenderer(model.Value); render.Cache(ResourcePack); return(render); }, (s, renderer) => { var render = new ItemModelRenderer(model.Value); render.Cache(ResourcePack); return(render); }); }); }
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); }
private void OnInventorySlotChanged(object sender, SlotChangedEventArgs e) { var inHand = Inventory.MainHand; Log.Info($"Inventory slot changed."); if (inHand == null && ItemRenderer != null) { ItemRenderer = null; return; } if (inHand != null) { if (!string.IsNullOrWhiteSpace(inHand.Name)) { /*var itemModel = Alex.Instance.Resources.ResourcePack.ItemModels.FirstOrDefault(x => * x.Key.Contains(inHand.Name, StringComparison.InvariantCultureIgnoreCase)); * * ItemRenderer = new ItemModelRenderer(itemModel.Value, Alex.Instance.Resources.ResourcePack);*/ var renderer = inHand.Renderer; if (renderer == null) { Log.Warn($"No renderer for item: {inHand.Name}"); return; } var itemModel = renderer.Model; ItemRenderer = renderer; if (this is Player) { if (itemModel.Display.TryGetValue("thirdperson_righthand", out var value)) { ItemRenderer.Rotation = value.Rotation; ItemRenderer.Translation = value.Translation; ItemRenderer.Scale = value.Scale; if (ModelRenderer.GetBone("rightItem", out EntityModelRenderer.ModelBone bone)) { Log.Info($"First Person item model rendering ready."); bone.Attach(ItemRenderer); } else { Log.Warn($"Bone not found: rightItem"); } } else { Log.Warn($"Failed to get item model display element!"); } } else { if (itemModel.Display.TryGetValue("thirdperson_righthand", out var value)) { ItemRenderer.Rotation = value.Rotation; ItemRenderer.Translation = value.Translation; ItemRenderer.Scale = value.Scale; if (ModelRenderer.GetBone("rightItem", out EntityModelRenderer.ModelBone bone)) { Log.Info($"Third Person item model rendering ready."); bone.Attach(ItemRenderer); } } else { Log.Warn($"Failed to get item model display element!"); } } } } }