Skip to content

Instantly share code, notes, and snippets.

@EvanHsieh0415
Created April 20, 2025 03:19
Show Gist options
  • Save EvanHsieh0415/7744f8585b234295d8145abb8ec123ee to your computer and use it in GitHub Desktop.
Save EvanHsieh0415/7744f8585b234295d8145abb8ec123ee to your computer and use it in GitHub Desktop.
/**
* @typedef {{armors: $Item_[], bonus: [mobEffect: Special.MobEffect, amplifier: number?, showParticles: boolean?]}} $ArmorSetBonus_
* @type {Object.<String, $ArmorSetBonus_}
*/
const bonuses = {
set1: {
armors: ["minecraft:leather_helmet", "minecraft:leather_boots"],
bonus: ["speed", 0],
},
set2: {
armors: ["minecraft:diamond_leggings"],
bonus: ["glowing", 0, true],
},
set3: {
armors: ["minecraft:diamond_chestplate"],
bonus: ["regeneration", 1, true],
},
};
function armor_set_bonus(/**@type {$ContextUtils$EntityEquipmentContext_}*/ context) {
const { entity, previousStack, currentStack } = context;
const { potionEffects, armorSlots } = entity;
/** @type {Special.Item[]} */
const now_set = armorSlots.toArray().map((i) => i.id);
/** @type {Special.Item[]} */
const last_set = armorSlots.toArray().map((i) => (i.id === currentStack.id ? previousStack.id : i.id));
Object.entries(bonuses).forEach((/**@type {[String, $ArmorSetBonus_]}*/ [key, data]) => {
let {
armors,
bonus: [mobEffect, amplifier, showParticles],
} = data;
// 移除旧的套装奖励
if (!armors.every((item) => last_set.includes(item))) entity.removeEffect(mobEffect);
// 添加新的套装奖励
if (armors.every((item) => now_set.includes(item)))
// -1 表示永久效果
potionEffects.add(mobEffect, -1, amplifier ?? 0, true, showParticles ?? false);
});
}
EntityJSEvents.modifyEntity((event) =>
event.modify("minecraft:player", (modifyBuilder) => {
modifyBuilder.onEquipItem((context) => armor_set_bonus(context));
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment