Properly implemented enderchests

master
Tiger Wang 2014-06-29 11:36:38 +01:00
parent 3b3160d6b4
commit dde641ce83
10 changed files with 104 additions and 101 deletions

View File

@ -12,9 +12,8 @@
cEnderChestEntity::cEnderChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) : cEnderChestEntity::cEnderChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
super(E_BLOCK_ENDER_CHEST, a_BlockX, a_BlockY, a_BlockZ, ContentsWidth, ContentsHeight, a_World) super(E_BLOCK_ENDER_CHEST, a_BlockX, a_BlockY, a_BlockZ, a_World)
{ {
cBlockEntityWindowOwner::SetBlockEntity(this);
} }
@ -34,60 +33,6 @@ cEnderChestEntity::~cEnderChestEntity()
bool cEnderChestEntity::LoadFromJson(const Json::Value & a_Value)
{
m_PosX = a_Value.get("x", 0).asInt();
m_PosY = a_Value.get("y", 0).asInt();
m_PosZ = a_Value.get("z", 0).asInt();
Json::Value AllSlots = a_Value.get("Slots", 0);
int SlotIdx = 0;
for (Json::Value::iterator itr = AllSlots.begin(); itr != AllSlots.end(); ++itr)
{
cItem Item;
Item.FromJson(*itr);
SetSlot(SlotIdx, Item);
SlotIdx++;
}
return true;
}
void cEnderChestEntity::SaveToJson(Json::Value & a_Value)
{
a_Value["x"] = m_PosX;
a_Value["y"] = m_PosY;
a_Value["z"] = m_PosZ;
Json::Value AllSlots;
for (int i = m_Contents.GetNumSlots() - 1; i >= 0; i--)
{
Json::Value Slot;
m_Contents.GetSlot(i).GetJson(Slot);
AllSlots.append(Slot);
}
a_Value["Slots"] = AllSlots;
}
void cEnderChestEntity::SendTo(cClientHandle & a_Client)
{
// The chest entity doesn't need anything sent to the client when it's created / gets in the viewdistance
// All the actual handling is in the cWindow UI code that gets called when the chest is rclked
UNUSED(a_Client);
}
void cEnderChestEntity::UsedBy(cPlayer * a_Player) void cEnderChestEntity::UsedBy(cPlayer * a_Player)
{ {
// If the window is not created, open it anew: // If the window is not created, open it anew:
@ -106,21 +51,13 @@ void cEnderChestEntity::UsedBy(cPlayer * a_Player)
a_Player->OpenWindow(Window); a_Player->OpenWindow(Window);
} }
} }
// This is rather a hack
// Instead of marking the chunk as dirty upon chest contents change, we mark it dirty now
// We cannot properly detect contents change, but such a change doesn't happen without a player opening the chest first.
// The few false positives aren't much to worry about
int ChunkX, ChunkZ;
cChunkDef::BlockToChunk(m_PosX, m_PosZ, ChunkX, ChunkZ);
m_World->MarkChunkDirty(ChunkX, ChunkZ);
} }
void cEnderChestEntity::OpenNewWindow(void) void cEnderChestEntity::OpenNewWindow()
{ {
OpenWindow(new cEnderChestWindow(this)); OpenWindow(new cEnderChestWindow(this));
} }
@ -128,3 +65,33 @@ void cEnderChestEntity::OpenNewWindow(void)
void cEnderChestEntity::LoadFromJson(const Json::Value & a_Value, cItemGrid & a_Grid)
{
int SlotIdx = 0;
for (Json::Value::iterator itr = a_Value.begin(); itr != a_Value.end(); ++itr)
{
cItem Item;
Item.FromJson(*itr);
a_Grid.SetSlot(SlotIdx, Item);
SlotIdx++;
}
}
void cEnderChestEntity::SaveToJson(Json::Value & a_Value, const cItemGrid & a_Grid)
{
for (int i = 0; i < a_Grid.GetNumSlots(); i++)
{
Json::Value Slot;
a_Grid.GetSlot(i).GetJson(Slot);
a_Value.append(Slot);
}
}

View File

