Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PotionData NPE #6757

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions src/main/java/ch/njol/skript/util/PotionEffectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,20 +346,7 @@ else if (HAS_SUSPICIOUS_META && meta instanceof SuspiciousStewMeta)
itemType.setItemMeta(meta);
}

@Nullable
private static final MethodHandle BASE_POTION_DATA_HANDLE;

static {
MethodHandle basePotionDataHandle = null;
if (Skript.methodExists(PotionMeta.class, "getBasePotionData")) {
try {
basePotionDataHandle = MethodHandles.lookup().findVirtual(PotionMeta.class, "getBasePotionData", MethodType.methodType(PotionData.class));
} catch (NoSuchMethodException | IllegalAccessException e) {
Skript.exception(e, "Failed to load legacy potion data support. Potions may not work as expected.");
}
}
BASE_POTION_DATA_HANDLE = basePotionDataHandle;
}
private static final boolean HAS_POTION_TYPE = Skript.methodExists(PotionMeta.class, "getBasePotionType");

/**
* Get all the PotionEffects of an ItemType
Expand All @@ -375,20 +362,20 @@ public static List<PotionEffect> getEffects(ItemType itemType) {
if (meta instanceof PotionMeta) {
PotionMeta potionMeta = ((PotionMeta) meta);
effects.addAll(potionMeta.getCustomEffects());
if (BASE_POTION_DATA_HANDLE != null) {
try {
effects.addAll(PotionDataUtils.getPotionEffects((PotionData) BASE_POTION_DATA_HANDLE.invoke(meta)));
} catch (Throwable e) {
throw Skript.exception(e, "An error occurred while trying to invoke legacy potion data support.");
if (HAS_POTION_TYPE) {
if (potionMeta.hasBasePotionType()) {
//noinspection ConstantConditions - checked via hasBasePotionType
effects.addAll(potionMeta.getBasePotionType().getPotionEffects());
}
} else { // use deprecated method
PotionData data = potionMeta.getBasePotionData();
if (data != null) {
effects.addAll(PotionDataUtils.getPotionEffects(data));
}
} else if (potionMeta.hasBasePotionType()) {
//noinspection ConstantConditions - checked via hasBasePotionType
effects.addAll(potionMeta.getBasePotionType().getPotionEffects());
}

} else if (HAS_SUSPICIOUS_META && meta instanceof SuspiciousStewMeta)
effects.addAll(((SuspiciousStewMeta) meta).getCustomEffects());
return effects;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test "potion missing base type":

assert potion effects of (plain potion of mundane) is not set with "it should not have any effects"
Loading