Skip to content

Commit

Permalink
added shared health
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmajid committed Mar 16, 2024
1 parent 88a4228 commit 2831456
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
manifest.json
/manifest.json
package.json
package-lock.json
node_modules
Expand Down
38 changes: 38 additions & 0 deletions mc-server/shared-health/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { system, world } from "@minecraft/server";

let lastHealth = 20;

world.afterEvents.playerSpawn.subscribe((data) => {
data.player.getComponent("health").setCurrentValue(lastHealth);
world.getDimension("overworld").runCommandAsync("gamerule naturalregeneration false");
world.getDimension("overworld").runCommandAsync("gamerule doimmediaterespawn true");
});

world.afterEvents.entityHealthChanged.subscribe(
(data) => {
if (data.entity.getComponent("health").currentValue === lastHealth) return;
const players = world.getAllPlayers();
for (let i = 0; i < players.length; i++) {
players[i].getComponent("health").setCurrentValue(data.newValue);
lastHealth = data.newValue;
}
},
{ entityTypes: ["minecraft:player"] }
);

world.afterEvents.entityDie.subscribe(
(data) => {
lastHealth = 20;
data.deadEntity.getComponent("health").setCurrentValue(20);
},
{ entityTypes: ["minecraft:player"] }
);

system.runInterval(() => {
if (lastHealth > 19) return;
lastHealth++;
const players = world.getAllPlayers();
for (let i = 0; i < players.length; i++) {
players[i].getComponent("health").setCurrentValue(lastHealth);
}
}, 60);

0 comments on commit 2831456

Please sign in to comment.