Store a set of active members for objective goals

This commit is contained in:
Shaun Bennett 2016-01-04 15:56:33 -05:00
parent ce8586e5c5
commit 05ef4d30c7
10 changed files with 245 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import java.util.List;
import java.util.UUID;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
/**
* An Objective represents
@ -13,14 +14,16 @@ import org.bukkit.entity.Player;
*/
public abstract class Objective<Data extends ObjectiveData>
{
private JavaPlugin _plugin;
private String _name;
private String _description;
private HashMap<UUID, Data> _active;
private List<ObjectiveListener> _listeners;
public Objective(String name, String description)
public Objective(JavaPlugin plugin, String name, String description)
{
_plugin = plugin;
_name = name;
_description = description;
@ -28,6 +31,11 @@ public abstract class Objective<Data extends ObjectiveData>
_listeners = new LinkedList<>();
}
public JavaPlugin getPlugin()
{
return _plugin;
}
/**
* Get the name of this Objective
*/
@ -70,7 +78,7 @@ public abstract class Objective<Data extends ObjectiveData>
Data data = getData(player);
_active.put(player.getUniqueId(), data);
_listeners.forEach(listener -> listener.start(player, this));
_listeners.forEach(listener -> listener.startObjective(player, this));
customStart(player);
}
@ -109,10 +117,15 @@ public abstract class Objective<Data extends ObjectiveData>
{
_active.remove(player.getUniqueId());
_listeners.forEach(listener -> listener.finish(player, this));
_listeners.forEach(listener -> listener.finishObjective(player, this));
customFinish(player);
}
protected abstract void customFinish(Player player);
/**
* Unregister all listeners associated with this Objective
*/
public abstract void unregisterAll();
}

View File

@ -1,15 +1,40 @@
package mineplex.core.common.objective;
public abstract class ObjectiveGoal
import java.util.HashSet;
import java.util.UUID;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
public abstract class ObjectiveGoal implements Listener
{
private Objective _objective;
private HashSet<UUID> _active;
private String _name;
private String _description;
public ObjectiveGoal(Objective objective)
public ObjectiveGoal(Objective objective, String name, String description)
{
_objective = objective;
_active = new HashSet<>();
_name = name;
_description = description;
}
public String getName()
{
return _name;
}
public String getDescription()
{
return _description;
}
public boolean contains(Player player)
{
return _active.contains(player.getUniqueId());
}
}

View File

@ -4,7 +4,7 @@ import org.bukkit.entity.Player;
public interface ObjectiveListener
{
public void start(Player player, Objective objective);
public void startObjective(Player player, Objective objective);
public void finish(Player player, Objective objective);
public void finishObjective(Player player, Objective objective);
}

View File

@ -0,0 +1,63 @@
package mineplex.core.common.objective;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;
public class OrderedObjective extends Objective<OrderedObjectiveData>
{
private List<ObjectiveGoal> _goals;
public OrderedObjective(JavaPlugin plugin, String name, String description)
{
super(plugin, name, description);
_goals = new ArrayList<>();
}
private void addGoal(ObjectiveGoal goal)
{
_goals.add(goal);
getPlugin().getServer().getPluginManager().registerEvents(goal, getPlugin());
}
@Override
protected void customStart(Player player)
{
}
@Override
protected void customLeave(Player player)
{
}
@Override
protected OrderedObjectiveData getData(Player player)
{
return new OrderedObjectiveData();
}
@Override
protected void completeGoal(ObjectiveGoal goal, Player player)
{
}
@Override
protected void customFinish(Player player)
{
}
@Override
public void unregisterAll()
{
_goals.forEach(HandlerList::unregisterAll);
}
}

View File

@ -0,0 +1,11 @@
package mineplex.core.common.objective;
public class OrderedObjectiveData extends ObjectiveData
{
private int _index;
public OrderedObjectiveData()
{
_index = 0;
}
}

View File

@ -1,18 +1,75 @@
package mineplex.game.clans.tutorial;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.objective.Objective;
import mineplex.core.common.objective.ObjectiveListener;
public class Tutorial
public abstract class Tutorial implements Listener, ObjectiveListener
{
private HashMap<Player, TutorialSession> _playerSessionMap;
private JavaPlugin _plugin;
private String _name;
private String _taskIdentifier;
private List<Objective> _objectiveGroups;
private List<Objective> _objectives;
public Tutorial(JavaPlugin plugin, String name, String taskIdentifier)
{
_plugin = plugin;
_name = name;
_taskIdentifier = taskIdentifier;
_objectives = new ArrayList<>();
}
protected void addObjective(Objective objective)
{
_objectives.add(objective);
}
public void start(Player player)
{
_playerSessionMap.put(player, new TutorialSession());
}
public boolean isInTutorial(Player player)
{
return _playerSessionMap.containsKey(player);
}
public JavaPlugin getPlugin()
{
return _plugin;
}
public String getName()
{
return _name;
}
public final String getTaskIdentifier()
{
return "clans.tutorial." + _taskIdentifier;
}
@Override
public void finishObjective(Player player, Objective objective)
{
int index = _objectives.indexOf(objective);
}
public void unregisterAll()
{
HandlerList.unregisterAll(this);
_objectives.forEach(Objective::unregisterAll);
}
}

View File

@ -1,15 +1,58 @@
package mineplex.game.clans.tutorial;
import java.util.EnumMap;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin;
import mineplex.game.clans.tutorial.tutorials.combat.CombatTutorial;
public class TutorialManager extends MiniPlugin
{
private EnumMap<TutorialType, Tutorial> _tutorialMap;
public TutorialManager(JavaPlugin plugin)
{
super("Clans Tutorial", plugin);
_tutorialMap = new EnumMap<TutorialType, Tutorial>(TutorialType.class);
addTutorial(TutorialType.COMBAT, new CombatTutorial());
}
private void addTutorial(TutorialType type, Tutorial tutorial)
{
if (_tutorialMap.containsKey(type))
{
_tutorialMap.remove(type).unregisterAll();
}
_tutorialMap.put(type, tutorial);
getPlugin().getServer().getPluginManager().registerEvents(tutorial, getPlugin());
}
public boolean inTutorial(Player player)
{
for (Tutorial tutorial : _tutorialMap.values())
{
if (tutorial.isInTutorial(player))
return true;
}
return false;
}
public boolean startTutorial(Player player, TutorialType type)
{
if (_tutorialMap.containsKey(type))
{
_tutorialMap.get(type).start(player);
return true;
}
return false;
}
}

View File

@ -2,6 +2,10 @@ package mineplex.game.clans.tutorial;
public class TutorialSession
{
private int _objectiveGroupIndex;
private int _objectiveIndex;
public TutorialSession()
{
}
}

View File

@ -0,0 +1,6 @@
package mineplex.game.clans.tutorial;
public enum TutorialType
{
COMBAT
}

View File

@ -0,0 +1,13 @@
package mineplex.game.clans.tutorial.tutorials.combat;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.game.clans.tutorial.Tutorial;
public class CombatTutorial extends Tutorial
{
public CombatTutorial(JavaPlugin plugin)
{
super(plugin, "Combat Tutorial", "combat");
}
}