--- deliantra/server/ext/commands.ext 2007/03/02 14:24:53 1.30 +++ deliantra/server/ext/commands.ext 2007/03/02 14:44:52 1.31 @@ -19,7 +19,7 @@ $item = $ob->find_best_object_match ($from) or return $ob->message ("rename: could not find a matching item to rename."); } else { - $item = $ob->find_marked_object () + $item = $ob->find_marked_object or return $ob->message ("rename: no from name and no marked item found to rename."); } @@ -193,78 +193,41 @@ 1 }; -cf::register_command applymode => sub { - my ($ob, $arg) = @_; - my @types = ("nochoice", "never", "always"); - my $mapping = { - nochoice => 1, - never => 2, - always => 3, - }; +sub _set_mode($$$@) { + my ($name, $ob, $arg, $slot, @choices) = @_; - my $oldmode = $ob->contr->unapply; - my $oldmode_name = $types[$oldmode]; + my $oldmode = $ob->contr->$slot; - return $ob->reply (undef, "applymode is set to $oldmode_name") + return $ob->reply (undef, "$name is set to $choices[$oldmode]") unless $arg; - return $ob->reply (undef, "applymode: Unknown options '$arg', valid options are @types") - unless $mapping->{$arg}; + my ($idx) = grep $choices[$_] eq $arg, 0 .. $#choices + or return $ob->reply (undef, "$name: Unknown options '$arg', valid options are @choices"), 1; - $ob->contr->unapply ($mapping->{$arg} - 1); # HACK: because of the $mapping->{$arg} check before, where $arg should not be 0 - # but $arg would be 0 if a user enters an incorrect value - $ob->reply (undef, "applymode" . ($oldmode == $ob->contr->unapply ? "" : " now") . " set to " . $types[$ob->contr->unapply]); + $ob->contr->$slot ($idx); + $ob->reply (undef, "$name" . ($oldmode == $ob->contr->unapply ? "" : " now") . " set to " . $choices[$ob->contr->$slot]); +} + +cf::register_command applymode => sub { + my ($ob, $arg) = @_; + + _set_mode "applymode", $ob, $arg, unapply => qw(nochoice never always); 1 }; cf::register_command petmode => sub { my ($ob, $arg) = @_; - my @types = ("normal", "sad", "defend", "arena"); - my $mapping = { - normal => 1, - sad => 2, - defend => 3, - arena => 4, - }; - my $oldtype = $ob->contr->petmode; - my $oldtype_name = $types[$oldtype]; - - return $ob->reply (undef, "petmode is set to $oldtype_name") - unless $arg; - - return $ob->reply (undef, "petmode: Unknown options '$arg', valid options are @types") - unless $mapping->{$arg}; - - $ob->contr->petmode ($mapping->{$arg} - 1); # HACK: because of the $mapping->{$arg} check before, where $arg should not be 0 - # but $arg would be 0 if a user enters an incorrect value - $ob->reply (undef, "petmode" . ($oldtype == $ob->contr->petmode ? "" : " now") . " set to " . $types[$ob->contr->petmode]); + _set_mode "petmode", $ob, $arg, petmode => qw(normal sad defend arena); 1 }; cf::register_command usekeys => sub { my ($ob, $arg) = @_; - my @types = ("inventory", "keyrings", "containers"); - my $mapping = { - inventory => 1, - keyrings => 2, - containers => 3, - }; - - my $oldtype = $ob->contr->usekeys; - my $oldtype_name = $types[$oldtype]; - - return $ob->reply (undef, "usekeys is set to $oldtype_name") - unless $arg; - - return $ob->reply (undef, "usekeys: Unknown options '$arg', valid options are @types") - unless $mapping->{$arg}; - $ob->contr->usekeys ($mapping->{$arg} - 1); # HACK: because of the $mapping->{$arg} check before, where $arg should not be 0 - # but $arg would be 0 if a user enters an incorrect value - $ob->reply (undef, "usekeys" . ($oldtype == $ob->contr->usekeys ? "" : " now") . " set to " . $types[$ob->contr->usekeys]); + _set_mode "usekeys", $ob, $arg, usekeys => qw(inventory keyrings containers); 1 };