-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial resource packs implementation
Signed-off-by: Joshua Castle <[email protected]>
- Loading branch information
Showing
14 changed files
with
472 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
core/src/main/java/net/paradisu/core/packs/PackManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package net.paradisu.core.packs; | ||
|
||
import it.unimi.dsi.fastutil.objects.Object2ObjectMap; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
import lombok.Getter; | ||
import lombok.experimental.Accessors; | ||
import net.kyori.adventure.resource.ResourcePackInfo; | ||
import net.kyori.adventure.resource.ResourcePackRequest; | ||
import net.paradisu.core.ParadisuPlugin; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* Class to manage resource packs | ||
*/ | ||
@Accessors(fluent = true) | ||
@Getter | ||
public class PackManager { | ||
/** | ||
* The plugin instance | ||
*/ | ||
private ParadisuPlugin paradisu; | ||
/** | ||
* The list of resource pack urls | ||
*/ | ||
private List<String> resourcePackUrls; | ||
/** | ||
* The default request for the resource packs | ||
*/ | ||
private CompletableFuture<ResourcePackRequest> defaultRequest; | ||
/** | ||
* The staging pack requests | ||
*/ | ||
private Object2ObjectMap<String, Optional<CompletableFuture<ResourcePackRequest>>> stagingPackRequests; | ||
|
||
/** | ||
* Constructor for PackManager class to manage resource packs | ||
* | ||
* @param paradisu the plugin instance | ||
* @param resourcePackUrls the list of resource pack urls | ||
*/ | ||
public PackManager(ParadisuPlugin paradisu, List<String> resourcePackUrls) { | ||
this.paradisu = paradisu; | ||
this.resourcePackUrls = resourcePackUrls; | ||
this.stagingPackRequests = new Object2ObjectOpenHashMap<>(); | ||
this.defaultRequest = this.buildDefaultRequest(this.buildDefaultList(this.resourcePackUrls)); | ||
} | ||
|
||
/** | ||
* Clear the staging pack requests | ||
*/ | ||
public void clearStagingRequests() { | ||
this.stagingPackRequests.clear(); | ||
} | ||
|
||
/** | ||
* Reload the default resource packs and clear the staging requests | ||
*/ | ||
public void reload() { | ||
this.defaultRequest = this.buildDefaultRequest(this.buildDefaultList(this.resourcePackUrls)); | ||
this.clearStagingRequests(); | ||
} | ||
|
||
/** | ||
* Set the resource pack urls | ||
* | ||
* @param resourcePackUrls the list of resource pack urls | ||
*/ | ||
public void resourcePackUrls(List<String> resourcePackUrls) { | ||
this.resourcePackUrls = resourcePackUrls; | ||
this.defaultRequest = this.buildDefaultRequest(this.buildDefaultList(this.resourcePackUrls)); | ||
} | ||
|
||
/** | ||
* Get the staging request for a resource pack | ||
* | ||
* @param url the url of the resource pack | ||
* @param tag the tag of the resource pack | ||
* @return the staging request | ||
*/ | ||
public Optional<CompletableFuture<ResourcePackRequest>> stagingRequest(String url, String tag) { | ||
return this.stagingPackRequests.computeIfAbsent(tag, k -> { | ||
return this.buildPackInfo(url).map(this::buildRequestFuture); | ||
}); | ||
} | ||
|
||
/** | ||
* Get the staging request for a resource pack | ||
* | ||
* @param tag the tag of the resource pack | ||
* @return the staging request | ||
*/ | ||
public Optional<CompletableFuture<ResourcePackRequest>> stagingRequest(String tag) { | ||
return this.stagingPackRequests.getOrDefault(tag, Optional.empty()); | ||
} | ||
|
||
/** | ||
* Build the default list of resource packs | ||
* | ||
* @param urls the list of resource pack urls | ||
* @return the list of resource pack info futures | ||
*/ | ||
private List<CompletableFuture<ResourcePackInfo>> buildDefaultList(List<String> urls) { | ||
List<CompletableFuture<ResourcePackInfo>> packs = new ArrayList<>(); | ||
|
||
for (String url : urls) { | ||
buildPackInfo(url).ifPresent(packInfo -> { | ||
packs.add(packInfo); | ||
}); | ||
} | ||
|
||
return packs; | ||
} | ||
|
||
private CompletableFuture<ResourcePackRequest> buildDefaultRequest(List<CompletableFuture<ResourcePackInfo>> packInfos) { | ||
return this.buildRequestFuture(packInfos).whenCompleteAsync((request, e) -> { | ||
if (e == null) { | ||
this.paradisu.logger().info("Successfully loaded the default resource packs"); | ||
} else { | ||
this.paradisu.logger().error("Failed to build default request: " + e.getMessage()); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Build the pack info for a resource pack | ||
* | ||
* @param url the url of the resource pack | ||
* @return the pack info future | ||
*/ | ||
private Optional<CompletableFuture<ResourcePackInfo>> buildPackInfo(String url) { | ||
try { | ||
return Optional.of(ResourcePackInfo.resourcePackInfo().uri(new URI(url)).computeHashAndBuild()); | ||
} catch (URISyntaxException e) { | ||
this.paradisu.logger().error("Failed to load pack: " + url + ": " + e.getMessage()); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
/** | ||
* Build the request future for a resource pack | ||
* | ||
* @param packInfo the pack info future | ||
* @return the request future | ||
*/ | ||
private CompletableFuture<ResourcePackRequest> buildRequestFuture(CompletableFuture<ResourcePackInfo> packInfo) { | ||
return packInfo.thenApply(info -> { | ||
return ResourcePackRequest.resourcePackRequest().packs(info).build(); | ||
}); | ||
} | ||
|
||
/** | ||
* Build the request future for a list of resource packs | ||
* | ||
* @param packInfos the list of pack info futures | ||
* @return the request future | ||
*/ | ||
private CompletableFuture<ResourcePackRequest> buildRequestFuture(List<CompletableFuture<ResourcePackInfo>> packInfos) { | ||
@SuppressWarnings("unchecked") | ||
CompletableFuture<ResourcePackInfo>[] packFutures = packInfos.toArray(new CompletableFuture[0]); | ||
|
||
return CompletableFuture.allOf(packFutures).thenApply(v -> { | ||
List<ResourcePackInfo> packs = new ArrayList<>(); | ||
|
||
for (CompletableFuture<ResourcePackInfo> packFuture : packFutures) { | ||
try { | ||
packs.add(packFuture.get()); | ||
} catch (InterruptedException | ExecutionException e) { | ||
this.paradisu.logger().error("Failed to load pack: " + e.getMessage()); | ||
} | ||
|
||
} | ||
return ResourcePackRequest.resourcePackRequest().packs(packs).build(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,65 @@ | ||
[versions] | ||
dotenv-kotlin = "6.4.1" | ||
mariadb-java-client = "3.3.3" | ||
mysql-connector-j = "8.3.0" | ||
cloud = "1.8.4" | ||
inventorygui = "1.6.2-SNAPSHOT" | ||
paper-api = "1.20.4-R0.1-SNAPSHOT" | ||
protocollib = "5.2.0-SNAPSHOT" | ||
luckperms-api = "5.4" | ||
configurate-yaml = "4.1.2" | ||
adventure = "4.17.0-SNAPSHOT" | ||
adventure-platform = "4.3.3-SNAPSHOT" | ||
blossom = "1.2.0" | ||
cloud = "1.8.4" | ||
configurate-yaml = "4.1.2" | ||
connectorplugin = "1.5.1-SNAPSHOT" | ||
dotenv-kotlin = "6.4.1" | ||
fastutil = "8.5.2" | ||
guava = "31.1-jre" | ||
log4j-slf4j-impl = "2.23.1" | ||
velocity-api = "3.3.0-SNAPSHOT" | ||
shadow = "8.1.1" | ||
blossom = "1.2.0" | ||
indra = "3.1.3" | ||
inventorygui = "1.6.2-SNAPSHOT" | ||
log4j-slf4j-impl = "2.23.1" | ||
lombok = "8.6" | ||
luckperms-api = "5.4" | ||
mariadb-java-client = "3.3.3" | ||
mysql-connector-j = "8.3.0" | ||
paper-api = "1.20.4-R0.1-SNAPSHOT" | ||
protocollib = "5.2.0-SNAPSHOT" | ||
shadow = "8.1.1" | ||
velocity-api = "3.3.0-SNAPSHOT" | ||
|
||
[libraries] | ||
dotenv-kotlin = { group = "io.github.cdimascio", name = "dotenv-kotlin", version.ref = "dotenv-kotlin" } | ||
mariadb-java-client = { group = "org.mariadb.jdbc", name = "mariadb-java-client", version.ref = "mariadb-java-client" } | ||
mysql-connector-j = { group = "com.mysql", name = "mysql-connector-j", version.ref = "mysql-connector-j" } | ||
adventure-api = { group = "net.kyori", name = "adventure-api", version.ref = "adventure" } | ||
adventure-platform-bukkit = { group = "net.kyori", name = "adventure-platform-bukkit", version.ref = "adventure-platform" } | ||
adventure-text-minimessage = { group = "net.kyori", name = "adventure-text-minimessage", version.ref = "adventure" } | ||
cloud-core = { group = "cloud.commandframework", name = "cloud-core", version.ref = "cloud" } | ||
cloud-minecraft-extras = { group = "cloud.commandframework", name = "cloud-minecraft-extras", version.ref = "cloud" } | ||
cloud-paper = { group = "cloud.commandframework", name = "cloud-paper", version.ref = "cloud" } | ||
cloud-velocity = { group = "cloud.commandframework", name = "cloud-velocity", version.ref = "cloud" } | ||
cloud-minecraft-extras = { group = "cloud.commandframework", name = "cloud-minecraft-extras", version.ref = "cloud" } | ||
inventorygui = { group = "de.themoep", name = "inventorygui", version.ref = "inventorygui" } | ||
paper-api = { group = "io.papermc.paper", name = "paper-api", version.ref = "paper-api" } | ||
protocollib = { group = "com.comphenix.protocol", name = "ProtocolLib", version.ref = "protocollib" } | ||
luckperms-api = { group = "net.luckperms", name = "api", version.ref = "luckperms-api" } | ||
configurate-yaml = { group = "org.spongepowered", name = "configurate-yaml", version.ref = "configurate-yaml" } | ||
adventure-api = { group = "net.kyori", name = "adventure-api", version.ref = "adventure" } | ||
adventure-text-minimessage = { group = "net.kyori", name = "adventure-text-minimessage", version.ref = "adventure" } | ||
adventure-platform-bukkit = { group = "net.kyori", name = "adventure-platform-bukkit", version.ref = "adventure-platform" } | ||
guava = { group = "com.google.guava", name = "guava", version.ref = "guava" } | ||
velocity-api = { group = "com.velocitypowered", name = "velocity-api", version.ref = "velocity-api" } | ||
connectorplugin-bukkit = { group = "de.themoep.connectorplugin", name = "bukkit", version.ref = "connectorplugin" } | ||
connectorplugin-core = { group = "de.themoep.connectorplugin", name = "core", version.ref = "connectorplugin" } | ||
connectorplugin-velocity = { group = "de.themoep.connectorplugin", name = "velocity", version.ref = "connectorplugin" } | ||
connectorplugin-bukkit = { group = "de.themoep.connectorplugin", name = "bukkit", version.ref = "connectorplugin" } | ||
dotenv-kotlin = { group = "io.github.cdimascio", name = "dotenv-kotlin", version.ref = "dotenv-kotlin" } | ||
fastutil-object-object-maps = { group = "com.nukkitx.fastutil", name = "fastutil-object-object-maps", version.ref = "fastutil" } | ||
guava = { group = "com.google.guava", name = "guava", version.ref = "guava" } | ||
inventorygui = { group = "de.themoep", name = "inventorygui", version.ref = "inventorygui" } | ||
log4j-slf4j-impl = { group = "org.apache.logging.log4j", name = "log4j-slf4j-impl", version.ref = "log4j-slf4j-impl" } | ||
luckperms-api = { group = "net.luckperms", name = "api", version.ref = "luckperms-api" } | ||
mariadb-java-client = { group = "org.mariadb.jdbc", name = "mariadb-java-client", version.ref = "mariadb-java-client" } | ||
mysql-connector-j = { group = "com.mysql", name = "mysql-connector-j", version.ref = "mysql-connector-j" } | ||
paper-api = { group = "io.papermc.paper", name = "paper-api", version.ref = "paper-api" } | ||
protocollib = { group = "com.comphenix.protocol", name = "ProtocolLib", version.ref = "protocollib" } | ||
shadow = { group = "com.github.johnrengelman", name = "shadow", version.ref = "shadow" } | ||
velocity-api = { group = "com.velocitypowered", name = "velocity-api", version.ref = "velocity-api" } | ||
|
||
[bundles] | ||
cloud-velocity = ["cloud-velocity", "cloud-minecraft-extras"] | ||
cloud-paper = ["cloud-paper", "cloud-minecraft-extras"] | ||
cloud-core = ["cloud-core", "cloud-minecraft-extras"] | ||
adventure-core = ["adventure-api", "adventure-text-minimessage"] | ||
adventure-velocity = ["adventure-api", "adventure-text-minimessage"] | ||
adventure-bukkit = [ | ||
"adventure-api", | ||
"adventure-text-minimessage", | ||
"adventure-platform-bukkit", | ||
] | ||
adventure-core = ["adventure-api", "adventure-text-minimessage"] | ||
adventure-velocity = ["adventure-api", "adventure-text-minimessage"] | ||
cloud-core = ["cloud-core", "cloud-minecraft-extras"] | ||
cloud-paper = ["cloud-paper", "cloud-minecraft-extras"] | ||
cloud-velocity = ["cloud-velocity", "cloud-minecraft-extras"] | ||
fastutil = ["fastutil-object-object-maps"] | ||
|
||
[plugins] | ||
shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" } | ||
blossom = { id = "net.kyori.blossom", version.ref = "blossom" } | ||
indra-git = { id = "net.kyori.indra.git", version.ref = "indra" } | ||
lombok = { id = "io.freefair.lombok", version.ref = "lombok" } | ||
shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.