The TrustedHostsUtil interface provides methods for managing Essential’s list of trusted image hosts. This is used to control which domains are allowed to load images in Essential’s UI.
Getting TrustedHostsUtil
val trustedHosts = EssentialAPI.getTrustedHostsUtil()
TrustedHostsUtil trustedHosts = EssentialAPI.getTrustedHostsUtil();
Getting trusted hosts
getTrustedHosts()
Get all hosts from the trusted hosts list.
val hosts = trustedHosts.getTrustedHosts()
for (host in hosts) {
println("Host: ${host.name} (${host.id})")
println("Domains: ${host.domains.joinToString(", ")}")
}
Set<TrustedHost> hosts = trustedHosts.getTrustedHosts();
for (TrustedHost host : hosts) {
System.out.println("Host: " + host.getName() + " (" + host.getId() + ")");
System.out.println("Domains: " + String.join(", ", host.getDomains()));
}
Returns a Set<TrustedHost> containing all trusted hosts.
getTrustedHostByID()
Get a specific trusted host by its ID.
val host = trustedHosts.getTrustedHostByID("imgur")
if (host != null) {
println("Found host: ${host.name}")
} else {
println("Host not found")
}
TrustedHost host = trustedHosts.getTrustedHostByID("imgur");
if (host != null) {
System.out.println("Found host: " + host.getName());
} else {
System.out.println("Host not found");
}
The unique identifier of the host to retrieve
Returns the TrustedHost if found, or null if no host with that ID exists.
Managing trusted hosts
addTrustedHost()
Add a new image host to the trusted hosts list.
val newHost = TrustedHostsUtil.TrustedHost(
id = "my-cdn",
name = "My CDN",
domains = setOf("cdn.example.com", "images.example.com")
)
trustedHosts.addTrustedHost(newHost)
TrustedHost newHost = new TrustedHost(
"my-cdn",
"My CDN",
Set.of("cdn.example.com", "images.example.com")
);
trustedHosts.addTrustedHost(newHost);
The trusted host object to add to the list
removeTrustedHost()
Remove an image host from the trusted hosts list.
trustedHosts.removeTrustedHost("my-cdn")
trustedHosts.removeTrustedHost("my-cdn");
The ID of the host to remove
TrustedHost data class
The TrustedHost data class represents a trusted image host:
data class TrustedHost(
val id: String, // Unique identifier
val name: String, // Display name
val domains: Set<String> // Set of allowed domains
)
Properties
Unique identifier for the host (e.g., “imgur”, “discord”)
Human-readable name for the host (e.g., “Imgur”, “Discord CDN”)
Set of domain names that are trusted for this host (e.g., “i.imgur.com”, “cdn.discordapp.com”)
Example: Managing custom image hosts
import gg.essential.api.EssentialAPI
import gg.essential.api.utils.TrustedHostsUtil
import gg.essential.api.utils.TrustedHostsUtil.TrustedHost
class ImageHostManager {
private val trustedHosts = EssentialAPI.getTrustedHostsUtil()
fun addCustomImageHost() {
// Create a new trusted host
val customHost = TrustedHost(
id = "custom-cdn",
name = "Custom Image CDN",
domains = setOf(
"images.mymod.com",
"cdn.mymod.com",
"static.mymod.com"
)
)
// Add to trusted list
trustedHosts.addTrustedHost(customHost)
println("Added custom image host")
}
fun listAllHosts() {
val hosts = trustedHosts.getTrustedHosts()
println("Trusted hosts (${hosts.size}):")
for (host in hosts) {
println(" ${host.name} [${host.id}]")
for (domain in host.domains) {
println(" - $domain")
}
}
}
fun isHostTrusted(hostId: String): Boolean {
return trustedHosts.getTrustedHostByID(hostId) != null
}
fun removeCustomHost() {
trustedHosts.removeTrustedHost("custom-cdn")
println("Removed custom image host")
}
}
Use cases
The TrustedHostsUtil is useful for:
- Adding custom image sources - Allow images from your mod’s CDN
- Security - Control which domains can load images in Essential UI
- Debugging - List all trusted domains for troubleshooting
- User safety - Prevent images from untrusted sources
Be careful when adding trusted hosts. Only add domains you control or trust, as images from these domains will be loaded in Essential’s UI.
See also
Source: api/src/main/kotlin/gg/essential/api/utils/TrustedHostsUtil.kt