generated from Slimefun/Addon-Template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
3 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
51 changes: 51 additions & 0 deletions
51
...in/java/io/github/thebusybiscuit/mobcapturer/utils/compatibility/VillagerProfessionX.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,51 @@ | ||
package io.github.thebusybiscuit.mobcapturer.utils.compatibility; | ||
|
||
import io.github.thebusybiscuit.mobcapturer.MobCapturer; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.entity.ZombieVillager; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Locale; | ||
import java.util.logging.Level; | ||
|
||
// TODO: This needs to be changed since 1.22 the enum methods will be removed | ||
@UtilityClass | ||
public final class VillagerProfessionX { | ||
|
||
@Nonnull | ||
public static String getFromZombieVillager(@Nonnull ZombieVillager entity) { | ||
try { | ||
// get the profession of the zombie villager | ||
var getProfMethod = entity.getClass().getDeclaredMethod("getVillagerProfession"); | ||
Object prof = getProfMethod.invoke(entity); | ||
|
||
var getKeyMethod = prof.getClass().getDeclaredMethod("getKey"); | ||
var nsKey = (NamespacedKey) getKeyMethod.invoke(prof); | ||
return nsKey.getKey().toUpperCase(Locale.ROOT); | ||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
MobCapturer.getInstance().getLogger().log(Level.SEVERE, e, () -> "An error occurred while trying to get the profession of a ZombieVillager"); | ||
return "Unknown"; | ||
} | ||
} | ||
|
||
public static void setToZombieVillager(@Nonnull ZombieVillager entity, @Nonnull String profession) { | ||
try { | ||
// get the profession of the zombie villager | ||
var getProfMethod = entity.getClass().getDeclaredMethod("getVillagerProfession"); | ||
Object prof = getProfMethod.invoke(entity); | ||
|
||
var valueOfMethod = prof.getClass().getDeclaredMethod("valueOf", String.class); | ||
Object newProf = valueOfMethod.invoke(prof, profession); | ||
|
||
var setProfMethod = entity.getClass().getDeclaredMethod("setVillagerProfession", prof.getClass()); | ||
setProfMethod.invoke(entity, newProf); | ||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
// Ignore | ||
} | ||
} | ||
} |