Skip to content

Commit

Permalink
Merge pull request #118 from BTEUK/WorldEdit-vritual-blocks-not-displ…
Browse files Browse the repository at this point in the history
…ayed-on-lesson-continuation

Overhaul virtual blocks mechanism
  • Loading branch information
george112n authored Aug 15, 2024
2 parents 434f979 + ac732e4 commit 779d3cf
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 123 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

<name>TeachingTutorials</name>
Expand Down
39 changes: 29 additions & 10 deletions src/main/java/teachingtutorials/TeachingTutorials.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -53,12 +54,27 @@ public class TeachingTutorials extends JavaPlugin
//A list of all ongoing location creations
public ArrayList<NewLocation> newLocations;

//A list of all virtual blocks
public ConcurrentHashMap<VirtualBlockLocation, BlockData> virtualBlocks;
//A list of all virtual block groups. Each task's virtual blocks are stored in a group and placed here when active
private ArrayList<VirtualBlockGroup<org.bukkit.Location, BlockData>> virtualBlockGroups;

//Identifies which world edit is being used
public WorldEditImplementation worldEditImplementation;

public void addVirtualBlocks(VirtualBlockGroup<org.bukkit.Location, BlockData> virtualBlocks)
{
virtualBlockGroups.add(virtualBlocks);
}

public void removeVirtualBlocks(VirtualBlockGroup<org.bukkit.Location, BlockData> virtualBlocks)
{
virtualBlockGroups.remove(virtualBlocks);
}

public ArrayList<VirtualBlockGroup<org.bukkit.Location, BlockData>> getVirtualBlockGroups()
{
return virtualBlockGroups;
}

