|
| 1 | +package com.ghostchu.quickshop.platform.spigot.v1_20_1; |
| 2 | + |
| 3 | +import com.ghostchu.quickshop.platform.Platform; |
| 4 | +import com.ghostchu.quickshop.platform.Util; |
| 5 | +import com.ghostchu.quickshop.platform.spigot.AbstractSpigotPlatform; |
| 6 | +import net.kyori.adventure.key.Key; |
| 7 | +import net.kyori.adventure.nbt.api.BinaryTagHolder; |
| 8 | +import net.kyori.adventure.text.event.HoverEvent; |
| 9 | +import net.minecraft.nbt.CompoundTag; |
| 10 | +import org.bukkit.Bukkit; |
| 11 | +import org.bukkit.Material; |
| 12 | +import org.bukkit.NamespacedKey; |
| 13 | +import org.bukkit.command.Command; |
| 14 | +import org.bukkit.craftbukkit.v1_20_R1.CraftServer; |
| 15 | +import org.bukkit.craftbukkit.v1_20_R1.inventory.CraftItemStack; |
| 16 | +import org.bukkit.craftbukkit.v1_20_R1.potion.CraftPotionEffectType; |
| 17 | +import org.bukkit.craftbukkit.v1_20_R1.util.CraftMagicNumbers; |
| 18 | +import org.bukkit.enchantments.Enchantment; |
| 19 | +import org.bukkit.entity.EntityType; |
| 20 | +import org.bukkit.inventory.ItemStack; |
| 21 | +import org.bukkit.plugin.Plugin; |
| 22 | +import org.bukkit.potion.PotionEffectType; |
| 23 | +import org.bukkit.potion.PotionEffectTypeWrapper; |
| 24 | +import org.jetbrains.annotations.NotNull; |
| 25 | + |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Optional; |
| 28 | + |
| 29 | +public class Spigot1201Platform extends AbstractSpigotPlatform implements Platform { |
| 30 | + |
| 31 | + public Spigot1201Platform(@NotNull Plugin plugin) { |
| 32 | + super(plugin); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public @NotNull HoverEvent<HoverEvent.ShowItem> getItemStackHoverEvent(@NotNull ItemStack stack) { |
| 37 | + NamespacedKey namespacedKey = stack.getType().getKey(); |
| 38 | + Key key = Key.key(namespacedKey.toString()); |
| 39 | + BinaryTagHolder holder; |
| 40 | + if (Util.methodExists(BinaryTagHolder.class, "binaryTagHolder")) { |
| 41 | + holder = BinaryTagHolder.binaryTagHolder(CraftItemStack.asNMSCopy(stack).save(new CompoundTag()).toString()); |
| 42 | + } else { |
| 43 | + //noinspection UnstableApiUsage |
| 44 | + holder = BinaryTagHolder.of(CraftItemStack.asNMSCopy(stack).save(new CompoundTag()).toString()); |
| 45 | + } |
| 46 | + return HoverEvent.showItem(key, stack.getAmount(), holder); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public @NotNull String getMinecraftVersion() { |
| 51 | + return ((CraftServer) Bukkit.getServer()).getServer().getServerVersion(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void registerCommand(@NotNull String prefix, @NotNull Command command) { |
| 56 | + ((CraftServer) Bukkit.getServer()).getCommandMap().register(prefix, command); |
| 57 | + command.register(((CraftServer) Bukkit.getServer()).getCommandMap()); |
| 58 | + ((CraftServer) Bukkit.getServer()).syncCommands(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public @NotNull String getTranslationKey(@NotNull Material material) { |
| 63 | + if (material.isBlock()) { |
| 64 | + return CraftMagicNumbers.getBlock(material).getDescriptionId(); |
| 65 | + } else { |
| 66 | + return postProcessingTranslationKey(CraftMagicNumbers.getItem(material).getDescriptionId()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private String postProcessingTranslationKey(String key) { |
| 71 | + return this.translationMapping.getOrDefault(key, key); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public @NotNull String getTranslationKey(@NotNull EntityType type) { |
| 76 | + Optional<net.minecraft.world.entity.EntityType<?>> op = net.minecraft.world.entity.EntityType.byString(type.getKey().toString()); |
| 77 | + if (op.isPresent()) { |
| 78 | + return postProcessingTranslationKey(op.get().getDescriptionId()); |
| 79 | + } else { |
| 80 | + return postProcessingTranslationKey("entity." + type.getKey()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public @NotNull String getTranslationKey(@NotNull PotionEffectType potionEffectType) { |
| 86 | + if (potionEffectType instanceof PotionEffectTypeWrapper wrapper) { |
| 87 | + potionEffectType = wrapper.getType(); |
| 88 | + } |
| 89 | + CraftPotionEffectType craftPotionEffectType = (CraftPotionEffectType) potionEffectType; |
| 90 | + return postProcessingTranslationKey(craftPotionEffectType.getHandle().getDescriptionId()); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public @NotNull String getTranslationKey(@NotNull Enchantment enchantment) { |
| 95 | + return postProcessingTranslationKey(localeManager.queryEnchantments(Map.of(enchantment, 1)).getOrDefault(enchantment, "Unknown")); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public @NotNull String getTranslationKey(@NotNull ItemStack stack) { |
| 100 | + return postProcessingTranslationKey(stack.getTranslationKey()); |
| 101 | + } |
| 102 | +} |
0 commit comments