Remove /npc skin, fix duplicate NPCs on 1.7.8

This commit is contained in:
fullwall 2014-04-12 00:25:03 +08:00
parent 960b54d7d3
commit be209a0c88
6 changed files with 15 additions and 151 deletions

View File

@ -241,7 +241,7 @@ public class EventListen implements Listener {
@EventHandler
public void onNPCDespawn(NPCDespawnEvent event) {
if (event.getReason() == DespawnReason.PLUGIN || event.getReason() == DespawnReason.REMOVAL) {
toRespawn.remove(toCoord(event.getNPC().getEntity().getLocation()), event.getNPC());
toRespawn.remove(toCoord(event.getNPC().getStoredLocation()), event.getNPC());
}
}

View File

@ -44,7 +44,6 @@ import net.citizensnpcs.trait.HorseModifiers;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.NPCSkeletonType;
import net.citizensnpcs.trait.OcelotModifiers;
import net.citizensnpcs.trait.PlayerSkin;
import net.citizensnpcs.trait.Poses;
import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.SlimeSize;
@ -268,7 +267,7 @@ public class NPCCommands {
}
CommandSenderCreateNPCEvent event = sender instanceof Player ? new PlayerCreateNPCEvent((Player) sender, copy)
: new CommandSenderCreateNPCEvent(sender, copy);
: new CommandSenderCreateNPCEvent(sender, copy);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
event.getNPC().destroy();
@ -344,7 +343,7 @@ public class NPCCommands {
spawnLoc = args.getSenderLocation();
}
CommandSenderCreateNPCEvent event = sender instanceof Player ? new PlayerCreateNPCEvent((Player) sender, npc)
: new CommandSenderCreateNPCEvent(sender, npc);
: new CommandSenderCreateNPCEvent(sender, npc);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
npc.destroy();
@ -958,24 +957,6 @@ public class NPCCommands {
npc.getName());
}
@Command(
aliases = { "npc" },
usage = "skin [skin|-c]",
desc = "Sets a player NPC's skin",
flags = "c",
modifiers = { "skin" },
min = 1,
max = 2,
permission = "citizens.npc.skin")
@Requirements(selected = true, ownership = true, types = EntityType.PLAYER)
public void playerSkin(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!args.hasFlag('c') && args.argsLength() == 0)
throw new CommandException();
String skin = args.hasFlag('c') ? "" : args.getString(1);
npc.getTrait(PlayerSkin.class).setSkinName(skin);
Messaging.sendTr(sender, skin.isEmpty() ? Messages.SKIN_CLEARED : Messages.SKIN_SET, npc.getFullName(), skin);
}
@Command(
aliases = { "npc" },
usage = "pose (--save [name]|--assume [name]|--remove [name]) (-a)",
@ -1037,7 +1018,7 @@ public class NPCCommands {
@Requirements(selected = true, ownership = true, types = { EntityType.CREEPER })
public void power(CommandContext args, CommandSender sender, NPC npc) {
Messaging
.sendTr(sender, npc.getTrait(Powered.class).toggle() ? Messages.POWERED_SET : Messages.POWERED_STOPPED);
.sendTr(sender, npc.getTrait(Powered.class).toggle() ? Messages.POWERED_SET : Messages.POWERED_STOPPED);
}
@Command(

View File

@ -52,12 +52,16 @@ public class CitizensNPC extends AbstractNPC {
public boolean despawn(DespawnReason reason) {
if (!isSpawned()) {
Messaging.debug("Tried to despawn", getId(), "while already despawned.");
if (reason == DespawnReason.REMOVAL) {
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this, reason));
}
return false;
}
NPCDespawnEvent event = new NPCDespawnEvent(this, reason);
if (reason == DespawnReason.CHUNK_UNLOAD)
if (reason == DespawnReason.CHUNK_UNLOAD) {
event.setCancelled(Setting.KEEP_CHUNKS_LOADED.asBoolean());
}
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
getEntity().getLocation().getChunk().load();
@ -73,6 +77,7 @@ public class CitizensNPC extends AbstractNPC {
}
navigator.onDespawn();
entityController.remove();
return true;
}

View File

@ -26,7 +26,6 @@ import net.citizensnpcs.trait.HorseModifiers;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.NPCSkeletonType;
import net.citizensnpcs.trait.OcelotModifiers;
import net.citizensnpcs.trait.PlayerSkin;
import net.citizensnpcs.trait.Poses;
import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.Saddle;
@ -60,7 +59,6 @@ public class CitizensTraitFactory implements TraitFactory {
registerTrait(TraitInfo.create(LookClose.class).withName("lookclose"));
registerTrait(TraitInfo.create(OcelotModifiers.class).withName("ocelotmodifiers"));
registerTrait(TraitInfo.create(Owner.class).withName("owner"));
registerTrait(TraitInfo.create(PlayerSkin.class).withName("playerskin"));
registerTrait(TraitInfo.create(Poses.class).withName("poses"));
registerTrait(TraitInfo.create(Powered.class).withName("powered"));
registerTrait(TraitInfo.create(Saddle.class).withName("saddle"));

View File

@ -7,7 +7,6 @@ import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.trait.PlayerSkin;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_7_R2.PlayerInteractManager;
import net.minecraft.server.v1_7_R2.WorldServer;
@ -27,12 +26,14 @@ public class HumanController extends AbstractEntityController {
@Override
protected Entity createEntity(final Location at, final NPC npc) {
WorldServer ws = ((CraftWorld) at.getWorld()).getHandle();
String parseColors = Colorizer.parseColors(npc.getTrait(PlayerSkin.class).getSkinName());
String parseColors = Colorizer.parseColors(npc.getFullName());
if (parseColors.length() > 16) {
parseColors = parseColors.substring(0, 16);
}
final EntityHumanNPC handle = new EntityHumanNPC(ws.getServer().getServer(), ws, new GameProfile(UUID
.nameUUIDFromBytes(parseColors.getBytes()).toString(), parseColors), new PlayerInteractManager(ws), npc);
UUID uuid = UUID.randomUUID(); // clear version
uuid = new UUID(uuid.getMostSignificantBits() | 0x0000000000005000L, uuid.getLeastSignificantBits());
final EntityHumanNPC handle = new EntityHumanNPC(ws.getServer().getServer(), ws, new GameProfile(
uuid.toString(), parseColors), new PlayerInteractManager(ws), npc);
handle.setPositionRotation(at.getX(), at.getY(), at.getZ(), at.getYaw(), at.getPitch());
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override

View File

@ -1,121 +0,0 @@
package net.citizensnpcs.trait;
import java.util.List;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.trait.MobType;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Slime;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import com.google.common.collect.Lists;
public class PlayerSkin extends Trait {
private final List<Entity> nameCarriers = Lists.newArrayList();
public PlayerSkin() {
super("playerskin");
}
private void despawnNameCarriers() {
if (nameCarriers.isEmpty())
return;
for (Entity entity : nameCarriers) {
entity.remove();
}
nameCarriers.clear();
}
public String getSkinName() {
String skin = npc.data().get(NPC.PLAYER_SKIN_NAME_METADATA, "");
if (skin.isEmpty()) {
skin = npc.getFullName();
}
return skin;
}
public boolean isEnabled() {
return npc.getTrait(MobType.class).getType() == EntityType.PLAYER
&& !npc.data().get(NPC.PLAYER_SKIN_NAME_METADATA, "").isEmpty();
}
@Override
public void onDespawn() {
despawnNameCarriers();
}
@Override
public void onRemove() {
despawnNameCarriers();
}
private Entity prepareEntity(String name, EntityType type) {
NPC npcEntity = npc.getOwningRegistry().createNPC(type, name);
npcEntity.data().set(NPC.AMBIENT_SOUND_METADATA, "");
npcEntity.data().set(NPC.DEFAULT_PROTECTED_METADATA, true);
npcEntity.data().set(NPC.DEATH_SOUND_METADATA, "");
npcEntity.data().set(NPC.HURT_SOUND_METADATA, "");
npcEntity.data().set(NPC.SHOULD_SAVE_METADATA, false);
npcEntity.spawn(npc.getStoredLocation());
if (name.isEmpty()) {
((LivingEntity) npcEntity.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 20
* 60 * 60 * 24 * 7, 1));
} else {
((Slime) npcEntity.getEntity()).setSize(-2);
}
return npcEntity.getEntity();
}
private void refreshPlayer() {
despawnNameCarriers();
Location last = npc.getEntity().getLocation();
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.spawn(last);
}
@Override
public void run() {
if (!npc.isSpawned() || npc.getEntity().getType() != EntityType.PLAYER
|| npc.data().get(NPC.PLAYER_SKIN_NAME_METADATA, "").isEmpty()) {
despawnNameCarriers();
return;
}
if (nameCarriers.size() == 0) {
Entity previous = npc.getEntity();
for (int i = 0; i < 2; i++) {
Entity heightCarrier = prepareEntity("", EntityType.SKELETON);
previous.setPassenger(heightCarrier);
nameCarriers.add(previous = heightCarrier);
}
Entity nameCarrier = prepareEntity(npc.getFullName(), EntityType.SLIME);
previous.setPassenger(nameCarrier);
nameCarriers.add(nameCarrier);
}
for (Entity entity : nameCarriers) {
if (entity instanceof LivingEntity) {
LivingEntity le = (LivingEntity) entity;
le.setRemainingAir(20);
if (!(le instanceof Slime) && !le.hasPotionEffect(PotionEffectType.INVISIBILITY)) {
((LivingEntity) entity).addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 20 * 60
* 60 * 24 * 7, 1));
}
}
}
}
public void setSkinName(String name) {
npc.data().setPersistent(NPC.PLAYER_SKIN_NAME_METADATA, name);
if (npc.isSpawned()) {
refreshPlayer();
}
}
}