Advertisement
MajorVictory

CreateMaterial

Sep 6th, 2020
1,816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let matRarities = [
  2.   'Common',
  3.   'Uncommon',
  4.   'Rare',
  5.   'Very Rare',
  6.   'Legendary',
  7.   'Artifact'
  8. ];
  9.  
  10. let matTypes = {
  11.   'Metal': {tool: "Smith's Tools", uses: ['Weapons', 'Armors', 'Accessories']},
  12.   'Precious Metal': {tool: "Smith's Tools", uses: ['Weapons', 'Armors', 'Accessories']},
  13.   'Wood': {tool: "Carpenter's Tools", uses: ['Weapons', 'Armors', 'Accessories']},
  14.   'Herb': {tool: "Herbalism Kit", uses: ['Potions', 'Accessories']},
  15.   'Component': {tool: "Cook's Utensils", uses: ['Potions', 'Accessories']},
  16.   'Hide': {tool: "Leatherworker's Tools", uses: ['Weapons', 'Armors', 'Potions', 'Accessories']},
  17.   'Bone': {tool: "Cook's Utensils", uses: ['Weapons', 'Armors', 'Potions', 'Accessories']},
  18.   'Fluid': {tool: "Brewer's Supplies", uses: ['Potions', 'Accessories']},
  19.   'Mineral': {tool: "Jeweler's Tools", uses: ['Accessories']},
  20.   'Gem': {tool: "Jeweler's Tools", uses: ['Accessories']},
  21.   'Essence': {tool: "Alchemist's Supplies", uses: ['Weapons', 'Armors', 'Potions', 'Accessories']}
  22. };
  23.  
  24. let matQualities = [
  25.   '~~ None ~~',
  26.   'Acidic',
  27.   'Chaotic',
  28.   'Cold',
  29.   'Dimensional',
  30.   'Fiendish',
  31.   'Fiery',
  32.   'Forceful',
  33.   'Inert',
  34.   'Lightning',
  35.   'Mercurial',
  36.   'Necrotic',
  37.   'Phantasmal',
  38.   'Poisonous',
  39.   'Pyschic',
  40.   'Radiant',
  41.   'Theurgic',
  42.   'Thundering',
  43.   'Vital',
  44.   'Void',
  45.   'Zephyrous'
  46. ];
  47.  
  48. let rarityOptions = matRarities.map(name => {
  49.   return '<option value="'+name+'">'+name+'</option>';
  50. }).join('');
  51.  
  52. let typeOptions = Object.entries(matTypes).map(([name, data]) => {
  53.   return '<option value="'+name+'">'+name+'</option>';
  54. }).join('');
  55.  
  56. let qualityOptions = matQualities.map(name => {
  57.   return '<option value="'+name+'">'+name+'</option>';
  58. }).join('');
  59.  
  60. let folderOptions = game.folders.filter(f => f.data.type == "Item").map(f => {
  61.   return '<option value="'+f._id+'">'+(f.parent ? f.parent.name+'/':'')+f.name+'</option>';
  62. }).join('');
  63.  
  64. new Dialog({
  65.   title: "Create Crafting Material",
  66.   content: `<form class="editable flexcol" autocomplete="off">
  67.       <header class="sheet-header">
  68.         <h1><input name="matName" type="text" value="" placeholder="New Material Name"/></h1>
  69.       </header>
  70.       <div class="form-group"><label>Save to Folder: </label><select name="matFolder"><option value="">~~ None ~~</option>${folderOptions}</select></div>
  71.       <div class="form-group"><label>Print to Chat: </label><input type="checkbox" name="toChat" checked="checked"/></div>
  72.       <hr>
  73.       <div class="form-group"><label>Rarity: </label><select name="matRarity">${rarityOptions}</select></div>
  74.       <div class="form-group"><label>Type: </label><select name="matType">${typeOptions}</select></div>
  75.       <div class="form-group"><label>Quality: </label><select name="matQuality">${qualityOptions}</select></div>
  76.       <div class="form-group"><label>Value: </label><input name="matValue" type="number" value="0"/></div>
  77.       <div class="form-group"><label>Weight: </label><input name="matWeight" type="number" value="0"/></div>
  78.       <div class="form-group"><label>Description: </label><textarea name="matDesc"/></textarea></div>
  79.     </form>`,
  80.   buttons: {
  81.     ok: {
  82.       label: "Ok",
  83.       callback: (html) => {
  84.         createMaterial(
  85.           html.find('[name=matFolder]').val(),
  86.           html.find('[name=matName]').val(),
  87.           html.find('[name=matRarity]').val(),
  88.           html.find('[name=matType]').val(),
  89.           html.find('[name=matQuality]').val(),
  90.           html.find('[name=matValue]').val(),
  91.           html.find('[name=matWeight]').val(),
  92.           html.find('[name=matDesc]').val(),
  93.           html.find('[name=toChat]').is(':checked'),
  94.           matRarities,
  95.           matTypes,
  96.           matQualities
  97.         );
  98.       }
  99.     },
  100.     cancel: {
  101.       label: 'Cancel',
  102.       callback: (html) => {
  103.         dialogCancel();
  104.       }
  105.     }
  106.   },
  107.   default: "ok"
  108. }).render(true);
  109.  
  110. async function dialogCancel() { return; }
  111.  
  112. async function createMaterial(matFolder, matName, matRarity, matType, matQuality, matValue, matWeight, matDesc, toChat, matRarities, matTypes, matQualities) {
  113.  
  114.   let newMaterialItem = {
  115.     "name": (matName || 'Unidentified Item'),
  116.     "type": "loot",
  117.     "folder": matFolder ? game.folders.get(matFolder) : '',
  118.     "data": {
  119.       "description": {
  120.         "value": "<i>Material, "+matRarity+", "+matType+(matQuality ? ', '+matQuality : '')+"</i><br><b>Tool:</b> "+matTypes[matType].tool+"<br><b>Useful in: </b>"+matTypes[matType].uses.join(',')+"<hr><b>Description:</b> "+matDesc,
  121.         "chat": "",
  122.         "unidentified": ""
  123.       },
  124.       "source": "Homebrew",
  125.       "quantity": 1,
  126.       "weight": matWeight,
  127.       "price": matValue,
  128.       "attuned": false,
  129.       "equipped": false,
  130.       "rarity": matRarity,
  131.       "identified": true,
  132.       // not necessary, but could be useful
  133.       "material": {
  134.         "rarity": matRarity,
  135.         "type": matType,
  136.         "quality": matQuality,
  137.         "tool": matTypes[matType].tool,
  138.         "uses": matTypes[matType].uses
  139.       }
  140.       //
  141.     },
  142.     "img": '/Repository/Icons/Materials/'+matType+'.jpg'
  143.   };
  144.  
  145.   let newLoot = await Item.create(newMaterialItem, newMaterialItem);
  146.  
  147.   //console.log('newMaterialItem', newMaterialItem);
  148.   //console.log('newLoot', newLoot);
  149.  
  150.   if (toChat) {
  151.     ChatMessage.create({
  152.       content:"<h1>Loot get!</h1>@Item["+newLoot.id+"]{"+newLoot.name+"}"
  153.     });
  154.   }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement