Skip to content

Commit

Permalink
Alternative AdvancementProgress fix
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LXGaming committed May 19, 2018
1 parent c1c0ffa commit fcdd55e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,19 +32,70 @@
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 {

@Dynamic(mixin = org.spongepowered.common.mixin.core.advancement.MixinAdvancementProgress.class)
private Map<AdvancementCriterion, ICriterionProgress> progressMap;

@Inject(method = "isDone", at = @At(value = "HEAD"))
@Inject(method = "isDone", at = @At(value = "HEAD"), cancellable = true)
private void onIsDone(CallbackInfoReturnable<Boolean> 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<ICriterionProgress> 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<ICriterionProgress> getCriterionProgress(AdvancementCriterion advancementCriterion) {
for (Map.Entry<AdvancementCriterion, ICriterionProgress> 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<ICriterionProgress> getCriterionProgress(OperatorCriterion operatorCriterion) {
for (Map.Entry<AdvancementCriterion, ICriterionProgress> entry : progressMap.entrySet()) {
if (!(entry.getKey() instanceof OperatorCriterion)) {
continue;
}

return Optional.of(entry.getValue());
}

return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand Down

0 comments on commit fcdd55e

Please sign in to comment.