9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-26 10:09:10 +00:00

Migrate plugin to MineDown/Kyori-adventure

This commit is contained in:
William
2022-09-22 15:00:22 +01:00
parent 2ed7705903
commit 049cd8ecca
16 changed files with 53 additions and 38 deletions

View File

@@ -6,6 +6,7 @@ import dev.dejvokep.boostedyaml.settings.dumper.DumperSettings;
import dev.dejvokep.boostedyaml.settings.general.GeneralSettings;
import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings;
import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.william278.desertwell.Version;
import net.william278.husksync.command.BukkitCommand;
import net.william278.husksync.command.BukkitCommandType;
@@ -67,6 +68,8 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
private Settings settings;
private Locales locales;
private List<Migrator> availableMigrators;
private BukkitAudiences audiences;
private static BukkitHuskSync instance;
/**
@@ -92,6 +95,9 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
this.logger = new BukkitLogger(this.getLogger());
this.resourceReader = new BukkitResourceReader(this);
// Create adventure audience
this.audiences = BukkitAudiences.create(this);
// Load settings and locales
getLoggingAdapter().log(Level.INFO, "Loading plugin configuration settings & locales...");
initialized.set(reload().join());
@@ -131,7 +137,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
getLoggingAdapter().log(Level.INFO, "Successfully established a connection to the database");
} else {
throw new HuskSyncInitializationException("Failed to establish a connection to the database. " +
"Please check the supplied database credentials in the config file");
"Please check the supplied database credentials in the config file");
}
// Prepare redis connection
@@ -142,7 +148,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
getLoggingAdapter().log(Level.INFO, "Successfully established a connection to the Redis server");
} else {
throw new HuskSyncInitializationException("Failed to establish a connection to the Redis server. " +
"Please check the supplied Redis credentials in the config file");
"Please check the supplied Redis credentials in the config file");
}
// Register events
@@ -188,7 +194,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
getLatestVersionIfOutdated().thenAccept(newestVersion ->
newestVersion.ifPresent(newVersion -> getLoggingAdapter().log(Level.WARNING,
"An update is available for HuskSync, v" + newVersion
+ " (Currently running v" + getPluginVersion() + ")")));
+ " (Currently running v" + getPluginVersion() + ")")));
}
} catch (HuskSyncInitializationException exception) {
getLoggingAdapter().log(Level.SEVERE, """
@@ -305,6 +311,16 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
return Version.fromMinecraftVersionString(Bukkit.getBukkitVersion());
}
/**
* Returns the adventure Bukkit audiences
*
* @return The adventure Bukkit audiences
*/
@NotNull
public BukkitAudiences getAudiences() {
return audiences;
}
@Override
public CompletableFuture<Boolean> reload() {
return CompletableFuture.supplyAsync(() -> {

View File

@@ -55,8 +55,9 @@ public class BukkitCommand implements CommandExecutor, TabExecutor {
if (this.command instanceof ConsoleExecutable consoleExecutable) {
consoleExecutable.onConsoleExecute(args);
} else {
plugin.getLocales().getLocale("error_in_game_command_only").
ifPresent(locale -> sender.spigot().sendMessage(locale.toComponent()));
plugin.getLocales().getLocale("error_in_game_command_only")
.ifPresent(locale -> plugin.getAudiences().sender(sender)
.sendMessage(locale.toComponent()));
}
}
return true;

View File

@@ -1,6 +1,8 @@
package net.william278.husksync.player;
import de.themoep.minedown.MineDown;
import de.themoep.minedown.adventure.MineDown;
import de.themoep.minedown.adventure.MineDownParser;
import net.kyori.adventure.audience.Audience;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.william278.husksync.BukkitHuskSync;
@@ -47,10 +49,12 @@ public class BukkitPlayer extends OnlineUser {
PersistentDataType.TAG_CONTAINER};
private final Player player;
private final Audience audience;
private BukkitPlayer(@NotNull Player player) {
super(player.getUniqueId(), player.getName());
this.player = player;
this.audience = BukkitHuskSync.getInstance().getAudiences().player(player);
}
public static BukkitPlayer adapt(@NotNull Player player) {
@@ -565,8 +569,11 @@ public class BukkitPlayer extends OnlineUser {
@Override
public void showMenu(@NotNull ItemEditorMenu menu) {
BukkitSerializer.deserializeItemStackArray(menu.itemData.serializedItems).thenAccept(inventoryContents -> {
//todo show the inventory properly
/*final Inventory inventory = Bukkit.createInventory(player, menu.itemEditorMenuType.slotCount,
BaseComponent.toLegacyText(menu.menuTitle.toComponent()));*/
final Inventory inventory = Bukkit.createInventory(player, menu.itemEditorMenuType.slotCount,
BaseComponent.toLegacyText(menu.menuTitle.toComponent()));
menu.menuTitle.message());
inventory.setContents(inventoryContents);
Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> player.openInventory(inventory));
});
@@ -579,12 +586,16 @@ public class BukkitPlayer extends OnlineUser {
@Override
public void sendActionBar(@NotNull MineDown mineDown) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, mineDown.replace().toComponent());
audience.sendActionBar(mineDown
.disable(MineDownParser.Option.SIMPLE_FORMATTING)
.replace().toComponent());
}
@Override
public void sendMessage(@NotNull MineDown mineDown) {
player.spigot().sendMessage(mineDown.replace().toComponent());
audience.sendMessage(mineDown
.disable(MineDownParser.Option.SIMPLE_FORMATTING)
.replace().toComponent());
}
/**

View File

@@ -1,7 +1,5 @@
package net.william278.husksync.util;
import de.themoep.minedown.MineDown;
import net.md_5.bungee.api.chat.TextComponent;
import org.jetbrains.annotations.NotNull;
import java.util.logging.Level;
@@ -24,11 +22,6 @@ public class BukkitLogger extends Logger {
logger.log(level, message);
}
@Override
public void log(@NotNull Level level, @NotNull MineDown mineDown) {
logger.log(level, TextComponent.toLegacyText(mineDown.toComponent()));
}
@Override
public void info(@NotNull String message) {
logger.info(message);