Skip to content

Commit

Permalink
Merge pull request #107 from BTEUK/Develop
Browse files Browse the repository at this point in the history
Tweaks the Tutorials Menu and allows implementation via a Tutorials API/Library
  • Loading branch information
george112n authored Jun 24, 2024
2 parents 8fb6f46 + c11ffca commit 16a9c4a
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 73 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<groupId>me.bteuk</groupId>
<artifactId>TeachingTutorials</artifactId>
<version>1.1.0-Beta8</version>
<version>1.1.0-Beta8.1</version>
<packaging>jar</packaging>

<name>TeachingTutorials</name>

<properties>
<java.version>16</java.version>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down Expand Up @@ -121,7 +121,7 @@
<dependency>
<groupId>com.github.SmylerMC</groupId>
<artifactId>terraminusminus</artifactId>
<version>master-ab645ef750-1</version>
<version>e92dc85e64</version>
</dependency>
<dependency>
<groupId>com.onarandombox.multiversecore</groupId>
Expand Down
28 changes: 25 additions & 3 deletions src/main/java/teachingtutorials/TeachingTutorials.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ else if (!learningMenuSlot.equals(menu))
public void run()
{
//Deal with external events in the DB
ArrayList<Event> events = Event.getLatestEvents();
ArrayList<Event> events = Event.getLatestEvents(dbConnection);
int iNumEvents = events.size();
Event event;
User user;
Expand All @@ -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
Expand All @@ -211,7 +228,7 @@ public void run()
}
}
}
}, 0L, 60L);
}, 0L, 40L);

//----------------------------------------
//--------Refreshes virtual blocks--------
Expand Down Expand Up @@ -652,6 +669,11 @@ public static TeachingTutorials getInstance()
return instance;
}

public DBConnection getDBConnection()
{
return dbConnection;
}

public Connection getConnection()
{
return (dbConnection.getConnection());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/teachingtutorials/guis/AdminMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand All @@ -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);
}
});
Expand Down
79 changes: 73 additions & 6 deletions src/main/java/teachingtutorials/guis/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event> 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<Event> getLatestEvents(DBConnection dbConnection)
{
ArrayList<Event> events = new ArrayList<>();

Expand All @@ -37,7 +54,7 @@ public static ArrayList<Event> 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);
Expand All @@ -51,9 +68,10 @@ public static ArrayList<Event> 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++)
Expand Down Expand Up @@ -95,6 +113,11 @@ public static ArrayList<Event> getLatestEvents()
return events;
}

//SQL Updates

/**
* Removes this event from the database
*/
public void remove()
{
String sql;
Expand All @@ -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;
}
}
65 changes: 53 additions & 12 deletions src/main/java/teachingtutorials/guis/LibraryMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -133,27 +134,67 @@ 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++)
{
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");
}
}
}
});
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/teachingtutorials/guis/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/teachingtutorials/listeners/JoinLeaveEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 16a9c4a

Please sign in to comment.