Skip to content

Commit

Permalink
Many api fixes, add vanilla names to mob type -> string functions and…
Browse files Browse the repository at this point in the history
… mob spawner fixes.
  • Loading branch information
Howaner committed Nov 29, 2014
1 parent 648fee1 commit 2478e29
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 122 deletions.
20 changes: 14 additions & 6 deletions MCServer/Plugins/APIDump/APIDesc.lua
Expand Up @@ -1668,13 +1668,14 @@ a_Player:OpenWindow(Window);
SetCustomName = { Params = "string", Return = "", Notes = "Sets the custom name of the monster. You see the name over the monster. If you want to disable the custom name, simply set an empty string." },
IsCustomNameAlwaysVisible = { Params = "", Return = "bool", Notes = "Is the custom name of this monster always visible? If not, you only see the name when you sight the mob." },
SetCustomNameAlwaysVisible = { Params = "bool", Return = "", Notes = "Sets the custom name visiblity of this monster. If it's false, you only see the name when you sight the mob. If it's true, you always see the custom name." },
FamilyFromType = { Params = "{{cMonster#MobType|MobType}}", Return = "{{cMonster#MobFamily|MobFamily}}", Notes = "(STATIC) Returns the mob family ({{cMonster#MobFamily|mfXXX}} constants) based on the mob type ({{cMonster#MobType|mtXXX}} constants)" },
FamilyFromType = { Params = "{{Globals#MobType|MobType}}", Return = "{{cMonster#MobFamily|MobFamily}}", Notes = "(STATIC) Returns the mob family ({{cMonster#MobFamily|mfXXX}} constants) based on the mob type ({{Globals#MobType|mtXXX}} constants)" },
GetMobFamily = { Params = "", Return = "{{cMonster#MobFamily|MobFamily}}", Notes = "Returns this mob's family ({{cMonster#MobFamily|mfXXX}} constant)" },
GetMobType = { Params = "", Return = "{{cMonster#MobType|MobType}}", Notes = "Returns the type of this mob ({{cMonster#MobType|mtXXX}} constant)" },
GetMobType = { Params = "", Return = "{{Globals#MobType|MobType}}", Notes = "Returns the type of this mob ({{Globals#MobType|mtXXX}} constant)" },
GetSpawnDelay = { Params = "{{cMonster#MobFamily|MobFamily}}", Return = "number", Notes = "(STATIC) Returns the spawn delay - the number of game ticks between spawn attempts - for the specified mob family." },
MobTypeToString = { Params = "{{cMonster#MobType|MobType}}", Return = "string", Notes = "(STATIC) Returns the string representing the given mob type ({{cMonster#MobType|mtXXX}} constant), or empty string if unknown type." },
MobTypeToString = { Params = "{{Globals#MobType|MobType}}", Return = "string", Notes = "(STATIC) Returns the string representing the given mob type ({{Globals#MobType|mtXXX}} constant), or empty string if unknown type." },
MobTypeToVanillaName = { Params = "{{Globals#MobType|MobType}}", Return = "string", Notes = "(STATIC) Returns the correct vanilla name of the given mob type, or empty string if unknown type." },
MoveToPosition = { Params = "Position", Return = "", Notes = "Moves mob to the specified position" },
StringToMobType = { Params = "string", Return = "{{cMonster#MobType|MobType}}", Notes = "(STATIC) Returns the mob type ({{cMonster#MobType|mtXXX}} constant) parsed from the string type (\"creeper\"), or mtInvalidType if unrecognized." },
StringToMobType = { Params = "string", Return = "{{Globals#MobType|MobType}}", Notes = "(STATIC) Returns the mob type ({{Globals#MobType|mtXXX}} constant) parsed from the string type (\"creeper\"), or mtInvalidType if unrecognized." },
GetRelativeWalkSpeed = { Params = "", Return = "number", Notes = "Returns the relative walk speed of this mob. Standard is 1.0" },
SetRelativeWalkSpeed = { Params = "number", Return = "", Notes = "Sets the relative walk speed of this mob. Standard is 1.0" },
},
Expand Down Expand Up @@ -2571,7 +2572,7 @@ World:ForEachEntity(
return;
end
local Monster = tolua.cast(a_Entity, "cMonster"); -- Get the cMonster out of cEntity, now that we know the entity represents one.
if (Monster:GetMobType() == cMonster.mtSpider) then
if (Monster:GetMobType() == mtSpider) then
Monster:TeleportToCoords(Monster:GetPosX(), Monster:GetPosY() + 100, Monster:GetPosZ());
end
end
Expand Down Expand Up @@ -2928,7 +2929,7 @@ end
StringToDamageType = {Params = "string", Return = "{{Globals#DamageType|DamageType}}", Notes = "Converts a string representation to a {{Globals#DamageType|DamageType}} enumerated value."},
StringToDimension = {Params = "string", Return = "{{Globals#WorldDimension|Dimension}}", Notes = "Converts a string representation to a {{Globals#WorldDimension|Dimension}} enumerated value"},
StringToItem = {Params = "string, {{cItem|cItem}}", Return = "bool", Notes = "Parses the given string and sets the item; returns true if successful"},
StringToMobType = {Params = "string", Return = "{{cMonster#MobType|MobType}}", Notes = "Converts a string representation to a {{cMonster#MobType|MobType}} enumerated value"},
StringToMobType = {Params = "string", Return = "{{Globals#MobType|MobType}}", Notes = "Converts a string representation to a {{Globals#MobType|MobType}} enumerated value"},
StripColorCodes = {Params = "string", Return = "string", Notes = "Removes all control codes used by MC for colors and styles"},
TrimString = {Params = "string", Return = "string", Notes = "Trims whitespace at both ends of the string"},
md5 = {Params = "string", Return = "string", Notes = "converts a string to an md5 hash"},
Expand Down Expand Up @@ -3014,6 +3015,13 @@ end
gmXXX constants, the eGameMode_ constants are deprecated and will be removed from the API.
]],
},
MobType =
{
Include = { "^mt.*" },
TextBefore = [[
The following constants are used for the mob types.
]],
},
Weather =
{
Include = { "^eWeather_.*", "wSunny", "wRain", "wStorm", "wThunderstorm" },
Expand Down
37 changes: 37 additions & 0 deletions MCServer/Plugins/APIDump/Classes/BlockEntities.lua
Expand Up @@ -231,6 +231,43 @@ World:ForEachChestInChunk(Player:GetChunkX(), Player:GetChunkZ(),
},
}, -- cJukeboxEntity

