Energyxxer

Relative Blocks CPM Beta Plugin

Apr 17th, 2016
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pluginManager.register("Relative Blocks Beta",null,null,function(lib,storage){
  2.     for(var i = 0; i < lib.beforeCommands.length(); i++) {
  3.         var command = lib.beforeCommands.get(i);
  4.         while(command.contains("$pos")) {
  5.             if(storage == undefined) {
  6.                 storage = {};
  7.             }
  8.             var declarationStart = command.indexOf("$pos");
  9.             var declarationEnd = declarationStart + command.substring(declarationStart).indexOf(";")+1;
  10.             var replacementValue = "";
  11.    
  12.             if(declarationEnd <= declarationStart) {
  13.                 lib.throw("Error: [RELATIVE] Invalid position declaration in before command. Expected semicolon.","rgba(255,63,63,.75)");
  14.                 lib.break();
  15.                 return null;
  16.             }
  17.             var declaration = command.substring(declarationStart+4,declarationEnd-1).trim();
  18.             var args = declaration.split(" "); 
  19.    
  20.             var name = args[0];
  21.    
  22.             if(["set","add","get"].contains(args[1])) {
  23.                 if(args[1] == "set") {
  24.                     _set(name);
  25.                 } else if(args[1] == "add") {
  26.                     if(!["x","y","z"].contains(args[2].toLowerCase())) {
  27.                         lib.throw("Error: [RELATIVE] Invalid addition operation in before command. Expected \"x\", \"y\" or \"z\", but instead got \"" + args[2] + "\".","rgba(255,63,63,.75)");
  28.                         lib.break();
  29.                         return null;
  30.                     }
  31.                     if(isNaN(parseFloat(args[3]))) {
  32.                         lib.throw("Error: [RELATIVE] Invalid addition operation in before command. Expected a number, but instead got \"" + args[3] + "\".","rgba(255,63,63,.75)");
  33.                         lib.break();
  34.                         return null;
  35.                     }
  36.                     if(_add(name,args[2].toLowerCase(),parseFloat(args[3])) == null) {
  37.                         return null;
  38.                     }
  39.                 } else if(args[1] == "get") {
  40.                     if(_get(name) == null) {
  41.                         return null;
  42.                     } else {
  43.                         replacementValue = _get(name);
  44.                     }
  45.                 }
  46.             } else {
  47.                 lib.throw("Error: [RELATIVE] Invalid position operation in before command. Expected \"set\", \"get\" or \"add\", but instead got \"" + args[1] + "\".","rgba(255,63,63,.75)");
  48.                 lib.break();
  49.                 return null;
  50.             }
  51.    
  52.             command = (command.substring(0,declarationStart) + replacementValue + command.substring(declarationEnd)).trim()
  53.             lib.beforeCommands.set(i,command);
  54.    
  55.             function _set(name) {
  56.                 storage[name] = {};
  57.                 storage[name].x = 0;
  58.                 storage[name].y = -2;
  59.                 storage[name].z = 0;
  60.                 replacementValue = "";
  61.             }
  62.    
  63.             function _add(name,axis,offset) {
  64.                 if(name in storage) {
  65.                     storage[name][axis] += offset;
  66.                     replacementValue = "";
  67.                     return true;
  68.                 } else {
  69.                     lib.throw("Error: [RELATIVE] Could not find position " + name + ". Position not declared.","rgba(255,63,63,.75)");
  70.                     lib.break();
  71.                     return null;
  72.                 }
  73.             }
  74.  
  75.             function _get(name) {
  76.                 if(name in storage) {
  77.                     return ("~" + (storage[name].x) + " ~" + (storage[name].y) + " ~" + (storage[name].z)).replaceAll("~0","~");
  78.                 } else {
  79.                     lib.throw("Error: [RELATIVE] Could not find position " + name + ". Position not declared.","rgba(255,63,63,.75)");
  80.                     lib.break();
  81.                     return null;
  82.                 }
  83.             }
  84.         }
  85.     }
  86.     for(var i = 0; i < lib.afterCommands.length(); i++) {
  87.         var command = lib.afterCommands.get(i);
  88.         while(command.contains("$pos")) {
  89.             if(storage == undefined) {
  90.                 storage = {};
  91.             }
  92.             var declarationStart = command.indexOf("$pos");
  93.             var declarationEnd = declarationStart + command.substring(declarationStart).indexOf(";")+1;
  94.             var replacementValue = "";
  95.    
  96.             if(declarationEnd <= declarationStart) {
  97.                 lib.throw("Error: [RELATIVE] Invalid position declaration in after command. Expected semicolon.","rgba(255,63,63,.75)");
  98.                 lib.break();
  99.                 return null;
  100.             }
  101.             var declaration = command.substring(declarationStart+4,declarationEnd-1).trim();
  102.             var args = declaration.split(" "); 
  103.    
  104.             var name = args[0];
  105.    
  106.             if(["set","add","get"].contains(args[1])) {
  107.                 if(args[1] == "set") {
  108.                     _set(name);
  109.                 } else if(args[1] == "add") {
  110.                     if(!["x","y","z"].contains(args[2].toLowerCase())) {
  111.                         lib.throw("Error: [RELATIVE] Invalid addition operation in after command. Expected \"x\", \"y\" or \"z\", but instead got \"" + args[2] + "\".","rgba(255,63,63,.75)");
  112.                         lib.break();
  113.                         return null;
  114.                     }
  115.                     if(isNaN(parseFloat(args[3]))) {
  116.                         lib.throw("Error: [RELATIVE] Invalid addition operation in after command. Expected a number, but instead got \"" + args[3] + "\".","rgba(255,63,63,.75)");
  117.                         lib.break();
  118.                         return null;
  119.                     }
  120.                     if(_add(name,args[2].toLowerCase(),parseFloat(args[3])) == null) {
  121.                         return null;
  122.                     }
  123.                 } else if(args[1] == "get") {
  124.                     if(_get(name) == null) {
  125.                         return null;
  126.                     } else {
  127.                         replacementValue = _get(name);
  128.                     }
  129.                 }
  130.             } else {
  131.                 lib.throw("Error: [RELATIVE] Invalid position operation in after command. Expected \"set\", \"get\" or \"add\", but instead got \"" + args[1] + "\".","rgba(255,63,63,.75)");
  132.                 lib.break();
  133.                 return null;
  134.             }
  135.    
  136.             command = (command.substring(0,declarationStart) + replacementValue + command.substring(declarationEnd)).trim()
  137.             lib.afterCommands.set(i,command);
  138.    
  139.             function _set(name) {
  140.                 storage[name] = {};
  141.                 storage[name].x = 0;
  142.                 storage[name].y = -2;
  143.                 storage[name].z = 0;
  144.                 replacementValue = "";
  145.             }
  146.    
  147.             function _add(name,axis,offset) {
  148.                 if(name in storage) {
  149.                     storage[name][axis] += offset;
  150.                     replacementValue = "";
  151.                     return true;
  152.                 } else {
  153.                     lib.throw("Error: [RELATIVE] Could not find position " + name + ". Position not declared.","rgba(255,63,63,.75)");
  154.                     lib.break();
  155.                     return null;
  156.                 }
  157.             }
  158.  
  159.             function _get(name) {
  160.                 if(name in storage) {
  161.                     return ("~" + (storage[name].x) + " ~" + (storage[name].y) + " ~" + (storage[name].z)).replaceAll("~0","~");
  162.                 } else {
  163.                     lib.throw("Error: [RELATIVE] Could not find position " + name + ". Position not declared.","rgba(255,63,63,.75)");
  164.                     lib.break();
  165.                     return null;
  166.                 }
  167.             }
  168.         }
  169.     }
  170. },function(lib, storage){
  171.     var command = lib.getBlock().command + " ";
  172.     while(command.contains("$pos")) {
  173.         if(storage == undefined) {
  174.             storage = {};
  175.         }
  176.         var declarationStart = command.indexOf("$pos");
  177.         var declarationEnd = declarationStart + command.substring(declarationStart).indexOf(";")+1;
  178.         var replacementValue = "";
  179.  
  180.         if(declarationEnd <= declarationStart) {
  181.             lib.throw("Error: [RELATIVE] Invalid position declaration at line " + lib.getLineNumber().toString() + ". Expected semicolon.","rgba(255,63,63,.75)");
  182.             lib.break();
  183.             return null;
  184.         }
  185.         var declaration = command.substring(declarationStart+4,declarationEnd-1).trim();
  186.         var args = declaration.split(" ");
  187.  
  188.         var name = args[0];
  189.  
  190.         if(["set","add","get"].contains(args[1])) {
  191.             if(args[1] == "set") {
  192.                 _set(name);
  193.             } else if(args[1] == "add") {
  194.                 if(!["x","y","z"].contains(args[2].toLowerCase())) {
  195.                     lib.throw("Error: [RELATIVE] Invalid addition operation at line " + lib.getLineNumber().toString() + ". Expected \"x\", \"y\" or \"z\", but instead got \"" + args[2] + "\".","rgba(255,63,63,.75)");
  196.                     lib.break();
  197.                     return null;
  198.                 }
  199.                 if(isNaN(parseFloat(args[3]))) {
  200.                     lib.throw("Error: [RELATIVE] Invalid addition operation at line " + lib.getLineNumber().toString() + ". Expected a number, but instead got \"" + args[3] + "\".","rgba(255,63,63,.75)");
  201.                     lib.break();
  202.                     return null;
  203.                 }
  204.                 if(_add(name,args[2].toLowerCase(),parseFloat(args[3])) == null) {
  205.                     return null;
  206.                 }
  207.             } else if(args[1] == "get") {
  208.                 if(_get(name) == null) {
  209.                     return null;
  210.                 } else {
  211.                     replacementValue = _get(name);
  212.                 }
  213.             }
  214.         } else {
  215.             lib.throw("Error: [RELATIVE] Invalid position operation at line " + lib.getLineNumber().toString() + ". Expected \"set\", \"get\" or \"add\", but instead got \"" + args[1] + "\".","rgba(255,63,63,.75)");
  216.             lib.break();
  217.             return null;
  218.         }
  219.  
  220.         command = (command.substring(0,declarationStart) + replacementValue + command.substring(declarationEnd)).trim()
  221.         lib.setCommand(command);
  222.  
  223.         function _set(name) {
  224.             storage[name] = {};
  225.             storage[name].x = lib.getBlock().pos.x;
  226.             storage[name].y = lib.getBlock().pos.y;
  227.             storage[name].z = lib.getBlock().pos.z;
  228.             replacementValue = "";
  229.         }
  230.  
  231.         function _add(name,axis,offset) {
  232.             if(name in storage) {
  233.                 storage[name][axis] += offset;
  234.                 replacementValue = "";
  235.                 return true;
  236.             } else {
  237.                 lib.throw("Error: [RELATIVE] Could not find position " + name + ". Line " + lib.getLineNumber().toString() + ". Position not declared.","rgba(255,63,63,.75)");
  238.                 lib.break();
  239.                 return null;
  240.             }
  241.         }
  242.  
  243.         function _get(name) {
  244.             if(name in storage) {
  245.                 return ("~" + (storage[name].x - lib.getBlock().pos.x) + " ~" + (storage[name].y - lib.getBlock().pos.y) + " ~" + (storage[name].z - lib.getBlock().pos.z)).replaceAll("~0","~");
  246.             } else {
  247.                 lib.throw("Error: [RELATIVE] Could not find position " + name + ". Line " + lib.getLineNumber().toString() + ". Position not declared.","rgba(255,63,63,.75)");
  248.                 lib.break();
  249.                 return null;
  250.             }
  251.         }
  252.     }
  253. });
Advertisement
Add Comment
Please, Sign In to add comment