From e184847b9febcb57e016e87de63530442966525d Mon Sep 17 00:00:00 2001 From: george112n Date: Mon, 11 Dec 2023 23:02:20 +0000 Subject: [PATCH 1/5] Adds the video link feature. Players can now do /link or /video and get a link to a video walkthrough. --- pom.xml | 2 +- .../locationcreatemenus/StepEditorMenu.java | 87 ++++++++++++++++--- .../listeners/TextEditorBookListener.java | 26 +++++- .../tutorials/LocationStep.java | 32 ++++++- .../teachingtutorials/tutorials/Step.java | 21 ++++- .../tutorials/VideoLinkCommandListener.java | 56 ++++++++++++ 6 files changed, 202 insertions(+), 22 deletions(-) create mode 100644 src/main/java/teachingtutorials/tutorials/VideoLinkCommandListener.java diff --git a/pom.xml b/pom.xml index 218a11e..ebdc365 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.bteuk TeachingTutorials - 1.1.0-Beta5 + 1.1.0-Beta6 jar TeachingTutorials diff --git a/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java b/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java index e644c85..f02ae6e 100644 --- a/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java +++ b/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java @@ -30,9 +30,11 @@ public class StepEditorMenu extends Gui private final Step step; private final LocationStep locationStep; - private TextEditorBookListener bookListener; + private TextEditorBookListener instructionsBookListener; + private ItemStack instructionsBook; - private ItemStack book; + private TextEditorBookListener videoLinkBookListener; + private ItemStack videoLinkBook; public StepEditorMenu(TeachingTutorials plugin, User user, Step step, LocationStep locationStep) { @@ -42,10 +44,15 @@ public StepEditorMenu(TeachingTutorials plugin, User user, Step step, LocationSt this.step = step; this.locationStep = locationStep; - this.book = new ItemStack(Material.WRITABLE_BOOK, 1); - BookMeta bookMeta = (BookMeta) book.getItemMeta(); - bookMeta.setTitle(step.getName()); - this.book.setItemMeta(bookMeta); + this.instructionsBook = new ItemStack(Material.WRITABLE_BOOK, 1); + BookMeta instructionsBookMeta = (BookMeta) instructionsBook.getItemMeta(); + instructionsBookMeta.setTitle(step.getName()); + this.instructionsBook.setItemMeta(instructionsBookMeta); + + this.videoLinkBook = new ItemStack(Material.WRITABLE_BOOK, 1); + BookMeta videoLinkBookMeta = (BookMeta) videoLinkBook.getItemMeta(); + videoLinkBookMeta.setTitle(step.getName()); + this.videoLinkBook.setItemMeta(videoLinkBookMeta); setItems(); } @@ -59,11 +66,15 @@ public void setItems() ItemStack teleportToStart = Utils.createItem(Material.VILLAGER_SPAWN_EGG, 1, Component.text("Teleport back to the start location", NamedTextColor.GREEN)); + ItemStack videoLink = Utils.createItem(Material.PAINTING, 1, + Component.text("Set the video tutorial link if one exists", NamedTextColor.GREEN), + Component.text("This is specific to each location of the tutorial", NamedTextColor.DARK_GREEN)); + boolean bIsHologramNeeded = step.getInstructionDisplayType().equals(Display.DisplayType.hologram); if (bIsHologramNeeded) { //Set start location coordinates to current location - setItem(11, setStartLocation, new guiAction() { + setItem(10, setStartLocation, new guiAction() { @Override public void rightClick(User u) { leftClick(u); @@ -79,7 +90,7 @@ public void leftClick(User u) { ItemStack instructions = Utils.createItem(Material.WRITABLE_BOOK, 1, Component.text("Set the instructions", NamedTextColor.GREEN)); - setItem(13, instructions, new guiAction() { + setItem(12, instructions, new guiAction() { @Override public void rightClick(User u) { leftClick(u); @@ -88,7 +99,7 @@ public void rightClick(User u) { @Override public void leftClick(User u) { //The book must have the step name as the title - Utils.giveItem(u.player, book, "Instructions editor book"); + Utils.giveItem(u.player, instructionsBook, "Instructions editor book"); Display display = new Display(u.player, Component.text("Use the instructions editor book to set the instructions", NamedTextColor.GREEN)); display.Message(); @@ -96,8 +107,8 @@ public void leftClick(User u) { u.player.closeInventory(InventoryCloseEvent.Reason.PLUGIN); //Sets up the book listener and registers it - bookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getInstructionDisplayType(), step.getName(), book); - bookListener.register(); + instructionsBookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getInstructionDisplayType(), step.getName(), instructionsBook); + instructionsBookListener.register(); //step.tryNextStep() is called via instructionsEdited() from TextEditorBookListener once the book close event occurs } @@ -108,7 +119,7 @@ public void leftClick(User u) { Component.text("Set the instructions hologram location", NamedTextColor.GREEN), Component.text("Set the instructions hologram to your current position", NamedTextColor.DARK_GREEN)); - setItem(15, hologramLocation, new guiAction() { + setItem(14, hologramLocation, new guiAction() { @Override public void rightClick(User u) { leftClick(u); @@ -120,10 +131,35 @@ public void leftClick(User u) { step.tryNextStep(); } }); + + //Set video link + setItem(16, videoLink, new guiAction() { + @Override + public void rightClick(User u) { + leftClick(u); + } + + @Override + public void leftClick(User u) { + //The book must have the step name as the title + Utils.giveItem(u.player, videoLinkBook, "Video link editor book"); + Display display = new Display(u.player, Component.text("Use the video link editor book to set the video link", NamedTextColor.GREEN)); + display.Message(); + + //Closes the current inventory + u.player.closeInventory(InventoryCloseEvent.Reason.PLUGIN); + + //Sets up the book listener and registers it + videoLinkBookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getName(), videoLinkBook); + videoLinkBookListener.register(); + + //The listener unregisters itself once the book is closed. We parse the location step by reference so it can edit the link itself + } + }); } else { - setItem(13, setStartLocation, new guiAction() { + setItem(12, setStartLocation, new guiAction() { @Override public void rightClick(User u) { leftClick(u); @@ -134,6 +170,31 @@ public void leftClick(User u) { setStartLocation(u.player.getLocation()); } }); + + //Set video link + setItem(14, videoLink, new guiAction() { + @Override + public void rightClick(User u) { + leftClick(u); + } + + @Override + public void leftClick(User u) { + //The book must have the step name as the title + Utils.giveItem(u.player, videoLinkBook, "Video link editor book"); + Display display = new Display(u.player, Component.text("Use the video link editor book to set the video link", NamedTextColor.GREEN)); + display.Message(); + + //Closes the current inventory + u.player.closeInventory(InventoryCloseEvent.Reason.PLUGIN); + + //Sets up the book listener and registers it + videoLinkBookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getName(), videoLinkBook); + videoLinkBookListener.register(); + + //The listener unregisters itself once the book is closed. We parse the location step by reference so it can edit the link itself + } + }); } //Teleport to start diff --git a/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java b/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java index 0b0a764..a2e34fc 100644 --- a/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java +++ b/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java @@ -27,8 +27,12 @@ public class TextEditorBookListener implements Listener private String szStepName; private ItemStack book; + private boolean bWasInstructions; //If not then it was video link + + //Used for creating an instructions editor book public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, Display.DisplayType displayType, String szStepName, ItemStack book) { + this.bWasInstructions = true; this.plugin = plugin; this.user = user; this.locationStep = locationStep; @@ -38,6 +42,18 @@ public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep this.book = book; } + //Used for creating a video link editor book + public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, String szStepName, ItemStack book) + { + this.bWasInstructions = false; + this.plugin = plugin; + this.user = user; + this.locationStep = locationStep; + this.stepEditorMenu = stepEditorMenu; + this.szStepName = szStepName; + this.book = book; + } + public void register() { Bukkit.getServer().getPluginManager().registerEvents(this, plugin); @@ -66,8 +82,11 @@ public void BookCloseEvent(PlayerEditBookEvent e) //Removes the end space, the space after the last page is added in the loop but then needs to be removed szNewContent = szNewContent.substring(0, szNewContent.length() - 1); - //Edits the step instructions - locationStep.setInstruction(szNewContent, displayType, user.player, szStepName); + //Edits the step instructions or video link + if (bWasInstructions) + locationStep.setInstruction(szNewContent, displayType, user.player, szStepName); + else + locationStep.setVideoLink(szNewContent); //Saves the instructions in the book BookMeta bookMeta = (BookMeta) book.getItemMeta(); @@ -85,7 +104,8 @@ public void BookCloseEvent(PlayerEditBookEvent e) user.player.getInventory().getItemInMainHand().setAmount(0); //Informs the menu that some instructions were edited - stepEditorMenu.instructionsEdited(); + if (bWasInstructions) + stepEditorMenu.instructionsEdited(); } private void unregister() diff --git a/src/main/java/teachingtutorials/tutorials/LocationStep.java b/src/main/java/teachingtutorials/tutorials/LocationStep.java index ab64f94..bf51f31 100644 --- a/src/main/java/teachingtutorials/tutorials/LocationStep.java +++ b/src/main/java/teachingtutorials/tutorials/LocationStep.java @@ -1,5 +1,9 @@ package teachingtutorials.tutorials; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -33,6 +37,7 @@ public class LocationStep// extend step? private double dHologramLocationY; private double dHologramLocationZ; private Hologram instructions; + private String szVideoWalkthroughLink; private boolean bLocationSet; private boolean bInstructionsSet; @@ -43,6 +48,7 @@ public LocationStep(int iLocationID, int iStepID, boolean bHologramNeeded) this.iLocationID = iLocationID; this.iStepID = iStepID; this.szInstructions = ""; + this.szVideoWalkthroughLink = ""; bLocationSet = false; bInstructionsSet = false; @@ -107,6 +113,7 @@ public static LocationStep getFromStepAndLocation(int iStepID, int iLocationID, locationStep.dHologramLocationX = resultSet.getDouble("InstructionsX"); locationStep.dHologramLocationY = resultSet.getDouble("InstructionsY"); locationStep.dHologramLocationZ = resultSet.getDouble("InstructionsZ"); + locationStep.szVideoWalkthroughLink = resultSet.getString("VideoWalkthroughLink"); } } catch(SQLException se) @@ -135,7 +142,7 @@ public boolean storeDetailsInDB() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - sql = "INSERT INTO LocationSteps (Location, Step, Latitude, Longitude, StartYaw, StartPitch, Instructions, InstructionsX, InstructionsY, InstructionsZ) VALUES (" + sql = "INSERT INTO LocationSteps (Location, Step, Latitude, Longitude, StartYaw, StartPitch, Instructions, InstructionsX, InstructionsY, InstructionsZ, VideoWalkthroughLink) VALUES (" + iLocationID +", " + iStepID +", " + dStartLatitude +", " @@ -145,7 +152,8 @@ public boolean storeDetailsInDB() + szInstructions +"', " + dHologramLocationX +", " + dHologramLocationY +", " - + dHologramLocationZ + + dHologramLocationZ +", '" + + szVideoWalkthroughLink +"'" +")"; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +sql); iCount = SQL.executeUpdate(sql); @@ -320,4 +328,24 @@ public void setInstruction(String szInstructions, Display.DisplayType displayTyp this.bInstructionsSet = true; } + + public void setVideoLink(String szLink) + { + this.szVideoWalkthroughLink = szLink; + } + + public void displayVideoLink(Player player) + { + TextComponent linkMessage; + + if (szVideoWalkthroughLink.equals("")) + linkMessage = Component.text("There is no video available for this step, sorry", NamedTextColor.GREEN); + else + { + linkMessage = Component.text("Click here to access a video walk-through for this step !", NamedTextColor.GREEN); + ClickEvent openLinkEvent = ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, this.szVideoWalkthroughLink); + linkMessage = linkMessage.clickEvent(openLinkEvent); + } + player.sendMessage(linkMessage); + } } diff --git a/src/main/java/teachingtutorials/tutorials/Step.java b/src/main/java/teachingtutorials/tutorials/Step.java index a12528c..874dd59 100644 --- a/src/main/java/teachingtutorials/tutorials/Step.java +++ b/src/main/java/teachingtutorials/tutorials/Step.java @@ -24,6 +24,7 @@ public class Step private Player player; private TeachingTutorials plugin; public Stage parentStage; + private VideoLinkCommandListener videoLinkListener; /** * Notes whether all tasks have been completed/set or not @@ -69,6 +70,9 @@ public Step(int iStepID, int iStepInStage, String szStepName, Player player, Tea else //Gets the location specific data this.locationStep = LocationStep.getFromStepAndLocation(this.iStepID, this.parentStage.tutorialPlaythrough.getLocation().getLocationID(), getInstructionDisplayType().equals(Display.DisplayType.hologram)); + + //Initialises the video link listener + videoLinkListener = new VideoLinkCommandListener(this.plugin, this.player, this.locationStep); } //Used for adding a step to the DB @@ -217,6 +221,9 @@ public void run() { //Player is a student doing a tutorial if (!parentStage.bLocationCreation) { + //Registers the video link listener + videoLinkListener.register(); + //Register the start of all groups int i; int iGroups = groups.size(); @@ -321,9 +328,14 @@ protected void groupFinished() } else { + //Unregisters the video link listener + videoLinkListener.unregister(); + //Remove hologram if (getInstructionDisplayType().equals(Display.DisplayType.hologram)) locationStep.removeInstructionsHologram(); + + //Calls stage to start the next step parentStage.nextStep(); } } @@ -372,9 +384,8 @@ public void tryNextStep() public void terminateEarly() { - //Remove holograms - if (getInstructionDisplayType().equals(Display.DisplayType.hologram)) - this.locationStep.removeInstructionsHologram(); + //Unregisters the video link listener + videoLinkListener.unregister(); //Unregisters the current task listener if (parentStage.bLocationCreation) @@ -398,6 +409,10 @@ public void terminateEarly() Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +" [TeachingTutorials] Unregistered group "+(i+1)); } } + + //Remove holograms + if (getInstructionDisplayType().equals(Display.DisplayType.hologram)) + this.locationStep.removeInstructionsHologram(); } public static ArrayList fetchStepsByStageID(Player player, TeachingTutorials plugin, Stage stage) diff --git a/src/main/java/teachingtutorials/tutorials/VideoLinkCommandListener.java b/src/main/java/teachingtutorials/tutorials/VideoLinkCommandListener.java new file mode 100644 index 0000000..3924c16 --- /dev/null +++ b/src/main/java/teachingtutorials/tutorials/VideoLinkCommandListener.java @@ -0,0 +1,56 @@ +package teachingtutorials.tutorials; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import teachingtutorials.TeachingTutorials; + +/** + Listens out for /link when a tutorial is being played + */ +public class VideoLinkCommandListener implements Listener +{ + private TeachingTutorials plugin; + private Player player; + private LocationStep locationStep; + + public VideoLinkCommandListener(TeachingTutorials plugin, Player player, LocationStep locationStep) + { + this.plugin = plugin; + this.player = player; + this.locationStep = locationStep; + } + + //On command + @EventHandler(priority = EventPriority.LOWEST) + public void commandEvent(PlayerCommandPreprocessEvent event) + { + if (event.isCancelled()) + return; + + if (this.player.getUniqueId().equals(event.getPlayer().getUniqueId())) + { + if (event.getMessage().startsWith("/link") || event.getMessage().startsWith("/video")) + { + locationStep.displayVideoLink(event.getPlayer()); + event.setCancelled(true); + } + } + } + + public void register() + { + //Registers the listener + Bukkit.getServer().getPluginManager().registerEvents(this, plugin); + } + + public void unregister() + { + //Unregisters the listener + HandlerList.unregisterAll(this); + } +} From 5d305afc7e208403ebb7156ad3fa3e7cb25a77ad Mon Sep 17 00:00:00 2001 From: george112n Date: Sat, 3 Feb 2024 12:02:25 +0000 Subject: [PATCH 2/5] Updates all SQL commands to use quote marks \` \` around table and column name identifiers. This is to avoid any future issues as the MySQL database syntax changes. --- .../teachingtutorials/TeachingTutorials.java | 12 ++++----- .../teachingtutorials/tutorials/Group.java | 2 +- .../teachingtutorials/tutorials/Lesson.java | 12 ++++----- .../teachingtutorials/tutorials/Location.java | 14 +++++----- .../tutorials/LocationStep.java | 6 ++--- .../tutorials/LocationTask.java | 2 +- .../teachingtutorials/tutorials/Stage.java | 4 +-- .../teachingtutorials/tutorials/Step.java | 12 ++++++++- .../teachingtutorials/tutorials/Tutorial.java | 26 +++++++++---------- .../java/teachingtutorials/utils/User.java | 12 ++++----- 10 files changed, 56 insertions(+), 46 deletions(-) diff --git a/src/main/java/teachingtutorials/TeachingTutorials.java b/src/main/java/teachingtutorials/TeachingTutorials.java index 432e462..2ad4fd5 100644 --- a/src/main/java/teachingtutorials/TeachingTutorials.java +++ b/src/main/java/teachingtutorials/TeachingTutorials.java @@ -495,7 +495,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - sql = "INSERT INTO Tutorials (TutorialName, Author) VALUES ('"+tutorial.szTutorialName+"', '"+tutorial.uuidAuthor +"')"; + sql = "INSERT INTO `Tutorials` (`TutorialName`, `Author`) VALUES ('"+tutorial.szTutorialName+"', '"+tutorial.uuidAuthor +"')"; SQL.executeUpdate(sql); sql = "Select LAST_INSERT_ID()"; @@ -515,7 +515,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) { try { - sql = "INSERT INTO CategoryPoints (TutorialID, Category, Relevance) VALUES (" + iTutorialID + ", '" + tutorial.szCategoryEnumsInOrder[i] + "', " +((float) tutorial.categoryUsage[i])/100+ ")"; + sql = "INSERT INTO `CategoryPoints` (`TutorialID`, `Category`, `Relevance`) VALUES (" + iTutorialID + ", '" + tutorial.szCategoryEnumsInOrder[i] + "', " +((float) tutorial.categoryUsage[i])/100+ ")"; Bukkit.getConsoleSender().sendMessage(sql); SQL.executeUpdate(sql); } @@ -537,7 +537,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) Stage stage = stages.get(i); try { - sql = "INSERT INTO Stages (StageName, TutorialID, `Order`) VALUES ('"+stage.getName()+"', "+iTutorialID+", "+(i+1)+")"; + sql = "INSERT INTO `Stages` (`StageName`, `TutorialID`, `Order`) VALUES ('"+stage.getName()+"', "+iTutorialID+", "+(i+1)+")"; Bukkit.getConsoleSender().sendMessage(sql); SQL.executeUpdate(sql); @@ -565,7 +565,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) { Display.DisplayType instructionDisplayType = step.getInstructionDisplayType(); - sql = "INSERT INTO Steps (StepName, StageID, StepInStage, InstructionDisplay) VALUES ('"+step.getName()+"', "+iStageID+", "+(j+1)+",'" +instructionDisplayType +"')"; + sql = "INSERT INTO `Steps` (`StepName`, `StageID`, `StepInStage`, `InstructionDisplay`) VALUES ('"+step.getName()+"', "+iStageID+", "+(j+1)+",'" +instructionDisplayType +"')"; Bukkit.getConsoleSender().sendMessage(sql); SQL.executeUpdate(sql); @@ -593,7 +593,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"Adding group "+k +". Name: "+group.getName()); try { - sql = "INSERT INTO Groups (StepID)" + + sql = "INSERT INTO `Groups` (`StepID`)" + " VALUES (" +iStepID+")"; Bukkit.getConsoleSender().sendMessage(sql); SQL.executeUpdate(sql); @@ -628,7 +628,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) try { - sql = "INSERT INTO Tasks (GroupID, TaskType, `Order`, Details)" + + sql = "INSERT INTO `Tasks` (`GroupID`, `TaskType`, `Order`, `Details`)" + " VALUES (" +iGroupID+", '"+task.type+"', "+(l+1) +", '" +szDetails +"')"; Bukkit.getConsoleSender().sendMessage(sql); SQL.executeUpdate(sql); diff --git a/src/main/java/teachingtutorials/tutorials/Group.java b/src/main/java/teachingtutorials/tutorials/Group.java index 8469ad6..fcc63fb 100644 --- a/src/main/java/teachingtutorials/tutorials/Group.java +++ b/src/main/java/teachingtutorials/tutorials/Group.java @@ -160,7 +160,7 @@ public static ArrayList fetchGroupsByStepID(Player player, TeachingTutori try { //Compiles the command to fetch groups - sql = "Select * FROM Groups WHERE StepID = "+step.iStepID; + sql = "Select * FROM `Groups` WHERE `StepID` = "+step.iStepID; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query diff --git a/src/main/java/teachingtutorials/tutorials/Lesson.java b/src/main/java/teachingtutorials/tutorials/Lesson.java index db4d371..2ec6991 100644 --- a/src/main/java/teachingtutorials/tutorials/Lesson.java +++ b/src/main/java/teachingtutorials/tutorials/Lesson.java @@ -716,7 +716,7 @@ public boolean fetchCurrentFromUUID() try { //Compiles the command to fetch the lesson in progress - assumes a player can only have one lesson ongoing at a time - sql = "Select * FROM Lessons WHERE UUID = '" +creatorOrStudent.player.getUniqueId() +"' AND Finished = 0"; + sql = "SELECT * FROM `Lessons` WHERE `UUID` = '" +creatorOrStudent.player.getUniqueId() +"' AND `Finished` = 0"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query @@ -761,7 +761,7 @@ private void addLessonToDB() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - szSql = "INSERT INTO Lessons (UUID, TutorialID, Finished, StageAt, StepAt, LocationID)" + + szSql = "INSERT INTO `Lessons` (`UUID`, `TutorialID`, `Finished`, `StageAt`, `StepAt`, `LocationID`)" + " VALUES (" +"'"+creatorOrStudent.player.getUniqueId()+"', " +this.tutorial.getTutorialID()+", " @@ -771,7 +771,7 @@ private void addLessonToDB() +this.location.getLocationID() +")"; SQL.executeUpdate(szSql); - szSql = "Select LAST_INSERT_ID()"; + szSql = "SELECT LAST_INSERT_ID()"; resultSet = SQL.executeQuery(szSql); resultSet.next(); this.iLessonID = resultSet.getInt(1); @@ -796,10 +796,10 @@ protected void savePositions() SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //At this point StageIndex actually refers to what stage they are on and is 1 indexed - szSql = "UPDATE Lessons SET StageAt = " +iStageIndex +" WHERE LessonID = "+ this.iLessonID; + szSql = "UPDATE `Lessons` SET `StageAt` = " +iStageIndex +" WHERE `LessonID` = "+ this.iLessonID; SQL.executeUpdate(szSql); - szSql = "UPDATE Lessons SET StepAt = " +currentStage.getCurrentStep() +" WHERE LessonID = "+ this.iLessonID; + szSql = "UPDATE `Lessons` SET `StepAt` = " +currentStage.getCurrentStep() +" WHERE `LessonID` = "+ this.iLessonID; SQL.executeUpdate(szSql); } catch (Exception e) @@ -819,7 +819,7 @@ private void setLessonCompleteInDB() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - szSql = "UPDATE Lessons SET Finished = 1 WHERE LessonID = "+ this.iLessonID; + szSql = "UPDATE `Lessons` SET `Finished` = 1 WHERE `LessonID` = "+ this.iLessonID; SQL.executeUpdate(szSql); } catch (Exception e) diff --git a/src/main/java/teachingtutorials/tutorials/Location.java b/src/main/java/teachingtutorials/tutorials/Location.java index dd3a05e..fbbcbd0 100644 --- a/src/main/java/teachingtutorials/tutorials/Location.java +++ b/src/main/java/teachingtutorials/tutorials/Location.java @@ -83,7 +83,7 @@ private void fetchDetailsByLocationID() try { //Compiles the command to fetch location - sql = "Select * FROM Locations WHERE Locations.LocationID = " +iLocationID; + sql = "SELECT * FROM `Locations` WHERE `Locations`.`LocationID` = " +iLocationID; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -115,7 +115,7 @@ public static int[] getAllLocationIDsForTutorial(int iTutorialID) try { //Compiles the command to fetch all the locations for the tutorial - sql = "Select * FROM Locations WHERE TutorialID = " +iTutorialID; + sql = "SELECT * FROM `Locations` WHERE `TutorialID` = " +iTutorialID; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +sql); SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -159,7 +159,7 @@ public boolean insertNewLocation() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - sql = "INSERT INTO Locations (TutorialID) VALUES (" +iTutorialID+")"; + sql = "INSERT INTO `Locations` (`TutorialID`) VALUES (" +iTutorialID+")"; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +sql); iCount = SQL.executeUpdate(sql); @@ -169,7 +169,7 @@ public boolean insertNewLocation() } //Gets the LocationID of the newly inserted location - sql = "Select LAST_INSERT_ID()"; + sql = "SELECT LAST_INSERT_ID()"; resultSet = SQL.executeQuery(sql); resultSet.next(); iLocationID = resultSet.getInt(1); @@ -202,19 +202,19 @@ public static boolean deleteLocationByID(int iLocationID) SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Removes the answers - sql = "Delete FROM LocationTasks WHERE LocationID = " +iLocationID; + sql = "DELETE FROM `LocationTasks` WHERE `LocationID` = " +iLocationID; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"[TeachingTutorials] " +sql); iCount = SQL.executeUpdate(sql); Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"[TeachingTutorials] " +iCount +" LocationTasks were deleted"); //Removes the location specific step details - sql = "Delete FROM LocationSteps WHERE LocationID = " +iLocationID; + sql = "DELETE FROM `LocationSteps` WHERE `LocationID` = " +iLocationID; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"[TeachingTutorials] " +sql); iCount = SQL.executeUpdate(sql); Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"[TeachingTutorials] " +iCount +" LocationSteps were deleted"); //Removes the location - sql = "Delete FROM Locations WHERE LocationID = " +iLocationID; + sql = "DELETE FROM `Locations` WHERE `LocationID` = " +iLocationID; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"[TeachingTutorials] " +sql); iCount = SQL.executeUpdate(sql); diff --git a/src/main/java/teachingtutorials/tutorials/LocationStep.java b/src/main/java/teachingtutorials/tutorials/LocationStep.java index bf51f31..7617357 100644 --- a/src/main/java/teachingtutorials/tutorials/LocationStep.java +++ b/src/main/java/teachingtutorials/tutorials/LocationStep.java @@ -84,7 +84,7 @@ public boolean isOtherInformationSet() * //Accesses the DB and fetches the information about the step location * @param iStepID The step ID of the step * @param iLocationID The location that is being played - * @return + * @return A LocationStep object with the details of the LocationStep for the inputted step and location */ public static LocationStep getFromStepAndLocation(int iStepID, int iLocationID, boolean bHologramNeeded) { @@ -97,7 +97,7 @@ public static LocationStep getFromStepAndLocation(int iStepID, int iLocationID, try { //Compiles the command to fetch the location step - sql = "Select * FROM LocationSteps WHERE Step = "+iStepID +" AND Location = " +iLocationID; + sql = "SELECT * FROM `LocationSteps` WHERE `Step` = "+iStepID +" AND `Location` = " +iLocationID; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query @@ -142,7 +142,7 @@ public boolean storeDetailsInDB() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - sql = "INSERT INTO LocationSteps (Location, Step, Latitude, Longitude, StartYaw, StartPitch, Instructions, InstructionsX, InstructionsY, InstructionsZ, VideoWalkthroughLink) VALUES (" + sql = "INSERT INTO `LocationSteps` (`Location`, `Step`, `Latitude`, `Longitude`, `StartYaw`, `StartPitch`, `Instructions`, `InstructionsX`, `InstructionsY`, `InstructionsZ`, `VideoWalkthroughLink`) VALUES (" + iLocationID +", " + iStepID +", " + dStartLatitude +", " diff --git a/src/main/java/teachingtutorials/tutorials/LocationTask.java b/src/main/java/teachingtutorials/tutorials/LocationTask.java index 800f48f..c460112 100644 --- a/src/main/java/teachingtutorials/tutorials/LocationTask.java +++ b/src/main/java/teachingtutorials/tutorials/LocationTask.java @@ -40,7 +40,7 @@ public boolean storeNewData() try { - sql = "INSERT INTO LocationTasks (LocationID, TaskID, Answers, TpllDifficulty, WEDifficulty, ColouringDifficulty, DetailingDifficulty, TerraDifficulty) " + + sql = "INSERT INTO `LocationTasks` (`LocationID`, `TaskID`, `Answers`, `TpllDifficulty`, `WEDifficulty`, `ColouringDifficulty`, `DetailingDifficulty`, `TerraDifficulty`) " + "VALUES (" +iLocationID+", " +iTaskID+", '" +szAnswers+"'"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); diff --git a/src/main/java/teachingtutorials/tutorials/Stage.java b/src/main/java/teachingtutorials/tutorials/Stage.java index a0c54d0..33ec848 100644 --- a/src/main/java/teachingtutorials/tutorials/Stage.java +++ b/src/main/java/teachingtutorials/tutorials/Stage.java @@ -201,7 +201,7 @@ public static ArrayList fetchStagesByTutorialID(Player player, TeachingTu try { //Compiles the command to fetch stages - sql = "Select * FROM Stages WHERE TutorialID = "+lesson.getTutorial().getTutorialID() +" ORDER BY 'Order' ASC"; + sql = "SELECT * FROM `Stages` WHERE `TutorialID` = "+lesson.getTutorial().getTutorialID() +" ORDER BY 'Order' ASC"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query @@ -239,7 +239,7 @@ public static ArrayList fetchStagesByTutorialIDWithoutLocationInformation try { //Compiles the command to fetch stages - sql = "Select * FROM Stages WHERE TutorialID = "+iTutorialID +" ORDER BY 'Order' ASC"; + sql = "SELECT * FROM `Stages` WHERE `TutorialID` = "+iTutorialID +" ORDER BY 'Order' ASC"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query diff --git a/src/main/java/teachingtutorials/tutorials/Step.java b/src/main/java/teachingtutorials/tutorials/Step.java index 874dd59..486fc5e 100644 --- a/src/main/java/teachingtutorials/tutorials/Step.java +++ b/src/main/java/teachingtutorials/tutorials/Step.java @@ -382,6 +382,9 @@ public void tryNextStep() } } + /** + * Use this if a manual termination of the tutorial must occur, for example a player leaves the server. This will terminate the step, unregister listeners and unregister the tasks. + */ public void terminateEarly() { //Unregisters the video link listener @@ -415,6 +418,13 @@ public void terminateEarly() this.locationStep.removeInstructionsHologram(); } + /** + * Retrieves from the database the list of steps for the specified stage + * @param player The player playing through the tutorial + * @param plugin The instance of the plugin + * @param stage The stage for which all steps must be retrieved + * @return A list of steps for this stage + */ public static ArrayList fetchStepsByStageID(Player player, TeachingTutorials plugin, Stage stage) { ArrayList steps = new ArrayList<>(); @@ -426,7 +436,7 @@ public static ArrayList fetchStepsByStageID(Player player, TeachingTutoria try { //Compiles the command to fetch steps - sql = "Select * FROM Steps WHERE StageID = "+stage.iStageID +" ORDER BY 'StepInStage' ASC"; + sql = "SELECT * FROM `Steps` WHERE `StageID` = "+stage.iStageID +" ORDER BY 'StepInStage' ASC"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query diff --git a/src/main/java/teachingtutorials/tutorials/Tutorial.java b/src/main/java/teachingtutorials/tutorials/Tutorial.java index 83f1de5..276b028 100644 --- a/src/main/java/teachingtutorials/tutorials/Tutorial.java +++ b/src/main/java/teachingtutorials/tutorials/Tutorial.java @@ -71,9 +71,9 @@ public static Tutorial[] fetchAll(boolean bInUseOnly) { //Compiles the command to fetch tutorials if (bInUseOnly) - sql = "Select * FROM Tutorials WHERE Tutorials.InUse = 1"; + sql = "SELECT * FROM `Tutorials` WHERE `Tutorials`.`InUse` = 1"; else - sql = "Select * FROM Tutorials"; + sql = "SELECT * FROM `Tutorials`"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query @@ -107,9 +107,9 @@ public static Tutorial[] fetchAll(boolean bInUseOnly) //Compiles the command to fetch category relevancies if (bInUseOnly) - sql = "Select * FROM Tutorials,CategoryPoints WHERE Tutorials.InUse = 1 AND Tutorials.TutorialID = CategoryPoints.TutorialID"; + sql = "SELECT * FROM `Tutorials`,`CategoryPoints` WHERE `Tutorials`.`InUse` = 1 AND `Tutorials`.`TutorialID` = `CategoryPoints`.`TutorialID`"; else - sql = "Select * FROM Tutorials,CategoryPoints WHERE Tutorials.TutorialID = CategoryPoints.TutorialID"; + sql = "SELECT * FROM `Tutorials`,`CategoryPoints` WHERE `Tutorials`.`TutorialID` = `CategoryPoints`.`TutorialID`"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -223,7 +223,7 @@ public static Tutorial[] fetchAllForUser(UUID uuid) try { //Compiles the command to fetch tutorials - sql = "Select * FROM Tutorials WHERE Tutorials.Author = '" +uuid +"'"; + sql = "SELECT * FROM `Tutorials` WHERE `Tutorials`.`Author` = '" +uuid +"'"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -250,7 +250,7 @@ public static Tutorial[] fetchAllForUser(UUID uuid) } //Compiles the command to fetch category difficulties - sql = "Select * FROM Tutorials,CategoryPoints WHERE Tutorials.TutorialID = CategoryPoints.TutorialID AND Tutorials.Author = '"+uuid+"'"; + sql = "SELECT * FROM `Tutorials`,`CategoryPoints` WHERE `Tutorials`.`TutorialID` = `CategoryPoints`.`TutorialID` AND `Tutorials`.`Author` = '"+uuid+"'"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query @@ -312,8 +312,8 @@ public boolean fetchByTutorialID() try { - //Compiles the command to fetch tutoria - sql = "Select * FROM Tutorials WHERE Tutorials.TutorialID = " +this.iTutorialID; + //Compiles the command to fetch the tutorial + sql = "SELECT * FROM `Tutorials` WHERE `Tutorials`.`TutorialID` = " +this.iTutorialID; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -352,11 +352,11 @@ public void toggleCompulsory() { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); if (this.bCompulsory) - szSql = "UPDATE Tutorials SET Compulsory = 0 WHERE TutorialID = "+ this.iTutorialID; + szSql = "UPDATE `Tutorials` SET `Compulsory` = 0 WHERE `TutorialID` = "+ this.iTutorialID; else { setAllTutorialsNotCompulsory(); - szSql = "UPDATE Tutorials SET Compulsory = 1 WHERE TutorialID = "+ this.iTutorialID; + szSql = "UPDATE `Tutorials` SET `Compulsory` = 1 WHERE `TutorialID` = "+ this.iTutorialID; } SQL.executeUpdate(szSql); Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Set tutorial "+this.iTutorialID +" as compulsory"); @@ -379,7 +379,7 @@ private static void setAllTutorialsNotCompulsory() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - szSql = "UPDATE Tutorials SET Compulsory = 0"; + szSql = "UPDATE `Tutorials` SET `Compulsory` = 0"; SQL.executeUpdate(szSql); } catch (Exception e) @@ -401,11 +401,11 @@ public boolean toggleInUse() SQL = TeachingTutorials.getInstance().getConnection().createStatement(); if (this.bInUse) { - szSql = "UPDATE Tutorials SET InUse = 0 WHERE TutorialID = "+ this.iTutorialID; + szSql = "UPDATE `Tutorials` SET `InUse` = 0 WHERE `TutorialID` = "+ this.iTutorialID; } else if (Location.getAllLocationIDsForTutorial(this.iTutorialID).length > 0) { - szSql = "UPDATE Tutorials SET InUse = 1 WHERE TutorialID = "+ this.iTutorialID; + szSql = "UPDATE `Tutorials` SET `InUse` = 1 WHERE `TutorialID` = "+ this.iTutorialID; } else { diff --git a/src/main/java/teachingtutorials/utils/User.java b/src/main/java/teachingtutorials/utils/User.java index 40bd038..24ab8a2 100644 --- a/src/main/java/teachingtutorials/utils/User.java +++ b/src/main/java/teachingtutorials/utils/User.java @@ -301,7 +301,7 @@ public void fetchDetailsByUUID() try { //Compiles the command to select all data about the user - sql = "SELECT * FROM Players WHERE `UUID` = '"+player.getUniqueId()+"'"; + sql = "SELECT * FROM `Players` WHERE `UUID` = '"+player.getUniqueId()+"'"; System.out.println(sql); SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -316,7 +316,7 @@ public void fetchDetailsByUUID() //If no result is found for the user, insert a new user into the database else { - sql = "INSERT INTO Players (UUID) VALUES ('"+ player.getUniqueId() +"')"; + sql = "INSERT INTO `Players` (`UUID`) VALUES ('"+ player.getUniqueId() +"')"; SQL.executeUpdate(sql); this.bHasCompletedCompulsory = false; this.bInLesson = false; @@ -357,7 +357,7 @@ public void triggerCompulsory() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - szSql = "UPDATE Players SET CompletedCompulsory = 1 WHERE UUID = '"+ this.player.getUniqueId()+"'"; + szSql = "UPDATE `Players` SET `CompletedCompulsory` = 1 WHERE `UUID` = '"+ this.player.getUniqueId()+"'"; SQL.executeUpdate(szSql); } catch (Exception e) @@ -377,9 +377,9 @@ public void toogleInLesson() { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); if (this.bInLesson) - szSql = "UPDATE Players SET InLesson = 0 WHERE `UUID` = '"+player.getUniqueId() +"' "; + szSql = "UPDATE `Players` SET `InLesson` = 0 WHERE `UUID` = '"+player.getUniqueId() +"' "; else - szSql = "UPDATE Players SET InLesson = 1 WHERE `UUID` = '"+player.getUniqueId() +"' "; + szSql = "UPDATE `Players` SET `InLesson` = 1 WHERE `UUID` = '"+player.getUniqueId() +"' "; SQL.executeUpdate(szSql); } @@ -400,7 +400,7 @@ public void setInLesson(int i) { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - szSql = "UPDATE Players SET InLesson = " +i +" WHERE `UUID` = '"+player.getUniqueId() +"' "; + szSql = "UPDATE `Players` SET `InLesson` = " +i +" WHERE `UUID` = '"+player.getUniqueId() +"' "; SQL.executeUpdate(szSql); } From 66c9cb48c6e4d919dc8530f54157698e079940eb Mon Sep 17 00:00:00 2001 From: george112n Date: Sat, 3 Feb 2024 12:02:25 +0000 Subject: [PATCH 3/5] Updates all SQL commands to use quote marks \` \` around table and column name identifiers. This is to avoid any future issues as the MySQL database syntax changes. --- .../teachingtutorials/TeachingTutorials.java | 12 ++++----- .../teachingtutorials/tutorials/Group.java | 2 +- .../teachingtutorials/tutorials/Lesson.java | 12 ++++----- .../teachingtutorials/tutorials/Location.java | 14 +++++----- .../tutorials/LocationStep.java | 6 ++--- .../tutorials/LocationTask.java | 2 +- .../teachingtutorials/tutorials/Stage.java | 4 +-- .../teachingtutorials/tutorials/Step.java | 12 ++++++++- .../teachingtutorials/tutorials/Tutorial.java | 26 +++++++++---------- .../java/teachingtutorials/utils/User.java | 14 +++++----- 10 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/main/java/teachingtutorials/TeachingTutorials.java b/src/main/java/teachingtutorials/TeachingTutorials.java index 432e462..2ad4fd5 100644 --- a/src/main/java/teachingtutorials/TeachingTutorials.java +++ b/src/main/java/teachingtutorials/TeachingTutorials.java @@ -495,7 +495,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - sql = "INSERT INTO Tutorials (TutorialName, Author) VALUES ('"+tutorial.szTutorialName+"', '"+tutorial.uuidAuthor +"')"; + sql = "INSERT INTO `Tutorials` (`TutorialName`, `Author`) VALUES ('"+tutorial.szTutorialName+"', '"+tutorial.uuidAuthor +"')"; SQL.executeUpdate(sql); sql = "Select LAST_INSERT_ID()"; @@ -515,7 +515,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) { try { - sql = "INSERT INTO CategoryPoints (TutorialID, Category, Relevance) VALUES (" + iTutorialID + ", '" + tutorial.szCategoryEnumsInOrder[i] + "', " +((float) tutorial.categoryUsage[i])/100+ ")"; + sql = "INSERT INTO `CategoryPoints` (`TutorialID`, `Category`, `Relevance`) VALUES (" + iTutorialID + ", '" + tutorial.szCategoryEnumsInOrder[i] + "', " +((float) tutorial.categoryUsage[i])/100+ ")"; Bukkit.getConsoleSender().sendMessage(sql); SQL.executeUpdate(sql); } @@ -537,7 +537,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) Stage stage = stages.get(i); try { - sql = "INSERT INTO Stages (StageName, TutorialID, `Order`) VALUES ('"+stage.getName()+"', "+iTutorialID+", "+(i+1)+")"; + sql = "INSERT INTO `Stages` (`StageName`, `TutorialID`, `Order`) VALUES ('"+stage.getName()+"', "+iTutorialID+", "+(i+1)+")"; Bukkit.getConsoleSender().sendMessage(sql); SQL.executeUpdate(sql); @@ -565,7 +565,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) { Display.DisplayType instructionDisplayType = step.getInstructionDisplayType(); - sql = "INSERT INTO Steps (StepName, StageID, StepInStage, InstructionDisplay) VALUES ('"+step.getName()+"', "+iStageID+", "+(j+1)+",'" +instructionDisplayType +"')"; + sql = "INSERT INTO `Steps` (`StepName`, `StageID`, `StepInStage`, `InstructionDisplay`) VALUES ('"+step.getName()+"', "+iStageID+", "+(j+1)+",'" +instructionDisplayType +"')"; Bukkit.getConsoleSender().sendMessage(sql); SQL.executeUpdate(sql); @@ -593,7 +593,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"Adding group "+k +". Name: "+group.getName()); try { - sql = "INSERT INTO Groups (StepID)" + + sql = "INSERT INTO `Groups` (`StepID`)" + " VALUES (" +iStepID+")"; Bukkit.getConsoleSender().sendMessage(sql); SQL.executeUpdate(sql); @@ -628,7 +628,7 @@ public boolean addNewTutorialToDB(Tutorial tutorial) try { - sql = "INSERT INTO Tasks (GroupID, TaskType, `Order`, Details)" + + sql = "INSERT INTO `Tasks` (`GroupID`, `TaskType`, `Order`, `Details`)" + " VALUES (" +iGroupID+", '"+task.type+"', "+(l+1) +", '" +szDetails +"')"; Bukkit.getConsoleSender().sendMessage(sql); SQL.executeUpdate(sql); diff --git a/src/main/java/teachingtutorials/tutorials/Group.java b/src/main/java/teachingtutorials/tutorials/Group.java index 8469ad6..fcc63fb 100644 --- a/src/main/java/teachingtutorials/tutorials/Group.java +++ b/src/main/java/teachingtutorials/tutorials/Group.java @@ -160,7 +160,7 @@ public static ArrayList fetchGroupsByStepID(Player player, TeachingTutori try { //Compiles the command to fetch groups - sql = "Select * FROM Groups WHERE StepID = "+step.iStepID; + sql = "Select * FROM `Groups` WHERE `StepID` = "+step.iStepID; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query diff --git a/src/main/java/teachingtutorials/tutorials/Lesson.java b/src/main/java/teachingtutorials/tutorials/Lesson.java index db4d371..2ec6991 100644 --- a/src/main/java/teachingtutorials/tutorials/Lesson.java +++ b/src/main/java/teachingtutorials/tutorials/Lesson.java @@ -716,7 +716,7 @@ public boolean fetchCurrentFromUUID() try { //Compiles the command to fetch the lesson in progress - assumes a player can only have one lesson ongoing at a time - sql = "Select * FROM Lessons WHERE UUID = '" +creatorOrStudent.player.getUniqueId() +"' AND Finished = 0"; + sql = "SELECT * FROM `Lessons` WHERE `UUID` = '" +creatorOrStudent.player.getUniqueId() +"' AND `Finished` = 0"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query @@ -761,7 +761,7 @@ private void addLessonToDB() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - szSql = "INSERT INTO Lessons (UUID, TutorialID, Finished, StageAt, StepAt, LocationID)" + + szSql = "INSERT INTO `Lessons` (`UUID`, `TutorialID`, `Finished`, `StageAt`, `StepAt`, `LocationID`)" + " VALUES (" +"'"+creatorOrStudent.player.getUniqueId()+"', " +this.tutorial.getTutorialID()+", " @@ -771,7 +771,7 @@ private void addLessonToDB() +this.location.getLocationID() +")"; SQL.executeUpdate(szSql); - szSql = "Select LAST_INSERT_ID()"; + szSql = "SELECT LAST_INSERT_ID()"; resultSet = SQL.executeQuery(szSql); resultSet.next(); this.iLessonID = resultSet.getInt(1); @@ -796,10 +796,10 @@ protected void savePositions() SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //At this point StageIndex actually refers to what stage they are on and is 1 indexed - szSql = "UPDATE Lessons SET StageAt = " +iStageIndex +" WHERE LessonID = "+ this.iLessonID; + szSql = "UPDATE `Lessons` SET `StageAt` = " +iStageIndex +" WHERE `LessonID` = "+ this.iLessonID; SQL.executeUpdate(szSql); - szSql = "UPDATE Lessons SET StepAt = " +currentStage.getCurrentStep() +" WHERE LessonID = "+ this.iLessonID; + szSql = "UPDATE `Lessons` SET `StepAt` = " +currentStage.getCurrentStep() +" WHERE `LessonID` = "+ this.iLessonID; SQL.executeUpdate(szSql); } catch (Exception e) @@ -819,7 +819,7 @@ private void setLessonCompleteInDB() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - szSql = "UPDATE Lessons SET Finished = 1 WHERE LessonID = "+ this.iLessonID; + szSql = "UPDATE `Lessons` SET `Finished` = 1 WHERE `LessonID` = "+ this.iLessonID; SQL.executeUpdate(szSql); } catch (Exception e) diff --git a/src/main/java/teachingtutorials/tutorials/Location.java b/src/main/java/teachingtutorials/tutorials/Location.java index dd3a05e..fbbcbd0 100644 --- a/src/main/java/teachingtutorials/tutorials/Location.java +++ b/src/main/java/teachingtutorials/tutorials/Location.java @@ -83,7 +83,7 @@ private void fetchDetailsByLocationID() try { //Compiles the command to fetch location - sql = "Select * FROM Locations WHERE Locations.LocationID = " +iLocationID; + sql = "SELECT * FROM `Locations` WHERE `Locations`.`LocationID` = " +iLocationID; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -115,7 +115,7 @@ public static int[] getAllLocationIDsForTutorial(int iTutorialID) try { //Compiles the command to fetch all the locations for the tutorial - sql = "Select * FROM Locations WHERE TutorialID = " +iTutorialID; + sql = "SELECT * FROM `Locations` WHERE `TutorialID` = " +iTutorialID; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +sql); SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -159,7 +159,7 @@ public boolean insertNewLocation() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - sql = "INSERT INTO Locations (TutorialID) VALUES (" +iTutorialID+")"; + sql = "INSERT INTO `Locations` (`TutorialID`) VALUES (" +iTutorialID+")"; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +sql); iCount = SQL.executeUpdate(sql); @@ -169,7 +169,7 @@ public boolean insertNewLocation() } //Gets the LocationID of the newly inserted location - sql = "Select LAST_INSERT_ID()"; + sql = "SELECT LAST_INSERT_ID()"; resultSet = SQL.executeQuery(sql); resultSet.next(); iLocationID = resultSet.getInt(1); @@ -202,19 +202,19 @@ public static boolean deleteLocationByID(int iLocationID) SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Removes the answers - sql = "Delete FROM LocationTasks WHERE LocationID = " +iLocationID; + sql = "DELETE FROM `LocationTasks` WHERE `LocationID` = " +iLocationID; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"[TeachingTutorials] " +sql); iCount = SQL.executeUpdate(sql); Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"[TeachingTutorials] " +iCount +" LocationTasks were deleted"); //Removes the location specific step details - sql = "Delete FROM LocationSteps WHERE LocationID = " +iLocationID; + sql = "DELETE FROM `LocationSteps` WHERE `LocationID` = " +iLocationID; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"[TeachingTutorials] " +sql); iCount = SQL.executeUpdate(sql); Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"[TeachingTutorials] " +iCount +" LocationSteps were deleted"); //Removes the location - sql = "Delete FROM Locations WHERE LocationID = " +iLocationID; + sql = "DELETE FROM `Locations` WHERE `LocationID` = " +iLocationID; Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA +"[TeachingTutorials] " +sql); iCount = SQL.executeUpdate(sql); diff --git a/src/main/java/teachingtutorials/tutorials/LocationStep.java b/src/main/java/teachingtutorials/tutorials/LocationStep.java index bf51f31..7617357 100644 --- a/src/main/java/teachingtutorials/tutorials/LocationStep.java +++ b/src/main/java/teachingtutorials/tutorials/LocationStep.java @@ -84,7 +84,7 @@ public boolean isOtherInformationSet() * //Accesses the DB and fetches the information about the step location * @param iStepID The step ID of the step * @param iLocationID The location that is being played - * @return + * @return A LocationStep object with the details of the LocationStep for the inputted step and location */ public static LocationStep getFromStepAndLocation(int iStepID, int iLocationID, boolean bHologramNeeded) { @@ -97,7 +97,7 @@ public static LocationStep getFromStepAndLocation(int iStepID, int iLocationID, try { //Compiles the command to fetch the location step - sql = "Select * FROM LocationSteps WHERE Step = "+iStepID +" AND Location = " +iLocationID; + sql = "SELECT * FROM `LocationSteps` WHERE `Step` = "+iStepID +" AND `Location` = " +iLocationID; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query @@ -142,7 +142,7 @@ public boolean storeDetailsInDB() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - sql = "INSERT INTO LocationSteps (Location, Step, Latitude, Longitude, StartYaw, StartPitch, Instructions, InstructionsX, InstructionsY, InstructionsZ, VideoWalkthroughLink) VALUES (" + sql = "INSERT INTO `LocationSteps` (`Location`, `Step`, `Latitude`, `Longitude`, `StartYaw`, `StartPitch`, `Instructions`, `InstructionsX`, `InstructionsY`, `InstructionsZ`, `VideoWalkthroughLink`) VALUES (" + iLocationID +", " + iStepID +", " + dStartLatitude +", " diff --git a/src/main/java/teachingtutorials/tutorials/LocationTask.java b/src/main/java/teachingtutorials/tutorials/LocationTask.java index 800f48f..c460112 100644 --- a/src/main/java/teachingtutorials/tutorials/LocationTask.java +++ b/src/main/java/teachingtutorials/tutorials/LocationTask.java @@ -40,7 +40,7 @@ public boolean storeNewData() try { - sql = "INSERT INTO LocationTasks (LocationID, TaskID, Answers, TpllDifficulty, WEDifficulty, ColouringDifficulty, DetailingDifficulty, TerraDifficulty) " + + sql = "INSERT INTO `LocationTasks` (`LocationID`, `TaskID`, `Answers`, `TpllDifficulty`, `WEDifficulty`, `ColouringDifficulty`, `DetailingDifficulty`, `TerraDifficulty`) " + "VALUES (" +iLocationID+", " +iTaskID+", '" +szAnswers+"'"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); diff --git a/src/main/java/teachingtutorials/tutorials/Stage.java b/src/main/java/teachingtutorials/tutorials/Stage.java index a0c54d0..33ec848 100644 --- a/src/main/java/teachingtutorials/tutorials/Stage.java +++ b/src/main/java/teachingtutorials/tutorials/Stage.java @@ -201,7 +201,7 @@ public static ArrayList fetchStagesByTutorialID(Player player, TeachingTu try { //Compiles the command to fetch stages - sql = "Select * FROM Stages WHERE TutorialID = "+lesson.getTutorial().getTutorialID() +" ORDER BY 'Order' ASC"; + sql = "SELECT * FROM `Stages` WHERE `TutorialID` = "+lesson.getTutorial().getTutorialID() +" ORDER BY 'Order' ASC"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query @@ -239,7 +239,7 @@ public static ArrayList fetchStagesByTutorialIDWithoutLocationInformation try { //Compiles the command to fetch stages - sql = "Select * FROM Stages WHERE TutorialID = "+iTutorialID +" ORDER BY 'Order' ASC"; + sql = "SELECT * FROM `Stages` WHERE `TutorialID` = "+iTutorialID +" ORDER BY 'Order' ASC"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query diff --git a/src/main/java/teachingtutorials/tutorials/Step.java b/src/main/java/teachingtutorials/tutorials/Step.java index 874dd59..486fc5e 100644 --- a/src/main/java/teachingtutorials/tutorials/Step.java +++ b/src/main/java/teachingtutorials/tutorials/Step.java @@ -382,6 +382,9 @@ public void tryNextStep() } } + /** + * Use this if a manual termination of the tutorial must occur, for example a player leaves the server. This will terminate the step, unregister listeners and unregister the tasks. + */ public void terminateEarly() { //Unregisters the video link listener @@ -415,6 +418,13 @@ public void terminateEarly() this.locationStep.removeInstructionsHologram(); } + /** + * Retrieves from the database the list of steps for the specified stage + * @param player The player playing through the tutorial + * @param plugin The instance of the plugin + * @param stage The stage for which all steps must be retrieved + * @return A list of steps for this stage + */ public static ArrayList fetchStepsByStageID(Player player, TeachingTutorials plugin, Stage stage) { ArrayList steps = new ArrayList<>(); @@ -426,7 +436,7 @@ public static ArrayList fetchStepsByStageID(Player player, TeachingTutoria try { //Compiles the command to fetch steps - sql = "Select * FROM Steps WHERE StageID = "+stage.iStageID +" ORDER BY 'StepInStage' ASC"; + sql = "SELECT * FROM `Steps` WHERE `StageID` = "+stage.iStageID +" ORDER BY 'StepInStage' ASC"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query diff --git a/src/main/java/teachingtutorials/tutorials/Tutorial.java b/src/main/java/teachingtutorials/tutorials/Tutorial.java index 83f1de5..276b028 100644 --- a/src/main/java/teachingtutorials/tutorials/Tutorial.java +++ b/src/main/java/teachingtutorials/tutorials/Tutorial.java @@ -71,9 +71,9 @@ public static Tutorial[] fetchAll(boolean bInUseOnly) { //Compiles the command to fetch tutorials if (bInUseOnly) - sql = "Select * FROM Tutorials WHERE Tutorials.InUse = 1"; + sql = "SELECT * FROM `Tutorials` WHERE `Tutorials`.`InUse` = 1"; else - sql = "Select * FROM Tutorials"; + sql = "SELECT * FROM `Tutorials`"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query @@ -107,9 +107,9 @@ public static Tutorial[] fetchAll(boolean bInUseOnly) //Compiles the command to fetch category relevancies if (bInUseOnly) - sql = "Select * FROM Tutorials,CategoryPoints WHERE Tutorials.InUse = 1 AND Tutorials.TutorialID = CategoryPoints.TutorialID"; + sql = "SELECT * FROM `Tutorials`,`CategoryPoints` WHERE `Tutorials`.`InUse` = 1 AND `Tutorials`.`TutorialID` = `CategoryPoints`.`TutorialID`"; else - sql = "Select * FROM Tutorials,CategoryPoints WHERE Tutorials.TutorialID = CategoryPoints.TutorialID"; + sql = "SELECT * FROM `Tutorials`,`CategoryPoints` WHERE `Tutorials`.`TutorialID` = `CategoryPoints`.`TutorialID`"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -223,7 +223,7 @@ public static Tutorial[] fetchAllForUser(UUID uuid) try { //Compiles the command to fetch tutorials - sql = "Select * FROM Tutorials WHERE Tutorials.Author = '" +uuid +"'"; + sql = "SELECT * FROM `Tutorials` WHERE `Tutorials`.`Author` = '" +uuid +"'"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -250,7 +250,7 @@ public static Tutorial[] fetchAllForUser(UUID uuid) } //Compiles the command to fetch category difficulties - sql = "Select * FROM Tutorials,CategoryPoints WHERE Tutorials.TutorialID = CategoryPoints.TutorialID AND Tutorials.Author = '"+uuid+"'"; + sql = "SELECT * FROM `Tutorials`,`CategoryPoints` WHERE `Tutorials`.`TutorialID` = `CategoryPoints`.`TutorialID` AND `Tutorials`.`Author` = '"+uuid+"'"; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); //Executes the query @@ -312,8 +312,8 @@ public boolean fetchByTutorialID() try { - //Compiles the command to fetch tutoria - sql = "Select * FROM Tutorials WHERE Tutorials.TutorialID = " +this.iTutorialID; + //Compiles the command to fetch the tutorial + sql = "SELECT * FROM `Tutorials` WHERE `Tutorials`.`TutorialID` = " +this.iTutorialID; SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -352,11 +352,11 @@ public void toggleCompulsory() { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); if (this.bCompulsory) - szSql = "UPDATE Tutorials SET Compulsory = 0 WHERE TutorialID = "+ this.iTutorialID; + szSql = "UPDATE `Tutorials` SET `Compulsory` = 0 WHERE `TutorialID` = "+ this.iTutorialID; else { setAllTutorialsNotCompulsory(); - szSql = "UPDATE Tutorials SET Compulsory = 1 WHERE TutorialID = "+ this.iTutorialID; + szSql = "UPDATE `Tutorials` SET `Compulsory` = 1 WHERE `TutorialID` = "+ this.iTutorialID; } SQL.executeUpdate(szSql); Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Set tutorial "+this.iTutorialID +" as compulsory"); @@ -379,7 +379,7 @@ private static void setAllTutorialsNotCompulsory() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - szSql = "UPDATE Tutorials SET Compulsory = 0"; + szSql = "UPDATE `Tutorials` SET `Compulsory` = 0"; SQL.executeUpdate(szSql); } catch (Exception e) @@ -401,11 +401,11 @@ public boolean toggleInUse() SQL = TeachingTutorials.getInstance().getConnection().createStatement(); if (this.bInUse) { - szSql = "UPDATE Tutorials SET InUse = 0 WHERE TutorialID = "+ this.iTutorialID; + szSql = "UPDATE `Tutorials` SET `InUse` = 0 WHERE `TutorialID` = "+ this.iTutorialID; } else if (Location.getAllLocationIDsForTutorial(this.iTutorialID).length > 0) { - szSql = "UPDATE Tutorials SET InUse = 1 WHERE TutorialID = "+ this.iTutorialID; + szSql = "UPDATE `Tutorials` SET `InUse` = 1 WHERE `TutorialID` = "+ this.iTutorialID; } else { diff --git a/src/main/java/teachingtutorials/utils/User.java b/src/main/java/teachingtutorials/utils/User.java index 40bd038..8d3c637 100644 --- a/src/main/java/teachingtutorials/utils/User.java +++ b/src/main/java/teachingtutorials/utils/User.java @@ -84,7 +84,7 @@ private int calculateRating(Category category) try { //Compiles the command to add the new user - sql = "SELECT * FROM Lessons, Scores WHERE `Lessons`.`UUID` = '"+player.getUniqueId() +"' " + + sql = "SELECT * FROM `Lessons`, `Scores` WHERE `Lessons`.`UUID` = '"+player.getUniqueId() +"' " + "AND `Scores`.`Category` = '" + category.toString() + "' "+ "AND `Lessons`.`LessonID` = `Scores`.`LessonID` " + "ORDER BY `Scores`.`LessonID` DESC"; @@ -301,7 +301,7 @@ public void fetchDetailsByUUID() try { //Compiles the command to select all data about the user - sql = "SELECT * FROM Players WHERE `UUID` = '"+player.getUniqueId()+"'"; + sql = "SELECT * FROM `Players` WHERE `UUID` = '"+player.getUniqueId()+"'"; System.out.println(sql); SQL = TeachingTutorials.getInstance().getConnection().createStatement(); @@ -316,7 +316,7 @@ public void fetchDetailsByUUID() //If no result is found for the user, insert a new user into the database else { - sql = "INSERT INTO Players (UUID) VALUES ('"+ player.getUniqueId() +"')"; + sql = "INSERT INTO `Players` (`UUID`) VALUES ('"+ player.getUniqueId() +"')"; SQL.executeUpdate(sql); this.bHasCompletedCompulsory = false; this.bInLesson = false; @@ -357,7 +357,7 @@ public void triggerCompulsory() try { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - szSql = "UPDATE Players SET CompletedCompulsory = 1 WHERE UUID = '"+ this.player.getUniqueId()+"'"; + szSql = "UPDATE `Players` SET `CompletedCompulsory` = 1 WHERE `UUID` = '"+ this.player.getUniqueId()+"'"; SQL.executeUpdate(szSql); } catch (Exception e) @@ -377,9 +377,9 @@ public void toogleInLesson() { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); if (this.bInLesson) - szSql = "UPDATE Players SET InLesson = 0 WHERE `UUID` = '"+player.getUniqueId() +"' "; + szSql = "UPDATE `Players` SET `InLesson` = 0 WHERE `UUID` = '"+player.getUniqueId() +"' "; else - szSql = "UPDATE Players SET InLesson = 1 WHERE `UUID` = '"+player.getUniqueId() +"' "; + szSql = "UPDATE `Players` SET `InLesson` = 1 WHERE `UUID` = '"+player.getUniqueId() +"' "; SQL.executeUpdate(szSql); } @@ -400,7 +400,7 @@ public void setInLesson(int i) { SQL = TeachingTutorials.getInstance().getConnection().createStatement(); - szSql = "UPDATE Players SET InLesson = " +i +" WHERE `UUID` = '"+player.getUniqueId() +"' "; + szSql = "UPDATE `Players` SET `InLesson` = " +i +" WHERE `UUID` = '"+player.getUniqueId() +"' "; SQL.executeUpdate(szSql); } From 6152d36cc9633e1c17b1b53e5ac70b984912f943 Mon Sep 17 00:00:00 2001 From: george112n Date: Sat, 3 Feb 2024 15:03:28 +0000 Subject: [PATCH 4/5] Move the initialisation of the books for the text editor into the TextEditorBookListener itself, and now uses the same code for both book types. --- .../locationcreatemenus/StepEditorMenu.java | 18 +++-------- .../listeners/TextEditorBookListener.java | 32 +++++++++++++------ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java b/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java index f02ae6e..95b6d8a 100644 --- a/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java +++ b/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java @@ -8,7 +8,6 @@ import org.bukkit.Material; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.BookMeta; import teachingtutorials.TeachingTutorials; import teachingtutorials.guis.Gui; import teachingtutorials.listeners.TextEditorBookListener; @@ -44,16 +43,6 @@ public StepEditorMenu(TeachingTutorials plugin, User user, Step step, LocationSt this.step = step; this.locationStep = locationStep; - this.instructionsBook = new ItemStack(Material.WRITABLE_BOOK, 1); - BookMeta instructionsBookMeta = (BookMeta) instructionsBook.getItemMeta(); - instructionsBookMeta.setTitle(step.getName()); - this.instructionsBook.setItemMeta(instructionsBookMeta); - - this.videoLinkBook = new ItemStack(Material.WRITABLE_BOOK, 1); - BookMeta videoLinkBookMeta = (BookMeta) videoLinkBook.getItemMeta(); - videoLinkBookMeta.setTitle(step.getName()); - this.videoLinkBook.setItemMeta(videoLinkBookMeta); - setItems(); } @@ -107,7 +96,7 @@ public void leftClick(User u) { u.player.closeInventory(InventoryCloseEvent.Reason.PLUGIN); //Sets up the book listener and registers it - instructionsBookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getInstructionDisplayType(), step.getName(), instructionsBook); + instructionsBookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getInstructionDisplayType(), step.getName()); instructionsBookListener.register(); //step.tryNextStep() is called via instructionsEdited() from TextEditorBookListener once the book close event occurs @@ -150,7 +139,7 @@ public void leftClick(User u) { u.player.closeInventory(InventoryCloseEvent.Reason.PLUGIN); //Sets up the book listener and registers it - videoLinkBookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getName(), videoLinkBook); + videoLinkBookListener = new TextEditorBookListener(plugin, user, locationStep, StepEditorMenu.this, step.getName()); videoLinkBookListener.register(); //The listener unregisters itself once the book is closed. We parse the location step by reference so it can edit the link itself @@ -159,6 +148,7 @@ public void leftClick(User u) { } else { + //Set start location setItem(12, setStartLocation, new guiAction() { @Override public void rightClick(User u) { @@ -189,7 +179,7 @@ public void leftClick(User u) { u.player.closeInventory(InventoryCloseEvent.Reason.PLUGIN); //Sets up the book listener and registers it - videoLinkBookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getName(), videoLinkBook); + videoLinkBookListener = new TextEditorBookListener(plugin, user, locationStep, StepEditorMenu.this, step.getName()); videoLinkBookListener.register(); //The listener unregisters itself once the book is closed. We parse the location step by reference so it can edit the link itself diff --git a/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java b/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java index a2e34fc..f041ce5 100644 --- a/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java +++ b/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java @@ -3,6 +3,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; @@ -30,28 +31,39 @@ public class TextEditorBookListener implements Listener private boolean bWasInstructions; //If not then it was video link //Used for creating an instructions editor book - public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, Display.DisplayType displayType, String szStepName, ItemStack book) + public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, Display.DisplayType displayType, String szStepName) { - this.bWasInstructions = true; - this.plugin = plugin; - this.user = user; - this.locationStep = locationStep; - this.stepEditorMenu = stepEditorMenu; this.displayType = displayType; - this.szStepName = szStepName; - this.book = book; + this.bWasInstructions = true; + + commonSetup(plugin, user, locationStep, stepEditorMenu, szStepName); } //Used for creating a video link editor book - public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, String szStepName, ItemStack book) + public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, String szStepName) { this.bWasInstructions = false; + + commonSetup(plugin, user, locationStep, stepEditorMenu, szStepName); + } + + private void commonSetup(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, String szStepName) + { this.plugin = plugin; this.user = user; this.locationStep = locationStep; this.stepEditorMenu = stepEditorMenu; this.szStepName = szStepName; - this.book = book; + + //Creates the book + this.book = new ItemStack(Material.WRITABLE_BOOK, 1); + + //Extracts the book meta reference, and sets the title + BookMeta videoLinkBookMeta = (BookMeta) this.book.getItemMeta(); + videoLinkBookMeta.setTitle(szStepName); + + //Adds the meta of the book back in + this.book.setItemMeta(videoLinkBookMeta); } public void register() From 054454edda3e40b7321260e8e40856a0d26835c9 Mon Sep 17 00:00:00 2001 From: george112n Date: Sat, 3 Feb 2024 15:03:28 +0000 Subject: [PATCH 5/5] Move the initialisation of the books for the text editor into the TextEditorBookListener itself, and now uses the same code for both book types. --- .../locationcreatemenus/StepEditorMenu.java | 25 +++---------- .../listeners/TextEditorBookListener.java | 37 ++++++++++++++----- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java b/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java index f02ae6e..df780b3 100644 --- a/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java +++ b/src/main/java/teachingtutorials/guis/locationcreatemenus/StepEditorMenu.java @@ -8,7 +8,6 @@ import org.bukkit.Material; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.BookMeta; import teachingtutorials.TeachingTutorials; import teachingtutorials.guis.Gui; import teachingtutorials.listeners.TextEditorBookListener; @@ -31,10 +30,7 @@ public class StepEditorMenu extends Gui private final LocationStep locationStep; private TextEditorBookListener instructionsBookListener; - private ItemStack instructionsBook; - private TextEditorBookListener videoLinkBookListener; - private ItemStack videoLinkBook; public StepEditorMenu(TeachingTutorials plugin, User user, Step step, LocationStep locationStep) { @@ -44,15 +40,8 @@ public StepEditorMenu(TeachingTutorials plugin, User user, Step step, LocationSt this.step = step; this.locationStep = locationStep; - this.instructionsBook = new ItemStack(Material.WRITABLE_BOOK, 1); - BookMeta instructionsBookMeta = (BookMeta) instructionsBook.getItemMeta(); - instructionsBookMeta.setTitle(step.getName()); - this.instructionsBook.setItemMeta(instructionsBookMeta); - - this.videoLinkBook = new ItemStack(Material.WRITABLE_BOOK, 1); - BookMeta videoLinkBookMeta = (BookMeta) videoLinkBook.getItemMeta(); - videoLinkBookMeta.setTitle(step.getName()); - this.videoLinkBook.setItemMeta(videoLinkBookMeta); + this.videoLinkBookListener = new TextEditorBookListener(plugin, user, locationStep, StepEditorMenu.this, step.getName()); + this.instructionsBookListener = new TextEditorBookListener(plugin, user, locationStep, StepEditorMenu.this, step.getInstructionDisplayType(), step.getName()); setItems(); } @@ -99,7 +88,7 @@ public void rightClick(User u) { @Override public void leftClick(User u) { //The book must have the step name as the title - Utils.giveItem(u.player, instructionsBook, "Instructions editor book"); + Utils.giveItem(u.player, instructionsBookListener.getBook(), "Instructions editor book"); Display display = new Display(u.player, Component.text("Use the instructions editor book to set the instructions", NamedTextColor.GREEN)); display.Message(); @@ -107,7 +96,6 @@ public void leftClick(User u) { u.player.closeInventory(InventoryCloseEvent.Reason.PLUGIN); //Sets up the book listener and registers it - instructionsBookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getInstructionDisplayType(), step.getName(), instructionsBook); instructionsBookListener.register(); //step.tryNextStep() is called via instructionsEdited() from TextEditorBookListener once the book close event occurs @@ -142,7 +130,7 @@ public void rightClick(User u) { @Override public void leftClick(User u) { //The book must have the step name as the title - Utils.giveItem(u.player, videoLinkBook, "Video link editor book"); + Utils.giveItem(u.player, videoLinkBookListener.getBook(), "Video link editor book"); Display display = new Display(u.player, Component.text("Use the video link editor book to set the video link", NamedTextColor.GREEN)); display.Message(); @@ -150,7 +138,6 @@ public void leftClick(User u) { u.player.closeInventory(InventoryCloseEvent.Reason.PLUGIN); //Sets up the book listener and registers it - videoLinkBookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getName(), videoLinkBook); videoLinkBookListener.register(); //The listener unregisters itself once the book is closed. We parse the location step by reference so it can edit the link itself @@ -159,6 +146,7 @@ public void leftClick(User u) { } else { + //Set start location setItem(12, setStartLocation, new guiAction() { @Override public void rightClick(User u) { @@ -181,7 +169,7 @@ public void rightClick(User u) { @Override public void leftClick(User u) { //The book must have the step name as the title - Utils.giveItem(u.player, videoLinkBook, "Video link editor book"); + Utils.giveItem(u.player, videoLinkBookListener.getBook(), "Video link editor book"); Display display = new Display(u.player, Component.text("Use the video link editor book to set the video link", NamedTextColor.GREEN)); display.Message(); @@ -189,7 +177,6 @@ public void leftClick(User u) { u.player.closeInventory(InventoryCloseEvent.Reason.PLUGIN); //Sets up the book listener and registers it - videoLinkBookListener = new TextEditorBookListener(plugin, u, locationStep, StepEditorMenu.this, step.getName(), videoLinkBook); videoLinkBookListener.register(); //The listener unregisters itself once the book is closed. We parse the location step by reference so it can edit the link itself diff --git a/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java b/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java index a2e34fc..182bf46 100644 --- a/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java +++ b/src/main/java/teachingtutorials/listeners/TextEditorBookListener.java @@ -3,6 +3,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; @@ -30,28 +31,44 @@ public class TextEditorBookListener implements Listener private boolean bWasInstructions; //If not then it was video link //Used for creating an instructions editor book - public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, Display.DisplayType displayType, String szStepName, ItemStack book) + public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, Display.DisplayType displayType, String szStepName) { - this.bWasInstructions = true; - this.plugin = plugin; - this.user = user; - this.locationStep = locationStep; - this.stepEditorMenu = stepEditorMenu; this.displayType = displayType; - this.szStepName = szStepName; - this.book = book; + this.bWasInstructions = true; + + commonSetup(plugin, user, locationStep, stepEditorMenu, szStepName); } //Used for creating a video link editor book - public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, String szStepName, ItemStack book) + public TextEditorBookListener(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, String szStepName) { this.bWasInstructions = false; + + commonSetup(plugin, user, locationStep, stepEditorMenu, szStepName); + } + + private void commonSetup(TeachingTutorials plugin, User user, LocationStep locationStep, StepEditorMenu stepEditorMenu, String szStepName) + { this.plugin = plugin; this.user = user; this.locationStep = locationStep; this.stepEditorMenu = stepEditorMenu; this.szStepName = szStepName; - this.book = book; + + //Creates the book + this.book = new ItemStack(Material.WRITABLE_BOOK, 1); + + //Extracts the book meta reference, and sets the title + BookMeta videoLinkBookMeta = (BookMeta) this.book.getItemMeta(); + videoLinkBookMeta.setTitle(szStepName); + + //Adds the meta of the book back in + this.book.setItemMeta(videoLinkBookMeta); + } + + public ItemStack getBook() + { + return book; } public void register()