#Script optmization / make the code cleanner
22 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
Paste version of start_attributes.js from @cedar pine
Like this?
PlayerEvents.loggedIn(event => {
const {player} = event
player.setAttributeBaseValue("irons_spellbooks:mana_regen", 0.3)
//...
})
nice, that really looks better, i feel that doing things by running runCommandSilent is not soo good, i don't know if that is really a problem tho
rum command is hard to debug when things go wrong
i'm trying right now to optmize the best i can from my scripts
Also, since you are setting same things in both events, you can extract that into a function that takes an event
can you give an example? please.
Paste version of example.js from @marsh nebula
Ticket closed!
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...
})()
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");
});
})();
?