tModLoader v2024.11
A mod to make and play Terraria mods
Chest Class Reference

Represents a non-Player inventory, such as chests, portable storage, or NPC shops. More...

Inherits IFixLoadedData.

Public Member Functions

 Chest (bool bank=false)
 Create a new Chest with no name.
 
int AddItemToShop (Item newItem)
 Adds an item to the first empty slot in this shop. More...
 
object Clone ()
 
void FixLoadedData ()
 Calls Item.FixAgainstExploit on all of this chest's items.
 
void SetupShop (int type)
 
void SetupShop (string shopName, NPC npc)
 
override string ToString ()
 

Static Public Member Functions

static int AfterPlacement_Hook (int x, int y, int type=21, int style=0, int direction=1, int alternate=0)
 Registers a placed chest into the world. More...
 
static void AskForChestToEatItem (Vector2 worldPosition, int duration)
 Visually opens the chest at worldPosition for duration ticks. More...
 
static bool CanDestroyChest (int X, int Y)
 Determines if the chest at (X , Y ) can be destroyed. More...
 
static int CreateChest (int X, int Y, int id=-1)
 Creates and registers a new chest into the world. More...
 
static bool DestroyChest (int X, int Y)
 Attempts to destroy the chest at (X , Y ). More...
 
static void DestroyChestDirect (int X, int Y, int id)
 Forcibly destroys the chest at (X , Y ) with ID id if it exists.
Warning: This will destroy any items contained within the chest. More...
 
static int FindChest (int X, int Y)
 Tries to find a Chest located at (X , Y ) (tile coordinates). More...
 
static int FindChestByGuessing (int X, int Y)
 Tries to find the Chest associated with the tile at (X , Y ) (tile coordinates). More...
 
static int FindEmptyChest (int x, int y, int type=21, int style=0, int direction=1, int alternate=0)
 Determines if a new chest can exist at tile coordinates (x , y ). More...
 
static List< int > GetCurrentlyOpenChests ()
 Gets a list of currently-open chests in the world.
Never called on NetmodeID.MultiplayerClient. More...
 
static void Initialize ()
 
static bool IsLocked (int x, int y)
 Determines if the chest at Main.tile[x , y ] is locked. More...
 
static bool IsLocked (int x, int y, Tile t)
 
static bool Lock (int X, int Y)
 Locks an unlocked chest. More...
 
static bool NearOtherChests (int x, int y)
 Determines if there are any chests near the tile coordinates (x , y ). More...
 
static Item PutItemInNearbyChest (Item item, Vector2 position)
 Attempts to move the given item into any chests near the given position. Cannot place items into a chest if IsPlayerInChest(int) or if that chest IsLocked(int, int) More...
 
static void ServerPlaceItem (int plr, int slot)
 Attempts to move the item in plr 's inventory or Void Vault into nearby chests.
Only called on NetmodeID.Server. More...
 
static void SetupTravelShop ()
 Sets up the NPCID.TravellingMerchant's shop.
 
static void SetupTravelShop_AddToShop (int it, ref int added, ref int count)
 
static void SetupTravelShop_AdjustSlotRarities (int slotItemAttempts, ref int[] rarity)
 
static bool SetupTravelShop_CanAddItemToShop (int it)
 
static void SetupTravelShop_GetItem (Player playerWithHighestLuck, int[] rarity, ref int it, int minimumRarity=0)
 
static void SetupTravelShop_GetPainting (Player playerWithHighestLuck, int[] rarity, ref int it, int minimumRarity=0)
 
static bool Unlock (int X, int Y)
 Unlocks a locked chest. More...
 
static void UpdateChestFrames ()
 Visually updates all chests' frames.
Chests that are being used (GetCurrentlyOpenChests) or eating an item (eatingAnimationTime) will not be opened.
 
static int UsingChest (int i)
 Gets the Player currently using the given chest, if any. More...
 
static void VisualizeChestTransfer (Vector2 position, Vector2 chestPosition, Item item, int amountMoved)
 Visually moves item from position to chestPosition .
If called on NetmodeID.MultiplayerClient, sync the transfer to all clients. More...
 
