ArmorStand command and trait

This commit is contained in:
fullwall 2016-01-01 11:33:11 +08:00
parent 76c51248d5
commit 829cc1ef63
4 changed files with 136 additions and 9 deletions

View File

@ -67,6 +67,7 @@ import net.citizensnpcs.npc.entity.nonliving.ItemFrameController.ItemFrameNPC;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.trait.Age;
import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.Gravity;
@ -223,6 +224,33 @@ public class NPCCommands {
npc.teleport(args.getSenderLocation(), TeleportCause.COMMAND);
}
@Command(
aliases = { "npc" },
usage = "armorstand --visible [visible] --small [small] --gravity [gravity] --arms [arms] --baseplate [baseplate]",
desc = "C whether the NPC can be ridden and controlled",
modifiers = { "armorstand", "control" },
min = 1,
max = 1)
@Requirements(selected = true, ownership = true, types = EntityType.ARMOR_STAND)
public void armorstand(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
ArmorStandTrait trait = npc.getTrait(ArmorStandTrait.class);
if (args.hasValueFlag("visible")) {
trait.setVisible(Boolean.valueOf(args.getFlag("visible")));
}
if (args.hasValueFlag("small")) {
trait.setSmall(Boolean.valueOf(args.getFlag("small")));
}
if (args.hasValueFlag("gravity")) {
trait.setGravity(Boolean.valueOf(args.getFlag("gravity")));
}
if (args.hasValueFlag("arms")) {
trait.setHasArms(Boolean.valueOf(args.getFlag("arms")));
}
if (args.hasValueFlag("baseplate")) {
trait.setHasBaseplate(Boolean.valueOf(args.getFlag("baseplate")));
}
}
@Command(
aliases = { "npc" },
usage = "controllable|control (-m(ount),-y,-n,-o)",

View File

@ -1,7 +1,5 @@
package net.citizensnpcs.npc;
import gnu.trove.map.hash.TIntObjectHashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -10,6 +8,14 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import gnu.trove.map.hash.TIntObjectHashMap;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCCreateEvent;
@ -18,15 +24,9 @@ import net.citizensnpcs.api.npc.NPCDataStore;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.util.NMS;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
public class CitizensNPCRegistry implements NPCRegistry {
private final NPCCollection npcs = TROVE_EXISTS ? new TroveNPCCollection() : new MapNPCCollection();
private final NPCDataStore saves;
@ -49,6 +49,9 @@ public class CitizensNPCRegistry implements NPCRegistry {
throw new IllegalStateException("Could not create NPC.");
npcs.put(npc.getId(), npc);
Bukkit.getPluginManager().callEvent(new NPCCreateEvent(npc));
if (type == EntityType.ARMOR_STAND && !npc.hasTrait(ArmorStandTrait.class)) {
npc.addTrait(ArmorStandTrait.class);
}
return npc;
}

View File

@ -24,6 +24,7 @@ import net.citizensnpcs.api.trait.trait.Spawned;
import net.citizensnpcs.api.trait.trait.Speech;
import net.citizensnpcs.trait.Age;
import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.Gravity;
@ -52,6 +53,7 @@ public class CitizensTraitFactory implements TraitFactory {
public CitizensTraitFactory() {
registerTrait(TraitInfo.create(Age.class).withName("age"));
registerTrait(TraitInfo.create(ArmorStandTrait.class).withName("armorstandtrait"));
registerTrait(TraitInfo.create(Anchors.class).withName("anchors"));
registerTrait(TraitInfo.create(Controllable.class).withName("controllable"));
registerTrait(TraitInfo.create(Equipment.class).withName("equipment"));

View File

@ -0,0 +1,94 @@
package net.citizensnpcs.trait;
import org.bukkit.entity.ArmorStand;
import org.bukkit.util.EulerAngle;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
public class ArmorStandTrait extends Trait {
@Persist
private EulerAngle body;
@Persist
private boolean gravity = true;
@Persist
private boolean hasarms = true;
@Persist
private boolean hasbaseplate = true;
@Persist
private EulerAngle leftArm;
@Persist
private EulerAngle leftLeg;
@Persist
private EulerAngle rightArm;
@Persist
private EulerAngle rightLeg;
@Persist
private boolean small;
@Persist
private boolean visible = true;
public ArmorStandTrait() {
super("armorstandtrait");
}
@Override
public void onSpawn() {
if (npc.getEntity() instanceof ArmorStand) {
ArmorStand entity = (ArmorStand) npc.getEntity();
if (leftArm != null) {
entity.setLeftArmPose(leftArm);
}
if (leftLeg != null) {
entity.setLeftLegPose(leftLeg);
}
if (rightArm != null) {
entity.setRightArmPose(rightArm);
}
if (rightLeg != null) {
entity.setRightLegPose(rightLeg);
}
if (body != null) {
entity.setBodyPose(body);
}
}
}
@Override
public void run() {
if (npc.getEntity() instanceof ArmorStand) {
ArmorStand entity = (ArmorStand) npc.getEntity();
body = entity.getBodyPose();
leftArm = entity.getLeftArmPose();
leftLeg = entity.getLeftLegPose();
rightArm = entity.getRightArmPose();
rightLeg = entity.getRightLegPose();
entity.setVisible(visible);
entity.setGravity(gravity);
entity.setArms(hasarms);
entity.setBasePlate(hasbaseplate);
entity.setSmall(small);
}
}
public void setGravity(boolean gravity) {
this.gravity = gravity;
}
public void setHasArms(boolean arms) {
this.hasarms = arms;
}
public void setHasBaseplate(boolean baseplate) {
this.hasbaseplate = baseplate;
}
public void setSmall(boolean small) {
this.small = small;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}