Advertisement
OldFarmerJoe

Minecraft Item Id Reporter v7

Oct 31st, 2013
2,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 10.38 KB | None | 0 0
  1. #!C:Perl\bin\perl.exe
  2.  
  3. # Version: 7 - Oct 30th, 2013
  4. # Version: 6 - July 12th, 2013
  5. # Version: 5 - Jan 19th, 2013
  6. #
  7. # Generate a Minecraft block and item id report.
  8. # Recursively searches the current directory for minecraft config files.
  9. # Shows total for each config file and errors if an id is already used.
  10. # Then creates a table of all ids.
  11. #
  12. # Requires PERL to be installed. Top line of this script must be path to your Perl executable.
  13. # Runs outside of Minecraft.
  14. # Change the $startDir variable to .minecraft directory in your %APPDATA% folder.
  15. # Doesn't alter any config files, just creates the text file "minecraft_id.rpt"
  16.  
  17. use strict;
  18. use warnings;
  19.  
  20. use File::Path;
  21. use FileHandle;
  22.  
  23. # the directory separators are / not \, its a annoying difference between Unix and Windows
  24. # Perl uses the Unix way, \ is used to escape special characters in Perl
  25. my $startDir = "C:/Users/Bruce/AppData/Roaming/.minecraft";                      # more typical start directory
  26. #my $startDir = "C:/Users/Bruce/Minecraft/Minecraft_1.6.2_ver_1/data/.minecraft";  # start directory
  27. #my $startDir = "C:/Users/Bruce/Downloads/Fuzzzies Config Pack v3.5.12";           # lastest configs
  28.  
  29. my $repFile = "minecraft_id.rpt";  # report file
  30. my (%data, %biome);
  31. my $debug = 0;
  32. my $REP = OpenOutRef($repFile);
  33.  
  34. if ( -d $startDir ) {
  35.   print $REP "startDir = $startDir\n\n";
  36.   RecurseDir($startDir);
  37. }
  38. else {
  39.   print $REP "Error unable to find the startDir\n";
  40.   exit 1;
  41. }
  42.  
  43. print $REP "\n----------------------------------------------------------------\n";
  44. foreach my $id ( sort {$a <=> $b} keys %biome ) {
  45.   printf $REP ("%-5d  %s\n", $id, $biome{$id});
  46. }
  47. print $REP "----------------------------------------------------------------\n";
  48. foreach my $id ( sort {$a <=> $b} keys %data ) {
  49.   printf $REP ("%-5d  %s\n", $id, $data{$id});
  50. }
  51. close($REP);
  52.  
  53. exit 0;
  54.  
  55. #########################################
  56. # ID lines don't seem to have a set syntax,
  57. # so have to add a parser for each type.
  58. #########################################
  59. sub ReadCfgFile {
  60.   my ($cfgFile) = @_;
  61.   my $file = $cfgFile;
  62.   $file =~ s/$startDir\///g;
  63.   print $REP "Reading $file\n";
  64.   my $CFG = OpenInRef($cfgFile);
  65.   my $type = "";
  66.   my $count = 0;
  67.   my $error = 0;
  68.   my $indent = 0;
  69.   my $str = "";
  70.   my ($item,$id,@section);
  71.   my $prevLine = "";
  72.   my $sec = "";
  73.   my $secMatch = '(block|blocks|entity|item|items|id|ids|terrain block ids|biome|biome ids|multipart item ids|liquid ids|terrain block ids)';
  74.  
  75.   while ( my $line = <$CFG> ) {
  76.     chomp($line);
  77.     $line =~ s/^\s*#.+//;
  78.     next if ( $line =~ /^\s*$/ );
  79.     ($item,$id) = ("",0);
  80.  
  81.     ### section start on previous line
  82.     if ( $line =~ /^\s*\{\s*$/ ) {
  83.       $sec = $prevLine;
  84.       $sec =~ s/"//g;
  85.       $section[$indent] = $sec;
  86.       print $REP "  sec2 add indent=$indent sec=$sec\n" if ($debug);
  87.       if ( $sec =~ /^\b$secMatch\b/i ) {
  88.         $type = $sec;
  89.         print $REP "  indent=$indent type=$type\n" if ($debug);
  90.       }
  91.       $indent++;
  92.     }
  93.     # section start with {
  94.     elsif ( $line =~ /^\s*(.+)\s*\{\s*$/ ) {
  95.       $sec = $1;
  96.       chop($sec);
  97.       $sec =~ s/"//g;
  98.       $sec =~ s/biome ids/biome/;
  99.       #$sec =~ s/liquid ids/item/;
  100.       #$sec =~ s/items/item/;
  101.       #$sec =~ s/blocks/blocks/;
  102.       #$sec =~ s/multipart item ids/item/;
  103.       $section[$indent] = $sec;
  104.       print $REP "  sec1 add indent=$indent sec=$sec\n" if ($debug);
  105.       if ( $sec =~ /^\b$secMatch\b/i ) {
  106.         $type = $section[$indent];
  107.         print $REP "  indent=$indent type=$type\n" if ($debug);
  108.       }
  109.       $indent++;
  110.     }
  111.     # section end
  112.     elsif ( $line =~ /^\s*\}\s*$/ ) {
  113.       $indent-- if ($indent);
  114.       print $REP "  sec3 sub indent=$indent\n" if ($debug);
  115.       if ( $section[$indent] =~ /^\b$secMatch\b/i ) {
  116.         $type = "";
  117.       }
  118.     }
  119.  
  120.     ### Type blocks in { }
  121.     elsif ( $type ) {
  122.       if ( $section[0] =~ /(tweaks)/ ) {
  123.          print $REP " t0 skip this section\n" if ($debug);
  124.          # dont parse
  125.       }
  126.       #print $REP "    inside type block type=$type\n";
  127.       # I:block=val
  128.       elsif ( $line =~ /I:(block|item)=(\d+)/i ) {
  129.         ($type,$id) = ($1,$2);
  130.         $item = $section[0];
  131.         print $REP "  t1 type=$type name=$item id=$id\n" if ($debug);
  132.       }
  133.       # I:type.item.id=val
  134.       elsif ( $line =~ /I:$type.(\w+)\.id\s*=\s*(\d+)/i ) {
  135.         ($item,$id) = ($1,$2);
  136.         print $REP "  t2 type=$type name=$item id=$id\n" if ($debug);
  137.       }
  138.       # I:item=val.id=val
  139.       elsif ( $line =~ /I:(\w+)\.id\s*=\s*(\d+)/i ) {
  140.         ($item,$id) = ($1,$2);
  141.         print $REP "  t3 type=$type name=$item id=$id\n" if ($debug);
  142.       }
  143.       # I:item=valID=val
  144.       elsif ( $line =~ /I:(\w+)(Block|Item)Id\s*=\s*(\d+)/i ) {
  145.         ($item,$id) = ($1,$3);
  146.         print $REP "  t4 type=$type name=$item id=$id\n" if ($debug);
  147.       }
  148.       # I:item=valID=val
  149.       elsif ( $line =~ /I:(\w+)Id\s*=\s*(\d+)/i ) {
  150.         ($item,$id) = ($1,$2);
  151.         print $REP "  t5 type=$type name=$item id=$id\n" if ($debug);
  152.       }
  153.       # I:name=val
  154.       elsif ( $line =~ /I:(\w+)\s*=\s*(\d+)/i ) {
  155.         ($item,$id) = ($1,$2);
  156.         print $REP "  t6 type=$type name=$item id=$id\n" if ($debug);
  157.       }
  158.       # S:item_ItemID=val
  159.       elsif ( $line =~ /S:(\w+)_ItemID\s*=\s*(\d+)/i ) {
  160.         ($item,$id) = ($1,$2);
  161.         print $REP "  t7 type=$type name=$item id=$id\n" if ($debug);
  162.       }
  163.       # I:stuff_(Block|Item)item=val
  164.       elsif ( $line =~ /I:.+_(Block|Item)(\w+)\s*=\s*(\d+)/i ) {
  165.         ($item,$id) = ($2,$3);
  166.         print $REP "  t8 type=$type name=$item id=$id\n" if ($debug);
  167.       }
  168.       # item=val
  169.       elsif ( $line =~ /(\w+)\s*=\s*(\d+)/i ) {
  170.         ($item,$id) = ($1,$2);
  171.         print $REP "  t9 type=$type name=$item id=$id\n" if ($debug);
  172.       }
  173.       # I:"Ash Block ID"=163
  174.       elsif ( $line =~ /I:\"(.+)\"\s*=\s*(\d+)/i ) {
  175.         ($item,$id) = ($1,$2);
  176.         print $REP "  t10 type=$type name=$item id=$id\n" if ($debug);
  177.       }
  178.  
  179.       $str = "$type.$item";
  180.     }
  181.     ### Item outside type blocks
  182.     else {
  183.       $type = "";
  184.       # item.id=val
  185.       if ( $line =~ /^\s*(\w+)\.id\s*=\s*(\d+)$/i ) {
  186.         ($item,$id) = ($1,$2);
  187.         $str = $item;
  188.         print $REP "  o1 type=$type name=$item id=$id\n" if ($debug);
  189.       }
  190.       # item.id=val
  191.       elsif ( $line =~ /^\s*(\w+)_ID\s*=\s*(\d+)$/i ) {
  192.         ($item,$id) = ($1,$2);
  193.         $str = $item;
  194.         print $REP "  o2 type=$type name=$item id=$id\n" if ($debug);
  195.       }
  196.       # itemid=val
  197.       elsif ( $line =~ /^\s*(\w+)id\s*=\s*(\d+)\s*$/i ) {
  198.         ($item,$id) = ($1,$2);
  199.         $str = $item;
  200.         print $REP "  o3 type=$type name=$item id=$id\n" if ($debug);
  201.       }
  202.       # type.item.id=val
  203.       elsif ( $line =~ /^\s*(item|block|entity)\.(\w+)\.id\s*=\s*(\d+)\s*$/i ) {
  204.         ($type,$item,$id) = ($1,$2,$3);
  205.         $str = "$type.$item";
  206.         $type = "";
  207.         print $REP "  o4 type=$type name=$item id=$id\n" if ($debug);
  208.       }
  209.       # I:itemId=val
  210.       elsif ( $line =~ /^\s*I:(\w+)Id=(\d+)/ ) {
  211.         ($item,$id) = ($1,$2);
  212.         my $str2 = "";
  213.         if ( $indent ) {
  214.           foreach ( my $i=0; $i < $indent; $i++ ) {
  215.             $str2 .= $section[$i].".";
  216.           }
  217.           chop($str2);
  218.           $str = $str2.".".$item;
  219.         }
  220.         else {
  221.           $str = "";
  222.         }
  223.         print $REP "  o5 type=$type name=$item id=$id\n" if ($debug);
  224.       }
  225.       elsif ( $indent == 2 && $line =~ /^\s*I:(block|item)=(\d+)$/ && $section[1] =~ /id/i ) {
  226.         $line =~ /^\s*I:(block|item)=(\d+)$/;
  227.         ($type,$id) = ($1,$2);
  228.         $str = "$type.$section[0]";
  229.         #print $REP "  type=$type id=$id sec0=$section[0] sec1=$section[1]\n";
  230.         print $REP "  o6 type=$type name=$item id=$id\n" if ($debug);
  231.       }
  232.       # special items
  233.       elsif ( $line =~ /(propolisPipe)=(\d+)/ ) {
  234.         ($item,$id) = ($1,$2);
  235.     $type = "item";
  236.         $str = "$type.$item";
  237.         print $REP "  o7 type=$type name=$item id=$id\n" if ($debug);
  238.       }
  239.       else {
  240.         #print $REP "    parse failed line=$line\n";
  241.       }
  242.     }
  243.  
  244.     ###############################################
  245.     if ( $id && $type eq "biome" ) {
  246.       if ( !defined($biome{$id}) ) {
  247.         $biome{$id} = sprintf("%-35s   %s", $file, $str);
  248.         $count++;
  249.       }
  250.       else {
  251.         print $REP "  ERROR: biome id $id is already used for $biome{$id}, change: $str\n";
  252.         $error++;
  253.       }
  254.     }
  255.     elsif ( $id && !defined($data{$id}) ) {
  256.       $data{$id} = sprintf("%-35s   %s", $file, $str);
  257.       $count++;
  258.       }
  259.     elsif ( $id ) {
  260.       print $REP "  ERROR: id $id is already used for $data{$id}, change: $str\n";
  261.       $error++;
  262.     }
  263.  
  264.     if ( $line =~ /^\s*(\w+)\s*$/ ) {
  265.       $prevLine = $1;
  266.       print $REP "  prevLine=$prevLine\n" if ($debug);
  267.     }
  268.   }
  269.  
  270.   close($CFG);
  271.   print $REP "  count = $count\n";
  272.   if ( $error ) {
  273.     print $REP "  errors = $error\n";
  274.   }
  275. }
  276.  
  277. #########################################
  278. sub RecurseDir {
  279.   my($path) = @_;
  280.   my(@file) = ReadDir($path);
  281.   foreach my $file ( sort @file ) {
  282.     if ( ($file ne ".") && ($file ne "..") ) {
  283.       next if ( $path =~ /\bsaves\b/ );  # dont look in the saves folder
  284.       my $cur_path = $path."/".$file;
  285.       if ( -l $cur_path ) {
  286.         # dont follow links
  287.       }
  288.       elsif ( -d $cur_path && -X $cur_path ) {
  289.         RecurseDir($cur_path);
  290.       }
  291.       else {
  292.         # find config files
  293.         if ( $file =~ /(cfg|conf|properties)/ && $file !~ /^\./ && $file !~ /\~$/ ) {
  294.           ReadCfgFile($cur_path);
  295.         }
  296.       }
  297.     }
  298.   }
  299. }
  300.  
  301. #########################################
  302. sub ReadDir {
  303.   my($dir) = @_;
  304.   if ( opendir(DIR, $dir) ) {
  305.     my @file = readdir(DIR);
  306.       chomp(@file);
  307.       closedir(DIR);
  308.       return @file;
  309.   }
  310.   else {
  311.     print $REP "Error: Unable to open directory $dir\n $!";
  312.     exit 1;
  313.   }
  314. }
  315.  
  316. #########################################
  317. sub OpenOutRef {
  318.   my ($outFile) = (@_);
  319.   my $OUTFILE = FileHandle->new;
  320.   if ( $OUTFILE->open("> $outFile") == 0 ) {
  321.     print "Error unable to open out file: $outFile\n";
  322.     exit 1;
  323.   }
  324.   return $OUTFILE;
  325. }
  326.  
  327. sub OpenInRef {
  328.   my ($inFile) = (@_);
  329.   my $INFILE = FileHandle->new;
  330.   if ( $INFILE->open("< $inFile") == 0 ) {
  331.     print $REP "Error unable to open in file: $inFile\n";
  332.     exit 1;
  333.   }
  334.   return $INFILE;
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement