Skip to content

Commit

Permalink
Merge pull request fador#197 from fredreichbier/mobinteraction
Browse files Browse the repository at this point in the history
Mobinteraction
  • Loading branch information
fador committed Apr 7, 2012
2 parents 9beda38 + ef363ba commit 328cd4a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 38 deletions.
4 changes: 2 additions & 2 deletions plugins/commands/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ bool isValidItem(int id)
return true;
}

if (id > 355) // high items are invalid
if (id > 383) // high items are invalid
{
return false;
}
Expand Down Expand Up @@ -896,4 +896,4 @@ std::deque<std::string> parseCmd(std::string cmd)
}

return temp;
}
}
43 changes: 33 additions & 10 deletions plugins/passiveMobs/passiveMobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,32 +365,54 @@ void gotAttacked(const char* userIn,int mobID)

if (mobHealth <= 0) return;

mobHealth -= defaultDamage(atk_item);

if (mobHealth <= 0)
{
drop(mobID);
}
mineserver->mob.setHealth((int)mobID, (int)mobHealth);
}

void interact(const char* userIn,int mobID)
{
// Certain mobs drop something when the user interacts with them.
// Sheep drop wool.
std::string user(userIn);
int atk_item, _meta, _quant;
mineserver->user.getItemInHand(userIn, &atk_item, &_meta, &_quant);
int mobHealth = mineserver->mob.getHealth((int)mobID);

if (mobHealth <= 0) return;

int type = mineserver->mob.getType(mobID);
double x, y, z; int w;
mineserver->mob.getMobPositionW(mobID, &x, &y, &z, &w);
if (type == MOB_SHEEP)
{
// On unsheared sheeps, use a shear to obtain 1-3 wool blocks.
int8_t meta = mineserver->mob.getByteMetadata(mobID, 16);
if (!(meta & 0x10))
if (atk_item == ITEM_SHEARS && !(meta & 0x10))
{
size_t amount = rand() % 3 + 1;
mineserver->map.createPickupSpawn((int)floor(x),(int)floor(y),(int)floor(z),
BLOCK_WOOL, 1, meta, NULL);
BLOCK_WOOL, amount, meta, NULL);
meta |= 0x10;
mineserver->mob.setByteMetadata(mobID, 16, meta);
mineserver->mob.updateMetadata(mobID);
}
}

mobHealth -= defaultDamage(atk_item);

if (mobHealth <= 0)
} else if(type == MOB_COW)
{
drop(mobID);
if(atk_item == ITEM_BUCKET)
{
// give him milk, take the bucket!
_quant--;
mineserver->user.setItemInHand(userIn, atk_item, _meta, _quant);
mineserver->user.addItem(userIn, ITEM_MILK_BUCKET, 1, 0);
}
}
mineserver->mob.setHealth((int)mobID, (int)mobHealth);
}


std::string pluginName = "passiveMobs";

PLUGIN_API_EXPORT void CALLCONVERSION passiveMobs_init(mineserver_pointer_struct* mineserver_temp)
Expand All @@ -414,6 +436,7 @@ PLUGIN_API_EXPORT void CALLCONVERSION passiveMobs_init(mineserver_pointer_struct
}
mineserver->plugin.addCallback("Timer200", reinterpret_cast<voidF>(timer200Function));
mineserver->plugin.addCallback("gotAttacked", reinterpret_cast<voidF>(gotAttacked));
mineserver->plugin.addCallback("interact", reinterpret_cast<voidF>(interact));
}

PLUGIN_API_EXPORT void CALLCONVERSION passiveMobs_shutdown(void)
Expand Down
2 changes: 1 addition & 1 deletion src/constants_num.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ enum
ITEM_CLAY_BALLS, ITEM_REED, ITEM_PAPER, ITEM_BOOK, ITEM_SLIME_BALL,
ITEM_STORAGE_MINECART, ITEM_POWERED_MINECART, ITEM_EGG, ITEM_COMPASS, ITEM_FISHING_ROD,
ITEM_WATCH, ITEM_GLOWSTONE_DUST, ITEM_RAW_FISH, ITEM_COOKED_FISH, ITEM_DYE,
ITEM_BONE, ITEM_SUGAR, ITEM_CAKE, ITEM_BED
ITEM_BONE, ITEM_SUGAR, ITEM_CAKE, ITEM_BED, ITEM_SHEARS = 359
};

