Solved Validate Block Data with Material

Discussion in 'Plugin Development' started by Guru2764, Aug 27, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Guru2764

    Hello!

    I am making a world-editing plugin, and I am having trouble with validating block data.

    What happens is:
    1. User types a command with coordinates, a material type, and block data
    2. The plugin takes the information, and places that material with that block data at the location specified
    I would like to verify that the block data will work with the material before executing the command. For example, if someone inputs 'oak_stairs' I would like to check whether the data, for example 'NORTH' for the direction it faces will work for that block.

    I already have the code working for testing whether the 'NORTH' string works, and whether 'oak_stairs' is a valid block, but I cannot figure out a way to test whether that direction is valid without trying it on a physical block in the world.

    Example data will be formatted like 'facing:NORTH'

    Code:
    public static boolean dataValidate(Material material,String data) {
        if(data.isEmpty())
          return false;
        else {
          String dataHeader = StringUtils.substringBeforeLast(data,":");
          switch(dataHeader) {
            case "facing":
             //This is where I don't know how to connect it to the other method without finding a block in the world and making it the material type
            default:
              return false;
          }
        }
      }
    
    public static boolean validateFacing(Block currentBlock,String data,CommandSender user) {
        String dataContent = (StringUtils.substringAfterLast(data, ":")).toUpperCase();
        if(currentBlock.getBlockData() instanceof Directional) {
          try {
            //This is where it tests it on a real block
            Directional dir=(Directional)currentBlock.getBlockData();
            dir.setFacing(BlockFace.valueOf(dataContent));
            } catch(Exception ec) {
              user.sendMessage(ChatColor.RED+dataContent+" is not a valid direction for this block!");
              return false;
            }
        }
        else {
          user.sendMessage(ChatColor.RED+"You cannot set a direction for this block!");
          return false;
        }
        return true;
      }
    
    It doesn't seem to be that I can test that without performing the action on a physical block. Is my only option that avoids changing blocks to hard-code each block type and see if the inputted data matches the list of data it can have applied to it?

    The reason I want to not test it on a physical block is because I want to use these same methods for behind-the-scenes actions elsewhere.

    Thanks for any help,

    Guru
     
    Last edited: Aug 27, 2020
  2. Offline

    KarimAKL

    @Guru2764 What do you mean by "direction is valid"? How would it be invalid?
    It sounds like you just want to check if a field in the BlockFace enum by that name exists.
     
  3. Offline

    Guru2764

    Hello,

    Thank you for your reply!

    That wouldn't have worked for me because not every BlockFace can be applied to every Block type, like for example stairs only have 4 BlockFaces they can use, so I would have had to have gotten a Directional from a BlockData and ran the getFaces method. I think I may have found a solution though. I just found this method under Material:

    createBlockData()

    I will test and see if it works.

    EDIT:

    Thank you again, and for anyone else who's having the same issue:

    [material].createBlockData() will create a functioning BlockData you can test block states on.

    Marked as solved

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 30, 2020
Thread Status:
Not open for further replies.

Share This Page