-Objective rework

This commit is contained in:
Zino 2014-01-05 10:31:21 +01:00
parent 65f8f72d9f
commit c9724bd02b
7 changed files with 393 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package me.blackvein.quests.objectives;
import java.util.List;
import me.blackvein.quests.Quester;
import me.blackvein.quests.Quests;
import me.blackvein.quests.Stage;
import me.blackvein.quests.exceptions.InvalidStageException;
import me.blackvein.quests.objectives.core.ObjectiveType;
import org.bukkit.configuration.ConfigurationSection;
public abstract class Objective {
public abstract boolean isFinished(Quester quester);
public abstract void setObjectives(Quester quester, Stage stage);
public abstract void initiageStage(Stage stage, ConfigurationSection section) throws InvalidStageException;
public abstract ObjectiveType getType();
public abstract List<String> getObjectiveMessages(Quester quester);
public void printSevere(String string) {
Quests.log.severe(string);
}
}

View File

@ -0,0 +1,118 @@
package me.blackvein.quests.objectives;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import me.blackvein.quests.Quester;
import me.blackvein.quests.Quests;
import me.blackvein.quests.Stage;
import me.blackvein.quests.exceptions.InvalidStageException;
import me.blackvein.quests.objectives.core.EventWrapper;
import me.blackvein.quests.objectives.core.ObjectiveEvent;
import me.blackvein.quests.objectives.core.ObjectiveType;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.block.BlockBreakEvent;
public class ObjectiveBreakBlock extends Objective {
private HashMap<Quester, HashMap<Material, Integer>> questerMap = new HashMap<Quester, HashMap<Material, Integer>>();
private HashMap<Stage, HashMap<Material, Integer>> stageMap = new HashMap<Stage, HashMap<Material, Integer>>();
@Override
public boolean isFinished(Quester quester) {
if (questerMap.containsKey(quester)) {
for (int i : questerMap.get(quester).values()) {
if (i > 0) return false;
}
return true;
}
return false;
}
@Override
public void setObjectives(Quester quester, Stage stage) {
if (stageMap.containsKey(stage)) {
questerMap.put(quester, stageMap.get(stage));
} else {
questerMap.remove(quester);
}
}
@Override
public void initiageStage(Stage stage, ConfigurationSection section) throws InvalidStageException {
List<Integer> breakIds = new LinkedList<Integer>();
List<Integer> breakAmounts = new LinkedList<Integer>();
if (section.contains("break-block-ids")) {
if (!section.getIntegerList("break-block-ids").isEmpty()) {
breakIds = section.getIntegerList("break-block-ids");
} else {
printSevere("[Quests] break-block-ids: in Stage " + stage.getIndex() + " of Quest " + stage.getQuestName() + " is not a list of numbers!");
throw new InvalidStageException(stage.getQuestName(), stage.getIndex());
}
if (section.contains("break-block-amounts")) {
if (!section.getIntegerList("break-block-amounts").isEmpty()) {
breakAmounts = section.getIntegerList("break-block-amounts");
} else {
printSevere("[Quests] break-block-amounts: in Stage " + stage.getIndex() + " of Quest " + stage.getQuestName() + " is not a list of numbers!");
throw new InvalidStageException(stage.getQuestName(), stage.getIndex());
}
} else {
printSevere("[Quests] Stage " + stage.getIndex() + " of Quest " + stage.getQuestName() + " is missing break-block-amounts:");
throw new InvalidStageException(stage.getQuestName(), stage.getIndex());
}
}
HashMap<Material, Integer> tempMap = new HashMap<Material, Integer>();
for (int i = 0; i < breakIds.size(); i++) {
if (i < breakAmounts.size()) {
Material m = Material.getMaterial(breakIds.get(i));
int amounts = breakAmounts.get(i);
tempMap.put(m, amounts);
}
}
if (!tempMap.isEmpty()) {
stageMap.put(stage, tempMap);
stage.getObjectives().add(getType());
}
}
@Override
public ObjectiveType getType() {
return ObjectiveType.BREAK_BLOCK;
}
@ObjectiveEvent(BlockBreakEvent.class)
public void executeEvent(EventWrapper wrapper) {
BlockBreakEvent event = (BlockBreakEvent) wrapper.getEvent();
if (event.getPlayer().getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH) == true || event.isCancelled() == true) {
return;
}
Quester quester = Quests.getInstance().getQuester(event.getPlayer().getName());
if (questerMap.containsKey(quester)) {
Material material = event.getBlock().getType();
if (questerMap.get(quester).containsKey(material)) {
int toBreak = questerMap.get(quester).get(material) - 1;
questerMap.get(quester).put(material, toBreak);
if (toBreak <= 0) {
int totalBlocksToBeak = stageMap.get(quester.currentStage).get(material);
String finishMessage = ChatColor.GREEN + "(Completed) Break " + Quester.prettyItemString(material) + " " + totalBlocksToBeak + "/" + totalBlocksToBeak;
quester.finishObjective(this, finishMessage);
}
}
}
}
}

