diff --git a/pom.xml b/pom.xml index f82059c..27e4519 100644 --- a/pom.xml +++ b/pom.xml @@ -6,13 +6,13 @@ me.bteuk TeachingTutorials - 1.1.0-Beta8 + 1.1.0-Beta8.1 jar TeachingTutorials - 16 + 17 UTF-8 @@ -121,7 +121,7 @@ com.github.SmylerMC terraminusminus - master-ab645ef750-1 + e92dc85e64 com.onarandombox.multiversecore diff --git a/src/main/java/teachingtutorials/TeachingTutorials.java b/src/main/java/teachingtutorials/TeachingTutorials.java index 393de4d..5869dcb 100644 --- a/src/main/java/teachingtutorials/TeachingTutorials.java +++ b/src/main/java/teachingtutorials/TeachingTutorials.java @@ -186,7 +186,7 @@ else if (!learningMenuSlot.equals(menu)) public void run() { //Deal with external events in the DB - ArrayList events = Event.getLatestEvents(); + ArrayList events = Event.getLatestEvents(dbConnection); int iNumEvents = events.size(); Event event; User user; @@ -201,7 +201,24 @@ public void run() user = User.identifyUser(instance, event.player); if (user != null) { - MainMenu.performEvent(event.eventType, user, instance); + //Starts a lesson if the event was a library event type + if (event.eventType.equals(EventType.LIBRARY)) + { + Tutorial specifiedTutorial = new Tutorial(); + specifiedTutorial.setTutorialID(event.iData); + specifiedTutorial.fetchByTutorialID(dbConnection); + + //Creates a Lesson object + Lesson newLesson = new Lesson(user, instance, specifiedTutorial); + + //Launches them into the new lesson + newLesson.startLesson(); + } + + else + { + MainMenu.performEvent(event.eventType, user, instance); + } //We only want the event to be removed if the player was on the server and the event took place //There may be a delay/lag period where the event is in the DB but the user isn't yet on the server @@ -211,7 +228,7 @@ public void run() } } } - }, 0L, 60L); + }, 0L, 40L); //---------------------------------------- //--------Refreshes virtual blocks-------- @@ -652,6 +669,11 @@ public static TeachingTutorials getInstance() return instance; } + public DBConnection getDBConnection() + { + return dbConnection; + } + public Connection getConnection() { return (dbConnection.getConnection()); diff --git a/src/main/java/teachingtutorials/guis/AdminMenu.java b/src/main/java/teachingtutorials/guis/AdminMenu.java index 14cf906..76559cc 100644 --- a/src/main/java/teachingtutorials/guis/AdminMenu.java +++ b/src/main/java/teachingtutorials/guis/AdminMenu.java @@ -56,7 +56,7 @@ public void rightClick(User u) { @Override public void leftClick(User u) { delete(); - u.mainGui = new CompulsoryTutorialMenu(plugin, u, Tutorial.fetchAll(true, false)); + u.mainGui = new CompulsoryTutorialMenu(plugin, u, Tutorial.fetchAll(true, false, TeachingTutorials.getInstance().getDBConnection())); u.mainGui.open(u); } }); @@ -77,7 +77,7 @@ public void rightClick(User u) { @Override public void leftClick(User u) { delete(); - u.mainGui = new CreatorTutorialsMenu(plugin, u, Tutorial.fetchAllByCreator(u.player.getUniqueId())); + u.mainGui = new CreatorTutorialsMenu(plugin, u, Tutorial.fetchAllByCreator(u.player.getUniqueId(), TeachingTutorials.getInstance().getDBConnection())); u.mainGui.open(u); } }); diff --git a/src/main/java/teachingtutorials/guis/Event.java b/src/main/java/teachingtutorials/guis/Event.java index 34290ae..c7bbc3a 100644 --- a/src/main/java/teachingtutorials/guis/Event.java +++ b/src/main/java/teachingtutorials/guis/Event.java @@ -4,26 +4,43 @@ import org.bukkit.ChatColor; import org.bukkit.entity.Player; import teachingtutorials.TeachingTutorials; +import teachingtutorials.utils.DBConnection; import java.sql.*; import java.util.ArrayList; import java.util.UUID; +/** + * Handles tutorial Events. Data fields refer to those of the events table of the database + */ public class Event { public Player player; public EventType eventType; public Timestamp timestamp; - - public Event(Player player, EventType eventType, Timestamp timestamp) + public int iData; + + /** + * Constructs a new event + * @param player The player object for the event + * @param eventType The event type + * @param timestamp The time that the event was added to the DB + * @param iData The data of the event - The tutorial ID of the tutorial the event includes a reference to + */ + public Event(Player player, EventType eventType, Timestamp timestamp, int iData) { this.player = player; this.eventType = eventType; this.timestamp = timestamp; + this.iData = iData; } - //SQL Fetches - public static ArrayList getLatestEvents() + /** + * Gets a list of the current events in the events table + * @param dbConnection The database connection object + * @return A list of events + */ + public static ArrayList getLatestEvents(DBConnection dbConnection) { ArrayList events = new ArrayList<>(); @@ -37,7 +54,7 @@ public static ArrayList getLatestEvents() { //Compiles the command to fetch steps sql = "Select * FROM Events"; - SQL = TeachingTutorials.getInstance().getConnection().createStatement(); + SQL = dbConnection.getConnection().createStatement(); //Executes the query resultSet = SQL.executeQuery(sql); @@ -51,9 +68,10 @@ public static ArrayList getLatestEvents() continue; EventType eventType = EventType.valueOf(resultSet.getString("EventName")); Timestamp timestamp = resultSet.getTimestamp("Timestamp"); + int iData = resultSet.getInt("Data"); //Creates an event object to store the details - Event event = new Event(player, eventType, timestamp); + Event event = new Event(player, eventType, timestamp, iData); //Go through entire event list and check for any duplicate players and sort them for (int i = 0 ; i < events.size() ; i++) @@ -95,6 +113,11 @@ public static ArrayList getLatestEvents() return events; } + //SQL Updates + + /** + * Removes this event from the database + */ public void remove() { String sql; @@ -120,5 +143,49 @@ public void remove() Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[TeachingTutorials] - SQL - Non-SQL Error deleting event"); e.printStackTrace(); } + + } + + /** + * Adds an event to the events table + * @param eventType The event that is to take place + * @param userUUID The UUID of the player undergoing to event + * @param iData Any data - refers to the TutorialID of any tutorials the event refers to + * @param dbConnection The database connection object + * @return True if the event was successfully added to the DB, false if not + */ + public static boolean addEvent(EventType eventType, UUID userUUID, int iData, DBConnection dbConnection) + { + String sql; + Statement SQL = null; + + int iCount = 0; + + try + { + SQL = dbConnection.getConnection().createStatement(); + sql = "INSERT INTO `Events` (`UUID`,`EventName`,`Data`) VALUES('" +userUUID +"', '"+eventType.toString()+"', "+iData +")"; + Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"" +sql); + iCount = SQL.executeUpdate(sql); + } + catch (SQLException se) + { + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[TeachingTutorials] - SQL - SQL Error adding event"); + se.printStackTrace(); + return false; + } + catch (Exception e) + { + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[TeachingTutorials] - SQL - Non-SQL Error adding event"); + e.printStackTrace(); + return false; + } + if (iCount != 1) + { + Bukkit.getConsoleSender().sendMessage(ChatColor.RED +"[TeachingTutorials] - SQL - Update failed, count not equal to 1"); + return false; + } + else + return true; } } diff --git a/src/main/java/teachingtutorials/guis/LibraryMenu.java b/src/main/java/teachingtutorials/guis/LibraryMenu.java index 24ed27d..e659211 100644 --- a/src/main/java/teachingtutorials/guis/LibraryMenu.java +++ b/src/main/java/teachingtutorials/guis/LibraryMenu.java @@ -5,6 +5,7 @@ import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.TextDecoration; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -38,7 +39,7 @@ public LibraryMenu(TeachingTutorials plugin, User user, Tutorial[] tutorials) setActions(); } - private static Inventory getGUI (Tutorial[] tutorials) + public static Inventory getGUI (Tutorial[] tutorials) { //Declare variables int i; @@ -133,6 +134,22 @@ public void leftClick(User u) { } }); + //Initiates the current tutorial object + Tutorial currentTutorial = new Tutorial(); + + if (user.bInLesson) + { + //Get current lesson's tutorial ID and sets up the tutorial object from this + int iTutorialIDCurrentLesson = Lesson.getTutorialOfCurrentLessonOfPlayer(user.player.getUniqueId(), TeachingTutorials.getInstance().getDBConnection()); + if (iTutorialIDCurrentLesson == -1) + { + Bukkit.getConsoleSender().sendMessage(ChatColor.RED +"An error occurred. Player is in lesson but has no lesson in the database"); + } + Bukkit.getConsoleSender().sendMessage("Current TutorialID: "+iTutorialIDCurrentLesson); + currentTutorial.setTutorialID(iTutorialIDCurrentLesson); + currentTutorial.fetchByTutorialID(TeachingTutorials.getInstance().getDBConnection()); + } + //Inv slot 0 = the first one //Adds the actions of each slot for (i = 0 ; i < tutorials.length ; i++) @@ -140,20 +157,44 @@ public void leftClick(User u) { int iSlot = i; setAction(iSlot, new Gui.guiAction() { @Override - public void rightClick(User u) { - leftClick(u); + public void rightClick(User user) { + leftClick(user); } @Override - public void leftClick(User u) - { - //Creates a NewLocation object - Lesson newLesson = new Lesson(user, plugin, tutorials[iSlot]); - - //Launches them into the new location adding process - if (newLesson.startLesson()) - delete(); - user.mainGui = null; + public void leftClick(User user) { + Bukkit.getConsoleSender().sendMessage("Current TutorialID: "+currentTutorial.getTutorialID()); + Bukkit.getConsoleSender().sendMessage("TutorialID of slot: " +tutorials[iSlot].getTutorialID()); + boolean startTheLesson = false; + + if (user.bInLesson) + { + if (currentTutorial.getTutorialID() != tutorials[iSlot].getTutorialID()) + user.player.sendMessage(ChatColor.RED +"You cannot start a new tutorial before you finish your current one"); + else + startTheLesson = true; + } + else + { + startTheLesson = true; + } + + if(startTheLesson) + { + //Creates a Lesson object + Lesson newLesson = new Lesson(user, plugin, tutorials[iSlot]); + + //Launches them into the new lesson + if (newLesson.startLesson()) + { + delete(); + user.mainGui = null; + } + else + { + user.player.sendMessage(ChatColor.RED +"A problem occurred, please let staff know"); + } + } } }); } diff --git a/src/main/java/teachingtutorials/guis/MainMenu.java b/src/main/java/teachingtutorials/guis/MainMenu.java index a6a8768..b07d6ac 100644 --- a/src/main/java/teachingtutorials/guis/MainMenu.java +++ b/src/main/java/teachingtutorials/guis/MainMenu.java @@ -43,7 +43,7 @@ private void createGui() //Get compulsory tutorial ID int iCompulsoryTutorialID; - Tutorial[] compulsoryTutorials = Tutorial.fetchAll(true, true); + Tutorial[] compulsoryTutorials = Tutorial.fetchAll(true, true, TeachingTutorials.getInstance().getDBConnection()); if (compulsoryTutorials.length == 0) iCompulsoryTutorialID = -1; else @@ -54,13 +54,13 @@ private void createGui() if (user.bInLesson) { //Get current tutorial ID and sets up the tutorial object from this - int iTutorialIDCurrentLesson = Lesson.getTutorialOfCurrentLessonOfPlayer(user.player.getUniqueId()); + int iTutorialIDCurrentLesson = Lesson.getTutorialOfCurrentLessonOfPlayer(user.player.getUniqueId(), TeachingTutorials.getInstance().getDBConnection()); if (iTutorialIDCurrentLesson == -1) { Bukkit.getConsoleSender().sendMessage(ChatColor.RED +"An error occurred. Player is in lesson but has no lesson in the database"); } currentTutorial.setTutorialID(iTutorialIDCurrentLesson); - currentTutorial.fetchByTutorialID(); + currentTutorial.fetchByTutorialID(TeachingTutorials.getInstance().getDBConnection()); //Sets up the menu icon with the name of the current tutorial continueLearning_CompulsoryComplete = Utils.createItem(Material.WRITABLE_BOOK, 1, @@ -71,7 +71,7 @@ private void createGui() else { //Sets up the menu icon with the new tutorial's name - Tutorial nextTutorial = Lesson.decideTutorial(user); + Tutorial nextTutorial = Lesson.decideTutorial(user, TeachingTutorials.getInstance().getDBConnection()); continueLearning_CompulsoryComplete = Utils.createItem(Material.WRITABLE_BOOK, 1, TutorialGUIUtils.optionTitle("Start a new Tutorial:"), TutorialGUIUtils.optionLore(nextTutorial.szTutorialName)); @@ -352,9 +352,13 @@ public static boolean performEvent(EventType event, User user, TeachingTutorials user.mainGui.open(user); return true; } + //Here I use the library event as the action arising from clicking the library + //If it is an externally added event via the database then this method is not called + // and instead something different happens, because we use that event for when + // a player clicks a tutorial on the tutorial library to start case LIBRARY -> { - user.mainGui = new LibraryMenu(plugin, user, Tutorial.getInUseTutorialsWithLocations()); + user.mainGui = new LibraryMenu(plugin, user, Tutorial.getInUseTutorialsWithLocations(TeachingTutorials.getInstance().getDBConnection())); user.mainGui.open(user); return true; } diff --git a/src/main/java/teachingtutorials/listeners/JoinLeaveEvent.java b/src/main/java/teachingtutorials/listeners/JoinLeaveEvent.java index 8f2bdc5..8dbbc79 100644 --- a/src/main/java/teachingtutorials/listeners/JoinLeaveEvent.java +++ b/src/main/java/teachingtutorials/listeners/JoinLeaveEvent.java @@ -29,8 +29,8 @@ public JoinLeaveEvent(TeachingTutorials plugin) public void playerJoin(PlayerJoinEvent event) { User user = new User(event.getPlayer()); - user.fetchDetailsByUUID(); - user.calculateRatings(); + user.fetchDetailsByUUID(TeachingTutorials.getInstance().getDBConnection()); + user.calculateRatings(TeachingTutorials.getInstance().getDBConnection()); user.refreshScoreboard(); //Adds player to the main list of players diff --git a/src/main/java/teachingtutorials/tutorials/Lesson.java b/src/main/java/teachingtutorials/tutorials/Lesson.java index 81c5c97..b3c4dff 100644 --- a/src/main/java/teachingtutorials/tutorials/Lesson.java +++ b/src/main/java/teachingtutorials/tutorials/Lesson.java @@ -9,6 +9,7 @@ import teachingtutorials.TeachingTutorials; import teachingtutorials.TutorialPlaythrough; import teachingtutorials.listeners.Falling; +import teachingtutorials.utils.DBConnection; import teachingtutorials.utils.Display; import teachingtutorials.utils.Mode; import teachingtutorials.utils.User; @@ -204,7 +205,7 @@ else if (bTutorialDetailsAlreadyEntered) else //Find an appropriate tutorial and sets the ID to that { //Decides the tutorial - this.tutorial = decideTutorial(creatorOrStudent); + this.tutorial = decideTutorial(creatorOrStudent, TeachingTutorials.getInstance().getDBConnection()); if (this.tutorial == null) { @@ -305,8 +306,12 @@ private boolean fetchCompulsoryID() } } - //Fetches all tutorials that are marked as "In Use" and using the student's ratings, will decide on the best tutorial to make them do - public static Tutorial decideTutorial(User creatorOrStudent) + /** + * Fetches all tutorials that are marked as "In Use" and using the student's ratings, will decide on the best tutorial to make them do + * @param creatorOrStudent + * @return A Tutorial object holding the information of the tutorial they should complete next + */ + public static Tutorial decideTutorial(User creatorOrStudent, DBConnection dbConnection) { //Prevalence of each category in each tutorial needed @@ -382,7 +387,7 @@ public static Tutorial decideTutorial(User creatorOrStudent) //----------------------------------------------------------------- Tutorial[] tutorials; - tutorials = Tutorial.fetchAll(true, false); + tutorials = Tutorial.fetchAll(true, false, dbConnection); //---------------------------------------------------------------- @@ -417,18 +422,23 @@ public static Tutorial decideTutorial(User creatorOrStudent) } } - //Gets a list of all of the stages of the specified tutorial and loads each with the relevant data + /** + * Gets the list of all of the stages of the specified tutorial and loads each with the relevant data + */ private void fetchStages() { //List is in order of stage 1 1st stages = Stage.fetchStagesByTutorialID(creatorOrStudent.player, plugin, this); } - //Selects the location of a specific tutorial randomly + /** + * Selects the location of a specific tutorial randomly + * @return + */ private boolean selectLocation() { int[] iLocationIDs; - iLocationIDs = Location.getAllLocationIDsForTutorial(this.tutorial.getTutorialID()); + iLocationIDs = Location.getAllLocationIDsForTutorial(this.tutorial.getTutorialID(), TeachingTutorials.getInstance().getDBConnection()); //Checks to see if any locations were found if (iLocationIDs.length == 0) @@ -498,7 +508,7 @@ protected void endPlaythrough() //Scoring not to be included in first release. //And trigger the scoreboard to refresh - creatorOrStudent.calculateRatings(); + creatorOrStudent.calculateRatings(TeachingTutorials.getInstance().getDBConnection()); Bukkit.getScheduler().runTask(plugin, new Runnable() { @Override @@ -728,7 +738,7 @@ public boolean fetchCurrentFromUUID(boolean bResetProgress) { this.iLessonID = resultSet.getInt("LessonID"); this.tutorial.setTutorialID(resultSet.getInt("TutorialID")); - this.tutorial.fetchByTutorialID(); + this.tutorial.fetchByTutorialID(TeachingTutorials.getInstance().getDBConnection()); if (bResetProgress) { this.iStageIndex = 1; @@ -763,7 +773,7 @@ public boolean fetchCurrentFromUUID(boolean bResetProgress) * @param playerUUID * @return The id of the tutorial of the current lesson a player is in, or -1 if no lesson */ - public static int getTutorialOfCurrentLessonOfPlayer(UUID playerUUID) + public static int getTutorialOfCurrentLessonOfPlayer(UUID playerUUID, DBConnection dbConnection) { int iTutorialID; @@ -774,7 +784,7 @@ public static int getTutorialOfCurrentLessonOfPlayer(UUID playerUUID) try { sql = "SELECT `TutorialID` FROM `Lessons` WHERE `UUID` = '" +playerUUID +"' AND `Finished` = 0"; - SQL = TeachingTutorials.getInstance().getConnection().createStatement(); + SQL = dbConnection.getConnection().createStatement(); //Executes the query resultSet = SQL.executeQuery(sql); diff --git a/src/main/java/teachingtutorials/tutorials/Location.java b/src/main/java/teachingtutorials/tutorials/Location.java index fbbcbd0..9a44b38 100644 --- a/src/main/java/teachingtutorials/tutorials/Location.java +++ b/src/main/java/teachingtutorials/tutorials/Location.java @@ -4,6 +4,7 @@ import org.bukkit.ChatColor; import org.bukkit.World; import teachingtutorials.TeachingTutorials; +import teachingtutorials.utils.DBConnection; import java.sql.ResultSet; import java.sql.SQLException; @@ -103,7 +104,7 @@ private void fetchDetailsByLocationID() } } - public static int[] getAllLocationIDsForTutorial(int iTutorialID) + public static int[] getAllLocationIDsForTutorial(int iTutorialID, DBConnection dbConnection) { String sql; Statement SQL = null; @@ -117,7 +118,7 @@ public static int[] getAllLocationIDsForTutorial(int iTutorialID) //Compiles the command to fetch all the locations for the tutorial sql = "SELECT * FROM `Locations` WHERE `TutorialID` = " +iTutorialID; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +sql); - SQL = TeachingTutorials.getInstance().getConnection().createStatement(); + SQL = dbConnection.getConnection().createStatement(); //Executes the query resultSet = SQL.executeQuery(sql); diff --git a/src/main/java/teachingtutorials/tutorials/Tutorial.java b/src/main/java/teachingtutorials/tutorials/Tutorial.java index 17b7ee4..43c3d8a 100644 --- a/src/main/java/teachingtutorials/tutorials/Tutorial.java +++ b/src/main/java/teachingtutorials/tutorials/Tutorial.java @@ -3,6 +3,8 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import teachingtutorials.TeachingTutorials; +import teachingtutorials.utils.DBConnection; + import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -56,7 +58,7 @@ public void setTutorialID(int iTutorialID) //--------------------------------------------------- //Fetches all tutorials in the DB - public static Tutorial[] fetchAll(boolean bInUseOnly, boolean bCompulsoryOnly) + public static Tutorial[] fetchAll(boolean bInUseOnly, boolean bCompulsoryOnly, DBConnection dbConnection) { //Declare variables Tutorial[] tutorials; @@ -78,7 +80,7 @@ else if (bCompulsoryOnly) sql = "SELECT * FROM `Tutorials` WHERE `Tutorials`.`Compulsory` = 1"; else sql = "SELECT * FROM `Tutorials`"; - SQL = TeachingTutorials.getInstance().getConnection().createStatement(); + SQL = dbConnection.getConnection().createStatement(); //Executes the query resultSet = SQL.executeQuery(sql); @@ -115,7 +117,7 @@ else if (bCompulsoryOnly) else sql = "SELECT * FROM `Tutorials`,`CategoryPoints` WHERE `Tutorials`.`TutorialID` = `CategoryPoints`.`TutorialID`"; - SQL = TeachingTutorials.getInstance().getConnection().createStatement(); + SQL = dbConnection.getConnection().createStatement(); //Executes the query resultSet = SQL.executeQuery(sql); @@ -169,14 +171,14 @@ else if (bCompulsoryOnly) * * @return All in use tutorials which have at least one location */ - public static Tutorial[] getInUseTutorialsWithLocations() + public static Tutorial[] getInUseTutorialsWithLocations(DBConnection dbConnection) { int iAvailableTutorials; int iNumInUseTutorials; int i; //Fetches all in use tutorials - Tutorial[] allInUseTutorials = Tutorial.fetchAll(true, false); + Tutorial[] allInUseTutorials = Tutorial.fetchAll(true, false, dbConnection); iNumInUseTutorials = allInUseTutorials.length; //Boolean array storing whether each tutorial has at least one location @@ -186,7 +188,8 @@ public static Tutorial[] getInUseTutorialsWithLocations() iAvailableTutorials = 0; for (i = 0 ; i < iNumInUseTutorials ; i++) { - tutorialHasLocation[i] = (Location.getAllLocationIDsForTutorial(allInUseTutorials[i].getTutorialID()).length != 0); + //Determines whether tutorial i has any locations + tutorialHasLocation[i] = (Location.getAllLocationIDsForTutorial(allInUseTutorials[i].getTutorialID(), dbConnection).length != 0); if (tutorialHasLocation[i]) { @@ -211,7 +214,7 @@ public static Tutorial[] getInUseTutorialsWithLocations() } //Fetches all tutorials created by a user - public static Tutorial[] fetchAllByCreator(UUID uuid) + public static Tutorial[] fetchAllByCreator(UUID uuid, DBConnection dbConnection) { //Declare variables @@ -229,7 +232,7 @@ public static Tutorial[] fetchAllByCreator(UUID uuid) //Compiles the command to fetch tutorials sql = "SELECT * FROM `Tutorials` WHERE `Tutorials`.`Author` = '" +uuid +"'"; - SQL = TeachingTutorials.getInstance().getConnection().createStatement(); + SQL = dbConnection.getConnection().createStatement(); //Executes the query resultSet = SQL.executeQuery(sql); @@ -255,7 +258,7 @@ public static Tutorial[] fetchAllByCreator(UUID uuid) //Compiles the command to fetch category difficulties sql = "SELECT * FROM `Tutorials`,`CategoryPoints` WHERE `Tutorials`.`TutorialID` = `CategoryPoints`.`TutorialID` AND `Tutorials`.`Author` = '"+uuid+"'"; - SQL = TeachingTutorials.getInstance().getConnection().createStatement(); + SQL = dbConnection.getConnection().createStatement(); //Executes the query resultSet = SQL.executeQuery(sql); @@ -308,7 +311,7 @@ public static Tutorial[] fetchAllByCreator(UUID uuid) } //Fetches the details of a tutorial by the tutorial ID - public boolean fetchByTutorialID() + public boolean fetchByTutorialID(DBConnection dbConnection) { String sql; Statement SQL = null; @@ -319,7 +322,7 @@ public boolean fetchByTutorialID() //Compiles the command to fetch the tutorial sql = "SELECT * FROM `Tutorials` WHERE `Tutorials`.`TutorialID` = " +this.iTutorialID; - SQL = TeachingTutorials.getInstance().getConnection().createStatement(); + SQL = dbConnection.getConnection().createStatement(); //Executes the query resultSet = SQL.executeQuery(sql); @@ -407,7 +410,7 @@ public boolean toggleInUse() { szSql = "UPDATE `Tutorials` SET `InUse` = 0 WHERE `TutorialID` = "+ this.iTutorialID; } - else if (Location.getAllLocationIDsForTutorial(this.iTutorialID).length > 0) + else if (Location.getAllLocationIDsForTutorial(this.iTutorialID, TeachingTutorials.getInstance().getDBConnection()).length > 0) { szSql = "UPDATE `Tutorials` SET `InUse` = 1 WHERE `TutorialID` = "+ this.iTutorialID; } diff --git a/src/main/java/teachingtutorials/utils/DBConnection.java b/src/main/java/teachingtutorials/utils/DBConnection.java index 118dd25..ecee40f 100644 --- a/src/main/java/teachingtutorials/utils/DBConnection.java +++ b/src/main/java/teachingtutorials/utils/DBConnection.java @@ -61,6 +61,20 @@ public void mysqlSetup(TeachingTutorials plugin) + this.PORT + "/" + this.Database + "?&useSSL=false&"; } + + public void externalMySQLSetup(String szHost, int iPort, String szDatabaseName, String szUsername, String szPassword) + { + this.HOST = szHost; + this.PORT = iPort; + this.Database = szDatabaseName; + this.USER = szUsername; + this.PASSWORD = szPassword; + + this.DB_CON = "jdbc:mysql://" + this.HOST + ":" + + this.PORT + "/" + this.Database + "?&useSSL=false&"; + } + + public boolean connect() { Bukkit.getConsoleSender().sendMessage("Username: "+this.USER); diff --git a/src/main/java/teachingtutorials/utils/User.java b/src/main/java/teachingtutorials/utils/User.java index 3f8619a..3145f02 100644 --- a/src/main/java/teachingtutorials/utils/User.java +++ b/src/main/java/teachingtutorials/utils/User.java @@ -61,17 +61,17 @@ public Tutorial[] getAllTutorials() } //Recalculates all ratings - public void calculateRatings() + public void calculateRatings(DBConnection dbConnection) { - iScoreTpll = calculateRating(Category.tpll); - iScoreWE = calculateRating(Category.worldedit); - iScoreTerraforming = calculateRating(Category.terraforming); - iScoreColouring = calculateRating(Category.colouring); - iScoreDetailing = calculateRating(Category.detail); + iScoreTpll = calculateRating(Category.tpll, dbConnection); + iScoreWE = calculateRating(Category.worldedit, dbConnection); + iScoreTerraforming = calculateRating(Category.terraforming, dbConnection); + iScoreColouring = calculateRating(Category.colouring, dbConnection); + iScoreDetailing = calculateRating(Category.detail, dbConnection); } //Uses scores from previous lessons to calculate a rating for a player in a category - private int calculateRating(Category category) + private int calculateRating(Category category, DBConnection dbConnection) { //Declare variables int iTotalScore = 0; @@ -88,7 +88,7 @@ private int calculateRating(Category category) "AND `Scores`.`Category` = '" + category.toString() + "' "+ "AND `Lessons`.`LessonID` = `Scores`.`LessonID` " + "ORDER BY `Scores`.`LessonID` DESC"; - SQL = TeachingTutorials.getInstance().getConnection().createStatement(); + SQL = dbConnection.getConnection().createStatement(); //Executes the update and returns the amount of records updated resultSet = SQL.executeQuery(sql); @@ -292,7 +292,7 @@ public void run() //--------------------------------------------------- //Fetches all of the information about a user in the DB, and adds them to the DB if they are not in it - public void fetchDetailsByUUID() + public void fetchDetailsByUUID(DBConnection dbConnection) { String sql; Statement SQL = null; @@ -303,7 +303,7 @@ public void fetchDetailsByUUID() //Compiles the command to select all data about the user sql = "SELECT * FROM `Players` WHERE `UUID` = '"+player.getUniqueId()+"'"; System.out.println(sql); - SQL = TeachingTutorials.getInstance().getConnection().createStatement(); + SQL = dbConnection.getConnection().createStatement(); //Executes the update and returns the amount of records updated resultSet = SQL.executeQuery(sql); @@ -333,10 +333,10 @@ public void fetchDetailsByUUID() } } - //Fetches all tutorials made by a user - public void fetchAllTutorials() + //Fetches all tutorials made by the user + public void fetchAllTutorials(DBConnection dbConnection) { - this.allTutorials = Tutorial.fetchAllByCreator(this.player.getUniqueId()); + this.allTutorials = Tutorial.fetchAllByCreator(this.player.getUniqueId(), dbConnection); } //--------------------------------------------------- diff --git a/src/main/resources/TutorialsDDL.sql b/src/main/resources/TutorialsDDL.sql index 437f951..88fb699 100644 --- a/src/main/resources/TutorialsDDL.sql +++ b/src/main/resources/TutorialsDDL.sql @@ -49,8 +49,9 @@ DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `TeachingTutorials`.`Events` ( `UUID` CHAR(36) NOT NULL, - `EventName` ENUM('COMPULSORY', 'CONTINUE', 'ADMIN_MENU') NOT NULL, - `Timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) + `EventName` ENUM('COMPULSORY', 'CONTINUE', 'ADMIN_MENU', 'LIBRARY') NOT NULL, + `Timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `Data` INT) ENGINE = InnoDB DEFAULT CHARACTER SET = latin1;