static void VisualizeChestTransfer_CoinsBatch (Vector2 position, Vector2 chestPosition, long coinsMoved)
 Visually moves coinsMoved worth of coins from position to chestPosition .
If called on NetmodeID.MultiplayerClient, sync the transfer to all clients. More...
 

Public Attributes

bool bankChest
 Unused.
If true, this chest is a portable storage (such as ItemID.PiggyBank) linked to a player.
 
int eatingAnimationTime
 The duration of this chest's eating animation in ticks.
The eating animation plays when an item is quick-stacked into this chest.
 
int frame
 The current frame of this chest's animation.
 
int frameCounter
 The number of ticks this chest has been on its current frame value.
 
Item[] item
 The items this chest contains.
maxItems elements long.
 
string name
 The player-assigned name of this chest. May not exceed MaxNameLength characters in length.
 
int x
 The x-coordinate of this chest in the world, in tile coordinates. (Top left corner tile)
 
int y
 The y-coordinate of this chest in the world, in tile coordinates. (Top left corner tile)
 

Static Public Attributes

static int[] chestItemSpawn = new int[52]
 Associates a TileID.Containers style with the item type (Item.type) that is dropped when the chest is destroyed.
maxChestTypes elements long.
 
static int[] chestItemSpawn2 = new int[17]
 Associates a TileID.Containers2 style with the item type (Item.type) that is dropped when the chest is destroyed.
maxChestTypes2 elements long.
 
const float chestStackRange = 600f
 The maximum distance, in world coordinates, that a player can quick-stack items into chests.
 
static int[] chestTypeToIcon = new int[52]
 Associates a TileID.Containers style with the item type (Item.type) that is shown when the chest is moused over. Differs from chestItemSpawn to show key items for locked chest styles.
maxChestTypes elements long.
 
static int[] chestTypeToIcon2 = new int[17]
 Associates a TileID.Containers2 style with the item type (Item.type) that is shown when the chest is moused over.
maxChestTypes2 elements long. Differs from chestItemSpawn2 to show key items for locked chest styles.
 
static int[] dresserItemSpawn = new int[43]
 Associates a TileID.Dressers style with the item type (Item.type) that is dropped when the dresser is destroyed.
maxDresserTypes elements long.
 
static int[] dresserTypeToIcon = new int[43]
 Associates a TileID.Dressers style with the item type (Item.type) that is shown when the dresser is moused over.
maxDresserTypes elements long.
 
const int maxChestTypes = 52
 The number of vanilla chest styles contained in TileID.Containers.
 
const int maxChestTypes2 = 17
 The number of vanilla chest styles contained in TileID.Containers2.
 
const int maxDresserTypes = 43
 The number of vanilla dresser styles contained in TileID.Dressers.
 
const int maxItems = 40
 The maximum number of items chests can contain.
 
const int MaxNameLength = 63
 The maximum length that name is allowed to be.
 

Detailed Description

Represents a non-Player inventory, such as chests, portable storage, or NPC shops.

Member Function Documentation

◆ AddItemToShop()

int Chest.AddItemToShop ( Item  newItem)

Adds an item to the first empty slot in this shop.

Parameters
newItemThe item to add.
Returns
0 if the item could not be added to the shop, or the index in item if the item was successfully added.

◆ AfterPlacement_Hook()

static int Chest.AfterPlacement_Hook ( int  x,
int  y,
int  type = 21,
int  style = 0,
int  direction = 1,
int  alternate = 0 
)
static

Registers a placed chest into the world.

Parameters
xThe x-coordinate to check, in tile coordinates.
yThe y-coordinate to check, in tile coordinates.
typeThe tile type of the placed chest.
styleThe tile style of the placed chest.
directionUnused, but required for use with PlacementHook.hook.
alternateUnused, but required for use with PlacementHook.hook.
Returns
-1 if there exists a chest in Main.chest at the coordinates (x , y ), or if Main.chest contains no null elements.
Otherwise, the index in Main.chest of the newly-created chest.

◆ AskForChestToEatItem()

static void Chest.AskForChestToEatItem ( Vector2  worldPosition,
int  duration 
)
static

Visually opens the chest at worldPosition for duration ticks.

