1+ /**
2+ * Minecraft Bedrock Anti Hacked Items
3+ * @license MIT
4+ * @author Smell of curry & JaylyMC
5+ * @version 1.1.0
6+ * --------------------------------------------------------------------------
7+ * This is a anti hacked items, meaning it checks a players inventory every
8+ * tick then it tests if they have any banned items, then checks if they have
9+ * items that have hacked enchants and clears the item from inventory
10+ * --------------------------------------------------------------------------
11+ */
12+ import { MinecraftEnchantmentTypes , EnchantmentList , world , EntityInventoryComponent } from "mojang-minecraft" ;
13+
14+ function onTick ( ) {
15+ for ( const player of world . getPlayers ( ) ) {
16+ /** @type {EntityInventoryComponent } */
17+ // @ts -ignore
18+ const inventory = player . getComponent ( "minecraft:inventory" ) ;
19+ const container = inventory . container ;
20+ for ( let i = 0 ; i < container . size ; i ++ ) {
21+ const item = container . getItem ( i ) ;
22+ if ( ! item ) continue ;
23+ /** @type {EnchantmentList } */
24+ const enchantments = item . getComponent ( "enchantments" ) . enchantments ;
25+ let change = false ;
26+ for ( const Enchantment in MinecraftEnchantmentTypes ) {
27+ const ItemEnchantment = enchantments . getEnchantment ( MinecraftEnchantmentTypes [ Enchantment ] ) ;
28+ if ( ! ItemEnchantment ) continue ;
29+ const remove = ( ) => {
30+ enchantments . removeEnchantment ( ItemEnchantment . type ) ;
31+ change = true ;
32+ } ;
33+ const changeLevel = ( ) => {
34+ enchantments . removeEnchantment ( ItemEnchantment . type ) ;
35+ ItemEnchantment . level = ItemEnchantment . type . maxLevel ;
36+ enchantments . addEnchantment ( ItemEnchantment ) ;
37+ change = true ;
38+ } ;
39+ if ( enchantments . slot === 0 && ! enchantments . canAddEnchantment ( MinecraftEnchantmentTypes [ Enchantment ] ) ) remove ( ) ;
40+ else if ( ItemEnchantment . level > ItemEnchantment . type . maxLevel ) changeLevel ( ) ;
41+ }
42+ if ( ! change ) continue ;
43+ item . getComponent ( "enchantments" ) . enchantments = enchantments ;
44+ container . setItem ( i , item ) ;
45+ }
46+ }
47+ } ;
48+
49+ world . events . tick . subscribe ( onTick ) ;
0 commit comments