From db72d5bc38101862ce47cb07a693f427996090f9 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 15 Feb 2016 22:33:49 -0200 Subject: [PATCH] Bugfixes, two more goals implemented. --- src/me/TheBukor/SkStuff/SkStuff.java | 4 +- .../SkStuff/effects/EffRemovePathGoal.java | 7 + .../SkStuff/effects/EffSetPathGoal.java | 166 +++++++++++------- 3 files changed, 111 insertions(+), 66 deletions(-) diff --git a/src/me/TheBukor/SkStuff/SkStuff.java b/src/me/TheBukor/SkStuff/SkStuff.java index 694c27f..4093fd4 100644 --- a/src/me/TheBukor/SkStuff/SkStuff.java +++ b/src/me/TheBukor/SkStuff/SkStuff.java @@ -98,8 +98,8 @@ public class SkStuff extends JavaPlugin { getLogger().info("Trying to register version specific stuff..."); Skript.registerEffect(EffClearPathGoals.class, "(clear|delete) [all] pathfind[er] goals (of|from) %livingentity%"); - Skript.registerEffect(EffRemovePathGoal.class, "remove pathfind[er] goal (0¦(avoid|run away from) entit(y|ies)|1¦break door[s]|2¦breed|3¦eat grass|4¦(flee from the sun|seek shad(e|ow))|5¦float (in[side]|on) water|6¦follow (owner|tamer)|7¦follow (adult|parent)[s]|8¦(fight back|react to|target) (damager|attacker)|9¦o(c|z)elot jump on blocks|10¦leap at target|11¦look at entit(y|ies)|12¦melee attack entit(y|ies)|13¦move to[wards] target|14¦attack nearest entity|15¦o(c|z)elot attack [chicken[s]]|16¦open door[s]|17¦(panic|flee)|18¦look around randomly|19¦(walk around randomly|wander)|20¦sit|21¦[creeper] swell|22¦squid (swim|wander)|23¦shoot fireball[s]|24¦[silverfish] hide (in[side]|on) block[s]|25¦(wake other silverfish[es]|[silverfish] call (help|reinforcement|other [hidden] silverfish[es]))|26¦[enderm(a|e)n] pick[[ ]up] block[s]|27¦[enderm(a|e)n] place block[s]|28¦[enderman] attack player (staring|looking) [at eye[s]]|29¦ghast move to[wards] target|30¦ghast (idle move[ment]|wander|random fl(ight|y[ing]))) from %livingentity%"); - Skript.registerEffect(EffSetPathGoal.class, "add pathfind[er] goal [[with] priority %-integer%] (0¦(avoid|run away from) %entitydata%,[ ]radius %number%,[ ][at] speed %number%,[ ][at] speed if close %number%|1¦break door[s]|2¦breed,[ ][move to[wards] lover at] speed %number%|3¦eat grass|4¦(flee from the sun|seek shad(e|ow)),[ ][at] speed %number%|5¦float (in[side]|on) water|6¦follow (owner|tamer),[ ][at] speed %number%|7¦follow (adult|parent)[s],[ ][at] speed %number%|8¦(fight back|react to|target) (damager|attacker) [[of] type] %entitydata%|9¦o(c|z)elot jump on blocks,[ ][at] speed %number%|10¦leap at target [with [leap]] height %number%|11¦look at %entitydata%,[ ]radius %number%|12¦melee attack %entitydata%,[ ][at] speed %number%|13¦move to[wards] target,[ ][at] speed %number%,[ ]radius %number%|14¦attack nearest [entity [of] type] %entitydata%|15¦o(c|z)elot attack [chicken]|16¦open door[s]|17¦(panic|flee),[ ][at] speed %number%|18¦look around randomly|19¦(walk around randomly|wander),[ ][at] speed %number%,[ ][with] min[imum] [of] %-timespan% between mov(e[ment][s]|ing)|20¦sit|21¦[creeper] swell|22¦squid (swim|wander)|23¦shoot fireball[s]|24¦[silverfish] hide (in[side]|on) block[s]|25¦(wake other silverfish[es]|[silverfish] call (help|reinforcement|other [hidden] silverfish[es]))|26¦[enderm(a|e)n] pick[[ ]up] block[s]|27¦[enderm(a|e)n] place block[s]|28¦[enderman] attack player (staring|looking) [at eye[s]]|29¦ghast move to[wards] target|30¦ghast (idle move[ment]|wander|random fl(ight|y[ing]))) to %livingentity%"); + Skript.registerEffect(EffRemovePathGoal.class, "remove pathfind[er] goal (0¦(avoid|run away from) entit(y|ies)|1¦break door[s]|2¦breed|3¦eat grass|4¦(flee from the sun|seek shad(e|ow))|5¦float (in[side]|on) water|6¦follow (owner|tamer)|7¦follow (adult|parent)[s]|8¦(fight back|react to|target) (damager|attacker)|9¦o(c|z)elot jump on blocks|10¦leap at target|11¦look at entit(y|ies)|12¦melee attack entit(y|ies)|13¦move to[wards] target|14¦attack nearest entity|15¦o(c|z)elot attack [chicken[s]]|16¦open door[s]|17¦(panic|flee)|18¦look around randomly|19¦(walk around randomly|wander)|20¦sit|21¦[creeper] (explode|inflate|swell)|22¦squid (swim|wander)|23¦shoot fireball[s]|24¦[silverfish] hide (in[side]|on) block[s]|25¦(wake other silverfish[es]|[silverfish] call (help|reinforcement|other [hidden] silverfish[es]))|26¦[enderm(a|e)n] pick[[ ]up] block[s]|27¦[enderm(a|e)n] place block[s]|28¦[enderman] attack player (staring|looking) [at eye[s]]|29¦ghast move to[wards] target|30¦ghast (idle move[ment]|wander|random fl(ight|y[ing]))|31¦(tempt to|follow players (holding|with)) [a[n]] item|32¦target [random] entity (if|when) (not tamed|untamed)) from %livingentity%"); + Skript.registerEffect(EffSetPathGoal.class, "add pathfind[er] goal [[with] priority %-integer%] (0¦(avoid|run away from) %entitydata%,[ ]radius %number%,[ ][at] speed %number%|1¦break door[s]|2¦breed,[ ][move to[wards] lover at] speed %number%|3¦eat grass|4¦(flee from the sun|seek shad(e|ow)),[ ][at] speed %number%|5¦float (in[side]|on) water|6¦follow (owner|tamer),[ ][at] speed %number%|7¦follow (adult|parent)[s],[ ][at] speed %number%|8¦(fight back|react to|target) (damager|attacker) [[of] type] %entitydata%|9¦o(c|z)elot jump on blocks,[ ][at] speed %number%|10¦leap at target [with [leap]] height %number%|11¦look at %entitydata%,[ ]radius %number%|12¦melee attack %entitydata%,[ ][at] speed %number%|13¦move to[wards] target,[ ][at] speed %number%,[ ]radius %number%|14¦attack nearest [entity [of] type] %entitydata%|15¦o(c|z)elot attack [chicken]|16¦open door[s]|17¦(panic|flee),[ ][at] speed %number%|18¦look around randomly|19¦(walk around randomly|wander),[ ][at] speed %number%,[ ][with] min[imum] [of] %-timespan% between mov(e[ment][s]|ing)|20¦sit|21¦[creeper] (explode|inflate|swell)|22¦squid (swim|wander)|23¦shoot fireball[s]|24¦[silverfish] hide (in[side]|on) block[s]|25¦(wake other silverfish[es]|[silverfish] call (help|reinforcement|other [hidden] silverfish[es]))|26¦[enderm(a|e)n] pick[[ ]up] block[s]|27¦[enderm(a|e)n] place block[s]|28¦[enderman] attack player (staring|looking) [at eye[s]]|29¦ghast move to[wards] target|30¦ghast (idle move[ment]|wander|random fl(ight|y[ing]))|31¦(tempt to|follow players (holding|with)) %-itemstack%, [at] speed %number%|32¦target [random] %entitydata% (if|when) (not tamed|untamed)) to %livingentity%"); Skript.registerEffect(EffMakeJump.class, "make %livingentities% jump", "force %livingentities% to jump"); Skript.registerEffect(EffShowEntityEffect.class, "(display|play|show) entity effect (0¦firework[s] explo(de|sion)|1¦hurt|2¦[[iron] golem] (give|offer) (rose|poppy)|3¦[sheep] eat grass|4¦wolf shake) at %entity%"); Skript.registerExpression(ExprNBTOf.class, Object.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %entity/block/itemstack%", "%entity/block/itemstack%'s nbt[[ ]tag[s]]"); diff --git a/src/me/TheBukor/SkStuff/effects/EffRemovePathGoal.java b/src/me/TheBukor/SkStuff/effects/EffRemovePathGoal.java index 107e21e..a408ca1 100644 --- a/src/me/TheBukor/SkStuff/effects/EffRemovePathGoal.java +++ b/src/me/TheBukor/SkStuff/effects/EffRemovePathGoal.java @@ -169,6 +169,13 @@ public class EffRemovePathGoal extends Effect { } else if (mark == 30) { Class goalGhastIdleMove = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastIdleMove", false); toRemove = goalGhastIdleMove; + } else if (mark == 31) { + Class goalTempt = ReflectionUtils.getNMSClass("PathfinderGoalTempt", false); + toRemove = goalTempt; + } else if (mark == 32) { + target = true; + Class goalTargetNonTamed = ReflectionUtils.getNMSClass("PathfinderGoalRandomTargetNonTamed", false); + toRemove = goalTargetNonTamed; } if (toRemove == null) return; diff --git a/src/me/TheBukor/SkStuff/effects/EffSetPathGoal.java b/src/me/TheBukor/SkStuff/effects/EffSetPathGoal.java index 663e502..61676d8 100644 --- a/src/me/TheBukor/SkStuff/effects/EffSetPathGoal.java +++ b/src/me/TheBukor/SkStuff/effects/EffSetPathGoal.java @@ -4,7 +4,7 @@ import java.lang.reflect.Constructor; import javax.annotation.Nullable; -import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.entity.Animals; import org.bukkit.entity.Blaze; import org.bukkit.entity.Creeper; @@ -18,6 +18,10 @@ import org.bukkit.entity.Spider; import org.bukkit.entity.Squid; import org.bukkit.entity.Tameable; import org.bukkit.event.Event; +import org.bukkit.inventory.ItemStack; + +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; import ch.njol.skript.entity.EntityData; import ch.njol.skript.lang.Effect; @@ -31,8 +35,7 @@ public class EffSetPathGoal extends Effect { private Expression goalPriority; private Expression> typeToAvoid; private Expression avoidRadius; - private Expression avoidSpeed1; - private Expression avoidSpeed2; + private Expression avoidSpeed; private Expression breedSpeed; private Expression fleeSunSpeed; private Expression followOwnerSpeed; @@ -50,15 +53,20 @@ public class EffSetPathGoal extends Effect { private Expression panicSpeed; private Expression randomWalkSpeed; private Expression randomWalkInterval; + private Expression temptItem; + private Expression temptSpeed; + private Expression> nonTamedTarget; private Expression entity; private int mark; + private Class craftItemClass = ReflectionUtils.getOBCClass("inventory.CraftItemStack"); private Class craftLivEnt = ReflectionUtils.getOBCClass("entity.CraftLivingEntity"); private Class entAnimal = ReflectionUtils.getNMSClass("EntityAnimal", false); private Class entCreature = ReflectionUtils.getNMSClass("EntityCreature", false); private Class entInsent = ReflectionUtils.getNMSClass("EntityInsentient", false); private Class entTameable = ReflectionUtils.getNMSClass("EntityTameableAnimal", false); + private Class nmsItemClass = ReflectionUtils.getNMSClass("Item", false); @SuppressWarnings("unchecked") @Override @@ -68,40 +76,44 @@ public class EffSetPathGoal extends Effect { if (mark == 0) { typeToAvoid = (Expression>) expr[1]; avoidRadius = (Expression) expr[2]; - avoidSpeed1 = (Expression) expr[3]; - avoidSpeed2 = (Expression) expr[4]; + avoidSpeed = (Expression) expr[3]; } else if (mark == 2) { - breedSpeed = (Expression) expr[5]; + breedSpeed = (Expression) expr[4]; } else if (mark == 4) { - fleeSunSpeed = (Expression) expr[6]; + fleeSunSpeed = (Expression) expr[5]; } else if (mark == 6) { - followOwnerSpeed = (Expression) expr[7]; + followOwnerSpeed = (Expression) expr[6]; } else if (mark == 7) { - followAdultsSpeed = (Expression) expr[8]; + followAdultsSpeed = (Expression) expr[7]; } else if (mark == 8) { - typesToFightBack = (Expression>) expr[9]; + typesToFightBack = (Expression>) expr[8]; } else if (mark == 9) { - jumpOnBlockSpeed = (Expression) expr[10]; + jumpOnBlockSpeed = (Expression) expr[9]; } else if (mark == 10) { - leapHeight = (Expression) expr[11]; + leapHeight = (Expression) expr[10]; } else if (mark == 11) { - lookType = (Expression>) expr[12]; - lookRadius = (Expression) expr[13]; + lookType = (Expression>) expr[11]; + lookRadius = (Expression) expr[12]; } else if (mark == 12) { - meleeTarget = (Expression>) expr[14]; - meleeSpeed = (Expression) expr[15]; + meleeTarget = (Expression>) expr[13]; + meleeSpeed = (Expression) expr[14]; } else if (mark == 13) { - moveTargetSpeed = (Expression) expr[16]; - moveTargetRadius = (Expression) expr[17]; + moveTargetSpeed = (Expression) expr[15]; + moveTargetRadius = (Expression) expr[16]; } else if (mark == 14) { - nearTarget = (Expression>) expr[18]; + nearTarget = (Expression>) expr[17]; } else if (mark == 17) { - panicSpeed = (Expression) expr[19]; + panicSpeed = (Expression) expr[18]; } else if (mark == 19) { - randomWalkSpeed = (Expression) expr[20]; - randomWalkInterval = (Expression) expr[21]; + randomWalkSpeed = (Expression) expr[19]; + randomWalkInterval = (Expression) expr[20]; + } else if (mark == 31) { + temptItem = (Expression) expr[21]; + temptSpeed = (Expression) expr[22]; + } else if (mark == 32) { + nonTamedTarget = (Expression>) expr[23]; } - entity = (Expression) expr[22]; + entity = (Expression) expr[24]; return true; } @@ -138,8 +150,7 @@ public class EffSetPathGoal extends Effect { if (mark == 0) { Class goalAvoid = ReflectionUtils.getNMSClass("PathfinderGoalAvoidTarget", false); float radius = avoidRadius.getSingle(e).floatValue(); - double spd1 = avoidSpeed1.getSingle(e).doubleValue(); - double spd2 = avoidSpeed2.getSingle(e).doubleValue(); + double spd = avoidSpeed.getSingle(e).doubleValue(); EntityData entityData; String exprInput = typeToAvoid.toString(e, false); if (exprInput.startsWith("the ")) { @@ -152,11 +163,13 @@ public class EffSetPathGoal extends Effect { String className = entityData.getType().getSimpleName(); if (className.equals("HumanEntity")) className = "Human"; + else if (className.equals("LivingEntity")) + className = "Living"; className = "Entity" + className; Class nmsClass = ReflectionUtils.getNMSClass(className, false); if (nmsClass == null) return; - newGoal = goalAvoid.getConstructor(entCreature, Class.class, float.class, double.class, double.class).newInstance(nmsEnt, nmsClass, radius, spd1, spd2); + newGoal = goalAvoid.getConstructor(entCreature, Class.class, float.class, double.class, double.class).newInstance(nmsEnt, nmsClass, radius, spd, spd); } else if (mark == 1) { Class goalBreakDoor = ReflectionUtils.getNMSClass("PathfinderGoalBreakDoor", false); newGoal = goalBreakDoor.getConstructor(entInsent).newInstance(nmsEnt); @@ -202,11 +215,13 @@ public class EffSetPathGoal extends Effect { String className = entityData.getType().getSimpleName(); if (className.equals("HumanEntity")) className = "Human"; + else if (className.equals("LivingEntity")) + className = "Living"; className = "Entity" + className; Class[] nmsClass = new Class[] { ReflectionUtils.getNMSClass(className, false) }; if (nmsClass[0] == null) return; - newGoal = goalReactAttack.getConstructor(entCreature, boolean.class, Class[].class).newInstance(nmsEnt, false, nmsClass[0]); + newGoal = goalReactAttack.getConstructor(entCreature, boolean.class, Class[].class).newInstance(nmsEnt, false, nmsClass); } else if (mark == 9) { Class goalJumpOnBlock = ReflectionUtils.getNMSClass("PathfinderGoalJumpOnBlock", false); double spd = jumpOnBlockSpeed.getSingle(e).doubleValue(); @@ -230,6 +245,8 @@ public class EffSetPathGoal extends Effect { String className = entityData.getType().getSimpleName(); if (className.equals("HumanEntity")) className = "Human"; + else if (className.equals("LivingEntity")) + className = "Living"; className = "Entity" + className; Class nmsClass = ReflectionUtils.getNMSClass(className, false); if (nmsClass == null) @@ -245,34 +262,22 @@ public class EffSetPathGoal extends Effect { entityData = EntityData.parseWithoutIndefiniteArticle(exprInput); if (!LivingEntity.class.isAssignableFrom(entityData.getType())) return; + String className = entityData.getType().getSimpleName(); + if (className.equals("HumanEntity")) + className = "Human"; + else if (className.equals("LivingEntity")) + className = "Living"; + className = "Entity" + className; + Class nmsClass = ReflectionUtils.getNMSClass(className, false); + if (nmsClass == null) + return; if (ent instanceof Spider) { - Class[] classes = nmsEnt.getClass().getDeclaredClasses(); - Class clazz = null; - for (Class c : classes) { - Bukkit.broadcastMessage("Class name: \u00A7e" + c.getName()); - if (c.getName().equals("PathfinderGoalSpiderMeleeAttack")) - clazz = c; - } - Constructor[] constructors = clazz.getDeclaredConstructors(); - Constructor constr = null; - for (Constructor constructor : constructors) { - Bukkit.broadcastMessage("Constructor name: \u00A7a" + constructor.getName()); - if (constructor.getName().equals("PathfinderGoalSpiderMeleeAttack")) - constr = constructor; - } - if (constr == null) - return; + Class goalSpiderMelee = ReflectionUtils.getNMSClass("EntitySpider$PathfinderGoalSpiderMeleeAttack", false); + Constructor constr = goalSpiderMelee.getDeclaredConstructor(nmsEnt.getClass(), Class.class); constr.setAccessible(true); - newGoal = constr.newInstance(nmsEnt, entityData); + newGoal = constr.newInstance(nmsEnt, nmsClass); } else { Class goalMeleeAttack = ReflectionUtils.getNMSClass("PathfinderGoalMeleeAttack", false); - String className = entityData.getType().getSimpleName(); - if (className.equals("HumanEntity")) - className = "Human"; - className = "Entity" + className; - Class nmsClass = ReflectionUtils.getNMSClass(className, false); - if (nmsClass == null) - return; double spd = meleeSpeed.getSingle(e).doubleValue(); newGoal = goalMeleeAttack.getConstructor(entCreature, Class.class, double.class, boolean.class).newInstance(nmsEnt, nmsClass, spd, false); } @@ -291,20 +296,22 @@ public class EffSetPathGoal extends Effect { entityData = EntityData.parseWithoutIndefiniteArticle(exprInput); if (!LivingEntity.class.isAssignableFrom(entityData.getType())) return; + String className = entityData.getType().getSimpleName(); + if (className.equals("HumanEntity")) + className = "Human"; + else if (className.equals("LivingEntity")) + className = "Living"; + className = "Entity" + className; + Class nmsClass = ReflectionUtils.getNMSClass(className, false); + if (nmsClass == null) + return; if (ent instanceof Spider) { Class goalSpiderNearTarget = ReflectionUtils.getNMSClass("EntitySpider$PathfinderGoalSpiderNearestAttackableTarget", false); Constructor constr = goalSpiderNearTarget.getDeclaredConstructor(nmsEnt.getClass(), Class.class); constr.setAccessible(true); - newGoal = constr.newInstance(nmsEnt, entityData); + newGoal = constr.newInstance(nmsEnt, nmsClass); } else { Class goalNearTarget = ReflectionUtils.getNMSClass("PathfinderGoalNearestAttackableTarget", false); - String className = entityData.getType().getSimpleName(); - if (className.equals("HumanEntity")) - className = "Human"; - className = "Entity" + className; - Class nmsClass = ReflectionUtils.getNMSClass(className, false); - if (nmsClass == null) - return; newGoal = goalNearTarget.getConstructor(entCreature, Class.class, boolean.class).newInstance(nmsEnt, nmsClass, false); } } else if (mark == 15) { @@ -405,18 +412,49 @@ public class EffSetPathGoal extends Effect { constr.setAccessible(true); newGoal = constr.newInstance(nmsEnt); } else if (mark == 31) { + Class goalTempt = ReflectionUtils.getNMSClass("PathfinderGoalTempt", false); + ItemStack itemStack = temptItem.getSingle(e); + if (itemStack.getType() == Material.AIR || itemStack == null) + return; + Object nmsItemStack = craftItemClass.getMethod("asNMSCopy", ItemStack.class).invoke(itemStack, itemStack); + Object nmsItem = nmsItemStack.getClass().getMethod("getItem").invoke(nmsItemStack); + double spd = temptSpeed.getSingle(e).doubleValue(); + newGoal = goalTempt.getConstructor(entCreature, double.class, nmsItemClass, boolean.class).newInstance(nmsEnt, spd, nmsItem, false); + } else if (mark == 32) { + target = true; + Class goalTargetNonTamed = ReflectionUtils.getNMSClass("PathfinderGoalRandomTargetNonTamed", false); + if (!(ent instanceof Tameable)) + return; + EntityData entityData; + String exprInput = nonTamedTarget.toString(e, false); + if (exprInput.startsWith("the ")) { + exprInput = exprInput.substring(4); + } + entityData = EntityData.parseWithoutIndefiniteArticle(exprInput); + if (!LivingEntity.class.isAssignableFrom(entityData.getType())) { + return; + } + String className = entityData.getType().getSimpleName(); + if (className.equals("HumanEntity")) + className = "Human"; + else if (className.equals("LivingEntity")) + className = "Living"; + className = "Entity" + className; + Class nmsClass = ReflectionUtils.getNMSClass(className, false); + if (nmsClass == null) + return; + newGoal = goalTargetNonTamed.getConstructor(entTameable, Class.class, boolean.class, Predicate.class).newInstance(nmsEnt, nmsClass, false, Predicates.alwaysTrue()); + } else if (mark == 33) { + // TODO: Add more goal/target selectors /* Classes that have their own pathfinder goals: + * Guardian, 1 goal, guardian attack. * Rabbit, 3 goals, 2 adapted copies, 1 new (eat carrot crops) * Slime, 4 goals, random jump, go to near player, go in random direction and idle. * ZPigMan, 2 goals, anger and anger other (adapted HurtByTarget to work with Anger tag) */ - - /* Goals to add: - * Tempt - Mob follows you with a certain item in hand (e.g cow follows wheat) - * AttackNonTamed - Used by ocelots, to attack chickens (maybe for wolves to attack sheep to?) - */ + } if (newGoal == null) return;