Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyVadala committed May 28, 2020
2 parents 4b75e3e + 9219258 commit d714563
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 52 deletions.
110 changes: 58 additions & 52 deletions item/foundry_item_permission.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,70 @@
var folderName = "Test Deeper";
var desiredPermission = 0; // 0=None, 1=Limited, 2=Observer, 3=Owner
var applyInSubfolders = true;
const form = `
<div style="display: inline-block; width: 100px">Folder:</div>
<input type="string" id="folderName">
<br />
/*****
WARNING: This will overwrite the permissions of the items in the folder <folderName>!
That is, it will not keep the player permissions!
Tested on FoundryVTT 0.5.5 & 0.5.7
* * * * * * * * * * * * * * * * * * * * * *
WARNING: Make sure the folder name is unique across actors, scenes, items, journals and roll tables!
This script will apply the desired permission on the first folder it finds with that name.
******/
<div style="display: inline-block; width: 100px">Permission:</div>
<select id="desiredPermission" />
<option value="0">None</option>
<option value="1">Limited</option>
<option value="2">Observer</option>
<option value="3">Owner</option>
</select>
<br />
function repermission(currentFolder) {
console.log("Repermissioning: ", currentFolder.name);
<label>
<input type="checkbox" id="recurse" checked/>
Recurse into subfolders
</label>
`;

if (currentFolder.content){
currentFolder.content.map(item => {
let newPermissions = duplicate(item.data.permission);
newPermissions.default = desiredPermission;
console.log(" Item:", item.data.name);
item.update({permission: newPermissions});
});
const dialog = new Dialog({
title: "Set desired permission",
content: form,
buttons: {
use: {
label: "Apply permissions",
callback: applyPermissions
}
}
}).render(true);

function applyPermissions(html) {
const folderName = html.find(`input#folderName`)[0].value;
const permission = html.find(`select#desiredPermission`)[0].value;
const recurse = html.find(`input#recurse`)[0].checked;

if (currentFolder.children && applyInSubfolders) {
currentFolder.children.map(({data}) => {
repermission(game.folders.entities.filter(f => f.data._id == data._id)[0]);
});
const folders = game.folders.filter(f => f.name === folderName);
if (folders.length === 0) {
ui.notifications.error(`Your world does not have any folders named '${folderName}'.`);
}
}

function findFolder(parent) {
if (parent.data.name === folderName) {
return parent;
else if(folders.length > 1) {
ui.notifications.error(`Your world has more than one folder named ${folderName}`)
}

for (let child of parent.children) {
let foundFolder = findFolder(child);
if (foundFolder) {
return foundFolder;
}
else {
repermission(folders[0], permission, recurse);
ui.notifications.notify(`Desired permissions were set successfully for '${folderName}'.`);
}

return null;
}

if (game.folders.entities.length == 0) {
console.error("Your world does not have any folders.");
}

var root = { data: {}, children: game.folders.entities };
function repermission(currentFolder, desiredPermission, recurse) {
console.debug("Repermissioning: ", currentFolder.name);

if (currentFolder.content) {
currentFolder.content.map(item => {
let newPermissions = duplicate(item.data.permission);
newPermissions.default = desiredPermission;
console.debug(" Item:", item.data.name);
item.update({ permission: newPermissions });
});
}

var folder = findFolder(root);
if (!folder) {
console.error(`Your world does not have any folders named '${folderName}'.`);
if (currentFolder.children && recurse) {
currentFolder.children.map(({ data }) => {
repermission(
game.folders.entities.filter(f => f.data._id == data._id)[0],
desiredPermission,
recurse);
});
}
}
else {
repermission(folder);
console.log("Repermissioning finished successfully!");
}
8 changes: 8 additions & 0 deletions misc/close_all_doors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Closes all doors on the canvas
* Author: @Atropos#3814
*/

canvas.walls.updateMany(canvas.scene.data.walls.map(w => {
return {_id: w._id, ds: w.ds === 1 ? 0 : w.ds};
}));
19 changes: 19 additions & 0 deletions misc/folder_to_rollable_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Take the entries of a folder and turn it into a rollable table
// Author: @Atropos#3814

const folder = game.folders.getName("Herbalism & Alchemy");
const table = game.tables.getName("Common Ingredients");
const items = folder.entities;
const results = folder.entities.map(i => {
return {
text: i.data.name,
type: 1,
collection: "Item",
resultId: i.data._id,
img: i.data.img,
weight: 1,
range: [1, 1],
drawn: false
}
});
await table.createEmbeddedEntity("TableResult", results);

0 comments on commit d714563

Please sign in to comment.