mirror of
https://github.com/PlayPro/CoreProtect.git
synced 2026-10-28 09:44:56 +00:00
Added refreshBrain reflection (fixes #905)
This commit is contained in:
1 parent
6d60951cb9
commit
6d1ddb8580
3 files changed
+37
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package net.coreprotect.bukkit;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -25,6 +26,7 @@ import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.ItemFrame;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Painting;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.block.BlockExplodeEvent;
|
||||
import org.bukkit.event.block.SignChangeEvent;
|
||||
@@ -126,6 +128,17 @@ public class BukkitAdapter implements BukkitInterface {
|
||||
public void setMerchantRecipeMeta(MerchantRecipe recipe, List<?> recipeData) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshVillagerBrain(Villager villager) {
|
||||
try {
|
||||
Object handle = villager.getClass().getMethod("getHandle").invoke(villager);
|
||||
Object level = villager.getWorld().getClass().getMethod("getHandle").invoke(villager.getWorld());
|
||||
invokeSingleArgumentMethod(handle, "refreshBrain", level);
|
||||
}
|
||||
catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getWolfVariant(org.bukkit.entity.Wolf wolf, List<Object> info) {
|
||||
// Base implementation does nothing - Wolf variants only exist in 1.21+
|
||||
@@ -195,6 +208,24 @@ public class BukkitAdapter implements BukkitInterface {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
private static void invokeSingleArgumentMethod(Object target, String methodName, Object argument) {
|
||||
if (target == null || argument == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Method method : target.getClass().getMethods()) {
|
||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
if (method.getName().equals(methodName) && parameterTypes.length == 1 && parameterTypes[0].isInstance(argument)) {
|
||||
try {
|
||||
method.invoke(target, argument);
|
||||
}
|
||||
catch (Exception e) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------- Block methods --------------------
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Painting;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.block.BlockExplodeEvent;
|
||||
import org.bukkit.event.block.SignChangeEvent;
|
||||
@@ -300,6 +301,8 @@ public interface BukkitInterface {
|
||||
|
||||
void setMerchantRecipeMeta(MerchantRecipe recipe, List<?> recipeData);
|
||||
|
||||
void refreshVillagerBrain(Villager villager);
|
||||
|
||||
/**
|
||||
* Gets the wolf variant and adds it to the info list.
|
||||
* Only implemented in Minecraft 1.21+.
|
||||
|
||||
@@ -623,6 +623,9 @@ public class EntityUtil {
|
||||
}
|
||||
count++;
|
||||
}
|
||||
if (entity instanceof Villager) {
|
||||
BukkitAdapter.ADAPTER.refreshVillagerBrain((Villager) entity);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in new issue
Block a user