From fcdd55eeac193cafee0545f07ae77af849552629 Mon Sep 17 00:00:00 2001 From: Alex Thomson Date: Sat, 19 May 2018 14:17:19 +1200 Subject: [PATCH] Alternative AdvancementProgress fix Setting the progressMap to null may have been causing advancement messages to get spammed in chat, an alternative fix has been developed which should hopefully avoid this issue. --- build.gradle | 2 +- .../MixinAdvancementProgress.java | 55 ++++++++++++++++++- .../lxgaming/sledgehammer/util/Reference.java | 2 +- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 61f4df1..2ee1606 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ targetCompatibility = 1.8 group = "io.github.lxgaming" archivesBaseName = "Sledgehammer" -version = "1.1.1-1.12.2" +version = "1.1.2-1.12.2" minecraft { version = "1.12.2-14.23.3.2655" diff --git a/src/main/java/io/github/lxgaming/sledgehammer/mixin/core/advancements/MixinAdvancementProgress.java b/src/main/java/io/github/lxgaming/sledgehammer/mixin/core/advancements/MixinAdvancementProgress.java index ad6ab97..fe7e7e7 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/mixin/core/advancements/MixinAdvancementProgress.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/mixin/core/advancements/MixinAdvancementProgress.java @@ -18,7 +18,9 @@ import io.github.lxgaming.sledgehammer.Sledgehammer; import net.minecraft.advancements.AdvancementProgress; +import org.apache.commons.lang3.StringUtils; import org.spongepowered.api.advancement.criteria.AdvancementCriterion; +import org.spongepowered.api.advancement.criteria.OperatorCriterion; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.asm.mixin.Dynamic; import org.spongepowered.asm.mixin.Mixin; @@ -30,6 +32,7 @@ import org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements; import java.util.Map; +import java.util.Optional; @Mixin(value = AdvancementProgress.class, priority = 1337) public abstract class MixinAdvancementProgress implements org.spongepowered.api.advancement.AdvancementProgress, IMixinAdvancementProgress { @@ -37,12 +40,62 @@ public abstract class MixinAdvancementProgress implements org.spongepowered.api. @Dynamic(mixin = org.spongepowered.common.mixin.core.advancement.MixinAdvancementProgress.class) private Map progressMap; - @Inject(method = "isDone", at = @At(value = "HEAD")) + @Inject(method = "isDone", at = @At(value = "HEAD"), cancellable = true) private void onIsDone(CallbackInfoReturnable callbackInfoReturnable) { + /* + // Setting the progressMap to null might be the cause of the advancement message spam if (!get(getAdvancement().getCriterion()).isPresent()) { this.progressMap = null; Player player = ((IMixinPlayerAdvancements) getPlayerAdvancements()).getPlayer(); Sledgehammer.getInstance().debugMessage("Reset {} for {} ({})", getAdvancement().getCriterion().getName(), player.getName(), player.getUniqueId()); } + */ + + if (progressMap == null || progressMap.isEmpty()) { + callbackInfoReturnable.setReturnValue(false); + return; + } + + AdvancementCriterion advancementCriterion = getAdvancement().getCriterion(); + Player player = ((IMixinPlayerAdvancements) getPlayerAdvancements()).getPlayer(); + + Optional criterionProgress; + if (advancementCriterion instanceof OperatorCriterion) { + criterionProgress = getCriterionProgress((OperatorCriterion) advancementCriterion); + } else { + criterionProgress = getCriterionProgress(advancementCriterion); + } + + callbackInfoReturnable.setReturnValue(criterionProgress.map(ICriterionProgress::achieved).orElse(false)); + + if (!criterionProgress.isPresent()) { + Sledgehammer.getInstance().debugMessage("Failed to find {} for {} ({})", advancementCriterion.getName(), player.getName(), player.getUniqueId()); + } + } + + private Optional getCriterionProgress(AdvancementCriterion advancementCriterion) { + for (Map.Entry entry : progressMap.entrySet()) { + if (entry.getKey() instanceof OperatorCriterion) { + continue; + } + + if (StringUtils.equals(advancementCriterion.getName(), entry.getKey().getName())) { + return Optional.of(entry.getValue()); + } + } + + return Optional.empty(); + } + + private Optional getCriterionProgress(OperatorCriterion operatorCriterion) { + for (Map.Entry entry : progressMap.entrySet()) { + if (!(entry.getKey() instanceof OperatorCriterion)) { + continue; + } + + return Optional.of(entry.getValue()); + } + + return Optional.empty(); } } \ No newline at end of file diff --git a/src/main/java/io/github/lxgaming/sledgehammer/util/Reference.java b/src/main/java/io/github/lxgaming/sledgehammer/util/Reference.java index 981799f..e20e570 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/util/Reference.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/util/Reference.java @@ -20,7 +20,7 @@ public class Reference { public static final String PLUGIN_ID = "sledgehammer"; public static final String PLUGIN_NAME = "Sledgehammer"; - public static final String PLUGIN_VERSION = "1.1.1-1.12.2"; + public static final String PLUGIN_VERSION = "1.1.2-1.12.2"; public static final String DESCRIPTION = "Smashes the stupid out of the server."; public static final String AUTHORS = "LX_Gaming"; public static final String SOURCE = "https://github.com/LXGaming/Sledgehammer/";