Advertisement
PaleoCrafter

Untitled

Jun 11th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. public class BlockPattern
  2. {
  3.     public interface PatternElement
  4.     {
  5.         boolean match(World world, BlockPos position);
  6.     }
  7.  
  8.     public static class Layer
  9.     {
  10.         public static class Row
  11.         {
  12.             private final List<PatternElement> elements;
  13.  
  14.             public Row(List<PatternElement> elements)
  15.             {
  16.                 this.elements = elements;
  17.             }
  18.  
  19.             public boolean match(World world, BlockPos position)
  20.             {
  21.                 for (int z = 0; z < elements.size(); z++)
  22.                     if (!elements.get(z).match(world, position.add(0, 0, z)))
  23.                         return false;
  24.                 return true;
  25.             }
  26.         }
  27.  
  28.         private final List<Row> rows;
  29.         private final int minOccurrence;
  30.         private final int maxOccurrence;
  31.  
  32.         public Layer(List<Row> rows, int minOccurrence, int maxOccurrence)
  33.         {
  34.             this.rows = rows;
  35.             this.minOccurrence = minOccurrence;
  36.             this.maxOccurrence = maxOccurrence;
  37.         }
  38.  
  39.         public int match(World world, BlockPos position)
  40.         {
  41.             int y = 0;
  42.             occurrences:
  43.             for (; y < maxOccurrence; y++)
  44.             {
  45.                 for (int x = 0; x < rows.size(); x++)
  46.                 {
  47.                     Row row = rows.get(x);
  48.                     boolean rowMatch = row.match(world, position.add(x, y, 0));
  49.                     if (!rowMatch)
  50.                     {
  51.                         y -= 1;
  52.                         break occurrences;
  53.                     }
  54.                 }
  55.             }
  56.             if (y < minOccurrence)
  57.                 return -1;
  58.             return y + 1;
  59.         }
  60.     }
  61.  
  62.     private final List<Layer> layers;
  63.  
  64.     public BlockPattern(List<Layer> layers)
  65.     {
  66.         this.layers = layers;
  67.     }
  68.  
  69.     public boolean match(World world, BlockPos position)
  70.     {
  71.         int y = 0;
  72.         for (Layer layer : layers)
  73.         {
  74.             int match = layer.match(world, position.add(0, y, 0));
  75.             if (match == -1)
  76.                 return false;
  77.             else
  78.                 y += match;
  79.         }
  80.         return true;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement