diff --git a/src/main/java/fr/openmc/core/CommandsManager.java b/src/main/java/fr/openmc/core/CommandsManager.java index 132065593..6defe4096 100644 --- a/src/main/java/fr/openmc/core/CommandsManager.java +++ b/src/main/java/fr/openmc/core/CommandsManager.java @@ -3,6 +3,7 @@ import fr.openmc.api.cooldown.CooldownInterceptor; import fr.openmc.core.commands.debug.ChronometerCommand; import fr.openmc.core.commands.debug.CustomItemCommand; +import fr.openmc.core.commands.debug.ToastCommand; import fr.openmc.core.commands.fun.Diceroll; import fr.openmc.core.commands.fun.Playtime; import fr.openmc.core.commands.utils.RTPCommands; @@ -44,7 +45,8 @@ private static void registerCommands() { new ChronometerCommand(), new Restart(), new CreditsCommand(), - new CustomItemCommand() + new CustomItemCommand(), + new ToastCommand() ); } } diff --git a/src/main/java/fr/openmc/core/commands/debug/ToastCommand.java b/src/main/java/fr/openmc/core/commands/debug/ToastCommand.java new file mode 100644 index 000000000..d841aa915 --- /dev/null +++ b/src/main/java/fr/openmc/core/commands/debug/ToastCommand.java @@ -0,0 +1,19 @@ +package fr.openmc.core.commands.debug; + +import fr.openmc.core.utils.nms.ToastUtils; +import net.minecraft.advancements.AdvancementType; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import revxrsal.commands.annotation.Command; +import revxrsal.commands.annotation.Subcommand; +import revxrsal.commands.bukkit.annotation.CommandPermission; + +@Command("toast") +@CommandPermission("omc.admins.commands.toast") +public class ToastCommand { + @Subcommand("test") + @CommandPermission("omc.admins.commands.toast.test") + public void test(Player player) { + ToastUtils.sendCustomToast(player, Material.TEST_INSTANCE_BLOCK, "translation.key.fuck", AdvancementType.CHALLENGE); + } +} diff --git a/src/main/java/fr/openmc/core/utils/nms/ToastUtils.java b/src/main/java/fr/openmc/core/utils/nms/ToastUtils.java new file mode 100644 index 000000000..8b5723f4a --- /dev/null +++ b/src/main/java/fr/openmc/core/utils/nms/ToastUtils.java @@ -0,0 +1,82 @@ +package fr.openmc.core.utils.nms; + +import net.minecraft.advancements.*; +import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.game.ClientboundUpdateAdvancementsPacket; +import net.minecraft.resources.Identifier; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.ItemStackTemplate; +import org.bukkit.Material; +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; + +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +public class ToastUtils { + private static final Identifier TOAST_IDENTIFIER = Identifier.parse("omc:custom_toast"); + private static final AdvancementRequirements ADV_REQUIREMENTS = AdvancementRequirements.allOf(Set.of("c")); + + /** + * Affiche un Toast (= pop up lorsqu'on obtient un succes) Customisable + * @param player Le joueur ciblé + * @param item l'item utilisé + * @param translationKey la clé de translation du texte + * @param type le type du succes + */ + public static void sendCustomToast(Player player, org.bukkit.inventory.ItemStack item, String translationKey, AdvancementType type) { + Advancement adv = new Advancement( + Optional.empty(), + Optional.of(new DisplayInfo( + ItemStackTemplate.fromNonEmptyStack(ItemStack.fromBukkitCopy(item)), + Component.translatable(translationKey), + Component.empty(), + Optional.empty(), + type, + true, + false, + true + )), + AdvancementRewards.EMPTY, + Map.of(), + ADV_REQUIREMENTS, + false + ); + + AdvancementHolder holder = new AdvancementHolder(TOAST_IDENTIFIER, adv); + + AdvancementProgress progress = new AdvancementProgress(); + progress.update(ADV_REQUIREMENTS); + progress.grantProgress("c"); + + ServerPlayer nmsPlayer = ((CraftPlayer) player).getHandle(); + nmsPlayer.connection.send(new ClientboundUpdateAdvancementsPacket( + false, + Set.of(holder), + Set.of(), + Map.of(TOAST_IDENTIFIER, progress), + true + )); + + nmsPlayer.connection.send(new ClientboundUpdateAdvancementsPacket( + false, + Set.of(), + Set.of(TOAST_IDENTIFIER), + Map.of(), + false + )); + } + + /** + * Affiche un Toast (= pop up lorsqu'on obtient un succes) Customisable + * @param player Le joueur ciblé + * @param material le material utilisé + * @param translationKey la clé de translation du texte + * @param type le type du succes + */ + public static void sendCustomToast(Player player, Material material, String translationKey, AdvancementType type) { + sendCustomToast(player, new org.bukkit.inventory.ItemStack(material), translationKey, type); + } +}