示例#1
0
        /// <inheritdoc />
        public bool Passes(IBlockAccess world, Vector3 position, string rule, string value)
        {
            if (!MultiPartModelHelper.TryGetBlockface(rule, out BlockFace face))
            {
                return(false);
            }

            Vector3 offset = face.GetVector3();

            offset = position + offset;

            if (value == "side|up")
            {
                return(IsRedstoneWire(world, offset) ||
                       IsRedstoneWire(world, offset + Vector3.Down));
            }
            else if (value == "up")
            {
                return(IsRedstoneWire(world, offset + Vector3.Up));
            }
            else if (value == "none")
            {
                return(!IsRedstoneWire(world, offset));
            }

            return(false);
        }
示例#2
0
        private static BlockStateVariant ResolveVariant(BlockStateResource blockStateResource, BlockState state)
        {
            if (state.VariantMapper.IsMultiPart)
            {
                return(new BlockStateVariant(MultiPartModelHelper.GetModels(state, blockStateResource)));
            }

            int closestMatch = -1;
            KeyValuePair <string, BlockStateVariant> closest = default(KeyValuePair <string, BlockStateVariant>);

            foreach (var v in blockStateResource.Variants)
            {
                int matches = 0;
                //var variantBlockState = Blocks.State.BlockState.FromString(v.Key);
                var variant = Blocks.State.BlockState.ParseData(v.Key);
                foreach (var kv in state)
                {
                    if (variant.TryGetValue(kv.Key, out string vValue))
                    {
                        if (vValue.Equals(kv.Value, StringComparison.OrdinalIgnoreCase))
                        {
                            matches++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (matches > closestMatch)
                {
                    closestMatch = matches;
                    closest      = v;

                    if (matches == state.Count)
                    {
                        break;
                    }
                }
            }

            return(closest.Value);
        }
示例#3
0
文件: Block.cs 项目: lvyitian1/Alex
        public virtual void BlockUpdate(World world, BlockCoordinates position, BlockCoordinates updatedBlock)
        {
            if (BlockState.VariantMapper.IsMultiPart)
            {
                BlockStateResource blockStateResource;

                if (Alex.Instance.Resources.TryGetBlockState(BlockState.Name, out blockStateResource))
                {
                    var state = MultiPartModelHelper.GetBlockState(world, position, BlockState, blockStateResource);
                    if (state != BlockState)
                    {
                        world.SetBlockState(position, state);
                    }
                }
            }
        }
示例#4
0
文件: Block.cs 项目: lvyitian1/Alex
        public virtual BlockState BlockPlaced(IBlockAccess world, BlockState state, BlockCoordinates position)
        {
            //return state;

            if (state.VariantMapper.IsMultiPart)
            {
                BlockStateResource blockStateResource;

                if (Alex.Instance.Resources.TryGetBlockState(state.Name, out blockStateResource))
                {
                    return(MultiPartModelHelper.GetBlockState(world, position, state, blockStateResource));
                }
            }

            return(state);
        }
示例#5
0
        public void UpdateBuffer(GraphicsDevice device, IBlockAccess world, bool keepInMemory)
        {
            if (!Monitor.TryEnter(_dataLock, 0))
            {
                return;
            }

            try
            {
                //var chunkData     = ChunkData;

                if (ChunkData == null)
                {
                    return;
                }

                var chunkPosition = new Vector3(X << 4, 0, Z << 4);
                for (int sectionIndex = 0; sectionIndex < 16; sectionIndex++)
                {
                    var section = Sections[sectionIndex];

                    if (section == null)
                    {
                        continue;
                    }

                    //var sectionY = (sectionIndex << 4);

                    for (int x = 0; x < 16; x++)
                    {
                        for (int z = 0; z < 16; z++)
                        {
                            for (int y = 0; y < 16; y++)
                            {
                                var blockCoordinates = new BlockCoordinates(x, y + (sectionIndex * 16), z);

                                bool scheduled        = section.IsScheduled(x, y, z);
                                bool blockLightUpdate = section.IsBlockLightScheduled(x, y, z);
                                bool skyLightUpdate   = section.IsSkylightUpdateScheduled(x, y, z);


                                if ((!IsNew && !scheduled && !blockLightUpdate && !skyLightUpdate))
                                {
                                    continue;
                                }

                                try
                                {
                                    //ChunkData?.Remove(device, blockCoordinates);

                                    var blockPosition = new BlockCoordinates(
                                        (int)(chunkPosition.X + x), y + (sectionIndex << 4), (int)(chunkPosition.Z + z));

                                    foreach (var state in section.GetAll(x, y, z))
                                    {
                                        var blockState = state.State;
                                        if (blockState == null || blockState.Model == null || blockState.Block == null || !blockState.Block.Renderable)
                                        {
                                            continue;
                                        }

                                        var model = blockState.Model;

                                        if (blockState.Block.RequiresUpdate)
                                        {
                                            var newblockState = blockState.Block.BlockPlaced(
                                                world, blockState, blockPosition);

                                            if (newblockState != blockState)
                                            {
                                                blockState = newblockState;

                                                section.Set(state.Storage, x, y, z, blockState);
                                                model = blockState.Model;
                                            }
                                        }

                                        if (blockState.IsMultiPart)
                                        {
                                            var newBlockState = MultiPartModelHelper.GetBlockState(
                                                world, blockPosition, blockState, blockState.MultiPartHelper);

                                            if (newBlockState != blockState)
                                            {
                                                blockState = newBlockState;

                                                section.Set(state.Storage, x, y, z, blockState);
                                                model = blockState.Model;
                                            }
                                        }

                                        ChunkData?.Remove(device, blockCoordinates);

                                        model.GetVertices(world, ChunkData, blockCoordinates, blockPosition, blockState.Block);
                                    }
                                }
                                finally
                                {
                                    if (scheduled)
                                    {
                                        section.SetScheduled(x, y, z, false);
                                    }

                                    if (blockLightUpdate)
                                    {
                                        section.SetBlockLightScheduled(x, y, z, false);
                                    }

                                    if (skyLightUpdate)
                                    {
                                        section.SetSkyLightUpdateScheduled(x, y, z, false);
                                    }
                                }
                            }
                        }
                    }
                }

                ChunkData?.ApplyChanges(device, true);
                IsNew = false;
            }
            finally
            {
                _previousKeepInMemory = keepInMemory;
                Monitor.Exit(_dataLock);
            }
        }
示例#6
0
        private static BlockModel ResolveModel(ResourceManager resources, McResourcePack resourcePack,
                                               BlockState state)
        {
            string name = state.Name;

            if (string.IsNullOrWhiteSpace(name))
            {
                Log.Warn($"State name is null!");
                return(null);
            }

            if (name.Contains("water"))
            {
                return(new LiquidBlockModel()
                {
                    //	IsFlowing = false,
                    IsLava = false,
                    //	Level = state.GetTypedValue(Water.LEVEL)
                });
            }

            if (name.Contains("lava"))
            {
                return(new LiquidBlockModel()
                {
                    //	IsFlowing = false,
                    IsLava = true,
                    //	Level = state.GetTypedValue(Water.LEVEL)
                });;
            }

            BlockStateResource blockStateResource;

            if (resourcePack.BlockStates.TryGetValue(name, out blockStateResource))
            {
                if (blockStateResource != null && blockStateResource.Parts != null &&
                    blockStateResource.Parts.Length > 0 &&
                    blockStateResource.Parts.All(x => x.Apply.All(b => b.Model != null)))
                {
                    var models = MultiPartModelHelper.GetModels(state, blockStateResource);


                    state.MultiPartHelper = blockStateResource;
                    state.IsMultiPart     = true;
                    state.AppliedModels   = models.Select(x => x.ModelName).ToArray();

                    return(new ResourcePackBlockModel(resources, models));
                }

                if (blockStateResource?.Variants == null ||
                    blockStateResource.Variants.Count == 0)
                {
                    return(null);
                }

                if (blockStateResource.Variants.Count == 1)
                {
                    var v = blockStateResource.Variants.FirstOrDefault();
                    if (v.Value == null)
                    {
                        return(null);
                    }

                    var models = v.Value.Where(x => x.Model?.Elements != null && x.Model.Elements.Length > 0).ToArray();

                    if (models.Length == 0)
                    {
                        return(null);
                    }

                    return(new ResourcePackBlockModel(resources, models.ToArray(), v.Value.ToArray().Length > 1));
                }

                BlockStateVariant blockStateVariant = null;

                var data = state.ToDictionary();
                //	data.Remove("waterlogged");

                int closestMatch = 0;
                KeyValuePair <string, BlockStateVariant> closest = default(KeyValuePair <string, BlockStateVariant>);
                foreach (var v in blockStateResource.Variants)
                {
                    int matches           = 0;
                    var variantBlockState = Blocks.State.BlockState.FromString(v.Key);

                    foreach (var kv in data)
                    {
                        if (variantBlockState.TryGetValue(kv.Key, out string vValue))
                        {
                            if (vValue.Equals(kv.Value, StringComparison.InvariantCultureIgnoreCase))
                            {
                                matches++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (matches > closestMatch)
                    {
                        closestMatch = matches;
                        closest      = v;

                        if (matches == data.Count)
                        {
                            break;
                        }
                    }
                }

                blockStateVariant = closest.Value;

                if (blockStateVariant == null)
                {
                    var a = blockStateResource.Variants.FirstOrDefault();
                    blockStateVariant = a.Value;
                }

                var asArray = blockStateVariant.ToArray();

                if (asArray.Length == 0 || asArray.Any(x => x.Model?.Elements == null || x.Model.Elements.Length == 0))
                {
                    Log.Info($"No elements");
                    return(null);
                }

                return(new ResourcePackBlockModel(resources, asArray, asArray.Length > 1));
            }

            return(null);
        }
示例#7
0
        private static BlockModel ResolveModel(ResourceManager resources,
                                               BlockStateResource blockStateResource,
                                               BlockState state)
        {
            string name = state.Name;

            if (string.IsNullOrWhiteSpace(name))
            {
                Log.Warn($"State name is null!");

                return(null);
            }

            if (name.Contains("water"))
            {
                return(new LiquidBlockModel()
                {
                    IsLava = false
                });
            }

            if (name.Contains("lava"))
            {
                return(new LiquidBlockModel()
                {
                    IsLava = true
                });
            }

            if (blockStateResource != null && blockStateResource.Parts != null && blockStateResource.Parts.Length > 0)
            {
                var models = MultiPartModelHelper.GetModels(state, blockStateResource);
                BlockStateModelWrapper[] modelWrappers = new BlockStateModelWrapper[models.Length];

                for (var index = 0; index < models.Length; index++)
                {
                    var model = models[index];

                    if (resources.BlockModelRegistry.TryGet(model.ModelName, out var registryEntry))
                    {
                        modelWrappers[index] = new BlockStateModelWrapper(model, registryEntry.Value);
                    }
                }

                //	state.MultiPartHelper = blockStateResource;
                state.IsMultiPart = blockStateResource.Parts.Any(x => x.When != null && x.When.Length > 0);
                //state.AppliedModels = models.Select(x => x.ModelName).ToArray();

                return(new ResourcePackBlockModel(resources, modelWrappers));
            }

            if (blockStateResource?.Variants == null || blockStateResource.Variants.Count == 0)
            {
                return(null);
            }

            /*if (blockStateResource.Variants.Count == 1)
             * {
             *      var v = blockStateResource.Variants.FirstOrDefault();
             *
             *      if (v.Value == null)
             *      {
             *              return null;
             *      }
             *
             *      var models =
             *              v.Value.ToArray(); //.Where(x => x.Model?.Elements != null && x.Model.Elements.Length > 0).ToArray();
             *
             *      if (models.Length == 0)
             *      {
             *              return null;
             *      }
             *
             *      return new ResourcePackBlockModel(resources, models.ToArray(), v.Value.ToArray().Length > 1);
             * }*/

            BlockStateVariant blockStateVariant = null;

            //var data = state.ToDictionary();

            int closestMatch = 0;
            KeyValuePair <string, BlockStateVariant> closest = default(KeyValuePair <string, BlockStateVariant>);

            foreach (var v in blockStateResource.Variants)
            {
                int matches = 0;
                //var variantBlockState = Blocks.State.BlockState.FromString(v.Key);
                var variant = Blocks.State.BlockState.ParseData(v.Key);
                foreach (var kv in state)
                {
                    if (variant.TryGetValue(kv.Key, out string vValue))
                    {
                        if (vValue.Equals(kv.Value, StringComparison.OrdinalIgnoreCase))
                        {
                            matches++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (matches > closestMatch)
                {
                    closestMatch = matches;
                    closest      = v;

                    if (matches == state.Count)
                    {
                        break;
                    }
                }
            }

            blockStateVariant = closest.Value;

            if (blockStateVariant == null)
            {
                var a = blockStateResource.Variants.FirstOrDefault();
                blockStateVariant = a.Value;
            }

            var asArray = blockStateVariant.ToArray();

            if (asArray.Length == 0)
            {
                Log.Info($"No elements");

                return(null);
            }

            BlockStateModelWrapper[] wrappers = new BlockStateModelWrapper[asArray.Length];

            for (var index = 0; index < asArray.Length; index++)
            {
                var model = asArray[index];

                if (resources.BlockModelRegistry.TryGet(model.ModelName, out var registryEntry))
                {
                    wrappers[index] = new BlockStateModelWrapper(model, registryEntry.Value);
                }
            }

            return(new ResourcePackBlockModel(resources, wrappers, wrappers.Length > 1));
        }