2020-06-24 14:13:41 -04:00
/ * *
* The Star Wars 5 th Edition game system for Foundry Virtual Tabletop
* Author : Kakeman89
* Software License : GNU GPLv3
2020-10-23 13:32:23 -04:00
* Content License : https : //media.wizards.com/2016/downloads/SW5E/SRD-OGL_V5.1.pdf
2020-06-24 14:13:41 -04:00
* Repository : https : //gitlab.com/foundrynet/sw5e
* Issue Tracker : https : //gitlab.com/foundrynet/sw5e/issues
* /
// Import Modules
import { SW5E } from "./module/config.js" ;
import { registerSystemSettings } from "./module/settings.js" ;
import { preloadHandlebarsTemplates } from "./module/templates.js" ;
import { _getInitiativeFormula } from "./module/combat.js" ;
2020-10-23 13:32:23 -04:00
import { measureDistances , getBarAttribute } from "./module/canvas.js" ;
// Import Entities
import Actor5e from "./module/actor/entity.js" ;
import Item5e from "./module/item/entity.js" ;
// Import Applications
import AbilityTemplate from "./module/pixi/ability-template.js" ;
import AbilityUseDialog from "./module/apps/ability-use-dialog.js" ;
import ActorSheetFlags from "./module/apps/actor-flags.js" ;
2020-11-02 12:23:55 -07:00
import ActorSheet5eCharacter from "./module/actor/sheets/oldSheets/character.js" ;
import ActorSheet5eNPC from "./module/actor/sheets/oldSheets/npc.js" ;
import ActorSheet5eVehicle from "./module/actor/sheets/oldSheets/vehicle.js" ;
import ActorSheet5eCharacterNew from "./module/actor/sheets/newSheet/character.js" ;
2021-01-04 15:23:30 -05:00
import ActorSheet5eNPCNew from "./module/actor/sheets/newSheet/npc.js" ;
2020-10-23 13:32:23 -04:00
import ItemSheet5e from "./module/item/sheet.js" ;
import ShortRestDialog from "./module/apps/short-rest.js" ;
import TraitSelector from "./module/apps/trait-selector.js" ;
2021-01-04 15:23:30 -05:00
import ActorMovementConfig from "./module/apps/movement-config.js" ;
import ActorSensesConfig from "./module/apps/senses-config.js" ;
2020-10-23 13:32:23 -04:00
// Import Helpers
2020-06-24 14:13:41 -04:00
import * as chat from "./module/chat.js" ;
2020-10-23 13:32:23 -04:00
import * as dice from "./module/dice.js" ;
import * as macros from "./module/macros.js" ;
2020-06-24 14:13:41 -04:00
import * as migrations from "./module/migration.js" ;
/* -------------------------------------------- */
/* Foundry VTT Initialization */
/* -------------------------------------------- */
Hooks . once ( "init" , function ( ) {
2021-01-18 23:49:04 -05:00
console . log ( ` SW5e | Initializing SW5E System \n ${ SW5E . ASCII } ` ) ;
2020-06-24 14:13:41 -04:00
// Create a SW5E namespace within the game global
game . sw5e = {
2020-10-23 13:32:23 -04:00
applications : {
AbilityUseDialog ,
ActorSheetFlags ,
ActorSheet5eCharacter ,
2020-11-02 12:23:55 -07:00
ActorSheet5eCharacterNew ,
2020-10-23 13:32:23 -04:00
ActorSheet5eNPC ,
2021-01-04 15:23:30 -05:00
ActorSheet5eNPCNew ,
2020-10-23 13:32:23 -04:00
ActorSheet5eVehicle ,
ItemSheet5e ,
ShortRestDialog ,
2020-11-12 17:30:07 -05:00
TraitSelector ,
2021-01-04 15:23:30 -05:00
ActorMovementConfig
2020-10-23 13:32:23 -04:00
} ,
canvas : {
AbilityTemplate
} ,
config : SW5E ,
dice : dice ,
entities : {
Actor5e ,
Item5e ,
} ,
macros : macros ,
migrations : migrations ,
rollItemMacro : macros . rollItemMacro
2020-06-24 14:13:41 -04:00
} ;
// Record Configuration Values
CONFIG . SW5E = SW5E ;
CONFIG . Actor . entityClass = Actor5e ;
CONFIG . Item . entityClass = Item5e ;
2020-11-12 17:30:07 -05:00
CONFIG . time . roundTime = 6 ;
2021-01-18 23:49:04 -05:00
// 5e cone RAW should be 53.13 degrees
CONFIG . MeasuredTemplate . defaults . angle = 53.13 ;
2020-10-23 13:32:23 -04:00
// Add DND5e namespace for module compatability
game . dnd5e = game . sw5e ;
CONFIG . DND5E = CONFIG . SW5E ;
2020-06-24 14:13:41 -04:00
// Register System Settings
registerSystemSettings ( ) ;
// Patch Core Functions
2020-10-23 13:32:23 -04:00
CONFIG . Combat . initiative . formula = "1d20 + @attributes.init.mod + @attributes.init.prof + @attributes.init.bonus" ;
2020-06-24 14:13:41 -04:00
Combat . prototype . _getInitiativeFormula = _getInitiativeFormula ;
// Register sheet application classes
Actors . unregisterSheet ( "core" , ActorSheet ) ;
2020-11-02 12:23:55 -07:00
Actors . registerSheet ( "sw5e" , ActorSheet5eCharacterNew , {
2020-10-23 13:32:23 -04:00
types : [ "character" ] ,
makeDefault : true ,
label : "SW5E.SheetClassCharacter"
} ) ;
2020-11-02 12:23:55 -07:00
Actors . registerSheet ( "sw5e" , ActorSheet5eCharacter , {
types : [ "character" ] ,
makeDefault : false ,
label : "SW5E.SheetClassCharacterOld"
} ) ;
2021-01-04 15:23:30 -05:00
Actors . registerSheet ( "sw5e" , ActorSheet5eNPCNew , {
2020-10-23 13:32:23 -04:00
types : [ "npc" ] ,
makeDefault : true ,
label : "SW5E.SheetClassNPC"
} ) ;
2021-01-04 15:23:30 -05:00
Actors . registerSheet ( "sw5e" , ActorSheet5eNPC , {
types : [ "npc" ] ,
makeDefault : false ,
label : "SW5E.SheetClassNPCOld"
} ) ;
2020-10-23 13:32:23 -04:00
Actors . registerSheet ( 'sw5e' , ActorSheet5eVehicle , {
types : [ 'vehicle' ] ,
makeDefault : true ,
label : "SW5E.SheetClassVehicle"
} ) ;
2020-06-24 14:13:41 -04:00
Items . unregisterSheet ( "core" , ItemSheet ) ;
2020-10-23 12:36:42 -04:00
Items . registerSheet ( "sw5e" , ItemSheet5e , {
2020-11-05 15:37:53 -05:00
types : [ 'weapon' , 'equipment' , 'consumable' , 'tool' , 'loot' , 'class' , 'power' , 'feat' , 'species' , 'backpack' , 'archetype' , 'classfeature' , 'background' , 'fightingmastery' , 'fightingstyle' , 'lightsaberform' ] ,
2020-10-23 12:36:42 -04:00
makeDefault : true ,
label : "SW5E.SheetClassItem"
} ) ;
2020-06-24 14:13:41 -04:00
// Preload Handlebars Templates
preloadHandlebarsTemplates ( ) ;
} ) ;
/* -------------------------------------------- */
/* Foundry VTT Setup */
/* -------------------------------------------- */
/ * *
* This function runs after game data has been requested and loaded from the servers , so entities exist
* /
Hooks . once ( "setup" , function ( ) {
// Localize CONFIG objects once up-front
const toLocalize = [
2021-01-04 15:23:30 -05:00
"abilities" , "abilityAbbreviations" , "abilityActivationTypes" , "abilityConsumptionTypes" , "actorSizes" , "alignments" ,
2020-11-12 17:30:07 -05:00
"armorProficiencies" , "armorPropertiesTypes" , "conditionTypes" , "consumableTypes" , "cover" , "currencies" , "damageResistanceTypes" ,
"damageTypes" , "distanceUnits" , "equipmentTypes" , "healingTypes" , "itemActionTypes" , "languages" ,
2021-01-18 23:49:04 -05:00
"limitedUsePeriods" , "movementTypes" , "movementUnits" , "polymorphSettings" , "proficiencyLevels" , "senses" , "skills" ,
2021-01-04 15:23:30 -05:00
"powerComponents" , "powerLevels" , "powerPreparationModes" , "powerScalingModes" , "powerSchools" , "targetTypes" ,
"timePeriods" , "toolProficiencies" , "weaponProficiencies" , "weaponProperties" , "weaponTypes"
2020-10-23 13:32:23 -04:00
] ;
// Exclude some from sorting where the default order matters
const noSort = [
2020-11-12 17:30:07 -05:00
"abilities" , "alignments" , "currencies" , "distanceUnits" , "movementUnits" , "itemActionTypes" , "proficiencyLevels" ,
"limitedUsePeriods" , "powerComponents" , "powerLevels" , "powerPreparationModes" , "weaponTypes"
2020-06-24 14:13:41 -04:00
] ;
2020-10-23 13:32:23 -04:00
// Localize and sort CONFIG objects
2020-06-24 14:13:41 -04:00
for ( let o of toLocalize ) {
2020-10-23 13:32:23 -04:00
const localized = Object . entries ( CONFIG . SW5E [ o ] ) . map ( e => {
return [ e [ 0 ] , game . i18n . localize ( e [ 1 ] ) ] ;
} ) ;
if ( ! noSort . includes ( o ) ) localized . sort ( ( a , b ) => a [ 1 ] . localeCompare ( b [ 1 ] ) ) ;
CONFIG . SW5E [ o ] = localized . reduce ( ( obj , e ) => {
obj [ e [ 0 ] ] = e [ 1 ] ;
2020-06-24 14:13:41 -04:00
return obj ;
} , { } ) ;
}
2020-10-23 13:32:23 -04:00
// add DND5E translation for module compatability
game . i18n . translations . DND5E = game . i18n . translations . SW5E ;
2020-11-04 13:12:41 -07:00
// console.log(game.settings.get("sw5e", "colorTheme"));
let theme = game . settings . get ( "sw5e" , "colorTheme" ) + '-theme' ;
document . body . classList . add ( theme ) ;
2020-06-24 14:13:41 -04:00
} ) ;
/* -------------------------------------------- */
/ * *
* Once the entire VTT framework is initialized , check to see if we should perform a data migration
* /
Hooks . once ( "ready" , function ( ) {
2020-11-12 17:30:07 -05:00
// Wait to register hotbar drop hook on ready so that modules could register earlier if they want to
Hooks . on ( "hotbarDrop" , ( bar , data , slot ) => macros . create5eMacro ( data , slot ) ) ;
2020-06-24 14:13:41 -04:00
// Determine whether a system migration is required and feasible
2020-11-12 17:30:07 -05:00
if ( ! game . user . isGM ) return ;
2020-06-24 14:13:41 -04:00
const currentVersion = game . settings . get ( "sw5e" , "systemMigrationVersion" ) ;
2021-01-18 23:49:04 -05:00
const NEEDS _MIGRATION _VERSION = "1.2.1" ;
2020-06-24 14:13:41 -04:00
const COMPATIBLE _MIGRATION _VERSION = 0.80 ;
2020-11-12 17:30:07 -05:00
const needsMigration = currentVersion && isNewerVersion ( NEEDS _MIGRATION _VERSION , currentVersion ) ;
if ( ! needsMigration ) return ;
2020-06-24 14:13:41 -04:00
// Perform the migration
2020-11-12 17:30:07 -05:00
if ( currentVersion && isNewerVersion ( COMPATIBLE _MIGRATION _VERSION , currentVersion ) ) {
const warning = ` Your SW5e system data is from too old a Foundry version and cannot be reliably migrated to the latest version. The process will be attempted, but errors may occur. ` ;
ui . notifications . error ( warning , { permanent : true } ) ;
2020-06-24 14:13:41 -04:00
}
2020-11-12 17:30:07 -05:00
migrations . migrateWorld ( ) ;
2020-06-24 14:13:41 -04:00
} ) ;
/* -------------------------------------------- */
/* Canvas Initialization */
/* -------------------------------------------- */
Hooks . on ( "canvasInit" , function ( ) {
// Extend Diagonal Measurement
canvas . grid . diagonalRule = game . settings . get ( "sw5e" , "diagonalMovement" ) ;
2020-10-23 13:32:23 -04:00
SquareGrid . prototype . measureDistances = measureDistances ;
2020-06-24 14:13:41 -04:00
// Extend Token Resource Bars
Token . prototype . getBarAttribute = getBarAttribute ;
} ) ;
/* -------------------------------------------- */
/* Other Hooks */
/* -------------------------------------------- */
Hooks . on ( "renderChatMessage" , ( app , html , data ) => {
// Display action buttons
chat . displayChatActionButtons ( app , html , data ) ;
// Highlight critical success or failure die
chat . highlightCriticalSuccessFailure ( app , html , data ) ;
// Optionally collapse the content
if ( game . settings . get ( "sw5e" , "autoCollapseItemCards" ) ) html . find ( ".card-content" ) . hide ( ) ;
} ) ;
Hooks . on ( "getChatLogEntryContext" , chat . addChatMessageContextOptions ) ;
Hooks . on ( "renderChatLog" , ( app , html , data ) => Item5e . chatListeners ( html ) ) ;
2020-10-23 13:32:23 -04:00
Hooks . on ( "renderChatPopout" , ( app , html , data ) => Item5e . chatListeners ( html ) ) ;
2020-06-24 14:13:41 -04:00
Hooks . on ( 'getActorDirectoryEntryContext' , Actor5e . addDirectoryContextOptions ) ;
2020-11-02 12:23:55 -07:00
Hooks . on ( "renderSceneDirectory" , ( app , html , data ) => {
//console.log(html.find("header.folder-header"));
setFolderBackground ( html ) ;
} ) ;
Hooks . on ( "renderActorDirectory" , ( app , html , data ) => {
setFolderBackground ( html ) ;
} ) ;
Hooks . on ( "renderItemDirectory" , ( app , html , data ) => {
setFolderBackground ( html ) ;
} ) ;
Hooks . on ( "renderJournalDirectory" , ( app , html , data ) => {
setFolderBackground ( html ) ;
} ) ;
Hooks . on ( "renderRollTableDirectory" , ( app , html , data ) => {
setFolderBackground ( html ) ;
} ) ;
2020-11-05 20:37:19 -07:00
Hooks . on ( "ActorSheet5eCharacterNew" , ( app , html , data ) => {
console . log ( "renderSwaltSheet" ) ;
} ) ;
2020-10-23 13:32:23 -04:00
// TODO I should remove this
Handlebars . registerHelper ( 'getProperty' , function ( data , property ) {
return getProperty ( data , property ) ;
} ) ;
2020-11-02 12:23:55 -07:00
function setFolderBackground ( html ) {
html . find ( "header.folder-header" ) . each ( function ( ) {
let bgColor = $ ( this ) . css ( "background-color" ) ;
if ( bgColor == undefined )
bgColor = "rgb(255,255,255)" ;
$ ( this ) . closest ( 'li' ) . css ( "background-color" , bgColor ) ;
} )
}