Guest User

xConf

a guest
Jul 10th, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.70 KB | None | 0 0
  1. package xConf;
  2.  
  3. use Plugins;
  4. use Globals;
  5. use Log qw(message error debug);
  6.  
  7. ## startup
  8. Plugins::register('xConf', 'commands for change items_control, mon_control, pickupitems, priority', \&Unload, \&Reload);
  9.  
  10. ## Register command 'xConf'
  11. my $chooks = Commands::register(
  12.         ['iconf', 'edit items_control.txt', \&xConf],
  13.         ['mconf', 'edit mon_control.txt', \&xConf],
  14.         ['pconf', 'edit pickupitems.txt', \&xConf],
  15.         ['sconf', 'edit shop.txt', \&xConf],
  16.         ['priconf', 'edit priority.txt', \&priconf],
  17. );
  18.  
  19. sub Reload {
  20.         Commands::unregister($chooks);
  21.         message "xConf plugin reloading\n", 'success'
  22. }
  23. sub Unload {
  24.         Commands::unregister($chooks);
  25.         message "xConf plugin unloading\n", 'success'
  26. }
  27.  
  28. sub xConf {
  29.         my ($cmd, $args) = @_;
  30.         my ($key, $value) = $args =~ /([\s\S]+?)(?: |   )([\-\d\.]+[\s\S]*)/;
  31.         $key = $args if !$key;
  32. debug "KEY: $key, VALUE: $value\n";
  33.         if (!$key) {
  34.                 error "Syntax Error in function '$cmd'. Not found <key>\nUsage: $cmd <key> [<value>]\n";
  35.                 return
  36.         }
  37.         my ($file,$found,$name, %inf_hash, %ctrl_hash) = undef;
  38.  
  39.         if ($cmd eq 'mconf') {
  40.                 %inf_hash  = %monsters_lut;
  41.                 %ctrl_hash = %mon_control;
  42.                 $file = 'mon_control.txt'
  43.         } elsif ($cmd eq 'pconf') {
  44.                 %inf_hash  = %items_lut;
  45.                 %ctrl_hash = %pickupitems;
  46.                 $file = 'pickupitems.txt'
  47.         } elsif ($cmd eq 'iconf') {
  48.                 %inf_hash  = %items_lut;
  49.                 %ctrl_hash = %items_control;
  50.                 $file = 'items_control.txt'
  51.         } elsif ($cmd eq 'sconf') {
  52.                 %inf_hash = %items_lut;
  53.                 %ctrl_hash = %shop;
  54.                 $file = 'shop.txt'
  55.         }
  56.  
  57.         ## Check $key in tables\monsters.txt & items.txt
  58.  
  59.         if ($key ne "all") {
  60.                 if ($inf_hash{$key}) {
  61. debug "'$inf_hash{$key}' ID: $key is found in 'tables\\monsters.txt'.\n";
  62.                 $found = 1;
  63.                 $key = $inf_hash{$key}
  64.                 } else {
  65.                         foreach $name (values %inf_hash) {
  66.                                 if ($found = (lc($key) eq lc($name))) {
  67.                                         $key = $name;
  68. debug "'$name' is found in 'tables\\monsters.txt'.\n";
  69.                                         last
  70.                                 }
  71.                         }
  72.                 }
  73.                 if (!$found) {error "WARNING: '$key' is not found in 'tables\\monsters.txt' and in 'tables\\items.txt'!\n"}
  74.         }
  75.        
  76.         if ($value eq '') {
  77.                 if ($cmd eq 'sconf') {
  78.                         my $i = 0;
  79.                         $found = 0;
  80.                         until ($ctrl_hash{items}[$i]{name} eq "") {
  81.                                 if ($ctrl_hash{items}[$i]{name} eq $args) {
  82.                                         $found = 1;
  83.                                         message "$file: $ctrl_hash{items}[$i]{name} $ctrl_hash{items}[$i]{price} $ctrl_hash{items}[$i]{amount}\n", 'list'
  84.                                 }
  85.                         $i++;
  86.                         }
  87.                         if (!$found) {
  88.                                 error "The key '$key' is not found in '$file'\n";
  89.                         }
  90.                 } else {
  91.                         if ($ctrl_hash{lc($key)}) {
  92.                                 $key = lc($key);
  93.                                 if ($cmd eq 'mconf') {
  94.                                 message "$file: $key $ctrl_hash{$key}{attack_auto} $ctrl_hash{$key}{teleport_auto} $ctrl_hash{$key}{teleport_search} $ctrl_hash{$key}{skillcancel_auto} $ctrl_hash{$key}{attack_lvl} $ctrl_hash{$key}{attack_jlvl} $ctrl_hash{$key}{attack_hp} $ctrl_hash{$key}{attack_sp} $ctrl_hash{$key}{weight}\n", 'list'
  95.                                 } elsif ($cmd eq 'pconf') {
  96.                                         message "$file: $key $pickupitems{$key}\n", 'list'
  97.                                 } elsif ($cmd eq 'iconf') {
  98.                                         message "$file: $key $items_control{$key}{keep} $items_control{$key}{storage} $items_control{$key}{sell} $items_control{$key}{cart_add} $items_control{$key}{cart_get}\n", 'list'
  99.                                 } elsif ($cmd eq 'priconf') {
  100.                                         message "$file: $key $priority{$key}\n", 'list'
  101.                                 }
  102.                         } else {
  103.                                 error "The key '$key' is not found in '$file'\n"
  104.                         }
  105.                 }
  106.                 return
  107.         }
  108.         filewrite($file, $key, $value)
  109. }
  110.  
  111. ## write FILE
  112. sub filewrite {
  113.         my ($file, $key, $value) = @_;
  114.         my $controlfile = Settings::getControlFilename($file);
  115. debug "sub WRITE = FILE: $file, KEY: $key, VALUE: $value\n";
  116.         open(FILE, "<:utf8", $controlfile);
  117.         my @lines = <FILE>;
  118.         close(FILE);
  119.         chomp @lines;
  120.  
  121.         my $used = 0;
  122.         my $nochanges = 1;
  123.         foreach my $line (@lines) {
  124.                 my ($what) = $line =~ /([\s\S]+?)(?: |   )(?:[\-\d\.]+[\s\S]*)/;
  125.                 my $tmp = "";
  126.                 if (lc($what) eq lc($key)) {
  127.                         if ($file eq 'shop.txt') {
  128.                                 $tmp = join ('  ', $key, $value);
  129.                         } else {
  130.                                 $tmp = join (' ', $key, $value);
  131.                         }
  132.                         if ($line ne $tmp) {
  133.                                 $nochanges = 0;
  134.                                 $line = $tmp;
  135.                         }
  136.                         $used = 1;
  137.                 }
  138.         }
  139.         if ($used == 0) {
  140.                 $nochanges = 0;
  141.                 if ($file eq 'shop.txt') {
  142.                         push (@lines, $key.'    '.$value)
  143.                 } else {
  144.                         push (@lines, $key.' '.$value)
  145.                 }
  146.         }
  147.         message "$file: $key $value\n", 'system';
  148.         if ($nochanges == 0) {
  149.                 open(WRITE, ">:utf8", $controlfile);
  150.                 print WRITE join ("\n", @lines);
  151.                 close(WRITE);
  152.                 Commands::run("reload $file")
  153.         }
  154. }
  155.  
  156.  
  157. sub priconf {
  158.         my ($cmd, $args) = @_;
  159.         my @mobs = split /\s*,\s*/, $args;
  160.         if (@mobs == 0) {
  161.                 error "Syntax Error in function 'priconf'.\nUsage: priconf monster1, monster2, ...\n";
  162.                 return
  163.         }
  164.         my $controlfile = Settings::getControlFilename('priority.txt');
  165.         open(my $file,"<:utf8",$controlfile);
  166.         my @lines = ();
  167.         my $begin = 1;
  168.         while (my $line = <$file>) {
  169.                 my $tmp = $line;
  170.                 $tmp =~ s/\x{FEFF}//g;
  171.                 chomp($tmp);
  172.                
  173.                 if (($tmp =~ /^#/) or ($tmp =~ /^\s*$/)) {
  174.                         push @lines, $line;
  175.                         next;
  176.                 }
  177.                
  178.                 if ($begin == 1) {
  179.                         foreach (@mobs) { push @lines, $_."\n" }
  180.                         $begin = 0;            
  181.                 }
  182.                
  183.                 my $ok = 1;
  184.                 foreach (@mobs) {
  185.                         if (lc($_) eq lc($tmp)) {
  186.                                 $ok = 0;
  187.                                 last;
  188.                         }
  189.                 }
  190.                 if ($ok == 1) {
  191.                         push @lines, $line;
  192.                 }
  193.         }
  194.         close($file);
  195.         open($file,">:utf8",$controlfile);
  196.         print $file @lines;
  197.         close($file);
  198.         Commands::run("reload priority.txt");
  199. }
  200. return 1;
Advertisement
Add Comment
Please, Sign In to add comment