Java源码示例:net.citizensnpcs.api.npc.NPC
示例1
private CitizensNpc spawnNpc(Location location, String nametag, Consumer<Npc> skin) {
Objects.requireNonNull(this.npcRegistry, "npcRegistry");
// create a new npc
NPC npc = this.npcRegistry.createNPC(EntityType.PLAYER, nametag);
// add the trait
ClickableTrait trait = new ClickableTrait();
npc.addTrait(trait);
// create a new helperNpc instance
CitizensNpc helperNpc = new NpcImpl(npc, trait, location.clone());
trait.npc = helperNpc;
// apply the skin and spawn it
skin.accept(helperNpc);
npc.spawn(location);
return helperNpc;
}
示例2
@Override
@Deprecated
public void setSkin(@Nonnull String skinPlayer) {
try {
this.npc.data().set(NPC.PLAYER_SKIN_UUID_METADATA, skinPlayer);
this.npc.data().set(NPC.PLAYER_SKIN_USE_LATEST, true);
if (this.npc instanceof SkinnableEntity) {
((SkinnableEntity) this.npc).getSkinTracker().notifySkinChange(true);
}
if (this.npc.getEntity() instanceof SkinnableEntity) {
((SkinnableEntity) this.npc.getEntity()).getSkinTracker().notifySkinChange(true);
}
} catch (Throwable e) {
e.printStackTrace();
}
}
示例3
@Override
public boolean check() {
NPC npc = this.getNPC();
if (npc != null) {
String worldName = shopkeeper.getWorldName();
World world = Bukkit.getWorld(worldName);
int x = shopkeeper.getX();
int y = shopkeeper.getY();
int z = shopkeeper.getZ();
Location currentLocation = npc.getStoredLocation();
Location expectedLocation = new Location(world, x + 0.5D, y + 0.5D, z + 0.5D);
if (currentLocation == null) {
npc.teleport(expectedLocation, PlayerTeleportEvent.TeleportCause.PLUGIN);
Log.debug("Shopkeeper NPC (" + worldName + "," + x + "," + y + "," + z + ") had no location, teleported");
} else if (!currentLocation.getWorld().equals(expectedLocation.getWorld()) || currentLocation.distanceSquared(expectedLocation) > 1.0D) {
shopkeeper.setLocation(currentLocation);
Log.debug("Shopkeeper NPC (" + worldName + "," + x + "," + y + "," + z + ") out of place, re-indexing");
}
} else {
// Not going to force Citizens creation, this seems like it could go really wrong.
}
return false;
}
示例4
/**
* Gets the entity this NPC is guarding, or null.
*/
public LivingEntity getGuardingEntity() {
if (guardedNPC >= 0) {
NPC npc = CitizensAPI.getNPCRegistry().getById(guardedNPC);
if (npc != null && npc.isSpawned()) {
return (LivingEntity) npc.getEntity();
}
}
UUID guardId = getGuarding();
if (guardId == null) {
return null;
}
Player player = Bukkit.getPlayer(guardId);
if (player != null) {
return player;
}
if (SentinelVersionCompat.v1_12) {
Entity entity = Bukkit.getEntity(guardId);
if (entity instanceof LivingEntity) {
return (LivingEntity) entity;
}
}
return null;
}
示例5
@Override
public void setName(String name) {
NPC npc = this.getNPC();
if (npc == null) return;
if (Settings.showNameplates && name != null && !name.isEmpty()) {
if (Settings.nameplatePrefix != null && !Settings.nameplatePrefix.isEmpty()) {
name = Settings.nameplatePrefix + name;
}
name = this.trimToNameLength(name);
// set entity name plate:
npc.setName(name);
// this.entity.setCustomNameVisible(Settings.alwaysShowNameplates);
} else {
// remove name plate:
npc.setName("");
// this.entity.setCustomNameVisible(false);
}
}
示例6
public void saveHealth(NPC npc) {
if(isInvalid(npc)) return;
TraitCombatLogX traitCombatLogX = npc.getTrait(TraitCombatLogX.class);
OfflinePlayer owner = traitCombatLogX.getOwner();
YamlConfiguration config = getData(owner);
if(npc.isSpawned()) {
Entity entity = npc.getEntity();
if(entity instanceof LivingEntity) {
LivingEntity livingEntity = (LivingEntity) entity;
double health = livingEntity.getHealth();
config.set("citizens-compatibility.last-health", health);
setData(owner, config);
return;
}
}
config.set("citizens-compatibility.last-health", 0.0D);
setData(owner, config);
}
示例7
private void setLivingOptions(NPC npc, Player player) {
if(npc == null || player == null) return;
Entity entity = npc.getEntity();
if(!(entity instanceof LivingEntity)) return;
LivingEntity livingEntity = (LivingEntity) entity;
ICombatLogX plugin = this.expansion.getPlugin();
MultiVersionHandler<?> multiVersionHandler = plugin.getMultiVersionHandler();
AbstractNMS nmsHandler = multiVersionHandler.getInterface();
EntityHandler entityHandler = nmsHandler.getEntityHandler();
double health = player.getHealth();
double maxHealth = entityHandler.getMaxHealth(player);
double maxHealthValue = Math.max(health, maxHealth);
entityHandler.setMaxHealth(livingEntity, maxHealthValue);
livingEntity.setHealth(health);
setMobTargetable(npc);
}
示例8
private void setMobTargetable(NPC npc) {
if(npc == null) return;
FileConfiguration config = this.expansion.getConfig("citizens-compatibility.yml");
if(!config.getBoolean("npc-options.mob-target", true)) return;
Entity entity = npc.getEntity();
if(!(entity instanceof LivingEntity)) return;
LivingEntity livingEntity = (LivingEntity) entity;
List<Entity> nearbyEntityList = livingEntity.getNearbyEntities(16.0D, 16.0D, 16.0D);
for(Entity nearby : nearbyEntityList) {
if(nearby instanceof Player) continue;
if(!(nearby instanceof Monster)) continue;
Monster monster = (Monster) nearby;
monster.setTarget(livingEntity);
}
}
示例9
@EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
public void onJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
player.setCanPickupItems(false);
NPCManager npcManager = this.expansion.getNPCManager();
NPC npc = npcManager.getNPC(player);
if(npc != null) npc.despawn(DespawnReason.PLUGIN);
Runnable task = () -> {
punish(player);
player.setCanPickupItems(true);
};
JavaPlugin plugin = this.expansion.getPlugin().getPlugin();
BukkitScheduler scheduler = Bukkit.getScheduler();
scheduler.runTaskLater(plugin, task, 1L);
}
示例10
@Override
public void run() {
if(this.ticksUntilRemove > 0) {
this.ticksUntilRemove--;
return;
}
long survivalSeconds = getSurvivalSeconds();
if(survivalSeconds < 0) return;
Player enemy = getEnemy();
ICombatLogX plugin = this.expansion.getPlugin();
if(enemy != null && waitUntilEnemyEscape()) {
ICombatManager combatManager = plugin.getCombatManager();
if(combatManager.isInCombat(enemy)) return;
}
Runnable task = () -> {
NPC npc = getNPC();
npc.despawn(DespawnReason.PLUGIN);
};
JavaPlugin javaPlugin = plugin.getPlugin();
BukkitScheduler scheduler = Bukkit.getScheduler();
scheduler.runTaskLater(javaPlugin, task, 1L);
}
示例11
@Override
public void summon(String mob, Location location) {
NPC source = CitizensAPI.getNPCRegistry().getById(NumberUtil.parseInt(mob));
if (!(source instanceof AbstractNPC)) {
return;
}
NPC npc = registry.createTransientClone((AbstractNPC) source);
if (npc.isSpawned()) {
npc.despawn();
}
npc.spawn(location);
spawnedNPCs.add(npc);
GameWorld gameWorld = DungeonsXL.getInstance().getGameWorld(location.getWorld());
if (gameWorld == null) {
return;
}
new DMob((LivingEntity) npc.getEntity(), gameWorld, mob);
}
示例12
public void save(AbstractNPC npc, DataKey root) {
if (!npc.data().get(NPC.SHOULD_SAVE_METADATA, true)) {
return;
}
npc.data().saveTo(root.getRelative("metadata"));
root.setString("name", npc.getFullName());
root.setString("uuid", npc.getUniqueId().toString());
StringBuilder traitNames = new StringBuilder();
for (Trait trait : npc.getTraits()) {
DataKey traitKey = root.getRelative("traits." + trait.getName());
trait.save(traitKey);
PersistenceLoader.save(trait, traitKey);
//npc.removedTraits.remove(trait.getName());
traitNames.append(trait.getName() + ",");
}
if (traitNames.length() > 0) {
root.setString("traitnames", traitNames.substring(0, traitNames.length() - 1));
} else {
root.setString("traitnames", "");
}
//npc.removedTraits.clear();
}
示例13
@EventHandler(ignoreCancelled = true)
public void onNpcKill(MobKilledEvent event) {
NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getEntity());
if (npc == null) {
return;
}
if (npc.getId() != ID) {
return;
}
String playerID = PlayerConverter.getID(event.getPlayer());
NPCData playerData = (NPCData) dataMap.get(playerID);
if (containsPlayer(playerID) && checkConditions(playerID)) {
playerData.kill();
if (playerData.killed()) {
completeObjective(playerID);
}
}
}
示例14
@Override
protected Boolean execute(String playerID) throws QuestRuntimeException {
NPC npc = CitizensAPI.getNPCRegistry().getById(ID);
if (npc == null) {
throw new QuestRuntimeException("NPC with ID " + ID + " does not exist");
}
Entity npcEntity = npc.getEntity();
if (npcEntity == null) return false;
Player player = PlayerConverter.getPlayer(playerID);
WorldGuardPlatform worldguardPlatform = WorldGuard.getInstance().getPlatform();
RegionManager manager = worldguardPlatform.getRegionContainer().get(BukkitAdapter.adapt(player.getWorld()));
if (manager == null) {
return false;
}
ApplicableRegionSet set = manager.getApplicableRegions(BlockVector3.at(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ()));
for (ProtectedRegion compare : set) {
if (compare.equals(region))
return true;
}
return false;
}
示例15
private void handleClick(NPC npc, Player clicker) {
if (npc.hasTrait(ClickableTrait.class)) {
if (processMetadata(clicker)) {
return;
}
npc.getTrait(ClickableTrait.class).onClick(clicker);
}
}
示例16
@Override
protected void execute(Event evt) {
NPCRegistry registry = CitizensAPI.getNPCRegistry();
NPC npc = registry.getById(id.getSingle(evt).intValue());
if (npc != null && npc.getEntity().getType().equals(EntityType.PLAYER)) {
if (shouldCrouch) {
// Make crouch here
((Player) npc.getEntity()).setSneaking(true);
} else {
// Make uncrouch here
((Player) npc.getEntity()).setSneaking(false);
}
}
}
示例17
@Override
protected void execute(Event evt) {
NPCRegistry registry = CitizensAPI.getNPCRegistry();
NPC npc = registry.getById(id.getSingle(evt).intValue());
npc.addTrait(SentryTrait.class);
SentryInstance st = npc.getTrait(SentryTrait.class).getInstance();
st.FollowDistance = dis.getSingle(evt).intValue();
}
示例18
/**
* Gets the Sentinel Trait instance for a given command sender (based on their selected NPC).
*/
public SentinelTrait getSentinelFor(CommandSender sender) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender);
if (npc == null) {
return null;
}
if (npc.hasTrait(SentinelTrait.class)) {
return npc.getTrait(SentinelTrait.class);
}
return null;
}
示例19
/**
* Gets the owner identity of an NPC for output (player name or "server").
*/
public String getOwner(NPC npc) {
if (npc.getTrait(Owner.class).getOwnerId() == null) {
return npc.getTrait(Owner.class).getOwner();
}
OfflinePlayer player = Bukkit.getOfflinePlayer(npc.getTrait(Owner.class).getOwnerId());
if (player == null) {
return "Server/Unknown";
}
return player.getName();
}
示例20
@Command(aliases = {"sentinel"}, usage = "guard [PLAYERNAME]/npc:[ID]",
desc = "Makes the NPC guard a specific player or NPC - don't specify a player to stop guarding.",
modifiers = {"guard"}, permission = "sentinel.guard", min = 1, max = 2)
@Requirements(livingEntity = true, ownership = true, traits = {SentinelTrait.class})
public void guard(CommandContext args, CommandSender sender, SentinelTrait sentinel) {
if (args.argsLength() > 1) {
String inputArg = args.getString(1);
if (inputArg.toLowerCase().startsWith("npc:")) {
try {
int id = Integer.parseInt(inputArg.substring("npc:".length()));
NPC npc = CitizensAPI.getNPCRegistry().getById(id);
if (npc == null) {
sender.sendMessage(SentinelCommand.prefixBad + "NPC ID input invalid (that number isn't any NPC's ID)!");
return;
}
sentinel.setGuarding(id);
sender.sendMessage(SentinelCommand.prefixGood + "NPC now guarding the specified NPC!");
return;
}
catch (NumberFormatException ex) {
sender.sendMessage(SentinelCommand.prefixBad + "NPC ID input invalid (need a number)!");
return;
}
}
else {
Player pl = Bukkit.getPlayer(inputArg);
sentinel.setGuarding(pl == null ? null : pl.getUniqueId());
}
}
else {
sentinel.setGuarding(null);
}
if (sentinel.getGuarding() == null) {
sender.sendMessage(SentinelCommand.prefixGood + "NPC now guarding its area!");
}
else {
sender.sendMessage(SentinelCommand.prefixGood + "NPC now guarding that player!");
}
}
示例21
/**
* Tries to get a Sentinel from an entity. Returns null if it is not a Sentinel.
*/
public static SentinelTrait tryGetSentinel(Entity entity) {
if (CitizensAPI.getNPCRegistry().isNPC(entity)) {
NPC npc = CitizensAPI.getNPCRegistry().getNPC(entity);
if (npc.hasTrait(SentinelTrait.class)) {
return npc.getTrait(SentinelTrait.class);
}
}
return null;
}
示例22
@Override
protected void execute(Event evt) {
NPCRegistry registry = CitizensAPI.getNPCRegistry();
if (registry.getById(id.getSingle(evt).intValue()) != null) {
try {
NPC despawn = registry.getById(id.getSingle(evt).intValue());
despawn.despawn(null);
} catch (NullPointerException exp) {
return;
}
}
}
示例23
/**
* Gets the UUID of the player this Sentinel is set to be guarding.
* Null indicates not guarding anyone.
*/
public UUID getGuarding() {
if (guardedNPC >= 0) {
NPC npc = CitizensAPI.getNPCRegistry().getById(guardedNPC);
if (npc != null) {
return npc.getUniqueId();
}
}
if (guardingLower == 0 && guardingUpper == 0) {
return null;
}
return new UUID(guardingUpper, guardingLower);
}
示例24
@Override
protected void execute(Event evt) {
NPCRegistry registry = CitizensAPI.getNPCRegistry();
NPC npc = registry.getById(id.getSingle(evt).intValue());
npc.data().setPersistent(NPC.PLAYER_SKIN_UUID_METADATA,
toSkin.getSingle(evt).replace("\"", ""));
Location respawnloc = npc.getEntity().getLocation();
npc.despawn();
npc.spawn(respawnloc);
}
示例25
public boolean isInvalid(NPC npc) {
if(npc == null) return true;
if(!npc.hasTrait(TraitCombatLogX.class)) return true;
TraitCombatLogX traitCombatLogX = npc.getTrait(TraitCombatLogX.class);
return (traitCombatLogX.getOwner() == null);
}
示例26
public void createNPC(Player player, LivingEntity enemy) {
if(player == null) return;
Location location = player.getLocation().clone();
EntityType bukkitType = getMobType();
String playerName = player.getName();
NPCRegistry npcRegistry = CitizensAPI.getNPCRegistry();
NPC npc = npcRegistry.createNPC(bukkitType, playerName);
npc.removeTrait(Owner.class);
TraitCombatLogX traitCombatLogX = npc.getTrait(TraitCombatLogX.class);
traitCombatLogX.extendLife();
traitCombatLogX.setOwner(player);
if(enemy instanceof Player) {
Player enemyPlayer = (Player) enemy;
traitCombatLogX.setEnemy(enemyPlayer);
}
boolean spawned = npc.spawn(location);
if(!spawned) {
Logger logger = this.expansion.getLogger();
logger.warning("An NPC could not be spawned for " + playerName + ".");
return;
}
setOptions(npc, player);
SentinelManager sentinelManager = this.expansion.getSentinelManager();
FileConfiguration config = this.expansion.getConfig("citizens-compatibility.yml");
if(sentinelManager != null && config.getBoolean("sentinel-options.enable-sentinel", true)) {
sentinelManager.setOptions(npc, player, enemy);
}
}
示例27
private void setOptions(NPC npc, Player player) {
if(npc == null || player == null) return;
FileConfiguration config = this.expansion.getConfig("citizens-compatibility.yml");
if(config.getBoolean("npc-options.store-inventory", true)) {
saveInventory(player);
transferInventory(player, npc);
}
npc.setProtected(false);
setLivingOptions(npc, player);
}
示例28
public static Integer getNPCId(Entity entity) {
if (enabled) {
NPC npc = CitizensAPI.getNPCRegistry().getNPC(entity);
return npc != null ? npc.getId() : null;
} else {
return null;
}
}
示例29
@Override
protected void execute(Event evt) {
NPCRegistry registry = CitizensAPI.getNPCRegistry();
NPC npc = registry.getById(id.getSingle(evt).intValue());
if (npc != null && npc.getEntity().getType().equals(EntityType.PLAYER)) {
PlayerAnimation.ARM_SWING.play((Player) npc.getEntity());
}
}
示例30
@EventHandler(priority=EventPriority.NORMAL, ignoreCancelled=true)
public void beforeLogin(AsyncPlayerPreLoginEvent e) {
FileConfiguration config = this.expansion.getConfig("citizens-compatibility.yml");
if(!config.getBoolean("prevent-login", false)) return;
UUID uuid = e.getUniqueId();
NPCManager npcManager = this.expansion.getNPCManager();
NPC npc = npcManager.getNPC(uuid);
if(npc == null) return;
String message = this.expansion.getPlugin().getLanguageMessageColored("citizens-join-deny");
e.setKickMessage(message);
e.setLoginResult(Result.KICK_OTHER);
}