|
@@ -1,8 +1,6 @@
|
|
|
package me.km.snuviscript.commands;
|
|
|
|
|
|
import me.hammerle.snuviscript.code.ScriptManager;
|
|
|
-import me.km.entities.EntityItemProjectile;
|
|
|
-import me.km.scheduler.SnuviScheduler;
|
|
|
import static me.km.snuviscript.commands.CommandUtils.getNamedClass;
|
|
|
import me.km.utils.Location;
|
|
|
import me.km.utils.Mapper;
|
|
@@ -17,7 +15,6 @@ import net.minecraft.entity.passive.TameableEntity;
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
import net.minecraft.entity.player.ServerPlayerEntity;
|
|
|
import net.minecraft.entity.projectile.*;
|
|
|
-import net.minecraft.inventory.EquipmentSlotType;
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
|
|
import net.minecraft.nbt.JsonToNBT;
|
|
@@ -34,21 +31,17 @@ import net.minecraft.world.server.ServerWorld;
|
|
|
|
|
|
public class EntityCommands {
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- public static void registerFunctions(ScriptManager sm, SnuviScheduler scheduler) {
|
|
|
+ public static void registerFunctions(ScriptManager sm) {
|
|
|
sm.registerConsumer("entity.setnopickup", (sc, in) -> {
|
|
|
- ((AbstractArrowEntity) in[0].get(sc)).pickupStatus = AbstractArrowEntity.PickupStatus.DISALLOWED;
|
|
|
- });
|
|
|
- sm.registerFunction("entity.shootprojectile", (sc, in) -> launchProjectile((LivingEntity) in[0].get(sc),
|
|
|
- getNamedClass(in[1].getString(sc)), in[2].getDouble(sc), in.length >= 4 ? in[3].get(sc) : null));
|
|
|
- sm.registerFunction("entity.isblocking",
|
|
|
- (sc, in) -> ((LivingEntity) in[0].get(sc)).isActiveItemStackBlocking());
|
|
|
- sm.registerFunction("entity.getarmorthoughness",
|
|
|
- (sc, in) -> ((LivingEntity) in[0].get(sc)).getAttribute(Attributes.ARMOR_TOUGHNESS).getValue());
|
|
|
- sm.registerFunction("entity.getarmor",
|
|
|
- (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getTotalArmorValue());
|
|
|
- sm.registerFunction("entity.getenchantmentmodifier", (sc, in) -> EnchantmentHelper.getEnchantmentModifierDamage(
|
|
|
- ((LivingEntity) in[0].get(sc)).getArmorInventoryList(), (DamageSource) in[1].get(sc)));
|
|
|
- sm.registerConsumer("entity.setburning", (sc, in) -> ((Entity) in[0].get(sc)).forceFireTicks(in[1].getInt(sc)));
|
|
|
+ ((AbstractArrowEntity) in[0].get(sc)).pickupStatus =
|
|
|
+ AbstractArrowEntity.PickupStatus.DISALLOWED;
|
|
|
+ });
|
|
|
+ sm.registerFunction("entity.getenchantmentmodifier",
|
|
|
+ (sc, in) -> EnchantmentHelper.getEnchantmentModifierDamage(
|
|
|
+ ((LivingEntity) in[0].get(sc)).getArmorInventoryList(),
|
|
|
+ (DamageSource) in[1].get(sc)));
|
|
|
+ sm.registerConsumer("entity.setburning",
|
|
|
+ (sc, in) -> ((Entity) in[0].get(sc)).forceFireTicks(in[1].getInt(sc)));
|
|
|
sm.registerFunction("entity.isburning", (sc, in) -> ((Entity) in[0].get(sc)).isBurning());
|
|
|
sm.registerFunction("entity.getlook", (sc, in) -> {
|
|
|
Object[] o = new Object[3];
|
|
@@ -67,40 +60,17 @@ public class EntityCommands {
|
|
|
return o;
|
|
|
});
|
|
|
sm.registerFunction("entity.getlocation", (sc, in) -> new Location((Entity) in[0].get(sc)));
|
|
|
- sm.registerConsumer("entity.heal", (sc, in) -> {
|
|
|
- LivingEntity liv = (LivingEntity) in[0].get(sc);
|
|
|
- float heal = in[1].getFloat(sc);
|
|
|
- scheduler.scheduleTask(() -> liv.heal(heal));
|
|
|
- });
|
|
|
- sm.registerConsumer("entity.damage", (sc, in) -> {
|
|
|
- LivingEntity liv = (LivingEntity) in[0].get(sc);
|
|
|
- float damage = in[1].getFloat(sc);
|
|
|
- DamageSource damageSource = (in.length >= 3) ? (DamageSource) in[2].get(sc) : DamageSource.GENERIC;
|
|
|
- scheduler.scheduleTask(() -> liv.attackEntityFrom(damageSource, damage));
|
|
|
- });
|
|
|
- sm.registerFunction("entity.fromsource", (sc, in) -> {
|
|
|
- DamageSource ds = (DamageSource) in[0].get(sc);
|
|
|
- Entity ent = ds.getTrueSource();
|
|
|
- if (ent == null) {
|
|
|
- return ds.getImmediateSource();
|
|
|
- }
|
|
|
- return ent;
|
|
|
- });
|
|
|
- sm.registerAlias("damage.get", "entity.getdamagesource");
|
|
|
- sm.registerFunction("entity.getmaxhealth", (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getMaxHealth());
|
|
|
- sm.registerFunction("entity.gethealth", (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getHealth());
|
|
|
- sm.registerConsumer("entity.sethealth",
|
|
|
- (sc, in) -> ((LivingEntity) in[0].get(sc)).setHealth(in[1].getFloat(sc)));
|
|
|
sm.registerConsumer("entity.setname", (sc, in) -> {
|
|
|
Entity ent = (Entity) in[0].get(sc);
|
|
|
ent.setCustomName(new StringTextComponent(in[1].getString(sc)));
|
|
|
- if (in.length >= 3) {
|
|
|
+ if(in.length >= 3) {
|
|
|
ent.setCustomNameVisible(in[2].getBoolean(sc));
|
|
|
return;
|
|
|
}
|
|
|
ent.setCustomNameVisible(false);
|
|
|
});
|
|
|
- sm.registerFunction("entity.getname", (sc, in) -> ((Entity) in[0].get(sc)).getDisplayName().getString());
|
|
|
+ sm.registerFunction("entity.getname",
|
|
|
+ (sc, in) -> ((Entity) in[0].get(sc)).getDisplayName().getString());
|
|
|
sm.registerConsumer("entity.throw", (sc, in) -> {
|
|
|
Entity ent = (Entity) in[0].get(sc);
|
|
|
ent.setMotion(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc));
|
|
@@ -109,14 +79,14 @@ public class EntityCommands {
|
|
|
sm.registerConsumer("entity.teleport", (sc, in) -> {
|
|
|
Entity ent = (Entity) in[0].get(sc);
|
|
|
Location l = (Location) in[1].get(sc);
|
|
|
- if (l.getWorld() == null) {
|
|
|
+ if(l.getWorld() == null) {
|
|
|
throw new IllegalArgumentException("world must not be null");
|
|
|
}
|
|
|
- if (ent instanceof ServerPlayerEntity) {
|
|
|
+ if(ent instanceof ServerPlayerEntity) {
|
|
|
ServerPlayerEntity p = (ServerPlayerEntity) ent;
|
|
|
|
|
|
p.stopRiding();
|
|
|
- if (p.isSleeping()) {
|
|
|
+ if(p.isSleeping()) {
|
|
|
p.stopSleepInBed(true, true);
|
|
|
}
|
|
|
|
|
@@ -124,7 +94,7 @@ public class EntityCommands {
|
|
|
float pitch = l.getPitch() != 0.0f ? l.getPitch() : ent.rotationPitch;
|
|
|
p.teleport((ServerWorld) l.getWorld(), l.getX(), l.getY(), l.getZ(), yaw, pitch);
|
|
|
} else {
|
|
|
- if (ent.world != l.getWorld()) {
|
|
|
+ if(ent.world != l.getWorld()) {
|
|
|
ServerWorld ws = (ServerWorld) l.getWorld();
|
|
|
ent.changeDimension(ws);
|
|
|
}
|
|
@@ -133,55 +103,16 @@ public class EntityCommands {
|
|
|
ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), yaw, pitch);
|
|
|
}
|
|
|
});
|
|
|
- sm.registerConsumer("entity.setequip", (sc, in) -> {
|
|
|
- LivingEntity liv = (LivingEntity) in[0].get(sc);
|
|
|
- ItemStack stack = ((ItemStack) in[2].get(sc)).copy();
|
|
|
- switch (in[1].getString(sc)) {
|
|
|
- case "hand":
|
|
|
- liv.setItemStackToSlot(EquipmentSlotType.MAINHAND, stack);
|
|
|
- return;
|
|
|
- case "head":
|
|
|
- liv.setItemStackToSlot(EquipmentSlotType.HEAD, stack);
|
|
|
- return;
|
|
|
- case "chest":
|
|
|
- liv.setItemStackToSlot(EquipmentSlotType.CHEST, stack);
|
|
|
- return;
|
|
|
- case "legs":
|
|
|
- liv.setItemStackToSlot(EquipmentSlotType.LEGS, stack);
|
|
|
- return;
|
|
|
- case "feet":
|
|
|
- liv.setItemStackToSlot(EquipmentSlotType.FEET, stack);
|
|
|
- return;
|
|
|
- case "offhand":
|
|
|
- liv.setItemStackToSlot(EquipmentSlotType.OFFHAND, stack);
|
|
|
- }
|
|
|
- });
|
|
|
- sm.registerFunction("entity.getequip", (sc, in) -> {
|
|
|
- LivingEntity liv = (LivingEntity) in[0].get(sc);
|
|
|
- switch (in[1].getString(sc)) {
|
|
|
- case "hand":
|
|
|
- return liv.getItemStackFromSlot(EquipmentSlotType.MAINHAND);
|
|
|
- case "head":
|
|
|
- return liv.getItemStackFromSlot(EquipmentSlotType.HEAD);
|
|
|
- case "chest":
|
|
|
- return liv.getItemStackFromSlot(EquipmentSlotType.CHEST);
|
|
|
- case "legs":
|
|
|
- return liv.getItemStackFromSlot(EquipmentSlotType.LEGS);
|
|
|
- case "feet":
|
|
|
- return liv.getItemStackFromSlot(EquipmentSlotType.FEET);
|
|
|
- case "offhand":
|
|
|
- return liv.getItemStackFromSlot(EquipmentSlotType.OFFHAND);
|
|
|
- }
|
|
|
- return ItemStack.EMPTY;
|
|
|
- });
|
|
|
+
|
|
|
sm.registerConsumer("entity.removeall", (sc, in) -> {
|
|
|
- Class<? extends Entity> c = (Class<? extends Entity>) getNamedClass(in[0].getString(sc));
|
|
|
- if (c == Entity.class) {
|
|
|
+ Class<? extends Entity> c =
|
|
|
+ (Class<? extends Entity>) getNamedClass(in[0].getString(sc));
|
|
|
+ if(c == Entity.class) {
|
|
|
return;
|
|
|
}
|
|
|
Location l = (Location) in[1].get(sc);
|
|
|
- Utils.getEntities(l.getWorld(), l.getX(), l.getY(), l.getZ(), in[2].getDouble(sc), c).stream()
|
|
|
- .forEach(ent -> {
|
|
|
+ Utils.getEntities(l.getWorld(), l.getX(), l.getY(), l.getZ(), in[2].getDouble(sc), c)
|
|
|
+ .stream().forEach(ent -> {
|
|
|
ent.remove();
|
|
|
});
|
|
|
});
|
|
@@ -204,14 +135,15 @@ public class EntityCommands {
|
|
|
sm.registerConsumer("entity.addeffect", (sc, in) -> {
|
|
|
LivingEntity base = (LivingEntity) in[0].get(sc);
|
|
|
Effect potion = Mapper.getPotion(in[1].getString(sc));
|
|
|
- if (potion == null) {
|
|
|
+ if(potion == null) {
|
|
|
throw new IllegalArgumentException("potion does not exist");
|
|
|
}
|
|
|
- if (base.isPotionActive(potion)) {
|
|
|
+ if(base.isPotionActive(potion)) {
|
|
|
base.removePotionEffect(potion);
|
|
|
}
|
|
|
boolean showParticles = in.length >= 5 ? in[4].getBoolean(sc) : true;
|
|
|
- base.addPotionEffect(new EffectInstance(potion, in[2].getInt(sc), in[3].getInt(sc), false, showParticles));
|
|
|
+ base.addPotionEffect(new EffectInstance(potion, in[2].getInt(sc), in[3].getInt(sc),
|
|
|
+ false, showParticles));
|
|
|
});
|
|
|
sm.registerConsumer("entity.cleareffects", (sc, in) -> {
|
|
|
((LivingEntity) in[0].get(sc)).clearActivePotions();
|
|
@@ -235,8 +167,9 @@ public class EntityCommands {
|
|
|
return Utils.getEntity(l.getWorld(), l.getX(), l.getY(), l.getZ(), in[1].getDouble(sc),
|
|
|
(Class<? extends Entity>) getNamedClass(in[2].getString(sc)));
|
|
|
});
|
|
|
- sm.registerFunction("entity.getpotiontype", (sc, in) -> PotionUtils
|
|
|
- .getPotionFromItem(((PotionEntity) in[0].get(sc)).getItem()).getRegistryName().toString());
|
|
|
+ sm.registerFunction("entity.getpotiontype",
|
|
|
+ (sc, in) -> PotionUtils.getPotionFromItem(((PotionEntity) in[0].get(sc)).getItem())
|
|
|
+ .getRegistryName().toString());
|
|
|
sm.registerConsumer("entity.setgravity", (sc, in) -> {
|
|
|
((Entity) in[0].get(sc)).setNoGravity(!in[1].getBoolean(sc));
|
|
|
});
|
|
@@ -247,119 +180,66 @@ public class EntityCommands {
|
|
|
sm.registerFunction("entity.spawn", (sc, in) -> {
|
|
|
ResourceLocation type = new ResourceLocation(in[0].getString(sc));
|
|
|
Location l = (Location) in[1].get(sc);
|
|
|
- if (!World.isInvalidPosition(l.getBlockPos())) {
|
|
|
+ if(!World.isInvalidPosition(l.getBlockPos())) {
|
|
|
return null;
|
|
|
}
|
|
|
- CompoundNBT nbt = in.length >= 3 ? JsonToNBT.getTagFromJson(in[2].getString(sc)) : new CompoundNBT();
|
|
|
+ CompoundNBT nbt = in.length >= 3 ? JsonToNBT.getTagFromJson(in[2].getString(sc))
|
|
|
+ : new CompoundNBT();
|
|
|
nbt.putString("id", type.toString());
|
|
|
ServerWorld sw = (ServerWorld) l.getWorld();
|
|
|
Entity ent = EntityType.loadEntityAndExecute(nbt, sw, (e) -> {
|
|
|
- e.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), e.rotationYaw, e.rotationPitch);
|
|
|
+ e.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), e.rotationYaw,
|
|
|
+ e.rotationPitch);
|
|
|
return e;
|
|
|
});
|
|
|
- if (ent == null) {
|
|
|
+ if(ent == null) {
|
|
|
return ent;
|
|
|
}
|
|
|
- if (ent instanceof MobEntity) {
|
|
|
+ if(ent instanceof MobEntity) {
|
|
|
((MobEntity) ent).onInitialSpawn(sw, sw.getDifficultyForLocation(ent.getPosition()),
|
|
|
SpawnReason.COMMAND, null, null);
|
|
|
}
|
|
|
- if (!sw.func_242106_g(ent)) {
|
|
|
+ if(!sw.func_242106_g(ent)) {
|
|
|
return null;
|
|
|
}
|
|
|
return ent;
|
|
|
});
|
|
|
sm.registerFunction("entity.near", (sc, in) -> {
|
|
|
Object o = in[0].get(sc);
|
|
|
- if (o instanceof Location) {
|
|
|
+ if(o instanceof Location) {
|
|
|
return Utils.getEntities((Location) o, in[1].getDouble(sc));
|
|
|
}
|
|
|
return Utils.getEntities((Entity) o, in[1].getDouble(sc));
|
|
|
});
|
|
|
sm.registerConsumer("entity.setspeed", (sc, in) -> {
|
|
|
- ((LivingEntity) in[0].get(sc)).getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(in[1].getDouble(sc));
|
|
|
+ ((LivingEntity) in[0].get(sc)).getAttribute(Attributes.MOVEMENT_SPEED)
|
|
|
+ .setBaseValue(in[1].getDouble(sc));
|
|
|
});
|
|
|
sm.registerConsumer("entity.setgrowingage", (sc, in) -> {
|
|
|
((AgeableEntity) in[0].get(sc)).setGrowingAge(in[1].getInt(sc));
|
|
|
});
|
|
|
sm.registerFunction("entity.gettype",
|
|
|
(sc, in) -> ((Entity) in[0].get(sc)).getType().getRegistryName().getPath());
|
|
|
- sm.registerFunction("entity.issneaking", (sc, in) -> ((Entity) in[0].get(sc)).isCrouching());
|
|
|
- sm.registerFunction("entity.issneaking", (sc, in) -> ((Entity) in[0].get(sc)).isCrouching());
|
|
|
- sm.registerFunction("sheep.issheared", (sc, in) -> ((SheepEntity) in[0].get(sc)).getSheared());
|
|
|
- sm.registerFunction("sheep.getcolor", (sc, in) -> ((SheepEntity) in[0].get(sc)).getFleeceColor().toString());
|
|
|
- sm.registerConsumer("creeper.explode", (sc, in) -> ((CreeperEntity) in[0].get(sc)).ignite());
|
|
|
+ sm.registerFunction("entity.issneaking",
|
|
|
+ (sc, in) -> ((Entity) in[0].get(sc)).isCrouching());
|
|
|
+ sm.registerFunction("entity.issneaking",
|
|
|
+ (sc, in) -> ((Entity) in[0].get(sc)).isCrouching());
|
|
|
+ sm.registerFunction("sheep.issheared",
|
|
|
+ (sc, in) -> ((SheepEntity) in[0].get(sc)).getSheared());
|
|
|
+ sm.registerFunction("sheep.getcolor",
|
|
|
+ (sc, in) -> ((SheepEntity) in[0].get(sc)).getFleeceColor().toString());
|
|
|
+ sm.registerConsumer("creeper.explode",
|
|
|
+ (sc, in) -> ((CreeperEntity) in[0].get(sc)).ignite());
|
|
|
sm.registerFunction("pet.istamed", (sc, in) -> ((TameableEntity) in[0].get(sc)).isTamed());
|
|
|
sm.registerConsumer("pet.settamed", (sc, in) -> {
|
|
|
TameableEntity t = (TameableEntity) in[0].get(sc);
|
|
|
boolean b = in[1].getBoolean(sc);
|
|
|
- if (b) {
|
|
|
+ if(b) {
|
|
|
t.setTamedBy((PlayerEntity) in[2].get(sc));
|
|
|
}
|
|
|
t.setTamed(b);
|
|
|
});
|
|
|
- sm.registerFunction("pet.getowner", (sc, in) -> ((TameableEntity) in[0].get(sc)).getOwner());
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- private static <T> T launchProjectile(LivingEntity liv, Class<? extends T> projectile, double scale, Object data) {
|
|
|
- World w = liv.world;
|
|
|
- Entity launch = null;
|
|
|
-
|
|
|
- if (EntityItemProjectile.class == projectile) {
|
|
|
- if (data == null) {
|
|
|
- throw new NullPointerException("Data musn't be null for EntityItemProjectile");
|
|
|
- }
|
|
|
- ItemStack stack = (ItemStack) data;
|
|
|
- if (stack.isEmpty()) {
|
|
|
- throw new IllegalArgumentException("Empty ItemStack not allowed here");
|
|
|
- }
|
|
|
- launch = new EntityItemProjectile(liv, stack.copy());
|
|
|
- ((EntityItemProjectile) launch).setHeadingFromThrower(liv, liv.rotationPitch, liv.rotationYaw, 0.0f, 1.5f,
|
|
|
- 1.0f);
|
|
|
- } else if (SnowballEntity.class == projectile) {
|
|
|
- launch = new SnowballEntity(w, liv);
|
|
|
- ((SnowballEntity) launch).shoot(liv.rotationPitch, liv.rotationYaw, 0.0f, 1.5f, 1.0f);
|
|
|
- } else if (EggEntity.class == projectile) {
|
|
|
- launch = new EggEntity(w, liv);
|
|
|
- ((EggEntity) launch).shoot(liv.rotationPitch, liv.rotationYaw, 0.0f, 1.5f, 1.0f);
|
|
|
- } else if (EnderPearlEntity.class == projectile) {
|
|
|
- launch = new EnderPearlEntity(w, liv);
|
|
|
- ((EnderPearlEntity) launch).shoot(liv.rotationPitch, liv.rotationYaw, 0.0f, 1.5f, 1.0f);
|
|
|
- } else if (PotionEntity.class == projectile) {
|
|
|
- launch = new PotionEntity(w, liv);
|
|
|
- ((PotionEntity) launch).setItem((ItemStack) data);
|
|
|
- ((PotionEntity) launch).shoot(liv.rotationPitch, liv.rotationYaw, -20.0f, 0.5f, 1.0f);
|
|
|
- } else if (ExperienceBottleEntity.class == projectile) {
|
|
|
- launch = new ExperienceBottleEntity(w, liv);
|
|
|
- ((ExperienceBottleEntity) launch).shoot(liv.rotationPitch, liv.rotationYaw, -20.0f, 0.7f, 1.0f);
|
|
|
- } else if (AbstractArrowEntity.class.isAssignableFrom(projectile)) {
|
|
|
- if (SpectralArrowEntity.class == projectile) {
|
|
|
- launch = new SpectralArrowEntity(w, liv);
|
|
|
- } else {
|
|
|
- launch = new ArrowEntity(w, liv);
|
|
|
- if (data != null) {
|
|
|
- ((ArrowEntity) launch).setPotionEffect((ItemStack) data);
|
|
|
- }
|
|
|
- }
|
|
|
- ((AbstractArrowEntity) launch).shoot(liv.rotationPitch, liv.rotationYaw, 0.0F, 3.0F, 1.0F);
|
|
|
- } else if (DamagingProjectileEntity.class.isAssignableFrom(projectile)) {
|
|
|
- Vector3d v = liv.getLookVec().scale(10);
|
|
|
- if (SmallFireballEntity.class == projectile) {
|
|
|
- launch = new SmallFireballEntity(w, liv, v.x, v.y, v.z);
|
|
|
- } else if (WitherSkullEntity.class == projectile) {
|
|
|
- launch = new WitherSkullEntity(w, liv, v.x, v.y, v.z);
|
|
|
- } else if (DragonFireballEntity.class == projectile) {
|
|
|
- launch = new DragonFireballEntity(w, liv, v.x, v.y, v.z);
|
|
|
- } else {
|
|
|
- launch = new FireballEntity(w, liv, v.x, v.y, v.z);
|
|
|
- }
|
|
|
- } else {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- launch.setMotion(launch.getMotion().scale(scale));
|
|
|
- w.addEntity(launch);
|
|
|
- return (T) launch;
|
|
|
+ sm.registerFunction("pet.getowner",
|
|
|
+ (sc, in) -> ((TameableEntity) in[0].get(sc)).getOwner());
|
|
|
}
|
|
|
}
|