Add selection param

This commit is contained in:
fullwall 2012-10-15 11:31:40 +08:00
parent 56e9189193
commit 57c96f250a
8 changed files with 123 additions and 16 deletions

View File

@ -1,6 +1,8 @@
package net.citizensnpcs.command.command;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import net.citizensnpcs.Citizens;
@ -55,6 +57,7 @@ import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
@Requirements(selected = true, ownership = true)
public class NPCCommands {
@ -154,8 +157,8 @@ public class NPCCommands {
for (int i = 0; i < trait.getAnchors().size(); i++) {
String line = "<a>" + i + "<b> " + trait.getAnchors().get(i).getName() + "<c> "
+ trait.getAnchors().get(i).getLocation().getWorld().getName() + "<d> "
+ trait.getAnchors().get(i).getLocation().getBlockX()+ ", "
+ trait.getAnchors().get(i).getLocation().getBlockY()+ ", "
+ trait.getAnchors().get(i).getLocation().getBlockX() + ", "
+ trait.getAnchors().get(i).getLocation().getBlockY() + ", "
+ trait.getAnchors().get(i).getLocation().getBlockZ();
paginator.addLine(line);
}
@ -682,8 +685,8 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "select|sel [id] (--r range)",
desc = "Select a NPC with the given ID",
usage = "select|sel [id|name] (--r range)",
desc = "Select a NPC with the given ID or name",
modifiers = { "select", "sel" },
min = 1,
max = 2,
@ -695,7 +698,17 @@ public class NPCCommands {
if (!(sender instanceof Player))
throw new ServerCommandException();
double range = Math.abs(args.getFlagDouble("r", 10));
List<Entity> search = ((Player) sender).getNearbyEntities(range, range, range);
Player player = (Player) sender;
final Location location = player.getLocation();
List<Entity> search = player.getNearbyEntities(range, range, range);
Collections.sort(search, new Comparator<Entity>() {
@Override
public int compare(Entity o1, Entity o2) {
double d = o1.getLocation().distanceSquared(location)
- o2.getLocation().distanceSquared(location);
return d > 0 ? 1 : d < 0 ? -1 : 0;
}
});
for (Entity possibleNPC : search) {
NPC test = npcRegistry.getNPC(possibleNPC);
if (test == null)
@ -703,8 +716,25 @@ public class NPCCommands {
toSelect = test;
break;
}
} else
toSelect = npcRegistry.getById(args.getInteger(1));
} else {
try {
int id = args.getInteger(1);
toSelect = npcRegistry.getById(id);
} catch (NumberFormatException ex) {
String name = args.getString(1);
List<NPC> possible = Lists.newArrayList();
for (NPC test : npcRegistry) {
if (test.getName().equalsIgnoreCase(name))
possible.add(test);
}
if (possible.size() == 1)
toSelect = possible.get(0);
else if (possible.size() > 1) {
SelectionPrompt.start(selector, (Player) sender, possible);
return;
}
}
}
if (toSelect == null || !toSelect.getTrait(Spawned.class).shouldSpawn())
throw new CommandException(Messages.NPC_NOT_FOUND);
if (npc != null && toSelect.getId() == npc.getId())

View File

@ -0,0 +1,67 @@
package net.citizensnpcs.command.command;
import java.util.List;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.NPCSelector;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.ConversationFactory;
import org.bukkit.conversations.NumericPrompt;
import org.bukkit.conversations.Prompt;
import org.bukkit.entity.Player;
public class SelectionPrompt extends NumericPrompt {
private final List<NPC> choices;
private final NPCSelector selector;
public SelectionPrompt(NPCSelector selector, List<NPC> possible) {
choices = possible;
this.selector = selector;
}
public static void start(NPCSelector selector, Player player, List<NPC> possible) {
final Conversation conversation = new ConversationFactory(CitizensAPI.getPlugin())
.withLocalEcho(false).withEscapeSequence("exit").withModality(false)
.withFirstPrompt(new SelectionPrompt(selector, possible)).buildConversation(player);
conversation.begin();
}
@Override
public String getPromptText(ConversationContext context) {
String text = Messaging.tr(Messages.SELECTION_PROMPT);
int num = 1;
for (NPC npc : choices) {
text += "<br> - " + npc.getId() + "(" + num + ")";
context.setSessionData(npc.getId(), num);
num++;
}
return text;
}
@Override
protected Prompt acceptValidatedInput(ConversationContext context, Number input) {
Object num = context.getSessionData(input);
if (num == null) {
for (NPC npc : choices) {
if (input.intValue() == npc.getId()) {
num = input.intValue();
break;
}
}
if (num == null)
return this;
}
NPC toSelect = CitizensAPI.getNPCRegistry().getById((Integer) num);
CommandSender sender = (CommandSender) context.getForWhom();
selector.select(sender, toSelect);
Messaging.sendWithNPC(sender, Setting.SELECTION_MESSAGE.asString(), toSelect);
return null;
}
}

View File

@ -16,6 +16,7 @@ import net.citizensnpcs.npc.ai.CitizensNavigator;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityPlayer;
@ -172,8 +173,10 @@ public abstract class CitizensNPC extends AbstractNPC {
public void update() {
try {
super.update();
if (isSpawned())
if (isSpawned()) {
NMS.trySwim(getHandle());
navigator.update();
}
} catch (Exception ex) {
Messaging.logTr(Messages.EXCEPTION_UPDATING_NPC, getId(), ex.getMessage());
ex.printStackTrace();

View File

@ -8,7 +8,6 @@ import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityMonster;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.Navigation;
import net.minecraft.server.Packet18ArmAnimation;
@ -95,13 +94,13 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
navigation.a(target, parameters.speed());
NMS.look(handle.getControllerLook(), handle, target);
if (aggro && canAttack()) {
if (handle instanceof EntityMonster) {
NMS.attack(handle, target);
} else if (handle instanceof EntityPlayer) {
if (handle instanceof EntityPlayer) {
EntityPlayer humanHandle = (EntityPlayer) handle;
humanHandle.attack(target);
Util.sendPacketNearby(handle.getBukkitEntity().getLocation(), new Packet18ArmAnimation(
humanHandle, 1), 64);
} else {
NMS.attack(handle, target);
}
attackTicks = ATTACK_DELAY_TICKS;
}

View File

@ -139,10 +139,11 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
// taken from EntityLiving update method
if (bu) {
boolean inLiquid = H() || J();
if (inLiquid) {
motY += 0.04;
} else if (onGround && bE == 0) {
/* boolean inLiquid = H() || J();
if (inLiquid) {
motY += 0.04;
} else (handled elsewhere)*/
if (onGround && bE == 0) {
// this.aZ(); - this doesn't jump high enough
motY = 0.6;
bE = 10;

View File

@ -169,4 +169,5 @@ public class Messages {
public static final String VULNERABLE_STOPPED = "citizens.commands.npc.vulnerable.stopped";
public static final String WAYPOINT_PROVIDER_SET = "citizens.waypoints.set-provider";
public static final String WRITING_DEFAULT_SETTING = "citizens.settings.writing-default";
public static final String SELECTION_PROMPT = "citizens.editors.selection.start-prompt";
}

View File

@ -256,4 +256,9 @@ public class NMS {
Messaging.logTr(Messages.ERROR_GETTING_ID_MAPPING, e.getMessage());
}
}
public static void trySwim(EntityLiving handle) {
if ((handle.H() || handle.J()) && Math.random() < 0.8F)
handle.motY += 0.04;
}
}

View File

@ -118,6 +118,7 @@ citizens.editors.text.realistic-looking-set=[[Realistic looking]] set to [[{0}]]
citizens.editors.text.remove-prompt=Enter the index of the entry you wish to remove or [[page]] to view more pages.
citizens.editors.text.removed-entry=[[Removed]] entry at index [[{0}]].
citizens.editors.text.start-prompt=Type [[add]] to add an entry, [[edit]] to edit entries, [[remove]] to remove entries, [[close]] to toggle the NPC as a close talker, and [[random]] to toggle the NPC as a random talker. Type [[help]] to show this again.
citizens.editors.selection.start-prompt=There were multiple NPCs with the supplied name.<br>Please enter an id or number from the list below to select that NPC.
citizens.editors.waypoints.linear.added-waypoint=[[Added]] a waypoint at ({0}) ([[{1}]], [[{2}]])
citizens.editors.waypoints.linear.begin=<b>Entered the linear waypoint editor!<br>[[Left click]] to add a waypoint, [[right click]] to remove.<br>Type [[toggle path]] to toggle showing entities at waypoints.
citizens.editors.waypoints.linear.edit-slot-set=Editing slot set to [[{0}]] ({1}).