Parameters
worldPositionThe position of the chest to ask, in world coordinates.
durationThe duration of the eating animation in ticks.

◆ CanDestroyChest()

static bool Chest.CanDestroyChest ( int  X,
int  Y 
)
static

Determines if the chest at (X , Y ) can be destroyed.

Parameters
XThe x-coordinate to check, in tile coordinates.
YThe y-coordinate to check, in tile coordinates.
Returns
false if a chest exists at (X , Y ) and if any element in that chest's item array is not Item.IsAir. Otherwise, returns true.

◆ CreateChest()

static int Chest.CreateChest ( int  X,
int  Y,
int  id = -1 
)
static

Creates and registers a new chest into the world.

Parameters
XThe x-coordinate to check, in tile coordinates.
YThe y-coordinate to check, in tile coordinates.
idThe index in Main.chest that this chest should use. If id != -1, placement cannot fail.
Returns
-1 if id == -1 and FindEmptyChest(int, int, int, int, int, int) returns -1.
Otherwise, the index in Main.chest of the newly-created chest.

It is recommended to use WorldGen.PlaceChest(int, int, ushort, bool, int) to place chests into the world.

◆ DestroyChest()

static bool Chest.DestroyChest ( int  X,
int  Y 
)
static

Attempts to destroy the chest at (X , Y ).

Parameters
XThe x-coordinate to check, in tile coordinates.
YThe y-coordinate to check, in tile coordinates.
Returns
false if a chest exists at (X , Y ) and if any element in that chest's item array is not Item.IsAir. Otherwise, returns true.

◆ DestroyChestDirect()

static void Chest.DestroyChestDirect ( int  X,
int  Y,
int  id 
)
static

Forcibly destroys the chest at (X , Y ) with ID id if it exists.
Warning: This will destroy any items contained within the chest.

Parameters
XThe x-coordinate to check, in tile coordinates.
YThe y-coordinate to check, in tile coordinates.
idThe index in Main.chest of the chest to destroy.

◆ FindChest()

static int Chest.FindChest ( int  X,
int  Y 
)
static

Tries to find a Chest located at (X , Y ) (tile coordinates).

Parameters
XThe x-coordinate to check, in tile coordinates.
YThe y-coordinate to check, in tile coordinates.
Returns
The index in Main.chest of the chest at (X , Y ), or -1 if one doesn't exist.

If you don't have the top-left corner of the chest tile, use FindChestByGuessing(int, int).

◆ FindChestByGuessing()

static int Chest.FindChestByGuessing ( int  X,
int  Y 
)
static

Tries to find the Chest associated with the tile at (X , Y ) (tile coordinates).

Parameters
XThe x-coordinate to check, in tile coordinates.
YThe y-coordinate to check, in tile coordinates.
Returns
The index in Main.chest of the chest associated with the tile at (X , Y ), or -1 if one doesn't exist.

If you have the top-left corner of the chest tile, use FindChest(int, int).

◆ FindEmptyChest()

static int Chest.FindEmptyChest ( int  x,
int  y,
int  type = 21,
int  style = 0,
int  direction = 1,
int  alternate = 0 
)
static

Determines if a new chest can exist at tile coordinates (x , y ).

Parameters
xThe x-coordinate to check, in tile coordinates.
yThe y-coordinate to check, in tile coordinates.
typeUnused, but required for use with PlacementHook.hook.
styleUnused, but required for use with PlacementHook.hook.
directionUnused, but required for use with PlacementHook.hook.
alternateUnused, but required for use with PlacementHook.hook.
Returns
-1 if there exists a chest in Main.chest at the coordinates (x , y ), or if Main.chest contains no null elements.
Otherwise, the index in Main.chest of the first null element.

◆ GetCurrentlyOpenChests()

static List< int > Chest.GetCurrentlyOpenChests ( )
static

Gets a list of currently-open chests in the world.
Never called on NetmodeID.MultiplayerClient.

Returns
A List<T> containing the indices in Main.chest of all chests any active Player has open.

◆ IsLocked() [1/2]

static bool Chest.IsLocked ( int  x,
int  y 
)
static

