diff --git a/Items/GlobalItem.cs b/Items/GlobalItem.cs new file mode 100644 index 0000000..0658918 --- /dev/null +++ b/Items/GlobalItem.cs @@ -0,0 +1,38 @@ +using MagicStorage.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Terraria; +using Terraria.DataStructures; +using Terraria.Localization; +using Terraria.ModLoader; + +namespace MagicStorageVoidBag.Items { + internal class GlobalItem : Terraria.ModLoader.GlobalItem { + public override bool OnPickup(Item item, Player player) { + var i = player.inventory.Where(i => i.type == ModContent.ItemType()).DefaultIfEmpty(null).First(); + if (i != null && !player.ItemSpace(item).CanTakeItem) { + var bag = (MSVoidBag)i.ModItem; + + if (bag.location.X < 0 || bag.location.Y < 0) return false; + + Tile tile = Main.tile[bag.location.X, bag.location.Y]; + + if (!tile.HasTile || tile.TileType != ModContent.TileType() || tile.TileFrameX != 0 || tile.TileFrameY != 0) return false; + if (!TileEntity.ByPosition.TryGetValue(bag.location, out TileEntity te)) return false; + if (te.type != ModContent.TileEntityType()) return false; + + TEStorageHeart heart = (TEStorageHeart)te; + + int oldStack = item.stack; + heart.TryDeposit(item); + if (item.stack == 0) return true; + + } + + return base.OnPickup(item, player); + } + } +} diff --git a/Items/MSVoidBag.cs b/Items/MSVoidBag.cs new file mode 100644 index 0000000..2a513fc --- /dev/null +++ b/Items/MSVoidBag.cs @@ -0,0 +1,76 @@ +using MagicStorage.Items; +using System; +using System.Collections.Generic; +using Terraria; +using Terraria.GameContent.Creative; +using Terraria.ID; +using Terraria.Localization; +using Terraria.ModLoader; + +namespace MagicStorageVoidBag.Items { + [ExtendsFromMod("MagicStorage")] + public class MSVoidBag : PortableAccess { + + /* + Code here largely thanks to original MagicStorage source, which can be found at + https://github.com/blushiemagic/MagicStorage/ + License: + MIT License + + Copyright(c) 2017 Kaylee Minsuh Kim + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files(the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + */ + + 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); + } + + public override void ModifyTooltips(List 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(); + recipe.AddIngredient(ItemID.VoidLens); + recipe.AddIngredient(); + recipe.AddTile(TileID.LunarCraftingStation); + recipe.Register(); + } + } +} diff --git a/Items/MSVoidBag.png b/Items/MSVoidBag.png new file mode 100644 index 0000000..7f638f7 Binary files /dev/null and b/Items/MSVoidBag.png differ diff --git a/Localization/en-US.hjson b/Localization/en-US.hjson new file mode 100644 index 0000000..1651081 --- /dev/null +++ b/Localization/en-US.hjson @@ -0,0 +1,7 @@ +Mods: { + MagicStorageVoidBag: { + ItemName: { + MSVoidBag: Magic Void Bag + } + } +} \ No newline at end of file diff --git a/MagicStorageVoidBag.cs b/MagicStorageVoidBag.cs index 3a2e354..1500f8e 100644 --- a/MagicStorageVoidBag.cs +++ b/MagicStorageVoidBag.cs @@ -1,8 +1,57 @@ +using Mono.Cecil.Cil; +using Mono.Cecil; +using MonoMod.Cil; using Terraria.ModLoader; +using MagicStorageVoidBag.Items; -namespace MagicStorageVoidBag -{ - public class MagicStorageVoidBag : Mod - { - } +namespace MagicStorageVoidBag { + public class MagicStorageVoidBag : Mod { + public override void Load() { + IL.MagicStorage.Components.StorageHeart.RightClick += HeartRightClickPatch; + } + + public override void Unload() { + base.Unload(); + } + + // Patch MagicStorage IL to change the Storage Heart right click handler to also work with + // MSVoidBag. + private void HeartRightClickPatch(ILContext il) { + if (il == null) { + Logger.Error("ILContext null!"); + return; + } + + Logger.Debug("Patching MagicStorage IL..."); + + var c = new ILCursor(il); + + if (!c.TryGotoNext(i => i.MatchCallOrCallvirt("Terraria.ModLoader.ModContent", "ItemType"))) { + Logger.Warn("IL patching failed! :("); + } + c = c.GotoPrev().GotoPrev(); + + var c2 = c.Clone(); + + // copy IL up to call + while (c2.Next.OpCode != OpCodes.Call) { + if (c2.Next.Operand != null) { + c.Emit(c2.Next.OpCode, c2.Next.Operand); + } else { + c.Emit(c2.Next.OpCode); + } + + c2 = c2.GotoNext(); + } + + var method = typeof(ModContent).GetMethod("ItemType").MakeGenericMethod(typeof(MSVoidBag)); + + c.Emit(c2.Next.OpCode, method); + c2.GotoNext(); + c.Emit(c2.Next.OpCode, c2.Next.Operand); + + Logger.Debug("...MagicStorage IL patching complete!"); + } + + } } \ No newline at end of file diff --git a/MagicStorageVoidBag.csproj b/MagicStorageVoidBag.csproj index aff4c95..9ca5cab 100644 --- a/MagicStorageVoidBag.csproj +++ b/MagicStorageVoidBag.csproj @@ -10,4 +10,12 @@ + + + ..\Mod Libraries\MagicStorage.dll + + + lib\MMHOOK_MagicStorage.dll + + \ No newline at end of file diff --git a/MagicStorageVoidBag.sln b/MagicStorageVoidBag.sln new file mode 100644 index 0000000..f51c396 --- /dev/null +++ b/MagicStorageVoidBag.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32616.157 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MagicStorageVoidBag", "MagicStorageVoidBag.csproj", "{8310432C-954C-4401-B93A-27A289CF8B1C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8310432C-954C-4401-B93A-27A289CF8B1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8310432C-954C-4401-B93A-27A289CF8B1C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8310432C-954C-4401-B93A-27A289CF8B1C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8310432C-954C-4401-B93A-27A289CF8B1C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D3ABF40B-5B75-4B1A-A7C8-569E4A3D7101} + EndGlobalSection +EndGlobal diff --git a/build.txt b/build.txt index b1e59bd..346fefb 100644 --- a/build.txt +++ b/build.txt @@ -1,3 +1,8 @@ displayName = Magic Storage Void Bag author = jack -version = 0.1 \ No newline at end of file +version = 0.1 +modReferences = MagicStorage +dllReferences = MMHOOK_MagicStorage +sortAfter = MagicStorage +includeSource = true +homepage = https://github.com/jackbondpreston/MagicStorageVoidBag diff --git a/description.txt b/description.txt index d75503a..6482298 100644 --- a/description.txt +++ b/description.txt @@ -1 +1,2 @@ -Magic Storage Void Bag is a pretty cool mod, it does...this. Modify this file with a description of your mod. \ No newline at end of file +Adds an upgraded void bag that combines functionality of the Void Bag and Portable Remote Storage Access, by putting items into a magic storage system when your inventory is full (and allowing remote access as usual). +This item is crafted by combining a Void Bag with a Portable Remote Storage Access, and a Radiant Jewel. To link it with your storage system, right click the Storage Heart with the bag equipped. \ No newline at end of file diff --git a/description_workshop.txt b/description_workshop.txt new file mode 100644 index 0000000..6482298 --- /dev/null +++ b/description_workshop.txt @@ -0,0 +1,2 @@ +Adds an upgraded void bag that combines functionality of the Void Bag and Portable Remote Storage Access, by putting items into a magic storage system when your inventory is full (and allowing remote access as usual). +This item is crafted by combining a Void Bag with a Portable Remote Storage Access, and a Radiant Jewel. To link it with your storage system, right click the Storage Heart with the bag equipped. \ No newline at end of file diff --git a/icon.png b/icon.png index 707e835..ca903d7 100644 Binary files a/icon.png and b/icon.png differ diff --git a/lib/MMHOOK_MagicStorage.dll b/lib/MMHOOK_MagicStorage.dll new file mode 100644 index 0000000..07472b6 Binary files /dev/null and b/lib/MMHOOK_MagicStorage.dll differ