cMobHeadEntity =
{
Desc = [[
This class represents a mob head block entity in the world.
]],
Inherits = "cBlockEntity",
Functions =
{
SetType = { Params = "eMobHeadType", Return = "", Notes = "Set the type of the mob head" },
SetRotation = { Params = "eMobHeadRotation", Return = "", Notes = "Sets the rotation of the mob head" },
SetOwner = { Params = "string", Return = "", Notes = "Set the player name for mob heads with player type" },
GetType = { Params = "", Return = "eMobHeadType", Notes = "Returns the type of the mob head" },
GetRotation = { Params = "", Return = "eMobHeadRotation", Notes = "Returns the rotation of the mob head" },
GetOwner = { Params = "", Return = "string", Notes = "Returns the player name of the mob head" },
},
}, -- cMobHeadEntity

cMobSpawnerEntity =
{
Desc = [[
This class represents a mob spawner block entity in the world.
]],
Inherits = "cBlockEntity",
Functions =
{
UpdateActiveState = { Params = "", Return = "", Notes = "Upate the active flag from the mob spawner. This function will called every 5 seconds from the Tick() function." },
ResetTimer = { Params = "", Return = "", Notes = "Sets the spawn delay to a new random value." },
SpawnEntity = { Params = "", Return = "", Notes = "Spawns the entity. This function automaticly change the spawn delay!" },
GetEntity = { Params = "", Return = "{{Globals#MobType|MobType}}", Notes = "Returns the entity type that will be spawn by this mob spawner." },
SetEntity = { Params = "{{Globals#MobType|MobType}}", Return = "", Notes = "Sets the entity type who will be spawn by this mob spawner." },
GetSpawnDelay = { Params = "", Return = "number", Notes = "Returns the spawn delay. This is the tick delay that is needed to spawn new monsters." },
SetSpawnDelay = { Params = "number", Return = "", Notes = "Sets the spawn delay." },
GetNearbyPlayersNum = { Params = "", Return = "number", Notes = "Returns the amount of the nearby players in a 16-block radius." },
GetNearbyMonsterNum = { Params = "", Return = "number", Notes = "Returns the amount of this monster type in a 8-block radius (Y: 4-block radius)." },
},
}, -- cMobSpawnerEntity

