mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-17 11:24:56 -04:00
Rewriting ResourceLocation and rearranging some methods
This commit is contained in:
parent
4fee62885f
commit
dd9e06ea69
@ -16,23 +16,62 @@ import de.bixilon.minosoft.data.language.translate.Translatable
|
|||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
open class ResourceLocation(
|
/**
|
||||||
|
* A resource location is a string that identifies a resource. It is composed of a namespace and a path, separated by a colon (:).
|
||||||
|
* The namespace is optional and defaults to "minecraft". The path is the path to the resource, separated by forward slashes (/).
|
||||||
|
*
|
||||||
|
* @param namespace The namespace of the resource location
|
||||||
|
* @param path The path of the resource location
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException If the namespace or path does not match the allowed pattern. See [ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN] and [ProtocolDefinition.ALLOWED_PATH_PATTERN]
|
||||||
|
*
|
||||||
|
* @see <a href="https://minecraft.fandom.com/wiki/Resource_location">Resource location</a>
|
||||||
|
*/
|
||||||
|
open class ResourceLocation private constructor (
|
||||||
val namespace: String = ProtocolDefinition.DEFAULT_NAMESPACE,
|
val namespace: String = ProtocolDefinition.DEFAULT_NAMESPACE,
|
||||||
val path: String,
|
val path: String
|
||||||
) : Comparable<ResourceLocation>, Translatable { // compare is for moshi
|
) : Comparable<ResourceLocation>, Translatable { // compare is for moshi
|
||||||
private val hashCode = Objects.hash(namespace, path)
|
private val hashCode = Objects.hash(namespace, path)
|
||||||
open val full: String = "$namespace:$path"
|
|
||||||
|
|
||||||
override val translationKey: ResourceLocation
|
override val translationKey: ResourceLocation
|
||||||
get() = this
|
get() = this
|
||||||
|
|
||||||
constructor(full: String) : this(full.namespace, full.path)
|
init {
|
||||||
|
if (!ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches(namespace))
|
||||||
|
throw IllegalArgumentException("Namespace '$namespace' is not allowed!")
|
||||||
|
if (!ProtocolDefinition.ALLOWED_PATH_PATTERN.matches(path))
|
||||||
|
throw IllegalArgumentException("Path '$path' is not allowed!")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a prefix to the path of this resource location.
|
||||||
|
*
|
||||||
|
* @param prefix The prefix to add
|
||||||
|
*/
|
||||||
|
fun prefix(prefix: String): ResourceLocation {
|
||||||
|
return ResourceLocation(namespace, prefix + path)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return If the namespace is "minecraft", the path is returned. Otherwise, the full string is returned.
|
||||||
|
*/
|
||||||
|
fun toMinifiedString(): String {
|
||||||
|
return if (namespace == ProtocolDefinition.DEFAULT_NAMESPACE) path
|
||||||
|
else toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "$namespace:$path"
|
||||||
|
}
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
override fun hashCode(): Int {
|
||||||
return hashCode
|
return hashCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun compareTo(other: ResourceLocation): Int {
|
||||||
|
return hashCode() - other.hashCode()
|
||||||
|
}
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
if (other === this) {
|
if (other === this) {
|
||||||
return true
|
return true
|
||||||
@ -46,17 +85,6 @@ open class ResourceLocation(
|
|||||||
return path == other.path && namespace == other.namespace
|
return path == other.path && namespace == other.namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
return full
|
|
||||||
}
|
|
||||||
|
|
||||||
fun toMinifiedString(): String {
|
|
||||||
if (namespace == ProtocolDefinition.DEFAULT_NAMESPACE) {
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
return toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val String.namespace: String
|
val String.namespace: String
|
||||||
get() {
|
get() {
|
||||||
@ -77,23 +105,12 @@ open class ResourceLocation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getResourceLocation(resourceLocation: String): ResourceLocation {
|
fun getResourceLocation(resourceLocation: String): ResourceLocation {
|
||||||
return ResourceLocation(resourceLocation)
|
return ResourceLocation(resourceLocation.namespace, resourceLocation.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getPathResourceLocation(resourceLocation: String): ResourceLocation {
|
fun getResourceLocation(namespace: String, path: String): ResourceLocation {
|
||||||
if (resourceLocation.contains(":")) {
|
return ResourceLocation(namespace, path)
|
||||||
return ResourceLocation(resourceLocation)
|
|
||||||
}
|
|
||||||
val split = resourceLocation.split("/".toRegex(), 2).toTypedArray()
|
|
||||||
return ResourceLocation(split[0], split[1])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun prefix(prefix: String): ResourceLocation {
|
|
||||||
return ResourceLocation(namespace, prefix + path)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun compareTo(other: ResourceLocation): Int {
|
|
||||||
return hashCode() - other.hashCode()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user