1
0
Fork 0

Properly exported cItemFrame and cHangingEntity to Lua.

This commit is contained in:
Mattes D 2014-10-21 22:02:30 +02:00
parent 881bc66233
commit a42fa071bc
10 changed files with 100 additions and 67 deletions

View File

@ -41,6 +41,8 @@ $cfile "../Entities/FireChargeEntity.h"
$cfile "../Entities/FireworkEntity.h"
$cfile "../Entities/Floater.h"
$cfile "../Entities/GhastFireballEntity.h"
$cfile "../Entities/HangingEntity.h"
$cfile "../Entities/ItemFrame.h"
$cfile "../Entities/Pawn.h"
$cfile "../Entities/Player.h"
$cfile "../Entities/Painting.h"

View File

@ -82,6 +82,8 @@ set(BINDING_DEPENDENCIES
../Entities/FireworkEntity.h
../Entities/Floater.h
../Entities/GhastFireballEntity.h
../Entities/HangingEntity.h
../Entities/ItemFrame.h
../Entities/Pawn.h
../Entities/Player.h
../Entities/Painting.h

View File

@ -9,9 +9,9 @@
cHangingEntity::cHangingEntity(eEntityType a_EntityType, eBlockFace a_BlockFace, double a_X, double a_Y, double a_Z)
: cEntity(a_EntityType, a_X, a_Y, a_Z, 0.8, 0.8)
, m_BlockFace(a_BlockFace)
cHangingEntity::cHangingEntity(eEntityType a_EntityType, eBlockFace a_Facing, double a_X, double a_Y, double a_Z) :
cEntity(a_EntityType, a_X, a_Y, a_Z, 0.8, 0.8),
m_Facing(a_Facing)
{
SetMaxHealth(1);
SetHealth(1);
@ -21,15 +21,23 @@ cHangingEntity::cHangingEntity(eEntityType a_EntityType, eBlockFace a_BlockFace,
void cHangingEntity::SetDirection(eBlockFace a_BlockFace)
void cHangingEntity::SetFacing(eBlockFace a_Facing)
{
if ((a_BlockFace < 2) || (a_BlockFace > 5))
// Y-based faces are not allowed:
switch (a_Facing)
{
ASSERT(!"Tried to set a bad direction!");
return;
case BLOCK_FACE_NONE:
case BLOCK_FACE_YM:
case BLOCK_FACE_YP:
{
LOGWARNING("%s: Invalid facing: %d. Ignoring.", __FUNCTION__, a_Facing);
ASSERT(!"Tried to set a bad facing!");
return;
}
default: break;
}
m_BlockFace = a_BlockFace;
m_Facing = a_Facing;
}
@ -41,7 +49,7 @@ void cHangingEntity::SpawnOn(cClientHandle & a_ClientHandle)
int Dir = 0;
// The client uses different values for item frame directions and block faces. Our constants are for the block faces, so we convert them here to item frame faces
switch (m_BlockFace)
switch (m_Facing)
{
case BLOCK_FACE_ZP: Dir = 0; break;
case BLOCK_FACE_ZM: Dir = 2; break;
@ -49,8 +57,8 @@ void cHangingEntity::SpawnOn(cClientHandle & a_ClientHandle)
case BLOCK_FACE_XP: Dir = 3; break;
default:
{
LOGINFO("Invalid face (%d) in a cHangingEntity at {%d, %d, %d}, adjusting to BLOCK_FACE_XP.",
m_BlockFace, (int)GetPosX(), (int)GetPosY(), (int)GetPosZ()
LOGINFO("Invalid facing (%d) in a cHangingEntity at {%d, %d, %d}, adjusting to BLOCK_FACE_XP.",
m_Facing, (int)GetPosX(), (int)GetPosY(), (int)GetPosZ()
);
Dir = 3;
}

View File

@ -11,36 +11,41 @@
class cHangingEntity :
public cEntity
{
// tolua_end
typedef cEntity super;
public:
// tolua_end
CLASS_PROTODEF(cHangingEntity)
cHangingEntity(eEntityType a_EntityType, eBlockFace a_BlockFace, double a_X, double a_Y, double a_Z);
/** Returns the orientation from the hanging entity */
eBlockFace GetDirection() const { return m_BlockFace; } // tolua_export
// tolua_begin
/** Returns the direction in which the entity is facing. */
eBlockFace GetFacing() const { return m_Facing; }
/** Set the orientation from the hanging entity */
void SetDirection(eBlockFace a_BlockFace); // tolua_export
/** Set the direction in which the entity is facing. */
void SetFacing(eBlockFace a_Facing);
/** Returns the X coord. */
int GetTileX() const { return POSX_TOINT; } // tolua_export
/** Returns the X coord of the block in which the entity resides. */
int GetBlockX() const { return POSX_TOINT; }
/** Returns the Y coord. */
int GetTileY() const { return POSY_TOINT; } // tolua_export
/** Returns the Y coord of the block in which the entity resides. */
int GetBlockY() const { return POSY_TOINT; }
/** Returns the Z coord. */
int GetTileZ() const { return POSZ_TOINT; } // tolua_export
/** Returns the Z coord of the block in which the entity resides. */
int GetBlockZ() const { return POSZ_TOINT; }
// tolua_end
private:
virtual void SpawnOn(cClientHandle & a_ClientHandle) override;
virtual void Tick(float a_Dt, cChunk & a_Chunk) override {}
eBlockFace m_BlockFace;
eBlockFace m_Facing;
}; // tolua_export

View File

@ -9,10 +9,10 @@
cItemFrame::cItemFrame(eBlockFace a_BlockFace, double a_X, double a_Y, double a_Z)
: cHangingEntity(etItemFrame, a_BlockFace, a_X, a_Y, a_Z)
, m_Item(E_BLOCK_AIR)
, m_Rotation(0)
cItemFrame::cItemFrame(eBlockFace a_BlockFace, double a_X, double a_Y, double a_Z) :
cHangingEntity(etItemFrame, a_BlockFace, a_X, a_Y, a_Z),
m_Item(E_BLOCK_AIR),
m_ItemRotation(0)
{
}
@ -27,10 +27,10 @@ void cItemFrame::OnRightClicked(cPlayer & a_Player)
if (!m_Item.IsEmpty())
{
// Item not empty, rotate, clipping values to zero to three inclusive
m_Rotation++;
if (m_Rotation >= 8)
m_ItemRotation++;
if (m_ItemRotation >= 8)
{
m_Rotation = 0;
m_ItemRotation = 0;
}
}
else if (!a_Player.GetEquippedItem().IsEmpty())
@ -72,7 +72,7 @@ void cItemFrame::KilledBy(TakeDamageInfo & a_TDI)
SetHealth(GetMaxHealth());
m_Item.Empty();
m_Rotation = 0;
m_ItemRotation = 0;
SetInvulnerableTicks(0);
GetWorld()->BroadcastEntityMetadata(*this);
}

View File

@ -11,26 +11,31 @@
class cItemFrame :
public cHangingEntity
{
// tolua_end
typedef cHangingEntity super;
public:
// tolua_end
CLASS_PROTODEF(cItemFrame)
cItemFrame(eBlockFace a_BlockFace, double a_X, double a_Y, double a_Z);
// tolua_begin
/** Returns the item in the frame */
const cItem & GetItem(void) { return m_Item; } // tolua_export
const cItem & GetItem(void) { return m_Item; }
/** Set the item in the frame */
void SetItem(cItem & a_Item) { m_Item = a_Item; } // tolua_export
void SetItem(cItem & a_Item) { m_Item = a_Item; }
/** Returns the rotation from the item in the frame */
Byte GetRotation(void) const { return m_Rotation; } // tolua_export
Byte GetItemRotation(void) const { return m_ItemRotation; }
/** Set the rotation from the item in the frame */
void SetRotation(Byte a_Rotation) { m_Rotation = a_Rotation; } // tolua_export
void SetRotation(Byte a_ItemRotation) { m_ItemRotation = a_ItemRotation; }
// tolua_end
private:
@ -39,7 +44,7 @@ private:
virtual void GetDrops(cItems & a_Items, cEntity * a_Killer) override;
cItem m_Item;
Byte m_Rotation;
Byte m_ItemRotation;
}; // tolua_export

View File

@ -2803,7 +2803,7 @@ void cProtocol172::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity)
WriteByte(0xA2);
WriteItem(Frame.GetItem());
WriteByte(0x3);
WriteByte(Frame.GetRotation() / 2);
WriteByte(Frame.GetItemRotation() / 2);
break;
}
default: break;

View File

@ -3109,7 +3109,7 @@ void cProtocol180::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity)
WriteByte(0xA8);
WriteItem(Frame.GetItem());
WriteByte(0x09);
WriteByte(Frame.GetRotation());
WriteByte(Frame.GetItemRotation());
break;
}
default: break;

