Skip to content

How to get the material name? #843

@5RoD

Description

@5RoD

hey guys quick question about the API. im making a tool to check player history when they trigger a dupe alert, but i cant get the block names to show up. calling api.performLookup works but the results usually say the block is "0" or unknown. tried api.parseResult() and .getType() but it returns null a lot. am i missing something simple to get the real material name?

My code:

  // Parse block type
     if (actionTypeStr.equals("chat") || actionTypeStr.equals("command")) {
         // For chat/command, this field is the message/command
         action.setBlockType(result[6]); 
     } else {
         // Standard block/interaction handling (using ParseResult from API)
         Material type = parsed.getType();
         
         if (type != null) {
             action.setBlockType(type.name());
             
             // Check if it's a container
             if (isContainer(type)) {
                 action.setContainerType(type.name());
             }
         } else {
             // Fallback handling if ParseResult failed to resolve Material
             String rawType = result[6];

             if (rawType != null && !rawType.equals("0")) {
                 // Try to interpret rawType as Material name
                 Material fallbackType = Material.matchMaterial(rawType);
                 if (fallbackType != null) {
                     action.setBlockType(fallbackType.name());
                     if (isContainer(fallbackType)) {
                         action.setContainerType(fallbackType.name());
                     }
                 } else {
                     // Try to check if it represents a valid legacy ID > 0
                     try {
                         int id = Integer.parseInt(rawType);
                         if (id > 0) {
                             // It's a legacy ID we couldn't resolve
                             action.setBlockType("ID:" + id);
                         } else {
                             // It's 0 or negative, typically "0" which means unknown/standard
                         }
                     } catch (NumberFormatException e) {
                         // It's a string but not a material? Use distinct logic
                         action.setBlockType(rawType);
                     }
                 }
             }
             // If rawType is "0", we leave blockType as null, which displays as "Unknown" in GUI
         }
     }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions