Skip to content

Brittle Modifier (TCon Compat) causes server crash when on 1 durability #485

@CrafterDaemon

Description

@CrafterDaemon

In TraitBrittle.class,

The crash is happening because of this line:

Integer damageDone = random.nextInt(Math.min(5, durability - 1));

The problem:
When durability = 1:

  • durability - 1 = 0
  • Math.min(5, 0) = 0
  • random.nextInt(0) -> IllegalArgumentException: bound must be positive

Random.nextInt(n) requires n > 0. You can't call nextInt(0).

fix:

public void beforeBlockBreak(@Nonnull ItemStack tool, @Nonnull BreakEvent event) {
   Block block = event.getState().func_177230_c();
   if (block.func_176223_P().func_185904_a() == Material.field_151576_e) {
      Integer durability = ToolHelper.getCurrentDurability(tool);
      if (durability > 1) {  // Add this check
         Integer damageDone = random.nextInt(Math.min(5, durability - 1));
         ToolHelper.damageTool(tool, damageDone, event.getPlayer());
      }
   }
}

or you can just do

Integer maxDamage = Math.max(1, Math.min(5, durability - 1)); // Ensure at least 1
Integer damageDone = random.nextInt(maxDamage);

Found this while working on my modpack, thought I should report it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions