Added /wfind command

This commit is contained in:
LemADEC 2017-07-27 00:30:10 +02:00
parent f2266270cb
commit 883b6d57ac
3 changed files with 116 additions and 0 deletions

View file

@ -92,6 +92,7 @@ import cr0s.warpdrive.block.weapon.TileEntityWeaponController;
import cr0s.warpdrive.command.CommandDebug;
import cr0s.warpdrive.command.CommandDump;
import cr0s.warpdrive.command.CommandEntity;
import cr0s.warpdrive.command.CommandFind;
import cr0s.warpdrive.command.CommandGenerate;
import cr0s.warpdrive.command.CommandInvisible;
import cr0s.warpdrive.command.CommandJumpgates;
@ -750,6 +751,7 @@ public class WarpDrive implements LoadingCallback {
event.registerServerCommand(new CommandDebug());
event.registerServerCommand(new CommandDump());
event.registerServerCommand(new CommandEntity());
event.registerServerCommand(new CommandFind());
event.registerServerCommand(new CommandGenerate());
event.registerServerCommand(new CommandInvisible());
event.registerServerCommand(new CommandJumpgates());

View file

@ -0,0 +1,48 @@
package cr0s.warpdrive.command;
import cr0s.warpdrive.Commons;
import cr0s.warpdrive.WarpDrive;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
public class CommandFind extends CommandBase {
@Override
public int getRequiredPermissionLevel() {
return 2;
}
@Override
public String getCommandName() {
return "wfind";
}
@Override
public void processCommand(ICommandSender commandSender, String[] params) {
if (commandSender == null) { return; }
// parse arguments
//noinspection StatementWithEmptyBody
String nameToken = "";
if (params.length == 0) {
Commons.addChatMessage(commandSender, getCommandUsage(commandSender));
return;
} else if (params.length == 1) {
if (params[0].equalsIgnoreCase("help") || params[0].equalsIgnoreCase("?")) {
Commons.addChatMessage(commandSender, getCommandUsage(commandSender));
return;
}
nameToken = params[0];
}
final String result = WarpDrive.starMap.find(nameToken);
Commons.addChatMessage(commandSender, result);
}
@Override
public String getCommandUsage(final ICommandSender commandSender) {
return getCommandName() + " (<shipName>)"
+ "\nshipName: name of the ship to find. Exact casing is preferred.";
}
}

View file

@ -95,6 +95,72 @@ public class StarMapRegistry {
// not found => ignore it
}
public String find(final String nameShip) {
final int MAX_LENGTH = 2000;
final StringBuilder resultMatch = new StringBuilder();
final StringBuilder resultCaseInsensitive = new StringBuilder();
final StringBuilder resultContains = new StringBuilder();
for (final Integer dimensionId : registry.keySet()) {
final CopyOnWriteArraySet<StarMapRegistryItem> setStarMapRegistryItems = registry.get(dimensionId);
if (setStarMapRegistryItems == null) {
continue;
}
for (final StarMapRegistryItem starMapRegistryItem : setStarMapRegistryItems) {
if (starMapRegistryItem.type == EnumStarMapEntryType.SHIP) {
if (starMapRegistryItem.name.equals(nameShip)) {
if (resultMatch.length() < MAX_LENGTH) {
if (resultMatch.length() > 0) {
resultMatch.append("\n");
}
resultMatch.append(String.format("Ship '%s' found in DIM%d @ (%d %d %d)",
starMapRegistryItem.name,
starMapRegistryItem.dimensionId,
starMapRegistryItem.x, starMapRegistryItem.y, starMapRegistryItem.z));
} else {
resultMatch.append(".");
}
} else if (starMapRegistryItem.name.equalsIgnoreCase(nameShip)) {
if (resultMatch.length() < MAX_LENGTH) {
if (resultCaseInsensitive.length() > 0) {
resultCaseInsensitive.append("\n");
}
resultCaseInsensitive.append(String.format("Ship '%s' found in DIM%d @ (%d %d %d)",
starMapRegistryItem.name,
starMapRegistryItem.dimensionId,
starMapRegistryItem.x, starMapRegistryItem.y, starMapRegistryItem.z));
} else {
resultCaseInsensitive.append(".");
}
} else if (starMapRegistryItem.name.contains(nameShip)) {
if (resultMatch.length() < MAX_LENGTH) {
if (resultContains.length() > 0) {
resultContains.append("\n");
}
resultContains.append(String.format("Ship '%s' found in DIM%d @ (%d %d %d)",
starMapRegistryItem.name,
starMapRegistryItem.dimensionId,
starMapRegistryItem.x, starMapRegistryItem.y, starMapRegistryItem.z));
} else {
resultContains.append(".");
}
}
}
}
}
if (resultMatch.length() > 0) {
return resultMatch.toString();
}
if (resultCaseInsensitive.length() > 0) {
return resultCaseInsensitive.toString();
}
if (resultContains.length() > 0) {
return resultMatch.toString();
}
return String.format("No ship found with name '%s'", nameShip);
}
public void onBlockUpdated(final World world, final int x, final int y, final int z, final Block block, final int metadata) {
final CopyOnWriteArraySet<StarMapRegistryItem> setStarMapRegistryItems = registry.get(world.provider.dimensionId);
if (setStarMapRegistryItems == null) {