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; 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); } } 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); }
public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handHandling, ref EnumHandling handling) { EntityBehaviorMilkable bh; if (entitySel != null && (bh = entitySel.Entity.GetBehavior <EntityBehaviorMilkable>()) != null) { if (lcblock.GetContent(slot.Itemstack) != null) { if (api is ICoreClientAPI capi) { capi.TriggerIngameError(this, "useemptybucket", Lang.Get("Use an empty bucket for milking")); } return; } bh.TryBeginMilking(); handling = EnumHandling.PreventDefault; handHandling = EnumHandHandling.PreventDefault; } }
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); }
protected override void ActivateSlotLeftClick(ItemSlot sourceSlot, ref ItemStackMoveOperation op) { IWorldAccessor world = inventory.Api.World; BlockLiquidContainerBase liqCntBlock = sourceSlot.Itemstack?.Block as BlockLiquidContainerBase; if (liqCntBlock != null) { ItemStack contentStack = liqCntBlock.GetContent(sourceSlot.Itemstack); var liqProps = BlockLiquidContainerBase.GetContainableProps(contentStack); bool stackable = !Empty && itemstack.Equals(world, contentStack, GlobalConstants.IgnoredStackAttributes); if ((Empty || stackable) && contentStack != null) { ItemStack bucketStack = sourceSlot.Itemstack; float toMoveLitres = (op?.ActingPlayer?.Entity.Controls.Sneak ?? false) ? liqCntBlock.CapacityLitres : liqCntBlock.TransferSizeLitres; float curDestLitres = StackSize / liqProps.ItemsPerLitre; float curSrcLitres = contentStack.StackSize / liqProps.ItemsPerLitre; toMoveLitres = Math.Min(toMoveLitres, curSrcLitres); toMoveLitres *= bucketStack.StackSize; toMoveLitres = Math.Min(toMoveLitres, capacityLitres - curDestLitres); if (toMoveLitres > 0) { int moveQuantity = (int)(liqProps.ItemsPerLitre * toMoveLitres); ItemStack takenContentStack = liqCntBlock.TryTakeContent(bucketStack, moveQuantity / bucketStack.StackSize); takenContentStack.StackSize *= bucketStack.StackSize; takenContentStack.StackSize += StackSize; this.itemstack = takenContentStack; MarkDirty(); op.MovedQuantity = moveQuantity; } } return; } string contentItemCode = sourceSlot.Itemstack?.ItemAttributes?["contentItemCode"].AsString(); if (contentItemCode != null) { ItemStack contentStack = new ItemStack(world.GetItem(AssetLocation.Create(contentItemCode, sourceSlot.Itemstack.Collectible.Code.Domain))); bool stackable = !Empty && itemstack.Equals(world, contentStack, GlobalConstants.IgnoredStackAttributes); if ((Empty || stackable) && contentStack != null) { if (stackable) { this.itemstack.StackSize++; } else { this.itemstack = contentStack; } MarkDirty(); ItemStack bowlStack = new ItemStack(world.GetBlock(AssetLocation.Create(sourceSlot.Itemstack.ItemAttributes["emptiedBlockCode"].AsString(), sourceSlot.Itemstack.Collectible.Code.Domain))); if (sourceSlot.StackSize == 1) { sourceSlot.Itemstack = bowlStack; } else { sourceSlot.Itemstack.StackSize--; if (!op.ActingPlayer.InventoryManager.TryGiveItemstack(bowlStack)) { world.SpawnItemEntity(bowlStack, op.ActingPlayer.Entity.Pos.XYZ); } } sourceSlot.MarkDirty(); } return; } if (sourceSlot.Itemstack?.ItemAttributes?["contentItem2BlockCodes"].Exists == true) { return; } base.ActivateSlotLeftClick(sourceSlot, ref op); }
protected override void ActivateSlotRightClick(ItemSlot sourceSlot, ref ItemStackMoveOperation op) { IWorldAccessor world = inventory.Api.World; BlockLiquidContainerBase liqCntBlock = sourceSlot.Itemstack?.Block as BlockLiquidContainerBase; if (liqCntBlock != null) { if (Empty) { return; } ItemStack contentStack = liqCntBlock.GetContent(sourceSlot.Itemstack); float toMoveLitres = op.ShiftDown ? liqCntBlock.CapacityLitres : liqCntBlock.TransferSizeLitres; var srcProps = BlockLiquidContainerBase.GetContainableProps(Itemstack); float availableLitres = StackSize / (srcProps?.ItemsPerLitre ?? 1); toMoveLitres = Math.Min(toMoveLitres, availableLitres); toMoveLitres *= sourceSlot.Itemstack.StackSize; if (contentStack == null) { int moved = liqCntBlock.TryPutLiquid(sourceSlot.Itemstack, Itemstack, toMoveLitres / sourceSlot.Itemstack.StackSize); TakeOut(moved * sourceSlot.Itemstack.StackSize); MarkDirty(); } else { if (itemstack.Equals(world, contentStack, GlobalConstants.IgnoredStackAttributes)) { int moved = liqCntBlock.TryPutLiquid(sourceSlot.Itemstack, liqCntBlock.GetContent(sourceSlot.Itemstack), toMoveLitres / sourceSlot.Itemstack.StackSize); TakeOut(moved * sourceSlot.Itemstack.StackSize); MarkDirty(); return; } } return; } if (itemstack != null && sourceSlot.Itemstack?.ItemAttributes?["contentItem2BlockCodes"].Exists == true) { string outBlockCode = sourceSlot.Itemstack.ItemAttributes["contentItem2BlockCodes"][itemstack.Collectible.Code.ToShortString()].AsString(); if (outBlockCode != null) { ItemStack outBlockStack = new ItemStack(world.GetBlock(AssetLocation.Create(outBlockCode, sourceSlot.Itemstack.Collectible.Code.Domain))); if (sourceSlot.StackSize == 1) { sourceSlot.Itemstack = outBlockStack; } else { sourceSlot.Itemstack.StackSize--; if (!op.ActingPlayer.InventoryManager.TryGiveItemstack(outBlockStack)) { world.SpawnItemEntity(outBlockStack, op.ActingPlayer.Entity.Pos.XYZ); } } sourceSlot.MarkDirty(); TakeOut(1); } return; } if (sourceSlot.Itemstack?.ItemAttributes?["contentItem2BlockCodes"].Exists == true || sourceSlot.Itemstack?.ItemAttributes?["contentItemCode"].AsString() != null) { return; } base.ActivateSlotRightClick(sourceSlot, ref op); }