Best MockBukkit code snippet using be.seeseemelk.mockbukkit.plugin.PluginManagerMock.addCommandsFrom
Source:PluginManagerMock.java
...304 "MockBukkit-" + description.getName() + "-" + description.getVersion());305 System.arraycopy(parameters, 0, arguments, 4, parameters.length);306 307 JavaPlugin plugin = constructor.newInstance(arguments);308 addCommandsFrom(plugin);309 plugins.add(plugin);310 plugin.onLoad();311 return plugin;312 }313 catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException314 | IllegalArgumentException | IOException | InvocationTargetException e)315 {316 throw new RuntimeException("Failed to instantiate plugin", e);317 }318 }319 320 /**321 * Load a plugin from a class. It will use the system resource322 * {@code plugin.yml} as the resource file.323 * 324 * @param description325 * The {@link PluginDescriptionFile} that contains information about326 * the plugin.327 * @param class1328 * The plugin to load.329 * @return The loaded plugin.330 */331 public JavaPlugin loadPlugin(Class<? extends JavaPlugin> class1, PluginDescriptionFile description)332 {333 return loadPlugin(class1, description, new Object[0]);334 }335 336 /**337 * Load a plugin from a class. It will use the system resource338 * {@code plugin.yml} as the resource file.339 * 340 * @param class1341 * The plugin to load.342 * @param parameters343 * Extra parameters to pass on to the plugin constructor.344 * @return The loaded plugin.345 */346 public JavaPlugin loadPlugin(Class<? extends JavaPlugin> class1, Object[] parameters)347 {348 try349 {350 PluginDescriptionFile description = findPluginDescription(class1);351 return loadPlugin(class1, description, parameters);352 }353 catch (IOException | InvalidDescriptionException e)354 {355 throw new RuntimeException(e);356 }357 }358 359 /**360 * Tries to find the correct plugin.yml for a given plugin.361 * It does this by opening each plugin.yml that it finds and362 * comparing the 'main' property to the qualified name of the363 * plugin class.364 * @param class1 The class that is in a subpackage of where to get the file.365 * @return The plugin description file.366 * @throws IOException Thrown when the file wan't be found or loaded.367 * @throws InvalidDescriptionException If the plugin description file is formatted incorrectly.368 */369 private PluginDescriptionFile findPluginDescription(Class<? extends JavaPlugin> class1) throws IOException, InvalidDescriptionException370 {371 Enumeration<URL> resources = class1.getClassLoader().getResources("plugin.yml");372 while (resources.hasMoreElements())373 {374 URL url = resources.nextElement();375 PluginDescriptionFile description = new PluginDescriptionFile(url.openStream());376 String mainClass = description.getMain();377 if (class1.getName().equals(mainClass))378 return description;379 }380 throw new FileNotFoundException(String.format("Could not find file plugin.yml. Maybe forgot to add the 'main' property?"));381 }382 383 @Override384 public void callEvent(Event event) throws IllegalStateException385 {386 events.add(event);387 for (List<ListenerEntry> listeners : eventListeners.values())388 {389 for (ListenerEntry entry : listeners)390 {391 if (entry.isCompatibleFor(event))392 {393 entry.invokeUnsafe(event);394 }395 }396 }397 }398 399 @Override400 public void registerEvents(Listener listener, Plugin plugin)401 {402 if (!eventListeners.containsKey(plugin))403 eventListeners.put(plugin, new LinkedList<>());404 405 List<ListenerEntry> listeners = eventListeners.get(plugin);406 for (Method method : listener.getClass().getMethods())407 {408 EventHandler annotation = method.getAnnotation(EventHandler.class);409 if (annotation != null)410 listeners.add(new ListenerEntry(plugin, listener, method));411 }412 }413 414 @Override415 public void enablePlugin(Plugin plugin)416 {417 if (plugin instanceof JavaPlugin)418 {419 if (!plugin.isEnabled())420 {421 JavaPluginUtils.setEnabled((JavaPlugin) plugin, true);422 callEvent(new PluginEnableEvent(plugin));423 }424 }425 else426 {427 throw new IllegalArgumentException("Not a JavaPlugin");428 }429 }430 431 /**432 * Adds a configuration section to a command.433 * 434 * @param command435 * The command to add it to.436 * @param name437 * The name of the section, as read in a configuration file.438 * @param value439 * The value of the section, as parsed by {@link YamlConfiguration}440 */441 private void addSection(PluginCommand command, String name, Object value)442 {443 switch (name)444 {445 case "description":446 command.setDescription((String) value);447 break;448 case "aliases":449 List<String> aliases = new ArrayList<>();450 if (value instanceof List<?>)451 command.setAliases(452 ((List<?>) aliases).stream().map(object -> object.toString()).collect(Collectors.toList()));453 else454 command.setAliases(Arrays.asList(value.toString()));455 break;456 case "permission":457 command.setPermission((String) value);458 break;459 case "permission-message":460 command.setPermissionMessage((String) value);461 break;462 case "usage":463 command.setUsage((String) value);464 break;465 default:466 throw new UnsupportedOperationException("Unknown section " + value);467 }468 }469 470 /**471 * Add commands from a certain plugin to the internal list of commands.472 * 473 * @param plugin474 * The plugin from which to read commands.475 */476 protected void addCommandsFrom(JavaPlugin plugin)477 {478 Map<String, Map<String, Object>> commands = plugin.getDescription().getCommands();479 if (commands != null)480 {481 for (Entry<String, Map<String, Object>> entry : commands.entrySet())482 {483 PluginCommand command = PluginCommandUtils.createPluginCommand(entry.getKey(), plugin);484 for (Entry<String, Object> section : entry.getValue().entrySet())485 {486 addSection(command, section.getKey(), section.getValue());487 }488 this.commands.add(command);489 }490 }...
addCommandsFrom
Using AI Code Generation
1import org.bukkit.plugin.java.JavaPlugin;2import org.bukkit.plugin.PluginManager;3import org.bukkit.command.PluginCommandYamlParser;4import org.bukkit.command.Command;5import org.bukkit.command.CommandMap;6import org.bukkit.command.CommandExecutor;7import org.bukkit.command.CommandSender;8import org.bukkit.command.Command;9import be.seeseemelk.mockbukkit.MockBukkit;10import be.seeseemelk.mockbukkit.ServerMock;11import be.seeseemelk.mockbukkit.plugin.PluginManagerMock;12import java.io.File;13import java.io.IOException;14import java.util.ArrayList;15import java.util.List;16import java.util.Map;17import java.util.logging.Logger;18{19 private static final Logger logger = Logger.getLogger("Minecraft");20 private ServerMock server;21 private PluginManagerMock pluginManager;22 public void onEnable()23 {24 server = MockBukkit.mock();25 pluginManager = server.getPluginManager();26 pluginManager.addCommandsFrom(this);27 }28 public void onDisable()29 {30 MockBukkit.unmock();31 }32 public boolean onCommand(CommandSender sender, Command command, String label, String[] args)33 {34 logger.info("Command: " + label);35 return true;36 }37}
addCommandsFrom
Using AI Code Generation
1{2 public MyCommand()3 {4 super("mycommand");5 }6 public boolean execute(CommandSender sender, String label, String[] args)7 {8 sender.sendMessage("Hello, world!");9 return true;10 }11}12{13 private MockBukkit mockBukkit;14 private ServerMock server;15 private PluginManagerMock pluginManager;16 private CommandManagerMock commandManager;17 private MyCommand command;18 public void setUp()19 {20 mockBukkit = MockBukkit.mock();21 server = mockBukkit.getServer();22 pluginManager = server.getPluginManager();23 commandManager = server.getCommandManager();24 command = new MyCommand();25 }26 public void tearDown()27 {28 MockBukkit.unmock();29 }30 public void testCommand()31 {32 pluginManager.addCommandsFrom(new MyPlugin());33 commandManager.addCommand(command);34 PlayerMock player = server.addPlayer();35 player.performCommand("mycommand");36 assertThat(player, receivedMessage("Hello, world!"));37 }38}39{40 public void onEnable()41 {42 getCommand("mycommand").setExecutor(new MyCommand());43 }44}45{46 private MockBukkit mockBukkit;47 private ServerMock server;48 private PluginManagerMock pluginManager;49 private CommandManagerMock commandManager;50 private MyPlugin plugin;51 private MyCommand command;52 public void setUp()53 {54 mockBukkit = MockBukkit.mock();55 server = mockBukkit.getServer();56 pluginManager = server.getPluginManager();57 commandManager = server.getCommandManager();58 plugin = new MyPlugin();59 command = new MyCommand();60 }61 public void tearDown()62 {63 MockBukkit.unmock();64 }65 public void testPlugin()66 {67 pluginManager.addCommandsFrom(plugin);68 PlayerMock player = server.addPlayer();69 player.performCommand("
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!