Skip to content

Commit

Permalink
feat: support auto claiming rewards (#213)
Browse files Browse the repository at this point in the history
* feat: support auto claiming rewards

close #160

* chore: format and run on server thread
  • Loading branch information
SettingDust authored Apr 12, 2024
1 parent 990b748 commit fff8c2c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.teamresourceful.resourcefullib.common.codecs.EnumCodec;
import earth.terrarium.heracles.common.utils.ModUtils;

import java.util.Objects;

Expand All @@ -13,25 +12,28 @@ public final class QuestSettings {
EnumCodec.of(QuestDisplayStatus.class).fieldOf("hidden").orElse(QuestDisplayStatus.LOCKED).forGetter(QuestSettings::hiddenUntil),
Codec.BOOL.fieldOf("unlockNotification").orElse(false).forGetter(QuestSettings::unlockNotification),
Codec.BOOL.fieldOf("showDependencyArrow").orElse(true).forGetter(QuestSettings::showDependencyArrow),
Codec.BOOL.fieldOf("repeatable").orElse(false).forGetter(QuestSettings::repeatable)
Codec.BOOL.fieldOf("repeatable").orElse(false).forGetter(QuestSettings::repeatable),
Codec.BOOL.fieldOf("autoClaimRewards").orElse(false).forGetter(QuestSettings::autoClaimRewards)
).apply(instance, QuestSettings::new));

private boolean individualProgress;
private QuestDisplayStatus hiddenUntil;
private boolean unlockNotification;
private boolean showDependencyArrow;
private boolean repeatable;
private boolean autoClaimRewards;

