Skip to content

Commit

Permalink
database migration, mukyu mukyu
Browse files Browse the repository at this point in the history
  • Loading branch information
DoggySazHi committed Feb 20, 2024
1 parent 38ef466 commit 6384a82
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/main/java/net/gensokyoreimagined/motoori/Kosuzu.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ public final class Kosuzu extends JavaPlugin {
public final FileConfiguration config = getConfig();
public KosuzuRemembersEverything database;

public static Component HEADER = Component
public static final Component HEADER = Component
.text("[", NamedTextColor.GOLD)
.append(Component.text("Kosuzu", NamedTextColor.YELLOW, TextDecoration.BOLD))
.append(Component.text("] ", NamedTextColor.GOLD));

@Override
public void onEnable() {
config.addDefault("DO-NOT-EDIT-VERSION-UNLESS-YOU-KNOW-WHAT-YOU-ARE-DOING", 0);
config.addDefault("use-deepl-mobile", true);
config.addDefault("deepl-api-url", "https://api-free.deepl.com/v2/translate");
config.addDefault("deepl-api-key", "changeme");
Expand All @@ -52,7 +53,7 @@ public void onEnable() {

var regexDefaults = List.of(
"^<[^>]+> (.*)", // Vanilla
"^[^»]+» (.*)", // Discord
"^[^\\[][^»]+» (.*)", // Discord
"^(?::build:|:dev_server:).+?: (.*)" // Chatty
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,17 @@ public int hashCode() {
return Objects.hash(json);
}
}

public static enum TranslationMode {
OFF(0), ON(1), FORCE(2);
private final int value;

private TranslationMode(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public KosuzuHintsEverything(Kosuzu kosuzu) {
}

if (args.length == 2 && args[0].equalsIgnoreCase("auto") && sender.hasPermission("kosuzu.translate.auto")) {
return List.of("on", "off");
if (sender.hasPermission("kosuzu.translate.auto.force")) {
return List.of("on", "force", "off");
} else {
return List.of("on", "off");
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,25 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

if (args[0].equalsIgnoreCase("auto")) {
setAuto(sender, args);
return true;
}

invalidSubcommand(sender);
return true;
}

private static void invalidSubcommand(@NotNull CommandSender sender) {
sender.sendMessage(
Kosuzu.HEADER.append(Component.text("/kosuzu default <lang>", NamedTextColor.RED))
);
if (sender.hasPermission("kosuzu.translate.auto")) {
sender.sendMessage(
Kosuzu.HEADER.append(Component.text("/kosuzu <default|auto>", NamedTextColor.RED))
);
} else {
sender.sendMessage(
Kosuzu.HEADER.append(Component.text("/kosuzu <default>", NamedTextColor.RED))
);
}
}

private void changeUserLanguage(@NotNull CommandSender sender, @NotNull String[] args) {
Expand All @@ -73,7 +84,9 @@ private void changeUserLanguage(@NotNull CommandSender sender, @NotNull String[]
}

if (args == null || args.length < 2) {
invalidSubcommand(sender);
sender.sendMessage(
Kosuzu.HEADER.append(Component.text("/kosuzu default <language>", NamedTextColor.RED))
);
return;
}

Expand Down Expand Up @@ -108,6 +121,60 @@ private void changeUserLanguage(@NotNull CommandSender sender, @NotNull String[]
}
}

private void setAuto(@NotNull CommandSender sender, @NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage(
Kosuzu.HEADER.append(Component.text("bruh just copy-paste the text into DeepL", NamedTextColor.RED))
);
return;
}

var canForce = sender.hasPermission("kosuzu.translate.auto.force");

if (args.length < 2) {
if (canForce) {
sender.sendMessage(
Kosuzu.HEADER.append(Component.text("/kosuzu auto <on|off|force>", NamedTextColor.RED))
);
} else {
sender.sendMessage(
Kosuzu.HEADER.append(Component.text("/kosuzu auto <on|off>", NamedTextColor.RED))
);
}
return;
}

if (args[1].equalsIgnoreCase("on")) {
database.setUserAutoTranslate(player.getUniqueId(), KosuzuDatabaseModels.TranslationMode.ON);

sender.sendMessage(
Kosuzu.HEADER.append(Component.text(database.getTranslation("auto.on", database.getUserDefaultLanguage(player.getUniqueId())), NamedTextColor.GREEN))
);
} else if (args[1].equalsIgnoreCase("off")) {
database.setUserAutoTranslate(player.getUniqueId(), KosuzuDatabaseModels.TranslationMode.OFF);

sender.sendMessage(
Kosuzu.HEADER.append(Component.text(database.getTranslation("auto.off", database.getUserDefaultLanguage(player.getUniqueId())), NamedTextColor.GREEN))
);
} else if (args[1].equalsIgnoreCase("force") && canForce) {
database.setUserAutoTranslate(player.getUniqueId(), KosuzuDatabaseModels.TranslationMode.FORCE);

sender.sendMessage(
Kosuzu.HEADER.append(Component.text(database.getTranslation("auto.force", database.getUserDefaultLanguage(player.getUniqueId())), NamedTextColor.GREEN))
);
} else {
if (canForce) {
sender.sendMessage(
Kosuzu.HEADER.append(Component.text("/kosuzu auto <on|off|force>", NamedTextColor.RED))
);
} else {
sender.sendMessage(
Kosuzu.HEADER.append(Component.text("/kosuzu auto <on|off>", NamedTextColor.RED))
);
}
}
}

private void translateMessage(@NotNull CommandSender sender, @NotNull String[] args) {
if (args.length < 2) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -168,14 +169,32 @@ private void initializeDatabase(Kosuzu kosuzu) {
if (!requiresInitialization) return;

var initialization = kosuzu.getResource("dbinit.sql");
if (initialization == null) {
loadSQLFile(initialization);

for (int i = config.getInt("DO-NOT-EDIT-VERSION-UNLESS-YOU-KNOW-WHAT-YOU-ARE-DOING", 0); i < 1; i++) {
var update = kosuzu.getResource("migration" + i + ".sql");
if (update == null) {
continue;
}

logger.info("Applying migration " + i + " to database...");
loadSQLFile(update);
config.set("DO-NOT-EDIT-VERSION-UNLESS-YOU-KNOW-WHAT-YOU-ARE-DOING", i + 1);
kosuzu.saveConfig();
}

loadLanguages();
}

private void loadSQLFile(InputStream fileStream) {
if (fileStream == null) {
throw new KosuzuException("Failed to find dbinit.sql! Is the plugin jar corrupted?");
}

String sql;

try {
sql = new String(initialization.readAllBytes());
sql = new String(fileStream.readAllBytes());
} catch (IOException ex) {
throw new KosuzuException("Failed to load dbinit.sql! Is the plugin jar corrupted?", ex);
}
Expand All @@ -192,7 +211,14 @@ private void initializeDatabase(Kosuzu kosuzu) {
statement.execute(query);
}
}
} catch (SQLException e) {
logger.severe("Failed to initialize database! Violating query: " + lastQuery);
throw new KosuzuException(e);
}
}

private void loadLanguages() {
try (var connection = getConnection()) {
// Get all codes from the translations file
var keys = Objects.requireNonNull(translations.getConfigurationSection("language.english")).getKeys(false);

Expand All @@ -205,7 +231,7 @@ private void initializeDatabase(Kosuzu kosuzu) {
}
}
} catch (SQLException e) {
logger.severe("Failed to initialize database! Violating query: " + lastQuery);
logger.severe("Failed to load languages into database! " + e.getMessage());
throw new KosuzuException(e);
}
}
Expand Down Expand Up @@ -310,6 +336,20 @@ public void setUserDefaultLanguage(@NotNull UUID uuid, @NotNull String lang) {
}
}

// Use TranslationMode mode instead of directly using integers
public void setUserAutoTranslate(@NotNull UUID uuid, TranslationMode mode) {
try (var connection = getConnection()) {
try (var statement = connection.prepareStatement("UPDATE `user` SET `use_auto` = ? WHERE `uuid` = ?;")) {
statement.setInt(1, mode.getValue());
statement.setString(2, uuid.toString());
statement.execute();
}
} catch (SQLException e) {
logger.severe("Failed to set user auto translate!");
logger.severe(e.getMessage());
}
}

private Collection<Language> languages;

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/dbinit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ CREATE TABLE IF NOT EXISTS `user_message`
FOREIGN KEY (`message_id`) REFERENCES `message` (`uuid`) ON DELETE CASCADE
);

-- noinspection SqlResolve
CREATE INDEX `user_message_json_msg_idx` ON `user_message` (json_msg(255));
1 change: 1 addition & 0 deletions src/main/resources/migration1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `user` ADD COLUMN `use_auto` TINYINT(1) NOT NULL DEFAULT 0 AFTER `default_language`;

0 comments on commit 6384a82

Please sign in to comment.