Skip to content

Commit 2a0d137

Browse files
committed
Send advancement updates without client toast during share handling
1 parent 1605548 commit 2a0d137

File tree

1 file changed

+24
-2
lines changed
  • src/main/java/org/mvplugins/multiverse/inventories/share

1 file changed

+24
-2
lines changed

src/main/java/org/mvplugins/multiverse/inventories/share/Sharables.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.bukkit.World;
88
import org.bukkit.advancement.AdvancementProgress;
99
import org.bukkit.inventory.Inventory;
10-
import org.jetbrains.annotations.ApiStatus;
1110
import org.mvplugins.multiverse.core.economy.MVEconomist;
1211
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
1312
import org.mvplugins.multiverse.core.utils.ReflectHelper;
@@ -33,6 +32,7 @@
3332
import org.bukkit.potion.PotionEffect;
3433
import org.mvplugins.multiverse.inventories.util.RespawnLocation;
3534

35+
import java.lang.reflect.Method;
3636
import java.util.ArrayList;
3737
import java.util.Arrays;
3838
import java.util.Collection;
@@ -45,7 +45,7 @@
4545
import java.util.Map;
4646
import java.util.Objects;
4747
import java.util.Set;
48-
import java.util.UUID;
48+
import java.util.function.Consumer;
4949
import java.util.stream.Collectors;
5050

5151
import static org.mvplugins.multiverse.inventories.util.MinecraftTools.findBedFromRespawnLocation;
@@ -776,6 +776,7 @@ public boolean updatePlayer(Player player, ProfileData profile) {
776776
player.setExp(exp);
777777
player.setLevel(level);
778778
player.setTotalExperience(totalExperience);
779+
sendAdvancementUpdateWithoutToast.accept(player);
779780
if (announceAdvancements) {
780781
player.getWorld().setGameRule(GameRule.ANNOUNCE_ADVANCEMENTS, true);
781782
}
@@ -784,6 +785,27 @@ public boolean updatePlayer(Player player, ProfileData profile) {
784785
}
785786
}).defaultSerializer(new ProfileEntry(false, "advancements")).altName("achievements").optional().build();
786787

788+
private static final Consumer<Player> sendAdvancementUpdateWithoutToast;
789+
790+
static {
791+
// use reflection to send advancement update without client toast
792+
// should work 1.21.5+ papermc unless Mojang changes things again
793+
Option<Class<?>> craftPlayerClass = Option.of(ReflectHelper.getClass("org.bukkit.craftbukkit.entity.CraftPlayer"));
794+
Option<Method> getHandleMethod = craftPlayerClass.map(cls -> ReflectHelper.getMethod(cls, "getHandle"));
795+
Option<Class<?>> serverPlayerClass = Option.of(ReflectHelper.getClass("net.minecraft.server.level.ServerPlayer"));
796+
Option<Method> getAdvancementsMethod = serverPlayerClass.map(cls -> ReflectHelper.getMethod(cls, "getAdvancements"));
797+
Option<Class<?>> playerAdvancementsClass = Option.of(ReflectHelper.getClass("net.minecraft.server.PlayerAdvancements"));
798+
Option<Method> flushDirtyMethod = playerAdvancementsClass.flatMap(cls ->
799+
serverPlayerClass.map(cls2 -> ReflectHelper.getMethod(cls, "flushDirty", cls2, boolean.class)));
800+
801+
sendAdvancementUpdateWithoutToast = player -> getHandleMethod.flatMap(method ->
802+
Try.of(() -> method.invoke(player)).toOption())
803+
.flatMap(serverPlayer -> getAdvancementsMethod.flatMap(method ->
804+
Try.of(() -> method.invoke(serverPlayer)).toOption())
805+
.flatMap(playerAdvancements -> flushDirtyMethod.flatMap(method ->
806+
Try.of(() -> method.invoke(playerAdvancements, serverPlayer, false)).toOption())));
807+
}
808+
787809
/**
788810
* Sharing Statistics.
789811
*/

0 commit comments

Comments
 (0)