public override void TryMergeStacks(ItemStackMergeOperation op) { op.MovableQuantity = GetMergableQuantity(op.SinkSlot.Itemstack, op.SourceSlot.Itemstack); if (op.MovableQuantity == 0) { return; } if (!op.SinkSlot.CanTakeFrom(op.SourceSlot)) { return; } ItemStack sinkContent = GetContent(op.World, op.SinkSlot.Itemstack); ItemStack sourceContent = GetContent(op.World, op.SourceSlot.Itemstack); if (sinkContent == null && sourceContent == null) { base.TryMergeStacks(op); return; } if (sinkContent == null || sourceContent == null) { op.MovableQuantity = 0; return; } if (!sinkContent.Equals(op.World, sourceContent, GlobalConstants.IgnoredStackAttributes)) { op.MovableQuantity = 0; return; } WaterTightContainableProps props = GetStackProps(sourceContent); float maxItems = BucketCapacityLitres * props.ItemsPerLitre; int sourceEmptySpace = (int)(maxItems - (float)sourceContent.StackSize / props.ItemsPerLitre); int sinkEmptySpace = (int)(maxItems - (float)sinkContent.StackSize / props.ItemsPerLitre); if (sourceEmptySpace == 0 && sinkEmptySpace == 0) { // Full buckets are not stackable op.MovableQuantity = 0; //base.TryMergeStacks(op); return; } if (op.CurrentPriority == EnumMergePriority.DirectMerge) { int moved = TryAddContent(op.World, op.SinkSlot.Itemstack, sinkContent, sourceContent.StackSize); TryTakeContent(op.World, op.SourceSlot.Itemstack, moved); op.SourceSlot.MarkDirty(); op.SinkSlot.MarkDirty(); } op.MovableQuantity = 0; return; }
public override void TryMergeStacks(ItemStackMergeOperation op) { if (op.CurrentPriority == EnumMergePriority.DirectMerge) { float repstr = op.SourceSlot.Itemstack.ItemAttributes?["clothingRepairStrength"].AsFloat(0) ?? 0; if (repstr > 0 && op.SinkSlot.Itemstack.Attributes.GetFloat("condition") < 1) { ChangeCondition(op.SinkSlot, repstr); op.MovedQuantity = 1; op.SourceSlot.TakeOut(1); return; } } base.TryMergeStacks(op); }
public bool ServeIntoStack(ItemSlot bowlSlot, ItemSlot potslot, IWorldAccessor world) { if (world.Side == EnumAppSide.Client) { return(true); } float quantityServings = GetServings(world, potslot.Itemstack); string ownRecipeCode = GetRecipeCode(world, potslot.Itemstack); float servingCapacity = bowlSlot.Itemstack.Block.Attributes["servingCapacity"].AsFloat(1); // Merge existing servings if (bowlSlot.Itemstack.Block is IBlockMealContainer) { var mealcont = (bowlSlot.Itemstack.Block as IBlockMealContainer); ItemStack[] myStacks = GetNonEmptyContents(api.World, potslot.Itemstack); string hisRecipeCode = mealcont.GetRecipeCode(world, bowlSlot.Itemstack); ItemStack[] hisStacks = mealcont.GetNonEmptyContents(world, bowlSlot.Itemstack); float hisServings = mealcont.GetQuantityServings(world, bowlSlot.Itemstack); if (hisStacks != null && hisServings > 0) { if (myStacks.Length != hisStacks.Length) { return(false); } if (ownRecipeCode != hisRecipeCode) { return(false); } float remainingPlaceableServings = servingCapacity - hisServings; if (remainingPlaceableServings <= 0) { return(false); } for (int i = 0; i < myStacks.Length; i++) { if (!myStacks[i].Equals(world, hisStacks[i], GlobalConstants.IgnoredStackAttributes)) { return(false); } } // Ok merge transition states for (int i = 0; i < hisStacks.Length; i++) { ItemStackMergeOperation op = new ItemStackMergeOperation(world, EnumMouseButton.Left, 0, EnumMergePriority.ConfirmedMerge, myStacks[i].StackSize); op.SourceSlot = new DummySlot(myStacks[i]); op.SinkSlot = new DummySlot(hisStacks[i]); hisStacks[i].Collectible.TryMergeStacks(op); } // Now increase serving siize float movedservings = Math.Min(remainingPlaceableServings, quantityServings); mealcont.SetQuantityServings(world, bowlSlot.Itemstack, hisServings + movedservings); SetServings(world, potslot.Itemstack, quantityServings - movedservings); if (quantityServings - movedservings <= 0) { Block emptyPotBlock = world.GetBlock(new AssetLocation(Attributes["emptiedBlockCode"].AsString())); potslot.Itemstack = new ItemStack(emptyPotBlock); } potslot.Itemstack.Attributes.RemoveAttribute("sealed"); potslot.MarkDirty(); bowlSlot.MarkDirty(); return(true); } } ItemStack[] stacks = GetContents(api.World, potslot.Itemstack); string code = bowlSlot.Itemstack.Block.Attributes["mealBlockCode"].AsString(); Block mealblock = api.World.GetBlock(new AssetLocation(code)); float servingsToTransfer = Math.Min(quantityServings, servingCapacity); ItemStack stack = new ItemStack(mealblock); (mealblock as IBlockMealContainer).SetContents(ownRecipeCode, stack, stacks, servingsToTransfer); SetServings(world, potslot.Itemstack, quantityServings - servingsToTransfer); if (quantityServings - servingsToTransfer <= 0) { Block emptyPotBlock = world.GetBlock(new AssetLocation(Attributes["emptiedBlockCode"].AsString())); potslot.Itemstack = new ItemStack(emptyPotBlock); } potslot.MarkDirty(); bowlSlot.Itemstack = stack; bowlSlot.MarkDirty(); return(true); }
private bool tryMergeServingsIntoBE(IBlockEntityMealContainer bemeal, ItemSlot potslot) { ItemStack[] myStacks = GetNonEmptyContents(api.World, potslot.Itemstack); string hisRecipeCode = bemeal.RecipeCode; ItemStack[] hisStacks = bemeal.GetNonEmptyContentStacks(); float hisServings = bemeal.QuantityServings; string ownRecipeCode = GetRecipeCode(api.World, potslot.Itemstack); float servingCapacity = (bemeal as BlockEntity).Block.Attributes["servingCapacity"].AsFloat(1); // Empty if (hisStacks == null || hisServings == 0) { return(false); } // Different ingredient quantity if (myStacks.Length != hisStacks.Length) { return(true); } // Different recipe if (ownRecipeCode != hisRecipeCode) { return(true); } // No more empty space float remainingPlaceableServings = servingCapacity - hisServings; if (remainingPlaceableServings <= 0) { return(true); } // Different ingredients for (int i = 0; i < myStacks.Length; i++) { if (!myStacks[i].Equals(api.World, hisStacks[i], GlobalConstants.IgnoredStackAttributes)) { return(true); } } // Ok merge transition states for (int i = 0; i < hisStacks.Length; i++) { ItemStackMergeOperation op = new ItemStackMergeOperation(api.World, EnumMouseButton.Left, 0, EnumMergePriority.ConfirmedMerge, myStacks[i].StackSize); op.SourceSlot = new DummySlot(myStacks[i]); op.SinkSlot = new DummySlot(hisStacks[i]); hisStacks[i].Collectible.TryMergeStacks(op); } // Now increase serving siize float quantityServings = GetServings(api.World, potslot.Itemstack); float movedservings = Math.Min(remainingPlaceableServings, quantityServings); bemeal.QuantityServings = hisServings + movedservings; SetServings(api.World, potslot.Itemstack, quantityServings - movedservings); if (quantityServings - movedservings <= 0) { string loc = Attributes["emptiedBlockCode"].AsString(); if (loc != null) { Block emptyPotBlock = api.World.GetBlock(new AssetLocation(loc)); potslot.Itemstack = new ItemStack(emptyPotBlock); } else { SetRecipeCode(api.World, potslot.Itemstack, null); } } potslot.MarkDirty(); bemeal.MarkDirty(true); return(true); }