-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlightswitch.min.js
4 lines (4 loc) · 3.43 KB
/
lightswitch.min.js
1
2
3
4
var debug=!1;class LightSwitch{static debug=!1;static async init(){console.log("%c Light%cSwitch %c| initializing","color: #7bf542","color: #d8eb34","color: #ffffff"),LightSwitch.SOCKET="module.LightSwitch",game.socket.on(LightSwitch.SOCKET,data=>{debug&&(console.log("%c Light%cSwitch %c| received socket data","color: #7bf542","color: #d8eb34","color: #ffffff"),console.log(data)),1==data.switchLight&&game.user.isGM&&LightSwitch.switchLight(data)})}static switchLight(data){console.log("%c Light%cSwitch %c| switching lights","color: #7bf542","color: #d8eb34","color: #ffffff");var light=data.lightName;console.log(`light name im searching for: ${light}`);var allLights=LightingLayer.instance.objects.children,lightOnIDs=[],lightOffIDs=[];for(let i=0;i<=allLights.length-1;i++)allLights[i].data.flags.LightSwitch&&allLights[i].data.flags.LightSwitch.lightName==light&&(debug&&console.log(`found a light named ${light}`),(0==allLights[i].data.hidden?lightOffIDs:lightOnIDs).push(allLights[i].data._id));debug&&(console.log("on"),console.log(lightOnIDs),console.log("off"),console.log(lightOffIDs)),console.log(`%c Light%cSwitch %c| switching lights off: ${lightOffIDs.join(", ")}`,"color: #7bf542","color: #d8eb34","color: #ffffff"),canvas.lighting.updateAll({hidden:!0},light=>lightOffIDs.includes(light.id)),console.log(`%c Light%cSwitch %c| switching lights on: ${lightOnIDs.join(", ")}`,"color: #7bf542","color: #d8eb34","color: #ffffff"),canvas.lighting.updateAll({hidden:!1},light=>lightOnIDs.includes(light.id))}static async onRenderLightConfig(customNameEl,newwheight,data){var lightNameTitle=game.i18n.localize("LIGHTSWITCH.LightName.title"),lightTypeSelector=newwheight.find("[name='t']").parent(),windowDiv=lightTypeSelector.parent().parent().parent(),newwheight=windowDiv.height()+30;windowDiv.height(newwheight);const lightObj=customNameEl.object;customNameEl=lightObj.getFlag("LightSwitch","lightName")||"light1",customNameEl=$(`<div class="form-group">
<label>${lightNameTitle}</label>
<input type="text" name="lightSwitch.lightName" value="${customNameEl}" data-dtype="String">
</div>`);lightTypeSelector.before(customNameEl)}static onUpdateLight(scene,object,changes,diff){object.lightSwitch&&changes.diff&&LightSwitch.saveCustomProperties(object)}static async saveCustomProperties(placeable){var customProperties=JSON.parse(JSON.stringify(placeable.lightSwitch)),placeable=canvas.lighting.get(placeable._id);customProperties&&0!=Object.getOwnPropertyNames(customProperties).length?await placeable.setFlag("LightSwitch","lightName",customProperties.lightName):await placeable.unsetFlag("LightSwitch","lightName")}}async function flipTheSwitch(lightName){debug&&console.log("%c Light%cSwitch %c| Starting to send to socket","color: #7bf542","color: #d8eb34","color: #ffffff"),game.socket.emit(LightSwitch.SOCKET,{switchLight:!0,lightName:lightName})}async function flipTheSwitchGM(data){data={lightName:data};debug&&console.log("%c Light%cSwitch %c| flipping the switch","color: #7bf542","color: #d8eb34","color: #ffffff"),LightSwitch.switchLight(data)}Hooks.once("init",LightSwitch.init),Hooks.on("renderLightConfig",LightSwitch.onRenderLightConfig),Hooks.on("updateAmbientLight",LightSwitch.onUpdateLight),Hooks.on("ready",()=>{console.log("%c Light%cSwitch %c| Creating Macro Hooks","color: #7bf542","color: #d8eb34","color: #ffffff"),game.LightSwitch={flipTheSwitch:flipTheSwitch,flipTheSwitchGM:flipTheSwitchGM}});export{flipTheSwitch,flipTheSwitchGM};