@ -1,20 +1,9 @@
#pragma once #pragma once
#include "BlockEntityWithItems.h" #include "BlockEntity.h"
#include "UI/WindowOwner.h"
#include "json/json.h"
namespace Json
{
class Value;
};
class cClientHandle;
class cServer;
class cNBTData;
@ -22,33 +11,28 @@ class cNBTData;
// tolua_begin // tolua_begin
class cEnderChestEntity : class cEnderChestEntity :
public cBlockEntityWithItems public cBlockEntity,
public cBlockEntityWindowOwner
{ {
typedef cBlockEntityWithItems super; typedef cBlockEntity super;
public:
enum {
ContentsHeight = 3,
ContentsWidth = 9,
} ;
public:
// tolua_end // tolua_end
/// Constructor used for normal operation cEnderChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World);
cEnderChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World);
virtual ~cEnderChestEntity(); virtual ~cEnderChestEntity();
static const char * GetClassStatic(void) { return "cEnderChestEntity"; } static const char * GetClassStatic(void) { return "cEnderChestEntity"; }
bool LoadFromJson(const Json::Value & a_Value);
// cBlockEntity overrides: // cBlockEntity overrides:
virtual void SaveToJson(Json::Value & a_Value) override;
virtual void SendTo(cClientHandle & a_Client) override;
virtual void UsedBy(cPlayer * a_Player) override; virtual void UsedBy(cPlayer * a_Player) override;
virtual void SaveToJson(Json::Value & a_Value) override { UNUSED(a_Value); }
virtual void SendTo(cClientHandle & a_Client) override { UNUSED(a_Client); }
static void LoadFromJson(const Json::Value & a_Value, cItemGrid & a_Grid);
static void SaveToJson(Json::Value & a_Value, const cItemGrid & a_Grid);
/// Opens a new chest window for this chest. Scans for neighbors to open a double chest window, if appropriate. /** Opens a new enderchest window for this enderchest */
void OpenNewWindow(void); void OpenNewWindow(void);
} ; // tolua_export } ; // tolua_export

View File

@ -8,6 +8,7 @@
#include "../World.h" #include "../World.h"
#include "../Bindings/PluginManager.h" #include "../Bindings/PluginManager.h"
#include "../BlockEntities/BlockEntity.h" #include "../BlockEntities/BlockEntity.h"
#include "../BlockEntities/EnderChestEntity.h"
#include "../GroupManager.h" #include "../GroupManager.h"
#include "../Group.h" #include "../Group.h"
#include "../Root.h" #include "../Root.h"
@ -46,6 +47,7 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName)
, m_bTouchGround(false) , m_bTouchGround(false)
, m_Stance(0.0) , m_Stance(0.0)
, m_Inventory(*this) , m_Inventory(*this)
, m_EnderChestContents(9, 3)
, m_CurrentWindow(NULL) , m_CurrentWindow(NULL)
, m_InventoryWindow(NULL) , m_InventoryWindow(NULL)
, m_Color('-') , m_Color('-')
@ -1743,6 +1745,7 @@ bool cPlayer::LoadFromDisk()
} }
m_Inventory.LoadFromJson(root["inventory"]); m_Inventory.LoadFromJson(root["inventory"]);
cEnderChestEntity::LoadFromJson(root["enderchestinventory"], m_EnderChestContents);
m_LoadedWorldName = root.get("world", "world").asString(); m_LoadedWorldName = root.get("world", "world").asString();
@ -1780,10 +1783,14 @@ bool cPlayer::SaveToDisk()
Json::Value JSON_Inventory; Json::Value JSON_Inventory;
m_Inventory.SaveToJson(JSON_Inventory); m_Inventory.SaveToJson(JSON_Inventory);
Json::Value JSON_EnderChestInventory;
cEnderChestEntity::SaveToJson(JSON_EnderChestInventory, m_EnderChestContents);
Json::Value root; Json::Value root;
root["position"] = JSON_PlayerPosition; root["position"] = JSON_PlayerPosition;
root["rotation"] = JSON_PlayerRotation; root["rotation"] = JSON_PlayerRotation;
root["inventory"] = JSON_Inventory; root["inventory"] = JSON_Inventory;
root["enderchestinventory"] = JSON_EnderChestInventory;
root["health"] = m_Health; root["health"] = m_Health;
root["xpTotal"] = m_LifetimeTotalXp; root["xpTotal"] = m_LifetimeTotalXp;
root["xpCurrent"] = m_CurrentXp; root["xpCurrent"] = m_CurrentXp;

