refactor: adjust way advancement messages are cleared, close #285

feat/data-edit-commands
William 7 months ago
parent e35dcf3aad
commit fa5cea2aa3
No known key found for this signature in database

@ -30,7 +30,10 @@ import net.william278.husksync.BukkitHuskSync;
import net.william278.husksync.HuskSync;
import net.william278.husksync.adapter.Adaptable;
import net.william278.husksync.user.BukkitUser;
import org.bukkit.*;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Registry;
import org.bukkit.Statistic;
import org.bukkit.advancement.AdvancementProgress;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
@ -342,16 +345,10 @@ public abstract class BukkitData implements Data {
private void setAdvancement(@NotNull HuskSync plugin, @NotNull org.bukkit.advancement.Advancement advancement,
@NotNull Player player, @NotNull BukkitUser user,
@NotNull Collection<String> toAward, @NotNull Collection<String> toRevoke) {
final boolean folia = ((BukkitHuskSync) plugin).getScheduler().isUsingFolia();
plugin.runSync(() -> {
// Track player exp level & progress
final int expLevel = player.getLevel();
final float expProgress = player.getExp();
boolean gameRuleUpdated = false;
if (!folia && Boolean.TRUE.equals(player.getWorld().getGameRuleValue(GameRule.ANNOUNCE_ADVANCEMENTS))) {
player.getWorld().setGameRule(GameRule.ANNOUNCE_ADVANCEMENTS, false);
gameRuleUpdated = true;
}
// Award and revoke advancement criteria
final AdvancementProgress progress = player.getAdvancementProgress(advancement);
@ -363,9 +360,6 @@ public abstract class BukkitData implements Data {
player.setLevel(expLevel);
player.setExp(expProgress);
}
if (gameRuleUpdated) {
player.getWorld().setGameRule(GameRule.ANNOUNCE_ADVANCEMENTS, true);
}
}, user);
}

@ -29,6 +29,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.MapInitializeEvent;
import org.bukkit.event.world.WorldSaveEvent;
@ -102,6 +103,10 @@ public class BukkitEventListener extends EventListener implements BukkitJoinEven
super.saveOnPlayerDeath(user, BukkitData.Items.ItemArray.adapt(event.getDrops()));
}
// Handle advancement grant events (empty on Spigot due to missing message API)
protected void handleLockedAdvancementGrant(@NotNull @SuppressWarnings("unused") PlayerAdvancementDoneEvent event) {
}
@EventHandler(ignoreCancelled = true)
public void onWorldSave(@NotNull WorldSaveEvent event) {
if (!plugin.getSettings().getSynchronization().isSaveOnWorldSave()) {
@ -121,6 +126,13 @@ public class BukkitEventListener extends EventListener implements BukkitJoinEven
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerAdvancementDone(@NotNull PlayerAdvancementDoneEvent event) {
if (lockedHandler.cancelPlayerEvent(event.getPlayer().getUniqueId())) {
handleLockedAdvancementGrant(event);
}
}
// We handle commands here to allow specific command handling on ProtocolLib servers
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onCommandProcessed(@NotNull PlayerCommandPreprocessEvent event) {

@ -25,6 +25,7 @@ import net.william278.husksync.data.BukkitData;
import net.william278.husksync.user.BukkitUser;
import net.william278.husksync.user.OnlineUser;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.jetbrains.annotations.NotNull;
@ -68,6 +69,12 @@ public class PaperEventListener extends BukkitEventListener {
super.saveOnPlayerDeath(user, BukkitData.Items.ItemArray.adapt(itemsToSave));
}
@Override
protected void handleLockedAdvancementGrant(@NotNull PlayerAdvancementDoneEvent event) {
// Disable advancement chat messages
event.message(null);
}
@NotNull
private List<ItemStack> preserveOrder(@NotNull PlayerInventory inventory, @NotNull List<ItemStack> toKeep) {
final List<ItemStack> preserved = Lists.newArrayList();

Loading…
Cancel
Save