Skip to content

Commit ac9f7de

Browse files
authored
Source Code
1 parent fd10d42 commit ac9f7de

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

scripts/OPS/main.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { world } from "mojang-minecraft";
2+
import {configuration} from "../configuration.js"
3+
4+
var {maxDuration} = configuration;
5+
6+
function stringtifyCommand(command){
7+
let result = world.getDimension("overworld").runCommand(command);
8+
return JSON.stringify(result.statusMessage);
9+
}
10+
11+
world.events.beforeItemUseOn.subscribe((useOn) =>{
12+
wantsToSleep(useOn);
13+
});
14+
15+
world.events.tick.subscribe( e => {
16+
let players = world.getPlayers();
17+
for(let player of players){
18+
try {
19+
player.runCommand(`scoreboard objectives add sleepTick dummy "SleepTick"`);
20+
player.runCommand(`scoreboard objectives add sleepSecond dummy "SleepSecond"`);
21+
}
22+
catch {
23+
player.runCommand(`scoreboard players add ${player.name} "sleepTick" 0`);
24+
player.runCommand(`scoreboard players add ${player.name} "sleepSecond" 0`);
25+
}
26+
finally {
27+
if(player.hasTag("start_sleeping")){
28+
let sleepTick = parseInt((stringtifyCommand(`scoreboard players test ${player.name} "sleepTick" * *`)).split(" ")[1]);
29+
player.runCommand(`scoreboard players add ${player.name} "sleepTick" ${1}`);
30+
if(sleepTick % 20 === 0) {
31+
let sleepSecond = parseInt((stringtifyCommand(`scoreboard players test ${player.name} "sleepSecond" * *`)).split(" ")[1]);
32+
player.runCommand(`scoreboard players add ${player.name} "sleepSecond" ${1}`);
33+
if (sleepSecond >= maxDuration) {
34+
player.runCommandAsync(`time set day`);
35+
}
36+
}
37+
}
38+
else{
39+
player.runCommand(`scoreboard players set ${player.name} "sleepTick" ${0}`);
40+
player.runCommand(`scoreboard players set ${player.name} "sleepSecond" ${0}`);
41+
}
42+
}
43+
}
44+
});
45+
46+
47+
function getDayLightCycle(timeQuery){
48+
const timeCycles = {
49+
daytime: 1000, noon: 6000, sunset: 12000, night: 13000, midnight: 18000, sunrise: 23000
50+
};
51+
52+
if(Object.keys(timeCycles).some( timeCycle => timeCycle === timeQuery)) return timeCycles[timeQuery];
53+
return -1;
54+
}
55+
56+
function isNight(currentTick){
57+
// Checking what daytime cycle it is using the dayLightCycle on minecraft.
58+
return currentTick >= getDayLightCycle("night") && currentTick < getDayLightCycle("sunrise");
59+
}
60+
61+
62+
function wantsToSleep(player){
63+
player.source.removeTag("start_sleeping");
64+
65+
// Get world Tick using the command "time query daytime".
66+
let command = world.getDimension("overworld").runCommand('time query daytime');
67+
let worldTick = JSON.stringify(command.statusMessage).match(/[0-9]+/i);
68+
let block = world.getDimension("overworld").getBlock(player.blockLocation);
69+
70+
// If player interacts with a block named "bed", and is night time, while the player who touched is not sneaking.
71+
if(block.id == "minecraft:bed" && isNight(worldTick) && !player.source.isSneaking){
72+
player.source.addTag("start_sleeping");
73+
player.source.runCommand(`summon yn:not_sleeper ${player.blockLocation.x} ${player.blockLocation.y} ${player.blockLocation.z}`);
74+
}
75+
76+
// Else, the player interacted with the bed, but, it is not night time.
77+
else if(block.id == "minecraft:bed" && !isNight(worldTick)){
78+
player.source.runCommand(`title @s actionbar ${player.source.name} cannot sleep cause it's day.`);
79+
}
80+
}

scripts/configuration.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const configuration = {
2+
maxDuration: 3
3+
}

0 commit comments

Comments
 (0)