Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix advancement not triggering for entering nether/end worlds. #218

Merged
merged 1 commit into from
Feb 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.onarandombox.MultiverseNetherPortals.listeners;

import java.util.logging.Level;

import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
Expand All @@ -11,12 +9,15 @@
import com.onarandombox.MultiverseNetherPortals.utils.MVNameChecker;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.PortalType;
import org.bukkit.World;
import org.bukkit.advancement.Advancement;
import org.bukkit.advancement.AdvancementProgress;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
Expand All @@ -28,13 +29,20 @@ public class MVNPPlayerListener implements Listener {
private final MVLinkChecker linkChecker;
private final MVWorldManager worldManager;
private final PermissionTools pt;
private final Advancement enterNetherAdvancement;
private final Advancement enterEndAdvancement;

private static final String ENTER_NETHER_CRITERIA = "entered_nether";
private static final String ENTER_END_CRITERIA = "entered_end";

public MVNPPlayerListener(MultiverseNetherPortals plugin) {
this.plugin = plugin;
this.nameChecker = new MVNameChecker(plugin);
this.worldManager = this.plugin.getCore().getMVWorldManager();
this.pt = new PermissionTools(this.plugin.getCore());
this.linkChecker = new MVLinkChecker(this.plugin);
this.enterNetherAdvancement = this.plugin.getServer().getAdvancement(NamespacedKey.minecraft("story/enter_the_nether"));
this.enterEndAdvancement = this.plugin.getServer().getAdvancement(NamespacedKey.minecraft("story/enter_the_end"));
}

@EventHandler
Expand Down Expand Up @@ -148,6 +156,37 @@ public void onPlayerPortal(PlayerPortalEvent event) {
}
}
}

// Advancements need to be triggered manually
if (type == PortalType.NETHER && event.getTo().getWorld().getEnvironment() == World.Environment.NETHER) {
awardAdvancement(event.getPlayer(), enterNetherAdvancement, ENTER_NETHER_CRITERIA);
} else if (type == PortalType.ENDER && event.getTo().getWorld().getEnvironment() == World.Environment.THE_END) {
awardAdvancement(event.getPlayer(), enterEndAdvancement, ENTER_END_CRITERIA);
}
}
}

/**
* Award an advancement criteria to a player if not already awarded.
*
* @param player Target player to award the advancement criteria to.
* @param advancement {@link Advancement} the criteria belongs to.
* @param criteria Criteria to award the player.
*/
private void awardAdvancement(Player player, Advancement advancement, String criteria) {
if (advancement == null) {
Logging.warning("No advancement found for target criteria: %s", criteria);
return;
}
AdvancementProgress advancementProgress = player.getAdvancementProgress(advancement);
if (advancementProgress.isDone()) {
Logging.fine("%s has already been awarded advancement criteria %s.", player.getName(), criteria);
return;
}
if (!advancementProgress.awardCriteria(criteria)) {
Logging.warning("Unable to award advancement criteria %s to %s.", criteria, player.getName());
return;
}
Logging.fine("Awarded advancement criteria %s to %s.", criteria, player.getName());
}
}