Skip to content

Commit

Permalink
fix: Support RegionPlugin UltimateClaims v2 and v1
Browse files Browse the repository at this point in the history
With v2 the package changed because Songoda renamed to Craftaro at one point in time.

This fixes a `ClassNotFoundException` when using v2 of the plugin.
The commit also refactors the code a bit as the `UltimateClaims#getInstance()` method is marked as deprecated
and will be removed in the future.
  • Loading branch information
SpraxDev committed Feb 25, 2024
1 parent dbde32d commit ea8231e
Showing 1 changed file with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@

import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.plugin.java.JavaPlugin;

import java.lang.reflect.Method;

public class RTP_UltimateClaims implements RegionPluginCheck {

// NOT TESTED (3.1.0)
// UltimateClaims (v1.6.1) - Abandoned
// https://songoda.com/marketplace/product/ultimateclaims-the-ultimate-claiming-plugin.65
// TESTED (v2.2.0)
// UltimateClaims (v2.2.0 + v1.10.4)
// https://craftaro.com/marketplace/product/65
public boolean check(Location loc) {
boolean result = true;
if (REGIONPLUGINS.ULTIMATECLAIMS.isEnabled())
try {
Chunk chunk = loc.getChunk();

// Get instance of UltimateClaims
Class<?> ultimateClaimsClass = Class.forName("com.songoda.ultimateclaims.UltimateClaims");
Method getInstanceMethod = ultimateClaimsClass.getMethod("getInstance");
Object ultimateClaims = getInstanceMethod.invoke(null);
JavaPlugin ultimateClaimsInstance = JavaPlugin.getPlugin((Class<? extends JavaPlugin>) getPluginMainClass());

// Get the ClaimManager
Method getClaimManagerMethod = ultimateClaimsClass.getMethod("getClaimManager");
Object claimManager = getClaimManagerMethod.invoke(ultimateClaims);
Method getClaimManagerMethod = ultimateClaimsInstance.getClass().getMethod("getClaimManager");
Object claimManager = getClaimManagerMethod.invoke(ultimateClaimsInstance);

// Get the claim based on the chunk
Method getClaimMethod = claimManager.getClass().getMethod("getClaim", Chunk.class);
Object claimObj = getClaimMethod.invoke(claimManager, chunk);

// Check if a claim exists
return claimObj == null;
Method hasClaimMethod = claimManager.getClass().getMethod("hasClaim", Chunk.class);
return Boolean.FALSE.equals(hasClaimMethod.invoke(claimManager, loc.getChunk()));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

private Class<?> getPluginMainClass() throws ClassNotFoundException {
try {
return Class.forName("com.craftaro.ultimateclaims.UltimateClaims"); // v2
} catch (ClassNotFoundException ignore) {
return Class.forName("com.songoda.ultimateclaims.UltimateClaims"); // v1
}
}
}

0 comments on commit ea8231e

Please sign in to comment.