This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestock.js
78 lines (61 loc) · 3.05 KB
/
restock.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
importPackage(Packages.com.sk89q.worldedit); //Setup all the worldedit items
importPackage(Packages.com.sk89q.worldedit.blocks);
importPackage(Packages.com.sk89q.worldedit.world.block);
importPackage(Packages.com.sk89q.jnbt);
context.checkArgs(2, 3, "<namespace/lootTable> <forceReplace> <coordFile>\ncheck https://github.com/Voltane-EU/ReStock.js for Help \n§4Be EXTRA CAREFUL with §6forceReplace §b> §l§4TRUE !! \n§r§4TRIPPLE check that you have the correct coordinates!!"); //ensure atleast two/ maximum of three arguments have been given
var currentSession = context.remember();
var lootTable = argv[1]; //get loottable through first argument
var forceReplace = (argv[2] == 'true'); //if to replace targetblock with chest //convert to bool
var coordFile = argv.length > 3 ? (argv[3] + ".json") : 'default.json'; //get the json file name, if no argument present, get default.json
var coordArray = readCoords(coordFile);
changeBaseBlock(coordArray, forceReplace);
//Json reading
function readCoords(_coordFile) {
var reader = java.nio.file.Files.readAllLines(java.nio.file.Paths.get('config/worldedit/craftscripts/restock/' + _coordFile));
if (reader)
return JSON.parse(reader);
}
//Operation
function changeBaseBlock(_coordArray, _forceReplace) {
/*
currently the json looks like this:
[[54, -70, 28, -118],...]
or
[[TARGET, X, Y, Z],...]
*/
var counter = 0;
_coordArray = _coordArray[0];
_coordArray.forEach(coord => {
var position = new BlockVector(coord[1], coord[2], coord[3]);
var baseBlock = currentSession.getBlock(position);
var targetContainer = coord[0];
if (baseBlock.id != targetContainer) {
if (!_forceReplace) {
//not a chest, but forcereplace is false, so dont replace
player.print('§6skipping: '+ baseBlock.id +" | "+ position + ' isnt a chest! forceReplace is '+_forceReplace);
return
} else {
//not a chest, forcereplace is true, so replace'
player.print('§creplacing: '+ baseBlock.id +" | "+ position + ' with Chest!');
baseBlock = new BaseBlock(targetContainer);
nbtBuilder = CompoundTagBuilder.create();
}
} else {
//a chest, dont replace regardless of forcereplace
nbtData = baseBlock.getNbtData();
nbtLootTable = nbtData.getString('LootTable');
if (nbtLootTable == lootTable) {
player.print('§6skipping: '+ baseBlock.id +" | "+ position + ' already has desired loottable.');
return
}
nbtBuilder = nbtData.createBuilder();
}
//chest/container has been opened and will therefore be refilled
/*creating compoundbuilder from nbt data*/
nbtBuilder.putString('LootTable', lootTable);
baseBlock.setNbtData(nbtBuilder.build());
currentSession.rawSetBlock(position, baseBlock);
counter++;
});
player.print('§2finished: ' + counter + ' Containers have been filled.')
}