public override void RefreshItems(TEStorageCenter center = null) { if (Main.player[Main.myPlayer].GetModPlayer <StoragePlayer>().tileType == typeof(TCraftingStorageAccess)) { MagicStorageTwo.Instance.guiM?.CraftingGUI.RefreshItems(); } if (StoragePlayer.IsOnlyStorageCrafting()) { return; } items.Clear(); TEStorageHeart heart = center as TEStorageHeart ?? GetHeart(); if (heart == null) { return; } InitLangStuff(); InitSortButtons(); InitFilterButtons(); SortMode sortMode = (SortMode)sortButtons.Choice; FilterMode filterMode = (FilterMode)filterButtons.Choice; items.AddRange(ItemSorter.SortAndFilter(heart.GetStoredItems(), sortMode, filterMode, modSearchBar.Text, nameSearchBar.Text)); }
// In this example we used Initialize and Unload_Inner in conjunction with Load and Unload to avoid TypeInitializationException, but we could also just check Enabled before calling MagicStorageIntegration.Unload(); and assign MagicStorage and check Enabled before calling MagicStorageIntegration.Load(); // I opted to keep the MagicStorage integration logic in the MagicStorageIntegration class to keep MagicStorage related code as sparse and unobtrusive as possible within the remaining codebase for this mod. // That said, in ItemChecklistPlayer, we see this: // if (ItemChecklistUI.collectChestItems && MagicStorageIntegration.Enabled) // MagicStorageIntegration.FindItemsInStorage(); // I could have changed this to always call FindItemsInStorage and have FindItemsInStorage check Enabled, but that would require a FindItemsInStorage_Inner type class once again to prevent errors. // Whatever approach you do, be aware of what you are doing and be careful. // StoragePlayer and TEStorageHeart are classes in MagicStorage. // Make sure to extract the .dll from the .tmod and then add them to your .csproj as references. // As a convention, I rename the .dll file ModName_v1.2.3.4.dll and place them in Mod Sources/Mods/lib. // I do this for organization and so the .csproj loads properly for others using the GitHub repository. // Remind contributors to download the referenced mod itself if they wish to build the mod. internal static void FindItemsInStorage() { var storagePlayer = Main.LocalPlayer.GetModPlayer <StoragePlayer>(); Point16 storageAccess = storagePlayer.ViewingStorage(); if (storageAccess == previousStorageAccess) { return; } previousStorageAccess = storageAccess; if (!Main.playerInventory || storageAccess.X < 0 || storageAccess.Y < 0) { return; } ModTile modTile = TileLoader.GetTile(Main.tile[storageAccess.X, storageAccess.Y].type); if (modTile == null || !(modTile is StorageAccess)) { return; } TEStorageHeart heart = ((StorageAccess)modTile).GetHeart(storageAccess.X, storageAccess.Y); if (heart == null) { return; } var items = heart.GetStoredItems(); // Will 1000 items crash the chat? foreach (var item in items) { ItemChecklist.instance.GetGlobalItem <ItemChecklistGlobalItem>().ItemReceived(item); } }
public static bool DrawStorageGUI() { Player player = Main.player[Main.myPlayer]; StoragePlayer modPlayer = player.GetModPlayer <StoragePlayer>(); Point16 storageAccess = modPlayer.ViewingStorage(); if (!Main.playerInventory || storageAccess.X < 0 || storageAccess.Y < 0) { return(true); } ModTile modTile = TileLoader.GetTile(Main.tile[storageAccess.X, storageAccess.Y].type); if (modTile == null || !(modTile is StorageAccess)) { return(true); } TEStorageHeart heart = ((StorageAccess)modTile).GetHeart(storageAccess.X, storageAccess.Y); if (heart == null) { return(true); } if (modTile is CraftingAccess) { CraftingGUI.Draw(heart); } else { StorageGUI.Draw(heart); } return(true); }
public static void RefreshItems() { if (StoragePlayer.IsStorageCrafting()) { CraftingGUI.RefreshItems(); return; } items.Clear(); didMatCheck.Clear(); TEStorageHeart heart = GetHeart(); if (heart == null) { return; } InitLangStuff(); InitSortButtons(); InitFilterButtons(); SortMode sortMode = (SortMode)sortButtons.Choice; FilterMode filterMode = (FilterMode)filterButtons.Choice; items.AddRange(ItemSorter.SortAndFilter(heart.GetStoredItems(), sortMode, filterMode, searchBar2.Text, searchBar.Text)); for (int k = 0; k < items.Count; k++) { didMatCheck.Add(false); } }
public static void ReceiveClientSendTEUpdate(BinaryReader reader, int sender) { if (Main.netMode == 2) { int id = reader.ReadInt32(); TileEntity ent = TileEntity.Read(reader, true); ent.ID = id; TileEntity.ByID[id] = ent; TileEntity.ByPosition[ent.Position] = ent; if (ent is TEStorageUnit) { TEStorageHeart heart = ((TEStorageUnit)ent).GetHeart(); if (heart != null) { heart.ResetCompactStage(); } } NetMessage.SendData(MessageID.TileEntitySharing, -1, sender, null, id, ent.Position.X, ent.Position.Y); } else if (Main.netMode == 1) { //Still need to read the data reader.ReadInt32(); } }
private void HandleStorageItemChange(TEStorageHeart heart) { if (Main.netMode == 2) { NetHelper.SendRefreshNetworkItems(heart.ID); } else if (Main.netMode == 0) { StorageGUI.RefreshItems(); } }
public override void OnActivate() { heart = StoragePlayer.LocalPlayer.GetStorageHeart(); if (heart == null) { Deactivate(); return; } RefreshItems(); }
public static void DoWithdrawItemForCraft(Recipe self, ref Item item, ref int required) { if (StoragePlayer.Get.StorageAccess.X < 0) { return; } var instance = MagicStoragePlus.Instance.StorageUI; bool changed = false; foreach (Item i in instance.items) { if (required <= 0) { if (changed) { instance.RefreshItems(); } return; } if (item.IsTheSameAs(i) || self.useWood(item.type, i.type) || self.useSand(item.type, i.type) || self.useIronBar(item.type, i.type) || self.usePressurePlate(item.type, i.type) || self.useFragment(item.type, i.type) || self.AcceptedByItemGroups(item.type, i.type)) { int count = Math.Min(required, item.stack); required -= count; var request = i.Clone(); request.stack = count; TEStorageHeart heart = StoragePlayer.GetStorageHeart(); if (Main.netMode == 0) { request = heart.TryWithdraw(request); } else { NetHelper.SendWithdraw(heart.ID, request, NetHelper.StorageOp.WithdrawJustRemove); request = new Item(); } if (!request.IsAir) { request.TurnToAir(); changed = true; } } } if (changed) { instance.RefreshItems(); } }
public override void OnDeactivate() { heart = null; items = null; sortMode = SortMode.Default; filterMode = FilterMode.All; sortButtons.choice = 0; filterButtons.choice = 0; nameFilter = string.Empty; modFilter = string.Empty; }
public override void OnActivate() { heart = StoragePlayer.LocalPlayer.GetStorageHeart(); access = StoragePlayer.LocalPlayer.GetCraftingAccess(); if (access == null || heart == null) { Deactivate(); return; } Refresh(); }
public IEnumerable <Tuple <Item, object> > EnumerateItems(int x, int y) { TEStorageHeart targetHeart = FindHeart(x, y); if (targetHeart == null) { yield break; } foreach (var item in targetHeart.GetStoredItems()) { yield return(new Tuple <Item, object>(item, item)); } }
private void DoDeposit(Item item) { TEStorageHeart heart = GetHeart(); if (Main.netMode == NetmodeID.SinglePlayer) { heart.DepositItem(item); } else { NetHelper.SendDeposit(heart.ID, item); item.SetDefaults(0, true); } }
private static Item DoWithdraw(Item item, bool toInventory = false) { TEStorageHeart heart = GetHeart(); if (Main.netMode == 0) { return(heart.TryWithdraw(item)); } else { NetHelper.SendWithdraw(heart.ID, item, toInventory); return(new Item()); } }
private static void DoDeposit(Item item) { TEStorageHeart heart = GetHeart(); if (Main.netMode == 0) { heart.DepositItem(item); } else { NetHelper.SendDeposit(heart.ID, item); item.SetDefaults(0, true); } }
static Item DoWithdraw(Item item, bool toInventory = false) { TEStorageHeart heart = StoragePlayer.GetStorageHeart(); if (Main.netMode == 0) { return(heart.TryWithdraw(item)); } else { var type = toInventory ? NetHelper.StorageOp.WithdrawToInventory : NetHelper.StorageOp.Withdraw; NetHelper.SendWithdraw(heart.ID, item, type); return(new Item()); } }
public static void Draw(TEStorageHeart heart) { Player player = Main.player[Main.myPlayer]; StoragePlayer modPlayer = player.GetModPlayer <StoragePlayer>(); Initialize(); if (Main.mouseX > panelLeft && Main.mouseX < panelLeft + panelWidth && Main.mouseY > panelTop && Main.mouseY < panelTop + panelHeight) { player.mouseInterface = true; player.showItemIcon = false; InterfaceHelper.HideItemIconCache(); } basePanel.Draw(Main.spriteBatch); slotZone.DrawText(); sortButtons.DrawText(); filterButtons.DrawText(); }
public void TakeItem(int x, int y, object slot, int amount) { TEStorageHeart targetHeart = FindHeart(x, y); if (targetHeart == null) { return; } Item toWithdraw = ((Item)slot).Clone(); toWithdraw.stack = amount; targetHeart.TryWithdraw(toWithdraw); HandleStorageItemChange(targetHeart); }
private TEStorageHeart FindHeart(int x, int y) { Tile tile = Main.tile[x, y]; int originX = x - tile.frameX / 18; int originY = y - tile.frameY / 18; Point16 center = TEStorageComponent.FindStorageCenter(new Point16(originX, originY)); if (center.X == -1 && center.Y == -1) { return(null); } TEStorageHeart heart = ((TEStorageCenter)TileEntity.ByPosition[center]).GetHeart(); return(heart); }
public static void ReceiveCraftRequest(BinaryReader reader, int sender) { int ent = reader.ReadInt32(); int count = reader.ReadInt32(); if (Main.netMode != 2) { //Still need to read the data for exceptions to not be thrown for (int i = 0; i < count; i++) { ItemIO.Receive(reader, true); } ItemIO.Receive(reader, true); return; } if (!TileEntity.ByID.ContainsKey(ent) || !(TileEntity.ByID[ent] is TEStorageHeart)) { return; } TEStorageHeart heart = (TEStorageHeart)TileEntity.ByID[ent]; List <Item> toWithdraw = new List <Item>(); for (int k = 0; k < count; k++) { toWithdraw.Add(ItemIO.Receive(reader, true)); } Item result = ItemIO.Receive(reader, true); List <Item> items = CraftingGUI.DoCraft(heart, toWithdraw, result); if (items.Count > 0) { ModPacket packet = MagicStorage.Instance.GetPacket(); packet.Write((byte)MessageType.CraftResult); packet.Write(items.Count); foreach (Item item in items) { ItemIO.Send(item, packet, true); } packet.Send(sender); } SendRefreshNetworkItems(ent); }
public override void OnDeactivate() { Main.HidePlayerCraftingMenu = false; heart = null; access = null; recipes = null; recipeAvailable = null; selectedRecipe = null; filterButtons.choice = 0; sortButtons.choice = 0; recipeButtons.choice = 0; filterMode = FilterMode.All; sortMode = SortMode.Default; modFilter = string.Empty; nameFilter = string.Empty; }
public static void Draw(TEStorageHeart heart) { Player player = Main.player[Main.myPlayer]; StoragePlayer modPlayer = player.GetModPlayer <StoragePlayer>(MagicStorage.Instance); Initialize(); if (Main.mouseX > panelLeft && Main.mouseX < panelLeft + panelWidth && Main.mouseY > panelTop && Main.mouseY < panelTop + panelHeight) { player.mouseInterface = true; player.showItemIcon = false; InterfaceHelper.HideItemIconCache(); } basePanel.Draw(Main.spriteBatch); float itemSlotWidth = Main.inventoryBackTexture.Width * inventoryScale; float itemSlotHeight = Main.inventoryBackTexture.Height * inventoryScale; Vector2 slotZonePos = slotZone.GetDimensions().Position(); float oldScale = Main.inventoryScale; Main.inventoryScale = inventoryScale; Item[] temp = new Item[11]; for (int k = 0; k < numColumns * displayRows; k++) { int index = k + numColumns * (int)Math.Round(scrollBar.ViewPosition); Item item = index < items.Count ? items[index] : new Item(); if (!item.IsAir && !didMatCheck[index]) { item.checkMat(); didMatCheck[index] = true; } Vector2 drawPos = slotZonePos + new Vector2((itemSlotWidth + padding) * (k % 10), (itemSlotHeight + padding) * (k / 10)); temp[10] = item; ItemSlot.Draw(Main.spriteBatch, temp, 0, 10, drawPos); } if (hoverSlot >= 0 && hoverSlot < items.Count) { Main.HoverItem = items[hoverSlot].Clone(); Main.instance.MouseText(string.Empty); } sortButtons.DrawText(); filterButtons.DrawText(); Main.inventoryScale = oldScale; }
public bool InjectItem(int x, int y, Item item) { int oldstack = item.stack; TEStorageHeart targetHeart = FindHeart(x, y); if (targetHeart == null) { return(false); } targetHeart.DepositItem(item); if (oldstack != item.stack) { HandleStorageItemChange(targetHeart); return(true); } return(false); }
public static bool TryDepositAll() { Player player = Main.player[Main.myPlayer]; TEStorageHeart heart = StoragePlayer.GetStorageHeart(); bool changed = false; if (Main.netMode == 0) { for (int k = 10; k < 50; k++) { if (!player.inventory[k].IsAir && !player.inventory[k].favorited) { int oldStack = player.inventory[k].stack; heart.DepositItem(player.inventory[k]); if (oldStack != player.inventory[k].stack) { changed = true; } } } } else { List <Item> items = new List <Item>(); for (int k = 10; k < 50; k++) { if (!player.inventory[k].IsAir && !player.inventory[k].favorited) { items.Add(player.inventory[k]); } } NetHelper.SendDepositAll(heart.ID, items); foreach (Item item in items) { item.SetDefaults(0, true); } changed = true; items.Clear(); } return(changed); }
public static int ItemCount(int itemType) { int count = 0; List <Item> countList = new List <Item>(); TEStorageHeart heart = GetHeart(); if (heart == null) { return(-1); } countList.Clear(); countList.AddRange(ItemSorter.SortAndFilter(heart.GetStoredItems(), SortMode.Default, FilterMode.All, SubFilterMode.All, "", "")); foreach (Item sItem in countList) { if (sItem.type == itemType) { count += sItem.stack; } } return(count); }
public static void ReceiveCraftRequest(BinaryReader reader, int sender) { if (Main.netMode != NetmodeID.Server) { return; } int heartID = reader.ReadInt32(); if (!TileEntity.ByID.ContainsKey(heartID) || !(TileEntity.ByID[heartID] is TEStorageHeart)) { return; } TEStorageHeart heart = (TEStorageHeart)TileEntity.ByID[heartID]; TECraftingAccess access = (TECraftingAccess)TileEntity.ByID[reader.ReadInt32()]; int count = reader.ReadInt32(); List <Item> toWithdraw = new List <Item>(); for (int k = 0; k < count; k++) { toWithdraw.Add(ItemIO.Receive(reader, true)); } Item result = ItemIO.Receive(reader, true); List <Item> items = access.DoCraft(heart, toWithdraw, result); if (items.Count > 0) { ModPacket packet = MagicStorageTwo.Instance.GetPacket(); packet.Write((byte)MessageType.CraftResult); packet.Write(items.Count); foreach (Item item in items) { ItemIO.Send(item, packet, true); } packet.Send(sender); } SendRefreshNetworkItems(heartID); }
public static void Draw(TEStorageHeart heart) { Player player = Main.player[Main.myPlayer]; StoragePlayer modPlayer = player.GetModPlayer <StoragePlayer>(MagicStorage.Instance); Initialize(); if (Main.mouseX > panelLeft && Main.mouseX < panelLeft + panelWidth && Main.mouseY > panelTop && Main.mouseY < panelTop + panelHeight) { player.mouseInterface = true; player.showItemIcon = false; InterfaceHelper.HideItemIconCache(); } basePanel.Draw(Main.spriteBatch); float itemSlotWidth = Main.inventoryBackTexture.Width * inventoryScale; float itemSlotHeight = Main.inventoryBackTexture.Height * inventoryScale; Vector2 slotZonePos = slotZone.GetDimensions().Position(); float oldScale = Main.inventoryScale; Main.inventoryScale = inventoryScale; Item[] temp = new Item[11]; Item[] craftingStations = GetCraftingStations(); for (int k = 0; k < numColumns; k++) { temp[10] = craftingStations[k]; Vector2 drawPos = GetSlotPosition(k); ItemSlot.Draw(Main.spriteBatch, temp, 0, 10, drawPos); } if (hoverSlot >= 0 && hoverSlot < items.Count) { Main.HoverItem = items[hoverSlot].Clone(); Main.instance.MouseText(string.Empty); } sortButtons.DrawText(); filterButtons.DrawText(); Main.inventoryScale = oldScale; }
public void RefreshItems() { items.Clear(); TEStorageHeart heart = StoragePlayer.GetStorageHeart(); if (heart == null) { return; } // @Volatile :SearchBars var searchByName = sideBar[0] as UISearchBar; var searchByMod = sideBar[1] as UISearchBar; string nameFilter = ""; if (searchByName.Text != searchByName.HintText) { nameFilter = searchByName.Text; } string modFilter = ""; if (searchByMod.Text != searchByMod.HintText) { modFilter = searchByMod.Text; } var sort = sideBar[2] as UIDropDown; // @Volatile :SortAndFilter var filter = sideBar[3] as UIDropDown; // @Volatile :SortAndFilter items.AddRange(Sorting.Items.FilterAndSort(heart.GetStoredItems(), (SortMode)sort.CurrentOption, (FilterMode)filter.CurrentOption, modFilter, nameFilter)); items.ForEach(x => x.checkMat()); Recipe.FindRecipes(); }
internal List <Item> DoCraft(TEStorageHeart heart, List <Item> toWithdraw, Item result) { List <Item> items = new List <Item>(); foreach (Item tryWithdraw in toWithdraw) { Item withdrawn = heart.TryWithdraw(tryWithdraw); if (!withdrawn.IsAir) { items.Add(withdrawn); } if (withdrawn.stack < tryWithdraw.stack) { for (int k = 0; k < items.Count; k++) { heart.DepositItem(items[k]); if (items[k].IsAir) { items.RemoveAt(k); k--; } } return(items); } } items.Clear(); heart.DepositItem(result); if (!result.IsAir) { items.Add(result); } return(items); }
public static void ReceiveStorageOp(BinaryReader reader, int sender) { if (Main.netMode != 2) { return; } int ent = reader.ReadInt32(); if (!TileEntity.ByID.ContainsKey(ent) || !(TileEntity.ByID[ent] is TEStorageHeart)) { return; } TEStorageHeart heart = (TEStorageHeart)TileEntity.ByID[ent]; var op = (StorageOp)reader.ReadByte(); if (op == StorageOp.Deposit) { Item item = ItemIO.Receive(reader, true); heart.DepositItem(item); if (!item.IsAir) { ModPacket packet = PrepareOpResult(op); ItemIO.Send(item, packet, true); packet.Send(sender); } } else if (op == StorageOp.DepositList) { int count = reader.ReadByte(); List <Item> items = new List <Item>(); StartUpdateQueue(); for (int k = 0; k < count; k++) { Item item = ItemIO.Receive(reader, true); heart.DepositItem(item); if (!item.IsAir) { items.Add(item); } } ProcessUpdateQueue(); if (items.Count > 0) { ModPacket packet = PrepareOpResult(op); packet.Write((byte)items.Count); foreach (Item item in items) { ItemIO.Send(item, packet, true); } packet.Send(sender); } } else if (op == StorageOp.Withdraw || op == StorageOp.WithdrawToInventory) { Item item = ItemIO.Receive(reader, true); item = heart.TryWithdraw(item); if (!item.IsAir) { ModPacket packet = PrepareOpResult(op); ItemIO.Send(item, packet, true); packet.Send(sender); } } else if (op == StorageOp.WithdrawJustRemove) { // @Robustness: We assume that nothing could go wrong here. // And maybe nothing could. But still something to look at for potential bugs... Item item = ItemIO.Receive(reader, true); heart.TryWithdraw(item); } SendRefreshNetworkItems(ent); }
public static void Initialize() { InitLangStuff(); float itemSlotWidth = Main.inventoryBackTexture.Width * inventoryScale; float itemSlotHeight = Main.inventoryBackTexture.Height * inventoryScale; panelTop = Main.instance.invBottom + 60; panelLeft = 20f; basePanel = new UIPanel(); float innerPanelLeft = panelLeft + basePanel.PaddingLeft; float innerPanelWidth = numColumns * (itemSlotWidth + padding) + 20f + padding; panelWidth = basePanel.PaddingLeft + innerPanelWidth + basePanel.PaddingRight; panelHeight = Main.screenHeight - panelTop - 40f; basePanel.Left.Set(panelLeft, 0f); basePanel.Top.Set(panelTop, 0f); basePanel.Width.Set(panelWidth, 0f); basePanel.Height.Set(panelHeight, 0f); basePanel.Recalculate(); topBar = new UIElement(); topBar.Width.Set(0f, 1f); topBar.Height.Set(32f, 0f); basePanel.Append(topBar); InitSortButtons(); topBar.Append(sortButtons); depositButton.Left.Set(sortButtons.GetDimensions().Width + 2 * padding, 0f); depositButton.Width.Set(128f, 0f); depositButton.Height.Set(-2 * padding, 1f); depositButton.PaddingTop = 8f; depositButton.PaddingBottom = 8f; topBar.Append(depositButton); float depositButtonRight = sortButtons.GetDimensions().Width + 2 * padding + depositButton.GetDimensions().Width; searchBar.Left.Set(depositButtonRight + padding, 0f); searchBar.Width.Set(-depositButtonRight - 2 * padding, 1f); searchBar.Height.Set(0f, 1f); topBar.Append(searchBar); topBar2 = new UIElement(); topBar2.Width.Set(0f, 1f); topBar2.Height.Set(32f, 0f); topBar2.Top.Set(36f, 0f); basePanel.Append(topBar2); InitFilterButtons(); topBar2.Append(filterButtons); searchBar2.Left.Set(depositButtonRight + padding, 0f); searchBar2.Width.Set(-depositButtonRight - 2 * padding, 1f); searchBar2.Height.Set(0f, 1f); topBar2.Append(searchBar2); slotZone.Width.Set(0f, 1f); slotZone.Top.Set(76f, 0f); slotZone.Height.Set(-116f, 1f); basePanel.Append(slotZone); numRows = (items.Count + numColumns - 1) / numColumns; displayRows = (int)slotZone.GetDimensions().Height / ((int)itemSlotHeight + padding); slotZone.SetDimensions(numColumns, displayRows); int noDisplayRows = numRows - displayRows; if (noDisplayRows < 0) { noDisplayRows = 0; } scrollBarMaxViewSize = 1 + noDisplayRows; scrollBar.Height.Set(displayRows * (itemSlotHeight + padding), 0f); scrollBar.Left.Set(-20f, 1f); scrollBar.SetView(scrollBarViewSize, scrollBarMaxViewSize); slotZone.Append(scrollBar); bottomBar.Width.Set(0f, 1f); bottomBar.Height.Set(32f, 0f); bottomBar.Top.Set(-32f, 1f); basePanel.Append(bottomBar); capacityText.Left.Set(6f, 0f); capacityText.Top.Set(6f, 0f); TEStorageHeart heart = GetHeart(); int numItems = 0; int capacity = 0; if (heart != null) { foreach (TEAbstractStorageUnit abstractStorageUnit in heart.GetStorageUnits()) { if (abstractStorageUnit is TEStorageUnit) { TEStorageUnit storageUnit = (TEStorageUnit)abstractStorageUnit; numItems += storageUnit.NumItems; capacity += storageUnit.Capacity; } } } capacityText.SetText(numItems + "/" + capacity + " Items"); bottomBar.Append(capacityText); }