-2

I'm currently coding a Minecraft Mod in Java and I have some issues about implementing the Exception correctly.

I tried in the Class below different things:

  • Before I used Maps instead of List
  • using Try and Catch Method
  • For Loop to get the ID

What I wanted is if you use the register Method and any of one of the If Statements goes true it throwing an Exception but it doesn't at all. It's like the if statements are always false.

Here is the Class:

public class SoulType {
    private static final List<SoulType> 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<SoulType> 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.");
    }
}

Also here is the Class where the Method is called:

public class SoulTypes {
    public static final SoulType HARMONY = SoulType.register("harmony", "Harmonie", SoulTypeRarities.COMMON, 0.0, 1.0);
    public static final SoulType BOLLWERK = SoulType.register("bollwerk", "Bollwerk", SoulTypeRarities.RARE, 0.8, 1.5);
}

I hope someone can help me out of this Issue.

4
  • 1
    I have a few things: 1 - how are you executing this code? Is there something else calling it? 2 - The method you created that is handling the exceptions is not being called anywhere? 3 - Add some loggers throughout your code (system out print) so you can see where/what your code is at/doing. This will help to check why the ifs are not throwing the exceptions or whether it is even reaching that code at all. Commented Jul 15, 2024 at 17:50
  • What @JorgeCampos said plus running it in debug mode with breakpoints at the right places to inspect values. Commented Jul 15, 2024 at 18:19
  • Terrible title. Rewrite to summarize your specific technical issue. Commented Jul 15, 2024 at 18:30
  • I modified it and added more details (other two codes and changed title) Commented Jul 15, 2024 at 18:56

1 Answer 1

1

I think the problem is here in your register() method:

    SOUL_TYPES.add(newType);
    for (SoulType soulType : SOUL_TYPES) {
            if (soulType.getId().equals(id)) {
                throw new IllegalArgumentException("SoulType with id '" + id + "' already exists.");
            }

this will always throw an exception because you are adding the soulType with the current id right before checking whether or not it has that ID.

Instead try:

    for (SoulType soulType : SOUL_TYPES) {
            if (soulType.getId().equals(id)) {
                throw new IllegalArgumentException("SoulType with id '" + id + "' already exists.");
            }
        }
    SOUL_TYPES.add(newType);

(basically just switch the order of the statements)

Besides that when I tested your code if the corruption/strength is outside the bounds it throws an error correctly and otherwise it doesn't which means it's working properly.

I'm not too familiar with switch statements in Java or Minecraft mods so I'll let someone else answer the second question, but just off the top of my head I have a suggestion:

In your registerCommand function in the SoulCommand class try putting the execute statement inside the if block like so:

    if (entity != null)
                direction = entity.getDirection();
                SoulCMDProcedure.execute(entity);

In this case you already know that the entity is not null so you can remove that check from the execute method and see whether that helps indicate what the issue might be through the use of print statements for debugging etc.

Hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

The thing is if I Run the Client/Minecraft nothing happens (If I start Minecraft in the environment). The only thing I can think of is that is coming from the IDE that I use that it doesn't show up.
That may be the case; I tested your SoulType class on my machine in VS code and it worked according to the specification. Unfortunately, I have no experience working with Minecraft mods so I can't really pinpoint what might be the problem. You may want to test your logic in a different IDE to see whether that's the issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.