use of net.citizensnpcs.npc.ai.NPCHolder in project Citizens2 by CitizensDev.
the class PlayerAnimationImpl method play.
public static void play(PlayerAnimation animation, Player bplayer, int radius) {
// TODO: this is pretty gross
final EntityPlayer player = (EntityPlayer) NMSImpl.getHandle(bplayer);
if (DEFAULTS.containsKey(animation)) {
playDefaultAnimation(player, radius, DEFAULTS.get(animation));
return;
}
switch(animation) {
case SIT:
player.getBukkitEntity().setMetadata("citizens.sitting", new FixedMetadataValue(CitizensAPI.getPlugin(), true));
final NPC holder = CitizensAPI.getNPCRegistry().createNPC(EntityType.ARMOR_STAND, "");
holder.spawn(player.getBukkitEntity().getLocation());
ArmorStandTrait trait = holder.getTrait(ArmorStandTrait.class);
trait.setGravity(false);
trait.setHasArms(false);
trait.setHasBaseplate(false);
trait.setSmall(true);
trait.setMarker(true);
trait.setVisible(false);
holder.getTrait(ArmorStandTrait.class).setVisible(false);
holder.data().set(NPC.NAMEPLATE_VISIBLE_METADATA, false);
holder.data().set(NPC.DEFAULT_PROTECTED_METADATA, true);
new BukkitRunnable() {
@Override
public void cancel() {
super.cancel();
holder.destroy();
}
@Override
public void run() {
if (player.dead || !player.valid || !player.getBukkitEntity().getMetadata("citizens.sitting").get(0).asBoolean()) {
cancel();
return;
}
if (player instanceof NPCHolder && !((NPCHolder) player).getNPC().isSpawned()) {
cancel();
return;
}
if (!NMS.getPassengers(holder.getEntity()).contains(player.getBukkitEntity())) {
NMS.mount(holder.getEntity(), player.getBukkitEntity());
}
}
}.runTaskTimer(CitizensAPI.getPlugin(), 0, 1);
break;
case SLEEP:
PacketPlayOutBed packet = new PacketPlayOutBed(player, new BlockPosition((int) player.locX, (int) player.locY, (int) player.locZ));
sendPacketNearby(packet, player, radius);
break;
case SNEAK:
player.getBukkitEntity().setSneaking(true);
sendPacketNearby(new PacketPlayOutEntityMetadata(player.getId(), player.getDataWatcher(), true), player, radius);
break;
case START_USE_MAINHAND_ITEM:
player.c(EnumHand.MAIN_HAND);
sendPacketNearby(new PacketPlayOutEntityMetadata(player.getId(), player.getDataWatcher(), true), player, radius);
break;
case START_USE_OFFHAND_ITEM:
player.c(EnumHand.OFF_HAND);
sendPacketNearby(new PacketPlayOutEntityMetadata(player.getId(), player.getDataWatcher(), true), player, radius);
break;
case STOP_SITTING:
player.getBukkitEntity().setMetadata("citizens.sitting", new FixedMetadataValue(CitizensAPI.getPlugin(), false));
NMS.mount(player.getBukkitEntity(), null);
break;
case STOP_SLEEPING:
playDefaultAnimation(player, radius, 2);
break;
case STOP_SNEAKING:
player.getBukkitEntity().setSneaking(false);
sendPacketNearby(new PacketPlayOutEntityMetadata(player.getId(), player.getDataWatcher(), true), player, radius);
break;
case STOP_USE_ITEM:
player.clearActiveItem();
sendPacketNearby(new PacketPlayOutEntityMetadata(player.getId(), player.getDataWatcher(), true), player, radius);
break;
default:
throw new UnsupportedOperationException();
}
}
use of net.citizensnpcs.npc.ai.NPCHolder in project Citizens2 by CitizensDev.
the class CitizensBlockBreaker method run.
@Override
public BehaviorStatus run() {
if (entity.dead) {
return BehaviorStatus.FAILURE;
}
if (!isDigging) {
return BehaviorStatus.SUCCESS;
}
// CraftBukkit
currentTick = (int) (System.currentTimeMillis() / 50);
if (configuration.radiusSquared() > 0 && distanceSquared() >= configuration.radiusSquared()) {
startDigTick = currentTick;
if (entity instanceof NPCHolder) {
NPC npc = ((NPCHolder) entity).getNPC();
if (npc != null && !npc.getNavigator().isNavigating()) {
npc.getNavigator().setTarget(entity.world.getWorld().getBlockAt(x, y, z).getLocation().add(0, 1, 0));
}
}
return BehaviorStatus.RUNNING;
}
Util.faceLocation(entity.getBukkitEntity(), location);
if (entity instanceof EntityPlayer) {
PlayerAnimation.ARM_SWING.play((Player) entity.getBukkitEntity());
}
IBlockData block = entity.world.getType(new BlockPosition(x, y, z));
if (block == null || block == Blocks.AIR) {
return BehaviorStatus.SUCCESS;
} else {
int tickDifference = currentTick - startDigTick;
float damage = getStrength(block.getBlock()) * (tickDifference + 1) * configuration.blockStrengthModifier();
if (damage >= 1F) {
entity.world.getWorld().getBlockAt(x, y, z).breakNaturally(CraftItemStack.asCraftMirror(getCurrentItem()));
return BehaviorStatus.SUCCESS;
}
int modifiedDamage = (int) (damage * 10.0F);
if (modifiedDamage != currentDamage) {
setBlockDamage(modifiedDamage);
currentDamage = modifiedDamage;
}
}
return BehaviorStatus.RUNNING;
}
use of net.citizensnpcs.npc.ai.NPCHolder in project Citizens2 by CitizensDev.
the class NMSImpl method tick.
@Override
public boolean tick(org.bukkit.entity.Entity next) {
Entity entity = NMSImpl.getHandle(next);
if (!entity.dead) {
try {
entity.world.g(entity);
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.a(throwable, "Ticking player");
CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Player being ticked");
entity.appendEntityCrashDetails(crashreportsystemdetails);
throw new ReportedException(crashreport);
}
}
boolean removeFromPlayerList = ((NPCHolder) entity).getNPC().data().get("removefromplayerlist", Setting.REMOVE_PLAYERS_FROM_PLAYER_LIST.asBoolean());
if (entity.dead) {
entity.world.removeEntity(entity);
return true;
} else if (!removeFromPlayerList) {
if (!entity.world.players.contains(entity)) {
entity.world.players.add((EntityHuman) entity);
}
return true;
} else {
entity.world.players.remove(entity);
}
return false;
}
use of net.citizensnpcs.npc.ai.NPCHolder in project Citizens2 by CitizensDev.
the class NMSImpl method getTargetNavigator.
private MCNavigator getTargetNavigator(final org.bukkit.entity.Entity entity, final NavigatorParameters params, final Function<NavigationAbstract, Boolean> function) {
net.minecraft.server.v1_11_R1.Entity raw = getHandle(entity);
raw.onGround = true;
// not sure of a better way around this - if onGround is false, then
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
final NavigationAbstract navigation = NMSImpl.getNavigation(entity);
return new MCNavigator() {
float lastSpeed;
CancelReason reason;
@Override
public CancelReason getCancelReason() {
return reason;
}
@Override
public Iterable<Vector> getPath() {
return new NavigationIterable(navigation);
}
@Override
public void stop() {
if (navigation.k() != null) {
for (Player player : Bukkit.getOnlinePlayers()) {
for (int i = 0; i < navigation.k().d(); i++) {
PathPoint pp = navigation.k().a(i);
org.bukkit.block.Block block = new Vector(pp.a, pp.b, pp.c).toLocation(player.getWorld()).getBlock();
player.sendBlockChange(block.getLocation(), block.getType(), block.getData());
}
}
}
stopNavigation(navigation);
}
@Override
public boolean update() {
if (params.speed() != lastSpeed) {
if (Messaging.isDebugging()) {
Messaging.debug("Repathfinding " + ((NPCHolder) entity).getNPC().getId() + " due to speed change");
}
Entity handle = getHandle(entity);
float oldWidth = handle.width;
if (handle instanceof EntityHorse) {
handle.width = Math.min(0.99f, oldWidth);
}
if (!function.apply(navigation)) {
reason = CancelReason.STUCK;
}
// minecraft requires that an entity fit onto both blocks if width >= 1f,
handle.width = oldWidth;
// but we'd prefer to make it just fit on 1 so hack around it a bit.
lastSpeed = params.speed();
}
if (params.debug() && !NMSImpl.isNavigationFinished(navigation)) {
for (Player player : Bukkit.getOnlinePlayers()) {
for (int i = 0; i < navigation.k().d(); i++) {
PathPoint pp = navigation.k().a(i);
player.sendBlockChange(new Vector(pp.a, pp.b, pp.c).toLocation(player.getWorld()), Material.YELLOW_FLOWER, (byte) 0);
}
}
}
navigation.a(params.speed());
return NMSImpl.isNavigationFinished(navigation);
}
};
}
use of net.citizensnpcs.npc.ai.NPCHolder in project Citizens2 by CitizensDev.
the class NMSImpl method tick.
@Override
public boolean tick(org.bukkit.entity.Entity next) {
Entity entity = NMSImpl.getHandle(next);
Entity entity1 = entity.bJ();
if (entity1 != null) {
if ((entity1.dead) || (!entity1.w(entity))) {
entity.stopRiding();
}
} else {
if (!entity.dead) {
try {
entity.world.entityJoinedWorld(entity, true);
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.a(throwable, "Ticking player");
CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Player being ticked");
entity.appendEntityCrashDetails(crashreportsystemdetails);
throw new ReportedException(crashreport);
}
}
boolean removeFromPlayerList = ((NPCHolder) entity).getNPC().data().get("removefromplayerlist", Setting.REMOVE_PLAYERS_FROM_PLAYER_LIST.asBoolean());
if (entity.dead) {
entity.world.removeEntity(entity);
return true;
} else if (!removeFromPlayerList) {
if (!entity.world.players.contains(entity)) {
entity.world.players.add((EntityHuman) entity);
}
return true;
} else {
entity.world.players.remove(entity);
}
}
return false;
}
Aggregations