@Override
public void onEnable()
{
Expand Down Expand Up @@ -116,7 +132,7 @@ else if (Bukkit.getPluginManager().isPluginEnabled("WorldEdit"))
players = new ArrayList<>();
lessons = new ArrayList<>();
newLocations = new ArrayList<>();
virtualBlocks = new ConcurrentHashMap<>();
virtualBlockGroups = new ArrayList<>();

//-------------------------------------------------------------------------
//----------------------------------MySQL----------------------------------
Expand Down Expand Up @@ -209,7 +225,6 @@ else if (!learningMenuSlot.equals(menu))
//----------Sets up event check----------
//---------------------------------------

//3 second timer - checks events
this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run()
{
Expand Down Expand Up @@ -263,14 +278,18 @@ public void run()
//----------------------------------------
this.getServer().getScheduler().scheduleSyncRepeatingTask(this, () ->
{
int iSize;
iSize = virtualBlocks.size();
VirtualBlockLocation[] locations = virtualBlocks.keySet().toArray(VirtualBlockLocation[]::new);
BlockData[] blockData = virtualBlocks.values().toArray(BlockData[]::new);
//Declares the temporary list object
VirtualBlockGroup<Location, BlockData> virtualBlockGroup;

for (int i = 0 ; i < iSize ; i++)
//Goes through all virtual block groups
int iTasksActive = virtualBlockGroups.size();
for (int j = 0 ; j < iTasksActive ; j++)
{
locations[i].sendUpdate(blockData[i]);
//Extracts the jth virtual block group
virtualBlockGroup = virtualBlockGroups.get(j);

//Calls for the blocks to be displayed
virtualBlockGroup.displayBlocks();
}
}, 0, 10);

Expand Down
27 changes: 17 additions & 10 deletions src/main/java/teachingtutorials/TutorialPlaythrough.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import teachingtutorials.tutorials.Tutorial;
import teachingtutorials.utils.Mode;
import teachingtutorials.utils.User;
import teachingtutorials.utils.VirtualBlockLocation;
import teachingtutorials.utils.VirtualBlockGroup;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

//To be extended by lesson and new location
//They both share a lot of data and processes, and they are also rather similar in the user experience as well
Expand Down Expand Up @@ -90,17 +89,25 @@ protected void commonEndPlaythrough()
fallListener.unregister();

//Removes virtual blocks
ConcurrentHashMap<VirtualBlockLocation, BlockData> virtualBlocks = plugin.virtualBlocks;
int iSize;
iSize = virtualBlocks.size();
VirtualBlockLocation[] locations = virtualBlocks.keySet().toArray(VirtualBlockLocation[]::new);
ArrayList<VirtualBlockGroup<org.bukkit.Location, BlockData>> virtualBlockGroups = plugin.getVirtualBlockGroups();

for (int i = 0 ; i < iSize ; i++)
VirtualBlockGroup<org.bukkit.Location, BlockData> virtualBlockGroup;

//Goes through the list of the plugins active virtual block groups
for (int i = 0 ; i < virtualBlockGroups.size() ; i++)
{
if (locations[i].isFromTutorial(this))
virtualBlockGroup = virtualBlockGroups.get(i);

//Checks whether the virtual block group is of this tutorial playthrough
if (virtualBlockGroup.isOfPlaythrough(this))
{
virtualBlocks.remove(locations[i]);
locations[i].removeAndReset();
//Removes the list from the plugin's list of lists
this.plugin.removeVirtualBlocks(virtualBlockGroup);

//Resets the blocks back to the original state for the players
virtualBlockGroup.removeBlocks();

i--;
}
}

Expand Down
9 changes: 0 additions & 9 deletions src/main/java/teachingtutorials/fundamentalTasks/Command.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
package teachingtutorials.fundamentalTasks;

import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extension.platform.permission.ActorSelectorLimits;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
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 org.bukkit.util.BlockVector;
import teachingtutorials.TeachingTutorials;
import teachingtutorials.newlocation.DifficultyListener;
import teachingtutorials.tutorials.Group;
import teachingtutorials.tutorials.LocationTask;
import teachingtutorials.utils.Display;
import teachingtutorials.utils.VirtualBlock;
import teachingtutorials.utils.WorldEdit;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

enum commandType
{
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/teachingtutorials/fundamentalTasks/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import teachingtutorials.tutorials.Group;
import teachingtutorials.tutorials.LocationTask;
import teachingtutorials.utils.Display;
import teachingtutorials.utils.VirtualBlock;


public class Place extends Task implements Listener
Expand Down Expand Up @@ -53,7 +52,7 @@ public Place(TeachingTutorials plugin, Player player, Group parentGroup, int iTa
this.fDifficulty = fDifficulty;

//Calculates the virtual block
calculateVirtualBlocks();
addVirtualBlock();
}

/**
Expand Down Expand Up @@ -146,8 +145,8 @@ private void blockPlaced(Location newBlockLocation, Material newBlockMaterial)
//SpotHit is then called from inside the difficulty listener once the difficulty has been established
//This is what moves it onto the next task

//Calculates the list of virtual blocks
calculateVirtualBlocks();
//Adds the virtual block
addVirtualBlock();

//Displays the virtual blocks
displayVirtualBlocks();
Expand Down Expand Up @@ -229,11 +228,9 @@ public void newLocationSpotHit()
/**
* Uses the target coords and target material to calculate the virtual block
*/
public void calculateVirtualBlocks()
public void addVirtualBlock()
{
VirtualBlock virtualPlaceBlock = new VirtualBlock(this.parentGroup.parentStep.parentStage.tutorialPlaythrough, player, player.getWorld(),
iTargetCoords[0], iTargetCoords[1], iTargetCoords[2],
mTargetMaterial.createBlockData());
this.virtualBlocks.put(virtualPlaceBlock.blockLocation, virtualPlaceBlock.blockData);
Location location = new Location(this.parentGroup.parentStep.parentStage.tutorialPlaythrough.getLocation().getWorld(), iTargetCoords[0], iTargetCoords[1], iTargetCoords[2]);
this.virtualBlocks.put(location, mTargetMaterial.createBlockData());
}
}
31 changes: 8 additions & 23 deletions src/main/java/teachingtutorials/fundamentalTasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import teachingtutorials.TeachingTutorials;
import teachingtutorials.tutorials.Group;
import teachingtutorials.tutorials.Lesson;
import teachingtutorials.utils.VirtualBlockLocation;
import teachingtutorials.utils.VirtualBlockGroup;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

public class Task
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public class Task
/**
* A list of virtual blocks which are to be displayed as a result of task completion
*/
protected ConcurrentHashMap<VirtualBlockLocation, BlockData> virtualBlocks;
protected VirtualBlockGroup<Location, BlockData> virtualBlocks;

public void register()
{
Expand Down Expand Up @@ -93,7 +93,7 @@ public Task(TeachingTutorials plugin, Player player, Group parentGroup, int iTas
this.szDetails = szDetails;
this.type = szType;

this.virtualBlocks = new ConcurrentHashMap<>();
this.virtualBlocks = new VirtualBlockGroup<>(this.parentGroup.parentStep.parentStage.tutorialPlaythrough);

this.bCreatingNewLocation = bCreatingNewLocation;
}
Expand Down Expand Up @@ -124,34 +124,19 @@ public void unregister()
}

/**
* Adds all of the virtual blocks of this task to the plugin's virtual blocks list
* Adds the list of virtual blocks of this task to the plugin's virtual blocks list
*/
public void displayVirtualBlocks()
{
int iSize = virtualBlocks.size();
VirtualBlockLocation[] locations = virtualBlocks.keySet().toArray(VirtualBlockLocation[]::new);
BlockData[] blockData = virtualBlocks.values().toArray(BlockData[]::new);

for (int i = 0; i < iSize; i++)
{
//The put will overwrite any existing virtual blocks at this location
plugin.virtualBlocks.put(locations[i], blockData[i]);
}
plugin.addVirtualBlocks(virtualBlocks);
}

/**
* Removes all of the virtual blocks of this task from the plugin's virtual blocks list
* Removes the list of virtual blocks of this task from the plugin's list of virtual block groups
*/
protected void removeVirtualBlocks()
{
int iSize = virtualBlocks.size();
VirtualBlockLocation[] locations = virtualBlocks.keySet().toArray(VirtualBlockLocation[]::new);

for (int i = 0; i < iSize; i++)
{
plugin.virtualBlocks.remove(locations[i]);
}
plugin.getLogger().info(ChatColor.AQUA +"All virtual blocks from task with task id " +iTaskID +" removed");
plugin.removeVirtualBlocks(virtualBlocks);
}

public void newLocationSpotHit()
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/teachingtutorials/tutorials/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ public void terminateEarly()
currentTask.unregister();
}

/**
* Retrieves from the database the list of groups for the specified step
* @param player The player playing through the tutorial
* @param plugin The instance of the plugin
* @param step The stage for which all groups must be retrieved
* @return A list of groups for this step
*/
public static ArrayList<Group> fetchGroupsByStepID(Player player, TeachingTutorials plugin, Step step)
{
ArrayList<Group> groups = new ArrayList<>();
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/teachingtutorials/tutorials/Lesson.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ else if (creatorOrStudent.bInLesson)
return true;
}

//Resumes a previously started lesson
/**
* Resumes a previously started lesson
* @param bResetProgress Whether to reset the progress to stage 1 step 1
* @return Whether or not the lesson resumed successfully
*/
private boolean resumeLesson(boolean bResetProgress)
{
//Fetches the tutorial ID, the location, the stage and the step the player is at in their current tutorial
Expand All @@ -150,12 +154,16 @@ private boolean resumeLesson(boolean bResetProgress)
+", Tutorial ID = " +this.tutorial.getTutorialID()
+" and LocationID = "+this.location.getLocationID());

//Redisplays all virtual blocks
//Redisplays virtual blocks of steps already completed
//Goes through all stages up to the current
for (int i = 0 ; i < iStageIndex ; i++)
{
//Checks whether we are at their current stage or not
if (i != iStageIndex - 1)
//If this is not the current stage, display virtual blocks of all steps
stages.get(i).displayAllVirtualBlocks(0);
else
//Displays virtual blocks up to the step they are on
stages.get(i).displayAllVirtualBlocks(iStepToStart);
}

Expand Down
16 changes: 14 additions & 2 deletions src/main/java/teachingtutorials/tutorials/Stage.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,36 @@ public boolean isFirstStage()
return (iOrder == 1);
}

