filler

Earth Sword

Right click to summon spikes

← Back to main

Code Block:


api.giveItem(myId, "Diamond Sword", 1,{customDisplayName: "Earth Blade", customDescription: "Right Click to raise spikes from the ground"});

const earthSwordCooldowns = {} // Tracks cooldowns per player

function onPlayerAltActionEarth(playerId) {
  const now = Date.now()
  const lastUse = earthSwordCooldowns[playerId] || 0
  const cooldown = 10000 // 10 seconds

  const held = api.getHeldItem(playerId)
  if (!held || held.name !== "Diamond Sword" || held.attributes?.customDisplayName !== "Earth Blade") return



  if (now - lastUse < cooldown) {
    const timeLeft = Math.ceil((cooldown - (now - lastUse)) / 1000)
    api.sendMessage(playerId, `🕒 Earth Sword ready in ${timeLeft}s`, { color: "red" })
    return
  }



  const facing = api.getPlayerFacingInfo(playerId)
  const dir = facing.dir

  const fx = dir[0], fz = dir[2]
  const forwardMag = Math.sqrt(fx * fx + fz * fz)
  const nx = fx / forwardMag
  const nz = fz / forwardMag

  const perpX = -nz
  const perpZ = nx

  const pos = api.getPosition(playerId)
  if (!pos) return
  const [px, py, pz] = pos

  const placedSpikes = []

  for (let i = 3; i <= 10; i++) {
    const baseX = Math.floor(px + nx * i)
    const baseZ = Math.floor(pz + nz * i)

    for (let side = -1; side <= 2; side++) {
      const spikeX = Math.floor(baseX + perpX * side)
      const spikeZ = Math.floor(baseZ + perpZ * side)

      for (let dy = 0; dy <= 1; dy++) {
        const spikeY = Math.floor(py) + dy
        const existing = api.getBlock(spikeX, spikeY, spikeZ)
        if (existing === "Air") {
          api.setBlock(spikeX, spikeY, spikeZ, "Gold Spikes")
          placedSpikes.push([spikeX, spikeY, spikeZ])
        }
      }
    }
  }

  // Save cooldown
  earthSwordCooldowns[playerId] = now

  // Remove spikes after 5 seconds
  setTimeOut(() => {
    for (const [x, y, z] of placedSpikes) {
      if (api.getBlock(x, y, z) === "Gold Spikes") {
        api.setBlock(x, y, z, "Air")
      }
    }
  }, 5000)
}
      

World Code:


//Timeout Code by Sulfrox
let ordinary_tick_function,do_next_tick_queue=new Set,timed_functions_queue=[];function tick(t){if("function"==typeof ordinary_tick_function&&ordinary_tick_function(t),do_next_tick_queue.forEach((t=>{"function"==typeof t&&t(),do_next_tick_queue.delete(t)})),timed_functions_queue.length&&timed_functions_queue[0][0]{"function"==typeof t&&do_next_tick_queue.add(t)},setTimeOut=(t,e)=>{let n=Date.now()+e;"function"==typeof t&&(timed_functions_queue.push([n,t]),timed_functions_queue.sort((([t],[e])=>t-e)))};

// Handle action logic
onPlayerAltAction = function(playerId) {
  const held = api.getHeldItem(playerId)

  if (held && held.name === "Diamond Sword" && held.attributes?.customDisplayName === "Earth Blade") {
    onPlayerAltActionEarth(playerId)
  }
}