View File

@ -685,21 +685,21 @@ void cNBTChunkSerializer::AddProjectileEntity(cProjectileEntity * a_Projectile)
void cNBTChunkSerializer::AddHangingEntity(cHangingEntity * a_Hanging)
{
m_Writer.AddByte("Direction", (unsigned char)a_Hanging->GetDirection());
m_Writer.AddInt("TileX", a_Hanging->GetTileX());
m_Writer.AddInt("TileY", a_Hanging->GetTileY());
m_Writer.AddInt("TileZ", a_Hanging->GetTileZ());
switch (a_Hanging->GetDirection())
m_Writer.AddInt("TileX", a_Hanging->GetBlockX());
m_Writer.AddInt("TileY", a_Hanging->GetBlockY());
m_Writer.AddInt("TileZ", a_Hanging->GetBlockZ());
switch (a_Hanging->GetFacing())
{
case BLOCK_FACE_YM: m_Writer.AddByte("Dir", (unsigned char)2); break;
case BLOCK_FACE_YP: m_Writer.AddByte("Dir", (unsigned char)1); break;
case BLOCK_FACE_ZM: m_Writer.AddByte("Dir", (unsigned char)0); break;
case BLOCK_FACE_ZP: m_Writer.AddByte("Dir", (unsigned char)3); break;
case BLOCK_FACE_XM: m_Writer.AddByte("Facing", 1); break;
case BLOCK_FACE_XP: m_Writer.AddByte("Facing", 3); break;
case BLOCK_FACE_ZM: m_Writer.AddByte("Facing", 2); break;
case BLOCK_FACE_ZP: m_Writer.AddByte("Facing", 0); break;
case BLOCK_FACE_XM:
case BLOCK_FACE_XP:
case BLOCK_FACE_YM:
case BLOCK_FACE_YP:
case BLOCK_FACE_NONE:
{
// These directions are invalid, but they may have been previously loaded, so keep them.
break;
}
}
@ -740,7 +740,7 @@ void cNBTChunkSerializer::AddItemFrameEntity(cItemFrame * a_ItemFrame)
AddBasicEntity(a_ItemFrame, "ItemFrame");
AddHangingEntity(a_ItemFrame);
AddItem(a_ItemFrame->GetItem(), -1, "Item");
m_Writer.AddByte("ItemRotation", (unsigned char)a_ItemFrame->GetRotation());
m_Writer.AddByte("ItemRotation", (unsigned char)a_ItemFrame->GetItemRotation());
m_Writer.AddFloat("ItemDropChance", 1.0F);
m_Writer.EndCompound();
}