/**
* Displays the virtual blocks of all tasks in all steps of this stage up to but not including the iStepToStartTH step
* @param iStepToStart The step at which to start the lesson at (this is not zero indexed. The first step has iStepToStart = 1).
* <p> </p>
* For clarity, if iStepToStart is 1, no virtual blocks from will be displayed from this stage.
* <p>If iStepToStart = 2, only the virtual blocks from the first step (and all previous stages) will be displayed at the start of the lesson.</p>
*/
public void displayAllVirtualBlocks(int iStepToStart)
{
//Gets the steps from the DB
//Gets the steps of this stage from the DB
fetchAndInitialiseSteps();

//The number of steps to display the virtual blocks for
int iNumSteps;

if (iStepToStart == 0)
if (iStepToStart <= 1)
iNumSteps = steps.size();
else
iNumSteps = iStepToStart - 1;

//Calls each step to display all relevant virtual blocks
for (int i = 0 ; i < iNumSteps ; i++)
{
steps.get(i).displayAllVirtualBlocks();
}
}

/**
* Fetches the list of steps of this stage from the database and stores this in {@link #steps}. The list is ordered.
*/
private void fetchAndInitialiseSteps()
{
//Gets a list of all of the steps of the specified stage and loads each with the relevant data.
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/teachingtutorials/tutorials/Step.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public boolean getSelectionCompleteHold()
return selectionCompleteHold;
}

/**
* Displays the virtual blocks of all tasks is this step
*/
public void displayAllVirtualBlocks()
{
//Gets the groups from the DB
Expand All @@ -125,6 +128,11 @@ public void displayAllVirtualBlocks()
}
}

/**
* Fetches the list of groups of this step from the database and stores this in {@link #groups}.
* <p> </p>
* The order of groups does not matter and as such the order that they are fetched and stored in doesn't matter.
*/
private void fetchAndInitialiseGroups()
{
groups = Group.fetchGroupsByStepID(player, plugin, this);
Expand Down
Loading

0 comments on commit 779d3cf

Please sign in to comment.