Esempio n. 1
0
 public void OnContainedInteractStop(float secondsUsed, BlockEntityContainer be, ItemSlot slot, IPlayer byPlayer, BlockSelection blockSel)
 {
     if (tryFinishEatMeal(secondsUsed, slot, byPlayer.Entity, true))
     {
         be.MarkDirty(true);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Tries to take out as much items/liquid as possible from a placed bucket and returns it
        /// </summary>
        /// <param name="world"></param>
        /// <param name="pos"></param>
        /// <param name="quantityItem"></param>
        public ItemStack TryTakeContent(IWorldAccessor world, BlockPos pos, int quantityItem)
        {
            BlockEntityContainer becontainer = world.BlockAccessor.GetBlockEntity(pos) as BlockEntityContainer;

            if (becontainer == null)
            {
                return(null);
            }

            ItemStack stack = becontainer.Inventory[GetContainerSlotId(world, pos)].Itemstack;

            if (stack == null)
            {
                return(null);
            }

            ItemStack takenStack = stack.Clone();

            takenStack.StackSize = quantityItem;

            stack.StackSize -= quantityItem;
            if (stack.StackSize <= 0)
            {
                becontainer.Inventory[GetContainerSlotId(world, pos)].Itemstack = null;
            }
            else
            {
                becontainer.Inventory[GetContainerSlotId(world, pos)].Itemstack = stack;
            }

            becontainer.Inventory[GetContainerSlotId(world, pos)].MarkDirty();
            becontainer.MarkDirty(true);

            return(takenStack);
        }
        public bool OnContainedInteractStart(BlockEntityContainer be, ItemSlot slot, IPlayer byPlayer, BlockSelection blockSel)
        {
            var targetSlot = byPlayer.InventoryManager.ActiveHotbarSlot;

            if (targetSlot.Empty)
            {
                return(false);
            }

            if ((targetSlot.Itemstack.Collectible.Attributes?.IsTrue("mealContainer") == true || targetSlot.Itemstack.Block is IBlockMealContainer) && GetServings(api.World, slot.Itemstack) > 0)
            {
                if (targetSlot.StackSize > 1)
                {
                    targetSlot = new DummySlot(targetSlot.TakeOut(1));
                    byPlayer.InventoryManager.ActiveHotbarSlot.MarkDirty();
                    ServeIntoStack(targetSlot, slot, api.World);
                    if (!byPlayer.InventoryManager.TryGiveItemstack(targetSlot.Itemstack, true))
                    {
                        api.World.SpawnItemEntity(targetSlot.Itemstack, byPlayer.Entity.ServerPos.XYZ);
                    }
                }
                else
                {
                    ServeIntoStack(targetSlot, slot, api.World);
                }

                slot.MarkDirty();
                be.MarkDirty(true);
                return(true);
            }



            return(false);
        }
Esempio n. 4
0
        /// <summary>
        /// Sets the contents to placed container block
        /// </summary>
        /// <param name="world"></param>
        /// <param name="pos"></param>
        /// <param name="content"></param>
        public void SetContent(IWorldAccessor world, BlockPos pos, ItemStack content)
        {
            BlockEntityContainer beContainer = world.BlockAccessor.GetBlockEntity(pos) as BlockEntityContainer;

            if (beContainer == null)
            {
                return;
            }

            new DummySlot(content).TryPutInto(world, beContainer.Inventory[GetContainerSlotId(world, pos)], content.StackSize);

            beContainer.Inventory[GetContainerSlotId(world, pos)].MarkDirty();
            beContainer.MarkDirty(true);
        }
Esempio n. 5
0
        public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handHandling)
        {
            if (blockSel == null)
            {
                base.OnHeldInteractStart(slot, byEntity, blockSel, entitySel, firstEvent, ref handHandling);
                return;
            }

            BlockLiquidContainerBase blockLiqContainer = byEntity.World.BlockAccessor.GetBlock(blockSel.Position) as BlockLiquidContainerBase;
            IPlayer player = (byEntity as EntityPlayer)?.Player;

            string contents = BowlContentItemCode();

            if (blockLiqContainer != null)
            {
                if (contents == null)
                {
                    ItemStack stack = blockLiqContainer.GetContent(byEntity.World, blockSel.Position);
                    if (stack != null && ContentBlockForContents(stack.Collectible.Code.Path) != null)
                    {
                        InsertIntoBowl(slot, byEntity, stack.Collectible.Code.Path);
                        blockLiqContainer.TryTakeContent(byEntity.World, blockSel.Position, 1);
                    }

                    BlockEntityContainer bebarrel = api.World.BlockAccessor.GetBlockEntity(blockSel.Position) as BlockEntityContainer;
                    if (bebarrel != null && bebarrel.Inventory.Count > 0)
                    {
                        stack = bebarrel.Inventory[0].Itemstack;

                        if (stack != null && stack.Collectible.Attributes["crockable"].AsBool() == true)
                        {
                            Block     mealblock = api.World.GetBlock(AssetLocation.Create(slot.Itemstack.Collectible.Attributes["mealBlockCode"].AsString(), slot.Itemstack.Collectible.Code.Domain));
                            ItemStack mealstack = new ItemStack(mealblock);
                            mealstack.StackSize = 1;

                            (mealblock as IBlockMealContainer).SetContents(null, mealstack, new ItemStack[] { bebarrel.Inventory[0].TakeOut(4) }, 1);

                            if (slot.StackSize == 1)
                            {
                                slot.Itemstack = mealstack;
                            }
                            else
                            {
                                slot.TakeOut(1);
                                if (!player.InventoryManager.TryGiveItemstack(mealstack, true))
                                {
                                    api.World.SpawnItemEntity(mealstack, byEntity.Pos.XYZ.Add(0.5, 0.5, 0.5));
                                }
                            }
                            slot.MarkDirty();

                            bebarrel.MarkDirty(true);
                        }
                    }
                }
                else
                {
                    ItemStack stack = blockLiqContainer.GetContent(byEntity.World, blockSel.Position);
                    if (stack == null || stack.Collectible.Code.Equals(new AssetLocation(BowlContentItemCode())))
                    {
                        Item contentItem = byEntity.World.GetItem(new AssetLocation(BowlContentItemCode()));
                        if (blockLiqContainer.TryPutContent(byEntity.World, blockSel.Position, new ItemStack(contentItem), 1) > 0)
                        {
                            EmptyOutBowl(slot, byEntity);
                        }
                    }
                }

                handHandling = EnumHandHandling.PreventDefaultAction;
                return;
            }


            handHandling = EnumHandHandling.PreventDefault;

            base.OnHeldInteractStart(slot, byEntity, blockSel, entitySel, firstEvent, ref handHandling);
        }