View File

@ -1660,30 +1660,41 @@ void cWSSAnvil::LoadExpOrbFromNBT(cEntityList & a_Entities, const cParsedNBT & a
void cWSSAnvil::LoadHangingFromNBT(cHangingEntity & a_Hanging, const cParsedNBT & a_NBT, int a_TagIdx)
{
int Direction = a_NBT.FindChildByName(a_TagIdx, "Direction");
if (Direction > 0)
// "Facing" tag is the prime source of the Facing; if not available, translate from older "Direction" or "Dir"
int Facing = a_NBT.FindChildByName(a_TagIdx, "Facing");
if (Facing > 0)
{
Direction = (int)a_NBT.GetByte(Direction);
if ((Direction < 2) || (Direction > 5))
Facing = (int)a_NBT.GetByte(Facing);
if ((Facing >= 2) && (Facing <= 5))
{
a_Hanging.SetDirection(BLOCK_FACE_NORTH);
}
else
{
a_Hanging.SetDirection(static_cast<eBlockFace>(Direction));
a_Hanging.SetFacing(static_cast<eBlockFace>(Facing));
}
}
else
{
Direction = a_NBT.FindChildByName(a_TagIdx, "Dir");
if (Direction > 0)
Facing = a_NBT.FindChildByName(a_TagIdx, "Direction");
if (Facing > 0)
{
switch ((int)a_NBT.GetByte(Direction))
switch ((int)a_NBT.GetByte(Facing))
{
case 0: a_Hanging.SetDirection(BLOCK_FACE_NORTH); break;
case 1: a_Hanging.SetDirection(BLOCK_FACE_TOP); break;
case 2: a_Hanging.SetDirection(BLOCK_FACE_BOTTOM); break;
case 3: a_Hanging.SetDirection(BLOCK_FACE_SOUTH); break;
case 0: a_Hanging.SetFacing(BLOCK_FACE_ZM); break;
case 1: a_Hanging.SetFacing(BLOCK_FACE_XM); break;
case 2: a_Hanging.SetFacing(BLOCK_FACE_ZP); break;
case 3: a_Hanging.SetFacing(BLOCK_FACE_XP); break;
}
}
else
{
Facing = a_NBT.FindChildByName(a_TagIdx, "Dir"); // Has values 0 and 2 swapped
if (Facing > 0)
{
switch ((int)a_NBT.GetByte(Facing))
{
case 0: a_Hanging.SetFacing(BLOCK_FACE_ZP); break;
case 1: a_Hanging.SetFacing(BLOCK_FACE_XM); break;
case 2: a_Hanging.SetFacing(BLOCK_FACE_ZM); break;
case 3: a_Hanging.SetFacing(BLOCK_FACE_XP); break;
}
}
}
}