filler

Explosive Sword V2

Explodes on hit

← Back to main

Code Block:


function check_item(item, item_name, custom_name, customId){
  return (
    item &&
    item.name === item_name &&
    item.attributes.customDisplayName === custom_name &&
    item.attributes.customAttributes.weaponId === customId
  );
}

api.giveItem(myId, "Gold Sword", 1, {customDisplayName: "Explosive Sword",customDescription: "Explodes enemies on hit, Give protection for tiny explosions", customAttributes: {weaponId: 1, "enchantmentTier":"Tier 5", enchantments: {"Attack Speed":1, "Damage":2}}})

function onPlayerDamagingOtherPlayerExplosive(aId, eId, damage, item){
  held = api.getHeldItem(aId)
  pos = api.getPosition(eId)
  if(check_item(held,"Gold Sword","Explosive Sword",1) && !api.getEffects(aId).includes("Explosion Cooldown")){
    api.applyEffect(aId, "Explosion Cooldown", 20000, {icon: "Red Wool"})
    createExplosion([pos[0],pos[1]-1,pos[2]], 1)
  }else{
    return
  }
}
      

World Code:




// Handle attack logic
onPlayerDamagingOtherPlayer = (attackingPlayer, damagedPlayer, damageDealt, withItem, bodyPartHit, damagerDbId)=> {
  onPlayerDamagingOtherPlayerExplosive(attackingPlayer, damagedPlayer, damageDealt, withItem)
};

function createExplosion(pos, size) {

  m = api.attemptSpawnMob('Draugr Zombie',...pos)
  api.setMobSetting(m, 'attackItemName', size==1? "RPG": "Super RPG")
  api.setMobSetting(m, 'attackRadius', 1e6)
  api.setMobSetting(m, 'attackInterval', 1e6)
  api.setMobSetting(m, 'baseWalkingSpeed', 0)
  api.setMobSetting(m, 'baseRunningSpeed', 0)
  api.setMobSetting(m, 'attackImpulse', 0)
  api.setPlayerOpacity(m, 0)

  p = api.attemptSpawnMob('Pig',pos[0], pos[1]-2, pos[2])
  api.setMobSetting(p, 'baseWalkingSpeed', 0)
  api.setMobSetting(p, 'baseRunningSpeed', 0)
  api.setMobSetting(p, 'initialHealth', 1e6)
  api.setPlayerOpacity(p, 0)

  api.applyMeleeHit(p,m,[0,0,0])

  despawnQueue.push(m,p)
  pendingDespawn = 0
}


onMobDamagingPlayer = (attackingMob, damagedPlayer, damageDealt, withItem) => {
  if (withItem === "Rocket" && api.getHeldItem(damagedPlayer).attributes.customDisplayName === "Explosive Sword"){
    return Math.floor(damageDealt * 0)
  }else if (withItem === "Rocket"){
    return damageDealt
  }
}

//thanks to the_ccccccccc for explosion code
let pendingDespawn = 1e6
let despawnQueue = []

tick = () => {
  pendingDespawn++
  if (despawnQueue.length != 0 && pendingDespawn == 3) {
    for (mob of despawnQueue) {
      api.despawnMob(mob)
      despawnQueue = despawnQueue.filter(m => m != mob)
    }
  }
}