Skip to content

Commit

Permalink
chore: updated upstream
Browse files Browse the repository at this point in the history
latest commit hash: d3e824c740ab721026feb4d68bd40714546fbff5
  • Loading branch information
Siroshun09 committed Apr 20, 2024
1 parent a33da28 commit 8b42e1d
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 287 deletions.
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ dependencies {
paperweight.foliaDevBundle("$mcVersion-R0.1-SNAPSHOT")
compileOnly("me.clip:placeholderapi:2.11.5")

implementation("com.github.siroshun09.configapi:configapi-yaml:4.6.4")
implementation("com.github.siroshun09.translationloader:translationloader:2.0.2")
implementation("com.github.siroshun09.configapi:configapi-format-yaml:5.0.0-beta.3") {
exclude("org.yaml", "snakeyaml")
}
implementation("com.github.siroshun09.messages:messages-minimessage:0.6.0")
}

java {
Expand Down
147 changes: 90 additions & 57 deletions src/main/java/net/okocraft/scoreboard/ScoreboardPlugin.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package net.okocraft.scoreboard;

import com.github.siroshun09.configapi.api.util.ResourceUtils;
import com.github.siroshun09.configapi.yaml.YamlConfiguration;
import com.github.siroshun09.translationloader.directory.TranslationDirectory;
import net.kyori.adventure.key.Key;
import com.github.siroshun09.configapi.format.yaml.YamlFormat;
import com.github.siroshun09.messages.api.directory.DirectorySource;
import com.github.siroshun09.messages.api.directory.MessageProcessors;
import com.github.siroshun09.messages.api.source.StringMessageMap;
import com.github.siroshun09.messages.api.util.PropertiesFile;
import com.github.siroshun09.messages.minimessage.localization.MiniMessageLocalization;
import com.github.siroshun09.messages.minimessage.source.MiniMessageSource;
import net.okocraft.scoreboard.command.ScoreboardCommand;
import net.okocraft.scoreboard.config.BoardManager;
import net.okocraft.scoreboard.display.line.LineDisplay;
Expand All @@ -12,15 +15,20 @@
import net.okocraft.scoreboard.external.PlaceholderAPIHooker;
import net.okocraft.scoreboard.listener.PlayerListener;
import net.okocraft.scoreboard.listener.PluginListener;
import net.okocraft.scoreboard.message.Messages;
import net.okocraft.scoreboard.util.PlatformHelper;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.logging.Level;

public class ScoreboardPlugin extends JavaPlugin {
Expand All @@ -31,15 +39,10 @@ public class ScoreboardPlugin extends JavaPlugin {
return Objects.requireNonNull(INSTANCE);
}

private final TranslationDirectory translationDirectory =
TranslationDirectory.newBuilder()
.setKey(Key.key("scoreboard:languages"))
.setDirectory(getDataFolder().toPath().resolve("languages"))
.setDefaultLocale(Locale.ENGLISH)
.onDirectoryCreated(this::saveDefaultLanguages)
.build();
private final BoardManager boardManager = new BoardManager(this);

private BoardManager boardManager;
private boolean boardLoaded;
private MiniMessageLocalization localization;
private DisplayManager displayManager;
private PlayerListener playerListener;
private PluginListener pluginListener;
Expand All @@ -48,21 +51,8 @@ public class ScoreboardPlugin extends JavaPlugin {
public void onLoad() {
INSTANCE = this;

try {
saveDefaultFiles();
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Could not save default files", e);
}

try {
translationDirectory.load();
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Could not load languages", e);
}

loadConfig();
boardManager = new BoardManager(this);
boardManager.reload();
this.boardLoaded = this.reloadSettings(ex -> {
});
}

@Override
Expand All @@ -88,7 +78,9 @@ public void onEnable() {
command.setTabCompleter(impl);
}

PlatformHelper.runAsync(this::showDefaultBoardToOnlinePlayers);
if (this.boardLoaded) {
PlatformHelper.runAsync(this::showDefaultBoardToOnlinePlayers);
}
}

@Override
Expand All @@ -107,32 +99,56 @@ public void onDisable() {
}
}

public void reload() {
public boolean reload(@NotNull Consumer<Throwable> exceptionConsumer) {
displayManager.hideAllBoards();

if (this.reloadSettings(exceptionConsumer)) {
PlatformHelper.runAsync(this::showDefaultBoardToOnlinePlayers);
return true;
} else {
return false;
}
}

private boolean reloadSettings(@NotNull Consumer<Throwable> exceptionConsumer) {
try {
translationDirectory.load();
this.loadConfig();
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Could not load config.yml", e);
exceptionConsumer.accept(e);
return false;
}

try {
this.loadMessages();
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Could not load languages", e);
exceptionConsumer.accept(e);
return false;
}

try {
this.boardManager.reload();
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Could not load boards", e);
exceptionConsumer.accept(e);
return false;
}

loadConfig();
boardManager.reload();
return true;
}

PlatformHelper.runAsync(this::showDefaultBoardToOnlinePlayers);
public @NotNull MiniMessageLocalization getLocalization() {
return this.localization;
}

@NotNull
public BoardManager getBoardManager() {
if (boardManager == null) {
throw new IllegalStateException();
}

return boardManager;
}

public DisplayManager getDisplayManager() {
if (boardManager == null) {
if (displayManager == null) {
throw new IllegalStateException();
}

Expand All @@ -143,31 +159,48 @@ public void printPlaceholderIsAvailable() {
getLogger().info("PlaceholderAPI is available!");
}

private void saveDefaultFiles() throws IOException {
ResourceUtils.copyFromJarIfNotExists(
getFile().toPath(), "config.yml", getDataFolder().toPath().resolve("config.yml")
);
public Path saveResource(String filename) throws IOException {
var filepath = this.getDataFolder().toPath().resolve(filename);
if (!Files.isRegularFile(filepath)) {
try (var input = this.getResource(filename)) {
if (input == null) {
throw new IllegalStateException(filename + " was not found in the jar.");
}
Files.copy(input, filepath);
}
}
return filepath;
}

ResourceUtils.copyFromJarIfNotExists(
getFile().toPath(), "default.yml", getDataFolder().toPath().resolve("default.yml")
);
private void loadConfig() throws IOException {
var config = YamlFormat.DEFAULT.load(this.saveResource("config.yml"));
LineDisplay.globalLengthLimit = Math.max(config.getInteger("max-line-length", 32), 1);
}

private void loadConfig() {
try (var config = YamlConfiguration.create(getDataFolder().toPath().resolve("config.yml"))) {
config.load();
LineDisplay.globalLengthLimit = Math.max(config.getInteger("max-line-length", 32), 1);
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Could not load config.yml", e);
private void loadMessages() throws IOException {
if (this.localization == null) { // on startup
this.localization = new MiniMessageLocalization(MiniMessageSource.create(StringMessageMap.create(Messages.defaultMessages())), Messages::getLocaleFrom);
} else { // on reload
this.localization.clearSources();
}
}

private void saveDefaultLanguages(@NotNull Path directory) throws IOException {
var english = "en.yml";
ResourceUtils.copyFromJarIfNotExists(getFile().toPath(), english, directory.resolve(english));
DirectorySource.forStringMessageMap(this.getDataFolder().toPath().resolve("languages"))
.fileExtension(PropertiesFile.FILE_EXTENSION)
.defaultLocale(Locale.ENGLISH, Locale.JAPANESE)
.messageLoader(PropertiesFile.DEFAULT_LOADER)
.messageProcessor(MessageProcessors.appendMissingStringMessages(this::loadDefaultMessageMap, PropertiesFile.DEFAULT_APPENDER))
.messageProcessor(loaded -> MiniMessageSource.create(loaded.messageSource()))
.load(loaded -> this.localization.addSource(loaded.locale(), loaded.messageSource()));
}

var japanese = "ja_JP.yml";
ResourceUtils.copyFromJarIfNotExists(getFile().toPath(), japanese, directory.resolve(japanese));
private @Nullable Map<String, String> loadDefaultMessageMap(@NotNull Locale locale) throws IOException {
if (locale.equals(Locale.ENGLISH)) {
return Messages.defaultMessages();
} else {
try (var input = this.getResource(locale + ".properties")) {
return input != null ? PropertiesFile.load(input) : null;
}
}
}

private void showDefaultBoardToOnlinePlayers() {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/okocraft/scoreboard/command/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.okocraft.scoreboard.command;

import com.github.siroshun09.messages.minimessage.source.MiniMessageSource;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -39,7 +40,7 @@ public interface Command {
*
* @return the helps
*/
@NotNull Component getHelp();
@NotNull Component getHelp(@NotNull MiniMessageSource msgSrc);

/**
* Executes the command.
Expand All @@ -50,7 +51,7 @@ public interface Command {
* @param sender the executor
* @param args the array of arguments
*/
void onCommand(@NotNull CommandSender sender, @NotNull String[] args);
void onCommand(@NotNull CommandSender sender, @NotNull String[] args, @NotNull MiniMessageSource msgSrc);

/**
* Gets the tab-completion.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package net.okocraft.scoreboard.command;

import com.github.siroshun09.messages.minimessage.localization.MiniMessageLocalization;
import com.github.siroshun09.messages.minimessage.source.MiniMessageSource;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.okocraft.scoreboard.ScoreboardPlugin;
import net.okocraft.scoreboard.command.subcommand.HideCommand;
import net.okocraft.scoreboard.command.subcommand.ReloadCommand;
import net.okocraft.scoreboard.command.subcommand.ShowCommand;
import net.okocraft.scoreboard.message.CommandMessage;
import net.okocraft.scoreboard.message.Messages;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand All @@ -17,17 +21,15 @@
import java.util.Locale;
import java.util.Objects;

import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.format.NamedTextColor.DARK_GRAY;
import static net.kyori.adventure.text.format.NamedTextColor.GOLD;

public class ScoreboardCommand implements CommandExecutor, TabCompleter {

private static final String COMMAND_PERMISSION = "scoreboard.command";

private final MiniMessageLocalization localization;
private final SubCommandHolder subCommandHolder;

public ScoreboardCommand(@NotNull ScoreboardPlugin plugin) {
this.localization = plugin.getLocalization();
subCommandHolder = new SubCommandHolder(
new ShowCommand(plugin.getBoardManager(), plugin.getDisplayManager()),
new HideCommand(plugin.getDisplayManager()),
Expand All @@ -38,29 +40,31 @@ public ScoreboardCommand(@NotNull ScoreboardPlugin plugin) {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String label, @NotNull String[] args) {
var msgSrc = this.localization.findSource(sender);

if (!(sender.hasPermission(COMMAND_PERMISSION))) {
sender.sendMessage(CommandMessage.NO_PERMISSION.apply(COMMAND_PERMISSION));
Messages.NO_PERMISSION.apply(COMMAND_PERMISSION).source(msgSrc).send(sender);
return true;
}

if (args.length == 0 || args[0].equalsIgnoreCase("help")) {
sendHelp(sender);
sendHelp(sender, msgSrc);
return true;
}

var optionalSubCommand = subCommandHolder.search(args[0]);

if (optionalSubCommand.isEmpty()) {
sendHelp(sender);
sendHelp(sender, msgSrc);
return true;
}

var subCommand = optionalSubCommand.get();

if (sender.hasPermission(subCommand.getPermissionNode())) {
subCommand.onCommand(sender, args);
subCommand.onCommand(sender, args, msgSrc);
} else {
sender.sendMessage(CommandMessage.NO_PERMISSION.apply(subCommand.getPermissionNode()));
Messages.NO_PERMISSION.apply(subCommand.getPermissionNode()).source(msgSrc).send(sender);
}

return true;
Expand Down Expand Up @@ -89,16 +93,11 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
.orElse(Collections.emptyList());
}

private void sendHelp(@NotNull CommandSender sender) {
sender.sendMessage(
text("============================== ", DARK_GRAY)
.append(text("Scoreboard Help", GOLD))
.append(text(" ============================== ", DARK_GRAY))
);

subCommandHolder.getSubCommands()
.stream()
.map(net.okocraft.scoreboard.command.Command::getHelp)
.forEach(sender::sendMessage);
private void sendHelp(@NotNull CommandSender sender, @NotNull MiniMessageSource msgSrc) {
Messages.COMMAND_HELP_HEADER.source(msgSrc).send(sender);
sender.sendMessage(Component.join(
JoinConfiguration.newlines(),
((Iterable<Component>) subCommandHolder.getSubCommands().stream().map(cmd -> cmd.getHelp(msgSrc))::iterator)
));
}
}
Loading

0 comments on commit 8b42e1d

Please sign in to comment.