// Records
Expand Down
65 changes: 40 additions & 25 deletions src/packets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1280,10 +1280,10 @@ int PacketHandler::disconnect(User* user)
int PacketHandler::use_entity(User* user)
{
int32_t userID, target;
int8_t targetType;
int8_t leftClick;


user->buffer >> userID >> target >> targetType;
user->buffer >> userID >> target >> leftClick;

if (!user->buffer)
{
Expand All @@ -1292,9 +1292,20 @@ int PacketHandler::use_entity(User* user)

user->buffer.removePacket();

if (targetType != 1)
if (!leftClick)
{
// right clicks: interaction, attaching, ...
for (size_t i = 0; i < Mineserver::get()->mobs()->getMobCount(); i++)
{
if (Mineserver::get()->mobs()->getMobByID(i)->UID == (uint32_t)target)
{
(static_cast<Hook2<bool, const char* ,int32_t>*>(Mineserver::get()->plugin()->getHook("interact")))->doAll(user->nick.c_str(), (int32_t)Mineserver::get()->mobs()->getMobByTarget(target));
//make a callback
return PACKET_OK;
}
}

// No? Try to attach.
Packet pkt;
//Attach
if (user->attachedTo == 0)
Expand All @@ -1310,37 +1321,41 @@ int PacketHandler::use_entity(User* user)
}
user->sendAll(pkt);
return PACKET_OK;
}

if (Mineserver::get()->m_pvp_enabled)
}
else
{
//This is used when punching users, mobs or other entities
for (std::set<User*>::const_iterator it = Mineserver::get()->users().begin(); it != Mineserver::get()->users().end(); ++it)
// left clicks: fighhht!
if (Mineserver::get()->m_pvp_enabled)
{
if ((*it)->UID == (uint32_t)target)
//This is used when punching users, mobs or other entities
for (std::set<User*>::const_iterator it = Mineserver::get()->users().begin(); it != Mineserver::get()->users().end(); ++it)
{
(*it)->health--;
(*it)->sethealth((*it)->health);

if ((*it)->health <= 0)
if ((*it)->UID == (uint32_t)target)
{
Packet pkt;
pkt << PACKET_DEATH_ANIMATION << (int32_t)(*it)->UID << (int8_t)3;
(*it)->sendOthers(pkt);
(*it)->health--;
(*it)->sethealth((*it)->health);

if ((*it)->health <= 0)
{
Packet pkt;
pkt << PACKET_DEATH_ANIMATION << (int32_t)(*it)->UID << (int8_t)3;
(*it)->sendOthers(pkt);
}
break;
}
break;
}
}
}
for (size_t i = 0; i < Mineserver::get()->mobs()->getMobCount(); i++)
{
if (Mineserver::get()->mobs()->getMobByID(i)->UID == (uint32_t)target)
for (size_t i = 0; i < Mineserver::get()->mobs()->getMobCount(); i++)
{
//int h = Mineserver::get()->mobs()->getMobByID(i)->health - 1;
//Mineserver::get()->mobs()->getMobByID(i)->sethealth(h);
(static_cast<Hook2<bool, const char* ,int32_t>*>(Mineserver::get()->plugin()->getHook("gotAttacked")))->doAll(user->nick.c_str(),(int32_t)Mineserver::get()->mobs()->getMobByTarget(target));
//make a callback
break;
if (Mineserver::get()->mobs()->getMobByID(i)->UID == (uint32_t)target)
{
//int h = Mineserver::get()->mobs()->getMobByID(i)->health - 1;
//Mineserver::get()->mobs()->getMobByID(i)->sethealth(h);
(static_cast<Hook2<bool, const char* ,int32_t>*>(Mineserver::get()->plugin()->getHook("gotAttacked")))->doAll(user->nick.c_str(),(int32_t)Mineserver::get()->mobs()->getMobByTarget(target));
//make a callback
break;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Plugin::Plugin()
setHook("PlayerChatCommand", new Hook4<bool, const char*, const char*, int, const char**>);
setHook("PlayerRespawn", new Hook1<bool, const char*>);
setHook("gotAttacked", new Hook2<bool, const char*, int32_t>);
setHook("interact", new Hook2<bool, const char*, int32_t>);

init();
}
Expand Down

0 comments on commit 328cd4a

Please sign in to comment.