Testing if block material is in list of materials acting strange

Discussion in 'Plugin Development' started by jumbledProgram, Aug 25, 2020.

Thread Status:
Not open for further replies.
  1. I have a list of materials with this code:
    Code:
    Material[] infectKeep = {Material.AIR,Material.CAVE_AIR,Material.OBSIDIAN,Material.CRYING_OBSIDIAN,Material.NETHER_PORTAL,Material.END_PORTAL,Material.END_PORTAL_FRAME,Material.LAVA,Material.WATER,Material.BEDROCK};
    Material[] infectAir = {Material.VINE,Material.TWISTING_VINES,Material.TWISTING_VINES_PLANT,Material.WEEPING_VINES,Material.WEEPING_VINES_PLANT
                ,Material.BROWN_MUSHROOM,Material.RED_MUSHROOM,Material.SUNFLOWER,Material.TALL_GRASS,Material.TALL_SEAGRASS,Material.GRASS,Material.SEAGRASS};
    
    and i have some code to test for what to do with these blocks
    Code:
                      
    for(int z = -3; z < 4; z++) {
    for(int y = -3; y < 4; y++) {
    for(int x = -3; x < 4; x++) {
    var loc = player.getLocation().add(x, y, z);
        if (loc.getBlock().getType().equals(infectKeep)) {
          loc.getBlock().setType(loc.getBlock().getType());
        } else if (loc.getBlock().getType().equals(infectAir)) {
          loc.getBlock().setType(Material.AIR);
        } else {
          loc.getBlock().setType(Material.LIME_CONCRETE);
        }
     }
     }
     }
    }
    but strange things are happening, like vines, crimson vines and twisted vines are staying and grass and tall grass being set to lime concrete
     
  2. Online

    timtower Administrator Administrator Moderator

    @jumbledProgram getType()==infectedKeep
    But it is better if you just don't set those infectedKeep blocks
     
  3. hmmm, changing if (loc.getBlock().getType()==infectKeep) {
    to if (loc.getBlock().getType().equals(infectKeep)) {

    results in the error Incompatible operand types Material and Material[]
     
  4. Online

    timtower Administrator Administrator Moderator

    @jumbledProgram Then you need to check if infectedKeep contains the material of the block.
     
  5. Offline

    Strahan

    Yea, that's what I'd be doing. Checking it the type is in the array. Also I'd not be hard coding the arrays. You never know when/if you may want to tweak something. Personally, I'd do it like:
    Code:
    List<String> keep = plugin.getConfig().getStringList("keep");
    List<String> air = plugin.getConfig().getStringList("air");
    String newType = plugin.getConfig().getString("newtype", "LIME_CONCRETE");
    
    try {
      Material newMaterial = Material.matchMaterial(newType);
    
      for(int z = -3; z < 4; z++) {
        for(int y = -3; y < 4; y++) {
          for(int x = -3; x < 4; x++) {
            Location loc = player.getLocation().add(x, y, z);
            String mat = loc.getBlock().getType().name();
    
            if (keep.contains(mat)) continue;
            if (air.contains(mat)) {
              loc.getBlock().setType(Material.AIR);
              continue;
            }
         
            loc.getBlock().setType(newMaterial);
          }
        }
      }
    } catch (IllegalArgumentException ex) {
      plugin.getLogger().warning("Invalid material specified for infected replacement! (" + newType + ")");
    }
     
  6. if(infectKeep.contains(loc.getBlock().getType())) gets the error Cannot invoke contains(Material) on the array type Material[]
    (quoting timtower)
     
  7. Online

    timtower Administrator Administrator Moderator

    @jumbledProgram Arrays do that.
    That is why I use Lists for that instead.
     
Thread Status:
Not open for further replies.

Share This Page