#Game is crashing

1 messages · Page 1 of 1 (latest)

wheat sierra
#
function replaceItems(player) {
if (!player.isValid()) return;
  try {
  const replaceableItems = ["minecraft:torch", "minecraft:soul_torch", "minecraft:lantern", "minecraft:soul_lantern"];
  const customItems = {
    "minecraft:torch": "evo:torch",
    "minecraft:soul_torch": "evo:soul_torch",
    "minecraft:lantern": "evo:lantern",
    "minecraft:soul_lantern": "evo:soul_lantern"
  };
  
  const inventoryComponent = player.getComponent("minecraft:inventory");
  if (!inventoryComponent) return;
  const inventory = inventoryComponent.container;

  for (let i = 0; i < inventory.size; i++) {
    let itemStack = inventory.getItem(i);
    if (itemStack && replaceableItems.includes(itemStack.typeId)) {
      let newItemId = customItems[itemStack.typeId];
      if (newItemId) {
        let newItemStack = new ItemStack(newItemId, itemStack.amount);
        inventory.setItem(i, newItemStack);
      }
    }
  }
} catch (error) {
  console.warn("Error:", error);
  }
}
#

When I am drop my custom torch, is crashing my game instantly

#

Why?

#

I try everything to stop it crash, what am I doing wrong??

raven wyvern
#

your function is not even called

#

and why do you need an array and an object with the same values?

wheat sierra
#

Yes because I just send the function

wheat sierra
raven wyvern
#
const itemReplaceMap = {
  "minecraft:torch": "my:torch"
}

function replaceItem(player) {
  let inv = player.getComponent("minecraft:inventory").container
  for (let i = 0; i < inv.size; i++) {
    let item = inv.getItem(i)
    if (itemReplaceMap[item?.typeId] == undefined) continue
    inv.setItem(i, new ItemStack(itemReplaceMap[item.typeId], item.amount)) 
  }
}
raven wyvern
raven wyvern
wheat sierra
#

Ja is because I am trying to stop crash

#

Here

#
system.runInterval(() => {
  addLightTag();
  glowingEntities();
  
  const players = world.getAllPlayers();
  
  players.forEach(player => {
    updateEnchantScore(player);
  });
}, 1);
#

This is what call function

#

The crash is when I drop item like torch

raven wyvern
wheat sierra
#

Ok

#

But here error if helo

#
[Scripting][error]-TypeError: cannot read property 'container' of undefined    at replaceItems (index.js:95)
    at <anonymous> (index.js:142)
    at forEach (native)
    at <anonymous> (index.js:143)

(Am host on different account, that is how I see error)

raven wyvern
#

Are your games crashing or is the function not working?

wheat sierra
#

Crash

#

Only person who drop torch

raven wyvern
wheat sierra
#
function glowingEntities() {
dimensions.forEach(d => {
let dims = world.getDimension(d);

let entities = dims.getEntities({excludeTypes: ["minecraft:item", "minecraft:xp_orb"]})
entities.forEach(entity => {
if (entity.isValid()) {
try {
if (entity.getComponent("minecraft:onfire")) {
entity.addTag("onFire")
} else {
entity.removeTag("onFire");
}
} catch {}
}
})
})
}
#
const dimensions = ["overworld", "nether", "the_end"];

const tier15 = [
  "lantern", "evo:lantern", "beacon", "conduit", "pearlescent_froglight", 
  "verdant_froglight", "ochre_froglight", "glowstone", 
  "lit_pumpkin", "lava_bucket", "campfire", "sea_lantern", "shroomlight"
];
const tier14 = ["torch", "evo:torch", "glow_berries", "end_rod"];
const tier10 = ["evo:soul_torch", "soul_torch", "evo:soul_lantern", "soul_lantern", "soul_campfire", "crying_obsidian"];
const tier7 = ["enchanting_table", "ender_chest", "glow_lichen", "redstone_torch"];
const tier6 = ["sea_pickle", "sculk_catalyst"];
const tier3 = ["magma"];
const tier1 = ["brewing_stand", "dragon_egg", "sculk_sensor"];

