use of org.spongepowered.api.CatalogType in project LanternServer by LanternPowered.
the class LanternGameRegistry method registerModulePhase.
@SuppressWarnings("unchecked")
private void registerModulePhase() {
syncModules();
for (Class<? extends RegistryModule> moduleClass : this.orderedModules) {
if (!this.classMap.containsKey(moduleClass)) {
throw new IllegalStateException("Something funky happened! The module " + moduleClass + " is required but seems to be missing.");
}
tryModulePhaseRegistration(this.classMap.get(moduleClass));
if (this.phase == RegistrationPhase.INIT) {
Map.Entry<Class<? extends CatalogType>, CatalogRegistryModule<?>> selectedEntry = null;
for (Map.Entry<Class<? extends CatalogType>, CatalogRegistryModule<?>> entry : this.catalogRegistryMap.entrySet()) {
if (entry.getValue().getClass() == moduleClass) {
selectedEntry = entry;
break;
}
}
if (selectedEntry == null) {
continue;
}
final CatalogRegistryModule module = selectedEntry.getValue();
if (module instanceof AdditionalCatalogRegistryModule && module.getClass().getAnnotation(CustomRegistrationPhase.class) == null) {
this.game.getEventManager().post(new LanternGameRegistryRegisterEvent(CauseStack.current().getCurrentCause(), selectedEntry.getKey(), (AdditionalCatalogRegistryModule) module));
}
}
}
registerAdditionalPhase();
}
use of org.spongepowered.api.CatalogType in project Nucleus by NucleusPowered.
the class ItemInfoCommand method executeCommand.
@Override
public CommandResult executeCommand(Player player, CommandContext args) throws Exception {
Optional<CatalogType> catalogTypeOptional = args.getOne(key);
ItemStack it;
if (catalogTypeOptional.isPresent()) {
CatalogType ct = catalogTypeOptional.get();
if (ct instanceof ItemType) {
it = ((ItemType) ct).getTemplate().createStack();
} else {
BlockState bs = ((BlockState) ct);
it = bs.getType().getItem().orElseThrow(() -> new CommandException(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.invalidblockstate"))).getTemplate().createStack();
it.offer(Keys.ITEM_BLOCKSTATE, bs);
}
} else if (player.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
it = player.getItemInHand(HandTypes.MAIN_HAND).get();
} else {
player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.none"));
return CommandResult.empty();
}
final List<Text> lt = new ArrayList<>();
String id = it.getType().getId().toLowerCase();
lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.id", it.getType().getId(), it.getTranslation().get()));
Optional<BlockState> obs = it.get(Keys.ITEM_BLOCKSTATE);
if (obs.isPresent()) {
lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.extendedid", obs.get().getId()));
id = obs.get().getId().toLowerCase();
}
if (args.hasAny("e") || args.hasAny("extended")) {
// For each key, see if the item supports it. If so, get and
// print the value.
DataScanner.getInstance().getKeysForHolder(it).entrySet().stream().filter(x -> x.getValue() != null).filter(x -> {
// Work around a Sponge bug.
try {
return it.supports(x.getValue());
} catch (Exception e) {
return false;
}
}).forEach(x -> {
Key<? extends BaseValue<Object>> k = (Key<? extends BaseValue<Object>>) x.getValue();
if (it.get(k).isPresent()) {
DataScanner.getText(player, "command.iteminfo.key", x.getKey(), it.get(k).get()).ifPresent(lt::add);
}
});
}
ItemDataNode itemDataNode = itemDataService.getDataForItem(id);
// /buy and /sell prices
if (econHelper.economyServiceExists() && plugin.getModuleContainer().isModuleLoaded(ServerShopModule.ID)) {
boolean space = false;
double buyPrice = itemDataNode.getServerBuyPrice();
if (buyPrice >= 0) {
lt.add(Text.EMPTY);
lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.buyprice", econHelper.getCurrencySymbol(buyPrice)));
space = true;
}
double sellPrice = itemDataNode.getServerSellPrice();
if (sellPrice >= 0) {
if (!space) {
lt.add(Text.EMPTY);
}
lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.sellprice", econHelper.getCurrencySymbol(sellPrice)));
}
}
List<String> aliases = itemDataNode.getAliases();
if (!aliases.isEmpty()) {
lt.add(Text.EMPTY);
lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.list.aliases"));
Text.Builder tb = Text.builder();
Iterator<String> iterator = aliases.iterator();
tb.append(Text.of(TextColors.YELLOW, iterator.next()));
while (iterator.hasNext()) {
tb.append(comma, Text.of(TextColors.YELLOW, iterator.next()));
}
lt.add(tb.build());
}
Sponge.getServiceManager().provideUnchecked(PaginationService.class).builder().contents(lt).padding(Text.of(TextColors.GREEN, "-")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.list.header")).sendTo(player);
return CommandResult.success();
}
use of org.spongepowered.api.CatalogType in project Nucleus by NucleusPowered.
the class ClearItemAliasesCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
CatalogType al = args.<CatalogType>getOne(item).get();
String id = al.getId().toLowerCase();
ItemDataNode node = itemDataService.getDataForItem(id);
node.clearAliases();
itemDataService.setDataForItem(id, node);
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.removeitemalias.cleared", id));
return CommandResult.success();
}
use of org.spongepowered.api.CatalogType in project Nucleus by NucleusPowered.
the class SetItemAliasCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
// Do we have an item or blockstate?
String a = args.<String>getOne(alias).get().toLowerCase();
if (itemDataService.getIdFromAlias(a).isPresent()) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.setitemalias.inuse", a));
return CommandResult.empty();
}
if (!ItemDataNode.ALIAS_PATTERN.matcher(a).matches()) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.setitemalias.notvalid", a));
return CommandResult.empty();
}
CatalogType type = getCatalogTypeFromHandOrArgs(src, item, args);
// Set the alias.
String id = type.getId().toLowerCase();
ItemDataNode idn = itemDataService.getDataForItem(id);
idn.addAlias(a);
itemDataService.setDataForItem(id, idn);
// Tell the user
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.setitemalias.success", a, id));
return CommandResult.success();
}
use of org.spongepowered.api.CatalogType in project Nucleus by NucleusPowered.
the class BuyCommand method executeCommand.
@Override
public CommandResult executeCommand(final Player src, CommandContext args) throws Exception {
CatalogType ct = args.<CatalogType>getOne(itemKey).get();
int amount = args.<Integer>getOne(amountKey).get();
ItemDataNode node = itemDataService.getDataForItem(ct.getId());
if (node.getServerBuyPrice() < 0) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itembuy.notforsale"));
return CommandResult.empty();
}
if (amount > max) {
amount = max;
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itembuy.maximum", String.valueOf(amount)));
}
final ItemStack created;
if (ct instanceof ItemType) {
created = ItemStack.of((ItemType) ct, amount);
} else {
created = ItemStack.builder().fromBlockState((BlockState) ct).quantity(amount).build();
}
// Get the cost.
final double perUnitCost = node.getServerBuyPrice();
final int unitCount = amount;
final double overallCost = perUnitCost * unitCount;
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itembuy.summary", Text.of(String.valueOf(amount)), Text.of(created), Text.of(econHelper.getCurrencySymbol(overallCost))));
if (args.hasAny("y")) {
new BuyCallback(src, overallCost, created, unitCount, perUnitCost).accept(src);
} else {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itembuy.clickhere").toBuilder().onClick(TextActions.executeCallback(new BuyCallback(src, overallCost, created, unitCount, perUnitCost))).build());
}
return CommandResult.success();
}
Aggregations