use of io.github.nucleuspowered.nucleus.internal.TextFileController in project Nucleus by NucleusPowered.
the class InfoHandler method onReload.
@Override
public void onReload() throws Exception {
// Get the config directory, check to see if "info/" exists.
Path infoDir = plugin.getConfigDirPath().resolve("info");
if (!Files.exists(infoDir)) {
Files.createDirectories(infoDir);
AssetManager am = Sponge.getAssetManager();
// They exist.
am.getAsset(plugin, "info.txt").get().copyToFile(infoDir.resolve("info.txt"));
am.getAsset(plugin, "colors.txt").get().copyToFile(infoDir.resolve("colors.txt"));
am.getAsset(plugin, "links.txt").get().copyToFile(infoDir.resolve("links.txt"));
} else if (!Files.isDirectory(infoDir)) {
throw new IllegalStateException("The file " + infoDir.toAbsolutePath().toString() + " should be a directory.");
}
// Get all txt files.
List<Path> files;
try (Stream<Path> sp = Files.list(infoDir)) {
files = sp.filter(Files::isRegularFile).filter(x -> validFile.matcher(x.getFileName().toString()).matches()).collect(Collectors.toList());
}
// Collect them and put the resultant controllers into a temporary map.
Map<String, TextFileController> mst = Maps.newHashMap();
files.forEach(x -> {
try {
String name = x.getFileName().toString();
name = name.substring(0, name.length() - 4);
if (mst.keySet().stream().anyMatch(name::equalsIgnoreCase)) {
plugin.getLogger().warn(NucleusPlugin.getNucleus().getMessageProvider().getMessageWithFormat("info.load.duplicate", x.getFileName().toString()));
// This is a function, so return is appropriate, not break.
return;
}
mst.put(name, new TextFileController(x, true));
} catch (IOException e) {
e.printStackTrace();
}
});
// All good - replace it all!
infoFiles.clear();
infoFiles.putAll(mst);
}
use of io.github.nucleuspowered.nucleus.internal.TextFileController in project Nucleus by NucleusPowered.
the class InfoCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
Optional<InfoArgument.Result> oir = args.getOne(key);
if (infoConfig.isUseDefaultFile() && !oir.isPresent() && !args.hasAny("l")) {
// Do we have a default?
String def = infoConfig.getDefaultInfoSection();
Optional<TextFileController> list = infoHandler.getSection(def);
if (list.isPresent()) {
oir = Optional.of(new InfoArgument.Result(infoHandler.getInfoSections().stream().filter(def::equalsIgnoreCase).findFirst().get(), list.get()));
}
}
if (oir.isPresent()) {
TextFileController controller = oir.get().text;
Text def = TextSerializers.FORMATTING_CODE.deserialize(oir.get().name);
Text title = plugin.getMessageProvider().getTextMessageWithTextFormat("command.info.title.section", controller.getTitle(src).orElseGet(() -> Text.of(def)));
controller.sendToPlayer(src, title);
return CommandResult.success();
}
// Create a list of pages to load.
Set<String> sections = infoHandler.getInfoSections();
if (sections.isEmpty()) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.info.none"));
}
// Create the text.
List<Text> s = Lists.newArrayList();
sections.forEach(x -> {
Text.Builder tb = Text.builder().append(Text.builder(x).color(TextColors.GREEN).style(TextStyles.ITALIC).onHover(TextActions.showText(plugin.getMessageProvider().getTextMessageWithFormat("command.info.hover", x))).onClick(TextActions.runCommand("/info " + x)).build());
// If there is a title, then add it.
infoHandler.getSection(x).get().getTitle(src).ifPresent(sub -> tb.append(Text.of(TextColors.GOLD, " - ")).append(sub));
s.add(tb.build());
});
Util.getPaginationBuilder(src).contents().header(plugin.getMessageProvider().getTextMessageWithFormat("command.info.header.default")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.info.title.default")).contents(s.stream().sorted(Comparator.comparing(Text::toPlain)).collect(Collectors.toList())).padding(Text.of(TextColors.GOLD, "-")).sendTo(src);
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.internal.TextFileController in project Nucleus by NucleusPowered.
the class NucleusPlugin method reload.
@Override
public synchronized boolean reload() {
try {
this.moduleContainer.reloadSystemConfig();
reloadMessages();
this.commandsConfig.load();
this.itemDataService.load();
this.warmupConfig = null;
CoreConfig coreConfig = this.getInternalServiceManager().getService(CoreConfigAdapter.class).get().getNodeOrDefault();
this.isDebugMode = coreConfig.isDebugmode();
this.isTraceUserCreations = coreConfig.traceUserCreations();
this.savesandloads = coreConfig.isPrintSaveLoad();
for (TextFileController tfc : textFileControllers.values()) {
tfc.load();
}
fireReloadables();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
Aggregations