2026-03-01 12:16:08 +08:00
# include "stdafx.h"
# include "net.minecraft.network.packet.h"
# include "net.minecraft.world.item.h"
# include "net.minecraft.world.item.enchantment.h"
# include "..\Minecraft.Client\ServerPlayer.h"
# include "EnchantItemCommand.h"
EGameCommand EnchantItemCommand : : getId ( )
{
return eGameCommand_EnchantItem ;
}
int EnchantItemCommand : : getPermissionLevel ( )
{
2026-03-03 03:04:10 +08:00
return LEVEL_GAMEMASTERS ;
2026-03-01 12:16:08 +08:00
}
2026-03-02 17:37:16 +07:00
void EnchantItemCommand : : execute ( shared_ptr < CommandSender > source , byteArray commandData )
2026-03-01 12:16:08 +08:00
{
ByteArrayInputStream bais ( commandData ) ;
DataInputStream dis ( & bais ) ;
PlayerUID uid = dis . readPlayerUID ( ) ;
int enchantmentId = dis . readInt ( ) ;
int enchantmentLevel = dis . readInt ( ) ;
bais . reset ( ) ;
2026-03-02 17:37:16 +07:00
shared_ptr < ServerPlayer > player = getPlayer ( uid ) ;
2026-03-01 12:16:08 +08:00
if ( player = = NULL ) return ;
2026-03-02 17:37:16 +07:00
shared_ptr < ItemInstance > selectedItem = player - > getSelectedItem ( ) ;
2026-03-01 12:16:08 +08:00
if ( selectedItem = = NULL ) return ;
Enchantment * e = Enchantment : : enchantments [ enchantmentId ] ;
if ( e = = NULL ) return ;
if ( ! e - > canEnchant ( selectedItem ) ) return ;
if ( enchantmentLevel < e - > getMinLevel ( ) ) enchantmentLevel = e - > getMinLevel ( ) ;
if ( enchantmentLevel > e - > getMaxLevel ( ) ) enchantmentLevel = e - > getMaxLevel ( ) ;
if ( selectedItem - > hasTag ( ) )
{
ListTag < CompoundTag > * enchantmentTags = selectedItem - > getEnchantmentTags ( ) ;
if ( enchantmentTags ! = NULL )
{
for ( int i = 0 ; i < enchantmentTags - > size ( ) ; i + + )
{
int type = enchantmentTags - > get ( i ) - > getShort ( ( wchar_t * ) ItemInstance : : TAG_ENCH_ID ) ;
if ( Enchantment : : enchantments [ type ] ! = NULL )
{
Enchantment * other = Enchantment : : enchantments [ type ] ;
if ( ! other - > isCompatibleWith ( e ) )
{
return ;
//throw new CommandException("commands.enchant.cantCombine", e.getFullname(level), other.getFullname(enchantmentTags.get(i).getShort(ItemInstance.TAG_ENCH_LEVEL)));
}
}
}
}
}
selectedItem - > enchant ( e , enchantmentLevel ) ;
//logAdminAction(source, "commands.enchant.success");
logAdminAction ( source , ChatPacket : : e_ChatCustom , L " commands.enchant.success " ) ;
}
2026-03-02 17:37:16 +07:00
shared_ptr < GameCommandPacket > EnchantItemCommand : : preparePacket ( shared_ptr < Player > player , int enchantmentId , int enchantmentLevel )
2026-03-01 12:16:08 +08:00
{
if ( player = = NULL ) return nullptr ;
ByteArrayOutputStream baos ;
DataOutputStream dos ( & baos ) ;
dos . writePlayerUID ( player - > getXuid ( ) ) ;
dos . writeInt ( enchantmentId ) ;
dos . writeInt ( enchantmentLevel ) ;
2026-03-02 17:37:16 +07:00
return shared_ptr < GameCommandPacket > ( new GameCommandPacket ( eGameCommand_EnchantItem , baos . toByteArray ( ) ) ) ;
2026-03-01 12:16:08 +08:00
}