MagicStorageVoidBag/Items/MSVoidBag.cs

107 lines
4.0 KiB
C#
Raw Permalink Normal View History

2022-07-02 11:38:45 +01:00
using MagicStorage.Items;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2022-07-02 11:38:45 +01:00
using Terraria;
using Terraria.DataStructures;
2022-07-02 11:38:45 +01:00
using Terraria.GameContent.Creative;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
2022-07-02 11:38:45 +01:00
namespace MagicStorageVoidBag.Items {
[ExtendsFromMod("MagicStorage")]
public class MSVoidBag : PortableAccess {
[CloneByReference]
internal Dictionary<string, Point16> locationsByWorld = new();
2022-07-02 11:38:45 +01:00
public override void SetStaticDefaults() {
CreativeItemSacrificesCatalog.Instance.SacrificeCountNeededByItemId[Type] = 1;
}
public override void SetDefaults() {
Item.width = 26;
Item.height = 34;
Item.maxStack = 1;
Item.rare = ItemRarityID.Purple;
Item.useStyle = ItemUseStyleID.Swing;
Item.useAnimation = 28;
Item.useTime = 28;
Item.value = Item.sellPrice(gold: 10);
location = Point16.NegativeOne;
locationsByWorld[Main.worldName] = location;
2022-07-02 11:38:45 +01:00
}
public override void ModifyTooltips(List<TooltipLine> lines) {
bool isSet = location.X >= 0 && location.Y >= 0;
for (int k = 0; k < lines.Count; k++)
if (isSet && lines[k].Mod == "Terraria" && lines[k].Name == "Tooltip1") {
lines[k].Text = Language.GetTextValue("Mods.MagicStorage.SetTo", location.X, location.Y);
} else if (!isSet && lines[k].Mod == "Terraria" && lines[k].Name == "Tooltip2") {
lines.RemoveAt(k);
k--;
}
}
public override void AddRecipes() {
Recipe recipe = CreateRecipe();
recipe.AddIngredient<PortableAccess>();
recipe.AddIngredient(ItemID.VoidLens);
recipe.Register();
}
public override void UpdateInventory(Player player) {
if (!locationsByWorld.ContainsKey(Main.worldName) || location != locationsByWorld[Main.worldName]) {
locationsByWorld[Main.worldName] = location;
if (Main.netMode == NetmodeID.MultiplayerClient) {
int p = Array.IndexOf(Main.player, player);
int i = Array.IndexOf(player.inventory, Item);
if (p >= 0 && i >= 0) {
NetMessage.SendData(MessageID.SyncEquipment, -1, -1, null, p, i, player.inventory[i].prefix);
}
}
}
}
public override void SaveData(TagCompound tag) {
tag["X"] = location.X;
tag["Y"] = location.Y;
tag["version"] = SAVE_VERSION;
tag["locations"] = locationsByWorld
.Select(kvp => new TagCompound() {
["world"] = kvp.Key,
["X"] = kvp.Value.X,
["Y"] = kvp.Value.Y
})
.ToList();
}
public override void LoadData(TagCompound tag) {
if (tag.GetInt("version") < SAVE_VERSION || tag.GetList<TagCompound>("locations") is not List<TagCompound> locations) {
location = new Point16(tag.GetShort("X"), tag.GetShort("Y"));
locationsByWorld[Main.worldName] = location;
} else {
locationsByWorld = locations.ToDictionary(t => t.GetString("world"), t => new Point16(t.GetShort("X"), t.GetShort("Y")));
if (locationsByWorld.ContainsKey(Main.worldName)) {
location = locationsByWorld[Main.worldName];
}
}
}
public override void NetSend(BinaryWriter writer) {
writer.Write(location.X);
writer.Write(location.Y);
}
public override void NetReceive(BinaryReader reader) {
location = new Point16(reader.ReadInt16(), reader.ReadInt16());
locationsByWorld[Main.worldName] = location;
}
2022-07-02 11:38:45 +01:00
}
}