diff --git a/src/main/java/me/blackvein/quests/objectives/Objective.java b/src/main/java/me/blackvein/quests/objectives/Objective.java new file mode 100644 index 000000000..9c3199ee7 --- /dev/null +++ b/src/main/java/me/blackvein/quests/objectives/Objective.java @@ -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 getObjectiveMessages(Quester quester); + + public void printSevere(String string) { + Quests.log.severe(string); + } + +} diff --git a/src/main/java/me/blackvein/quests/objectives/ObjectiveBreakBlock.java b/src/main/java/me/blackvein/quests/objectives/ObjectiveBreakBlock.java new file mode 100644 index 000000000..4674b106c --- /dev/null +++ b/src/main/java/me/blackvein/quests/objectives/ObjectiveBreakBlock.java @@ -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> questerMap = new HashMap>(); + private HashMap> stageMap = new HashMap>(); + + + @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 breakIds = new LinkedList(); + List breakAmounts = new LinkedList(); + + 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 tempMap = new HashMap(); + 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); + } + } + } + } +} diff --git a/src/main/java/me/blackvein/quests/objectives/ObjectiveDamageBlock.java b/src/main/java/me/blackvein/quests/objectives/ObjectiveDamageBlock.java new file mode 100644 index 000000000..7b66a1ac5 --- /dev/null +++ b/src/main/java/me/blackvein/quests/objectives/ObjectiveDamageBlock.java @@ -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> questerMap = new HashMap>(); + private HashMap> stageMap = new HashMap>(); + + @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 damageIds = new LinkedList(); + List damageAmounts = new LinkedList(); + + 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 tempMap = new HashMap(); + 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; + } + +} diff --git a/src/main/java/me/blackvein/quests/objectives/core/EventWrapper.java b/src/main/java/me/blackvein/quests/objectives/core/EventWrapper.java new file mode 100644 index 000000000..4a0b00c1f --- /dev/null +++ b/src/main/java/me/blackvein/quests/objectives/core/EventWrapper.java @@ -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; + } +} diff --git a/src/main/java/me/blackvein/quests/objectives/core/ObjectiveEvent.java b/src/main/java/me/blackvein/quests/objectives/core/ObjectiveEvent.java new file mode 100644 index 000000000..638940466 --- /dev/null +++ b/src/main/java/me/blackvein/quests/objectives/core/ObjectiveEvent.java @@ -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 value(); + +} diff --git a/src/main/java/me/blackvein/quests/objectives/core/ObjectiveHandler.java b/src/main/java/me/blackvein/quests/objectives/core/ObjectiveHandler.java new file mode 100644 index 000000000..9e1fa2733 --- /dev/null +++ b/src/main/java/me/blackvein/quests/objectives/core/ObjectiveHandler.java @@ -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, HashMap, Method>> eventMap = new HashMap, HashMap, Method>>(); + private static HashMap, Objective> objectives = new HashMap, Objective>(); + + public ObjectiveHandler() { + this.registerClasses(); + } + + public static void executeEvent(Event event) { + if (eventMap.containsKey(event.getClass())) { + EventWrapper wrapper = new EventWrapper(event); + for (Entry, 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 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 eventClass = objectiveEvent.value(); + + if (!eventMap.containsKey(eventClass)) { + HashMap, Method> tempMap = new HashMap, Method>(); + eventMap.put(eventClass, tempMap); + } + eventMap.get(eventClass).put(module, method); + } + } + } +} diff --git a/src/main/java/me/blackvein/quests/objectives/core/ObjectiveType.java b/src/main/java/me/blackvein/quests/objectives/core/ObjectiveType.java new file mode 100644 index 000000000..c462238ab --- /dev/null +++ b/src/main/java/me/blackvein/quests/objectives/core/ObjectiveType.java @@ -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 type; + + ObjectiveType(Class type) { + this.type = type; + } + + public Class getType() { + return type; + } + +}