Added the ability to put a range in for quest types

This commit is contained in:
Teriuihi 2022-06-03 03:29:18 +02:00
parent c3fa337d2a
commit cdff96fb4d
11 changed files with 91 additions and 50 deletions

View File

@ -48,9 +48,10 @@ public class QuestsConfig extends AbstractConfig {
MINE_QUESTS.add(new MineQuestObject(key, MINE_QUESTS.add(new MineQuestObject(key,
configurationSection.getString(key + ".name"), configurationSection.getString(key + ".name"),
material, material,
configurationSection.getInt(key + ".amount"),
configurationSection.getStringList(key + ".quest-pages"), configurationSection.getStringList(key + ".quest-pages"),
configurationSection.getStringList(key + ".done-pages"))); configurationSection.getStringList(key + ".done-pages"),
configurationSection.getInt(key + ".amount-min"),
configurationSection.getInt(key + ".amount-max")));
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -84,9 +85,10 @@ public class QuestsConfig extends AbstractConfig {
KILL_MOB_QUEST.add(new KillMobsQuestObject(key, KILL_MOB_QUEST.add(new KillMobsQuestObject(key,
configurationSection.getString(key + ".name"), configurationSection.getString(key + ".name"),
entityType, entityType,
configurationSection.getInt(key + ".amount"),
configurationSection.getStringList(key + ".quest-pages"), configurationSection.getStringList(key + ".quest-pages"),
configurationSection.getStringList(key + ".done-pages"))); configurationSection.getStringList(key + ".done-pages"),
configurationSection.getInt(key + ".amount-min"),
configurationSection.getInt(key + ".amount-max")));
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -120,9 +122,10 @@ public class QuestsConfig extends AbstractConfig {
COLLECT_DROPS_QUEST.add(new CollectDropsQuestObject(key, COLLECT_DROPS_QUEST.add(new CollectDropsQuestObject(key,
configurationSection.getString(key + ".name"), configurationSection.getString(key + ".name"),
item, item,
configurationSection.getInt(key + ".amount"),
configurationSection.getStringList(key + ".quest-pages"), configurationSection.getStringList(key + ".quest-pages"),
configurationSection.getStringList(key + ".done-pages"))); configurationSection.getStringList(key + ".done-pages"),
configurationSection.getInt(key + ".amount-min"),
configurationSection.getInt(key + ".amount-max")));
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -156,9 +159,10 @@ public class QuestsConfig extends AbstractConfig {
BREED_MOB_QUEST.add(new BreedMobsQuestObject(key, BREED_MOB_QUEST.add(new BreedMobsQuestObject(key,
configurationSection.getString(key + ".name"), configurationSection.getString(key + ".name"),
entityType, entityType,
configurationSection.getInt(key + ".amount"),
configurationSection.getStringList(key + ".quest-pages"), configurationSection.getStringList(key + ".quest-pages"),
configurationSection.getStringList(key + ".done-pages"))); configurationSection.getStringList(key + ".done-pages"),
configurationSection.getInt(key + ".amount-min"),
configurationSection.getInt(key + ".amount-max")));
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -3,6 +3,7 @@ package com.alttd.altitudequests.objects;
import com.alttd.altitudequests.AQuest; import com.alttd.altitudequests.AQuest;
import com.alttd.altitudequests.config.Config; import com.alttd.altitudequests.config.Config;
import com.alttd.altitudequests.config.MessagesConfig; import com.alttd.altitudequests.config.MessagesConfig;
import com.alttd.altitudequests.database.Database;
import com.alttd.altitudequests.objects.quests.BreedMobsQuest; import com.alttd.altitudequests.objects.quests.BreedMobsQuest;
import com.alttd.altitudequests.objects.quests.CollectDropsQuest; import com.alttd.altitudequests.objects.quests.CollectDropsQuest;
import com.alttd.altitudequests.objects.quests.KillMobsQuest; import com.alttd.altitudequests.objects.quests.KillMobsQuest;
@ -20,6 +21,9 @@ import org.bukkit.scheduler.BukkitRunnable;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -42,6 +46,7 @@ public abstract class Quest {
private final Variant variant; private final Variant variant;
private boolean isDone; private boolean isDone;
private boolean rewardReceived; private boolean rewardReceived;
private final int amount;
public Quest(UUID uuid, int step1, int step2, Variant variant, boolean rewardReceived) { public Quest(UUID uuid, int step1, int step2, Variant variant, boolean rewardReceived) {
this.uuid = uuid; this.uuid = uuid;
@ -50,6 +55,28 @@ public abstract class Quest {
this.variant = variant; this.variant = variant;
this.isDone = rewardReceived; this.isDone = rewardReceived;
this.rewardReceived = rewardReceived; this.rewardReceived = rewardReceived;
if (variant == null)
amount = 0;
else
amount = variant.calculateAmount(loadQuestsDoneThisMonth(uuid));
}
private int loadQuestsDoneThisMonth(UUID uuid) {
String sql = "SELECT COUNT(uuid) AS total FROM quest_log WHERE uuid = ? AND year = ? AND month = ?";
try {
PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql);
statement.setString(1, uuid.toString());
Calendar instance = Calendar.getInstance();
instance.setTime(new Date());
statement.setInt(2, instance.get(Calendar.YEAR));
statement.setInt(3, instance.get(Calendar.MONTH));
ResultSet resultSet = statement.executeQuery();
if (resultSet.next())
return resultSet.getInt("total");
} catch (SQLException exception) {
exception.printStackTrace();
}
return 0;
} }
public static void createDailyQuest(Player player) { public static void createDailyQuest(Player player) {
@ -173,7 +200,7 @@ public abstract class Quest {
protected void checkDone() { protected void checkDone() {
if (isDone()) if (isDone())
return; return;
if (getStep1() == variant.getAmount() && getStep2() == variant.getAmount()) { if (getStep1() == getAmount() && getStep2() == getAmount()) {
setDone(true); setDone(true);
} }
} }
@ -182,6 +209,7 @@ public abstract class Quest {
checkDone(); checkDone();
if (!isDone) if (!isDone)
return; return;
//TODO add completed quest to database
QuestCompleteEvent event = new QuestCompleteEvent(player, this, true); QuestCompleteEvent event = new QuestCompleteEvent(player, this, true);
event.callEvent(); event.callEvent();
} }
@ -227,6 +255,10 @@ public abstract class Quest {
} }
public int getMaxToTurnIn() { public int getMaxToTurnIn() {
return Math.min(variant.getAmount() - getStep2(), getStep1() - getStep2()); return Math.min(getAmount() - getStep2(), getStep1() - getStep2());
}
public int getAmount() {
return amount;
} }
} }

View File

@ -4,19 +4,22 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import java.util.List; import java.util.List;
import java.util.Random;
public abstract class Variant { public abstract class Variant {
private final String internalName; private final String internalName;
private final Component name; private final Component name;
private final int amount; private final int rangeMin;
private final int rangeMax;
private final List<String> questPages; private final List<String> questPages;
private final List<String> donePages; private final List<String> donePages;
public Variant(String internalName, String name, int amount, List<String> questPages, List<String> donePages) { public Variant(String internalName, String name, List<String> questPages, List<String> donePages, int rangeMin, int rangeMax) {
this.internalName = internalName; this.internalName = internalName;
this.name = MiniMessage.miniMessage().deserialize(name); this.name = MiniMessage.miniMessage().deserialize(name);
this.amount = amount; this.rangeMin = rangeMin;
this.rangeMax = rangeMax;
this.questPages = questPages; this.questPages = questPages;
this.donePages = donePages; this.donePages = donePages;
} }
@ -29,10 +32,6 @@ public abstract class Variant {
return name; return name;
} }
public int getAmount() {
return amount;
}
public List<String> getQuestPages() { public List<String> getQuestPages() {
return questPages; return questPages;
} }
@ -40,4 +39,9 @@ public abstract class Variant {
public List<String> getDonePages() { public List<String> getDonePages() {
return donePages; return donePages;
} }
public int calculateAmount(int questsCompleted) {
int difficultyOffset = ((rangeMax - rangeMin) / 40) * questsCompleted;
return new Random().nextInt(Integer.min(rangeMax - 1, rangeMin + difficultyOffset), rangeMax);
}
} }

View File

@ -86,12 +86,12 @@ public class BreedMobsQuest extends Quest {
public TagResolver getTagResolvers() { public TagResolver getTagResolvers() {
TagResolver resolver = TagResolver.resolver( TagResolver resolver = TagResolver.resolver(
Placeholder.unparsed("mob", Utilities.formatName(breedMobsQuestObject.getEntityType().name())), Placeholder.unparsed("mob", Utilities.formatName(breedMobsQuestObject.getEntityType().name())),
Placeholder.parsed("step_1_progress", getStep1() == breedMobsQuestObject.getAmount() ? Placeholder.parsed("step_1_progress", getStep1() == getAmount() ?
"<green>" + getStep1() + "</green>" : "<red>" + getStep1() + "</red>"), "<green>" + getStep1() + "</green>" : "<red>" + getStep1() + "</red>"),
Placeholder.parsed("step_1_total", String.valueOf(breedMobsQuestObject.getAmount())), Placeholder.parsed("step_1_total", String.valueOf(getAmount())),
Placeholder.parsed("step_2_progress", getStep2() == breedMobsQuestObject.getAmount() ? Placeholder.parsed("step_2_progress", getStep2() == getAmount() ?
"<green>" + getStep2() + "</green>" : "<red>" + getStep2() + "</red>"), "<green>" + getStep2() + "</green>" : "<red>" + getStep2() + "</red>"),
Placeholder.parsed("step_2_total", String.valueOf(breedMobsQuestObject.getAmount())), Placeholder.parsed("step_2_total", String.valueOf(getAmount())),
Placeholder.unparsed("step_1", QuestsConfig.BREED_STEP_1), Placeholder.unparsed("step_1", QuestsConfig.BREED_STEP_1),
Placeholder.unparsed("step_2", QuestsConfig.BREED_STEP_2) Placeholder.unparsed("step_2", QuestsConfig.BREED_STEP_2)
); );

View File

@ -89,12 +89,12 @@ public class CollectDropsQuest extends Quest {
public TagResolver getTagResolvers() { public TagResolver getTagResolvers() {
TagResolver resolver = TagResolver.resolver( TagResolver resolver = TagResolver.resolver(
Placeholder.unparsed("item", Utilities.formatName(collectDropsQuestObject.getMaterial().name())), Placeholder.unparsed("item", Utilities.formatName(collectDropsQuestObject.getMaterial().name())),
Placeholder.parsed("step_1_progress", getStep1() == collectDropsQuestObject.getAmount() ? Placeholder.parsed("step_1_progress", getStep1() == getAmount() ?
"<green>" + getStep1() + "</green>" : "<red>" + getStep1() + "</red>"), "<green>" + getStep1() + "</green>" : "<red>" + getStep1() + "</red>"),
Placeholder.parsed("step_1_total", String.valueOf(collectDropsQuestObject.getAmount())), Placeholder.parsed("step_1_total", String.valueOf(getAmount())),
Placeholder.parsed("step_2_progress", getStep2() == collectDropsQuestObject.getAmount() ? Placeholder.parsed("step_2_progress", getStep2() == getAmount() ?
"<green>" + getStep2() + "</green>" : "<red>" + getStep2() + "</red>"), "<green>" + getStep2() + "</green>" : "<red>" + getStep2() + "</red>"),
Placeholder.parsed("step_2_total", String.valueOf(collectDropsQuestObject.getAmount())), Placeholder.parsed("step_2_total", String.valueOf(getAmount())),
Placeholder.unparsed("step_1", QuestsConfig.COLLECT_DROPS_STEP_1), Placeholder.unparsed("step_1", QuestsConfig.COLLECT_DROPS_STEP_1),
Placeholder.unparsed("step_2", QuestsConfig.COLLECT_DROPS_STEP_2), Placeholder.unparsed("step_2", QuestsConfig.COLLECT_DROPS_STEP_2),
Placeholder.unparsed("turn_in_text", QuestsConfig.COLLECT_DROPS_TURN_IN) Placeholder.unparsed("turn_in_text", QuestsConfig.COLLECT_DROPS_TURN_IN)
@ -123,11 +123,11 @@ public class CollectDropsQuest extends Quest {
.forEach(itemStack -> { .forEach(itemStack -> {
if (ref.tmpAmount == 0) if (ref.tmpAmount == 0)
return; return;
if (itemStack.getAmount() > ref.tmpAmount) { if (getAmount() > ref.tmpAmount) {
itemStack.setAmount(itemStack.getAmount() - ref.tmpAmount); itemStack.setAmount(getAmount() - ref.tmpAmount);
ref.tmpAmount = 0; ref.tmpAmount = 0;
} else { } else {
ref.tmpAmount -= itemStack.getAmount(); ref.tmpAmount -= getAmount();
itemStack.setAmount(0); itemStack.setAmount(0);
} }
}); });

View File

@ -86,12 +86,12 @@ public class KillMobsQuest extends Quest {
public TagResolver getTagResolvers() { public TagResolver getTagResolvers() {
TagResolver resolver = TagResolver.resolver( TagResolver resolver = TagResolver.resolver(
Placeholder.unparsed("mob", Utilities.formatName(killMobsQuestObject.getEntityType().name())), Placeholder.unparsed("mob", Utilities.formatName(killMobsQuestObject.getEntityType().name())),
Placeholder.parsed("step_1_progress", getStep1() == killMobsQuestObject.getAmount() ? Placeholder.parsed("step_1_progress", getStep1() == getAmount() ?
"<green>" + getStep1() + "</green>" : "<red>" + getStep1() + "</red>"), "<green>" + getStep1() + "</green>" : "<red>" + getStep1() + "</red>"),
Placeholder.parsed("step_1_total", String.valueOf(killMobsQuestObject.getAmount())), Placeholder.parsed("step_1_total", String.valueOf(getAmount())),
Placeholder.parsed("step_2_progress", getStep2() == killMobsQuestObject.getAmount() ? Placeholder.parsed("step_2_progress", getStep2() == getAmount() ?
"<green>" + getStep2() + "</green>" : "<red>" + getStep2() + "</red>"), "<green>" + getStep2() + "</green>" : "<red>" + getStep2() + "</red>"),
Placeholder.parsed("step_2_total", String.valueOf(killMobsQuestObject.getAmount())), Placeholder.parsed("step_2_total", String.valueOf(getAmount())),
Placeholder.unparsed("step_1", QuestsConfig.KILL_MOB_STEP_1), Placeholder.unparsed("step_1", QuestsConfig.KILL_MOB_STEP_1),
Placeholder.unparsed("step_2", QuestsConfig.KILL_MOB_STEP_2) Placeholder.unparsed("step_2", QuestsConfig.KILL_MOB_STEP_2)
); );

View File

@ -87,12 +87,12 @@ public class MineQuest extends Quest {
public TagResolver getTagResolvers() { public TagResolver getTagResolvers() {
TagResolver resolver = TagResolver.resolver( TagResolver resolver = TagResolver.resolver(
Placeholder.unparsed("block", Utilities.formatName(mineQuestObject.getMaterial().name())), Placeholder.unparsed("block", Utilities.formatName(mineQuestObject.getMaterial().name())),
Placeholder.parsed("step_1_progress", getStep1() == mineQuestObject.getAmount() ? Placeholder.parsed("step_1_progress", getStep1() == getAmount() ?
"<green>" + getStep1() + "</green>" : "<red>" + getStep1() + "</red>"), "<green>" + getStep1() + "</green>" : "<red>" + getStep1() + "</red>"),
Placeholder.parsed("step_1_total", String.valueOf(mineQuestObject.getAmount())), Placeholder.parsed("step_1_total", String.valueOf(getAmount())),
Placeholder.parsed("step_2_progress", getStep2() == mineQuestObject.getAmount() ? Placeholder.parsed("step_2_progress", getStep2() == getAmount() ?
"<green>" + getStep2() + "</green>" : "<red>" + getStep2() + "</red>"), "<green>" + getStep2() + "</green>" : "<red>" + getStep2() + "</red>"),
Placeholder.parsed("step_2_total", String.valueOf(mineQuestObject.getAmount())), Placeholder.parsed("step_2_total", String.valueOf(getAmount())),
Placeholder.unparsed("step_1", QuestsConfig.MINE_STEP_1), Placeholder.unparsed("step_1", QuestsConfig.MINE_STEP_1),
Placeholder.unparsed("step_2", QuestsConfig.MINE_STEP_2) Placeholder.unparsed("step_2", QuestsConfig.MINE_STEP_2)
); );
@ -120,11 +120,11 @@ public class MineQuest extends Quest {
.forEach(itemStack -> { .forEach(itemStack -> {
if (ref.tmpAmount == 0) if (ref.tmpAmount == 0)
return; return;
if (itemStack.getAmount() > ref.tmpAmount) { if (getAmount() > ref.tmpAmount) {
itemStack.setAmount(itemStack.getAmount() - ref.tmpAmount); itemStack.setAmount(getAmount() - ref.tmpAmount);
ref.tmpAmount = 0; ref.tmpAmount = 0;
} else { } else {
ref.tmpAmount -= itemStack.getAmount(); ref.tmpAmount -= getAmount();
itemStack.setAmount(0); itemStack.setAmount(0);
} }
}); });

View File

@ -9,9 +9,9 @@ public class BreedMobsQuestObject extends Variant {
private final EntityType entityType; private final EntityType entityType;
public BreedMobsQuestObject(String internalName, String name, EntityType entityType, int amount, public BreedMobsQuestObject(String internalName, String name, EntityType entityType,
List<String> questPages, List<String> donePages) { List<String> questPages, List<String> donePages, int min, int max) {
super(internalName, name, amount, questPages, donePages); super(internalName, name, questPages, donePages, min, max);
this.entityType = entityType; this.entityType = entityType;
} }

View File

@ -9,8 +9,9 @@ public class CollectDropsQuestObject extends Variant {
private final Material material; private final Material material;
public CollectDropsQuestObject(String internalName, String name, Material item, int amount, List<String> questPages, List<String> donePages) { public CollectDropsQuestObject(String internalName, String name, Material item,
super(internalName, name, amount, questPages, donePages); List<String> questPages, List<String> donePages, int min, int max) {
super(internalName, name, questPages, donePages, min, max);
this.material = item; this.material = item;
} }

View File

@ -9,9 +9,9 @@ public class KillMobsQuestObject extends Variant {
private final EntityType entityType; private final EntityType entityType;
public KillMobsQuestObject(String internalName, String name, EntityType entityType, int amount, public KillMobsQuestObject(String internalName, String name, EntityType entityType,
List<String> questPages, List<String> donePages) { List<String> questPages, List<String> donePages, int min, int max) {
super(internalName, name, amount, questPages, donePages); super(internalName, name, questPages, donePages, min, max);
this.entityType = entityType; this.entityType = entityType;
} }

View File

@ -9,9 +9,9 @@ public class MineQuestObject extends Variant {
private final Material material; private final Material material;
public MineQuestObject(String internalName, String name, Material material, int amount, public MineQuestObject(String internalName, String name, Material material,
List<String> questPages, List<String> donePages) { List<String> questPages, List<String> donePages, int min, int max) {
super(internalName, name, amount, questPages, donePages); super(internalName, name, questPages, donePages, min, max);
this.material = material; this.material = material;
} }