View File

@ -0,0 +1,118 @@
package me.blackvein.quests.objectives;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import me.blackvein.quests.Quester;
import me.blackvein.quests.Quests;
import me.blackvein.quests.Stage;
import me.blackvein.quests.exceptions.InvalidStageException;
import me.blackvein.quests.objectives.core.EventWrapper;
import me.blackvein.quests.objectives.core.ObjectiveEvent;
import me.blackvein.quests.objectives.core.ObjectiveType;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
public class ObjectiveDamageBlock extends Objective{
private HashMap<Quester, HashMap<Material, Integer>> questerMap = new HashMap<Quester, HashMap<Material, Integer>>();
private HashMap<Stage, HashMap<Material, Integer>> stageMap = new HashMap<Stage, HashMap<Material, Integer>>();
@Override
public boolean isFinished(Quester quester) {
if (questerMap.containsKey(quester)) {
for (int i : questerMap.get(quester).values()) {
if (i > 0) return false;
}
return true;
}
return false;
}
@Override
public void setObjectives(Quester quester, Stage stage) {
if (stageMap.containsKey(stage)) {
questerMap.put(quester, stageMap.get(stage));
} else {
questerMap.remove(quester);
}
}
@Override
public void initiageStage(Stage stage, ConfigurationSection section) throws InvalidStageException {
List<Integer> damageIds = new LinkedList<Integer>();
List<Integer> damageAmounts = new LinkedList<Integer>();
if (section.contains("damage-block-ids")) {
if (!section.getIntegerList("damage-block-ids").isEmpty()) {
damageIds = section.getIntegerList("damage-block-ids");
} else {
printSevere("[Quests] damage-block-ids: in Stage " + stage.getIndex() + " of Quest " + stage.getQuestName() + " is not a list of numbers!");
throw new InvalidStageException(stage.getQuestName(), stage.getIndex());
}
if (section.contains("damage-block-amounts")) {
if (!section.getIntegerList("damage-block-amounts").isEmpty()) {
damageAmounts = section.getIntegerList("damage-block-amounts");
} else {
printSevere("[Quests] damage-block-amounts: in Stage " + stage.getIndex() + " of Quest " + stage.getQuestName() + " is not a list of numbers!");
throw new InvalidStageException(stage.getQuestName(), stage.getIndex());
}
} else {
printSevere("[Quests] Stage " + stage.getIndex() + " of Quest " + stage.getQuestName() + " is missing damage-block-amounts:");
throw new InvalidStageException(stage.getQuestName(), stage.getIndex());
}
}
HashMap<Material, Integer> tempMap = new HashMap<Material, Integer>();
for (int i = 0; i < damageIds.size(); i++) {
if (i < damageAmounts.size()) {
Material m = Material.getMaterial(damageIds.get(i));
int amounts = damageAmounts.get(i);
tempMap.put(m, amounts);
}
}
if (!tempMap.isEmpty()) {
stageMap.put(stage, tempMap);
stage.getObjectives().add(getType());
}
}
@ObjectiveEvent(BlockDamageEvent.class)
public void onDamageBlock(EventWrapper wrapper) {
BlockDamageEvent event = (BlockDamageEvent) wrapper.getEvent();
Quester quester = Quests.getInstance().getQuester(event.getPlayer().getName());
if (questerMap.containsKey(quester)) {
Material material = event.getBlock().getType();
if (questerMap.get(quester).containsKey(material)) {
int toDamage = questerMap.get(quester).get(material) - 1;
questerMap.get(quester).put(material, toDamage);
if (toDamage <= 0) {
int totalBlocksToDamage = stageMap.get(quester.currentStage).get(material);
String finishMessage = ChatColor.GREEN + "(Completed) Damage " + Quester.prettyItemString(material) + " " + totalBlocksToDamage + "/" + totalBlocksToDamage;
quester.finishObjective(this, finishMessage);
}
}
}
}
@Override
public ObjectiveType getType() {
return ObjectiveType.DAMAGE_BLOCK;
}
}

