コード例 #1
0
        public override void OnTick(Level level)
        {
            if (Inventory == null)
            {
                return;
            }

            Furnace furnace = level.GetBlock(Coordinates) as Furnace;

            if (furnace == null)
            {
                return;
            }

            if (!(furnace is LitFurnace))
            {
                if (GetFuel().Id != 0)
                {
                    LitFurnace litFurnace = new LitFurnace
                    {
                        Coordinates = furnace.Coordinates,
                        Metadata    = furnace.Metadata
                    };

                    level.SetBlock(litFurnace);
                    furnace = litFurnace;

                    BurnTime       = GetFuelEfficiency(GetFuel());
                    FuelEfficiency = BurnTime;
                    CookTime       = 0;
                    Inventory.DecreaseSlot(1);
                }
            }

            if (!(furnace is LitFurnace))
            {
                return;
            }

            if (BurnTime > 0)
            {
                BurnTime--;
                BurnTick = (short)Math.Ceiling((double)BurnTime / FuelEfficiency * 200d);

                if (GetIngredient().Id != 0)
                {
                    CookTime++;
                    if (CookTime >= 200)
                    {
                        Item result = GetResult(GetIngredient());
                        if (result != null)
                        {
                            Inventory.DecreaseSlot(0);
                            Inventory.IncreaseSlot(2, result.Id, result.Metadata);
                        }

                        CookTime = 0;
                    }
                }
                else
                {
                    CookTime = 0;
                }
            }

            if (BurnTime <= 0)
            {
                if (!Inventory.DecreaseSlot(1))
                {
                    //CookTime = 0;
                    BurnTime       = GetFuelEfficiency(GetFuel());
                    FuelEfficiency = BurnTime;
                    BurnTick       = (short)Math.Ceiling((double)BurnTime / FuelEfficiency * 200d);
                }
                else
                {
                    // No more fule
                    Furnace unlitFurnace = new Furnace
                    {
                        Coordinates = furnace.Coordinates,
                        Metadata    = furnace.Metadata
                    };

                    level.SetBlock(unlitFurnace);
                    FuelEfficiency = 0;
                    BurnTick       = 0;
                    BurnTime       = 0;
                    CookTime       = 0;
                }
            }

            level.RelayBroadcast(new McpeContainerSetData
            {
                windowId = Inventory.Id,
                property = 0,
                value    = CookTime
            });

            level.RelayBroadcast(new McpeContainerSetData
            {
                windowId = Inventory.Id,
                property = 1,
                value    = BurnTick
            });
        }
コード例 #2
0
        public override void OnTick(Level level)
        {
            if (Inventory == null)
            {
                return;
            }

            var furnace = level.GetBlock(Coordinates) as FurnaceBase;

            if (furnace == null)
            {
                return;
            }

            if (!(furnace is LitFurnace))
            {
                Item fuel       = GetFuel();
                Item ingredient = GetIngredient();
                Item smelt      = ingredient.GetSmelt();
                // To light a furnace you need both fule and proper ingredient.
                if (fuel.Count > 0 && fuel.FuelEfficiency > 0 && smelt != null)
                {
                    LitFurnace litFurnace = new LitFurnace
                    {
                        Coordinates = furnace.Coordinates,
                    };
                    litFurnace.SetState(furnace.GetState().States);
                    level.SetBlock(litFurnace);
                    furnace = litFurnace;

                    BurnTime       = GetFuelEfficiency(fuel);
                    FuelEfficiency = BurnTime;
                    CookTime       = 0;
                    Inventory.DecreaseSlot(1);
                }
            }

            if (furnace is LitFurnace)
            {
                if (BurnTime > 0)
                {
                    BurnTime--;
                    BurnTick = (short)Math.Ceiling((double)BurnTime / FuelEfficiency * 200d);

                    Item ingredient = GetIngredient();
                    Item smelt      = ingredient.GetSmelt();
                    if (smelt != null)
                    {
                        CookTime++;
                        if (CookTime >= 200)
                        {
                            Inventory.DecreaseSlot(0);
                            Inventory.IncreaseSlot(2, smelt.Id, smelt.Metadata);

                            CookTime = 0;
                        }
                    }
                    else
                    {
                        CookTime = 0;
                    }
                }

                if (BurnTime <= 0)
                {
                    var  fuel       = GetFuel();
                    Item ingredient = GetIngredient();
                    Item smelt      = ingredient.GetSmelt();
                    if (fuel.Count > 0 && fuel.FuelEfficiency > 0 && smelt != null)
                    {
                        Inventory.DecreaseSlot(1);

                        CookTime       = 0;
                        BurnTime       = GetFuelEfficiency(fuel);
                        FuelEfficiency = BurnTime;
                        BurnTick       = (short)Math.Ceiling((double)BurnTime / FuelEfficiency * 200d);
                    }
                    else
                    {
                        // No more fule or nothin more to smelt.
                        var unlitFurnace = new Furnace
                        {
                            Coordinates = furnace.Coordinates,
                        };
                        unlitFurnace.SetState(furnace.GetState().States);
                        level.SetBlock(unlitFurnace);
                        FuelEfficiency = 0;
                        BurnTick       = 0;
                        BurnTime       = 0;
                        CookTime       = 0;
                    }
                }
            }

            foreach (var observer in Inventory.Observers)
            {
                var cookTimeSetData = McpeContainerSetData.CreateObject();
                cookTimeSetData.windowId = Inventory.WindowsId;
                cookTimeSetData.property = 0;
                cookTimeSetData.value    = CookTime;
                observer.SendPacket(cookTimeSetData);

                var burnTimeSetData = McpeContainerSetData.CreateObject();
                burnTimeSetData.windowId = Inventory.WindowsId;
                burnTimeSetData.property = 1;
                burnTimeSetData.value    = BurnTick;
                observer.SendPacket(burnTimeSetData);
            }
        }