1
0
Fork 0

Implemented Chest Minecarts

This commit is contained in:
Tiger Wang 2014-09-12 23:18:02 +01:00
parent 4019847857
commit 3e74113427
15 changed files with 167 additions and 100 deletions

View File

@ -23,7 +23,7 @@ class cBlockEntityWithItems :
// tolua_end
// tolua doesn't seem to support multiple inheritance?
, public cItemGrid::cListener
, public cBlockEntityWindowOwner
, public cWindowOwner
// tolua_begin
{
typedef cBlockEntity super;

View File

@ -15,7 +15,6 @@ cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_
super(a_Type, a_BlockX, a_BlockY, a_BlockZ, ContentsWidth, ContentsHeight, a_World),
m_NumActivePlayers(0)
{
cBlockEntityWindowOwner::SetBlockEntity(this);
}

View File

@ -17,7 +17,6 @@
cDispenserEntity::cDispenserEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
super(E_BLOCK_DISPENSER, a_BlockX, a_BlockY, a_BlockZ, a_World)
{
SetBlockEntity(this); // cBlockEntityWindowOwner
}

View File

@ -19,7 +19,6 @@ cDropSpenserEntity::cDropSpenserEntity(BLOCKTYPE a_BlockType, int a_BlockX, int
m_ShouldDropSpense(false),
m_IsPowered(false)
{
SetBlockEntity(this); // cBlockEntityWindowOwner
}

View File

@ -15,7 +15,6 @@
cDropperEntity::cDropperEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
super(E_BLOCK_DROPPER, a_BlockX, a_BlockY, a_BlockZ, a_World)
{
SetBlockEntity(this); // cBlockEntityWindowOwner
}

View File

@ -12,7 +12,7 @@
// tolua_begin
class cEnderChestEntity :
public cBlockEntity,
public cBlockEntityWindowOwner
public cWindowOwner
{
typedef cBlockEntity super;

View File

@ -36,7 +36,6 @@ cFurnaceEntity::cFurnaceEntity(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTY
m_LastProgressFuel(0),
m_LastProgressCook(0)
{
cBlockEntityWindowOwner::SetBlockEntity(this);
m_Contents.AddListener(*this);
}

View File

@ -1103,19 +1103,10 @@ void cRideableMinecart::OnRightClicked(cPlayer & a_Player)
// cMinecartWithChest:
cMinecartWithChest::cMinecartWithChest(double a_X, double a_Y, double a_Z) :
super(mpChest, a_X, a_Y, a_Z)
super(mpChest, a_X, a_Y, a_Z),
m_Contents(ContentsWidth, ContentsHeight)
{
}
void cMinecartWithChest::SetSlot(size_t a_Idx, const cItem & a_Item)
{
ASSERT(a_Idx < ARRAYCOUNT(m_Items));
m_Items[a_Idx] = a_Item;
m_Contents.AddListener(*this);
}
@ -1124,8 +1115,42 @@ void cMinecartWithChest::SetSlot(size_t a_Idx, const cItem & a_Item)
void cMinecartWithChest::OnRightClicked(cPlayer & a_Player)
{
// Show the chest UI window to the player
// TODO
// If the window is not created, open it anew:
cWindow * Window = GetWindow();
if (Window == NULL)
{
OpenNewWindow();
Window = GetWindow();
}
// Open the window for the player:
if (Window != NULL)
{
if (a_Player.GetWindow() != Window)
{
a_Player.OpenWindow(Window);
}
}
}
void cMinecartWithChest::OpenNewWindow()
{
OpenWindow(new cMinecartWithChestWindow(this));
}
void cMinecartWithChest::Destroyed()
{
cItems Pickups;
m_Contents.CopyToItems(Pickups);
GetWorld()->SpawnItemPickups(Pickups, GetPosX(), GetPosY() + 1, GetPosZ(), 4);
}

View File

@ -10,6 +10,7 @@
#pragma once
#include "Entity.h"
#include "../UI/WindowOwner.h"
@ -108,27 +109,46 @@ protected:
class cMinecartWithChest :
public cMinecart
public cMinecart,
public cItemGrid::cListener,
public cWindowOwner
{
typedef cMinecart super;
public:
CLASS_PROTODEF(cMinecartWithChest)
/// Number of item slots in the chest
static const int NumSlots = 9 * 3;
cMinecartWithChest(double a_X, double a_Y, double a_Z);
enum
{
ContentsHeight = 3,
ContentsWidth = 9,
};
const cItem & GetSlot(int a_Idx) const { return m_Items[a_Idx]; }
cItem & GetSlot(int a_Idx) { return m_Items[a_Idx]; }
void SetSlot(size_t a_Idx, const cItem & a_Item);
const cItem & GetSlot(int a_Idx) const { return m_Contents.GetSlot(a_Idx); }
void SetSlot(size_t a_Idx, const cItem & a_Item) { m_Contents.SetSlot(a_Idx, a_Item); }
protected:
cItemGrid m_Contents;
void OpenNewWindow(void);
virtual void Destroyed() override;
/// The chest contents:
cItem m_Items[NumSlots];
// cItemGrid::cListener overrides:
virtual void OnSlotChanged(cItemGrid * a_Grid, int a_SlotNum)
{
UNUSED(a_SlotNum);
ASSERT(a_Grid == &m_Contents);
if (m_World != NULL)
{
if (GetWindow() != NULL)
{
GetWindow()->BroadcastWholeWindow();
}
m_World->MarkChunkDirty(GetChunkX(), GetChunkZ());
}
}
// cEntity overrides:
virtual void OnRightClicked(cPlayer & a_Player) override;

View File

@ -10,6 +10,7 @@
#include "../BlockEntities/DropSpenserEntity.h"
#include "../BlockEntities/EnderChestEntity.h"
#include "../BlockEntities/FurnaceEntity.h"
#include "../Entities/Minecart.h"
#include "../Items/ItemHandler.h"
#include "Window.h"
#include "../CraftingRecipes.h"
@ -1919,6 +1920,40 @@ void cSlotAreaFurnace::HandleSmeltItem(const cItem & a_Result, cPlayer & a_Playe
////////////////////////////////////////////////////////////////////////////////
// cSlotAreaMinecartWithChest:
cSlotAreaMinecartWithChest::cSlotAreaMinecartWithChest(cMinecartWithChest * a_Chest, cWindow & a_ParentWindow) :
cSlotArea(27, a_ParentWindow),
m_Chest(a_Chest)
{
}
const cItem * cSlotAreaMinecartWithChest::GetSlot(int a_SlotNum, cPlayer & a_Player) const
{
// a_SlotNum ranges from 0 to 26, use that to index the minecart chest entity's inventory directly:
UNUSED(a_Player);
return &(m_Chest->GetSlot(a_SlotNum));
}
void cSlotAreaMinecartWithChest::SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item)
{
UNUSED(a_Player);
m_Chest->SetSlot(a_SlotNum, a_Item);
}
////////////////////////////////////////////////////////////////////////////////
// cSlotAreaInventoryBase:

View File

@ -20,6 +20,7 @@ class cChestEntity;
class cDropSpenserEntity;
class cEnderChestEntity;
class cFurnaceEntity;
class cMinecartWithChest;
class cCraftingRecipe;
class cEnchantingWindow;
class cWorld;
@ -455,3 +456,16 @@ protected:
class cSlotAreaMinecartWithChest :
public cSlotArea
{
public:
cSlotAreaMinecartWithChest(cMinecartWithChest * a_ChestCart, cWindow & a_ParentWindow);
virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override;
virtual void SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
protected:
cMinecartWithChest * m_Chest;
};

View File

@ -14,6 +14,7 @@
#include "../BlockEntities/DropSpenserEntity.h"
#include "../BlockEntities/EnderChestEntity.h"
#include "../BlockEntities/HopperEntity.h"
#include "../Entities/Minecart.h"
#include "../Root.h"
#include "../Bindings/PluginManager.h"
@ -1047,6 +1048,34 @@ cChestWindow::~cChestWindow()
////////////////////////////////////////////////////////////////////////////////
// cMinecartWithChestWindow:
cMinecartWithChestWindow::cMinecartWithChestWindow(cMinecartWithChest * a_ChestCart) :
cWindow(wtChest, "Minecart with Chest"),
m_ChestCart(a_ChestCart)
{
m_ShouldDistributeToHotbarFirst = false;
m_SlotAreas.push_back(new cSlotAreaMinecartWithChest(a_ChestCart, *this));
m_SlotAreas.push_back(new cSlotAreaInventory(*this));
m_SlotAreas.push_back(new cSlotAreaHotBar(*this));
a_ChestCart->GetWorld()->BroadcastSoundEffect("random.chestopen", a_ChestCart->GetPosX(), a_ChestCart->GetPosY(), a_ChestCart->GetPosZ(), 1, 1);
}
cMinecartWithChestWindow::~cMinecartWithChestWindow()
{
m_ChestCart->GetWorld()->BroadcastSoundEffect("random.chestclosed", m_ChestCart->GetPosX(), m_ChestCart->GetPosY(), m_ChestCart->GetPosZ(), 1, 1);
}
////////////////////////////////////////////////////////////////////////////////
// cDropSpenserWindow:
@ -1073,6 +1102,7 @@ cEnderChestWindow::cEnderChestWindow(cEnderChestEntity * a_EnderChest) :
m_BlockY(a_EnderChest->GetPosY()),
m_BlockZ(a_EnderChest->GetPosZ())
{
m_ShouldDistributeToHotbarFirst = false;
m_SlotAreas.push_back(new cSlotAreaEnderChest(a_EnderChest, *this));
m_SlotAreas.push_back(new cSlotAreaInventory(*this));
m_SlotAreas.push_back(new cSlotAreaHotBar(*this));

View File

@ -23,6 +23,7 @@ class cDropSpenserEntity;
class cEnderChestEntity;
class cFurnaceEntity;
class cHopperEntity;
class cMinecartWithChest;
class cBeaconEntity;
class cSlotArea;
class cSlotAreaAnvil;
@ -360,6 +361,20 @@ protected:
class cMinecartWithChestWindow :
public cWindow
{
public:
cMinecartWithChestWindow(cMinecartWithChest * a_ChestCart);
~cMinecartWithChestWindow();
private:
cMinecartWithChest * m_ChestCart;
};
class cEnderChestWindow :
public cWindow
{

View File

@ -52,77 +52,10 @@ public:
{
return m_Window;
}
/// Returns the block position at which the element owning the window is
virtual void GetBlockPos(int & a_BlockX, int & a_BlockY, int & a_BlockZ) = 0;
private:
cWindow * m_Window;
} ;
/**
Window owner that is associated with a block entity (chest, furnace, ...)
*/
class cBlockEntityWindowOwner :
public cWindowOwner
{
public:
cBlockEntityWindowOwner(void) :
m_BlockEntity(NULL)
{
}
void SetBlockEntity(cBlockEntity * a_BlockEntity)
{
m_BlockEntity = a_BlockEntity;
}
virtual void GetBlockPos(int & a_BlockX, int & a_BlockY, int & a_BlockZ) override
{
a_BlockX = m_BlockEntity->GetPosX();
a_BlockY = m_BlockEntity->GetPosY();
a_BlockZ = m_BlockEntity->GetPosZ();
}
private:
cBlockEntity * m_BlockEntity;
} ;
/**
Window owner that is associated with an entity (chest minecart)
*/
class cEntityWindowOwner :
public cWindowOwner
{
public:
cEntityWindowOwner(void) :
m_Entity(NULL)
{
}
void SetEntity(cEntity * a_Entity)
{
m_Entity = a_Entity;
}
virtual void GetBlockPos(int & a_BlockX, int & a_BlockY, int & a_BlockZ) override
{
a_BlockX = (int)floor(m_Entity->GetPosX() + 0.5);
a_BlockY = (int)floor(m_Entity->GetPosY() + 0.5);
a_BlockZ = (int)floor(m_Entity->GetPosZ() + 0.5);
}
private:
cEntity * m_Entity;
} ;
};

View File

@ -738,7 +738,7 @@ void cNBTChunkSerializer::AddItemFrameEntity(cItemFrame * a_ItemFrame)
void cNBTChunkSerializer::AddMinecartChestContents(cMinecartWithChest * a_Minecart)
{
m_Writer.BeginList("Items", TAG_Compound);
for (int i = 0; i < cMinecartWithChest::NumSlots; i++)
for (int i = 0; i < cMinecartWithChest::ContentsHeight * cMinecartWithChest::ContentsWidth; i++)
{
const cItem & Item = a_Minecart->GetSlot(i);
if (Item.IsEmpty())