spenk

Untitled

Mar 4th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.15 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.logging.Level;
  3.  
  4. public class Craft
  5. {
  6.   public static ArrayList<Craft> craftList = new ArrayList<Craft>();
  7.   CraftType type;
  8.   String name;
  9.   short[][][] matrix;
  10.   ArrayList<Craft.DataBlock> dataBlocks;
  11.   ArrayList<Craft.CraftComplexBlock> complexBlocks;
  12.   int sizeX = 0;
  13.   int sizeZ = 0;
  14.   int sizeY = 0;
  15.   int posX;
  16.   int posY;
  17.   int posZ;
  18.   int blockCount = 0;
  19.   int flyBlockCount = 0;
  20.   int maxBlocks;
  21.   int waterLevel = -1;
  22.   int newWaterLevel = -1;
  23.  
  24.   short waterType = 0;
  25.  
  26.   int minX = 0;
  27.   int maxX = 0;
  28.   int minY = 0;
  29.   int maxY = 0;
  30.   int minZ = 0;
  31.   int maxZ = 0;
  32.   static Player player;
  33.   int speed = 1;
  34.  
  35.   long lastMove = System.currentTimeMillis();
  36.   boolean haveControl = true;
  37.   boolean isOnBoard = true;
  38.   String customName = null;
  39.  
  40.   boolean blockPlaced = false;
  41.  
  42.   Craft(CraftType type, Player player, String customName)
  43.   {
  44.     this.type = type;
  45.     name = type.name;
  46.     this.customName = customName;
  47.     Craft.player = player;
  48.   }
  49.  
  50.   public static Craft getCraft(Player player)
  51.   {
  52.     if (craftList.isEmpty()) return null;
  53.  
  54.     for (Craft craft : craftList) {
  55.       if (Craft.player.getName().equalsIgnoreCase(player.getName())) {
  56.         return craft;
  57.       }
  58.     }
  59.  
  60.     return null;
  61.   }
  62.  
  63.   public static Craft getCraft(int x, int y, int z)
  64.   {
  65.     if (craftList.isEmpty()) return null;
  66.  
  67.     for (Craft craft : craftList) {
  68.       if (craft.isIn(x, y, z)) {
  69.         return craft;
  70.       }
  71.     }
  72.     return null;
  73.   }
  74.  
  75.   public void addBlock(Block block)
  76.   {
  77.     int x = block.getX() - posX;
  78.     int y = block.getY() - posY;
  79.     int z = block.getZ() - posZ;
  80.  
  81.     if (((x < sizeX - 1) && (!isFree(matrix[(x + 1)][y][z]))) || ((x > 0) && (!isFree(matrix[(x - 1)][y][z]))) || ((y < sizeY - 1) && (!isFree(matrix[x][(y + 1)][z]))) || ((y > 0) && (!isFree(matrix[x][(y - 1)][z]))) || ((z < sizeZ - 1) && (!isFree(matrix[x][y][(z + 1)]))) || ((z > 0) && (!isFree(matrix[x][y][(z - 1)]))))
  82.     {
  83.       short blockId = (short)block.getType();
  84.  
  85.       if (blockId == 331) {
  86.         blockId = 55;
  87.       } else if (blockId == 323) {
  88.         blockId = 68;
  89.       } else if (blockId == 324) {
  90.         blockId = 64;
  91.         matrix[x][(y + 1)][z] = blockId;
  92.         dataBlocks.add(new Craft.DataBlock(x, y + 1, z, block.getData() + 8));
  93.         blockCount += 1;
  94.       }
  95.       else if (blockId == 330) {
  96.         blockId = 71;
  97.         matrix[x][(y + 1)][z] = blockId;
  98.         dataBlocks.add(new Craft.DataBlock(x, y + 1, z, block.getData() + 8));
  99.         blockCount += 1;
  100.       }
  101.       else if (blockId == 338) {
  102.         blockId = 83;
  103.       } else if (blockId >= 256) {
  104.         return;
  105.       }
  106.  
  107.       matrix[x][y][z] = blockId;
  108.  
  109.       if (BlocksInfo.isDataBlock(blockId)) {
  110.         dataBlocks.add(new Craft.DataBlock(x, y, z, block.getData()));
  111.       }
  112.  
  113.       blockCount += 1;
  114.     }
  115.   }
  116.  
  117.   public boolean isIn(int x, int y, int z)
  118.   {
  119.     return (x >= posX) && (x < posX + sizeX) && (y >= posY) && (y < posY + sizeY) && (z >= posZ) && (z < posZ + sizeZ);
  120.   }
  121.  
  122.   static void addCraft(Craft craft) {
  123.     craftList.add(craft);
  124.   }
  125.  
  126.   static void removeCraft(Craft craft) {
  127.     craftList.remove(craft);
  128.   }
  129.  
  130.   private boolean canGoThrough(int blockId, int data)
  131.   {
  132.     if ((blockId == 0) || ((blockId >= 8) && (blockId <= 11) && (data != 0))) return true;
  133.  
  134.     if ((!type.canNavigate) && (!type.canDive)) return false;
  135.  
  136.     if (((blockId == 8) || (blockId == 9)) &&
  137.       (waterType == 8)) return true;
  138.  
  139.     if (((blockId == 10) || (blockId == 11)) &&
  140.       (waterType == 10)) return true;
  141.  
  142.     return (blockId == 79) && (type.iceBreaker) &&
  143.       (waterType == 8);
  144.   }
  145.  
  146.   private static boolean isFree(int blockId)
  147.   {
  148.     return (blockId == 0) || (blockId == -1);
  149.   }
  150.  
  151.   @SuppressWarnings("unused")
  152. private static boolean isAirOrWater(int blockId)
  153.   {
  154.     return (blockId == 0) || ((blockId >= 8) && (blockId <= 11));
  155.   }
  156.  
  157.   public boolean isOnCraft(Player player, boolean precise)
  158.   {
  159.     int x = (int)Math.floor(player.getX());
  160.     int y = (int)Math.floor(player.getY());
  161.     int z = (int)Math.floor(player.getZ());
  162.  
  163.     if (isIn(x, y - 1, z))
  164.     {
  165.       if (!precise) return true;
  166.  
  167.       if (matrix[(x - posX)][(y - posY - 1)][(z - posZ)] != -1) {
  168.         return true;
  169.       }
  170.     }
  171.  
  172.     return false;
  173.   }
  174.  
  175.   public static void setBlock(int id, int x, int y, int z)
  176.   {
  177.     if ((y < 0) || (y > 127) || (id < 0) || (id > 255)) {
  178.       MoveCraft.log.log(Level.SEVERE, "Invalid setBlock : id=" + id + " x=" + x + " y=" + y + " z=" + z);
  179.       return;
  180.     }
  181.  
  182.     player.getWorld().setBlockAt(id, x, y, z);
  183.   }
  184.  
  185.   private boolean isCraftBlock(int x, int y, int z)
  186.   {
  187.     if ((x >= 0) && (y >= 0) && (z >= 0) && (x < sizeX) && (y < sizeY) && (z < sizeZ))
  188.     {
  189.       return matrix[x][y][z] != -1;
  190.     }
  191.     return false;
  192.   }
  193.  
  194.   public boolean canMove(int dx, int dy, int dz)
  195.   {
  196.     dx = speed * dx;
  197.     dz = speed * dz;
  198.  
  199.     if (Math.abs(speed * dy) > 1) {
  200.       dy = speed * dy / 2;
  201.       if (Math.abs(dy) == 0) dy = (int)Math.signum(dy);
  202.  
  203.     }
  204.  
  205.     if ((posY + dy < 0) || (posY + sizeY + dy > 128)) {
  206.       return false;
  207.     }
  208.  
  209.     if (isOnCraft(player, true))
  210.     {
  211.       int X = (int)Math.floor(player.getX()) + dx;
  212.       int Y = (int)Math.floor(player.getY()) + dy;
  213.       int Z = (int)Math.floor(player.getZ()) + dz;
  214.  
  215.       if (((!isCraftBlock(X - posX, Y - posY, Z - posZ)) && (!canGoThrough(player.getWorld().getBlockIdAt(X, Y, Z), 0))) || ((!isCraftBlock(X - posX, Y + 1 - posY, Z - posZ)) && (!canGoThrough(player.getWorld().getBlockIdAt(X, Y + 1, Z), 0))))
  216.       {
  217.         return false;
  218.       }
  219.     }
  220.  
  221.     newWaterLevel = waterLevel;
  222.  
  223.     for (int x = 0; x < sizeX; x++) {
  224.       for (int z = 0; z < sizeZ; z++) {
  225.         for (int y = 0; y < sizeY; y++)
  226.         {
  227.           if ((isFree(matrix[x][y][z])) || (isCraftBlock(x + dx, y + dy, z + dz))) {
  228.             continue;
  229.           }
  230.           int blockId = player.getWorld().getBlockIdAt(posX + x + dx, posY + y + dy, posZ + z + dz);
  231.           int blockData = player.getWorld().getBlockData(posX + x + dx, posY + y + dy, posZ + z + dz);
  232.  
  233.           if ((dy < 0) && (blockId >= 8) && (blockId <= 11))
  234.           {
  235.             if (y > newWaterLevel) {
  236.               newWaterLevel = y;
  237.             }
  238.           }
  239.           else if ((dy > 0) && (blockId == 0))
  240.           {
  241.             if (y - 1 < newWaterLevel) {
  242.               newWaterLevel = (y - 1);
  243.             }
  244.           }
  245.  
  246.           if (!canGoThrough(blockId, blockData))
  247.           {
  248.             return false;
  249.           }
  250.         }
  251.       }
  252.  
  253.     }
  254.  
  255.     return true;
  256.   }
  257.  
  258.   public void move(int dx, int dy, int dz)
  259.   {
  260.     dx = speed * dx;
  261.     dz = speed * dz;
  262.  
  263.     if (Math.abs(speed * dy) > 1) {
  264.       dy = speed * dy / 2;
  265.       if (Math.abs(dy) == 0) dy = (int)Math.signum(dy);
  266.  
  267.     }
  268.  
  269.     for (int x = 0; x < sizeX; x++) {
  270.       for (int y = 0; y < sizeY; y++) {
  271.         for (int z = 0; z < sizeZ; z++) {
  272.           int craftBlockId = matrix[x][y][z];
  273.  
  274.           if ((craftBlockId == -1) || (craftBlockId == 0) || ((craftBlockId >= 8) && (craftBlockId <= 11)))
  275.             continue;
  276.           int blockId = player.getWorld().getBlockIdAt(posX + x, posY + y, posZ + z);
  277.  
  278.           if ((craftBlockId == 46) && (type.bomber)) {
  279.             continue;
  280.           }
  281.           if ((blockId == 0) || ((blockId >= 8) && (blockId <= 11))) {
  282.             if ((waterType != 0) && (y <= waterLevel))
  283.               matrix[x][y][z] = 0;
  284.             else {
  285.               matrix[x][y][z] = -1;
  286.             }
  287.             blockCount -= 1;
  288.           }
  289.         }
  290.  
  291.       }
  292.  
  293.     }
  294.  
  295.     for (Craft.CraftComplexBlock complexBlock : complexBlocks)
  296.     {
  297.       complexBlock.data = player.getWorld().getComplexBlock(posX + complexBlock.x, posY + complexBlock.y, posZ + complexBlock.z);
  298.     }
  299.  
  300.     for (int x = 0; x < sizeX; x++) {
  301.       for (int z = 0; z < sizeZ; z++) {
  302.         for (int y = 0; y < sizeY; y++)
  303.         {
  304.           short blockId = matrix[x][y][z];
  305.  
  306.           if (!BlocksInfo.needsSupport(blockId))
  307.           {
  308.             continue;
  309.           }
  310.  
  311.           if (((blockId == 64) || (blockId == 71)) &&
  312.             (player.getWorld().getBlockData(posX + x, posY + y, posZ + z) >= 8))
  313.           {
  314.             continue;
  315.           }
  316.           setBlock(0, posX + x, posY + y, posZ + z);
  317.         }
  318.  
  319.       }
  320.  
  321.     }
  322.  
  323.     for (int x = 0; x < sizeX; x++) {
  324.       for (int z = 0; z < sizeZ; z++) {
  325.         for (int y = 0; y < sizeY; y++)
  326.         {
  327.           short blockId = matrix[x][y][z];
  328.  
  329.           if (blockId == -1) {
  330.             continue;
  331.           }
  332.           if ((x - dx >= 0) && (y - dy >= 0) && (z - dz >= 0) && (x - dx < sizeX) && (y - dy < sizeY) && (z - dz < sizeZ))
  333.           {
  334.             if ((matrix[(x - dx)][(y - dy)][(z - dz)] == -1) || (BlocksInfo.needsSupport(matrix[(x - dx)][(y - dy)][(z - dz)]))) {
  335.               if ((y > waterLevel) || ((!type.canNavigate) && (!type.canDive)))
  336.                 setBlock(0, posX + x, posY + y, posZ + z);
  337.               else {
  338.                 setBlock(waterType, posX + x, posY + y, posZ + z);
  339.               }
  340.             }
  341.  
  342.           }
  343.           else if ((y > waterLevel) || ((!type.canNavigate) && (!type.canDive)))
  344.             setBlock(0, posX + x, posY + y, posZ + z);
  345.           else {
  346.             setBlock(waterType, posX + x, posY + y, posZ + z);
  347.           }
  348.  
  349.           if (BlocksInfo.needsSupport(blockId)) {
  350.             continue;
  351.           }
  352.           if ((x + dx >= 0) && (y + dy >= 0) && (z + dz >= 0) && (x + dx < sizeX) && (y + dy < sizeY) && (z + dz < sizeZ)) {
  353.             if (matrix[x][y][z] != matrix[(x + dx)][(y + dy)][(z + dz)]) {
  354.               setBlock(blockId, posX + dx + x, posY + dy + y, posZ + dz + z);
  355.             }
  356.           }
  357.           else
  358.           {
  359.             setBlock(blockId, posX + dx + x, posY + dy + y, posZ + dz + z);
  360.           }
  361.         }
  362.  
  363.       }
  364.  
  365.     }
  366.  
  367.     Block block = new Block();
  368.  
  369.     for (Craft.DataBlock dataBlock : dataBlocks)
  370.     {
  371.       if (BlocksInfo.needsSupport(matrix[dataBlock.x][dataBlock.y][dataBlock.z])) {
  372.         block.setX(posX + dx + dataBlock.x);
  373.         block.setY(posY + dy + dataBlock.y);
  374.         block.setZ(posZ + dz + dataBlock.z);
  375.  
  376.         block.setType(matrix[dataBlock.x][dataBlock.y][dataBlock.z]);
  377.         block.setData(dataBlock.data);
  378.         block.update();
  379.       }
  380.       else
  381.       {
  382.        block.getWorld().setBlockData(posX + dx + dataBlock.x, posY + dy + dataBlock.y, posZ + dz + dataBlock.z, dataBlock.data);
  383.       }
  384.  
  385.     }
  386.  
  387.     for (int x = 0; x < sizeX; x++) {
  388.       for (int z = 0; z < sizeZ; z++) {
  389.         for (int y = 0; y < sizeY; y++)
  390.         {
  391.           short blockId = matrix[x][y][z];
  392.  
  393.           if ((!BlocksInfo.needsSupport(blockId)) || (BlocksInfo.isDataBlock(blockId)))
  394.             continue;
  395.           setBlock(blockId, posX + dx + x, posY + dy + y, posZ + dz + z);
  396.         }
  397.  
  398.       }
  399.  
  400.     }
  401.  
  402.     for (Craft.CraftComplexBlock complexBlock : complexBlocks)
  403.     {
  404.       if ((complexBlock.data instanceof Sign))
  405.       {
  406.         Sign sign = (Sign)block.getWorld().getComplexBlock(posX + dx + complexBlock.x, posY + dy + complexBlock.y, posZ + dz + complexBlock.z);
  407.  
  408.         sign.setText(0, ((Sign)(Sign)complexBlock.data).getText(0));
  409.         sign.setText(1, ((Sign)(Sign)complexBlock.data).getText(1));
  410.         sign.setText(2, ((Sign)(Sign)complexBlock.data).getText(2));
  411.         sign.setText(3, ((Sign)(Sign)complexBlock.data).getText(3));
  412.         sign.update();
  413.       }
  414.  
  415.     }
  416.  
  417.     for (Player p : etc.getServer().getPlayerList()) {
  418.       if ((p.getX() >= posX) && (p.getX() < posX + sizeX) && (p.getY() >= posY) && (p.getY() <= posY + sizeY) && (p.getZ() >= posZ) && (p.getZ() < posZ + sizeZ)) {
  419.         p.teleportTo(p.getX() + dx, p.getY() + dy, p.getZ() + dz, p.getRotation(), p.getPitch());
  420.       }
  421.     }
  422.  
  423.     posX += dx;
  424.     posY += dy;
  425.     posZ += dz;
  426.  
  427.     minX = posX;
  428.     minY = posY;
  429.     minZ = posZ;
  430.     maxX = (posX + sizeX - 1);
  431.     maxY = (posY + sizeY - 1);
  432.     maxZ = (posZ + sizeZ - 1);
  433.  
  434.     if ((waterLevel == sizeY - 1) && (newWaterLevel < waterLevel))
  435.     {
  436.       waterLevel = newWaterLevel;
  437.     }
  438.     else if ((waterLevel <= -1) && (newWaterLevel > waterLevel))
  439.     {
  440.       waterLevel = newWaterLevel;
  441.     } else if ((waterLevel >= 0) && (waterLevel < sizeY - 1))
  442.     {
  443.       waterLevel -= dy;
  444.     }
  445.  
  446.     lastMove = System.currentTimeMillis();
  447.   }
  448.  
  449.   public void setSpeed(int speed)
  450.   {
  451.     if (speed < 1)
  452.       this.speed = speed;
  453.     else if (speed > type.maxSpeed)
  454.       speed = type.maxSpeed;
  455.     else
  456.       this.speed = speed;
  457.   }
  458.  
  459.   public int getSpeed() {
  460.     return speed;
  461.   }
  462.  
  463.   public static class CraftComplexBlock
  464.   {
  465.     int x;
  466.     int y;
  467.     int z;
  468.     ComplexBlock data;
  469.  
  470.     CraftComplexBlock(int x, int y, int z, ComplexBlock data)
  471.     {
  472.       this.x = x;
  473.       this.y = y;
  474.       this.z = z;
  475.       this.data = data;
  476.     }
  477.   }
  478.  
  479.   public static class DataBlock
  480.   {
  481.     int x;
  482.     int y;
  483.     int z;
  484.     int data;
  485.  
  486.     DataBlock(int x, int y, int z, int data)
  487.     {
  488.       this.x = x;
  489.       this.y = y;
  490.       this.z = z;
  491.       this.data = data;
  492.     }
  493.   }
  494. }
Advertisement
Add Comment
Please, Sign In to add comment