View File

@ -124,6 +124,9 @@ public:
inline double GetStance(void) const { return GetPosY() + 1.62; } // tolua_export // TODO: Proper stance when crouching etc. inline double GetStance(void) const { return GetPosY() + 1.62; } // tolua_export // TODO: Proper stance when crouching etc.
inline cInventory & GetInventory(void) { return m_Inventory; } // tolua_export inline cInventory & GetInventory(void) { return m_Inventory; } // tolua_export
inline const cInventory & GetInventory(void) const { return m_Inventory; } inline const cInventory & GetInventory(void) const { return m_Inventory; }
/** Gets the contents of the player's associated enderchest */
cItemGrid & GetEnderChestContents(void) { return m_EnderChestContents; }
inline const cItem & GetEquippedItem(void) const { return GetInventory().GetEquippedItem(); } // tolua_export inline const cItem & GetEquippedItem(void) const { return GetInventory().GetEquippedItem(); } // tolua_export
@ -449,7 +452,13 @@ protected:
float m_LastGroundHeight; float m_LastGroundHeight;
bool m_bTouchGround; bool m_bTouchGround;
double m_Stance; double m_Stance;
/** Stores the player's inventory, consisting of crafting grid, hotbar, and main slots */
cInventory m_Inventory; cInventory m_Inventory;
/** An item grid that stores the player specific enderchest contents */
cItemGrid m_EnderChestContents;
cWindow * m_CurrentWindow; cWindow * m_CurrentWindow;
cWindow * m_InventoryWindow; cWindow * m_InventoryWindow;
@ -510,8 +519,6 @@ protected:
cStatManager m_Stats; cStatManager m_Stats;
/** Sets the speed and sends it to the client, so that they are forced to move so. */ /** Sets the speed and sends it to the client, so that they are forced to move so. */
virtual void DoSetSpeed(double a_SpeedX, double a_SpeedY, double a_SpeedZ) override; virtual void DoSetSpeed(double a_SpeedX, double a_SpeedY, double a_SpeedZ) override;

View File

@ -1342,7 +1342,7 @@ cSlotAreaEnderChest::cSlotAreaEnderChest(cEnderChestEntity * a_EnderChest, cWind
const cItem * cSlotAreaEnderChest::GetSlot(int a_SlotNum, cPlayer & a_Player) const const cItem * cSlotAreaEnderChest::GetSlot(int a_SlotNum, cPlayer & a_Player) const
{ {
// a_SlotNum ranges from 0 to 26, use that to index the chest entity's inventory directly: // a_SlotNum ranges from 0 to 26, use that to index the chest entity's inventory directly:
return &(m_EnderChest->GetSlot(a_SlotNum)); return &(a_Player.GetEnderChestContents().GetSlot(a_SlotNum));
} }
@ -1351,7 +1351,7 @@ const cItem * cSlotAreaEnderChest::GetSlot(int a_SlotNum, cPlayer & a_Player) co
void cSlotAreaEnderChest::SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) void cSlotAreaEnderChest::SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item)
{ {
m_EnderChest->SetSlot(a_SlotNum, a_Item); a_Player.GetEnderChestContents().SetSlot(a_SlotNum, a_Item);
} }

View File

