Skip to content

Commit

Permalink
Merge pull request #15 from Alathra/feat/db-testing-commands
Browse files Browse the repository at this point in the history
Feat/db testing commands
  • Loading branch information
ShermansWorld authored Feb 27, 2024
2 parents 10f97df + abf3b46 commit 74588e4
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import io.github.alathra.alathraskills.command.CommandHandler;
import io.github.alathra.alathraskills.config.ConfigHandler;
import io.github.alathra.alathraskills.db.DatabaseHandler;
import io.github.alathra.alathraskills.db.testing.TestGetExerienceCommand;
import io.github.alathra.alathraskills.hooks.VaultHook;
import io.github.alathra.alathraskills.listeners.ListenerHandler;
import io.github.alathra.alathraskills.utility.Logger;

import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.github.alathra.alathraskills.AlathraSkills;
import io.github.alathra.alathraskills.Reloadable;
import io.github.alathra.alathraskills.db.testing.TestGetExerienceCommand;
import io.github.alathra.alathraskills.db.testing.TestSetExerienceCommand;
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkitConfig;

Expand All @@ -25,6 +27,8 @@ public void onEnable() {
CommandAPI.onEnable();

// Register commands here
new TestGetExerienceCommand();
new TestSetExerienceCommand();
new ExampleCommand();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jooq.Record1;
import org.jooq.Record2;
import org.jooq.Result;
import org.jooq.exception.DataAccessException;

import java.nio.ByteBuffer;
import java.sql.Connection;
Expand Down Expand Up @@ -51,6 +52,8 @@ public static void saveSkillCategoryExperience(UUID uuid, int skillCategoryId, F
.onDuplicateKeyUpdate()
.set(PLAYER_SKILLCATEGORYINFO.EXPERIENCE, experience.doubleValue())
.execute();
} catch (DataAccessException e) {
Logger.get().error("SQL Query threw an error!", e);
} catch (SQLException e) {
Logger.get().error("SQL Query threw an error!", e);
}
Expand Down Expand Up @@ -82,6 +85,8 @@ public static void saveSkillInfo(UUID uuid, int skillId) {
skillId
)
.execute();
} catch (DataAccessException e) {
Logger.get().error("SQL Query threw an error!", e);
} catch (SQLException e) {
Logger.get().error("SQL Query threw an error!", e);
}
Expand Down Expand Up @@ -111,9 +116,12 @@ public static Record1<Double> getSkillCategoryExperience(UUID uuid, int skillCat
.where(PLAYER_SKILLCATEGORYINFO.UUID.equal(convertUUIDToBytes(uuid)))
.and(PLAYER_SKILLCATEGORYINFO.SKILLCATEGORYID.equal(skillCategoryId))
.fetchOne();
} catch (SQLException e) {
} catch (DataAccessException e) {
Logger.get().error("SQL Query threw an error!", e);
return null;
} catch (SQLException e) {
Logger.get().error("SQL Query threw an error!", e);
return null;
}
}

