Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
theripper93 committed Sep 28, 2024
1 parent 0a3930d commit a8d8cfc
Showing 1 changed file with 39 additions and 60 deletions.
99 changes: 39 additions & 60 deletions scripts/CombatHud.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,50 @@
* COMBAT HUD *
**************/
Hooks.once("init", function () {
if (game.system.id == "dnd5e") {
Hooks.on("createChatMessage", function (msg) {
try{
if (
!game.user.isGM ||
!msg.speaker.actor ||
!game.settings.get("combatbooster", "enableHud")
)
return;
const actor = game.actors.get(msg.speaker.actor);
const messageContent = $(msg.content);
if(!messageContent.length)return;
const itemId = messageContent[0].dataset?.itemId;
if (!itemId) return;
let oldItemsIds =
actor.getFlag(COMBAT_BOOSTER_MODULE_NAME, "recentItems") ?? [];
let newItemsIds = [itemId];
for (let itemIdo of oldItemsIds) {
if (itemIdo !== itemId) {
newItemsIds.push(itemIdo);
}
}
if (game.system.id != "dnd5e") return;

Hooks.on("dnd5e.postUseActivity", (activity) => {
const item = activity.item;
const actor = item.actor;
const token = actor.token;
const actorId = token?.actorId ?? actor.id;

const protoActor = game.actors.get(actorId);
const oldItemsIds = protoActor.getFlag(COMBAT_BOOSTER_MODULE_NAME, "recentItems") ?? [];
const newItemsIds = [item.id, ...oldItemsIds.filter((id) => id !== item.id)];
setTimeout(() => {
actor.setFlag(COMBAT_BOOSTER_MODULE_NAME, "recentItems", newItemsIds);
}, 2000)
}catch(e){}
protoActor.setFlag(COMBAT_BOOSTER_MODULE_NAME, "recentItems", newItemsIds);
}, 2000);
});

Hooks.on("renderTokenHUD", function (HUD, html, data) {
if (!game.settings.get("combatbooster", "enableHud")) return;
const actor = game.actors.get(data.actorId);
let recentItems =
actor.getFlag(COMBAT_BOOSTER_MODULE_NAME, "recentItems") || [];
const maxCol =
game.settings.get("combatbooster", "hudMaxCol") ||
HUD.object.document.width * 2;
const maxEls = game.settings.get("combatbooster", "hudRecent");
let cols =
Math.min(maxEls, recentItems.length) > maxCol
? maxCol
: Math.min(maxEls, recentItems.length);
let recentItemsHtml = `<div class="combatHUD" style="width:${
cols * 50
}px;">`;
if (!actor || recentItems.length === 0) return;
for (let i = 0; i < Math.min(maxEls, recentItems.length); i++) {
let itemId = recentItems[i];
let item = actor.items.find((i) => i.id === itemId);
if (item) {
recentItemsHtml += `<div class="control-icon" name="CBHUDbtn" id="${item.name}">
if (!game.settings.get("combatbooster", "enableHud")) return;
const actor = game.actors.get(data.actorId);
let recentItems = actor.getFlag(COMBAT_BOOSTER_MODULE_NAME, "recentItems") || [];
const maxCol = game.settings.get("combatbooster", "hudMaxCol") || HUD.object.document.width * 2;
const maxEls = game.settings.get("combatbooster", "hudRecent");
let cols = Math.min(maxEls, recentItems.length) > maxCol ? maxCol : Math.min(maxEls, recentItems.length);
let recentItemsHtml = `<div class="combatHUD" style="width:${cols * 50}px;">`;
if (!actor || recentItems.length === 0) return;
for (let i = 0; i < Math.min(maxEls, recentItems.length); i++) {
let itemId = recentItems[i];
let item = actor.items.find((i) => i.id === itemId);
if (item) {
recentItemsHtml += `<div class="control-icon" name="CBHUDbtn" id="${item.name}">
<img src="${item.img}" width="36" height="36" title='${item.name}'></i>
</div>`;
}
}
recentItemsHtml += `</div>`;
const controlIcons = html.find(`div[class="col left"]`);
controlIcons.before(recentItemsHtml);
const cHUDWidth = $(".combatHUD").outerWidth(true);
const hudWidth = $(html).outerWidth(true);
const diff = (hudWidth - cHUDWidth) / 2;
$(".combatHUD").css({ left: diff });
$(html.find(`div[name="CBHUDbtn"]`)).on("click", test);
function test() {
dnd5e.documents.macro.rollItem(this.id);
}
}
recentItemsHtml += `</div>`;
const controlIcons = html.find(`div[class="col left"]`);
controlIcons.before(recentItemsHtml);
const cHUDWidth = $(".combatHUD").outerWidth(true);
const hudWidth = $(html).outerWidth(true);
const diff = (hudWidth - cHUDWidth) / 2;
$(".combatHUD").css({ left: diff });
$(html.find(`div[name="CBHUDbtn"]`)).on("click", test);
function test() {
dnd5e.documents.macro.rollItem(this.id);
}
});
}
});
});

0 comments on commit a8d8cfc

Please sign in to comment.