Skip to content

Commit

Permalink
Implemented Chest Minecarts
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerw committed Sep 12, 2014
1 parent 4019847 commit 3e74113
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/BlockEntities/BlockEntityWithItems.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/BlockEntities/ChestEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
1 change: 0 additions & 1 deletion src/BlockEntities/DispenserEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
1 change: 0 additions & 1 deletion src/BlockEntities/DropSpenserEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ cDropSpenserEntity::cDropSpenserEntity(BLOCKTYPE a_BlockType, int a_BlockX, int
m_ShouldDropSpense(false),
m_IsPowered(false)
{
SetBlockEntity(this); // cBlockEntityWindowOwner
}


Expand Down
1 change: 0 additions & 1 deletion src/BlockEntities/DropperEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
2 changes: 1 addition & 1 deletion src/BlockEntities/EnderChestEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// tolua_begin
class cEnderChestEntity :
public cBlockEntity,
public cBlockEntityWindowOwner
public cWindowOwner
{
typedef cBlockEntity super;

Expand Down
1 change: 0 additions & 1 deletion src/BlockEntities/FurnaceEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
41 changes: 33 additions & 8 deletions src/Entities/Minecart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,29 +1103,54 @@ 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)
{
m_Contents.AddListener(*this);
}





void cMinecartWithChest::SetSlot(size_t a_Idx, const cItem & a_Item)
void cMinecartWithChest::OnRightClicked(cPlayer & a_Player)
{
ASSERT(a_Idx < ARRAYCOUNT(m_Items));

m_Items[a_Idx] = a_Item;
// 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::OnRightClicked(cPlayer & a_Player)
void cMinecartWithChest::OpenNewWindow()
{
OpenWindow(new cMinecartWithChestWindow(this));
}





void cMinecartWithChest::Destroyed()
{
// Show the chest UI window to the player
// TODO
cItems Pickups;
m_Contents.CopyToItems(Pickups);
GetWorld()->SpawnItemPickups(Pickups, GetPosX(), GetPosY() + 1, GetPosZ(), 4);
}


Expand Down
40 changes: 30 additions & 10 deletions src/Entities/Minecart.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include "Entity.h"
#include "../UI/WindowOwner.h"



Expand Down Expand Up @@ -108,27 +109,46 @@ class cRideableMinecart :


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;
Expand Down
35 changes: 35 additions & 0 deletions src/UI/SlotArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:

Expand Down
14 changes: 14 additions & 0 deletions src/UI/SlotArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class cChestEntity;
class cDropSpenserEntity;
class cEnderChestEntity;
class cFurnaceEntity;
class cMinecartWithChest;
class cCraftingRecipe;
class cEnchantingWindow;
class cWorld;
Expand Down Expand Up @@ -455,3 +456,16 @@ class cSlotAreaFurnace :




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;
};
30 changes: 30 additions & 0 deletions src/UI/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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:

Expand All @@ -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));
Expand Down
15 changes: 15 additions & 0 deletions src/UI/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class cDropSpenserEntity;
class cEnderChestEntity;
class cFurnaceEntity;
class cHopperEntity;
class cMinecartWithChest;
class cBeaconEntity;
class cSlotArea;
class cSlotAreaAnvil;
Expand Down Expand Up @@ -360,6 +361,20 @@ class cChestWindow :



class cMinecartWithChestWindow :
public cWindow
{
public:
cMinecartWithChestWindow(cMinecartWithChest * a_ChestCart);
~cMinecartWithChestWindow();
private:
cMinecartWithChest * m_ChestCart;
};





class cEnderChestWindow :
public cWindow
{
Expand Down

0 comments on commit 3e74113

Please sign in to comment.