#Need help with crate system

1 messages · Page 1 of 1 (latest)

sonic pike
#
world.beforeEvents.playerInteractWithBlock.subscribe((e) => {
    const { player, block } = e;
    if (block.typeId == "minecraft:stone") {
        player.sendMessage("You clicked on stone")
    }
})
odd mantle
#

@hearty glacier

import { world, Player } from "@minecraft/server";

world.beforeEvents.playerInteractWithBlock.subscribe(ev => {
    const jugador = ev.player;
    const bloque = ev.block;

    if (
        !ev.isFirstEvent || 
        !(jugador instanceof Player) || 
        ev.itemStack?.typeId !== "minecraft:stick" || 
        bloque.typeId !== "minecraft:grass"
    ) return;

});
hearty glacier
#

Can you fix this I kept getting error like crate opening failed: TypeError: not a function @odd mantle

#

I Need help with my crate system

#

Do you think you can help me @odd mantle

hearty glacier
#

Yo @odd mantle can you help me

odd mantle
#

@hearty glacier Pass code

hearty glacier
#

Ok

#

This and can you make it use structure like if I get randomly got a apple it will load the structure apple above my head if you want to

#

And if you want to can you make it use a chance system so like 50percent or like 12 percent if you want you don’t have to I just if you don’t want to I just need this to work @odd mantle

#

There you go @odd mantle

unborn hazel
unborn hazel
hearty glacier
#

Bro what ok

unborn hazel
# hearty glacier This and can you make it use structure like if I get randomly got a apple it wil...
import { system, world, ItemStack } from '@minecraft/server';

const cooldowns = new Map();
const cooldownTime = 10 * 1000;

const rewards = [
  {
    name: 'Epic Sword',
    itemId: 'minecraft:diamond_sword',
    amount: 1,
    lore: ['Forged in fire'],
    nameTag: 'Epic Sword'
  },
  {
    name: 'Golden Apple',
    itemId: 'minecraft:golden_apple',
    amount: 3
  },
  {
    name: 'Diamond',
    itemId: 'minecraft:diamond',
    amount: 5
  }
];

function getRandomReward() {
  return rewards[Math.floor(Math.random() * rewards.length)];
}

world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
  const { player, block } = event;
  if (block.typeId !== 'minecraft:light_gray_shulker_box') return;

  const { container } = player.getComponent('inventory');
  let keySlot = -1;

  for (let i = 0; i < container.size; i++) {
    const item = container.getItem(i);
    if (item?.typeId === 'minecraft:trial_key') {
      keySlot = i;
      break;
    }
  }

  if (keySlot === -1) return player.sendMessage('§cYou need a Crate Key to open this!');

  const playerId = player.id;
  const currentTime = Date.now();

  if (cooldowns.has(playerId)) {
    const lastUse = cooldowns.get(playerId);
    const timePassed = currentTime - lastUse;
    if (timePassed < cooldownTime) {
      const secondsLeft = Math.ceil((cooldownTime - timePassed) / 1000);
      return player.sendMessage(`§eCrate cooling down... ${secondsLeft}s left.`);
    }
  }

  cooldowns.set(playerId, currentTime);
  const reward = getRandomReward();
  const itemStack = new ItemStack(reward?.itemId ?? 'minecraft:stick', reward?.amount ?? 1);

  if (reward?.nameTag) itemStack.nameTag = reward.nameTag;
  if (reward?.lore) itemStack.setLore(reward.lore);

  if (container.emptySlotsCount === 0) return console.error('Inventory full');
  else system.run(() => container.addItem(itemStack));

  player.sendMessage(`§aYou opened a crate and received: §b${reward.name}`);
  system.run(() => player.runCommand('playsound random.levelup @s'));

  const keyItem = container.getItem(keySlot);
  if (keyItem.amount > 1) {
    keyItem.amount--;
    system.run(() => container.setItem(keySlot, keyItem));
  } else container.setItem(keySlot, undefined);
});```
tame waveBOT
tame waveBOT
hearty glacier
#

Yes @unborn hazel

#

What is this

#

Wait to use this bot for future stuff like this