#Script optmization / make the code cleanner

22 messages · Page 1 of 1 (latest)

cedar pine
#

Hello guys, i have this script that i made to set starting / base attributes for players. While this works, i feel that there is a much better way to do it. Can anyone recommend to me a better / cleanner way to do this?

strong obsidianBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

brazen oceanBOT
#

Paste version of start_attributes.js from @cedar pine

granite obsidian
cedar pine
granite obsidian
#

rum command is hard to debug when things go wrong

cedar pine
#

i'm trying right now to optmize the best i can from my scripts

marsh nebula
#

Also, since you are setting same things in both events, you can extract that into a function that takes an event

cedar pine
marsh nebula
brazen oceanBOT
#

Paste version of example.js from @marsh nebula

cedar pine
#

thank you both

strong obsidianBOT
#

Ticket closed!

marsh nebula
#

BTW - the way I did that put the function and the attributes constant in global scope.
This may cause problems if you run many scripts, like name clashes, as all scripts of a type run under one common global scope.
To mitigate this, you can put the script above inside of the IIFE (Immediately Invoked Function Expression):

(() => {
  // your code here...
})()
cedar pine
#

like this:

(() => {
  function setAttributeBase(event, attributeName, baseValue) {
    event.player.setAttributeBaseValue(attributeName, baseValue);
  }

  const attributes = [
    { name: "irons_spellbooks:mana_regen", value: 0.3 },
    { name: "irons_spellbooks:cooldown_reduction", value: 0.5 },
    { name: "irons_spellbooks:cast_time_reduction", value: 0.5 },
    { name: "irons_spellbooks:spell_power", value: 0.5 },
    { name: "irons_spellbooks:fire_spell_power", value: 0.5 },
    { name: "irons_spellbooks:ice_spell_power", value: 0.5 },
    { name: "irons_spellbooks:lightning_spell_power", value: 0.5 },
    { name: "irons_spellbooks:evocation_spell_power", value: 0.5 },
    { name: "irons_spellbooks:nature_spell_power", value: 0.5 },
    { name: "irons_spellbooks:holy_spell_power", value: 0.5 },
    { name: "irons_spellbooks:ender_spell_power", value: 0.5 },
    { name: "irons_spellbooks:blood_spell_power", value: 0.5 },
    { name: "irons_spellbooks:eldritch_spell_power", value: 0.5 },
    { name: "apothic_attributes:crit_damage", value: 1.1 },
    { name: "apothic_attributes:crit_chance", value: 0.05 },
    { name: "apothic_attributes:draw_speed", value: 0.5 },
    { name: "apothic_attributes:arrow_velocity", value: 0.5 },
    { name: "apothic_attributes:arrow_damage", value: 0.5 },
    { name: "minecraft:generic.attack_speed", value: 3.5 },
    { name: "minecraft:generic.attack_damage", value: 1.0 },
    { name: "minecraft:player.entity_interaction_range", value: 2.5 },
    { name: "minecraft:player.sweeping_damage_ratio", value: 0.1 },
    { name: "minecraft:generic.movement_speed", value: 0.09 },
    { name: "neoforge:swim_speed", value: 0.8 },
    { name: "combat_roll:recharge", value: 5 },
    { name: "combat_roll:count", value: 1 },
    { name: "puffish_attributes:stamina", value: 3 },
    { name: "unionlib:generic.critical_damage", value: 0 },
    { name: "unionlib:generic.critical_rate", value: 0 },
  ];

  PlayerEvents.loggedIn((event) => {
    let { server, player } = event;

    attributes.forEach((attribute) => setAttributeBase(event, attribute.name, attribute.value));

    server.runCommandSilent("attribute " + player.username + " puffish_attributes:healing modifier add starthealing -0.25 add_multiplied_total");
    server.runCommandSilent("attribute " + player.username + " puffish_attributes:sprinting_speed modifier add startsprint 0.05 add_multiplied_base");
  });

  PlayerEvents.respawned((event) => {
    let { server, player } = event;

    attributes.forEach((attribute) => setAttributeBase(event, attribute.name, attribute.value));

    server.runCommandSilent("attribute " + player.username + " puffish_attributes:healing modifier add starthealing -0.25 add_multiplied_total");
    server.runCommandSilent("attribute " + player.username + " puffish_attributes:sprinting_speed modifier add startsprint 0.05 add_multiplied_base");
  });
})();
#

?

marsh nebula
#

Yes

#

Now setAttributeBase and attributes can't be referenced from anywhere outside of that function expression

cedar pine
#

ahh nice, makes sense, as in the link you sent i've read that this way i'm creating something of a small local scope

#

nice

#

thank you again