Hopefuly should work fine.

Updated to 1.9, moving from Reflection to a new method using and
Interface with useful NMS methods (some expressions are still using
Reflection).
This commit is contained in:
Richard 2016-03-03 22:01:19 -03:00
parent f3fcb59fa1
commit b0aa11475e
23 changed files with 4900 additions and 4238 deletions

View File

@ -9,5 +9,6 @@
<classpathentry kind="lib" path="C:/Users/Rubi/AppData/Roaming/.minecraft/workspace/Spigot 1.8, 1.8.3/1.7.10.jar"/>
<classpathentry kind="lib" path="C:/Users/Rubi/AppData/Roaming/.minecraft/workspace/Spigot 1.8, 1.8.3/1.8.3.jar"/>
<classpathentry kind="lib" path="C:/Users/Rubi/AppData/Roaming/.minecraft/workspace/Spigot 1.8, 1.8.3/1.8.jar"/>
<classpathentry kind="lib" path="C:/Users/Rubi/AppData/Roaming/.minecraft/workspace/Spigot 1.8, 1.8.3/1.9.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -1,6 +1,6 @@
name: SkStuff
author: TheBukor
description: A Skript addon which adds extra functionalities such as NBT and extended WorldEdit support.
version: 1.5.1
version: 1.6
main: me.TheBukor.SkStuff.SkStuff
softdepend: [Skript, WorldEdit]

View File

