Skip to content

Commit

Permalink
Somewhat fix the sit command
Browse files Browse the repository at this point in the history
NPC's now sit on little entity arrows... that sometimes bug themselves
for no good reason and kind of mess things up. but at least it sits
temporarily. Also it stands wrong. sometimes takes the stand command
twice and a teleport to get it going.
  • Loading branch information
mcmonkey4eva committed Oct 19, 2014
1 parent 6c9fbd9 commit dcfbb5f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 22 deletions.
9 changes: 0 additions & 9 deletions src/main/java/net/aufdemrand/denizen/NPCCommandHandler.java
Expand Up @@ -321,10 +321,6 @@ public void sitting(CommandContext args, CommandSender sender, NPC npc) throws C
if (!npc.hasTrait(SittingTrait.class)) npc.addTrait(SittingTrait.class);
SittingTrait trait = npc.getTrait(SittingTrait.class);

if (trait.isSitting()) {
Messaging.sendError(sender, npc.getName() + " is already sitting!");
return;
}
if (args.hasFlag('c')) {
trait.sit(args.getSenderTargetBlockLocation());
} else if (args.hasValueFlag("location")) {
Expand Down Expand Up @@ -359,11 +355,6 @@ public void standing(CommandContext args, CommandSender sender, NPC npc) throws

if (npc.hasTrait(SittingTrait.class)) {
SittingTrait trait = npc.getTrait(SittingTrait.class);
if (!trait.isSitting()) {
npc.removeTrait(SittingTrait.class);
Messaging.sendError(sender, npc.getName() + " is already standing!");
return;
}
trait.stand();
npc.removeTrait(SittingTrait.class);
} else if (npc.hasTrait(SneakingTrait.class)) {
Expand Down
40 changes: 36 additions & 4 deletions src/main/java/net/aufdemrand/denizen/npc/traits/SittingTrait.java
Expand Up @@ -2,17 +2,21 @@

import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.entity.CraftFakeArrow;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.util.PlayerAnimation;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.vehicle.VehicleExitEvent;

public class SittingTrait extends Trait implements Listener {

Expand All @@ -35,6 +39,13 @@ public void onSpawn() {
if (sitting) sit();
}

@Override
public void onDespawn() {
if (npc.getEntity().getVehicle() != null) {
npc.getEntity().getVehicle().setPassenger(null);
}
}

// <--[action]
// @Actions
// sit
Expand All @@ -60,13 +71,20 @@ public void sit() {
}

private void sitInternal() {
PlayerAnimation.SIT.play((Player)npc.getEntity());
CraftFakeArrow.createArrow(npc.getEntity().getLocation()).setPassenger(npc.getEntity());
//PlayerAnimation.SIT.play((Player)npc.getEntity());
//eh.getDataWatcher().watch(0, (byte) 0x04);
sitting = true;
}

private void standInternal() {
PlayerAnimation.STOP_SITTING.play((Player)npc.getEntity());
Entity vehicle = npc.getEntity().getVehicle();
npc.despawn();
npc.spawn(npc.getStoredLocation());
if (vehicle != null && vehicle.isValid()) {
vehicle.remove();
}
//PlayerAnimation.STOP_SITTING.play((Player)npc.getEntity());
//eh.getDataWatcher().watch(0, (byte) 0x00);
sitting = false;
}
Expand All @@ -88,7 +106,7 @@ public void sit(Location location) {
* sending the sit packet to the clients.
*/
// TODO: Make this work better.
npc.teleport(location.add(0.5, 0, 0.5), PlayerTeleportEvent.TeleportCause.PLUGIN);
npc.teleport(location.clone().add(0.5, 0, 0.5), PlayerTeleportEvent.TeleportCause.PLUGIN);

sitInternal();
chairLocation = location;
Expand Down Expand Up @@ -121,7 +139,7 @@ public void stand() {
* @return boolean
*/
public boolean isSitting() {
return sitting;
return true; // If the trait is attached, let's assume the NPC is sitting
}

/**
Expand All @@ -148,6 +166,20 @@ public void onBlockBreak(BlockBreakEvent event) {
}
}

@EventHandler
public void arrowDismount(final VehicleExitEvent event) {
if (event.getVehicle() instanceof CraftFakeArrow) {
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
if (event.getVehicle().isValid()) {
event.getVehicle().remove();
}
}
}, 1);
}
}

public SittingTrait() {
super("sitting");
}
Expand Down
Expand Up @@ -60,10 +60,6 @@ else if (scriptEntry.getNPC().getEntityType() == EntityType.WOLF) {
dB.echoDebug(scriptEntry, "...added sitting trait");
}

if (trait.isSitting()) {
dB.echoError(scriptEntry.getResidingQueue(), "...NPC is already sitting");
}

if (location != null) {
trait.sit(location);
} else {
Expand Down
Expand Up @@ -55,11 +55,6 @@ else if (scriptEntry.getNPC().getEntityType() == EntityType.WOLF) {
dB.echoDebug(scriptEntry, "...added sitting trait");
}

if (!trait.isSitting()) {
dB.echoError(scriptEntry.getResidingQueue(), "...NPC is already standing, removing trait");
npc.removeTrait(SittingTrait.class);
return;
}
trait.stand();
npc.removeTrait(SittingTrait.class);
}
Expand Down
@@ -0,0 +1,33 @@
package net.aufdemrand.denizen.utilities.entity;

import net.minecraft.server.v1_7_R4.EntityArrow;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R4.CraftServer;
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftArrow;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Vehicle;

public class CraftFakeArrow extends CraftArrow implements Vehicle {
public CraftFakeArrow(CraftServer craftServer, EntityArrow entityArrow) {
super(craftServer, entityArrow);
}

public void setShooter (LivingEntity livingEntity) {
}

@Override
public void remove() {
if (getPassenger() != null) {
return;
}
super.remove();
}

public static Arrow createArrow(Location location) {
CraftWorld world = (CraftWorld) location.getWorld();
EntityArrow arrow = new FakeArrowEntity(world, location);
return (Arrow) arrow.getBukkitEntity();
}
}
@@ -0,0 +1,22 @@
package net.aufdemrand.denizen.utilities.entity;

import net.minecraft.server.v1_7_R4.EntityArrow;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R4.CraftServer;
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;

public class FakeArrowEntity extends EntityArrow {

public FakeArrowEntity(CraftWorld craftWorld, Location location) {
super(craftWorld.getHandle());
setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
world.addEntity(this);
bukkitEntity = new CraftFakeArrow((CraftServer) Bukkit.getServer(), this);
}

@Override
public void h() {
// Do nothing
}
}

0 comments on commit dcfbb5f

Please sign in to comment.