@ -145,8 +145,7 @@ void cWindow::GetSlots(cPlayer & a_Player, cItems & a_Slots) const
{ {
int NumSlots = (*itr)->GetNumSlots(); int NumSlots = (*itr)->GetNumSlots();
for (int i = 0; i < NumSlots; i++) for (int i = 0; i < NumSlots; i++)
{ {
const cItem * Item = (*itr)->GetSlot(i, a_Player); const cItem * Item = (*itr)->GetSlot(i, a_Player);
if (Item == NULL) if (Item == NULL)
{ {
@ -1017,6 +1016,7 @@ cEnderChestWindow::~cEnderChestWindow()
// Send out the chest-close packet: // Send out the chest-close packet:
m_World->BroadcastBlockAction(m_BlockX, m_BlockY, m_BlockZ, 1, 0, E_BLOCK_ENDER_CHEST); m_World->BroadcastBlockAction(m_BlockX, m_BlockY, m_BlockZ, 1, 0, E_BLOCK_ENDER_CHEST);
// Play the closing sound
m_World->BroadcastSoundEffect("random.chestclosed", m_BlockX * 8, m_BlockY * 8, m_BlockZ * 8, 1, 1); m_World->BroadcastSoundEffect("random.chestclosed", m_BlockX * 8, m_BlockY * 8, m_BlockZ * 8, 1, 1);
} }

View File

@ -11,6 +11,7 @@
#include "FastNBT.h" #include "FastNBT.h"
#include "../BlockEntities/ChestEntity.h" #include "../BlockEntities/ChestEntity.h"
#include "../BlockEntities/EnderChestEntity.h"
#include "../BlockEntities/CommandBlockEntity.h" #include "../BlockEntities/CommandBlockEntity.h"
#include "../BlockEntities/DispenserEntity.h" #include "../BlockEntities/DispenserEntity.h"
#include "../BlockEntities/DropperEntity.h" #include "../BlockEntities/DropperEntity.h"
@ -189,6 +190,18 @@ void cNBTChunkSerializer::AddChestEntity(cChestEntity * a_Entity)
void cNBTChunkSerializer::AddEnderChestEntity(cEnderChestEntity * a_Entity)
{
m_Writer.BeginCompound("");
AddBasicTileEntity(a_Entity, "EnderChest");
// No need to store anything more
m_Writer.EndCompound();
}
void cNBTChunkSerializer::AddDispenserEntity(cDispenserEntity * a_Entity) void cNBTChunkSerializer::AddDispenserEntity(cDispenserEntity * a_Entity)
{ {
m_Writer.BeginCompound(""); m_Writer.BeginCompound("");
@ -820,6 +833,7 @@ void cNBTChunkSerializer::BlockEntity(cBlockEntity * a_Entity)
case E_BLOCK_CHEST: AddChestEntity ((cChestEntity *) a_Entity); break; case E_BLOCK_CHEST: AddChestEntity ((cChestEntity *) a_Entity); break;
case E_BLOCK_DISPENSER: AddDispenserEntity ((cDispenserEntity *) a_Entity); break; case E_BLOCK_DISPENSER: AddDispenserEntity ((cDispenserEntity *) a_Entity); break;
case E_BLOCK_DROPPER: AddDropperEntity ((cDropperEntity *) a_Entity); break; case E_BLOCK_DROPPER: AddDropperEntity ((cDropperEntity *) a_Entity); break;
case E_BLOCK_ENDER_CHEST: AddEnderChestEntity ((cEnderChestEntity *) a_Entity); break;
case E_BLOCK_FLOWER_POT: AddFlowerPotEntity ((cFlowerPotEntity *) a_Entity); break; case E_BLOCK_FLOWER_POT: AddFlowerPotEntity ((cFlowerPotEntity *) a_Entity); break;
case E_BLOCK_FURNACE: AddFurnaceEntity ((cFurnaceEntity *) a_Entity); break; case E_BLOCK_FURNACE: AddFurnaceEntity ((cFurnaceEntity *) a_Entity); break;
case E_BLOCK_HOPPER: AddHopperEntity ((cHopperEntity *) a_Entity); break; case E_BLOCK_HOPPER: AddHopperEntity ((cHopperEntity *) a_Entity); break;

View File

@ -21,6 +21,7 @@ class cEntity;
class cBlockEntity; class cBlockEntity;
class cBoat; class cBoat;
class cChestEntity; class cChestEntity;
class cEnderChestEntity;
class cCommandBlockEntity; class cCommandBlockEntity;
class cDispenserEntity; class cDispenserEntity;
class cDropperEntity; class cDropperEntity;
@ -93,6 +94,7 @@ protected:
// Block entities: // Block entities:
void AddBasicTileEntity(cBlockEntity * a_Entity, const char * a_EntityTypeID); void AddBasicTileEntity(cBlockEntity * a_Entity, const char * a_EntityTypeID);
void AddChestEntity (cChestEntity * a_Entity); void AddChestEntity (cChestEntity * a_Entity);
void AddEnderChestEntity(cEnderChestEntity * a_Entity);
void AddDispenserEntity(cDispenserEntity * a_Entity); void AddDispenserEntity(cDispenserEntity * a_Entity);
void AddDropperEntity (cDropperEntity * a_Entity); void AddDropperEntity (cDropperEntity * a_Entity);
void AddFurnaceEntity (cFurnaceEntity * a_Furnace); void AddFurnaceEntity (cFurnaceEntity * a_Furnace);

View File

@ -16,6 +16,7 @@
#include "../StringCompression.h" #include "../StringCompression.h"
#include "../BlockEntities/ChestEntity.h" #include "../BlockEntities/ChestEntity.h"
#include "../BlockEntities/EnderChestEntity.h"
#include "../BlockEntities/CommandBlockEntity.h" #include "../BlockEntities/CommandBlockEntity.h"
#include "../BlockEntities/DispenserEntity.h" #include "../BlockEntities/DispenserEntity.h"
#include "../BlockEntities/DropperEntity.h" #include "../BlockEntities/DropperEntity.h"
@ -583,6 +584,10 @@ void cWSSAnvil::LoadBlockEntitiesFromNBT(cBlockEntityList & a_BlockEntities, con
if (strncmp(a_NBT.GetData(sID), "Chest", a_NBT.GetDataLength(sID)) == 0) if (strncmp(a_NBT.GetData(sID), "Chest", a_NBT.GetDataLength(sID)) == 0)
{ {
LoadChestFromNBT(a_BlockEntities, a_NBT, Child); LoadChestFromNBT(a_BlockEntities, a_NBT, Child);
}
else if (strncmp(a_NBT.GetData(sID), "EnderChest", a_NBT.GetDataLength(sID)) == 0)
{
} }
else if (strncmp(a_NBT.GetData(sID), "Control", a_NBT.GetDataLength(sID)) == 0) else if (strncmp(a_NBT.GetData(sID), "Control", a_NBT.GetDataLength(sID)) == 0)
{ {
@ -762,6 +767,22 @@ void cWSSAnvil::LoadChestFromNBT(cBlockEntityList & a_BlockEntities, const cPars
void cWSSAnvil::LoadEnderChestFromNBT(cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx)
{
ASSERT(a_NBT.GetType(a_TagIdx) == TAG_Compound);
int x, y, z;
if (!GetBlockEntityNBTPos(a_NBT, a_TagIdx, x, y, z))
{
return;
}
std::auto_ptr<cEnderChestEntity> EnderChest(new cEnderChestEntity(x, y, z, m_World));
a_BlockEntities.push_back(EnderChest.release());
}
void cWSSAnvil::LoadDispenserFromNBT(cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx) void cWSSAnvil::LoadDispenserFromNBT(cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx)
{ {
ASSERT(a_NBT.GetType(a_TagIdx) == TAG_Compound); ASSERT(a_NBT.GetType(a_TagIdx) == TAG_Compound);

View File

@ -134,6 +134,7 @@ protected:
void LoadItemGridFromNBT(cItemGrid & a_ItemGrid, const cParsedNBT & a_NBT, int a_ItemsTagIdx, int s_SlotOffset = 0); void LoadItemGridFromNBT(cItemGrid & a_ItemGrid, const cParsedNBT & a_NBT, int a_ItemsTagIdx, int s_SlotOffset = 0);
void LoadChestFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); void LoadChestFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx);
void LoadEnderChestFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx);
void LoadDispenserFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); void LoadDispenserFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx);
void LoadDropperFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); void LoadDropperFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx);
void LoadFlowerPotFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); void LoadFlowerPotFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx);