Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/main/java/fr/openmc/core/CommandsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +45,8 @@ private static void registerCommands() {
new ChronometerCommand(),
new Restart(),
new CreditsCommand(),
new CustomItemCommand()
new CustomItemCommand(),
new ToastCommand()
);
}
}
19 changes: 19 additions & 0 deletions src/main/java/fr/openmc/core/commands/debug/ToastCommand.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
82 changes: 82 additions & 0 deletions src/main/java/fr/openmc/core/utils/nms/ToastUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading