Citizens2/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java

94 lines
2.9 KiB
Java
Raw Normal View History

2012-03-08 19:01:12 +01:00
package net.citizensnpcs.npc.entity;
2012-02-03 10:20:48 +01:00
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;
2012-03-08 19:01:12 +01:00
import net.citizensnpcs.npc.network.NPCNetHandler;
import net.citizensnpcs.npc.network.NPCNetworkManager;
import net.citizensnpcs.npc.network.NPCSocket;
2012-02-03 10:20:48 +01:00
import net.citizensnpcs.util.Messaging;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.ItemInWorldManager;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.NetHandler;
import net.minecraft.server.NetworkManager;
import net.minecraft.server.World;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.craftbukkit.entity.CraftPlayer;
@SuppressWarnings("unchecked")
public class EntityHumanNPC extends EntityPlayer {
public EntityHumanNPC(MinecraftServer minecraftServer, World world, String string,
ItemInWorldManager itemInWorldManager) {
super(minecraftServer, world, string, itemInWorldManager);
itemInWorldManager.setGameMode(0);
NPCSocket socket = new NPCSocket();
NetworkManager netMgr = new NPCNetworkManager(socket, "npc mgr", new NetHandler() {
@Override
public boolean c() {
return false;
}
});
netServerHandler = new NPCNetHandler(minecraftServer, netMgr, this);
netMgr.a(netServerHandler);
try {
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public CraftPlayer getBukkitEntity() {
if (bukkitEntity == null) {
super.getBukkitEntity();
removeFromPlayerMap(name);
// Bukkit uses a map of player names to CraftPlayer instances to
// solve a reconnect issue, so NPC names will conflict with ordinary
// player names. Workaround.
}
return super.getBukkitEntity();
}
2012-03-10 06:59:37 +01:00
public void moveOnCurrentHeading() {
if (this.aZ) {
if (aT()) {
this.motY += 0.03999999910593033D;
} else if (aU()) {
this.motY += 0.03999999910593033D;
} else if (this.onGround && this.q == 0) {
this.motY = 0.5;
this.q = 10;
}
} else {
this.q = 0;
}
aX *= 0.98F;
this.a(aW, aX);
X = yaw; // TODO: this looks jerky
2012-03-10 06:59:37 +01:00
}
2012-02-03 10:20:48 +01:00
public void removeFromPlayerMap(String name) {
2012-03-10 06:59:37 +01:00
if (players != null) {
2012-02-03 10:20:48 +01:00
players.remove(name);
2012-03-10 06:59:37 +01:00
}
2012-02-03 10:20:48 +01:00
}
private static Map<String, CraftPlayer> players;
static {
try {
Field f = CraftEntity.class.getDeclaredField("players");
f.setAccessible(true);
players = (Map<String, CraftPlayer>) f.get(null);
} catch (Exception ex) {
Messaging.log("Unable to fetch player map from CraftEntity: " + ex.getMessage());
}
}
}