public QuestSettings(boolean individualProgress, QuestDisplayStatus hiddenUntil, boolean unlockNotification, boolean showDependencyArrow, boolean repeatable) {
public QuestSettings(boolean individualProgress, QuestDisplayStatus hiddenUntil, boolean unlockNotification, boolean showDependencyArrow, boolean repeatable, boolean autoClaimRewards) {
this.individualProgress = individualProgress;
this.hiddenUntil = hiddenUntil;
this.unlockNotification = unlockNotification;
this.showDependencyArrow = showDependencyArrow;
this.repeatable = repeatable;
this.autoClaimRewards = autoClaimRewards;
}

public static QuestSettings createDefault() {
return new QuestSettings(false, QuestDisplayStatus.LOCKED, false, true, false);
return new QuestSettings(false, QuestDisplayStatus.LOCKED, false, true, false, false);
}

public boolean individualProgress() {
Expand All @@ -54,6 +56,10 @@ public Boolean repeatable() {
return repeatable;
}

public boolean autoClaimRewards() {
return autoClaimRewards;
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
Expand All @@ -64,12 +70,13 @@ public boolean equals(Object obj) {
this.hiddenUntil == that.hiddenUntil &&
this.unlockNotification == that.unlockNotification &&
this.showDependencyArrow == that.showDependencyArrow &&
this.repeatable == that.repeatable;
this.repeatable == that.repeatable &&
this.autoClaimRewards == that.autoClaimRewards;
}

@Override
public int hashCode() {
return Objects.hash(individualProgress, hiddenUntil, unlockNotification, showDependencyArrow, repeatable);
return Objects.hash(individualProgress, hiddenUntil, unlockNotification, showDependencyArrow, repeatable, autoClaimRewards);
}

public void update(QuestSettings newSettings) {
Expand Down Expand Up @@ -100,4 +107,8 @@ public void setRepeatable(boolean repeatable) {
this.repeatable = repeatable;
}

public void setAutoClaimRewards(boolean autoClaimRewards) {
this.autoClaimRewards = autoClaimRewards;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public CreationData create(@Nullable QuestSettings object) {
settings.put("unlock_notification", BooleanSetting.FALSE, object != null && object.unlockNotification());
settings.put("show_dependency_arrow", BooleanSetting.TRUE, object != null && object.showDependencyArrow());
settings.put("repeatable", BooleanSetting.FALSE, object != null && object.repeatable());
settings.put("auto_claim_rewards", BooleanSetting.FALSE, object != null && object.autoClaimRewards());
return settings;
}

Expand All @@ -29,7 +30,8 @@ public QuestSettings create(String id, QuestSettings object, Data data) {
data.get("hidden", new EnumSetting<>(QuestDisplayStatus.class, QuestDisplayStatus.LOCKED)).orElse(object != null ? object.hiddenUntil() : QuestDisplayStatus.LOCKED),
data.get("unlock_notification", BooleanSetting.FALSE).orElse(object != null && object.unlockNotification()),
data.get("show_dependency_arrow", BooleanSetting.TRUE).orElse(object != null && object.showDependencyArrow()),
data.get("repeatable", BooleanSetting.FALSE).orElse(object != null && object.repeatable())
data.get("repeatable", BooleanSetting.FALSE).orElse(object != null && object.repeatable()),
data.get("auto_claim_rewards", BooleanSetting.FALSE).orElse(object != null && object.autoClaimRewards())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ public SelectQuestWidget(int x, int y, int width, int height, QuestsWidget widge
.hiddenUntil(questSettings.hiddenUntil())
.unlockNotification(questSettings.unlockNotification())
.showDependencyArrow(questSettings.showDependencyArrow())
.repeatable(questSettings.repeatable());
.repeatable(questSettings.repeatable())
.autoClaimRewards(questSettings.autoClaimRewards());
})
);
edit.setTitle(Component.translatable("gui.heracles.quests.edit_quest_settings"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package earth.terrarium.heracles.common.handlers.progress;

import earth.terrarium.heracles.Heracles;
import earth.terrarium.heracles.api.events.HeraclesEvents;
import earth.terrarium.heracles.api.quests.Quest;
import earth.terrarium.heracles.api.teams.TeamProviders;
import earth.terrarium.heracles.common.handlers.quests.QuestHandler;
Expand All @@ -19,6 +20,15 @@ public class QuestProgressHandler extends SavedData {
private final Map<UUID, QuestsProgress> progress = new HashMap<>();

public QuestProgressHandler() {
HeraclesEvents.QuestCompleteListener.register(it -> {
if (it.quest().settings().autoClaimRewards()) {
// For avoiding the reward trigger the task checking again.
it.player().server.execute(() -> {
var questsProgress = getProgress(it.player().getUUID());
it.quest().claimRewards(it.player(), it.id(), questsProgress, questsProgress.getProgress(it.id()));
});
}
});
}

public QuestsProgress getProgress(UUID uuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static class Builder {
private TriState unlockNotification = TriState.UNDEFINED;
private TriState showDependencyArrow = TriState.UNDEFINED;
private TriState repeatable = TriState.UNDEFINED;
private TriState autoClaimRewards = TriState.UNDEFINED;
private Set<String> dependencies;
private Map<String, QuestTask<?, ?, ?>> tasks;
private Map<String, QuestReward<?>> rewards;
Expand Down Expand Up @@ -139,6 +140,11 @@ public Builder repeatable(boolean repeatable) {
return this;
}

public Builder autoClaimRewards(boolean autoClaimRewards) {
this.autoClaimRewards = TriState.of(autoClaimRewards);
return this;
}

public Builder dependencies(Set<String> dependencies) {
this.dependencies = new HashSet<>(dependencies);
return this;
Expand Down Expand Up @@ -173,7 +179,8 @@ public NetworkQuestData build() {
Optional.ofNullable(hiddenUntil),
Optional.ofNullable(unlockNotification.isUndefined() ? null : unlockNotification.isTrue()),
Optional.ofNullable(showDependencyArrow.isUndefined() ? null : showDependencyArrow.isTrue()),
Optional.ofNullable(repeatable.isUndefined() ? null : repeatable.isTrue())
Optional.ofNullable(repeatable.isUndefined() ? null : repeatable.isTrue()),
Optional.ofNullable(autoClaimRewards.isUndefined() ? null : autoClaimRewards.isTrue())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public record NetworkQuestSettingsData(
Optional<QuestDisplayStatus> hiddenUntil,
Optional<Boolean> unlockNotification,
Optional<Boolean> showDependencyArrow,
Optional<Boolean> repeatable
Optional<Boolean> repeatable,
Optional<Boolean> autoClaimRewards
) {

public static final ByteCodec<NetworkQuestSettingsData> CODEC = ObjectByteCodec.create(
Expand All @@ -22,6 +23,7 @@ public record NetworkQuestSettingsData(
ByteCodec.BOOLEAN.optionalFieldOf(NetworkQuestSettingsData::unlockNotification),
ByteCodec.BOOLEAN.optionalFieldOf(NetworkQuestSettingsData::showDependencyArrow),
ByteCodec.BOOLEAN.optionalFieldOf(NetworkQuestSettingsData::repeatable),
ByteCodec.BOOLEAN.optionalFieldOf(NetworkQuestSettingsData::autoClaimRewards),
NetworkQuestSettingsData::new
);

Expand All @@ -32,5 +34,6 @@ public void update(Quest quest) {
unlockNotification.ifPresent(settings::setUnlockNotification);
showDependencyArrow.ifPresent(settings::setShowDependencyArrow);
repeatable.ifPresent(settings::setRepeatable);
autoClaimRewards.ifPresent(settings::setAutoClaimRewards);
}
}
1 change: 1 addition & 0 deletions common/src/main/resources/assets/heracles/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"setting.heracles.quest.unlock_notification": "Unlock Notification",
"setting.heracles.quest.show_dependency_arrow": "Show Dependency Arrow",
"setting.heracles.quest.repeatable": "Repeatable",
"setting.heracles.quest.auto_claim_rewards": "Auto Claim Rewards",

"rei.sections.odyssey": "Project Odyssey",
"rei.heracles.heracles.tooltip": "Open Quests",
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/assets/heracles/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"setting.heracles.quest.unlock_notification": "解锁通知",
"setting.heracles.quest.show_dependency_arrow": "显示依赖箭头",
"setting.heracles.quest.repeatable": "可重复",
"setting.heracles.quest.auto_claim_rewards": "自动领取奖励",

"rei.sections.odyssey": "Project Odyssey",
"rei.heracles.heracles.tooltip": "打开任务",
Expand Down

0 comments on commit fff8c2c

Please sign in to comment.