Expand All @@ -130,7 +138,11 @@ public static Record1<Double> getSkillCategoryExperience(Player p, int skillCate
*/

public static float getSkillCategoryExperienceFloat(UUID uuid, int skillCategoryId) {
return (float) getSkillCategoryExperience(uuid, skillCategoryId).getValue("EXP");
Record1<Double> returnRecord = getSkillCategoryExperience(uuid, skillCategoryId);
if (returnRecord == null) {
return 0;
}
return ((Double) returnRecord.getValue("EXPERIENCE")).floatValue();
}

public static float getSkillCategoryExperienceFloat(Player p, int skillCategoryId) {
Expand All @@ -156,9 +168,12 @@ public static Boolean doesPlayerHaveSkill(UUID uuid, int skillId) {
.selectFrom(PLAYER_SKILLINFO)
.where(PLAYER_SKILLINFO.UUID.equal(convertUUIDToBytes(uuid)))
.and(PLAYER_SKILLINFO.SKILLID.equal(skillId)));
} catch (SQLException e) {
} catch (DataAccessException e) {
Logger.get().error("SQL Query threw an error!", e);
return null;
} catch (SQLException e) {
Logger.get().error("SQL Query threw an error!", e);
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class JooqContext {
JooqLogger.globalThreshold(Log.Level.ERROR); // Silence JOOQ warnings
}

private static final String TABLE_PREFIX = Cfg.get().getOrDefault("db.prefix", "alathras_skills_"); // The table prefix as grabbed from config
private static final String TABLE_PREFIX = Cfg.get().getOrDefault("db.prefix", "alathra_skills_"); // The table prefix as grabbed from config
private static final Pattern MATCH_ALL_EXCEPT_INFORMATION_SCHEMA = Pattern.compile("^(?!INFORMATION_SCHEMA)(.*?)$");
private static final Pattern MATCH_ALL = Pattern.compile("^(.*?)$");
private static final String REPLACEMENT = "%s$0".formatted(TABLE_PREFIX); //
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.alathra.alathraskills.db.testing;

import org.bukkit.entity.Player;

import com.github.milkdrinkers.colorparser.ColorParser;

import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.arguments.IntegerArgument;
import dev.jorel.commandapi.executors.CommandArguments;
import io.github.alathra.alathraskills.db.DatabaseQueries;

public class TestGetExerienceCommand {

public TestGetExerienceCommand() {
new CommandAPICommand("testGetExperience")
.withArguments(new IntegerArgument("skillCategoryID"))
.withFullDescription("Get Experience For a Given Skill Category.")
.withShortDescription("Get Experience")
.withPermission("example.command")
.executesPlayer(this::runCommand)
.register();
}

private void runCommand(Player player, CommandArguments args) {
if (args.get("skillCategoryID") == null) {
player.sendMessage(
ColorParser.of("Provide a value after the command to indicate skill category.")
.parseLegacy() // Parse legacy color codes
.build()
);
return;
}
// TODO Make Async
float dbReturnValue = DatabaseQueries.getSkillCategoryExperienceFloat(player, (Integer) args.get("skillCategoryID"));
String returnString =
"Player with ID " +
player.getUniqueId() +
" has an experience value of " +
Float.toString(dbReturnValue) +
" in skill category " +
args.get(0) +
".";
player.sendMessage(
ColorParser.of(returnString)
.parseLegacy() // Parse legacy color codes
.build()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.github.alathra.alathraskills.db.testing;

import org.bukkit.entity.Player;

import com.github.milkdrinkers.colorparser.ColorParser;

import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.arguments.FloatArgument;
import dev.jorel.commandapi.arguments.IntegerArgument;
import dev.jorel.commandapi.executors.CommandArguments;
import io.github.alathra.alathraskills.db.DatabaseQueries;

public class TestSetExerienceCommand {

public TestSetExerienceCommand() {
new CommandAPICommand("testSetExperience")
.withArguments(new IntegerArgument("skillCategoryID"), new FloatArgument("Experience"))
.withFullDescription("Set Experience For a Given Skill Category.")
.withShortDescription("Set Experience")
.withPermission("example.command")
.executesPlayer(this::runCommand)
.register();
}

private void runCommand(Player player, CommandArguments args) {
if (args.get("skillCategoryID") == null) {
player.sendMessage(
ColorParser.of("Provide a value after the command to indicate skill category.")
.parseLegacy() // Parse legacy color codes
.build()
);
}
if (args.get("Experience") == null) {
player.sendMessage(
ColorParser.of("Provide a value after the skill category to indicate experience amount.")
.parseLegacy() // Parse legacy color codes
.build()
);
return;
}
// TODO Make Async
DatabaseQueries.saveSkillCategoryExperience(player, (Integer) args.get("skillCategoryID"), (float) args.get("Experience"));
String returnString =
"Player with ID " +
player.getUniqueId() +
" has had an experience value of " +
args.get("Experience") +
" set in skill category " +
args.get("skillCategoryID") +
".";
player.sendMessage(
ColorParser.of(returnString)
.parseLegacy() // Parse legacy color codes
.build()
);
}
}
10 changes: 5 additions & 5 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Database Settings
db:
type: "hsqldb" # Available types: "hsqldb", "h2", "mysql", "mariadb"
prefix: "example_" # The table prefix
type: "mariadb" # Available types: "mysql", "mariadb"
prefix: "alathra_skills_" # The table prefix
host: "127.0.0.1"
port: 3306
database: "database_name"
user: "name"
pass: "123"
database: "alathra"
user: "root"
pass: ""
repair: false

0 comments on commit 74588e4

Please sign in to comment.