Determines if the chest at Main.tile[x , y ] is locked.

Parameters
xThe x-coordinate of the chest to check, in tile coordinates.
yThe y-coordinate of the chest to check, in tile coordinates.
Returns
true if the chest is locked, false otherwise.

◆ IsLocked() [2/2]

static bool Chest.IsLocked ( int  x,
int  y,
Tile  t 
)
static
Parameters
x
y
tThe Tile at Main.tile[x , y ]

◆ Lock()

static bool Chest.Lock ( int  X,
int  Y 
)
static

Locks an unlocked chest.

Parameters
XThe x-coordinate of the unlocked chest, in tile coordinates.
YThe y-coordinate of the unlocked chest, in tile coordinates.
Returns
true if the chest successfully locked, false otherwise.

◆ NearOtherChests()

static bool Chest.NearOtherChests ( int  x,
int  y 
)
static

Determines if there are any chests near the tile coordinates (x , y ).

Parameters
xThe x-coordinate to check, in tile coordinates.
yThe y-coordinate to check, in tile coordinates.
Returns
true if any position in Main.tile within [-25, 25) tiles horizontally and [-8, 8) tiles vertically has a tile (Tile.HasTile) and if that tile's Tile.TileType is in TileID.Sets.BasicChest. Otherwise, returns false.

◆ PutItemInNearbyChest()

static Item Chest.PutItemInNearbyChest ( Item  item,
Vector2  position 
)
static

Attempts to move the given item into any chests near the given position. Cannot place items into a chest if IsPlayerInChest(int) or if that chest IsLocked(int, int)

Parameters
itemThe item to attempt moving.
positionThe position to attempt moving from.
Returns
If on NetmodeID.MultiplayerClient, item without modification.
Otherwise, item after being quick-stacked into any nearby chests. May be Item.IsAir.

◆ ServerPlaceItem()

static void Chest.ServerPlaceItem ( int  plr,
int  slot 
)
static

Attempts to move the item in plr 's inventory or Void Vault into nearby chests.
Only called on NetmodeID.Server.

Parameters
plrThe index of the Player in Main.player to try moving an item for.
slotThe PlayerItemSlotID to move an item from.
If slot < 58, then that slot of plr 's inventory is moved.
If slot >= PlayerItemSlotID.Bank4_0 && slot < PlayerItemSlotID.Bank4_0 + 40, then that slot (minus PlayerItemSlotID.Bank4_0) of plr 's Void Vault is moved.
Otherwise, no item is moved.

It's recommended to use Player.QuickStackAllChests to handle this automatically.

◆ Unlock()

static bool Chest.Unlock ( int  X,
int  Y 
)
static

Unlocks a locked chest.

Parameters
XThe x-coordinate of the locked chest, in tile coordinates.
YThe y-coordinate of the locked chest, in tile coordinates.
Returns
true if the chest successfully unlocked, false otherwise.

◆ UsingChest()

static int Chest.UsingChest ( int  i)
static

Gets the Player currently using the given chest, if any.

Parameters
iThe index of the chest in Main.chest to check.
Returns
The index in Main.player of the Player using this chest, or -1 if no player is using this chest.

◆ VisualizeChestTransfer()

static void Chest.VisualizeChestTransfer ( Vector2  position,
Vector2  chestPosition,
Item  item,
int  amountMoved 
)
static

Visually moves item from position to chestPosition .
If called on NetmodeID.MultiplayerClient, sync the transfer to all clients.

Parameters
positionThe original position of item in world coordinates.
chestPositionThe position of the destination chest in world coordinates.
itemThe item to move. Only Item.type is read.
amountMovedUnused.

◆ VisualizeChestTransfer_CoinsBatch()

static void Chest.VisualizeChestTransfer_CoinsBatch ( Vector2  position,
Vector2  chestPosition,
long  coinsMoved 
)
static

Visually moves coinsMoved worth of coins from position to chestPosition .
If called on NetmodeID.MultiplayerClient, sync the transfer to all clients.

Parameters
positionThe original position of the coins in world coordinates.
chestPositionThe position of the destination chest in world coordinates.
coinsMovedThe value of the coins moved in copper coins.