Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void onEnable() {
discoverer = new ItemDiscoverer(50, Duration.ofMinutes(5), 50, getServer(), getLogger());
BukkitScheduler scheduler = getServer().getScheduler();
executorState = new ExecutorState(databaseExecutor, scheduler.getMainThreadExecutor(this));
gui = new ShopResultsGUI(this, this.replacements, () -> this.settings);
gui = new ShopResultsGUI(this, this.replacements, () -> this.settings, () -> this.messageContainer);
SqlSessionFactory sessionFactory = MariaDatabase.buildSessionFactory(this.databaseSettings);
Supplier<DatabaseSession> sessionSupplier =
() -> new DatabaseSession(sessionFactory,
Expand Down Expand Up @@ -136,6 +136,11 @@ public void onDisable() {
getLogger().info("Plugin disabled");
}

@NotNull
public MessageContainer messages() {
return this.messageContainer;
}

private void registerCommands(@NotNull SqlSessionFactory sessionFactory) {
PluginManager pluginManager = getServer().getPluginManager();
Supplier<DatabaseSession> sessionSupplier = () -> new DatabaseSession(sessionFactory,
Expand All @@ -160,7 +165,7 @@ private void registerCommands(@NotNull SqlSessionFactory sessionFactory) {
this.messageContainer);
List<CommandBean> commands = List.of(
findCommand,
new ResyncCommand(this, resyncTaskFactory),
new ResyncCommand(this, resyncTaskFactory, this.messageContainer),
new ReloadCommand(this)
);
var csdb = Commands.literal("csdb");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.dialog.Dialog;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -64,9 +63,7 @@ private LiteralArgumentBuilder<CommandSourceStack> baseFindCommand() {
}
ItemStack inMainHand = player.getInventory().getItemInMainHand().asOne();
if (inMainHand.isEmpty()) {
player.sendMessage(Component.text(
"You must hold an item in your hand or specify an item code!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor("find.command.need-item"));
return Command.SINGLE_SUCCESS;
}
processCommandWithItem(player, inMainHand);
Expand Down Expand Up @@ -101,22 +98,16 @@ private LiteralArgumentBuilder<CommandSourceStack> buildToggleHologram() {
}
Block block = player.getTargetBlockExact(5);
if (block == null || !Tag.SIGNS.isTagged(block.getType())) {
player.sendMessage(Component.text(
"You must be looking at a shop sign!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor("find.command.sign-target"));
return Command.SINGLE_SUCCESS;
}
Sign sign = (Sign) block.getState(false);
if (!ChestShopSign.isValid(sign)) {
player.sendMessage(Component.text(
"You must be looking at a shop sign!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor("find.command.sign-target"));
return Command.SINGLE_SUCCESS;
}
if (!ChestShopSign.canAccess(player, sign)) {
player.sendMessage(Component.text(
"You do not have access to this shop sign!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor("find.command.sign-no-access"));
return Command.SINGLE_SUCCESS;
}
World world = sign.getWorld();
Expand Down Expand Up @@ -158,18 +149,14 @@ private LiteralArgumentBuilder<CommandSourceStack> buildToggleHologram() {
.whenComplete((success, ex) -> {
if (ex != null) {
ex.printStackTrace();
player.sendMessage(Component.text(
"Internal error occurred!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor("find.command.internal-error"));
return;
}
player.sendMessage(Component.text(
"Visibility toggled to " + visible,
NamedTextColor.AQUA));
player.sendMessage(messages.messageResolving("find.command.visibility-toggled",
Placeholder.unparsed("value", String.valueOf(visible))));
if (!success) {
player.sendMessage(Component.text(
"Failed to update hologram!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor(
"find.command.visibility-toggled-failed"));
}
});
return Command.SINGLE_SUCCESS;
Expand All @@ -187,22 +174,16 @@ private LiteralArgumentBuilder<CommandSourceStack> buildToggleVisibility() {
}
Block block = player.getTargetBlockExact(5);
if (block == null || !Tag.SIGNS.isTagged(block.getType())) {
player.sendMessage(Component.text(
"You must be looking at a shop sign!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor("find.command.sign-target"));
return Command.SINGLE_SUCCESS;
}
Sign sign = (Sign) block.getState(false);
if (!ChestShopSign.isValid(sign)) {
player.sendMessage(Component.text(
"You must be looking at a shop sign!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor("find.command.sign-target"));
return Command.SINGLE_SUCCESS;
}
if (!ChestShopSign.canAccess(player, sign)) {
player.sendMessage(Component.text(
"You do not have access to this shop sign!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor("find.command.sign-no-access"));
return Command.SINGLE_SUCCESS;
}
UUID world = sign.getWorld().getUID();
Expand All @@ -219,14 +200,11 @@ private LiteralArgumentBuilder<CommandSourceStack> buildToggleVisibility() {
.whenComplete((unused, ex) -> {
if (ex != null) {
ex.printStackTrace();
player.sendMessage(Component.text(
"Internal error occurred!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor("find.command.internal-error"));
return;
}
player.sendMessage(Component.text(
"Visibility toggled to " + visible,
NamedTextColor.AQUA));
player.sendMessage(messages.messageResolving("find.command.visibility-toggled",
Placeholder.unparsed("value", String.valueOf(visible))));
});
return Command.SINGLE_SUCCESS;
}));
Expand All @@ -245,14 +223,11 @@ private LiteralArgumentBuilder<CommandSourceStack> buildTogglePreview() {
previewHandler.setVisible(player, visible).whenComplete((unused, ex) -> {
if (ex != null) {
ex.printStackTrace();
player.sendMessage(Component.text(
"Internal error occurred!",
NamedTextColor.RED));
player.sendMessage(messages.messageFor("find.command.internal-error"));
return;
}
player.sendMessage(Component.text(
"Preview visibility toggled to " + visible,
NamedTextColor.AQUA));
player.sendMessage(messages.messageResolving("find.command.preview-toggled",
Placeholder.unparsed("value", String.valueOf(visible))));
});
return Command.SINGLE_SUCCESS;
}));
Expand All @@ -267,8 +242,8 @@ private void processCommandWithItem(@NotNull Player player, @NotNull ItemStack i
);
this.discoverer.discoverCodeFromItemStack(itemStack, code -> {
if (code == null || code.isEmpty()) {
player.sendMessage(Component.text("Unknown item: ", NamedTextColor.RED)
.append(itemStack.effectiveName()));
player.sendMessage(messages.messageResolving("find.unknown-item-display",
Placeholder.component("item", itemStack.effectiveName())));
return;
}
FindState findState = new FindState(
Expand Down Expand Up @@ -297,7 +272,8 @@ private void processCommandWithItemCode(@NotNull Player player, @NotNull String
String normalized = this.shopState.normalizeItemCode(itemCode);
this.discoverer.discoverItemStackFromCode(normalized, item -> {
if (item == null || item.isEmpty()) {
player.sendMessage(Component.text("Unknown item: " + itemCode, NamedTextColor.RED));
player.sendMessage(messages.messageResolving("find.unknown-item-code",
Placeholder.unparsed("code", itemCode)));
return;
}
FindState findState = new FindState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.jetbrains.annotations.NotNull;

public record ReloadCommand(@NotNull ChestshopDatabasePlugin plugin) implements CommandBean.Single {
Expand All @@ -18,19 +16,15 @@ public record ReloadCommand(@NotNull ChestshopDatabasePlugin plugin) implements
.requires(source -> source.getSender().hasPermission("csdb.reload"))
.executes(ctx -> {
Audience audience = ctx.getSource().getSender();
audience.sendMessage(Component.text("Reloading CSDB messages and settings",
NamedTextColor.GREEN));
audience.sendMessage(plugin.messages().messageFor("command.reload.start"));
plugin.reload().whenComplete((success, error) -> {
if (error != null) {
error.printStackTrace();
}
if (!success || error != null) {
audience.sendMessage(Component.text(
"Reload unsuccessful, check console for errors",
NamedTextColor.RED));
audience.sendMessage(plugin.messages().messageFor("command.reload.failure"));
} else {
audience.sendMessage(Component.text("Reload successful",
NamedTextColor.GREEN));
audience.sendMessage(plugin.messages().messageFor("command.reload.success"));
}
});
return Command.SINGLE_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.util.Tick;
import io.github.md5sha256.chestshopdatabase.settings.MessageContainer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
Expand All @@ -20,7 +21,8 @@

public record ResyncCommand(
@NotNull Plugin plugin,
@NotNull ResyncTaskFactory taskFactory
@NotNull ResyncTaskFactory taskFactory,
@NotNull MessageContainer messages
) implements CommandBean.Single {


Expand All @@ -32,29 +34,31 @@ public record ResyncCommand(
.executes(ctx -> {
CommandSender sender = ctx.getSource().getSender();
BukkitScheduler scheduler = plugin.getServer().getScheduler();
sender.sendMessage(Component.text("Resync queued",
NamedTextColor.GREEN));
sender.sendMessage(messages.messageFor("resync.queued"));
int chunksPerInterval = ctx.getArgument("chunksPerTick", Integer.class);
taskFactory.triggerResync(chunksPerInterval, 1).thenAccept(progress -> {
int ticks = Tick.tick().fromDuration(Duration.ofSeconds(30));
BukkitTask bukkitTask = scheduler.runTaskTimer(plugin, () -> {
sender.sendMessage(Component.text(formatProgress(progress),
NamedTextColor.AQUA));
sender.sendMessage(formatProgress(progress));
}, ticks, ticks);
progress.chainOnComplete(() -> {
bukkitTask.cancel();
sender.sendMessage(String.format("Resync complete! (%d)", progress.total()));
sender.sendMessage(messages.messageResolving("resync.complete",
Placeholder.unparsed("total", String.valueOf(progress.total()))));
});
});
return Command.SINGLE_SUCCESS;
}));
}

private String formatProgress(@NotNull TaskProgress progress) {
private Component formatProgress(@NotNull TaskProgress progress) {
int done = progress.completed();
int total = progress.total();
double percent = 100D * done / total;
return String.format("Resync Progress: %d/%d (%.2f%%)", done, total, percent);
double percent = total == 0 ? 0 : 100D * done / total;
return messages.messageResolving("resync.progress",
Placeholder.unparsed("done", String.valueOf(done)),
Placeholder.unparsed("total", String.valueOf(total)),
Placeholder.unparsed("percent", String.format("%.2f", percent)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import io.github.md5sha256.chestshopdatabase.ReplacementRegistry;
import io.github.md5sha256.chestshopdatabase.model.Shop;
import io.github.md5sha256.chestshopdatabase.model.ShopType;
import io.github.md5sha256.chestshopdatabase.settings.MessageContainer;
import io.github.md5sha256.chestshopdatabase.settings.Settings;
import io.github.md5sha256.chestshopdatabase.util.BlockPosition;
import io.github.md5sha256.chestshopdatabase.util.SimpleItemStack;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
Expand All @@ -35,7 +35,8 @@

public record ShopResultsGUI(@NotNull Plugin plugin,
@NotNull ReplacementRegistry replacements,
@NotNull Supplier<Settings> settings) {
@NotNull Supplier<Settings> settings,
@NotNull Supplier<MessageContainer> messages) {


private static String distanceString(Shop shop, @Nullable BlockPosition queryPosition) {
Expand Down Expand Up @@ -164,7 +165,7 @@ public ChestGui createGui(@NotNull Component title,
StaticPane footerPane = new StaticPane(0, 5, 9, 1, Pane.Priority.LOWEST);
ItemStack backItem = ItemStack.of(Material.ARROW);
backItem.editMeta(meta -> {
Component displayName = Component.text("Back", NamedTextColor.RED)
Component displayName = messages.get().messageFor("gui.results.back")
.decoration(TextDecoration.ITALIC, false);
meta.displayName(displayName);
});
Expand All @@ -184,9 +185,9 @@ public ChestGui createGui(@NotNull Component title,
Pane.Priority.HIGH,
mainPane,
this.plugin);
Component nextPageComp = Component.text("Next Page")
Component nextPageComp = messages.get().messageFor("gui.results.next-page")
.decoration(TextDecoration.ITALIC, false);
Component prevPageComp = Component.text("Prev Page")
Component prevPageComp = messages.get().messageFor("gui.results.prev-page")
.decoration(TextDecoration.ITALIC, false);
ItemStack nextButton = ItemStack.of(Material.PAPER);
nextButton.editMeta(meta -> meta.displayName(nextPageComp));
Expand Down
Loading
Loading