mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-11-23 02:55:45 +01:00
AbstractNPC
This commit is contained in:
parent
9bbb55435c
commit
89d3dc13a1
@ -1,53 +1,25 @@
|
||||
package net.citizensnpcs.npc;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.citizensnpcs.api.CitizensAPI;
|
||||
import net.citizensnpcs.api.event.NPCDespawnEvent;
|
||||
import net.citizensnpcs.api.event.NPCSpawnEvent;
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.npc.AbstractNPC;
|
||||
import net.citizensnpcs.api.npc.ai.Navigator;
|
||||
import net.citizensnpcs.api.npc.trait.Character;
|
||||
import net.citizensnpcs.api.npc.trait.Trait;
|
||||
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
|
||||
import net.citizensnpcs.resources.lib.CraftNPC;
|
||||
import net.citizensnpcs.util.Messaging;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Entity;
|
||||
|
||||
public class CitizensNPC implements NPC {
|
||||
private final int id;
|
||||
private Character character;
|
||||
private final Map<Class<? extends Trait>, Trait> traits = new HashMap<Class<? extends Trait>, Trait>();
|
||||
private String name;
|
||||
public class CitizensNPC extends AbstractNPC {
|
||||
private CraftNPC mcEntity;
|
||||
private boolean spawned;
|
||||
private final CitizensNPCManager manager;
|
||||
|
||||
public CitizensNPC(int id, String name, Character character) {
|
||||
this.name = name;
|
||||
this.character = character;
|
||||
manager = (CitizensNPCManager) CitizensAPI.getNPCManager();
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public CitizensNPC(String name, Character character) {
|
||||
this.name = name;
|
||||
this.character = character;
|
||||
manager = (CitizensNPCManager) CitizensAPI.getNPCManager();
|
||||
id = manager.getUniqueID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTrait(Trait trait) {
|
||||
if (!hasTrait(trait.getClass()))
|
||||
traits.put(trait.getClass(), trait);
|
||||
else
|
||||
Messaging.debug("The NPC already has the trait '" + getTrait(trait.getClass()).getName() + "'.");
|
||||
public CitizensNPC(CitizensNPCManager manager, int id, String name) {
|
||||
super(id, name);
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -70,52 +42,16 @@ public class CitizensNPC implements NPC {
|
||||
return getHandle().getBukkitEntity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Character getCharacter() {
|
||||
return character;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public CraftNPC getHandle() {
|
||||
return mcEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return ChatColor.stripColor(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Navigator getNavigator() {
|
||||
// TODO add default navigator
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Trait> T getTrait(Class<T> clazz) {
|
||||
Trait t = traits.get(clazz);
|
||||
return t != null ? clazz.cast(t) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Trait> getTraits() {
|
||||
return traits.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTrait(Class<? extends Trait> trait) {
|
||||
return traits.containsKey(trait);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpawned() {
|
||||
return spawned;
|
||||
@ -128,29 +64,6 @@ public class CitizensNPC implements NPC {
|
||||
manager.remove(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTrait(Class<? extends Trait> trait) {
|
||||
if (!hasTrait(trait)) {
|
||||
Messaging.debug("The NPC does not have a trait with the name of '" + trait.getName() + ".");
|
||||
return;
|
||||
}
|
||||
traits.remove(trait);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCharacter(Character character) {
|
||||
if (this.character.equals(character)) {
|
||||
Messaging.debug("The NPC already has the character '" + character.getName() + "'.");
|
||||
return;
|
||||
}
|
||||
this.character = character;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spawn(Location loc) {
|
||||
if (isSpawned()) {
|
||||
|
@ -37,7 +37,8 @@ public class CitizensNPCManager implements NPCManager {
|
||||
|
||||
@Override
|
||||
public NPC createNPC(String name, Character character) {
|
||||
CitizensNPC npc = new CitizensNPC(name, character);
|
||||
CitizensNPC npc = new CitizensNPC(this, getUniqueID(), name);
|
||||
npc.setCharacter(character);
|
||||
byID.put(npc.getId(), npc);
|
||||
return npc;
|
||||
}
|
||||
@ -90,11 +91,8 @@ public class CitizensNPCManager implements NPCManager {
|
||||
|
||||
public int getUniqueID() {
|
||||
int count = 0;
|
||||
while (true) {
|
||||
if (getNPC(count) == null)
|
||||
break;
|
||||
count++;
|
||||
}
|
||||
while (getNPC(count++) != null)
|
||||
;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user