Refactor code for creating and retrieving villager profession

Removed the deprecated use of OldEnum and Villager.Profession.valueOf in creating villagers and retrieving their professions. Refactored code to use the more updated NamespacedKey and Registry methods to handle these tasks, enhancing both code stability and maintainability.
This commit is contained in:
Teriuihi 2024-07-20 01:35:20 +02:00
parent 30a7d85f3d
commit ab9a7b22cb
3 changed files with 20 additions and 5 deletions

View File

@ -17,7 +17,6 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.util.OldEnum;
import java.util.*;
import java.util.stream.Collectors;
@ -49,7 +48,7 @@ public class CommandCreateVillager extends SubCommand {
commandSender.sendMiniMessage(getHelpMessage(), null);
return true;
}
Location location = new Location(world, Double.parseDouble(args[3]),Double.parseDouble(args[4]),Double.parseDouble(args[5]), Float.parseFloat(args[6]), Float.parseFloat(args[7]));
Location location = new Location(world, Double.parseDouble(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Float.parseFloat(args[6]), Float.parseFloat(args[7]));
Villager villager = (Villager) world.spawnEntity(location, EntityType.VILLAGER, CreatureSpawnEvent.SpawnReason.CUSTOM);
villager.setPersistent(true);
villager.setInvulnerable(true);
@ -82,7 +81,9 @@ public class CommandCreateVillager extends SubCommand {
case 2 -> res.addAll(VillagerTypeManager.getVillagerTypes().stream()
.map(VillagerType::getName)
.collect(Collectors.toList()));
case 3 -> res.addAll(Arrays.stream(Villager.Type.values()).map(OldEnum::name).collect(Collectors.toList()));
case 3 -> res.addAll(Arrays.stream(Villager.Type.values())
.map(something -> something.name())
.collect(Collectors.toList()));
case 4 -> {
if (commandSender instanceof Player player) {
res.add(String.valueOf(Utilities.round(player.getLocation().getX(), 2)));

View File

@ -2,6 +2,8 @@ package com.alttd.objects;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.entity.Villager;
import org.bukkit.inventory.ItemStack;
@ -24,10 +26,15 @@ public class BlackMarketVillagerType implements VillagerType {
public BlackMarketVillagerType(String name, String displayName, String profession, int maxAvailableItems, int maxTradesPerReboot, Set<ItemStack> trading) {
this.name = name;
this.displayName = displayName;
this.profession = Villager.Profession.valueOf(profession.toUpperCase());
this.maxAvailableItems = maxAvailableItems;
this.maxTradesPerReboot = maxTradesPerReboot;
this.trading = trading;
this.profession = getProfession(profession.toLowerCase());
}
Villager.Profession getProfession(String professionName) {
NamespacedKey namespacedKey = NamespacedKey.minecraft(professionName);
return Registry.VILLAGER_PROFESSION.get(namespacedKey);
}
private Set<ItemStack> getResizedCustomTradingSet() {

View File

@ -2,6 +2,8 @@ package com.alttd.objects;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.entity.Villager;
import org.bukkit.inventory.ItemStack;
@ -22,7 +24,12 @@ public class ShopVillagerType implements VillagerType{
this.displayName = displayName;
this.buying = buying;
this.selling = selling;
this.profession = Villager.Profession.valueOf(profession.toUpperCase());
this.profession = getProfession(profession.toLowerCase());
}
Villager.Profession getProfession(String professionName) {
NamespacedKey namespacedKey = NamespacedKey.minecraft(professionName);
return Registry.VILLAGER_PROFESSION.get(namespacedKey);
}
public String getName() {