Example #1
0
 /// <summary>Write a field to a mod data dictionary if it does not yet exist.</summary>
 /// <param name="data">The mod data dictionary to update.</param>
 /// <param name="key">The dictionary key to write.</param>
 /// <param name="value">The value to write, or <c>null</c> to remove it.</param>
 public static ModDataDictionary WriteFieldIfNotExists(this ModDataDictionary data, string key, string value)
 {
     if (!data.ContainsKey(key))
     {
         data[key] = value;
     }
     return(data);
 }
Example #2
0
        public Item UnpackItem(ModDataDictionary modData, string recipientName)
        {
            string[] fields = new[] {
                "giftsender", "giftname", "giftid",
                "giftparentid", "gifttype", "giftstack",
                "giftquality", "giftpreserve", "gifthoney",
                "giftcolour", "giftdata"
            };
            if (fields.Any(field => !modData.ContainsKey(AssetPrefix + field)))
            {
                string msg = fields.Where(field => !modData.ContainsKey(field))
                             .Aggregate("This gift is missing data:", (str, field) => str + "\n" + field)
                             + "\nIf this gift was placed before updating, please revert to the previous version and collect the gift!"
                             + "\nOtherwise, leave a report on the mod page for Gift Wrapper with your log file (https://smapi.io/log).";
                Monitor.Log(msg, LogLevel.Warn);
                return(null);
            }

            // Parse the wrapped gift's serialised modData fields to use in rebuilding its gift item
            long   giftSender   = long.Parse(modData[AssetPrefix + fields[0]]);
            string giftName     = modData[AssetPrefix + fields[1]];
            int    giftId       = int.Parse(modData[AssetPrefix + fields[2]]);
            int    giftParentId = int.Parse(modData[AssetPrefix + fields[3]]);
            int    giftType     = int.Parse(modData[AssetPrefix + fields[4]]);
            int    giftStack    = int.Parse(modData[AssetPrefix + fields[5]]);
            int    giftQuality  = int.Parse(modData[AssetPrefix + fields[6]]);
            int    giftPreserve = int.Parse(modData[AssetPrefix + fields[7]]);
            int    giftHoney    = int.Parse(modData[AssetPrefix + fields[8]]);
            string giftColour   = modData[AssetPrefix + fields[9]];
            string giftData     = modData[AssetPrefix + fields[10]];
            Item   actualGift   = null;

            switch (giftType)
            {
            case (int)GiftType.BedFurniture:
                actualGift = new BedFurniture(which: giftId, tile: Vector2.Zero);
                break;

            case (int)GiftType.Furniture:
                actualGift = new Furniture(which: giftId, tile: Vector2.Zero);
                break;

            case (int)GiftType.BigCraftable:
                actualGift = new StardewValley.Object(tileLocation: Vector2.Zero, parentSheetIndex: giftId, isRecipe: false);
                break;

            case (int)GiftType.MeleeWeapon:
                actualGift = new MeleeWeapon(spriteIndex: giftId);
                break;

            case (int)GiftType.Hat:
                actualGift = new Hat(which: giftId);
                break;

            case (int)GiftType.Boots:
                actualGift = new Boots(which: giftId);                         // todo: test boots colour
                ((Boots)actualGift).appliedBootSheetIndex.Set(giftQuality);
                ((Boots)actualGift).indexInColorSheet.Set(int.Parse(giftColour));
                break;

            case (int)GiftType.Clothing:
                int[] colourSplit = giftColour.Split('/').ToList().ConvertAll(int.Parse).ToArray();
                Color colour      = new Color(r: colourSplit[0], g: colourSplit[1], b: colourSplit[2], a: colourSplit[3]);
                actualGift = new Clothing(item_index: giftId);
                ((Clothing)actualGift).clothesColor.Set(colour);
                break;

            case (int)GiftType.Ring:
                actualGift = new Ring(which: giftId);
                break;

            case (int)GiftType.Object:
                actualGift = new StardewValley.Object(parentSheetIndex: giftId, initialStack: giftStack)
                {
                    Quality = giftQuality
                };
                actualGift.Name = giftName;
                if (giftParentId != -1)
                {
                    ((StardewValley.Object)actualGift).preservedParentSheetIndex.Value = giftParentId;
                }
                if (giftPreserve != -1)
                {
                    ((StardewValley.Object)actualGift).preserve.Value = (StardewValley.Object.PreserveType)giftPreserve;
                }
                if (giftHoney != 0)
                {
                    ((StardewValley.Object)actualGift).honeyType.Value = (StardewValley.Object.HoneyType)giftHoney;
                }
                break;
            }

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

            Dictionary <string, string> giftDataDeserialised = ((Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(giftData)).ToObject <Dictionary <string, string> >();

            if (giftDataDeserialised != null)
            {
                // Apply serialised mod data back to the gifted item
                actualGift.modData.Set(giftDataDeserialised);
            }

            if (recipientName != null && Game1.player.UniqueMultiplayerID != giftSender)
            {
                // Show a message to all players to celebrate this wonderful event
                Multiplayer multiplayer = Helper.Reflection.GetField <Multiplayer>(typeof(Game1), "multiplayer").GetValue();
                multiplayer.globalChatInfoMessage(AssetPrefix + (giftStack > 1 ? "message.giftopened_quantity" : "message.giftopened"),
                                                  recipientName,                    // Recipient's name
                                                  Game1.getFarmer(giftSender).Name, // Sender's name
                                                  actualGift.DisplayName,           // Gift name
                                                  giftStack.ToString());            // Gift quantity
            }

            return(actualGift);
        }