Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is This Possible?

Comments in 'Plugin Development' started by The_Doctor_123, Dec 15, 2013.

  1. The_Doctor_123
    Offline

    The_Doctor_123 New Member

    Joined:
    Dec 15, 2013
    Posts:
    5
    Minecraft User:
    BarakaInChains
    Hello. I am a Java developer and happen to know a little bit of Lua as well. I've taken an interest in this project since it's a lot like Bukkit, except that these plugins aren't made in Java. But I'd like to ask, is there a way to convert Lua code into PHP?
  2. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    if you know PHP language, you can translate it to another language :)
  3. nore
    Offline

    nore New Member

    Joined:
    Sep 15, 2013
    Posts:
    10
    Minecraft User:
    nore
    This is the closest I've found. Credit to Thomas Andersen.

    "
    Ever since i started playing this game, I've been interested in getting data out of it. I've never really was able to find a LUA to PHP data converter, all though i got pretty close on some website which I don't remember. My found there is the basis of the result i came up with here.

    This should only be used on LUA files containing data. Not for scripts.

    Be adviced: I use eval() to set the returned array. So if there is php code in the lua file you can risk it will be executed, all though my test show it will most likely just fail instead.

    LuaConverter.class.php

    1. <?php

    2. class LuaConverter{

    3. private $variables;
    4. private $debug;

    5. function __construct($dbg = false){
    6. $this->variables = null;
    7. $this->debug = $dbg;
    8. }


    9. public function convert($lua_text){
    10. if((explode("\n", $lua_text)) > 0){
    11. $result = array();
    12. $lua_text = preg_replace("/\ -- (.*?)\n/","\n",$lua_text);
    13. $lua_text = str_replace(",\n)", "\n)",
    14. str_replace("{", "array(",
    15. str_replace("}", ");",
    16. str_replace("},", "),",
    17. str_replace("[\"", "\"",
    18. str_replace("] = ", " => ",
    19. str_replace("\t", "",
    20. str_replace("{\n}", "false\n", $lua_text )
    21. )
    22. )
    23. )
    24. )
    25. )
    26. )
    27. );
    28. foreach(explode("\n", $lua_text) as $lua_line){
    29. if($this->debug){print($lua_line."\n");}
    30. $lua_line = chop($lua_line);
    31. if(eregi("^[a-z]", $lua_line)){
    32. if(is_numeric($lua_line[strlen($lua_line)-1]) || $lua_line[strlen($lua_line)-1] == "\""){
    33. $lua_line.=";";
    34. }
    35. if($this->debug){print("variable start: $lua_line\n");}

    36. $result[] = $lua_line;
    37. $this->variables[] = substr($lua_line, 0, strpos($lua_line, " "));
    38. }else{
    39. $result[] = $lua_line;
    40. }
    41. }
    42. }
    43. eval('$return = $'.implode("", $result));
    44. return $return;
    45. }

    46. public function getVariables(){
    47. return $this->variables;
    48. }

    49. public function variableCount(){
    50. return(count($this->variables));
    51. }
    52. }
    53. ?>
    An example of use:

    1. <?php
    2. include('LuaConverter.class.php'); // the class file

    3. //If you are reading and uploaded epgp log file.
    4. $myFile = $_FILES['upload_file']['tmp_name']; //The uploaded file
    5. $fh = fopen($myFile, 'r');
    6. $theData = fread($fh, filesize($myFile));
    7. fclose($fh);

    8. // The actual conversion
    9. $lua = new LuaConverter(); // Creating a new object
    10. $epgp_data = $lua->convert($theData); // converting the data.
    11. ?>
    The $epgp_data is an array matching the LUA array. Just iterate overit and get the data you need."
  4. The_Doctor_123
    Offline

    The_Doctor_123 New Member

    Joined:
    Dec 15, 2013
    Posts:
    5
    Minecraft User:
    BarakaInChains
    Sadly, I do not as I am focused on Java at this moment.

    So, I'm not exactly clear, do I run the Strings or something?
  5. Glitchmaster_PE
    Offline

    Glitchmaster_PE Notable Member Plugin Developer

    Joined:
    Aug 31, 2013
    Posts:
    834
    Plugins:
    8
    Minecraft User:
    Glitchmaster_PE
    Hmm, I am not quite familiar with Lua, but if you know Java, you can easily learn PHP. Try http://codecademy.com
  6. ZacHack
    Offline

    ZacHack Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    325
    Plugins:
    6
    Minecraft User:
    _ZacHack_
    You also need to learn the PocketMine API
  7. The_Doctor_123
    Offline

    The_Doctor_123 New Member

    Joined:
    Dec 15, 2013
    Posts:
    5
    Minecraft User:
    BarakaInChains
    Ehh.. I'm not exactly sure if it's really worth learning another language just to make plugins for PocketMine. Although, you're probably right about that since I understand OOP concepts.

    Yes, I realize that.
  8. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    PHP won't be that hard if you know Java plus it is useful for many other things not just PocketMine.
  9. The_Doctor_123
    Offline

    The_Doctor_123 New Member

    Joined:
    Dec 15, 2013
    Posts:
    5
    Minecraft User:
    BarakaInChains
    I currently have no other use for it.
  10. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    754
    Plugins:
    2
    Minecraft User:
    Darunia18
    He is right. PHP is really easy to learn. I started learning Java (but I never finished learning) and then I started learning PHP on codecademy.com to make plugins and if you put like a week of work into it, you'll be able to understand how things work.
  11. The_Doctor_123
    Offline

    The_Doctor_123 New Member

    Joined:
    Dec 15, 2013
    Posts:
    5
    Minecraft User:
    BarakaInChains
    Time is tight with high school and all, so it's very unlikely now. I may consider going on another learning streak in the summer, though.
  12. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Well maybe sometime :)
  13. Farmglitch
    Offline

    Farmglitch New Member

    Joined:
    Nov 15, 2013
    Posts:
    43
    Minecraft User:
    farmglitch
    It is very useful if you ever plan on getting into website design.
  14. wies
    Offline

    wies Notable Member

    Joined:
    Aug 23, 2013
    Posts:
    390
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Basically the hard point of 100% auto Java=>PHP translator/converter is the java classes.
    See this example:
    PHP:
    function convertScript($classFile){
    $varChars=array('a','b','c'/*blah blah blah, a-z, A-Z, '_', '$', 0-9*/);
    $negChars=array(' ','tab'/*tab character. Can't type it in browser*/,'\n');
    $content=file_get_content($classFile);
    #remove comments
    for($i=0;$i<strlen($content);$i++){#shall make an independent function for it. It just gets repeated by "\'" and "\"" and "//" and "/**/"
      
    if(/*substr($content, $i, 1)===" " and */in_array(substr($content$i2), $varChars)){
        
    $memory=0;
        for(
    $j=$i;$j<strlen($content)-2;$j++){
          if(!(
    in_array(substr($content$j1),$varChars)))break;
        }
        if(
    $memory===0)return "eval\'d code error at character $i";
        if(
    substr($content$j1)!=="("){#if it is not calling a function
          
    $content=substr($content0$i)."$".substr($content$i);
          continue;
        }
        
    $content=substr($content0$i)."$"."this->".substr($content$i));#IDK how to avoid "$this" be reconginzed as $this
      
    }
    }
    I had paid a lot of time to make a javascript reader coded in PHP (PocketMine), but finally I gave up because it is no longer as fun as originally it had been, and there was so much trouble.

    So you see, when I was making it, every night on my bed I found a new exception in the script (e.g. in this one if the originally script was not trying to resolve a variable but using a keyword like "for", "try", etc.)
  16. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,397
    Plugins:
    14
    PHP is 3½× easier to learn than Java.

Share This Page

Advertisement