Skip to content

Commit

Permalink
Better minecraft badge 1.0.0
Browse files Browse the repository at this point in the history
New add-on.
  • Loading branch information
MrPandir authored Jul 30, 2024
1 parent 1313429 commit 5a4f4a5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/better-minecraft-badge/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class BetterMineBadge extends Addon {
constructor(...args) {
super(...args);
this.inject('chat.badges');

this.idBadge = 'minecraft-15th-anniversary-celebration';
this.newBadgeUrl = 'https://cdn.frankerfacez.com/emoticon/753195';
this.originalBadgeUrl = null;
}

onEnable() {
this._updateTwitchBadges = this.badges.updateTwitchBadges;
this.badges.updateTwitchBadges = (badges, ...args) => {
// We guarantee that if the code breaks, it will not break the FFZ API
try {
this.rememberOriginalBadgeUrl(badges);
badges = this.replaceBadge(badges, this.idBadge, this.newBadgeUrl);
} catch (e) {
this.log.error(e);
}
this._updateTwitchBadges(badges, ...args);
};

// If we didn’t have time to replace the function before calling it, we call it ourselves
if (this.getBadgeUrl(this.idBadge) !== this.newBadgeUrl && this.badges.twitch_badges) {
this.badges.updateTwitchBadges(this.badges.twitch_badges);
}
}

onDisable() {
this.badges.updateTwitchBadges = this._updateTwitchBadges;
const badges = this.replaceBadge(this.badges.twitch_badges, this.idBadge, this.originalBadgeUrl);
this.badges.updateTwitchBadges(badges);
}

getBadgeUrl(idBadge, badgesSet = null) {
const badges = badgesSet || this.badges.twitch_badges || {};

if (!badges || Object.keys(badges).length === 0) return null;

let targetBadge;
if (Array.isArray(badges)) targetBadge = badges.find(item => item.setID === idBadge);
else targetBadge = badges[idBadge][1];

if (!targetBadge?.image1x) return null;

return targetBadge.image1x.slice(0, -2); // remove '/1'
}

rememberOriginalBadgeUrl(badgesSet = null) {
if (this.originalBadgeUrl) return;

const badgeUrl = this.getBadgeUrl(this.idBadge, badgesSet);
if (badgeUrl) this.originalBadgeUrl = badgeUrl;
}

replaceBadge(badges, idBadge, newBadgeUrl) {
if (Array.isArray(badges) && badges?.length) {
const newBadgesArray = JSON.parse(JSON.stringify(badges)); // avoid only-read

const targetBadge = newBadgesArray.find(item => item.setID === idBadge);

this._replaceBadgeImages(targetBadge, newBadgeUrl);
return newBadgesArray;
} else if (badges[idBadge]) {
const targetBadge = badges[idBadge][1];

this._replaceBadgeImages(targetBadge, newBadgeUrl);
return badges;
}
}

_replaceBadgeImages(badge, newBaseUrl) {
if (!badge) return;
badge.image1x = `${newBaseUrl}/1`;
badge.image2x = `${newBaseUrl}/2`;
badge.image4x = `${newBaseUrl}/${newBaseUrl.includes('cdn.frankerfacez.com') ? '4' : '3'}`;
}
}

BetterMineBadge.register();
Binary file added src/better-minecraft-badge/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/better-minecraft-badge/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"enabled": true,
"requires": [],
"version": "1.0.0",
"short_name": "BetterMineBadge",
"name": "Better minecraft badge",
"author": "MrPandir",
"description": "Replace the Minecraft badge for the 15th anniversary with a more beautiful one"
}

0 comments on commit 5a4f4a5

Please sign in to comment.