cNoteEntity =
{
Desc = [[
Expand Down
1 change: 1 addition & 0 deletions src/Bindings/AllToLua.pkg
Expand Up @@ -74,6 +74,7 @@ $cfile "../BlockEntities/JukeboxEntity.h"
$cfile "../BlockEntities/NoteEntity.h"
$cfile "../BlockEntities/SignEntity.h"
$cfile "../BlockEntities/MobHeadEntity.h"
$cfile "../BlockEntities/MobSpawnerEntity.h"
$cfile "../BlockEntities/FlowerPotEntity.h"
$cfile "../WebAdmin.h"
$cfile "../Root.h"
Expand Down
12 changes: 6 additions & 6 deletions src/BlockEntities/MobHeadEntity.h
Expand Up @@ -33,22 +33,22 @@ class cMobHeadEntity :

// tolua_begin

/** Set the Type */
/** Set the type of the mob head */
void SetType(const eMobHeadType & a_SkullType);

/** Set the Rotation */
/** Set the rotation of the mob head */
void SetRotation(eMobHeadRotation a_Rotation);

/** Set the Player Name for Mobheads with Player type */
/** Set the player name for mob heads with player type */
void SetOwner(const AString & a_Owner);

/** Get the Type */
/** Returns the type of the mob head */
eMobHeadType GetType(void) const { return m_Type; }

/** Get the Rotation */
/** Returns the rotation of the mob head */
eMobHeadRotation GetRotation(void) const { return m_Rotation; }

/** Get the setted Player Name */
/** Returns the player name of the mob head */
AString GetOwner(void) const { return m_Owner; }

// tolua_end
Expand Down
47 changes: 1 addition & 46 deletions src/BlockEntities/MobSpawnerEntity.cpp
Expand Up @@ -49,7 +49,7 @@ void cMobSpawnerEntity::UsedBy(cPlayer * a_Player)
{
a_Player->GetInventory().RemoveOneEquippedItem();
}
LOGD("Changed monster spawner at {%d, %d, %d} to type %s.", GetPosX(), GetPosY(), GetPosZ(), GetEntityName().c_str());
LOGD("Changed monster spawner at {%d, %d, %d} to type %s.", GetPosX(), GetPosY(), GetPosZ(), cMonster::MobTypeToString(MonsterType).c_str());
}
}

Expand Down Expand Up @@ -195,51 +195,6 @@ void cMobSpawnerEntity::SpawnEntity(void)



AString cMobSpawnerEntity::GetEntityName() const
{
switch (m_Entity)
{
case mtBat: return "Bat";
case mtBlaze: return "Blaze";
case mtCaveSpider: return "CaveSpider";
case mtChicken: return "Chicken";
case mtCow: return "Cow";
case mtCreeper: return "Creeper";
case mtEnderDragon: return "EnderDragon";
case mtEnderman: return "Enderman";
case mtGhast: return "Ghast";
case mtGiant: return "Giant";
case mtHorse: return "EntityHorse";
case mtIronGolem: return "VillagerGolem";
case mtMagmaCube: return "LavaSlime";
case mtMooshroom: return "MushroomCow";
case mtOcelot: return "Ozelot";
case mtPig: return "Pig";
case mtSheep: return "Sheep";
case mtSilverfish: return "Silverfish";
case mtSkeleton: return "Skeleton";
case mtSlime: return "Slime";
case mtSnowGolem: return "SnowMan";
case mtSpider: return "Spider";
case mtSquid: return "Squid";
case mtVillager: return "Villager";
case mtWitch: return "Witch";
case mtWither: return "WitherBoss";
case mtWolf: return "Wolf";
case mtZombie: return "Zombie";
case mtZombiePigman: return "PigZombie";
default:
{
ASSERT(!"Unknown monster type!");
return "Pig";
}
}
}





int cMobSpawnerEntity::GetNearbyPlayersNum(void)
{
Vector3d SpawnerPos(m_PosX + 0.5, m_PosY + 0.5, m_PosZ + 0.5);
Expand Down
10 changes: 5 additions & 5 deletions src/BlockEntities/MobSpawnerEntity.h
Expand Up @@ -35,22 +35,22 @@ class cMobSpawnerEntity :
/** Spawns the entity. This function automaticly change the spawn delay! */
void SpawnEntity(void);

/** Returns the entity type who will be spawn by this mob spawner. */
/** Returns the entity type that will be spawn by this mob spawner. */
eMonsterType GetEntity(void) const { return m_Entity; }

/** Sets the entity type who will be spawn by this mob spawner. */
void SetEntity(eMonsterType a_EntityType) { m_Entity = a_EntityType; }

/** Returns the entity name. (Required by the protocol) */
AString GetEntityName(void) const;

/** Returns the spawn delay. */
/** Returns the spawn delay. This is the tick delay that is needed to spawn new monsters. */
short GetSpawnDelay(void) const { return m_SpawnDelay; }

/** Sets the spawn delay. */
void SetSpawnDelay(short a_Delay) { m_SpawnDelay = a_Delay; }

/** Returns the amount of the nearby players in a 16-block radius. */
int GetNearbyPlayersNum(void);

/** Returns the amount of this monster type in a 8-block radius (Y: 4-block radius). */
int GetNearbyMonsterNum(eMonsterType a_EntityType);

// tolua_end
Expand Down
113 changes: 61 additions & 52 deletions src/Mobs/Monster.cpp
Expand Up @@ -26,36 +26,37 @@ static const struct
{
eMonsterType m_Type;
const char * m_lcName;
const char * m_VanillaName;
} g_MobTypeNames[] =
{
{mtBat, "bat"},
{mtBlaze, "blaze"},
{mtCaveSpider, "cavespider"},
{mtChicken, "chicken"},
{mtCow, "cow"},
{mtCreeper, "creeper"},
{mtEnderman, "enderman"},
{mtEnderDragon, "enderdragon"},
{mtGhast, "ghast"},
{mtHorse, "horse"},
{mtIronGolem, "irongolem"},
{mtMagmaCube, "magmacube"},
{mtMooshroom, "mooshroom"},
{mtOcelot, "ocelot"},
{mtPig, "pig"},
{mtSheep, "sheep"},
{mtSilverfish, "silverfish"},
{mtSkeleton, "skeleton"},
{mtSlime, "slime"},
{mtSnowGolem, "snowgolem"},
{mtSpider, "spider"},
{mtSquid, "squid"},
{mtVillager, "villager"},
{mtWitch, "witch"},
{mtWither, "wither"},
{mtWolf, "wolf"},
{mtZombie, "zombie"},
{mtZombiePigman, "zombiepigman"},
{mtBat, "bat", "Bat"},
{mtBlaze, "blaze", "Blaze"},
{mtCaveSpider, "cavespider", "CaveSpider"},
{mtChicken, "chicken", "Chicken"},
{mtCow, "cow", "Cow"},
{mtCreeper, "creeper", "Creeper"},
{mtEnderman, "enderman", "Enderman"},
{mtEnderDragon, "enderdragon", "EnderDragon"},
{mtGhast, "ghast", "Ghast"},
{mtHorse, "horse", "EntityHorse"},
{mtIronGolem, "irongolem", "VillagerGolem"},
{mtMagmaCube, "magmacube", "LavaSlime"},
{mtMooshroom, "mooshroom", "MushroomCow"},
{mtOcelot, "ocelot", "Ozelot"},
{mtPig, "pig", "Pig"},
{mtSheep, "sheep", "Sheep"},
{mtSilverfish, "silverfish", "Silverfish"},
{mtSkeleton, "skeleton", "Skeleton"},
{mtSlime, "slime", "Slime"},
{mtSnowGolem, "snowgolem", "SnowMan"},
{mtSpider, "spider", "Spider"},
{mtSquid, "squid", "Squid"},
{mtVillager, "villager", "Villager"},
{mtWitch, "witch", "Witch"},
{mtWither, "wither", "WitherBoss"},
{mtWolf, "wolf", "Wolf"},
{mtZombie, "zombie", "Zombie"},
{mtZombiePigman, "zombiepigman", "PigZombie"},
} ;


Expand Down Expand Up @@ -784,39 +785,47 @@ AString cMonster::MobTypeToString(eMonsterType a_MobType)



eMonsterType cMonster::StringToMobType(const AString & a_Name)
AString cMonster::MobTypeToVanillaName(eMonsterType a_MobType)
{
AString lcName = StrToLower(a_Name);

// Binary-search for the lowercase name:
int lo = 0, hi = ARRAYCOUNT(g_MobTypeNames) - 1;
while (hi - lo > 1)
// Mob types aren't sorted, so we need to search linearly:
for (size_t i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
{
int mid = (lo + hi) / 2;
int res = strcmp(g_MobTypeNames[mid].m_lcName, lcName.c_str());
if (res == 0)
{
return g_MobTypeNames[mid].m_Type;
}
if (res < 0)
{
lo = mid;
}
else
if (g_MobTypeNames[i].m_Type == a_MobType)
{
hi = mid;
return g_MobTypeNames[i].m_VanillaName;
}
}
// Range has collapsed to at most two elements, compare each:
if (strcmp(g_MobTypeNames[lo].m_lcName, lcName.c_str()) == 0)

// Not found:
return "";
}





eMonsterType cMonster::StringToMobType(const AString & a_Name)
{
AString lcName = StrToLower(a_Name);

// Search MCServer name:
for (size_t i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
{
return g_MobTypeNames[lo].m_Type;
if (strcmp(g_MobTypeNames[i].m_lcName, lcName.c_str()) == 0)
{
return g_MobTypeNames[i].m_Type;
}
}
if ((lo != hi) && (strcmp(g_MobTypeNames[hi].m_lcName, lcName.c_str()) == 0))

// Not found. Search Vanilla name:
for (size_t i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
{
return g_MobTypeNames[hi].m_Type;
if (strcmp(StrToLower(g_MobTypeNames[i].m_VanillaName).c_str(), lcName.c_str()) == 0)
{
return g_MobTypeNames[i].m_Type;
}
}

// Not found:
return mtInvalidType;
}
Expand Down
11 changes: 7 additions & 4 deletions src/Mobs/Monster.h
Expand Up @@ -133,16 +133,19 @@ class cMonster :
If it's false, you only see the name when you sight the mob. If it's true, you always see the custom name. */
void SetCustomNameAlwaysVisible(bool a_CustomNameAlwaysVisible);

/// Translates MobType enum to a string, empty string if unknown
/** Translates MobType enum to a string, empty string if unknown */
static AString MobTypeToString(eMonsterType a_MobType);

/// Translates MobType string to the enum, mtInvalidType if not recognized
/** Translates MobType enum to the correct vanilla name of the mob, empty string if unknown. */
static AString MobTypeToVanillaName(eMonsterType a_MobType);

/** Translates MobType string to the enum, mtInvalidType if not recognized */
static eMonsterType StringToMobType(const AString & a_MobTypeName);

/// Returns the mob family based on the type
/** Returns the mob family based on the type */
static eFamily FamilyFromType(eMonsterType a_MobType);

/// Returns the spawn delay (number of game ticks between spawn attempts) for the given mob family
/** Returns the spawn delay (number of game ticks between spawn attempts) for the given mob family */
static int GetSpawnDelay(cMonster::eFamily a_MobFamily);

// tolua_end
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Protocol17x.cpp
Expand Up @@ -2670,7 +2670,7 @@ void cProtocol172::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt
Writer.AddInt("x", MobSpawnerEntity.GetPosX());
Writer.AddInt("y", MobSpawnerEntity.GetPosY());
Writer.AddInt("z", MobSpawnerEntity.GetPosZ());
Writer.AddString("EntityId", MobSpawnerEntity.GetEntityName());
Writer.AddString("EntityId", cMonster::MobTypeToVanillaName(MobSpawnerEntity.GetEntity()));
Writer.AddShort("Delay", MobSpawnerEntity.GetSpawnDelay());
Writer.AddString("id", "MobSpawner");
break;
Expand Down
1 change: 1 addition & 0 deletions src/Protocol/Protocol17x.h
Expand Up @@ -33,6 +33,7 @@ Declares the 1.7.x protocol classes:

#include "PolarSSL++/AesCfb128Decryptor.h"
#include "PolarSSL++/AesCfb128Encryptor.h"
#include "../Mobs/MonsterTypes.h"



Expand Down

0 comments on commit 2478e29

Please sign in to comment.