View File

@ -0,0 +1,17 @@
package me.blackvein.quests.objectives.core;
import org.bukkit.event.Event;
public class EventWrapper {
private Event event;
public EventWrapper(Event event) {
this.event = event;
}
public Event getEvent() {
return event;
}
}

View File

@ -0,0 +1,15 @@
package me.blackvein.quests.objectives.core;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.bukkit.event.Event;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ObjectiveEvent {
public Class<? extends Event> value();
}

View File

@ -0,0 +1,78 @@
package me.blackvein.quests.objectives.core;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map.Entry;
import me.blackvein.quests.Quester;
import me.blackvein.quests.Stage;
import me.blackvein.quests.exceptions.InvalidStageException;
import me.blackvein.quests.objectives.Objective;
import me.blackvein.quests.objectives.ObjectiveBreakBlock;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.Event;
public class ObjectiveHandler {
private static HashMap<Class<? extends Event>, HashMap<Class<? extends Objective>, Method>> eventMap = new HashMap<Class<? extends Event>, HashMap<Class<? extends Objective>, Method>>();
private static HashMap<Class<? extends Objective>, Objective> objectives = new HashMap<Class<? extends Objective>, Objective>();
public ObjectiveHandler() {
this.registerClasses();
}
public static void executeEvent(Event event) {
if (eventMap.containsKey(event.getClass())) {
EventWrapper wrapper = new EventWrapper(event);
for (Entry<Class<? extends Objective>, Method> entry : eventMap.get(event.getClass()).entrySet()) {
Objective objective = objectives.get(entry.getKey());
try {
entry.getValue().invoke(objective, new Object[] {wrapper});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static Objective getObjective(ObjectiveType type) {
return objectives.get(type);
}
public static void initStage(Stage oStage, ConfigurationSection section) {
for (Objective ob : objectives.values()) {
try {
ob.initiageStage(oStage, section);
} catch (InvalidStageException e) {
return;
}
}
}
public void registerClasses() {
try {
registerClass(ObjectiveBreakBlock.class);
} catch (Exception e) {
e.printStackTrace();
}
}
private void registerClass(Class<? extends Objective> module) throws InstantiationException, IllegalAccessException {
objectives.put(module, module.newInstance());
for (Method method : module.getMethods()) {
if (method.isAnnotationPresent(ObjectiveEvent.class)) {
ObjectiveEvent objectiveEvent = method.getAnnotation(ObjectiveEvent.class);
Class<? extends Event> eventClass = objectiveEvent.value();
if (!eventMap.containsKey(eventClass)) {
HashMap<Class<? extends Objective>, Method> tempMap = new HashMap<Class<? extends Objective>, Method>();
eventMap.put(eventClass, tempMap);
}
eventMap.get(eventClass).put(module, method);
}
}
}
}

View File

@ -0,0 +1,22 @@
package me.blackvein.quests.objectives.core;
import me.blackvein.quests.objectives.Objective;
import me.blackvein.quests.objectives.ObjectiveBreakBlock;
import me.blackvein.quests.objectives.ObjectiveDamageBlock;
public enum ObjectiveType {
BREAK_BLOCK(ObjectiveBreakBlock.class),
DAMAGE_BLOCK(ObjectiveDamageBlock.class);
private Class<? extends Objective> type;
ObjectiveType(Class<? extends Objective> type) {
this.type = type;
}
public Class<? extends Objective> getType() {
return type;
}
}