Skip to content

Commit

Permalink
use registry access
Browse files Browse the repository at this point in the history
  • Loading branch information
NonSwag committed Jul 13, 2024
1 parent 3330820 commit e426ee1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.thenextlvl.tweaks.command.item;

import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
Expand All @@ -8,11 +10,9 @@
import net.thenextlvl.tweaks.command.api.CommandInfo;
import net.thenextlvl.tweaks.command.api.CommandSenderException;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -41,7 +41,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (namespacedKey == null)
return false;

var enchantment = Registry.ENCHANTMENT.get(namespacedKey);
var enchantment = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT).get(namespacedKey);
if (enchantment == null)
return false;

Expand Down Expand Up @@ -81,18 +81,18 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return null;

if (args.length == 1) {
return Registry.ENCHANTMENT.stream()
return RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT).stream()
.filter(enchantment -> enchantment.canEnchantItem(item)
&& (item.getEnchantments().keySet().stream().noneMatch(enchantment::conflictsWith)
|| item.getEnchantments().containsKey(enchantment)))
.map(enchantment -> enchantment.getKey().asString()).toList();
}
if (args.length == 2) {
NamespacedKey namespacedKey = NamespacedKey.fromString(args[0]);
var namespacedKey = NamespacedKey.fromString(args[0]);
if (namespacedKey == null)
return null;

Enchantment enchantment = Registry.ENCHANTMENT.get(namespacedKey);
var enchantment = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT).get(namespacedKey);
if (enchantment == null)
return null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package net.thenextlvl.tweaks.command.item;

import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.tweaks.TweaksPlugin;
import net.thenextlvl.tweaks.command.api.CommandInfo;
import net.thenextlvl.tweaks.command.api.CommandSenderException;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
Expand Down Expand Up @@ -45,7 +46,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

for (var key : args) {
var namespacedKey = NamespacedKey.fromString(key);
var enchantment = namespacedKey != null ? Registry.ENCHANTMENT.get(namespacedKey) : null;
var enchantment = namespacedKey != null ? RegistryAccess.registryAccess()
.getRegistry(RegistryKey.ENCHANTMENT).get(namespacedKey) : null;

if (enchantment == null) {
plugin.bundle().sendMessage(player, "enchantment.invalid", Placeholder.parsed("enchantment", key));
Expand All @@ -56,9 +58,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}

enchantments.forEach(enchantment -> {
var message = item.removeEnchantment(enchantment) != 0 ? "enchantment.removed" : "enchantment.absent";
var level = item.removeEnchantment(enchantment);
var message = level != 0 ? "enchantment.removed" : "enchantment.absent";
plugin.bundle().sendMessage(player, message, Placeholder.component("enchantment",
Component.translatable(enchantment.translationKey())));
enchantment.displayName(level).style(Style.empty())));
});

return true;
Expand Down

0 comments on commit e426ee1

Please sign in to comment.