Exemple #1
0
 private void PrePlaceChest(CommandArgs e)
 {
     for (int i = 0; i < shop.Length; i++)
     {
         if (shop[i] == null || !shop[i].enabled)
         {
             shop[i] = new ShopChest()
             {
                 active = true,
                 owner  = e.Player.Index,
                 player = e.TPlayer,
                 index  = i
             };
             e.Player.SendSuccessMessage(string.Concat("Shop with index ", i, " made available (hit or place a chest)."));
             break;
         }
     }
 }
Exemple #2
0
 private void OnGetData(GetDataEventArgs e)
 {
     if (!e.Handled)
     {
         if (e.MsgID == PacketTypes.PlayerSlot)
         {
             using (BinaryReader br = new BinaryReader(new MemoryStream(e.Msg.readBuffer, e.Index, e.Length)))
             {
                 byte  id     = br.ReadByte();
                 byte  slot   = br.ReadByte();
                 short stack  = br.ReadInt16();
                 byte  prefix = br.ReadByte();
                 short netID  = br.ReadInt16();
             }
         }
         if (e.MsgID == PacketTypes.ChestGetContents)
         {
             using (BinaryReader br = new BinaryReader(new MemoryStream(e.Msg.readBuffer, e.Index, e.Length)))
             {
                 short tileX = br.ReadInt16();
                 short tileY = br.ReadInt16();
                 foreach (ShopChest sc in shop)
                 {
                     if (sc != null)
                     {
                         var r = new RUDD.Region("", new Vector2(tileX - 1, tileY - 1), new Vector2(tileX + 1, tileY + 1));
                         if (r.Contains(sc.x, sc.y))
                         {
                             for (int i = 0; i < sc.chest.item.Length; i++)
                             {
                                 if (sc.contents[i] != null)
                                 {
                                     if (sc.refill || resetContents[sc.index2])
                                     {
                                         Main.chest[sc.index].item[i].netDefaults(sc.contents[i].type);
                                         Main.chest[sc.index].item[i].stack  = sc.contents[i].stack;
                                         Main.chest[sc.index].item[i].prefix = sc.contents[i].prefix;
                                         TShock.Players[sc.owner].SendData(PacketTypes.ChestItem, "", sc.index, i, Main.chest[sc.index].item[i].stack, Main.chest[sc.index].item[i].prefix, Main.chest[sc.index].item[i].type);
                                     }
                                 }
                             }
                             resetContents[sc.index2] = false;
                             break;
                         }
                     }
                 }
             }
         }
         if (e.MsgID == PacketTypes.ChestItem)
         {
             using (BinaryReader br = new BinaryReader(new MemoryStream(e.Msg.readBuffer, e.Index, e.Length)))
             {
                 short cID    = br.ReadInt16();
                 byte  slot   = br.ReadByte();
                 short stack  = br.ReadInt16();
                 byte  prefix = br.ReadByte();
                 short itemID = br.ReadInt16();
                 foreach (ShopChest sc in shop)
                 {
                     if (sc != null)
                     {
                         if (sc.index == cID)
                         {
                             Player player = null;
                             foreach (Player p in Main.player)
                             {
                                 if (p.chest == sc.index)
                                 {
                                     player = p;
                                     break;
                                 }
                             }
                             if (player != null)
                             {
                                 if (itemID == 0)
                                 {
                                     if (sc.contents != null)
                                     {
                                         if (sc.contents[slot] != null)
                                         {
                                             if (sc.contents[slot].type != 0)
                                             {
                                                 int value = sc.contents[slot].value == 0 ? sc.chest.item[slot].value : sc.contents[slot].value;
                                                 value = Main.hardMode && priceDouble ? value * 2 : value;
                                                 if (!CoinPurse.ShopItem(player.whoAmI, value))
                                                 {
                                                     active[player.whoAmI]         = new ShopChest();
                                                     active[player.whoAmI].invalid = new Item();
                                                     active[player.whoAmI].invalid.netDefaults(sc.contents[slot].type);
                                                     active[player.whoAmI].invalid.stack  = sc.contents[slot].stack;
                                                     active[player.whoAmI].invalid.prefix = sc.contents[slot].prefix;
                                                     TShock.Players[player.whoAmI].SendInfoMessage(string.Concat("This item requires ", value, " copper."));
                                                     justBought[player.whoAmI] = true;
                                                     resetContents[sc.index2]  = true;
                                                 }
                                                 else
                                                 {
                                                     var block = CoinStorage.data.GetBlock(TShock.Players[player.whoAmI].UUID);
                                                     block.WriteValue("Deliver", "True");
                                                     block.IncreaseValue("Coins", value);
                                                     soldItem[sc.owner] = true;
                                                     if (!sc.refill)
                                                     {
                                                         sc.contents[slot].type = 0;
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                             break;
                         }
                     }
                 }
             }
         }
         else if (e.MsgID == PacketTypes.ChestOpen)
         {
             using (BinaryReader br = new BinaryReader(new MemoryStream(e.Msg.readBuffer, e.Index, e.Length)))
             {
                 short id = br.ReadInt16();
                 short x  = br.ReadInt16();
                 short y  = br.ReadInt16();
             }
         }
         else if (e.MsgID == PacketTypes.PlaceChest)
         {
             using (BinaryReader br = new BinaryReader(new MemoryStream(e.Msg.readBuffer, e.Index, e.Length)))
             {
                 byte  id    = br.ReadByte();
                 short tileX = br.ReadInt16();
                 short tileY = br.ReadInt16();
             }
         }
         if (e.MsgID == PacketTypes.Tile)
         {
             using (BinaryReader br = new BinaryReader(new MemoryStream(e.Msg.readBuffer, e.Index, e.Length)))
             {
                 byte  action = br.ReadByte();
                 short x      = br.ReadInt16();
                 short y      = br.ReadInt16();
                 if (action == 0)
                 {
                     foreach (Player player in Main.player)
                     {
                         if (player.active)
                         {
                             if (canSelect[player.whoAmI])
                             {
                                 if (Main.tile[x, y].type == TileID.Containers)
                                 {
                                     for (int i = 0; i < shop.Length; i++)
                                     {
                                         if (shop[i] == null || !shop[i].enabled)
                                         {
                                             for (int j = 0; j < Main.chest.Length; j++)
                                             {
                                                 if (new Microsoft.Xna.Framework.Rectangle(Main.chest[j].x * 16, Main.chest[j].y * 16, 32, 32).Contains(x * 16 + 8, y * 16 + 8))
                                                 {
                                                     shop[i]          = new ShopChest();
                                                     shop[i].chest    = Main.chest[j];
                                                     shop[i].contents = ShopItem.Convert(Main.chest[j].item, priceFloor[player.whoAmI]);
                                                     shop[i].index    = j;
                                                     shop[i].index2   = i;
                                                     shop[i].x        = Main.chest[j].x;
                                                     shop[i].y        = Main.chest[j].y;
                                                     shop[i].enabled  = true;
                                                     shop[i].active   = true;
                                                     shop[i].owner    = player.whoAmI;
                                                     shop[i].value    = priceFloor[player.whoAmI];
                                                     shop[i].refill   = chestRefill[player.whoAmI];
                                                     CoinStorage.WriteBlock(TShock.Players[player.whoAmI]);
                                                     if (data.BlockExists("Chest" + j))
                                                     {
                                                         var block = data.GetBlock("Chest" + j);
                                                         block.WriteValue("ChestX", x.ToString());
                                                         block.WriteValue("ChestY", y.ToString());
                                                         block.WriteValue("Index", j.ToString());
                                                         block.WriteValue("OwnerID", player.whoAmI.ToString());
                                                         block.WriteValue("Price", priceFloor[player.whoAmI].ToString());
                                                         block.WriteValue("Refill", chestRefill[player.whoAmI].ToString());
                                                         for (int m = 0; m < shop[i].contents.Length; m++)
                                                         {
                                                             block.WriteValue("Slot" + m, shop[i].contents[m].type.ToString());
                                                         }
                                                     }
                                                     canSelect[player.whoAmI]  = false;
                                                     priceFloor[player.whoAmI] = 0;
                                                     TShock.Players[player.whoAmI].SendSuccessMessage("The chest is now set as a shop.");
                                                     break;
                                                 }
                                             }
                                             break;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #3
0
        private void SetAsShop(CommandArgs e)
        {
            string raw = string.Empty, cmd = string.Empty, opt = string.Empty;

            switch (0)
            {
            case 0:
                raw = e.Message.Substring(10);
                if (raw.Contains(" "))
                {
                    cmd = raw.Substring(0, raw.IndexOf(" "));
                }
                else
                {
                    goto case 1;
                }
                try
                {
                    opt = raw.Substring(raw.LastIndexOf(" ") + 1);
                }
                catch
                {
                    goto case 1;
                }
                break;

            case 1:
                e.Player.SendInfoMessage("Commands for shop chests:\naddcontent [item ID]\nremovecontent [item ID]\ncomplete [name]\nfloor [price]");
                return;
            }
            ShopChest sc = null;

            foreach (ShopChest t in shop)
            {
                if (t != null)
                {
                    if ((!t.enabled && t.active && t.player == e.TPlayer) || t.name == opt)
                    {
                        sc = t;
                        break;
                    }
                }
            }
            if (sc == null)
            {
                return;
            }
            switch (cmd)
            {
            case "complete":
                sc.enabled = true;
                sc.name    = opt;
                sc.player  = e.TPlayer;
                sc.owner   = e.Player.Index;
                e.Player.SendInfoMessage("Mine a chest to complete the setup.");
                break;

            case "addcontent":
                for (int i = 0; i < sc.contents.Length; i++)
                {
                    if (sc.contents[i] == null)
                    {
                        sc.contents[i] = new ShopItem();
                    }
                    int type = 0;
                    if (sc.contents[i].type == 0 && int.TryParse(opt, out type))
                    {
                        sc.contents[i].type = type;
                        e.Player.SendInfoMessage("Added item [" + type + "].");
                        break;
                    }
                }
                break;

            case "removecontent":
                for (int i = 0; i < sc.contents.Length; i++)
                {
                    int type = 0;
                    if ((sc.contents[i] != null || sc.contents[i].type != 0) && int.TryParse(opt, out type))
                    {
                        if (sc.contents[i].type == type)
                        {
                            sc.contents[i].type = 0;
                            e.Player.SendInfoMessage("Removed item [" + type + "].");
                            break;
                        }
                    }
                }
                break;

            case "list":
                string list = "";
                foreach (var item in sc.contents)
                {
                    if (item != null)
                    {
                        if (item.type != 0)
                        {
                            list += string.Concat(item.type, " ");
                        }
                    }
                }
                e.Player.SendInfoMessage(list);
                break;

            default:
                break;
            }
        }
Exemple #4
0
 private void OnPostInit(EventArgs e)
 {
     data             = new DataStore("config\\player_shop_" + Main.worldName);
     CoinStorage.data = new DataStore("config\\coin_storage_" + Main.worldName);
     string[] list = new string[46];
     list[0] = "ChestX";
     list[1] = "ChestY";
     list[2] = "Index";
     list[3] = "OwnerID";
     list[4] = "Price";
     list[5] = "Refill";
     for (int i = 6; i < 46; i++)
     {
         list[i] += "Slot" + (i - 6);
     }
     for (int m = 0; m < shop.Length; m++)
     {
         string heading = "Chest" + m;
         if (!data.BlockExists(heading))
         {
             data.NewBlock(list, heading);
         }
         else
         {
             var  block  = data.GetBlock(heading);
             int  Index  = int.Parse(block.GetValue("Index"));
             int  value  = int.Parse(block.GetValue("Price"));
             bool Refill = block.GetValue("Refill").ToLower() != "true" ? false : true;
             shop[m] = new ShopChest()
             {
                 active  = true,
                 enabled = Index != 0,
                 refill  = Refill,
                 owner   = int.Parse(block.GetValue("OwnerID")),
                 index   = Index,
                 index2  = m
             };
             if (shop[m].enabled)
             {
                 if (Main.chest[Index] != null)
                 {
                     shop[m].chest = Main.chest[Index];
                     shop[m].x     = shop[m].chest.x;
                     shop[m].y     = shop[m].chest.y;
                     for (int j = 0; j < shop[m].contents.Length; j++)
                     {
                         shop[m].contents[j] = new ShopItem()
                         {
                             type   = int.Parse(block.GetValue("Slot" + j)),
                             value  = int.Parse(block.GetValue("Price")),
                             stack  = 1,
                             prefix = 0
                         };
                     }
                 }
                 else
                 {
                     shop[m].enabled = false;
                 }
             }
         }
     }
 }