From d2b3011c05890652b0aa09f6e128004056a98258 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sun, 2 Jun 2013 21:44:24 +0000 Subject: Added the possibility to specify enchantments by name in the constructor and AddFromString() function git-svn-id: http://mc-server.googlecode.com/svn/trunk@1542 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- MCServer/Plugins/Debuggers/Debuggers.lua | 2 ++ source/Bindings.cpp | 34 ++++++++++++++++++++- source/Bindings.h | 2 +- source/Enchantments.cpp | 51 +++++++++++++++++++++++++++++++- source/Enchantments.h | 7 +++-- 5 files changed, 91 insertions(+), 5 deletions(-) diff --git a/MCServer/Plugins/Debuggers/Debuggers.lua b/MCServer/Plugins/Debuggers/Debuggers.lua index 7c20e90e6..a9e834361 100644 --- a/MCServer/Plugins/Debuggers/Debuggers.lua +++ b/MCServer/Plugins/Debuggers/Debuggers.lua @@ -600,10 +600,12 @@ function HandleTestWndCmd(a_Split, a_Player) local Item4 = cItem(E_ITEM_DIAMOND_PICKAXE); Item4.m_Enchantments:SetLevel(cEnchantments.enchUnbreaking, 5); Item4.m_Enchantments:SetLevel(cEnchantments.enchEfficiency, 3); + local Item5 = cItem(E_ITEM_DIAMOND_CHESTPLATE, 1, 0, "thorns=1;unbreaking=3"); Window:SetSlot(a_Player, 0, cItem(E_ITEM_DIAMOND, 64)); Window:SetSlot(a_Player, 1, Item2); Window:SetSlot(a_Player, 2, Item3); Window:SetSlot(a_Player, 3, Item4); + Window:SetSlot(a_Player, 4, Item5); Window:SetOnClosing(OnClosing); Window:SetOnSlotChanged(OnSlotChanged); diff --git a/source/Bindings.cpp b/source/Bindings.cpp index 50f417cc1..f4d24529d 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 06/02/13 12:36:50. +** Generated automatically by tolua++-1.0.92 on 06/02/13 23:42:02. */ #ifndef __cplusplus @@ -14145,6 +14145,37 @@ static int tolua_AllToLua_cEnchantments_IsEmpty00(lua_State* tolua_S) } #endif //#ifndef TOLUA_DISABLE +/* method: StringToEnchantmentID of class cEnchantments */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_cEnchantments_StringToEnchantmentID00 +static int tolua_AllToLua_cEnchantments_StringToEnchantmentID00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"cEnchantments",0,&tolua_err) || + !tolua_iscppstring(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const AString a_EnchantmentName = ((const AString) tolua_tocppstring(tolua_S,2,0)); + { + int tolua_ret = (int) cEnchantments::StringToEnchantmentID(a_EnchantmentName); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + tolua_pushcppstring(tolua_S,(const char*)a_EnchantmentName); + } + } + return 2; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'StringToEnchantmentID'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + /* method: new of class cItem */ #ifndef TOLUA_DISABLE_tolua_AllToLua_cItem_new00 static int tolua_AllToLua_cItem_new00(lua_State* tolua_S) @@ -27701,6 +27732,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"SetLevel",tolua_AllToLua_cEnchantments_SetLevel00); tolua_function(tolua_S,"Clear",tolua_AllToLua_cEnchantments_Clear00); tolua_function(tolua_S,"IsEmpty",tolua_AllToLua_cEnchantments_IsEmpty00); + tolua_function(tolua_S,"StringToEnchantmentID",tolua_AllToLua_cEnchantments_StringToEnchantmentID00); tolua_endmodule(tolua_S); #ifdef __cplusplus tolua_cclass(tolua_S,"cItem","cItem","",tolua_collect_cItem); diff --git a/source/Bindings.h b/source/Bindings.h index 1c7aa7261..3c6433f8c 100644 --- a/source/Bindings.h +++ b/source/Bindings.h @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 06/02/13 12:36:50. +** Generated automatically by tolua++-1.0.92 on 06/02/13 23:42:02. */ /* Exported function */ diff --git a/source/Enchantments.cpp b/source/Enchantments.cpp index d0bf8c731..4802db3f7 100644 --- a/source/Enchantments.cpp +++ b/source/Enchantments.cpp @@ -51,9 +51,13 @@ void cEnchantments::AddFromString(const AString & a_StringSpec) continue; } int id = atoi(Split[0].c_str()); + if ((id == 0) && (Split[0] != "0")) + { + id = StringToEnchantmentID(Split[0]); + } int lvl = atoi(Split[1].c_str()); if ( - ((id == 0) && (Split[0] != "0")) || + ((id <= 0) && (Split[0] != "0")) || ((lvl == 0) && (Split[1] != "0")) ) { @@ -145,6 +149,51 @@ bool cEnchantments::IsEmpty(void) const +int cEnchantments::StringToEnchantmentID(const AString & a_EnchantmentName) +{ + struct + { + int m_Value; + const char * m_Name; + } EnchantmentNames[] = + { + { enchProtection, "Protection"}, + { enchFireProtection, "FireProtection"}, + { enchFeatherFalling, "FeatherFalling"}, + { enchBlastProtection, "BlastProtection"}, + { enchProjectileProtection, "ProjectileProtection"}, + { enchRespiration, "Respiration"}, + { enchAquaAffinity, "AquaAffinity"}, + { enchThorns, "Thorns"}, + { enchSharpness, "Sharpness"}, + { enchSmite, "Smite"}, + { enchBaneOfArthropods, "BaneOfArthropods"}, + { enchKnockback, "Knockback"}, + { enchFireAspect, "FireAspect"}, + { enchLooting, "Looting"}, + { enchEfficiency, "Efficiency"}, + { enchSilkTouch, "SilkTouch"}, + { enchUnbreaking, "Unbreaking"}, + { enchFortune, "Fortune"}, + { enchPower, "Power"}, + { enchPunch, "Punch"}, + { enchFlame, "Flame"}, + { enchInfinity, "Infinity"}, + } ; + for (int i = 0; i < ARRAYCOUNT(EnchantmentNames); i++) + { + if (NoCaseCompare(EnchantmentNames[i].m_Name, a_EnchantmentName) == 0) + { + return EnchantmentNames[i].m_Value; + } + } // for i - EnchantmentNames[] + return -1; +} + + + + + void cEnchantments::WriteToNBTCompound(cFastNBTWriter & a_Writer, const AString & a_ListTagName) const { // Write the enchantments into the specified NBT writer diff --git a/source/Enchantments.h b/source/Enchantments.h index 96ebc1880..002626413 100644 --- a/source/Enchantments.h +++ b/source/Enchantments.h @@ -26,9 +26,9 @@ class cParsedNBT; /** Class that stores item enchantments or stored-enchantments The enchantments may be serialized to a stringspec and read back from such stringspec. The format for the stringspec is "id=lvl;id=lvl;id=lvl...", with an optional semicolon at the end, -mapping each enchantment's id onto its level. +mapping each enchantment's id onto its level. ID may be either a number or the enchantment name. Level value of 0 means no such enchantment, and it will not be stored in the m_Enchantments. -Serialization will never put zero-level enchantments into the stringspec. +Serialization will never put zero-level enchantments into the stringspec and will always use numeric IDs. */ class cEnchantments { @@ -84,6 +84,9 @@ public: /// Returns true if there are no enchantments bool IsEmpty(void) const; + /// Converts enchantment name to the numeric representation; returns -1 if enchantment name not found; case insensitive + static int StringToEnchantmentID(const AString & a_EnchantmentName); + // tolua_end /// Writes the enchantments into the specified NBT writer; begins with the LIST tag of the specified name ("ench" or "StoredEnchantments") -- cgit v1.2.3