@ -69,6 +69,7 @@ import me.TheBukor.SkStuff.util.NMS_v1_7_R4;
import me.TheBukor.SkStuff.util.NMS_v1_8_R1;
import me.TheBukor.SkStuff.util.NMS_v1_8_R2;
import me.TheBukor.SkStuff.util.NMS_v1_8_R3;
import me.TheBukor.SkStuff.util.NMS_v1_9_R1;
import me.TheBukor.SkStuff.util.ReflectionUtils;
public class SkStuff extends JavaPlugin {
@ -78,8 +79,6 @@ public class SkStuff extends JavaPlugin {
private int exprAmount = 0;
private int typeAmount = 0;
public static SkStuff instance;
private static NMSInterface nmsMethods;
public void onEnable() {
@ -103,7 +102,7 @@ public class SkStuff extends JavaPlugin {
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¦target 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)|33¦guardian attack [entity]|34¦[z[ombie[ ]]pig[man]] attack [player[s]] (if|when) angry|35¦[z[ombie[ ]]pig[man]] (react to|fight back|target) (attacker|damager) (if|when) angry|36¦[rabbit] eat carrot crops|37¦[killer] rabbit [melee] attack|38¦slime [random] jump|39¦slime change (direction|facing) randomly|40¦slime (idle move[ment]|wander)) from %livingentity%");
Skript.registerEffect(EffSetPathGoal.class, "add pathfind[er] goal [[with] priority %-integer%] (0¦(avoid|run away from) %entitydata%[, radius %-number%[, speed %-number%[, speed (if|when) (close|near) %-number%]]]|1¦break door[s]|2¦breed[,[move[ment]] speed %-number%]|3¦eat grass|4¦(flee from the sun|seek shad(e|ow))[, [move[ment]] speed %-number%]|5¦(float (in[side]|on) water|swim)|6¦follow (owner|tamer)[, speed %-number%[, min[imum] distance %-number%[, max[imum] distance %-number%]]]|7¦follow (adult|parent)[s][, [move[ment]] speed %-number%]|8¦(fight back|react to|target) (damager|attacker) [[of] type] %entitydata%[, call ([for] help|reinforcement) %-boolean%]|9¦o(c|z)elot jump on blocks[, [move[ment]] speed %-number%]|10¦leap at target[, [leap] height %-number%]|11¦look at %entitydata%[, (radius|max[imum] distance) %-number%]|12¦melee attack %entitydata%[, [move[ment]] speed %-number%[, (memorize|do('nt| not) forget) target [for [a] long[er] time] %-boolean%]]|13¦move to[wards] target[, [move[ment]] speed %-number%[, (radius|max[imum] distance) %-number%]]|14¦target nearest [entity [of] type] %entitydata%[, check sight %-boolean%]|15¦o(c|z)elot attack|16¦open door[s]|17¦(panic|flee)[, [move[ment]] speed %-number%]|18¦look around randomly|19¦(walk around randomly|wander)[, [move[ment]] speed %-number%[, min[imum] [of] %-timespan% between mov(e[ment][s]|ing)]]|20¦sit|21¦[creeper] (explode|inflate|swell)|22¦squid (swim around|wander)|23¦shoot fireball[s]|24¦[silverfish] hide (in[side]|on) block[s]|25¦((call|summon|wake) [other] [hidden] silverfish[es])|26¦[enderman] pick[[ ]up] block[s]|27¦[enderman] place block[s]|28¦[enderman] attack player (staring|looking) at [their] 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%[, [move[ment]] speed %number%[, scared of player movement %-boolean%]]|32¦target [random] %entitydata% (if|when) (not |un)tamed|33¦guardian attack [entities]|34¦[z[ombie[ ]]pig[man]] attack [player[s]] (if|when) angry|35¦[z[ombie[ ]]pig[man]] (react to|fight back|target) (attacker|damager) (if|when) angry|36¦[rabbit] eat carrot crops|37¦[killer] rabbit [melee] attack|38¦slime [random] jump|39¦slime change (direction|facing) randomly|40¦slime (idle move[ment]|wander)) to %livingentity%");
Skript.registerEffect(EffMakeJump.class, "make %livingentities% jump", "force %livingentities% to jump");
Skript.registerEffect(EffGZipFile.class, "create [a] gzip[ped] file [at] %string%");
Skript.registerEffect(EffGZipFile.class, "create [a] gzip[ped] file [at] [path] %string%");
Skript.registerExpression(ExprNBTOf.class, Object.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %~object%", "%~object%'s nbt[[ ]tag[s]]");
Skript.registerExpression(ExprItemNBT.class, ItemStack.class, ExpressionType.SIMPLE, "%itemstack% with [custom] nbt[[ ]tag[s]] %string%");
Skript.registerExpression(ExprTagOf.class, Object.class, ExpressionType.PROPERTY, "[nbt[ ]]tag %string% of [[nbt] compound] %compound%");
@ -199,16 +198,15 @@ public class SkStuff extends JavaPlugin {
} else if (version.equals("v1_8_R3.")) {
nmsMethods = new NMS_v1_8_R3();
getLogger().info("It looks like you're either running 1.8.7, 1.8.8 or 1.8.9!");
} else if (version.equals("v1_9_R1.")) {
nmsMethods = new NMS_v1_9_R1();
getLogger().info("It looks like you're running 1.9.0!");
} else {
getLogger().warning("It looks like you're running an unsupported server version, some features will not be available :(");
}
return nmsMethods != null;
}
public static SkStuff getInstance() {
return instance;
}
public static NMSInterface getNMSMethods() {
return nmsMethods;
}

View File

@ -17,8 +17,8 @@ import me.TheBukor.SkStuff.util.ReflectionUtils;
public class EffClearPathGoals extends Effect {
private Expression<LivingEntity> entity;
private Class<?> goalSelectorClass = ReflectionUtils.getNMSClass("PathfinderGoalSelector", false);
private Class<?> insentientEnt = ReflectionUtils.getNMSClass("EntityInsentient", false);
private Class<?> goalSelectorClass = ReflectionUtils.getNMSClass("PathfinderGoalSelector");
private Class<?> insentientEnt = ReflectionUtils.getNMSClass("EntityInsentient");
private Class<?> craftLivEnt = ReflectionUtils.getOBCClass("entity.CraftLivingEntity");
@SuppressWarnings("unchecked")

View File

@ -28,21 +28,13 @@ public class EffGZipFile extends Effect {
@Override
public String toString(@Nullable Event e, boolean debug) {
return "create GZipped file at " + filePath.toString(e, debug);
return "create GZipped file at path " + filePath.toString(e, debug);
}
@Override
protected void execute(Event e) {
File newFile = new File(filePath.getSingle(e));
/*
if (!newFile.exists()) {
try {
newFile.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
}
*/
try {
new GZIPOutputStream(new FileOutputStream(newFile)).close();
} catch (FileNotFoundException ex) {
@ -54,3 +46,4 @@ public class EffGZipFile extends Effect {
}
}
}
}

View File

@ -17,7 +17,7 @@ import me.TheBukor.SkStuff.util.ReflectionUtils;
public class EffMakeJump extends Effect {
private Expression<LivingEntity> entities;
private Class<?> entInsent = ReflectionUtils.getNMSClass("EntityInsentient", false);
private Class<?> entInsent = ReflectionUtils.getNMSClass("EntityInsentient");
private Class<?> craftLivEnt = ReflectionUtils.getOBCClass("entity.CraftLivingEntity");
@SuppressWarnings("unchecked")

View File

@ -1,8 +1,5 @@
package me.TheBukor.SkStuff.effects;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.bukkit.entity.Blaze;
@ -18,6 +15,7 @@ import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import me.TheBukor.SkStuff.SkStuff;
import me.TheBukor.SkStuff.util.ReflectionUtils;
public class EffRemovePathGoal extends Effect {
@ -25,7 +23,7 @@ public class EffRemovePathGoal extends Effect {
private int mark;
private Class<?> entInsent = ReflectionUtils.getNMSClass("EntityInsentient", false);
private Class<?> entInsent = ReflectionUtils.getNMSClass("EntityInsentient");
private Class<?> craftLivEnt = ReflectionUtils.getOBCClass("entity.CraftLivingEntity");
@SuppressWarnings("unchecked")
@ -49,171 +47,169 @@ public class EffRemovePathGoal extends Effect {
Object obcEnt = craftLivEnt.cast(ent);
try {
Object nmsEnt = entInsent.cast(obcEnt.getClass().getMethod("getHandle").invoke(obcEnt));
Object goalSelector = ReflectionUtils.getField("goalSelector", entInsent, nmsEnt);
Object targetSelector = ReflectionUtils.getField("targetSelector", entInsent, nmsEnt);
Object toRemove = null;
Class<?> toRemove = null;
boolean target = false;
boolean resetGoalTarget = false;
if (mark == 0) {
if (ent instanceof Rabbit) {
Class<?> goalRabbitAvoid = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalRabbitAvoidTarget", false);
Class<?> goalRabbitAvoid = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalRabbitAvoidTarget");
toRemove = goalRabbitAvoid;
} else {
Class<?> goalAvoid = ReflectionUtils.getNMSClass("PathfinderGoalAvoidTarget", false);
Class<?> goalAvoid = ReflectionUtils.getNMSClass("PathfinderGoalAvoidTarget");
toRemove = goalAvoid;
}
} else if (mark == 1) {
Class<?> goalBreakDoor = ReflectionUtils.getNMSClass("PathfinderGoalBreakDoor", false);
Class<?> goalBreakDoor = ReflectionUtils.getNMSClass("PathfinderGoalBreakDoor");
toRemove = goalBreakDoor;
} else if (mark == 2) {
Class<?> goalBreed = ReflectionUtils.getNMSClass("PathfinderGoalBreed", false);
Class<?> goalBreed = ReflectionUtils.getNMSClass("PathfinderGoalBreed");
toRemove = goalBreed;
} else if (mark == 3) {
Class<?> goalEatGrass = ReflectionUtils.getNMSClass("PathfinderGoalEatTile", false);
Class<?> goalEatGrass = ReflectionUtils.getNMSClass("PathfinderGoalEatTile");
toRemove = goalEatGrass;
} else if (mark == 4) {
Class<?> goalFleeSun = ReflectionUtils.getNMSClass("PathfinderGoalFleeSun", false);
Class<?> goalFleeSun = ReflectionUtils.getNMSClass("PathfinderGoalFleeSun");
toRemove = goalFleeSun;
} else if (mark == 5) {
Class<?> goalFloat = ReflectionUtils.getNMSClass("PathfinderGoalFloat", false);
Class<?> goalFloat = ReflectionUtils.getNMSClass("PathfinderGoalFloat");
toRemove = goalFloat;
} else if (mark == 6) {
Class<?> goalFollowOwner = ReflectionUtils.getNMSClass("PathfinderGoalFollowOwner", false);
Class<?> goalFollowOwner = ReflectionUtils.getNMSClass("PathfinderGoalFollowOwner");
toRemove = goalFollowOwner;
} else if (mark == 7) {
Class<?> goalFollowAdults = ReflectionUtils.getNMSClass("PathfinderGoalFollowParent", false);
Class<?> goalFollowAdults = ReflectionUtils.getNMSClass("PathfinderGoalFollowParent");
toRemove = goalFollowAdults;
} else if (mark == 8) {
target = true;
Class<?> goalReactAttack = ReflectionUtils.getNMSClass("PathfinderGoalHurtByTarget", false);
Class<?> goalReactAttack = ReflectionUtils.getNMSClass("PathfinderGoalHurtByTarget");
toRemove = goalReactAttack;
} else if (mark == 9) {
Class<?> goalJumpOnBlock = ReflectionUtils.getNMSClass("PathfinderGoalJumpOnBlock", false);
Class<?> goalJumpOnBlock = ReflectionUtils.getNMSClass("PathfinderGoalJumpOnBlock");
toRemove = goalJumpOnBlock;
} else if (mark == 10) {
Class<?> goalLeapTarget = ReflectionUtils.getNMSClass("PathfinderGoalLeapAtTarget", false);
Class<?> goalLeapTarget = ReflectionUtils.getNMSClass("PathfinderGoalLeapAtTarget");
toRemove = goalLeapTarget;
} else if (mark == 11) {
Class<?> goalLookEntities = ReflectionUtils.getNMSClass("PathfinderGoalLookAtPlayer", false);
Class<?> goalLookEntities = ReflectionUtils.getNMSClass("PathfinderGoalLookAtPlayer");
toRemove = goalLookEntities;
} else if (mark == 12) {
if (ent instanceof Spider) {
Class<?> goalSpiderMelee = ReflectionUtils.getNMSClass("EntitySpider$PathfinderGoalSpiderMeleeAttack", false);
Class<?> goalSpiderMelee = ReflectionUtils.getNMSClass("EntitySpider$PathfinderGoalSpiderMeleeAttack");
toRemove = goalSpiderMelee;
} else {
Class<?> goalMeleeAttack = ReflectionUtils.getNMSClass("PathfinderGoalMeleeAttack", false);
Class<?> goalMeleeAttack = ReflectionUtils.getNMSClass("PathfinderGoalMeleeAttack");
toRemove = goalMeleeAttack;
}
} else if (mark == 13) {
if (ent instanceof Ghast) {
Class<?> goalGhastGotoTarget = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalMoveTowardsTarget", false);
Class<?> goalGhastGotoTarget = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalMoveTowardsTarget");
toRemove = goalGhastGotoTarget;
} else {
Class<?> goalGotoTarget = ReflectionUtils.getNMSClass("PathfinderGoalMoveTowardsTarget", false);
Class<?> goalGotoTarget = ReflectionUtils.getNMSClass("PathfinderGoalMoveTowardsTarget");
toRemove = goalGotoTarget;
}
} else if (mark == 14) {
target = true;
if (ent instanceof Spider) {
Class<?> goalSpiderNearTarget = ReflectionUtils.getNMSClass("EntitySpider$PathfinderGoalSpiderNearestAttackableTarget", false);
Class<?> goalSpiderNearTarget = ReflectionUtils.getNMSClass("EntitySpider$PathfinderGoalSpiderNearestAttackableTarget");
toRemove = goalSpiderNearTarget;
} else {
Class<?> goalNearTarget = ReflectionUtils.getNMSClass("PathfinderGoalNearestAttackableTarget", false);
Class<?> goalNearTarget = ReflectionUtils.getNMSClass("PathfinderGoalNearestAttackableTarget");
toRemove = goalNearTarget;
}
} else if (mark == 15) {
Class<?> goalOcelotAttack = ReflectionUtils.getNMSClass("PathfinderGoalOcelotAttack", false);
Class<?> goalOcelotAttack = ReflectionUtils.getNMSClass("PathfinderGoalOcelotAttack");
toRemove = goalOcelotAttack;
} else if (mark == 16) {
Class<?> goalOpenDoors = ReflectionUtils.getNMSClass("PathfinderGoalOpenDoor", false);
Class<?> goalOpenDoors = ReflectionUtils.getNMSClass("PathfinderGoalOpenDoor");
toRemove = goalOpenDoors;
} else if (mark == 17) {
if (ent instanceof Rabbit) {
Class<?> goalRabbitPanic = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalRabbitPanic", false);
Class<?> goalRabbitPanic = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalRabbitPanic");
toRemove = goalRabbitPanic;
} else {
Class<?> goalPanic = ReflectionUtils.getNMSClass("PathfinderGoalPanic", false);
Class<?> goalPanic = ReflectionUtils.getNMSClass("PathfinderGoalPanic");
toRemove = goalPanic;
}
} else if (mark == 18) {
Class<?> goalRandomLook = ReflectionUtils.getNMSClass("PathfinderGoalRandomLookaround", false);
Class<?> goalRandomLook = ReflectionUtils.getNMSClass("PathfinderGoalRandomLookaround");
toRemove = goalRandomLook;
} else if (mark == 19) {
Class<?> goalWander = ReflectionUtils.getNMSClass("PathfinderGoalRandomStroll", false);
Class<?> goalWander = ReflectionUtils.getNMSClass("PathfinderGoalRandomStroll");
toRemove = goalWander;
} else if (mark == 20) {
Class<?> goalSit = ReflectionUtils.getNMSClass("PathfinderGoalSit", false);
Class<?> goalSit = ReflectionUtils.getNMSClass("PathfinderGoalSit");
toRemove = goalSit;
} else if (mark == 21) {
Class<?> goalSwell = ReflectionUtils.getNMSClass("PathfinderGoalSwell", false);
Class<?> goalSwell = ReflectionUtils.getNMSClass("PathfinderGoalSwell");
toRemove = goalSwell;
} else if (mark == 22) {
Class<?> goalSquid = ReflectionUtils.getNMSClass("EntitySquid$PathfinderGoalSquid", false);
Class<?> goalSquid = ReflectionUtils.getNMSClass("EntitySquid$PathfinderGoalSquid");
toRemove = goalSquid;
} else if (mark == 23) {
resetGoalTarget = true;
if (ent instanceof Blaze) {
Class<?> goalBlazeFireball = ReflectionUtils.getNMSClass("EntityBlaze$PathfinderGoalBlazeFireball", false);
Class<?> goalBlazeFireball = ReflectionUtils.getNMSClass("EntityBlaze$PathfinderGoalBlazeFireball");
toRemove = goalBlazeFireball;
} else if (ent instanceof Ghast) {
Class<?> goalGhastFireball = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastAttackTarget", false);
Class<?> goalGhastFireball = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastAttackTarget");
toRemove = goalGhastFireball;
}
} else if (mark == 24) {
Class<?> goalHideInBlock = ReflectionUtils.getNMSClass("EntitySilverfish$PathfinderGoalSilverfishHideInBlock", false);
Class<?> goalHideInBlock = ReflectionUtils.getNMSClass("EntitySilverfish$PathfinderGoalSilverfishHideInBlock");
toRemove = goalHideInBlock;
} else if (mark == 25) {
Class<?> goalWakeSilverfish = ReflectionUtils.getNMSClass("EntitySilverfish$PathfinderGoalSilverfishWakeOthers", false);
Class<?> goalWakeSilverfish = ReflectionUtils.getNMSClass("EntitySilverfish$PathfinderGoalSilverfishWakeOthers");
toRemove = goalWakeSilverfish;
} else if (mark == 26) {
Class<?> goalPickBlocks = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalEndermanPickupBlock", false);
Class<?> goalPickBlocks = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalEndermanPickupBlock");
toRemove = goalPickBlocks;
} else if (mark == 27) {
Class<?> goalPlaceBlocks = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalEndermanPlaceBlock", false);
Class<?> goalPlaceBlocks = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalEndermanPlaceBlock");
toRemove = goalPlaceBlocks;
} else if (mark == 28) {
target = true;
Class<?> goalAttackLooker = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalPlayerWhoLookedAtTarget", false);
Class<?> goalAttackLooker = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalPlayerWhoLookedAtTarget");
toRemove = goalAttackLooker;
} else if (mark == 29) {
Class<?> goalGhastMoveTarget = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastMoveTowardsTarget", false);
Class<?> goalGhastMoveTarget = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastMoveTowardsTarget");
toRemove = goalGhastMoveTarget;
} else if (mark == 30) {
Class<?> goalGhastIdleMove = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastIdleMove", false);
Class<?> goalGhastIdleMove = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastIdleMove");
toRemove = goalGhastIdleMove;
} else if (mark == 31) {
Class<?> goalTempt = ReflectionUtils.getNMSClass("PathfinderGoalTempt", false);
Class<?> goalTempt = ReflectionUtils.getNMSClass("PathfinderGoalTempt");
toRemove = goalTempt;
} else if (mark == 32) {
target = true;
Class<?> goalTargetNonTamed = ReflectionUtils.getNMSClass("PathfinderGoalRandomTargetNonTamed", false);
Class<?> goalTargetNonTamed = ReflectionUtils.getNMSClass("PathfinderGoalRandomTargetNonTamed");
toRemove = goalTargetNonTamed;
} else if (mark == 33) {
resetGoalTarget = true;
Class<?> goalGuardianAttack = ReflectionUtils.getNMSClass("EntityGuardian$PathfinderGoalGuardianAttack", false);
Class<?> goalGuardianAttack = ReflectionUtils.getNMSClass("EntityGuardian$PathfinderGoalGuardianAttack");
toRemove = goalGuardianAttack;
} else if (mark == 34) {
target = true;
Class<?> goalAnger = ReflectionUtils.getNMSClass("EntityPigZombie$PathfinderGoalAnger", false);
Class<?> goalAnger = ReflectionUtils.getNMSClass("EntityPigZombie$PathfinderGoalAnger");
toRemove = goalAnger;
} else if (mark == 35) {
target = true;
Class<?> goalAngerOther = ReflectionUtils.getNMSClass("EntityPigZombie$PathfinderGoalAngerOther", false);
Class<?> goalAngerOther = ReflectionUtils.getNMSClass("EntityPigZombie$PathfinderGoalAngerOther");
toRemove = goalAngerOther;
} else if (mark == 36) {
Class<?> goalEatCarrots = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalEatCarrots", false);
Class<?> goalEatCarrots = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalEatCarrots");
toRemove = goalEatCarrots;
} else if (mark == 37) {
Class<?> goalRabbitAttack = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalKillerRabbitMeleeAttack", false);
Class<?> goalRabbitAttack = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalKillerRabbitMeleeAttack");
toRemove = goalRabbitAttack;
} else if (mark == 38) {
Class<?> goalJump = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeRandomJump", false);
Class<?> goalJump = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeRandomJump");
toRemove = goalJump;
} else if (mark == 39) {
Class<?> goalRandomDir = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeRandomDirection", false);
Class<?> goalRandomDir = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeRandomDirection");
toRemove = goalRandomDir;
} else if (mark == 40) {
Class<?> goalSlimeWander = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeIdle", false);
Class<?> goalSlimeWander = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeIdle");
toRemove = goalSlimeWander;
}
if (toRemove == null)
@ -231,24 +227,7 @@ public class EffRemovePathGoal extends Effect {
((Creature) entity.getSingle(e)).setTarget(null);
}
Class<?> goalSelectorClass = ReflectionUtils.getNMSClass("PathfinderGoalSelector", false);
if (target) { //Target Selector
Iterator<?> targets = ((List<?>) ReflectionUtils.getField("b", goalSelectorClass, targetSelector)).iterator();
while (targets.hasNext()) {
Object o = targets.next();
if (ReflectionUtils.getField("a", o.getClass(), o).getClass() == toRemove) {
targets.remove();
}
}
} else { //Goal Selector
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", goalSelectorClass, goalSelector)).iterator();
while (goals.hasNext()) {
Object o = goals.next();
if (ReflectionUtils.getField("a", o.getClass(), o).getClass() == toRemove) {
goals.remove();
}
}
}
SkStuff.getNMSMethods().removePathfinderGoal(nmsEnt, toRemove, target);
} catch (Exception ex) {
ex.printStackTrace();
}

View File

@ -31,8 +31,10 @@ import ch.njol.skript.entity.EntityData;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.localization.Language;
import ch.njol.skript.util.Timespan;
import ch.njol.util.Kleenean;
import me.TheBukor.SkStuff.SkStuff;
import me.TheBukor.SkStuff.util.ReflectionUtils;
public class EffSetPathGoal extends Effect {
@ -73,11 +75,11 @@ public class EffSetPathGoal extends Effect {
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);
private Class<?> entAnimal = ReflectionUtils.getNMSClass("EntityAnimal");
private Class<?> entCreature = ReflectionUtils.getNMSClass("EntityCreature");
private Class<?> entInsent = ReflectionUtils.getNMSClass("EntityInsentient");
private Class<?> entTameable = ReflectionUtils.getNMSClass("EntityTameableAnimal");
private Class<?> nmsItemClass = ReflectionUtils.getNMSClass("Item");
@SuppressWarnings("unchecked")
@Override
@ -163,9 +165,8 @@ public class EffSetPathGoal extends Effect {
boolean target = false;
Object newGoal = null;
nmsEnt = entInsent.cast(obcEnt.getClass().getMethod("getHandle").invoke(obcEnt));
Object goals = ReflectionUtils.getField("goalSelector", entInsent, nmsEnt);
Object targets = ReflectionUtils.getField("targetSelector", entInsent, nmsEnt);
if (mark == 0) {
boolean wasLocal = Language.setUseLocal(false);
float radius = 6.0F;
double spd = 1.0D;
double nearSpd = 1.2D;
@ -180,7 +181,11 @@ public class EffSetPathGoal extends Effect {
if (exprInput.startsWith("the ")) {
exprInput = exprInput.substring(4);
}
try {
entityData = EntityData.parseWithoutIndefiniteArticle(exprInput);
} finally {
Language.setUseLocal(wasLocal);
}
if (!LivingEntity.class.isAssignableFrom(entityData.getType())) {
return;
}
@ -190,21 +195,21 @@ public class EffSetPathGoal extends Effect {
else if (className.equals("LivingEntity"))
className = "Living";
className = "Entity" + className;
Class<?> nmsClass = ReflectionUtils.getNMSClass(className, false);
Class<?> nmsClass = ReflectionUtils.getNMSClass(className);
if (nmsClass == null)
return;
if (ent instanceof Rabbit) {
Class<?> goalRabbitAvoid = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalRabbitAvoidTarget", false);
Class<?> goalRabbitAvoid = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalRabbitAvoidTarget");
newGoal = goalRabbitAvoid.getDeclaredConstructor(nmsEnt.getClass(), Class.class, float.class, double.class, double.class).newInstance(nmsEnt, nmsClass, radius, spd, nearSpd);
} else {
Class<?> goalAvoid = ReflectionUtils.getNMSClass("PathfinderGoalAvoidTarget", false);
Class<?> goalAvoid = ReflectionUtils.getNMSClass("PathfinderGoalAvoidTarget");
newGoal = goalAvoid.getConstructor(entCreature, Class.class, float.class, double.class, double.class).newInstance(nmsEnt, nmsClass, radius, spd, nearSpd);
}
} else if (mark == 1) {
Class<?> goalBreakDoor = ReflectionUtils.getNMSClass("PathfinderGoalBreakDoor", false);
Class<?> goalBreakDoor = ReflectionUtils.getNMSClass("PathfinderGoalBreakDoor");
newGoal = goalBreakDoor.getConstructor(entInsent).newInstance(nmsEnt);
} else if (mark == 2) {
Class<?> goalBreed = ReflectionUtils.getNMSClass("PathfinderGoalBreed", false);
Class<?> goalBreed = ReflectionUtils.getNMSClass("PathfinderGoalBreed");
double spd = 1.0D;
if (breedSpeed != null)
spd = breedSpeed.getSingle(e).doubleValue();
@ -212,19 +217,19 @@ public class EffSetPathGoal extends Effect {
return;
newGoal = goalBreed.getConstructor(entAnimal, double.class).newInstance(nmsEnt, spd);
} else if (mark == 3) {
Class<?> goalEatGrass = ReflectionUtils.getNMSClass("PathfinderGoalEatTile", false);
Class<?> goalEatGrass = ReflectionUtils.getNMSClass("PathfinderGoalEatTile");
newGoal = goalEatGrass.getConstructor(entInsent).newInstance(nmsEnt);
} else if (mark == 4) {
Class<?> goalFleeSun = ReflectionUtils.getNMSClass("PathfinderGoalFleeSun", false);
Class<?> goalFleeSun = ReflectionUtils.getNMSClass("PathfinderGoalFleeSun");
double spd = 1.0D;
if (fleeSunSpeed != null)
spd = fleeSunSpeed.getSingle(e).doubleValue();
newGoal = goalFleeSun.getConstructor(entCreature, double.class).newInstance(nmsEnt, spd);
} else if (mark == 5) {
Class<?> goalFloat = ReflectionUtils.getNMSClass("PathfinderGoalFloat", false);
Class<?> goalFloat = ReflectionUtils.getNMSClass("PathfinderGoalFloat");
newGoal = goalFloat.getConstructor(entInsent).newInstance(nmsEnt);
} else if (mark == 6) {
Class<?> goalFollowOwner = ReflectionUtils.getNMSClass("PathfinderGoalFollowOwner", false);
Class<?> goalFollowOwner = ReflectionUtils.getNMSClass("PathfinderGoalFollowOwner");
double spd = 1.0D;
if (followOwnerSpeed != null)
spd = followOwnerSpeed.getSingle(e).doubleValue();
@ -238,7 +243,7 @@ public class EffSetPathGoal extends Effect {
return;
newGoal = goalFollowOwner.getConstructor(entTameable, double.class, float.class, float.class).newInstance(nmsEnt, spd, maxDist, minDist);
} else if (mark == 7) {
Class<?> goalFollowAdults = ReflectionUtils.getNMSClass("PathfinderGoalFollowParent", false);
Class<?> goalFollowAdults = ReflectionUtils.getNMSClass("PathfinderGoalFollowParent");
double spd = 1.0D;
if (followAdultsSpeed != null)
spd = followAdultsSpeed.getSingle(e).doubleValue();
@ -246,15 +251,20 @@ public class EffSetPathGoal extends Effect {
return;
newGoal = goalFollowAdults.getConstructor(entAnimal, double.class).newInstance(nmsEnt, spd);
} else if (mark == 8) {
boolean wasLocal = Language.setUseLocal(false);
target = true;
boolean callHelp = false;
Class<?> goalReactAttack = ReflectionUtils.getNMSClass("PathfinderGoalHurtByTarget", false);
Class<?> goalReactAttack = ReflectionUtils.getNMSClass("PathfinderGoalHurtByTarget");
EntityData<?> entityData;
String exprInput = typesToFightBack.toString(e, false);
if (exprInput.startsWith("the ")) {
exprInput = exprInput.substring(4);
}
try {
entityData = EntityData.parseWithoutIndefiniteArticle(exprInput);
} finally {
Language.setUseLocal(wasLocal);
}
if (!LivingEntity.class.isAssignableFrom(entityData.getType()))
return;
String className = entityData.getType().getSimpleName();
@ -263,14 +273,14 @@ public class EffSetPathGoal extends Effect {
else if (className.equals("LivingEntity"))
className = "Living";
className = "Entity" + className;
Class<?>[] nmsClass = new Class<?>[] { ReflectionUtils.getNMSClass(className, false) };
Class<?>[] nmsClass = new Class<?>[] { ReflectionUtils.getNMSClass(className) };
if (nmsClass[0] == null)
return;
if (callForHelp != null)
callHelp = callForHelp.getSingle(e);
newGoal = goalReactAttack.getConstructor(entCreature, boolean.class, Class[].class).newInstance(nmsEnt, callHelp, nmsClass);
} else if (mark == 9) {
Class<?> goalJumpOnBlock = ReflectionUtils.getNMSClass("PathfinderGoalJumpOnBlock", false);
Class<?> goalJumpOnBlock = ReflectionUtils.getNMSClass("PathfinderGoalJumpOnBlock");
double spd = 1.0D;
if (jumpOnBlockSpeed != null)
spd = jumpOnBlockSpeed.getSingle(e).doubleValue();
@ -278,19 +288,24 @@ public class EffSetPathGoal extends Effect {
return;
newGoal = goalJumpOnBlock.getConstructor(nmsEnt.getClass(), double.class).newInstance(nmsEnt, spd);
} else if (mark == 10) {
Class<?> goalLeapTarget = ReflectionUtils.getNMSClass("PathfinderGoalLeapAtTarget", false);
Class<?> goalLeapTarget = ReflectionUtils.getNMSClass("PathfinderGoalLeapAtTarget");
float height = 0.4F;
if (leapHeight != null)
height = leapHeight.getSingle(e).floatValue();
newGoal = goalLeapTarget.getConstructor(entInsent, float.class).newInstance(nmsEnt, height);
} else if (mark == 11) {
Class<?> goalLookEntities = ReflectionUtils.getNMSClass("PathfinderGoalLookAtPlayer", false);
boolean wasLocal = Language.setUseLocal(false);
Class<?> goalLookEntities = ReflectionUtils.getNMSClass("PathfinderGoalLookAtPlayer");
EntityData<?> entityData;
String exprInput = lookType.toString(e, false);
if (exprInput.startsWith("the ")) {
exprInput = exprInput.substring(4);
}
try {
entityData = EntityData.parseWithoutIndefiniteArticle(exprInput);
} finally {
Language.setUseLocal(wasLocal);
}
if (!LivingEntity.class.isAssignableFrom(entityData.getType()))
return;
String className = entityData.getType().getSimpleName();
@ -299,7 +314,7 @@ public class EffSetPathGoal extends Effect {
else if (className.equals("LivingEntity"))
className = "Living";
className = "Entity" + className;
Class<?> nmsClass = ReflectionUtils.getNMSClass(className, false);
Class<?> nmsClass = ReflectionUtils.getNMSClass(className);
if (nmsClass == null)
return;
float radius = 1.0F;
@ -307,12 +322,17 @@ public class EffSetPathGoal extends Effect {
radius = lookRadius.getSingle(e).floatValue();
newGoal = goalLookEntities.getConstructor(entInsent, Class.class, float.class).newInstance(nmsEnt, nmsClass, radius);
} else if (mark == 12) {
boolean wasLocal = Language.setUseLocal(false);
EntityData<?> entityData;
String exprInput = meleeTarget.toString(e, false);
if (exprInput.startsWith("the ")) {
exprInput = exprInput.substring(4);
}
try {
entityData = EntityData.parseWithoutIndefiniteArticle(exprInput);
} finally {
Language.setUseLocal(wasLocal);
}
if (!LivingEntity.class.isAssignableFrom(entityData.getType()))
return;
String className = entityData.getType().getSimpleName();
@ -321,16 +341,16 @@ public class EffSetPathGoal extends Effect {
else if (className.equals("LivingEntity"))
className = "Living";
className = "Entity" + className;
Class<?> nmsClass = ReflectionUtils.getNMSClass(className, false);
Class<?> nmsClass = ReflectionUtils.getNMSClass(className);
if (nmsClass == null)
return;
if (ent instanceof Spider) {
Class<?> goalSpiderMelee = ReflectionUtils.getNMSClass("EntitySpider$PathfinderGoalSpiderMeleeAttack", false);
Class<?> goalSpiderMelee = ReflectionUtils.getNMSClass("EntitySpider$PathfinderGoalSpiderMeleeAttack");
Constructor<?> constr = goalSpiderMelee.getDeclaredConstructor(nmsEnt.getClass(), Class.class);
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt, nmsClass);
} else {
Class<?> goalMeleeAttack = ReflectionUtils.getNMSClass("PathfinderGoalMeleeAttack", false);
Class<?> goalMeleeAttack = ReflectionUtils.getNMSClass("PathfinderGoalMeleeAttack");
double spd = 1.0D;
if (meleeSpeed != null)
spd = meleeSpeed.getSingle(e).doubleValue();
@ -340,7 +360,7 @@ public class EffSetPathGoal extends Effect {
newGoal = goalMeleeAttack.getConstructor(entCreature, Class.class, double.class, boolean.class).newInstance(nmsEnt, nmsClass, spd, memorize);
}
} else if (mark == 13) {
Class<?> goalGotoTarget = ReflectionUtils.getNMSClass("PathfinderGoalMoveTowardsTarget", false);
Class<?> goalGotoTarget = ReflectionUtils.getNMSClass("PathfinderGoalMoveTowardsTarget");
double spd = 1.0D;
if (moveTargetSpeed != null)
spd = moveTargetSpeed.getSingle(e).doubleValue();
@ -349,6 +369,7 @@ public class EffSetPathGoal extends Effect {
radius = moveTargetRadius.getSingle(e).floatValue();
newGoal = goalGotoTarget.getConstructor(entCreature, double.class, float.class).newInstance(nmsEnt, spd, radius);
} else if (mark == 14) {
boolean wasLocal = Language.setUseLocal(false);
target = true;
boolean checkView = false;
EntityData<?> entityData;
@ -356,7 +377,11 @@ public class EffSetPathGoal extends Effect {
if (exprInput.startsWith("the ")) {
exprInput = exprInput.substring(4);
}
try {
entityData = EntityData.parseWithoutIndefiniteArticle(exprInput);
} finally {
Language.setUseLocal(wasLocal);
}
if (!LivingEntity.class.isAssignableFrom(entityData.getType()))
return;
String className = entityData.getType().getSimpleName();
@ -365,43 +390,43 @@ public class EffSetPathGoal extends Effect {
else if (className.equals("LivingEntity"))
className = "Living";
className = "Entity" + className;
Class<?> nmsClass = ReflectionUtils.getNMSClass(className, false);
Class<?> nmsClass = ReflectionUtils.getNMSClass(className);
if (nmsClass == null)
return;
if (ent instanceof Spider) {
Class<?> goalSpiderNearTarget = ReflectionUtils.getNMSClass("EntitySpider$PathfinderGoalSpiderNearestAttackableTarget", false);
Class<?> goalSpiderNearTarget = ReflectionUtils.getNMSClass("EntitySpider$PathfinderGoalSpiderNearestAttackableTarget");
Constructor<?> constr = goalSpiderNearTarget.getDeclaredConstructor(nmsEnt.getClass(), Class.class);
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt, nmsClass);
} else {
if (checkSight != null)
checkView = checkSight.getSingle(e);
Class<?> goalNearTarget = ReflectionUtils.getNMSClass("PathfinderGoalNearestAttackableTarget", false);
Class<?> goalNearTarget = ReflectionUtils.getNMSClass("PathfinderGoalNearestAttackableTarget");
newGoal = goalNearTarget.getConstructor(entCreature, Class.class, boolean.class).newInstance(nmsEnt, nmsClass, checkView);
}
} else if (mark == 15) {
Class<?> goalOcelotAttack = ReflectionUtils.getNMSClass("PathfinderGoalOcelotAttack", false);
Class<?> goalOcelotAttack = ReflectionUtils.getNMSClass("PathfinderGoalOcelotAttack");
newGoal = goalOcelotAttack.getConstructor(entInsent).newInstance(nmsEnt);
} else if (mark == 16) {
Class<?> goalOpenDoors = ReflectionUtils.getNMSClass("PathfinderGoalOpenDoor", false);
Class<?> goalOpenDoors = ReflectionUtils.getNMSClass("PathfinderGoalOpenDoor");
newGoal = goalOpenDoors.getConstructor(entInsent, boolean.class).newInstance(nmsEnt, false);
} else if (mark == 17) {
double spd = 1.0D;
if (panicSpeed != null)
spd = panicSpeed.getSingle(e).doubleValue();
if (ent instanceof Rabbit) {
Class<?> goalRabbitPanic = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalRabbitPanic", false);
Class<?> goalRabbitPanic = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalRabbitPanic");
newGoal = goalRabbitPanic.getDeclaredConstructor(nmsEnt.getClass(), double.class).newInstance(nmsEnt, spd);
} else {
Class<?> goalPanic = ReflectionUtils.getNMSClass("PathfinderGoalPanic", false);
Class<?> goalPanic = ReflectionUtils.getNMSClass("PathfinderGoalPanic");
newGoal = goalPanic.getConstructor(entCreature, double.class).newInstance(nmsEnt, spd);
}
} else if (mark == 18) {
Class<?> goalRandomLook = ReflectionUtils.getNMSClass("PathfinderGoalRandomLookaround", false);
Class<?> goalRandomLook = ReflectionUtils.getNMSClass("PathfinderGoalRandomLookaround");
newGoal = goalRandomLook.getConstructor(entInsent).newInstance(nmsEnt);
} else if (mark == 19) {
Class<?> goalWander = ReflectionUtils.getNMSClass("PathfinderGoalRandomStroll", false);
Class<?> goalWander = ReflectionUtils.getNMSClass("PathfinderGoalRandomStroll");
double spd = 1.0D;
if (randomWalkSpeed != null)
spd = randomWalkSpeed.getSingle(e).doubleValue();
@ -410,17 +435,17 @@ public class EffSetPathGoal extends Effect {
interval = randomWalkInterval.getSingle(e).getTicks();
newGoal = goalWander.getConstructor(entCreature, double.class, int.class).newInstance(nmsEnt, spd, interval);
} else if (mark == 20) {
Class<?> goalSit = ReflectionUtils.getNMSClass("PathfinderGoalSit", false);
Class<?> goalSit = ReflectionUtils.getNMSClass("PathfinderGoalSit");
if (!(ent instanceof Tameable))
return;
newGoal = goalSit.getConstructor(entTameable).newInstance(nmsEnt);
} else if (mark == 21) {
Class<?> goalSwell = ReflectionUtils.getNMSClass("PathfinderGoalSwell", false);
Class<?> goalSwell = ReflectionUtils.getNMSClass("PathfinderGoalSwell");
if (!(ent instanceof Creeper))
return;
newGoal = goalSwell.getConstructor(nmsEnt.getClass()).newInstance(nmsEnt);
} else if (mark == 22) {
Class<?> goalSquid = ReflectionUtils.getNMSClass("EntitySquid$PathfinderGoalSquid", false);
Class<?> goalSquid = ReflectionUtils.getNMSClass("EntitySquid$PathfinderGoalSquid");
if (!(ent instanceof Squid))
return;
Constructor<?> constr = goalSquid.getDeclaredConstructor(nmsEnt.getClass());
@ -428,39 +453,39 @@ public class EffSetPathGoal extends Effect {
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 23) {
if (ent instanceof Blaze) {
Class<?> goalBlazeFireball = ReflectionUtils.getNMSClass("EntityBlaze$PathfinderGoalBlazeFireball", false);
Class<?> goalBlazeFireball = ReflectionUtils.getNMSClass("EntityBlaze$PathfinderGoalBlazeFireball");
Constructor<?> constr = goalBlazeFireball.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (ent instanceof Ghast) {
Class<?> goalGhastFireball = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastAttackTarget", false);
Class<?> goalGhastFireball = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastAttackTarget");
Constructor<?> constr = goalGhastFireball.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
}
} else if (mark == 24) {
Class<?> goalHideInBlock = ReflectionUtils.getNMSClass("EntitySilverfish$PathfinderGoalSilverfishHideInBlock", false);
Class<?> goalHideInBlock = ReflectionUtils.getNMSClass("EntitySilverfish$PathfinderGoalSilverfishHideInBlock");
if (!(ent instanceof Silverfish))
return;
Constructor<?> constr = goalHideInBlock.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 25) {
Class<?> goalWakeSilverfish = ReflectionUtils.getNMSClass("EntitySilverfish$PathfinderGoalSilverfishWakeOthers", false);
Class<?> goalWakeSilverfish = ReflectionUtils.getNMSClass("EntitySilverfish$PathfinderGoalSilverfishWakeOthers");
if (!(ent instanceof Silverfish))
return;
Constructor<?> constr = goalWakeSilverfish.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 26) {
Class<?> goalPickBlocks = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalEndermanPickupBlock", false);
Class<?> goalPickBlocks = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalEndermanPickupBlock");
if (!(ent instanceof Enderman))
return;
Constructor<?> constr = goalPickBlocks.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 27) {
Class<?> goalPlaceBlocks = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalEndermanPlaceBlock", false);
Class<?> goalPlaceBlocks = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalEndermanPlaceBlock");
if (!(ent instanceof Enderman))
return;
Constructor<?> constr = goalPlaceBlocks.getDeclaredConstructor(nmsEnt.getClass());
@ -468,28 +493,28 @@ public class EffSetPathGoal extends Effect {
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 28) {
target = true;
Class<?> goalAttackLooker = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalPlayerWhoLookedAtTarget", false);
Class<?> goalAttackLooker = ReflectionUtils.getNMSClass("EntityEnderman$PathfinderGoalPlayerWhoLookedAtTarget");
if (!(ent instanceof Enderman))
return;
Constructor<?> constr = goalAttackLooker.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 29) {
Class<?> goalGhastMoveTarget = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastMoveTowardsTarget", false);
Class<?> goalGhastMoveTarget = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastMoveTowardsTarget");
if (!(ent instanceof Ghast))
return;
Constructor<?> constr = goalGhastMoveTarget.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 30) {
Class<?> goalGhastIdleMove = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastIdleMove", false);
Class<?> goalGhastIdleMove = ReflectionUtils.getNMSClass("EntityGhast$PathfinderGoalGhastIdleMove");
if (!(ent instanceof Ghast))
return;
Constructor<?> constr = goalGhastIdleMove.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 31) {
Class<?> goalTempt = ReflectionUtils.getNMSClass("PathfinderGoalTempt", false);
Class<?> goalTempt = ReflectionUtils.getNMSClass("PathfinderGoalTempt");
ItemStack itemStack = temptItem.getSingle(e);
if (itemStack.getType() == Material.AIR || itemStack == null)
return;
@ -503,8 +528,9 @@ public class EffSetPathGoal extends Effect {
scared = temptScared.getSingle(e);
newGoal = goalTempt.getConstructor(entCreature, double.class, nmsItemClass, boolean.class).newInstance(nmsEnt, spd, nmsItem, scared);
} else if (mark == 32) {
boolean wasLocal = Language.setUseLocal(false);
target = true;
Class<?> goalTargetNonTamed = ReflectionUtils.getNMSClass("PathfinderGoalRandomTargetNonTamed", false);
Class<?> goalTargetNonTamed = ReflectionUtils.getNMSClass("PathfinderGoalRandomTargetNonTamed");
if (!(ent instanceof Tameable))
return;
EntityData<?> entityData;
@ -512,7 +538,11 @@ public class EffSetPathGoal extends Effect {
if (exprInput.startsWith("the ")) {
exprInput = exprInput.substring(4);
}
try {
entityData = EntityData.parseWithoutIndefiniteArticle(exprInput);
} finally {
Language.setUseLocal(wasLocal);
}
if (!LivingEntity.class.isAssignableFrom(entityData.getType())) {
return;
}
@ -522,14 +552,14 @@ public class EffSetPathGoal extends Effect {
else if (className.equals("LivingEntity"))
className = "Living";
className = "Entity" + className;
Class<?> nmsClass = ReflectionUtils.getNMSClass(className, false);
Class<?> nmsClass = ReflectionUtils.getNMSClass(className);
if (nmsClass == null)
return;
newGoal = goalTargetNonTamed.getConstructor(entTameable, Class.class, boolean.class, Predicate.class).newInstance(nmsEnt, nmsClass, false, Predicates.alwaysTrue());
} else if (mark == 33) {
if (!(ent instanceof Guardian))
return;
Class<?> goalGuardianAttack = ReflectionUtils.getNMSClass("EntityGuardian$PathfinderGoalGuardianAttack", false);
Class<?> goalGuardianAttack = ReflectionUtils.getNMSClass("EntityGuardian$PathfinderGoalGuardianAttack");
Constructor<?> constr = goalGuardianAttack.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
@ -537,7 +567,7 @@ public class EffSetPathGoal extends Effect {
if (!(ent instanceof PigZombie))
return;
target = true;
Class<?> goalAnger = ReflectionUtils.getNMSClass("EntityPigZombie$PathfinderGoalAnger", false);
Class<?> goalAnger = ReflectionUtils.getNMSClass("EntityPigZombie$PathfinderGoalAnger");
Constructor<?> constr = goalAnger.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
@ -545,56 +575,49 @@ public class EffSetPathGoal extends Effect {
if (!(ent instanceof PigZombie))
return;
target = true;
Class<?> goalAngerOther = ReflectionUtils.getNMSClass("EntityPigZombie$PathfinderGoalAngerOther", false);
Class<?> goalAngerOther = ReflectionUtils.getNMSClass("EntityPigZombie$PathfinderGoalAngerOther");
Constructor<?> constr = goalAngerOther.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 36) {
if (!(ent instanceof Rabbit))
return;
Class<?> goalEatCarrots = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalEatCarrots", false);
Class<?> goalEatCarrots = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalEatCarrots");
Constructor<?> constr = goalEatCarrots.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 37) {
if (!(ent instanceof Rabbit))
return;
Class<?> goalRabbitAttack = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalKillerRabbitMeleeAttack", false);
Class<?> goalRabbitAttack = ReflectionUtils.getNMSClass("EntityRabbit$PathfinderGoalKillerRabbitMeleeAttack");
Constructor<?> constr = goalRabbitAttack.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 38) {
if (!(ent instanceof Slime))
return;
Class<?> goalJump = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeRandomJump", false);
Class<?> goalJump = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeRandomJump");
Constructor<?> constr = goalJump.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 39) {
if (!(ent instanceof Slime))
return;
Class<?> goalRandomDir = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeRandomDirection", false);
Class<?> goalRandomDir = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeRandomDirection");
Constructor<?> constr = goalRandomDir.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
} else if (mark == 40) {
if (!(ent instanceof Slime))
return;
Class<?> goalSlimeWander = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeIdle", false);
Class<?> goalSlimeWander = ReflectionUtils.getNMSClass("EntitySlime$PathfinderGoalSlimeIdle");
Constructor<?> constr = goalSlimeWander.getDeclaredConstructor(nmsEnt.getClass());
constr.setAccessible(true);
newGoal = constr.newInstance(nmsEnt);
}
if (newGoal == null)
return;
Class<?> goal = ReflectionUtils.getNMSClass("PathfinderGoal", false);
Class<?> goalSelector = ReflectionUtils.getNMSClass("PathfinderGoalSelector", false);
if (target) { //Target Selector
newGoal = goalSelector.getMethod("a", int.class, goal).invoke(targets, priority, newGoal);
} else { //Goal Selector
newGoal = goalSelector.getMethod("a", int.class, goal).invoke(goals, priority, newGoal);
}
SkStuff.getNMSMethods().addPathfinderGoal(nmsEnt, priority, newGoal, target);
} catch (Exception ex) {
ex.printStackTrace();
}

View File

@ -25,10 +25,10 @@ import me.TheBukor.SkStuff.util.ReflectionUtils;
public class ExprEndermanBlocks extends SimpleExpression<ItemStack> {
private Expression<Entity> entity;
private Class<?> nmsBlockClass = ReflectionUtils.getNMSClass("Block", false);
private Class<?> endermanClass = ReflectionUtils.getNMSClass("EntityEnderman", false);
private Class<?> nmsIBlockData = ReflectionUtils.getNMSClass("IBlockData", false);
private Class<?> nmsItemClass = ReflectionUtils.getNMSClass("ItemStack", false);
private Class<?> nmsBlockClass = ReflectionUtils.getNMSClass("Block");
private Class<?> endermanClass = ReflectionUtils.getNMSClass("EntityEnderman");
private Class<?> nmsIBlockData = ReflectionUtils.getNMSClass("IBlockData");
private Class<?> nmsItemClass = ReflectionUtils.getNMSClass("ItemStack");
private Class<?> craftEntClass = ReflectionUtils.getOBCClass("entity.CraftEntity");
private Class<?> craftItemClass = ReflectionUtils.getOBCClass("inventory.CraftItemStack");

View File

@ -18,7 +18,7 @@ import me.TheBukor.SkStuff.util.ReflectionUtils;
public class ExprFileNBT extends SimpleExpression<Object> {
private Expression<String> input;
private Class<?> nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound", false);
private Class<?> nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound");
@Override
public Class<? extends Object> getReturnType() {
@ -62,7 +62,8 @@ public class ExprFileNBT extends SimpleExpression<Object> {
return;
Object fileNBT = SkStuff.getNMSMethods().getFileNBT(file);
if (mode == ChangeMode.ADD) {
Object parsedNBT = SkStuff.getNMSMethods().parseRawNBT((String) delta[0]);
Object parsedNBT = null;
parsedNBT = SkStuff.getNMSMethods().parseRawNBT((String) delta[0]);
SkStuff.getNMSMethods().addToCompound(fileNBT, parsedNBT);
SkStuff.getNMSMethods().setFileNBT(file, fileNBT);
} else if (mode == ChangeMode.REMOVE) {

View File

@ -48,7 +48,8 @@ public class ExprItemNBT extends SimpleExpression<ItemStack> {
if (item.getType() == Material.AIR || item == null) {
return null;
}
Object parsedNBT = SkStuff.getNMSMethods().parseRawNBT(newTags);
Object parsedNBT = null;
parsedNBT = SkStuff.getNMSMethods().parseRawNBT(newTags);
ItemStack newItem = SkStuff.getNMSMethods().getItemWithNBT(item, parsedNBT);
return new ItemStack[] { newItem };
}

View File

@ -4,20 +4,19 @@ import javax.annotation.Nullable;
import org.bukkit.event.Event;
//import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
//import ch.njol.util.coll.CollectionUtils;
import ch.njol.util.coll.CollectionUtils;
import me.TheBukor.SkStuff.SkStuff;
//import me.TheBukor.SkStuff.util.ReflectionUtils;
import me.TheBukor.SkStuff.util.ReflectionUtils;
public class ExprNBTListContents extends SimpleExpression<Object> {
private Expression<Object> nbtList;
//private Class<?> nbtListClass = ReflectionUtils.getNMSClass("NBTTagList", false);
//private Class<?> nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound", false);
private Class<?> nbtBaseClass = ReflectionUtils.getNMSClass("NBTBase");
@Override
public Class<? extends Object> getReturnType() {
@ -33,7 +32,7 @@ public class ExprNBTListContents extends SimpleExpression<Object> {
@Override
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean arg2, ParseResult result) {
nbtList = (Expression<Object>) expr[0];
return false;
return true;
}
@Override
@ -45,22 +44,16 @@ public class ExprNBTListContents extends SimpleExpression<Object> {
@Nullable
protected Object[] get(Event e) {
Object list = nbtList.getSingle(e);
SkStuff.getNMSMethods().getContents(list);
return null;
return SkStuff.getNMSMethods().getContents(list);
}
/*
@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
Object list = nbtList.getSingle(e);
if (mode == ChangeMode.ADD) {
if (!(delta[0] instanceof Number || delta[0] instanceof String || nbtListClass.isAssignableFrom(delta[0].getClass()) || nbtClass.isAssignableFrom(delta[0].getClass())))
if (!(delta[0] instanceof Number || delta[0] instanceof String || nbtBaseClass.isAssignableFrom(delta[0].getClass())))
return; //NBT can only store numbers, strings, lists or compounds.
SkStuff.getNMSMethods().addToList(list, delta[0]);
} else if (mode == ChangeMode.REMOVE) {
// TODO A method to remove a specific object from a NBT List
} else if (mode == ChangeMode.REMOVE_ALL) {
// TODO A method to remove all objects of some type from a NBT List
}
}
@ -73,5 +66,4 @@ public class ExprNBTListContents extends SimpleExpression<Object> {
}
return null;
}
*/
}

View File

@ -17,7 +17,7 @@ public class ExprNBTListIndex extends SimpleExpression<Object> {
private Expression<Object> nbtList;
private Expression<Number> index;
private Class<?> nbtBaseClass = ReflectionUtils.getNMSClass("NBTBase", false);
private Class<?> nbtBaseClass = ReflectionUtils.getNMSClass("NBTBase");
@Override
public Class<? extends Object> getReturnType() {
@ -34,7 +34,7 @@ public class ExprNBTListIndex extends SimpleExpression<Object> {
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean arg2, ParseResult result) {
nbtList = (Expression<Object>) expr[0];
index = (Expression<Number>) expr[1];
return false;
return true;
}
@Override
@ -47,8 +47,7 @@ public class ExprNBTListIndex extends SimpleExpression<Object> {
protected Object[] get(Event e) {
int i = index.getSingle(e).intValue();
Object list = nbtList.getSingle(e);
SkStuff.getNMSMethods().getIndex(list, i);
return null;
return new Object[] { SkStuff.getNMSMethods().getIndex(list, i) };
}
@Override

View File

@ -4,13 +4,11 @@ import java.util.Arrays;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import ch.njol.skript.ScriptLoader;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.lang.Expression;
@ -26,7 +24,7 @@ import me.TheBukor.SkStuff.util.ReflectionUtils;
public class ExprNBTOf extends SimpleExpression<Object> {
private Expression<Object> target;
private Class<?> nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound", false);
private Class<?> nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound");
@Override
public Class<? extends Object> getReturnType() {
@ -43,11 +41,6 @@ public class ExprNBTOf extends SimpleExpression<Object> {
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean arg2, ParseResult result) {
target = (Expression<Object>) expr[0];
Class<?> type = target.getReturnType();
Class<? extends Event>[] evts = ScriptLoader.getCurrentEvents();
for (int i = 0; i < evts.length; i++) {
Bukkit.broadcastMessage("Event #" + i + ": \u00A7b" + evts[i].getSimpleName());
}
Bukkit.broadcastMessage("Object type: \u00A79" + type.getSimpleName());
if (type != Entity.class || type != Block.class || type != ItemStack.class || type != Slot.class) {
Skript.error(target.toString() + " is neither an entity, a block nor an itemstack.", ErrorQuality.SEMANTIC_ERROR);
}
@ -69,6 +62,8 @@ public class ExprNBTOf extends SimpleExpression<Object> {
return new Object[] { SkStuff.getNMSMethods().getTileNBT((Block) tar) };
} else if (tar instanceof ItemStack) {
return new Object[] { SkStuff.getNMSMethods().getItemNBT((ItemStack) tar) };
} else if (tar instanceof Slot) {
return new Object[] { SkStuff.getNMSMethods().getItemNBT(((Slot) tar).getItem()) };
}
return null;
}
@ -76,10 +71,13 @@ public class ExprNBTOf extends SimpleExpression<Object> {
@Override
public void change(Event e, Object[] delta, ChangeMode mode) {
Object tar = target.getSingle(e);
Object parsedNBT = null;
if (delta != null) {
parsedNBT = SkStuff.getNMSMethods().parseRawNBT((String) delta[0]);
}
if (tar instanceof Entity) {
Object entNBT = SkStuff.getNMSMethods().getEntityNBT((Entity) tar);
if (mode == ChangeMode.ADD) {
Object parsedNBT = SkStuff.getNMSMethods().parseRawNBT((String) delta[0]);
SkStuff.getNMSMethods().removeFromCompound(parsedNBT, "UUIDMost", "UUIDLeast", "WorldUUDMost", "WorldUUIDLeast", "Bukkit.updateLevel");
SkStuff.getNMSMethods().addToCompound(entNBT, parsedNBT);
SkStuff.getNMSMethods().setEntityNBT((Entity) tar, entNBT);
@ -94,7 +92,6 @@ public class ExprNBTOf extends SimpleExpression<Object> {
} else if (tar instanceof Block) {
Object blockNBT = SkStuff.getNMSMethods().getTileNBT((Block) tar);
if (mode == ChangeMode.ADD) {
Object parsedNBT = SkStuff.getNMSMethods().parseRawNBT((String) delta[0]);
SkStuff.getNMSMethods().removeFromCompound(parsedNBT, "x", "y", "z", "id");
SkStuff.getNMSMethods().addToCompound(blockNBT, parsedNBT);
SkStuff.getNMSMethods().setTileNBT((Block) tar, blockNBT);
@ -109,7 +106,6 @@ public class ExprNBTOf extends SimpleExpression<Object> {
} else if (tar instanceof ItemStack) {
Object itemNBT = SkStuff.getNMSMethods().getItemNBT((ItemStack) tar);
if (mode == ChangeMode.ADD) {
Object parsedNBT = SkStuff.getNMSMethods().parseRawNBT((String) delta[0]);
SkStuff.getNMSMethods().addToCompound(itemNBT, parsedNBT);
ItemStack newItem = SkStuff.getNMSMethods().getItemWithNBT((ItemStack) tar, itemNBT);
Object slot = target.getSource().getSingle(e);
@ -131,6 +127,21 @@ public class ExprNBTOf extends SimpleExpression<Object> {
((Slot) slot).setItem(newItem);
}
}
} else if (tar instanceof Slot) {
Object itemNBT = SkStuff.getNMSMethods().getItemNBT(((Slot) tar).getItem());
if (mode == ChangeMode.ADD) {
SkStuff.getNMSMethods().addToCompound(itemNBT, parsedNBT);
ItemStack newItem = SkStuff.getNMSMethods().getItemWithNBT((ItemStack) tar, itemNBT);
((Slot) tar).setItem(newItem);
} else if (mode == ChangeMode.REMOVE) {
String[] toRemove = Arrays.copyOf(delta, delta.length, String[].class);
SkStuff.getNMSMethods().removeFromCompound(itemNBT, toRemove);
ItemStack newItem = SkStuff.getNMSMethods().getItemWithNBT((ItemStack) tar, itemNBT);
((Slot) tar).setItem(newItem);
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
ItemStack newItem = SkStuff.getNMSMethods().getItemWithNBT((ItemStack) tar, null);
((Slot) tar).setItem(newItem);
}
}
}

View File

@ -16,8 +16,8 @@ public class ExprTagOf extends SimpleExpression<Object> {
private Expression<String> string;
private Expression<Object> compound;
private Class<?> nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound", false);
private Class<?> nbtBaseClass = ReflectionUtils.getNMSClass("NBTBase", false);
private Class<?> nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound");
private Class<?> nbtBaseClass = ReflectionUtils.getNMSClass("NBTBase");
@Override
public Class<? extends Object> getReturnType() {

View File

@ -1,179 +0,0 @@
package me.TheBukor.SkStuff.util;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.fusesource.jansi.Ansi;
import ch.njol.skript.Skript;
public class NBTUtil {
private static Class<?> nbtBaseClass = ReflectionUtils.getNMSClass("NBTBase", false);
private static Class<?> nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound", false);
private static Class<?> nbtListClass = ReflectionUtils.getNMSClass("NBTTagList", false);
/**
* This is actually a copy of the "a(NBTTagCompound)" method in the NBTTagCompound class.
* I needed to add this because the 1.7 and before versions of the NBTTagCompound class didn't have this method,
* so there wasn't actually a reliable way to multiple tags at once into a compound.
* For the original code for the method, check https://github.com/linouxis9/mc-dev-1.8.7/blob/master/net/minecraft/server/NBTTagCompound.java#L348
*
* Please note that I adapted it to work using reflection.
*/
@SuppressWarnings("unchecked")
public static void addCompound(Object NBT, Object toAdd) {
if (NBT.getClass() == nbtClass && toAdd.getClass() == nbtClass) {
try {
HashMap<String, Object> map = (HashMap<String, Object>) ReflectionUtils.getField("map", nbtClass, toAdd);
Set<String> keySet = (Set<String>) nbtClass.getMethod("c").invoke(toAdd);
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()) {
String string = (String) iterator.next();
Object base = nbtBaseClass.cast(map.get(string));
if((byte) nbtBaseClass.getMethod("getTypeId").invoke(base) == 10) {
if((boolean) nbtClass.getMethod("hasKeyOfType", String.class, int.class).invoke(NBT, string, 10)) {
Object localNBT = null;
localNBT = nbtClass.getMethod("getCompound", String.class).invoke(localNBT, string);
NBTUtil.addCompound(localNBT, nbtBaseClass.cast(nbtClass));
} else {
nbtClass.getMethod("set", String.class, nbtBaseClass).invoke(NBT, string, base.getClass().getMethod("clone").invoke(base));
}
} else {
nbtClass.getMethod("set", String.class, nbtBaseClass).invoke(NBT, string, base.getClass().getMethod("clone").invoke(base));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**
* Gets the ID of the contents inside a NBT List.
* I needed to add this because the 1.7 and before versions of the NBTTagList
* class had a different name for the method that got this value.
* 1.8 used "f()", while 1.7 used "d()".
*/
public static int getContentsId(Object list) {
if (list.getClass() == nbtListClass) {
Field type = null;
int result = 0;
try {
type = nbtListClass.getDeclaredField("type");
type.setAccessible(true);
result = type.getInt(list);
type.setAccessible(false);
return result;
} catch (Exception ex) {
type.setAccessible(false);
ex.printStackTrace();
}
return result;
}
return 0;
}
/**
* Used for the "addToList()" and "setIndex()" methods if the typeId of the contents is still not defined.
*/
public static void setContentsId(Object list, int newId) {
if (list.getClass() == nbtListClass) {
Field type = null;
try {
type = nbtListClass.getDeclaredField("type");
type.setAccessible(true);
type.set(list, newId);
type.setAccessible(false);
} catch (Exception ex) {
type.setAccessible(false);
ex.printStackTrace();
}
}
}
@SuppressWarnings("unchecked")
public static List<Object> getContents(Object list) {
if (list.getClass() == nbtListClass) {
List<Object> result = null;
result = (List<Object>) ReflectionUtils.getField("list", nbtListClass, list);
return result;
}
return null;
}
/**
* Kind of a copy of the "add()" method from the NBTTagList class.
*/
public static void addToList(Object list, Object[] toAdd) {
if (list.getClass() == nbtListClass && toAdd[0].getClass() == nbtBaseClass) {
int listTypeId = NBTUtil.getContentsId(list);
int toAddId = 0;
try {
toAddId = (int) toAdd.getClass().getMethod("getTypeId").invoke(toAdd);
} catch (Exception ex) {
ex.printStackTrace();
}
if (listTypeId == 0) {
try {
NBTUtil.setContentsId(list, toAddId);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (listTypeId != toAddId) {
Skript.warning(Ansi.ansi().fgBright(Ansi.Color.RED) + "Adding mismatching tag types to NBT list" + Ansi.ansi().fgBright(Ansi.Color.DEFAULT));
return;
}
for (Object tag : toAdd) {
NBTUtil.getContents(list).add(tag);
}
}
}
public static void removefromList(Object list, int index) {
if (list.getClass() == nbtListClass) {
if (index >= 0 && index < NBTUtil.getContents(list).size()) {
NBTUtil.getContents(list).remove(index);
}
}
}
public static void setIndex(Object list, int index, Object toAdd) {
if (list.getClass() == nbtListClass && toAdd.getClass() == nbtBaseClass) {
if (index >= 0 && index < NBTUtil.getContents(list).size()) {
int listTypeId = NBTUtil.getContentsId(list);
int toAddId = 0;
try {
toAddId = (int) toAdd.getClass().getMethod("getTypeId").invoke(toAdd);
} catch (Exception ex) {
ex.printStackTrace();
}
if (listTypeId == 0) {
try {
NBTUtil.setContentsId(list, toAddId);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (listTypeId != toAddId) {
Skript.warning("Adding mismatching tag types to NBT list");
return;
}
NBTUtil.getContents(list).set(index, toAdd);
}
}
}
public static Object getIndex(Object list, int index) {
if (list.getClass() == nbtListClass) {
if (index >= 0 && index < NBTUtil.getContents(list).size()) {
NBTUtil.getContents(list).get(index);
}
}
return null;
}
}

View File

@ -49,4 +49,8 @@ public interface NMSInterface {
public Object getFileNBT(File file);
public void setFileNBT(File file, Object newCompound);
public Object convertToNBT(Number number);
public Object convertToNBT(String string);
}

View File

@ -7,7 +7,6 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -36,13 +35,15 @@ import net.minecraft.server.v1_7_R4.NBTBase;
import net.minecraft.server.v1_7_R4.NBTCompressedStreamTools;
import net.minecraft.server.v1_7_R4.NBTTagCompound;
import net.minecraft.server.v1_7_R4.NBTTagDouble;
import net.minecraft.server.v1_7_R4.NBTTagEnd;
import net.minecraft.server.v1_7_R4.NBTTagFloat;
import net.minecraft.server.v1_7_R4.NBTTagInt;
import net.minecraft.server.v1_7_R4.NBTTagList;
import net.minecraft.server.v1_7_R4.NBTTagString;
import net.minecraft.server.v1_7_R4.TileEntity;
import net.minecraft.server.v1_7_R4.World;
import net.minecraft.server.v1_8_R3.NBTTagByte;
import net.minecraft.server.v1_8_R3.NBTTagLong;
import net.minecraft.server.v1_8_R3.NBTTagShort;
public class NMS_v1_7_R4 implements NMSInterface {
@ -74,12 +75,14 @@ public class NMS_v1_7_R4 implements NMSInterface {
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
((NBTTagCompound) compound).remove(toRemove.toString()); //FIXME
for (String s : toRemove) {
((NBTTagCompound) compound).remove(s);
}
}
}
@Override
public Object parseRawNBT(String rawNBT) {
public NBTTagCompound parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
parsedNBT = (NBTTagCompound) MojangsonParser.parse(rawNBT);
return parsedNBT;
@ -96,13 +99,13 @@ public class NMS_v1_7_R4 implements NMSInterface {
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
List<Object> contents = new ArrayList<Object>();
Object[] contents = new Object[((NBTTagList) nbtList).size()];
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents.add(getIndex(nbtList, i));
contents[i] = getIndex(nbtList, i);
}
}
return contents.toArray();
return contents;
}
return null;
}
@ -127,9 +130,18 @@ public class NMS_v1_7_R4 implements NMSInterface {
@SuppressWarnings("unchecked")
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && toSet instanceof NBTBase && index >= 0 && index < ((NBTTagList) nbtList).size()) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
int typeId = getContentsId(nbtList);
int toSetId = ((NBTBase) toSet).getTypeId();
NBTBase toSetNBT = null;
if (toSet instanceof NBTBase)
toSetNBT = (NBTBase) toSet;
else if (toSet instanceof Number)
toSetNBT = (NBTBase) convertToNBT((Number) toSet);
else if (toSet instanceof String)
toSetNBT = convertToNBT((String) toSet);
else
return;
int toSetId = (toSetNBT).getTypeId();
if (typeId == 0) {
ReflectionUtils.setField("type", NBTTagList.class, nbtList, toSetId);
} else if (typeId != toSetId) {
@ -138,7 +150,7 @@ public class NMS_v1_7_R4 implements NMSInterface {
}
List<Object> actualList = null;
actualList = (List<Object>) ReflectionUtils.getField("list", NBTTagList.class, nbtList);
actualList.set(index, toSet);
actualList.set(index, toSetNBT);
}
}
@ -149,9 +161,6 @@ public class NMS_v1_7_R4 implements NMSInterface {
List<NBTBase> actualList = null;
actualList = (List<NBTBase>) ReflectionUtils.getField("list", NBTTagList.class, nbtList);
NBTBase value = (NBTBase) actualList.get(index);
if (value instanceof NBTTagEnd)
return null;
else
return value;
}
return null;
@ -187,12 +196,12 @@ public class NMS_v1_7_R4 implements NMSInterface {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBTTagCompound parsedNBT = parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBTTagCompound parsedNBT = parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
@ -217,7 +226,7 @@ public class NMS_v1_7_R4 implements NMSInterface {
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
rawNBT.substring(4);
NBTTagCompound NBT = (NBTTagCompound) parseRawNBT(rawNBT);
NBTTagCompound NBT = parseRawNBT(rawNBT);
if (NBT.toString().equals("{}")) {
return null;
}
@ -322,7 +331,7 @@ public class NMS_v1_7_R4 implements NMSInterface {
}
@Override
public Object getEntityNBT(Entity entity) {
public NBTTagCompound getEntityNBT(Entity entity) {
net.minecraft.server.v1_7_R4.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
@ -330,7 +339,7 @@ public class NMS_v1_7_R4 implements NMSInterface {
}
@Override
public Object getTileNBT(Block block) {
public NBTTagCompound getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(block.getX(), block.getY(), block.getZ());
@ -341,7 +350,7 @@ public class NMS_v1_7_R4 implements NMSInterface {
}
@Override
public Object getItemNBT(ItemStack itemStack) {
public NBTTagCompound getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound NBT = new NBTTagCompound();
@ -349,7 +358,7 @@ public class NMS_v1_7_R4 implements NMSInterface {
NBT = nmsItem.getTag();
if (NBT == null || NBT.toString().equals("{}")) //Null or empty.
return null;
return new Object[] { NBT };
return NBT;
}
@Override
@ -389,7 +398,7 @@ public class NMS_v1_7_R4 implements NMSInterface {
}
@Override
public Object getFileNBT(File file) {
public NBTTagCompound getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
@ -441,4 +450,49 @@ public class NMS_v1_7_R4 implements NMSInterface {
}
}
}
public NBTTagByte convertToNBT(byte b) {
return new NBTTagByte(b);
}
public NBTTagShort convertToNBT(short s) {
return new NBTTagShort(s);
}
public NBTTagInt convertToNBT(int i) {
return new NBTTagInt(i);
}
public NBTTagLong convertToNBT(long l) {
return new NBTTagLong(l);
}
public NBTTagFloat convertToNBT(float f) {
return new NBTTagFloat(f);
}
public NBTTagDouble convertToNBT(double d) {
return new NBTTagDouble(d);
}
public NBTTagString convertToNBT(String string) {
return new NBTTagString(string);
}
@Override
public Object convertToNBT(Number number) {
if (number instanceof Byte)
return convertToNBT((byte) number);
else if (number instanceof Short)
return convertToNBT((short) number);
else if (number instanceof Integer)
return convertToNBT((int) number);
else if (number instanceof Long)
return convertToNBT((long) number);
else if (number instanceof Float)
return convertToNBT((float) number);
else if (number instanceof Double)
return convertToNBT((double) number);
return number;
}
}

View File

@ -6,8 +6,9 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.io.StreamCorruptedException;
import java.util.Iterator;
import java.util.List;
@ -24,20 +25,24 @@ import org.bukkit.inventory.ItemStack;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import ch.njol.yggdrasil.Fields;
import net.minecraft.server.v1_8_R1.BlockPosition;
import net.minecraft.server.v1_8_R1.EntityInsentient;
import net.minecraft.server.v1_8_R1.MojangsonParser;
import net.minecraft.server.v1_8_R1.NBTBase;
import net.minecraft.server.v1_8_R1.NBTCompressedStreamTools;
import net.minecraft.server.v1_8_R1.NBTTagByte;
import net.minecraft.server.v1_8_R1.NBTTagCompound;
import net.minecraft.server.v1_8_R1.NBTTagDouble;
import net.minecraft.server.v1_8_R1.NBTTagEnd;
import net.minecraft.server.v1_8_R1.NBTTagFloat;
import net.minecraft.server.v1_8_R1.NBTTagInt;
import net.minecraft.server.v1_8_R1.NBTTagList;
import net.minecraft.server.v1_8_R1.NBTTagLong;
import net.minecraft.server.v1_8_R1.NBTTagShort;
import net.minecraft.server.v1_8_R1.NBTTagString;
import net.minecraft.server.v1_8_R1.PathfinderGoal;
import net.minecraft.server.v1_8_R1.PathfinderGoalSelector;
@ -56,12 +61,14 @@ public class NMS_v1_8_R1 implements NMSInterface {
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
((NBTTagCompound) compound).remove(toRemove.toString()); //FIXME
for (String s : toRemove) {
((NBTTagCompound) compound).remove(s);
}
}
}
@Override
public Object parseRawNBT(String rawNBT) {
public NBTTagCompound parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
parsedNBT = MojangsonParser.parse(rawNBT);
return parsedNBT;
@ -78,13 +85,13 @@ public class NMS_v1_8_R1 implements NMSInterface {
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
List<Object> contents = new ArrayList<Object>();
Object[] contents = new Object[((NBTTagList) nbtList).size()];
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents.add(getIndex(nbtList, i));
contents[i] = getIndex(nbtList, i);
}
}
return contents.toArray();
return contents;
}
return null;
}
@ -98,26 +105,45 @@ public class NMS_v1_8_R1 implements NMSInterface {
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
((NBTTagList) nbtList).a(index);
}
}
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && toSet instanceof NBTBase) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
if (toSet instanceof NBTBase) {
((NBTTagList) nbtList).a(index, (NBTBase) toSet);
} else if (toSet instanceof Number) {
((NBTTagList) nbtList).a(index, convertToNBT((Number) toSet));
} else if (toSet instanceof String) {
((NBTTagList) nbtList).a(index, convertToNBT((String) toSet));
}
}
}
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
NBTBase value = ((NBTTagList) nbtList).g(index);
if (value instanceof NBTTagEnd)
return null;
else
return value;
if (value instanceof NBTTagByte) {
return ((NBTTagByte) value).f(); //Byte stored inside a NBTNumber
} else if (value instanceof NBTTagShort) {
return ((NBTTagShort) value).e(); //Short inside a NBTNumber
} else if (value instanceof NBTTagInt) {
return ((NBTTagInt) value).d(); //Integer inside a NBTNumber
} else if (value instanceof NBTTagLong) {
return ((NBTTagLong) value).c(); //Long inside a NBTNumber
} else if (value instanceof NBTTagFloat) {
return ((NBTTagFloat) value).h(); //Float inside a NBTNumber
} else if (value instanceof NBTTagDouble) {
return ((NBTTagDouble) value).g(); //Double inside a NBTNumber
} else if (value instanceof NBTTagString) {
return ((NBTTagString) value).a_(); //String inside the NBTTagString
} else if (value instanceof NBTTagList || value instanceof NBTTagCompound) {
return value; //No need to convert anything, these are registered by the addon.
}
}
return null;
}
@ -176,12 +202,14 @@ public class NMS_v1_8_R1 implements NMSInterface {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
@ -205,12 +233,8 @@ public class NMS_v1_8_R1 implements NMSInterface {
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
rawNBT.substring(4);
rawNBT.substring(4);
NBTTagCompound NBT = (NBTTagCompound) parseRawNBT(rawNBT);
if (NBT.toString().equals("{}")) {
return null;
}
NBTTagCompound NBT = null;
NBT = parseRawNBT(rawNBT.substring(4));
return NBT;
}
return null;
@ -225,6 +249,45 @@ public class NMS_v1_8_R1 implements NMSInterface {
public String toVariableNameString(NBTTagCompound compound) {
return "nbt:" + compound.toString();
}
}).serializer(new Serializer<NBTTagCompound>() {
@Override
public Fields serialize(NBTTagCompound compound) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", compound.toString());
return f;
}
@Override
public void deserialize(NBTTagCompound compound, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagCompound deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
@Nullable
public NBTTagCompound deserialize(String s) {
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@ -246,7 +309,15 @@ public class NMS_v1_8_R1 implements NMSInterface {
@Override
public void change(NBTTagList[] nbtList, @Nullable Object[] delta, ChangeMode mode) {
int typeId = 0;
if (delta instanceof Float[]) {
if (delta instanceof Byte[]) {
typeId = 1;
} else if (delta instanceof Short[]) {
typeId = 2;
} else if (delta instanceof Integer[]) {
typeId = 3;
} else if (delta instanceof Long[]) {
typeId = 4;
} else if (delta instanceof Float[]) {
typeId = 5;
} else if (delta instanceof Double[]) {
typeId = 6;
@ -256,8 +327,6 @@ public class NMS_v1_8_R1 implements NMSInterface {
typeId = 9;
} else if (delta instanceof NBTTagCompound[]) {
typeId = 10;
} else if (delta instanceof Integer[]) {
typeId = 11;
} else {
return;
}
@ -266,21 +335,12 @@ public class NMS_v1_8_R1 implements NMSInterface {
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (typeId == 5) {
NBTTagFloat floatTag = new NBTTagFloat((float) delta[0]);
addToList(nbtList[0], floatTag);
} else if (typeId == 6) {
NBTTagDouble doubleTag = new NBTTagDouble((double) delta[0]);
addToList(nbtList[0], doubleTag);
} else if (typeId == 8) {
NBTTagString stringTag = new NBTTagString((String) delta [0]);
addToList(nbtList[0], stringTag);
} else if (typeId == 10) {
addToList(nbtList[0], delta[0]);
} else if (typeId == 11) {
NBTTagInt intTag = new NBTTagInt((int) delta[0]);
addToList(nbtList[0], intTag);
}
if (delta[0] instanceof Number)
addToList(nbtList, convertToNBT((Number) delta[0]));
else if (delta[0] instanceof String)
addToList(nbtList, convertToNBT((String) delta[0]));
else if (delta[0] instanceof NBTTagCompound || delta[0] instanceof NBTTagList)
addToList(nbtList, delta[0]);
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
@ -295,7 +355,13 @@ public class NMS_v1_8_R1 implements NMSInterface {
@Override
@Nullable
public NBTTagList parse(String ignored, ParseContext context) {
public NBTTagList parse(String listString, ParseContext context) {
if (listString.startsWith("[") && listString.endsWith("]")) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:[0:" + listString.substring(1) + "}");
NBTTagList parsedList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return parsedList;
}
return null;
}
@ -308,11 +374,52 @@ public class NMS_v1_8_R1 implements NMSInterface {
public String toVariableNameString(NBTTagList nbtList) {
return nbtList.toString();
}
}).serializer(new Serializer<NBTTagList>() {
@Override
public Fields serialize(NBTTagList nbtList) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", nbtList.toString());
return f;
}
@Override
public void deserialize(NBTTagList nbtList, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagList deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
@Nullable
public NBTTagList deserialize(String s) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@Override
public Object getEntityNBT(Entity entity) {
public NBTTagCompound getEntityNBT(Entity entity) {
net.minecraft.server.v1_8_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
@ -320,7 +427,7 @@ public class NMS_v1_8_R1 implements NMSInterface {
}
@Override
public Object getTileNBT(Block block) {
public NBTTagCompound getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
@ -331,15 +438,13 @@ public class NMS_v1_8_R1 implements NMSInterface {
}
@Override
public Object getItemNBT(ItemStack itemStack) {
public NBTTagCompound getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound NBT = new NBTTagCompound();
net.minecraft.server.v1_8_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
NBT = nmsItem.getTag();
if (NBT == null || NBT.toString().equals("{}")) //Null or empty.
return null;
return new Object[] { NBT };
NBTTagCompound itemNBT = CraftItemStack.asNMSCopy(itemStack).getTag();
if (String.valueOf(itemNBT).equals("{}"))
itemNBT = null;
return itemNBT;
}
@Override
@ -365,21 +470,25 @@ public class NMS_v1_8_R1 implements NMSInterface {
@Override
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound) {
if (compound instanceof NBTTagCompound) {
if (itemStack.getType() == Material.AIR || itemStack == null)
return null;
if (compound == null || compound.toString().equals("{}"))
return itemStack;
net.minecraft.server.v1_8_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
if (compound instanceof NBTTagCompound && itemStack != null) {
if (itemStack.getType() == Material.AIR)
return null;
if (((NBTTagCompound) compound).isEmpty())
return itemStack;
nmsItem.setTag((NBTTagCompound) compound);
ItemStack newItem = CraftItemStack.asCraftMirror(nmsItem);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
} else if (compound == null) {
nmsItem.setTag(null);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
}
return null;
return itemStack;
}
@Override
public Object getFileNBT(File file) {
public NBTTagCompound getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
@ -391,11 +500,9 @@ public class NMS_v1_8_R1 implements NMSInterface {
fileNBT = NBTCompressedStreamTools.a(fis);
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Nothing.
} else {
ex.printStackTrace();
}
}
@ -404,6 +511,7 @@ public class NMS_v1_8_R1 implements NMSInterface {
@Override
public void setFileNBT(File file, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
@ -413,22 +521,34 @@ public class NMS_v1_8_R1 implements NMSInterface {
try {
NBTCompressedStreamTools.a((NBTTagCompound) newCompound, os);
os.close();
} catch (Exception ex) {
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Ignore, just end of the file
} else {
ex.printStackTrace();
}
} finally {
try {
os.close();
} catch (Exception ex) {
if (ex instanceof EOFException) {
; //Ignore.
} else {
ex.printStackTrace();
}
}
}
public NBTBase convertToNBT(Number number) {
if (number instanceof Byte) {
return new NBTTagByte((byte) number);
} else if (number instanceof Short) {
return new NBTTagShort((short) number);
} else if (number instanceof Integer) {
return new NBTTagInt((int) number);
} else if (number instanceof Long) {
return new NBTTagLong((long) number);
} else if (number instanceof Float) {
return new NBTTagFloat((float) number);
} else if (number instanceof Double) {
return new NBTTagDouble((double) number);
}
return null;
}
public NBTTagString convertToNBT(String string) {
return new NBTTagString(string);
}
}

View File

@ -6,8 +6,9 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.io.StreamCorruptedException;
import java.util.Iterator;
import java.util.List;
@ -25,21 +26,25 @@ import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import ch.njol.yggdrasil.Fields;
import net.minecraft.server.v1_8_R2.BlockPosition;
import net.minecraft.server.v1_8_R2.EntityInsentient;
import net.minecraft.server.v1_8_R2.MojangsonParseException;
import net.minecraft.server.v1_8_R2.MojangsonParser;
import net.minecraft.server.v1_8_R2.NBTBase;
import net.minecraft.server.v1_8_R2.NBTCompressedStreamTools;
import net.minecraft.server.v1_8_R2.NBTTagByte;
import net.minecraft.server.v1_8_R2.NBTTagCompound;
import net.minecraft.server.v1_8_R2.NBTTagDouble;
import net.minecraft.server.v1_8_R2.NBTTagEnd;
import net.minecraft.server.v1_8_R2.NBTTagFloat;
import net.minecraft.server.v1_8_R2.NBTTagInt;
import net.minecraft.server.v1_8_R2.NBTTagList;
import net.minecraft.server.v1_8_R2.NBTTagLong;
import net.minecraft.server.v1_8_R2.NBTTagShort;
import net.minecraft.server.v1_8_R2.NBTTagString;
import net.minecraft.server.v1_8_R2.PathfinderGoal;
import net.minecraft.server.v1_8_R2.PathfinderGoalSelector;
@ -58,12 +63,14 @@ public class NMS_v1_8_R2 implements NMSInterface {
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
((NBTTagCompound) compound).remove(toRemove.toString()); //FIXME
for (String s : toRemove) {
((NBTTagCompound) compound).remove(s);
}
}
}
@Override
public Object parseRawNBT(String rawNBT) {
public NBTTagCompound parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
try {
parsedNBT = MojangsonParser.parse(rawNBT);
@ -84,13 +91,13 @@ public class NMS_v1_8_R2 implements NMSInterface {
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
List<Object> contents = new ArrayList<Object>();
Object[] contents = new Object[((NBTTagList) nbtList).size()];
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents.add(getIndex(nbtList, i));
contents[i] = getIndex(nbtList, i);
}
}
return contents.toArray();
return contents;
}
return null;
}
@ -104,26 +111,45 @@ public class NMS_v1_8_R2 implements NMSInterface {
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
((NBTTagList) nbtList).a(index);
}
}
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && toSet instanceof NBTBase) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
if (toSet instanceof NBTBase) {
((NBTTagList) nbtList).a(index, (NBTBase) toSet);
} else if (toSet instanceof Number) {
((NBTTagList) nbtList).a(index, convertToNBT((Number) toSet));
} else if (toSet instanceof String) {
((NBTTagList) nbtList).a(index, convertToNBT((String) toSet));
}
}
}
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
NBTBase value = ((NBTTagList) nbtList).g(index);
if (value instanceof NBTTagEnd)
return null;
else
return value;
if (value instanceof NBTTagByte) {
return ((NBTTagByte) value).f(); //Byte stored inside a NBTNumber
} else if (value instanceof NBTTagShort) {
return ((NBTTagShort) value).e(); //Short inside a NBTNumber
} else if (value instanceof NBTTagInt) {
return ((NBTTagInt) value).d(); //Integer inside a NBTNumber
} else if (value instanceof NBTTagLong) {
return ((NBTTagLong) value).c(); //Long inside a NBTNumber
} else if (value instanceof NBTTagFloat) {
return ((NBTTagFloat) value).h(); //Float inside a NBTNumber
} else if (value instanceof NBTTagDouble) {
return ((NBTTagDouble) value).g(); //Double inside a NBTNumber
} else if (value instanceof NBTTagString) {
return ((NBTTagString) value).a_(); //String inside the NBTTagString
} else if (value instanceof NBTTagList || value instanceof NBTTagCompound) {
return value; //No need to convert anything, these are registered by the addon.
}
}
return null;
}
@ -182,12 +208,14 @@ public class NMS_v1_8_R2 implements NMSInterface {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
@ -211,11 +239,8 @@ public class NMS_v1_8_R2 implements NMSInterface {
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
rawNBT.substring(4);
NBTTagCompound NBT = (NBTTagCompound) parseRawNBT(rawNBT);
if (NBT.toString().equals("{}")) {
return null;
}
NBTTagCompound NBT = null;
NBT = parseRawNBT(rawNBT.substring(4));
return NBT;
}
return null;
@ -230,6 +255,45 @@ public class NMS_v1_8_R2 implements NMSInterface {
public String toVariableNameString(NBTTagCompound compound) {
return "nbt:" + compound.toString();
}
}).serializer(new Serializer<NBTTagCompound>() {
@Override
public Fields serialize(NBTTagCompound compound) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", compound.toString());
return f;
}
@Override
public void deserialize(NBTTagCompound compound, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagCompound deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
@Nullable
public NBTTagCompound deserialize(String s) {
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@ -251,7 +315,15 @@ public class NMS_v1_8_R2 implements NMSInterface {
@Override
public void change(NBTTagList[] nbtList, @Nullable Object[] delta, ChangeMode mode) {
int typeId = 0;
if (delta instanceof Float[]) {
if (delta instanceof Byte[]) {
typeId = 1;
} else if (delta instanceof Short[]) {
typeId = 2;
} else if (delta instanceof Integer[]) {
typeId = 3;
} else if (delta instanceof Long[]) {
typeId = 4;
} else if (delta instanceof Float[]) {
typeId = 5;
} else if (delta instanceof Double[]) {
typeId = 6;
@ -261,8 +333,6 @@ public class NMS_v1_8_R2 implements NMSInterface {
typeId = 9;
} else if (delta instanceof NBTTagCompound[]) {
typeId = 10;
} else if (delta instanceof Integer[]) {
typeId = 11;
} else {
return;
}
@ -271,21 +341,12 @@ public class NMS_v1_8_R2 implements NMSInterface {
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (typeId == 5) {
NBTTagFloat floatTag = new NBTTagFloat((float) delta[0]);
addToList(nbtList[0], floatTag);
} else if (typeId == 6) {
NBTTagDouble doubleTag = new NBTTagDouble((double) delta[0]);
addToList(nbtList[0], doubleTag);
} else if (typeId == 8) {
NBTTagString stringTag = new NBTTagString((String) delta [0]);
addToList(nbtList[0], stringTag);
} else if (typeId == 10) {
addToList(nbtList[0], delta[0]);
} else if (typeId == 11) {
NBTTagInt intTag = new NBTTagInt((int) delta[0]);
addToList(nbtList[0], intTag);
}
if (delta[0] instanceof Number)
addToList(nbtList, convertToNBT((Number) delta[0]));
else if (delta[0] instanceof String)
addToList(nbtList, convertToNBT((String) delta[0]));
else if (delta[0] instanceof NBTTagCompound || delta[0] instanceof NBTTagList)
addToList(nbtList, delta[0]);
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
@ -300,7 +361,13 @@ public class NMS_v1_8_R2 implements NMSInterface {
@Override
@Nullable
public NBTTagList parse(String ignored, ParseContext context) {
public NBTTagList parse(String listString, ParseContext context) {
if (listString.startsWith("[") && listString.endsWith("]")) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:[0:" + listString.substring(1) + "}");
NBTTagList parsedList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return parsedList;
}
return null;
}
@ -313,11 +380,52 @@ public class NMS_v1_8_R2 implements NMSInterface {
public String toVariableNameString(NBTTagList nbtList) {
return nbtList.toString();
}
}).serializer(new Serializer<NBTTagList>() {
@Override
public Fields serialize(NBTTagList nbtList) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", nbtList.toString());
return f;
}
@Override
public void deserialize(NBTTagList nbtList, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagList deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
@Nullable
public NBTTagList deserialize(String s) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@Override
public Object getEntityNBT(Entity entity) {
public NBTTagCompound getEntityNBT(Entity entity) {
net.minecraft.server.v1_8_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
@ -325,7 +433,7 @@ public class NMS_v1_8_R2 implements NMSInterface {
}
@Override
public Object getTileNBT(Block block) {
public NBTTagCompound getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
@ -336,15 +444,13 @@ public class NMS_v1_8_R2 implements NMSInterface {
}
@Override
public Object getItemNBT(ItemStack itemStack) {
public NBTTagCompound getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound NBT = new NBTTagCompound();
net.minecraft.server.v1_8_R2.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
NBT = nmsItem.getTag();
if (NBT == null || NBT.toString().equals("{}")) //Null or empty.
return null;
return new Object[] { NBT };
NBTTagCompound itemNBT = CraftItemStack.asNMSCopy(itemStack).getTag();
if (String.valueOf(itemNBT).equals("{}"))
itemNBT = null;
return itemNBT;
}
@Override
@ -370,21 +476,25 @@ public class NMS_v1_8_R2 implements NMSInterface {
@Override
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound) {
if (compound instanceof NBTTagCompound) {
if (itemStack.getType() == Material.AIR || itemStack == null)
return null;
if (compound == null || compound.toString().equals("{}"))
return itemStack;
net.minecraft.server.v1_8_R2.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
if (compound instanceof NBTTagCompound && itemStack != null) {
if (itemStack.getType() == Material.AIR)
return null;
if (((NBTTagCompound) compound).isEmpty())
return itemStack;
nmsItem.setTag((NBTTagCompound) compound);
ItemStack newItem = CraftItemStack.asCraftMirror(nmsItem);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
} else if (compound == null) {
nmsItem.setTag(null);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
}
return null;
return itemStack;
}
@Override
public Object getFileNBT(File file) {
public NBTTagCompound getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
@ -396,11 +506,9 @@ public class NMS_v1_8_R2 implements NMSInterface {
fileNBT = NBTCompressedStreamTools.a(fis);
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Nothing.
} else {
ex.printStackTrace();
}
}
@ -409,6 +517,7 @@ public class NMS_v1_8_R2 implements NMSInterface {
@Override
public void setFileNBT(File file, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
@ -418,22 +527,34 @@ public class NMS_v1_8_R2 implements NMSInterface {
try {
NBTCompressedStreamTools.a((NBTTagCompound) newCompound, os);
os.close();
} catch (Exception ex) {
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Ignore, just end of the file
} else {
ex.printStackTrace();
}
} finally {
try {
os.close();
} catch (Exception ex) {
if (ex instanceof EOFException) {
; //Ignore.
} else {
ex.printStackTrace();
}
}
}
public NBTBase convertToNBT(Number number) {
if (number instanceof Byte) {
return new NBTTagByte((byte) number);
} else if (number instanceof Short) {
return new NBTTagShort((short) number);
} else if (number instanceof Integer) {
return new NBTTagInt((int) number);
} else if (number instanceof Long) {
return new NBTTagLong((long) number);
} else if (number instanceof Float) {
return new NBTTagFloat((float) number);
} else if (number instanceof Double) {
return new NBTTagDouble((double) number);
}
return null;
}
public NBTTagString convertToNBT(String string) {
return new NBTTagString(string);
}
}

View File

@ -9,13 +9,11 @@ import java.io.IOException;
import java.io.NotSerializableException;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
@ -42,7 +40,6 @@ import net.minecraft.server.v1_8_R3.NBTCompressedStreamTools;
import net.minecraft.server.v1_8_R3.NBTTagByte;
import net.minecraft.server.v1_8_R3.NBTTagCompound;
import net.minecraft.server.v1_8_R3.NBTTagDouble;
import net.minecraft.server.v1_8_R3.NBTTagEnd;
import net.minecraft.server.v1_8_R3.NBTTagFloat;
import net.minecraft.server.v1_8_R3.NBTTagInt;
import net.minecraft.server.v1_8_R3.NBTTagList;
@ -73,7 +70,7 @@ public class NMS_v1_8_R3 implements NMSInterface {
}
@Override
public Object parseRawNBT(String rawNBT) {
public NBTTagCompound parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
try {
parsedNBT = MojangsonParser.parse(rawNBT);
@ -94,13 +91,13 @@ public class NMS_v1_8_R3 implements NMSInterface {
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
List<Object> contents = new ArrayList<Object>();
Object[] contents = new Object[((NBTTagList) nbtList).size()];
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents.add(getIndex(nbtList, i));
contents[i] = getIndex(nbtList, i);
}
}
return contents.toArray();
return contents;
}
return null;
}
@ -114,43 +111,29 @@ public class NMS_v1_8_R3 implements NMSInterface {
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
((NBTTagList) nbtList).a(index);
}
}
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
if (toSet instanceof NBTBase) {
((NBTTagList) nbtList).a(index, (NBTBase) toSet);
} else {
if (toSet instanceof Byte) {
((NBTTagList) nbtList).a(index, new NBTTagByte((byte) toSet));
} else if (toSet instanceof Short) {
((NBTTagList) nbtList).a(index, new NBTTagShort((short) toSet));
} else if (toSet instanceof Integer) {
((NBTTagList) nbtList).a(index, new NBTTagInt((int) toSet));
} else if (toSet instanceof Long) {
((NBTTagList) nbtList).a(index, new NBTTagLong((long) toSet));
} else if (toSet instanceof Float) {
((NBTTagList) nbtList).a(index, new NBTTagFloat((float) toSet));
} else if (toSet instanceof Double) {
((NBTTagList) nbtList).a(index, new NBTTagDouble((double) toSet));
} else if (toSet instanceof Number) {
((NBTTagList) nbtList).a(index, convertToNBT((Number) toSet));
} else if (toSet instanceof String) {
((NBTTagList) nbtList).a(index, new NBTTagString((String) toSet));
} //No need to check for NBTTagList and NBTTagCompound, these extend NBTBase.
((NBTTagList) nbtList).a(index, convertToNBT((String) toSet));
}
}
}
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
NBTBase value = ((NBTTagList) nbtList).g(index);
if (value instanceof NBTTagEnd) {
return null;
} else if (value instanceof NBTTagByte) {
if (value instanceof NBTTagByte) {
return ((NBTTagByte) value).f(); //Byte stored inside a NBTNumber
} else if (value instanceof NBTTagShort) {
return ((NBTTagShort) value).e(); //Short inside a NBTNumber
@ -225,12 +208,14 @@ public class NMS_v1_8_R3 implements NMSInterface {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
@ -254,8 +239,8 @@ public class NMS_v1_8_R3 implements NMSInterface {
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
Bukkit.broadcastMessage("rawNBT: \u00A72" + rawNBT.substring(4));
NBTTagCompound NBT = (NBTTagCompound) parseRawNBT(rawNBT.substring(4));
NBTTagCompound NBT = null;
NBT = parseRawNBT(rawNBT.substring(4));
return NBT;
}
return null;
@ -292,14 +277,16 @@ public class NMS_v1_8_R3 implements NMSInterface {
@Override
protected NBTTagCompound deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound compound = (NBTTagCompound) parseRawNBT(s);
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
@Nullable
public NBTTagCompound deserialize(String s) {
NBTTagCompound compound = (NBTTagCompound) parseRawNBT(s);
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@ -354,32 +341,12 @@ public class NMS_v1_8_R3 implements NMSInterface {
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (typeId == 1) {
NBTTagByte byteTag = new NBTTagByte((byte) delta[0]);
addToList(nbtList[0], byteTag);
} else if (typeId == 2) {
NBTTagShort shortTag = new NBTTagShort((short) delta[0]);
addToList(nbtList[0], shortTag);
} else if (typeId == 3) {
NBTTagInt intTag = new NBTTagInt((int) delta[0]);
addToList(nbtList[0], intTag);
} else if (typeId == 4) {
NBTTagLong longTag = new NBTTagLong((long) delta[0]);
addToList(nbtList[0], longTag);
} else if (typeId == 5) {
NBTTagFloat floatTag = new NBTTagFloat((float) delta[0]);
addToList(nbtList[0], floatTag);
} else if (typeId == 6) {
NBTTagDouble doubleTag = new NBTTagDouble((double) delta[0]);
addToList(nbtList[0], doubleTag);
} else if (typeId == 8) {
NBTTagString stringTag = new NBTTagString((String) delta [0]);
addToList(nbtList[0], stringTag);
} else if (typeId == 9) {
addToList(nbtList[0], delta[0]);
} else if (typeId == 10) {
addToList(nbtList[0], delta[0]);
}
if (delta[0] instanceof Number)
addToList(nbtList, convertToNBT((Number) delta[0]));
else if (delta[0] instanceof String)
addToList(nbtList, convertToNBT((String) delta[0]));
else if (delta[0] instanceof NBTTagCompound || delta[0] instanceof NBTTagList)
addToList(nbtList, delta[0]);
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
@ -396,8 +363,9 @@ public class NMS_v1_8_R3 implements NMSInterface {
@Nullable
public NBTTagList parse(String listString, ParseContext context) {
if (listString.startsWith("[") && listString.endsWith("]")) {
NBTTagCompound NBT = (NBTTagCompound) parseRawNBT("{SkStuffIsCool:[0:" + listString.substring(1) + "}");
NBTTagList parsedList = (NBTTagList) NBT.get("SkStuffIsCool");
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:[0:" + listString.substring(1) + "}");
NBTTagList parsedList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return parsedList;
}
return null;
@ -434,7 +402,8 @@ public class NMS_v1_8_R3 implements NMSInterface {
@Override
protected NBTTagList deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound tempNBT = (NBTTagCompound) parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@ -442,7 +411,8 @@ public class NMS_v1_8_R3 implements NMSInterface {
@Override
@Nullable
public NBTTagList deserialize(String s) {
NBTTagCompound tempNBT = (NBTTagCompound) parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@ -455,7 +425,7 @@ public class NMS_v1_8_R3 implements NMSInterface {
}
@Override
public Object getEntityNBT(Entity entity) {
public NBTTagCompound getEntityNBT(Entity entity) {
net.minecraft.server.v1_8_R3.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
@ -463,7 +433,7 @@ public class NMS_v1_8_R3 implements NMSInterface {
}
@Override
public Object getTileNBT(Block block) {
public NBTTagCompound getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
@ -474,12 +444,11 @@ public class NMS_v1_8_R3 implements NMSInterface {
}
@Override
public Object getItemNBT(ItemStack itemStack) {
public NBTTagCompound getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound itemNBT = CraftItemStack.asNMSCopy(itemStack).getTag();
Bukkit.broadcastMessage(String.valueOf(itemNBT));
if (itemNBT.isEmpty())
if (String.valueOf(itemNBT).equals("{}"))
itemNBT = null;
return itemNBT;
}
@ -525,7 +494,7 @@ public class NMS_v1_8_R3 implements NMSInterface {
}
@Override
public Object getFileNBT(File file) {
public NBTTagCompound getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
@ -542,22 +511,13 @@ public class NMS_v1_8_R3 implements NMSInterface {
} else {
ex.printStackTrace();
}
} finally {
try {
fis.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Ignore it!
} else {
ex.printStackTrace();
}
}
}
return fileNBT;
}
@Override
public void setFileNBT(File file, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
@ -573,16 +533,28 @@ public class NMS_v1_8_R3 implements NMSInterface {
} else {
ex.printStackTrace();
}
} finally {
try {
os.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Ignore.
} else {
ex.printStackTrace();
}
}
}
public NBTBase convertToNBT(Number number) {
if (number instanceof Byte) {
return new NBTTagByte((byte) number);
} else if (number instanceof Short) {
return new NBTTagShort((short) number);
} else if (number instanceof Integer) {
return new NBTTagInt((int) number);
} else if (number instanceof Long) {
return new NBTTagLong((long) number);
} else if (number instanceof Float) {
return new NBTTagFloat((float) number);
} else if (number instanceof Double) {
return new NBTTagDouble((double) number);
}
return null;
}
public NBTTagString convertToNBT(String string) {
return new NBTTagString(string);
}
}

View File

@ -0,0 +1,562 @@
package me.TheBukor.SkStuff.util;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_9_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import ch.njol.yggdrasil.Fields;
import net.minecraft.server.v1_9_R1.BlockPosition;
import net.minecraft.server.v1_9_R1.EntityInsentient;
import net.minecraft.server.v1_9_R1.IBlockData;
import net.minecraft.server.v1_9_R1.MojangsonParseException;
import net.minecraft.server.v1_9_R1.MojangsonParser;
import net.minecraft.server.v1_9_R1.NBTBase;
import net.minecraft.server.v1_9_R1.NBTCompressedStreamTools;
import net.minecraft.server.v1_9_R1.NBTTagByte;
import net.minecraft.server.v1_9_R1.NBTTagCompound;
import net.minecraft.server.v1_9_R1.NBTTagDouble;
import net.minecraft.server.v1_9_R1.NBTTagFloat;
import net.minecraft.server.v1_9_R1.NBTTagInt;
import net.minecraft.server.v1_9_R1.NBTTagList;
import net.minecraft.server.v1_9_R1.NBTTagLong;
import net.minecraft.server.v1_9_R1.NBTTagShort;
import net.minecraft.server.v1_9_R1.NBTTagString;
import net.minecraft.server.v1_9_R1.PathfinderGoal;
import net.minecraft.server.v1_9_R1.PathfinderGoalSelector;
import net.minecraft.server.v1_9_R1.TileEntity;
import net.minecraft.server.v1_9_R1.World;
public class NMS_v1_9_R1 implements NMSInterface {
@Override
public void addToCompound(Object compound, Object toAdd) {
if (compound instanceof NBTTagCompound && toAdd instanceof NBTTagCompound) {
((NBTTagCompound) compound).a((NBTTagCompound) toAdd);
}
}
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
for (String s : toRemove) {
((NBTTagCompound) compound).remove(s);
}
}
}
@Override
public NBTTagCompound parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
try {
parsedNBT = MojangsonParser.parse(rawNBT);
} catch (MojangsonParseException ex) {
Skript.warning("Error when parsing NBT - " + ex.getMessage());
}
return parsedNBT;
}
@Override
public int getContentsId(Object nbtList) {
if (nbtList instanceof NBTTagList) {
return ((NBTTagList) nbtList).d();
}
return 0;
}
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
Object[] contents = new Object[((NBTTagList) nbtList).size()];
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents[i] = getIndex(nbtList, i);
}
}
return contents;
}
return null;
}
@Override
public void addToList(Object nbtList, Object toAdd) {
if (nbtList instanceof NBTTagList && toAdd instanceof NBTBase) {
((NBTTagList) nbtList).add((NBTBase) toAdd);
}
}
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
((NBTTagList) nbtList).remove(index);
}
}
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
if (toSet instanceof NBTBase) {
((NBTTagList) nbtList).a(index, (NBTBase) toSet);
} else if (toSet instanceof Number) {
((NBTTagList) nbtList).a(index, convertToNBT((Number) toSet));
} else if (toSet instanceof String) {
((NBTTagList) nbtList).a(index, convertToNBT((String) toSet));
}
}
}
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
NBTBase value = ((NBTTagList) nbtList).h(index);
if (value instanceof NBTTagByte) {
return ((NBTTagByte) value).f(); //Byte stored inside a NBTNumber
} else if (value instanceof NBTTagShort) {
return ((NBTTagShort) value).e(); //Short inside a NBTNumber
} else if (value instanceof NBTTagInt) {
return ((NBTTagInt) value).d(); //Integer inside a NBTNumber
} else if (value instanceof NBTTagLong) {
return ((NBTTagLong) value).c(); //Long inside a NBTNumber
} else if (value instanceof NBTTagFloat) {
return ((NBTTagFloat) value).h(); //Float inside a NBTNumber
} else if (value instanceof NBTTagDouble) {
return ((NBTTagDouble) value).g(); //Double inside a NBTNumber
} else if (value instanceof NBTTagString) {
return ((NBTTagString) value).a_(); //String inside the NBTTagString
} else if (value instanceof NBTTagList || value instanceof NBTTagCompound) {
return value; //No need to convert anything, these are registered by the addon.
}
}
return null;
}
@Override
public void removePathfinderGoal(Object entity, Class<?> goalClass, boolean isTargetSelector) {
if (entity instanceof EntityInsentient) {
((EntityInsentient) entity).setGoalTarget(null);
if (isTargetSelector) {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).targetSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
} else {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).goalSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
}
}
}
@Override
public void addPathfinderGoal(Object entity, int priority, Object goal, boolean isTargetSelector) {
if (entity instanceof EntityInsentient && goal instanceof PathfinderGoal) {
if (isTargetSelector)
((EntityInsentient) entity).targetSelector.a(priority, (PathfinderGoal) goal);
else
((EntityInsentient) entity).goalSelector.a(priority, (PathfinderGoal) goal);
}
}
@Override
public void registerCompoundClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagCompound>(NBTTagCompound.class, "compound").user("((nbt)?( ?tag)?) ?compounds?").name("NBT Compound").changer(new Changer<NBTTagCompound>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.SET) {
return CollectionUtils.array(String[].class, NBTTagCompound[].class);
}
return null;
}
@Override
public void change(NBTTagCompound[] NBT, @Nullable Object[] delta, ChangeMode mode) {
if (mode == ChangeMode.SET) {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
}
} else if (mode == ChangeMode.REMOVE) {
if (delta[0] instanceof NBTTagCompound)
return;
for (Object s : delta) {
NBT[0].remove((String) s);
}
}
}
}).parser(new Parser<NBTTagCompound>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
NBTTagCompound NBT = null;
NBT = parseRawNBT(rawNBT.substring(4));
return NBT;
}
return null;
}
@Override
public String toString(NBTTagCompound compound, int arg1) {
return compound.toString();
}
@Override
public String toVariableNameString(NBTTagCompound compound) {
return "nbt:" + compound.toString();
}
}).serializer(new Serializer<NBTTagCompound>() {
@Override
public Fields serialize(NBTTagCompound compound) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", compound.toString());
return f;
}
@Override
public void deserialize(NBTTagCompound compound, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagCompound deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
@Nullable
public NBTTagCompound deserialize(String s) {
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@Override
public void registerNBTListClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagList>(NBTTagList.class, "nbtlist").user("nbt ?list ?(tag)?").name("NBT List").changer(new Changer<NBTTagList>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
return CollectionUtils.array(Float[].class, Double[].class, String[].class, NBTTagCompound[].class, Integer[].class, NBTTagList[].class);
}
return null;
}
@Override
public void change(NBTTagList[] nbtList, @Nullable Object[] delta, ChangeMode mode) {
int typeId = 0;
if (delta instanceof Byte[]) {
typeId = 1;
} else if (delta instanceof Short[]) {
typeId = 2;
} else if (delta instanceof Integer[]) {
typeId = 3;
} else if (delta instanceof Long[]) {
typeId = 4;
} else if (delta instanceof Float[]) {
typeId = 5;
} else if (delta instanceof Double[]) {
typeId = 6;
} else if (delta instanceof String[]) {
typeId = 8;
} else if (delta instanceof NBTTagList[]) {
typeId = 9;
} else if (delta instanceof NBTTagCompound[]) {
typeId = 10;
} else {
return;
}
if (mode == ChangeMode.SET) {
if (typeId == 9)
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (delta[0] instanceof Number)
addToList(nbtList, convertToNBT((Number) delta[0]));
else if (delta[0] instanceof String)
addToList(nbtList, convertToNBT((String) delta[0]));
else if (delta[0] instanceof NBTTagCompound || delta[0] instanceof NBTTagList)
addToList(nbtList, delta[0]);
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
}
}
}).parser(new Parser<NBTTagList>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagList parse(String listString, ParseContext context) {
if (listString.startsWith("[") && listString.endsWith("]")) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:[0:" + listString.substring(1) + "}");
NBTTagList parsedList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return parsedList;
}
return null;
}
@Override
public String toString(NBTTagList nbtList, int arg1) {
return nbtList.toString();
}
@Override
public String toVariableNameString(NBTTagList nbtList) {
return nbtList.toString();
}
}).serializer(new Serializer<NBTTagList>() {
@Override
public Fields serialize(NBTTagList nbtList) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", nbtList.toString());
return f;
}
@Override
public void deserialize(NBTTagList nbtList, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagList deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
@Nullable
public NBTTagList deserialize(String s) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@Override
public NBTTagCompound getEntityNBT(Entity entity) {
net.minecraft.server.v1_9_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
return NBT;
}
@Override
public NBTTagCompound getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return null;
tileEntity.save(NBT);
return NBT;
}
@Override
public NBTTagCompound getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound itemNBT = CraftItemStack.asNMSCopy(itemStack).getTag();
if (String.valueOf(itemNBT).equals("{}"))
itemNBT = null;
return itemNBT;
}
@Override
public void setEntityNBT(Entity entity, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
net.minecraft.server.v1_9_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
nmsEntity.f((NBTTagCompound) newCompound);
}
}
@Override
public void setTileNBT(Block block, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return;
tileEntity.a((NBTTagCompound) newCompound);
tileEntity.update();
IBlockData tileEntType = nmsWorld.getType(new BlockPosition(block.getX(), block.getY(), block.getZ()));
nmsWorld.notify(tileEntity.getPosition(), tileEntType, tileEntType, 3);
}
}
@Override
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound) {
net.minecraft.server.v1_9_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
if (compound instanceof NBTTagCompound && itemStack != null) {
if (itemStack.getType() == Material.AIR)
return null;
if (((NBTTagCompound) compound).isEmpty())
return itemStack;
nmsItem.setTag((NBTTagCompound) compound);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
} else if (compound == null) {
nmsItem.setTag(null);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
}
return itemStack;
}
@Override
public NBTTagCompound getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
return null; //File doesn't exist.
}
NBTTagCompound fileNBT = null;
try {
fileNBT = NBTCompressedStreamTools.a(fis);
fis.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Nothing.
} else {
ex.printStackTrace();
}
}
return fileNBT;
}
@Override
public void setFileNBT(File file, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
try {
NBTCompressedStreamTools.a((NBTTagCompound) newCompound, os);
os.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Ignore, just end of the file
} else {
ex.printStackTrace();
}
}
}
}
public NBTBase convertToNBT(Number number) {
if (number instanceof Byte) {
return new NBTTagByte((byte) number);
} else if (number instanceof Short) {
return new NBTTagShort((short) number);
} else if (number instanceof Integer) {
return new NBTTagInt((int) number);
} else if (number instanceof Long) {
return new NBTTagLong((long) number);
} else if (number instanceof Float) {
return new NBTTagFloat((float) number);
} else if (number instanceof Double) {
return new NBTTagDouble((double) number);
}
return null;
}
public NBTTagString convertToNBT(String string) {
return new NBTTagString(string);
}
}

View File

@ -1,16 +1,15 @@
package me.TheBukor.SkStuff.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import org.bukkit.Bukkit;
public class ReflectionUtils {
public static Class<?> getNMSClass(String classString, boolean isArray) {
public static Class<?> getNMSClass(String classString) {
String version = getVersion();
String name = "net.minecraft.server." + version + classString;
if (isArray)
name = "[L" + name + ";";
Class<?> nmsClass = null;
try {
nmsClass = Class.forName(name);
@ -58,6 +57,17 @@ public class ReflectionUtils {
}
}
public static Constructor<?> getConstructor(String constructor, Class<?> clazz, Object object, Class<?> ... params) {
Constructor<?> constr = null;
try {
constr = clazz.getDeclaredConstructor(params);
constr.setAccessible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
return constr;
}
public static String getVersion() {
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3] + ".";
}