MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / spawnItem

Method spawnItem

source/game/StarCommandProcessor.cpp:327–374  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

325}
326
327String CommandProcessor::spawnItem(ConnectionId connectionId, String const& argumentString) {
328 if (auto errorMsg = adminCheck(connectionId, "spawn items"))
329 return *errorMsg;
330
331 auto arguments = m_parser.tokenizeToStringList(argumentString);
332
333 if (arguments.empty())
334 return "Not enough arguments to /spawnitem";
335
336 try {
337 String kind = arguments.at(0);
338 Json parameters = JsonObject();
339 unsigned amount = 1;
340 Maybe<float> level;
341 Maybe<uint64_t> seed;
342
343 if (arguments.size() >= 2)
344 amount = lexicalCast<unsigned>(arguments.at(1));
345
346 if (arguments.size() >= 3)
347 parameters = Json::parse(arguments.at(2));
348
349 if (arguments.size() >= 4)
350 level = lexicalCast<float>(arguments.at(3));
351
352 if (arguments.size() >= 5)
353 seed = lexicalCast<uint64_t>(arguments.at(4));
354
355 bool done = m_universe->executeForClient(connectionId, [&](WorldServer* world, PlayerPtr const& player) {
356 auto itemDatabase = Root::singleton().itemDatabase();
357 world->addEntity(ItemDrop::createRandomizedDrop(itemDatabase->item(ItemDescriptor(kind, amount, parameters), level, seed, true), player->aimPosition()));
358 });
359
360 return done ? "" : "Invalid client state";
361 } catch (JsonParsingException const& exception) {
362 Logger::warn("Error while processing /spawnitem '{}' command. Json parse problem: {}", arguments.at(0), outputException(exception, false));
363 return "Could not parse item parameters";
364 } catch (ItemException const& exception) {
365 Logger::warn("Error while processing /spawnitem '{}' command. Item instantiation problem: {}", arguments.at(0), outputException(exception, false));
366 return strf("Could not load item '{}'", arguments.at(0));
367 } catch (BadLexicalCast const& exception) {
368 Logger::warn("Error while processing /spawnitem command. Number expected. Got something else: {}", outputException(exception, false));
369 return strf("Could not load item '{}'", arguments.at(0));
370 } catch (StarException const& exception) {
371 Logger::warn("Error while processing /spawnitem command '{}', exception caught: {}", argumentString, outputException(exception, false));
372 return strf("Could not load item '{}'", arguments.at(0));
373 }
374}
375
376String CommandProcessor::spawnTreasure(ConnectionId connectionId, String const& argumentString) {
377 if (auto errorMsg = adminCheck(connectionId, "spawn items"))

Callers

nothing calls this directly

Calls 14

singletonClass · 0.85
ItemDescriptorClass · 0.85
strfFunction · 0.85
tokenizeToStringListMethod · 0.80
executeForClientMethod · 0.80
itemDatabaseMethod · 0.80
parseFunction · 0.50
outputExceptionFunction · 0.50
emptyMethod · 0.45
atMethod · 0.45
sizeMethod · 0.45
addEntityMethod · 0.45

Tested by

no test coverage detected