Block pattern recognition

Discussion in 'Plugin Development' started by Clavus, Feb 25, 2012.

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

    Clavus

    I was wondering what are good ways to implement block pattern recognition. I'm planning on making a teleporter plugin that requires you to create a specific setup of blocks (a square of iron blocks with a wool 'color code' in the middle to link teleporters). Now I could just check block place / break events (do those trigger on explosions too?) and see if the teleporter is still intact or being created with long world.getBlockAt if / else trees, but there has to be a better way.

    Has anyone ever written a library for this? Any other good tips?
     
  2. Offline

    heisan213

    Oooo, watchin' this! :D
     
  3. Offline

    Clavus

    Nobody? :(

    If we'd have a general library for recognizing block patterns in Minecraft, you could do a ton of things with it because you could identify when a player build a specific structure.
     
  4. Offline

    Sphax

    I would be interested in this.

    Is there a way to create our own block pattern recognition (like Iron Golem or Snow Golem) ?
    That would be cool to be able to create Mobs and/or other blocks (for example the pattern would disapear to create another pattern instead or 1 or more Mobs)
     
  5. Offline

    dillyg10

    If u want, I can write code...
    But basically, just get block place n break events, probabbly use some sort of hashmap system, and it should work :D.
     
  6. Offline

    desht

    Probably the best, most general, way to do this is to create a class for it, let's call it BlockPattern. To use it:
    • Instantiate an object of the class with a 3d template of material values & data ids. The class can store this as an y/x/z array of material/data pairs (the Bukkit MaterialData class should suffice here, although it doesn't provide a method for constructing a MaterialData from a String description so maybe subclass it).
    • To do a match, you'd call a match() method on your BlockPattern object, giving it a Location to start the match at.
    • You'd also want to provide an setAnchor() method, to specify which position of your template the match should start at.
    Example usage of this hypothetical class:
    PHP:
    MaterialData stone = new MaterialData(1);
    MaterialData blueWool = new MaterialData(3511);
    // create a 1-block tall and 3x3 horizontal template which is a cross of blue wool on stone
    MaterialData[][][] tmpl =
    {
      {
        { 
    stoneblueWoolstone },
        { 
    blueWoolblueWoolblueWool },
        { 
    stoneblueWoolstone },
      }
    };
    BlockPattern bp = new BlockPattern(tmpl);
    bp.setAnchor(011);  // y-x-z: anchor the match on the centre block
    Location loc someLocation;
    if (
    bp.match(loc)) {
      
    // we have a match!
    }
    The above code will match if there is a cross of blue wool on a stone background centred at the given location.

    We could also permit null for template elements for a "don't care" setting (and allow -1 for the data byte for the same thing).

    Hmm, I might have to go and implement this now :)
     
  7. Offline

    zaph34r

    Just stumbled across this thread, so in case anyone is still watching this:
    I am currently experimenting with block-based buildings that do stuff. At the moment it can match against .schematic files, recognizes patterns rotated in 90° increments around the y-axis, and can place stuff into the world using worldedit as a dependency. I'm using it for providing buildable teleporters and other buildings that have effects on players, and can be used by them, kind of inspired by Runecraft, with the difference that the buildings require some valuable materials to construct (and some to use), stay in the world, and are destructable if a player decides to trash it or some creeper is feeling particularly evil at the moment, in which case the valuable building materials are consumed.
    Source is not yet uploaded anywhere as it is fairly early in development, but feel free to drop me a PM if you want to look at it :) It is in python, but i guess the translation to java is pretty straightforward.
     
Thread Status:
Not open for further replies.

Share This Page