Update AI

This commit is contained in:
sky 2020-04-16 21:58:15 +08:00
parent e784fe7fca
commit 6b81874bee
2 changed files with 115 additions and 3 deletions

View File

@ -28,10 +28,32 @@ public abstract class PathfinderExecutor {
public abstract void setPathEntity(LivingEntity entity, Object pathEntity);
@Deprecated
public abstract void setGoalAi(LivingEntity entity, SimpleAi ai, int priority);
@Deprecated
public abstract void setTargetAi(LivingEntity entity, SimpleAi ai, int priority);
public abstract void addGoalAi(LivingEntity entity, SimpleAi ai, int priority);
public abstract void addTargetAi(LivingEntity entity, SimpleAi ai, int priority);
public abstract void replaceGoalAi(LivingEntity entity, SimpleAi ai, int priority);
public abstract void replaceTargetAi(LivingEntity entity, SimpleAi ai, int priority);
public abstract void replaceGoalAi(LivingEntity entity, SimpleAi ai, int priority, String name);
public abstract void replaceTargetAi(LivingEntity entity, SimpleAi ai, int priority, String name);
public abstract void removeGoalAi(LivingEntity entity, SimpleAi ai, int priority);
public abstract void removeTargetAi(LivingEntity entity, SimpleAi ai, int priority);
public abstract void removeGoalAi(LivingEntity entity, SimpleAi ai, String name);
public abstract void removeTargetAi(LivingEntity entity, SimpleAi ai, String name);
public abstract void clearGoalAi(LivingEntity entity);
public abstract void clearTargetAi(LivingEntity entity);

View File

@ -1,5 +1,6 @@
package io.izzel.taboolib.module.ai.internal;
import io.izzel.taboolib.Version;
import io.izzel.taboolib.module.ai.PathfinderExecutor;
import io.izzel.taboolib.module.ai.SimpleAi;
import io.izzel.taboolib.module.ai.SimpleAiSelector;
@ -12,6 +13,7 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
/**
@ -25,6 +27,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
private Field pathEntity;
private Field pathfinderGoalSelectorSet;
private Field controllerJumpCurrent;
private boolean v11400 = Version.isAfter(Version.v1_14);
public InternalPathfinderExecutor() {
try {
@ -104,14 +107,102 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override
public void setGoalAi(LivingEntity entity, SimpleAi ai, int priority) {
((EntityInsentient) getEntityInsentient(entity)).goalSelector.a(priority, (PathfinderGoal) SimpleAiSelector.getCreator().createPathfinderGoal(ai));
addGoalAi(entity, ai, priority);
}
@Override
public void setTargetAi(LivingEntity entity, SimpleAi ai, int priority) {
addTargetAi(entity, ai, priority);
}
@Override
public void addGoalAi(LivingEntity entity, SimpleAi ai, int priority) {
((EntityInsentient) getEntityInsentient(entity)).goalSelector.a(priority, (PathfinderGoal) SimpleAiSelector.getCreator().createPathfinderGoal(ai));
}
@Override
public void addTargetAi(LivingEntity entity, SimpleAi ai, int priority) {
((EntityInsentient) getEntityInsentient(entity)).targetSelector.a(priority, (PathfinderGoal) SimpleAiSelector.getCreator().createPathfinderGoal(ai));
}
@Override
public void replaceGoalAi(LivingEntity entity, SimpleAi ai, int priority) {
replaceGoalAi(entity, ai, priority, null);
}
@Override
public void replaceTargetAi(LivingEntity entity, SimpleAi ai, int priority) {
replaceTargetAi(entity, ai, priority, null);
}
@Override
public void replaceGoalAi(LivingEntity entity, SimpleAi ai, int priority, String name) {
if (name == null) {
removeGoal(priority, ((EntityInsentient) getEntityInsentient(entity)).goalSelector);
} else {
removeGoal(name, ((EntityInsentient) getEntityInsentient(entity)).goalSelector);
}
addGoalAi(entity, ai, priority);
}
@Override
public void replaceTargetAi(LivingEntity entity, SimpleAi ai, int priority, String name) {
if (name == null) {
removeGoal(priority, ((EntityInsentient) getEntityInsentient(entity)).targetSelector);
} else {
removeGoal(name, ((EntityInsentient) getEntityInsentient(entity)).targetSelector);
}
addTargetAi(entity, ai, priority);
}
@Override
public void removeGoalAi(LivingEntity entity, SimpleAi ai, int priority) {
removeGoal(priority, ((EntityInsentient) getEntityInsentient(entity)).goalSelector);
}
@Override
public void removeTargetAi(LivingEntity entity, SimpleAi ai, int priority) {
removeGoal(priority, ((EntityInsentient) getEntityInsentient(entity)).targetSelector);
}
@Override
public void removeGoalAi(LivingEntity entity, SimpleAi ai, String name) {
removeGoal(name, ((EntityInsentient) getEntityInsentient(entity)).goalSelector);
}
@Override
public void removeTargetAi(LivingEntity entity, SimpleAi ai, String name) {
removeGoal(name, ((EntityInsentient) getEntityInsentient(entity)).targetSelector);
}
private void removeGoal(String name, Object targetSelector) {
Collection c = getGoal(targetSelector);
for (Object element : new ArrayList<>(c)) {
if (SimpleReflection.getFieldValueChecked(element.getClass(), element, "a", true).getClass().getName().contains(name)) {
c.remove(element);
}
}
}
private void removeGoal(int priority, Object targetSelector) {
Collection c = getGoal(targetSelector);
for (Object element : new ArrayList<>(c)) {
if ((int) SimpleReflection.getFieldValueChecked(element.getClass(), element, "b", true) == priority) {
c.remove(element);
}
}
}
private Collection getGoal(Object targetSelector) {
Collection c;
if (v11400) {
c = (Collection) SimpleReflection.getFieldValueChecked(PathfinderGoalSelector.class, targetSelector, "b", true);
} else {
c = (Collection) SimpleReflection.getFieldValueChecked(PathfinderGoalSelector.class, targetSelector, "d", true);
}
return c;
}
@Override
public void clearGoalAi(LivingEntity entity) {
try {
@ -162,8 +253,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override
public void setTargetAi(LivingEntity entity, Iterable ai) {
try {
Ref.putField(((EntityInsentient) getEntityInsentient(entity)).targetSelector,
this.pathfinderGoalSelectorSet, ai);
Ref.putField(((EntityInsentient) getEntityInsentient(entity)).targetSelector, this.pathfinderGoalSelectorSet, ai);
} catch (Throwable t) {
t.printStackTrace();
}