forked from FBQingYi/LXL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtombstone.js
87 lines (82 loc) · 2.92 KB
/
tombstone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const pluginName = 'tombstone';
const pluginDescribe = '墓碑';
const pluginVersion = [0, 0, 1];
const pluginOther = { "版权归属": "LIGHT服务器" };
let path = `./plugins/tombstone/data/`
let DeathDoNotDropQuery;
if (ll.require("hwsa.js")) {
DeathDoNotDropQuery = ll.import("DeathDoNotDropQuery");
}
mc.listen("onPlayerDie", (player, _source) => {
if (!DeathDoNotDropQuery(player)) {
let allItemString = [];
let pos = player.blockPos;
let availablePos = getAvailablePos(pos);
let fileName = `${availablePos.x}_${availablePos.y}_${availablePos.z}_${availablePos.dimid}.json`;
let playerInventoryC = player.getInventory().getAllItems();
let playerArmorC = player.getArmor().getAllItems();
let playerOffHandI = player.getOffHand();
playerInventoryC.forEach(item => {
if (!item.isNull()) {
let itemSNBT = item.getNbt().toSNBT();
allItemString.push(itemSNBT);
}
});
playerArmorC.forEach(item => {
if (!item.isNull()) {
let itemSNBT = item.getNbt().toSNBT();
allItemString.push(itemSNBT);
}
});
let itemSNBT = playerOffHandI.getNbt().toSNBT();
allItemString.push(itemSNBT);
let data = JSON.stringify(allItemString)
File.writeTo(path + fileName, data);
mc.setBlock(availablePos, "minecraft:skull");
player.getInventory().removeAllItems();
player.getArmor().removeAllItems();
player.getOffHand().setNull();
player.refreshItems();
}
})
mc.listen("onDestroyBlock", (_player, block) => {
if (block.type == "minecraft:skull") {
let pos = block.pos;
let fileName = `${pos.x}_${pos.y}_${pos.z}_${pos.dimid}.json`;
let data = File.readFrom(path + fileName);
if (data != undefined) {
let itemAllSNBT = JSON.parse(data);
itemAllSNBT.forEach(itemSNBT => {
let nbt = NBT.parseSNBT(itemSNBT);
mc.spawnItem(mc.newItem(nbt), pos);
});
block.destroy(false);
File.delete(path + fileName);
return false;
}
}
})
function getAvailablePos(pos) {
let block = mc.getBlock(pos);
if (block.isAir) {
return pos;
} else {
let posArray = getAdjacentPoints([pos.x, pos.y, pos.z]);
for (let i = 0; i < posArray.length; i++) {
let posA1 = posArray[i];
let block = mc.getBlock(posA1[0], posA1[1], posA1[2], pos.dimid);
if (block.isAir) {
return new IntPos(posA1[0], posA1[1], posA1[2], pos.dimid);
}
}
}
}
function getAdjacentPoints([x, y, z]) {
return [
[x + 1, y, z],
[x - 1, y, z],
[x, y + 1, z],
[x, y, z + 1],
[x, y, z - 1],
];
}