function addLightTag() {
  dimensions.forEach(d => {  	
    let dims = world.getDimension(d);
    let items = dims.getEntities({ type: "minecraft:item" });
    items.forEach(itemEntity => {
       if (!itemEntity.isValid()) return;
      const itemComponent = itemEntity.getComponent("item");
      let itemId = itemComponent.itemStack.typeId;
      if (itemId.startsWith("minecraft:")) {
        itemId = itemId.slice(10);
      }
      if (tier15.includes(itemId)) {
        itemEntity.addTag("lv15");
      } else if (tier14.includes(itemId)) {
        itemEntity.addTag("lv14");
      } else if (tier10.includes(itemId)) {
        itemEntity.addTag("lv10");
      } else if (tier7.includes(itemId)) {
        itemEntity.addTag("lv7");
      } else if (tier6.includes(itemId)) {
        itemEntity.addTag("lv6");
      } else if (tier3.includes(itemId)) {
        itemEntity.addTag("lv3");
      } else if (tier1.includes(itemId)) {
        itemEntity.addTag("lv1");
      }
    });
  });
}
#
function updateEnchantScore(player) {
  if (!player.isValid()) return;
  
  system.run(() => {
    const armourSlots = ["Head", "Chest", "Legs", "Feet", "Mainhand"];
    const excludedItems = ["evo:torch", "evo:soul_torch", "evo:lantern", "evo:soul_lantern"];
    let enchantScore = 0;

    let equipComponent = player.getComponent("equippable");
    if (!equipComponent) return;

    let mainhandItem = equipComponent.getEquipment("Mainhand");
    let offhandItem = equipComponent.getEquipment("Offhand");

    if (
      (mainhandItem && excludedItems.includes(mainhandItem.typeId)) ||
      (offhandItem && excludedItems.includes(offhandItem.typeId))
    ) {
      enchantScore = 0;
    } else {
      armourSlots.forEach(slot => {
        let armourItem = equipComponent.getEquipment(slot);
        if (armourItem) {
          const enchantComponent = armourItem.getComponent("enchantable");
          if (enchantComponent && enchantComponent.getEnchantments().length > 0) {
            enchantScore += 1;
          }
        }
      });
    }
    let score = world.scoreboard.getObjective("enchant");
    if (score) {
      score.setScore(player, enchantScore);
    }
  });
}
#

These functions work

#

It is only the replacement item I am have trouble with

#

I don't understand how container is undefined, that is what confusing

raven wyvern
#

send all the code via file

wheat sierra
#

Ok

#

Wrong one

#

Sorry

#

wait

#

why is not working??

#

let me reset my discord

#

Here it work, sorry

raven wyvern
#

something tells me that you just have a very large nested loop and the code takes a very long time to execute, so mine collapses the world

wheat sierra
#

There is no lag??

wheat sierra
#

What is nested loop?

raven wyvern
#

try to optimize the code or disable a couple of functions and see how it works

wheat sierra
#

Okay, I will try

raven wyvern
wheat sierra
#

But am confused, why am I getting error if it is because of loop?

#

Is saying inventory container is undefined but I check for it

raven wyvern
wheat sierra
#

Watch dog

#

Hm ok

raven wyvern
#

although I'm not sure what your long code is. But still try to turn off some functions and see how it works

wheat sierra
#

But my account that is hosting does not crash or get kick

raven wyvern
#

hmm

wheat sierra
#

Here what happen:

raven wyvern
#

maybe Mojang did something weird and made some kind of "protection" from the crash of worlds

wheat sierra
#

I drop item on other account. Then on my host account, it spam the console with the error I put before for a few seconds then stop

wheat sierra
#

I do crash

#

But here is what confuse me

#

The function is working

#

It will change torch into my custom one, but as soon as I drop it it crash

#

If it was because of too much lag, there would not be error?

#

Here is video show error.