From 883b6d57aca02851adf1ce097628019bd2617555 Mon Sep 17 00:00:00 2001 From: LemADEC Date: Thu, 27 Jul 2017 00:30:10 +0200 Subject: [PATCH] Added /wfind command --- src/main/java/cr0s/warpdrive/WarpDrive.java | 2 + .../cr0s/warpdrive/command/CommandFind.java | 48 ++++++++++++++ .../cr0s/warpdrive/data/StarMapRegistry.java | 66 +++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/main/java/cr0s/warpdrive/command/CommandFind.java diff --git a/src/main/java/cr0s/warpdrive/WarpDrive.java b/src/main/java/cr0s/warpdrive/WarpDrive.java index e93024fc..b94f6f3d 100644 --- a/src/main/java/cr0s/warpdrive/WarpDrive.java +++ b/src/main/java/cr0s/warpdrive/WarpDrive.java @@ -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()); diff --git a/src/main/java/cr0s/warpdrive/command/CommandFind.java b/src/main/java/cr0s/warpdrive/command/CommandFind.java new file mode 100644 index 00000000..20809485 --- /dev/null +++ b/src/main/java/cr0s/warpdrive/command/CommandFind.java @@ -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() + " ()" + + "\nshipName: name of the ship to find. Exact casing is preferred."; + } +} diff --git a/src/main/java/cr0s/warpdrive/data/StarMapRegistry.java b/src/main/java/cr0s/warpdrive/data/StarMapRegistry.java index 73562a3e..7e07e4d3 100644 --- a/src/main/java/cr0s/warpdrive/data/StarMapRegistry.java +++ b/src/main/java/cr0s/warpdrive/data/StarMapRegistry.java @@ -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 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 setStarMapRegistryItems = registry.get(world.provider.dimensionId); if (setStarMapRegistryItems == null) {