Skip to content

Commit

Permalink
fix(spigot): language selector not opening on Spigot 1.21.1
Browse files Browse the repository at this point in the history
This was caused by a change in the PatternType bukkit class, which was
changed from an Enum to an Interface.
Since the inner code of Triton is not remapped by bukkit, the plugin
threw an error trying to call the valueOf method.
  • Loading branch information
diogotcorreia committed Aug 21, 2024
1 parent bd70339 commit 98c5619
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
4 changes: 2 additions & 2 deletions triton-spigot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies {
compileOnly project(':core')
implementation project(':spigot-legacy')

compileOnly 'org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT'
compileOnly 'org.spigotmc:spigot-api:1.21.1-R0.1-SNAPSHOT'

compileOnly 'net.kyori:adventure-text-serializer-gson:4.15.0'
compileOnly 'net.kyori:adventure-text-serializer-legacy:4.15.0'
Expand All @@ -34,7 +34,7 @@ dependencies {
// Test dependencies
testImplementation project(":api")

testImplementation 'org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT'
testImplementation 'org.spigotmc:spigot-api:1.21.1-R0.1-SNAPSHOT'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.rexcantor64.triton.spigot.banners;

import lombok.Getter;
import lombok.val;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.block.banner.PatternType;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Locale;

@Getter
public enum Patterns {
Expand Down Expand Up @@ -50,6 +57,18 @@ public enum Patterns {
private final char code;
private final String[] typeAliases;

private final static @Nullable Method valueOfMethod;

static {
@Nullable Method method;
try {
method = PatternType.class.getMethod("valueOf", String.class);
} catch (NoSuchMethodException e) {
method = null;
}
valueOfMethod = method;
}

Patterns(char code, String... typeAliases) {
this.code = code;
this.typeAliases = typeAliases;
Expand All @@ -63,10 +82,43 @@ public static Patterns getByCode(char code) {
}

public PatternType toPatternType() {
try {
// For 1.21.1 and above
return toPatternTypeFromRegistry();
} catch (NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError ignore) {
// BANNER_PATTERN Registry does not exist in this Spigot version
}
// Fallback to enum's valueOf
return toPatternTypeFromEnum();
}

/**
* Gets the pattern type from Bukkit's registry.
* This is the correct way to get pattern types since Spigot 1.21.1.
*
* @return The Bukkit PatternType that corresponds to this type.
*/
private PatternType toPatternTypeFromRegistry() {
for (String alias : this.typeAliases) {
val type = Registry.BANNER_PATTERN.get(NamespacedKey.fromString(alias.toLowerCase(Locale.ROOT)));
if (type != null) {
return type;
}
}
throw new IllegalArgumentException("Cannot find corresponding pattern type to " + Arrays.toString(this.typeAliases));
}

/**
* Gets the pattern type from the PatternType enum itself.
* This only works up to Spigot 1.21.0.
*
* @return The Bukkit PatternType that corresponds to this type.
*/
private PatternType toPatternTypeFromEnum() {
for (String alias : this.typeAliases) {
try {
return PatternType.valueOf(alias);
} catch (IllegalArgumentException ignore) {
return (PatternType) valueOfMethod.invoke(null, alias);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException ignore) {
}
}
throw new IllegalArgumentException("Cannot find corresponding pattern type to " + Arrays.toString(this.typeAliases));
Expand Down

0 comments on commit 98c5619

Please sign in to comment.