Я пробовал в классе ниже разные вещи:
- Раньше я использовал Карты вместо списка.
- Использовал метод Try and Catch.
- Цикл для получения идентификатор
Вот класс:
Код: Выделить всё
public class SoulType {
private static final List SOUL_TYPES = new ArrayList();
private final String id;
private final String displayName;
private final SoulTypeRarities rarity;
private final double corruption;
private double strength;
private SoulType(String id, String displayName, SoulTypeRarities rarity, double corruption, double strength) {
this.id = id;
this.displayName = displayName;
this.rarity = rarity;
this.corruption = corruption;
this.strength = strength;
}
public static List getSoulTypes() { // Neue Getter-Methode
return SOUL_TYPES;
}
public String getId() {
return id;
}
public String getDisplayName() {
return displayName;
}
public SoulTypeRarities getRarity() {
return rarity;
}
public Double getCorruption() {
return corruption;
}
public Double getStrength() {
return strength;
}
public void setStrength(double newStrength) {
this.strength = newStrength;
}
public void addStrength(double addStrength) {
this.strength += addStrength;
}
public static SoulType register(String id, String displayName, SoulTypeRarities rarity, double corruption, double strength) throws IllegalArgumentException {
double minCorruption = 0;
double maxCorruption = 2;
double minStrength = 0;
double maxStrength = 5;
if (corruption < minCorruption || corruption > maxCorruption) {
throw new IllegalArgumentException("Corruption must be between " + minCorruption + " and " + maxCorruption + " .");
}
if (strength < minStrength || strength > maxStrength) {
throw new IllegalArgumentException("Strength must be between " + minStrength + " and " + maxStrength + " .");
}
SoulType newType = new SoulType(id, displayName, rarity, corruption, strength);
SOUL_TYPES.add(newType);
for (SoulType soulType : SOUL_TYPES) {
if (soulType.getId().equals(id)) {
throw new IllegalArgumentException("SoulType with id '" + id + "' already exists.");
}
}
return newType;
}
public static SoulType fromId(String id) {
for (SoulType soulType : SOUL_TYPES) {
if (soulType.getId().equals(id)) {
return soulType;
}
}
throw new IllegalArgumentException("SoulType with id '" + id + "' not found.");
}
}
Вот класс:
Код: Выделить всё
public class SoulCMDProcedure {
public static void execute(Entity entity) {
if (entity == null)
return;
if (entity instanceof Player player && !player.level().isClientSide()) {
player.displayClientMessage(Component.literal("Registered Soul Types:"), false);
for (SoulType soulType : SoulType.getSoulTypes()) {
ChatFormatting rarityColor = switch (soulType.getRarity()) {
case COMMON -> ChatFormatting.WHITE;
case UNCOMMON -> ChatFormatting.GREEN;
case RARE -> ChatFormatting.BLUE;
case EPIC -> ChatFormatting.DARK_PURPLE;
case LEGENDARY -> ChatFormatting.GOLD;
};
ChatFormatting resetColor = ChatFormatting.RESET;
String message = String.format("- %s (ID: %s, Rarity: %s, Corruption: %.2f, Strength: %.2f)", soulType.getDisplayName(), soulType.getId(), rarityColor + soulType.getRarity().toString() + resetColor, soulType.getCorruption(),
soulType.getStrength());
player.displayClientMessage(Component.literal(message), false);
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... how-i-want