aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/GameRules/AddEnchantmentRuleDefinition.cpp
diff options
context:
space:
mode:
authordaoge_cmd <3523206925@qq.com>2026-03-01 12:16:08 +0800
committerdaoge_cmd <3523206925@qq.com>2026-03-01 12:16:08 +0800
commitb691c43c44ff180d10e7d4a9afc83b98551ff586 (patch)
tree3e9849222cbc6ba49f2f1fc6e5fe7179632c7390 /Minecraft.Client/Common/GameRules/AddEnchantmentRuleDefinition.cpp
parentdef8cb415354ac390b7e89052a50605285f1aca9 (diff)
Initial commit
Diffstat (limited to 'Minecraft.Client/Common/GameRules/AddEnchantmentRuleDefinition.cpp')
-rw-r--r--Minecraft.Client/Common/GameRules/AddEnchantmentRuleDefinition.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/Minecraft.Client/Common/GameRules/AddEnchantmentRuleDefinition.cpp b/Minecraft.Client/Common/GameRules/AddEnchantmentRuleDefinition.cpp
new file mode 100644
index 00000000..eabc1401
--- /dev/null
+++ b/Minecraft.Client/Common/GameRules/AddEnchantmentRuleDefinition.cpp
@@ -0,0 +1,70 @@
+#include "stdafx.h"
+#include "..\..\..\Minecraft.World\StringHelpers.h"
+#include "..\..\..\Minecraft.World\net.minecraft.world.item.h"
+#include "..\..\..\Minecraft.World\net.minecraft.world.item.enchantment.h"
+#include "AddEnchantmentRuleDefinition.h"
+
+AddEnchantmentRuleDefinition::AddEnchantmentRuleDefinition()
+{
+ m_enchantmentId = m_enchantmentLevel = 0;
+}
+
+void AddEnchantmentRuleDefinition::writeAttributes(DataOutputStream *dos, UINT numAttributes)
+{
+ GameRuleDefinition::writeAttributes(dos, numAttributes + 2);
+
+ ConsoleGameRules::write(dos, ConsoleGameRules::eGameRuleAttr_enchantmentId);
+ dos->writeUTF( _toString( m_enchantmentId ) );
+
+ ConsoleGameRules::write(dos, ConsoleGameRules::eGameRuleAttr_enchantmentLevel);
+ dos->writeUTF( _toString( m_enchantmentLevel ) );
+}
+
+void AddEnchantmentRuleDefinition::addAttribute(const wstring &attributeName, const wstring &attributeValue)
+{
+ if(attributeName.compare(L"enchantmentId") == 0)
+ {
+ int value = _fromString<int>(attributeValue);
+ if(value < 0) value = 0;
+ if(value >= 256) value = 255;
+ m_enchantmentId = value;
+ app.DebugPrintf("AddEnchantmentRuleDefinition: Adding parameter enchantmentId=%d\n",m_enchantmentId);
+ }
+ else if(attributeName.compare(L"enchantmentLevel") == 0)
+ {
+ int value = _fromString<int>(attributeValue);
+ if(value < 0) value = 0;
+ m_enchantmentLevel = value;
+ app.DebugPrintf("AddEnchantmentRuleDefinition: Adding parameter enchantmentLevel=%d\n",m_enchantmentLevel);
+ }
+ else
+ {
+ GameRuleDefinition::addAttribute(attributeName, attributeValue);
+ }
+}
+
+bool AddEnchantmentRuleDefinition::enchantItem(shared_ptr<ItemInstance> item)
+{
+ bool enchanted = false;
+ if (item != NULL)
+ {
+ // 4J-JEV: Ripped code from enchantmenthelpers
+ // Maybe we want to add an addEnchantment method to EnchantmentHelpers
+ if (item->id == Item::enchantedBook_Id)
+ {
+ Item::enchantedBook->addEnchantment( item, new EnchantmentInstance(m_enchantmentId, m_enchantmentLevel) );
+ }
+ else if (item->isEnchantable())
+ {
+ Enchantment *e = Enchantment::enchantments[m_enchantmentId];
+
+ if(e != NULL && e->category->canEnchant(item->getItem()))
+ {
+ int level = min(e->getMaxLevel(), m_enchantmentLevel);
+ item->enchant(e, m_enchantmentLevel);
+ enchanted = true;
+ }
+ }
+ }
+ return enchanted;
+} \ No newline at end of file