View Single Post
  #6  
Old 12-17-2015, 04:51 AM
ghanja's Avatar
ghanja
Dragon
 
Join Date: Aug 2012
Location: Hershey, PA
Posts: 499
Default

Just to add, for on-lookers. The above example assumes you are running this version of /plugins/check_handin.pl

Code:
# plugin::check_handin($item1 => #required_amount,...);
# autoreturns extra unused items on success
sub check_handin {
    my $hashref = shift;
    my %required = @_;
    foreach my $req (keys %required) {
        if ((!defined $hashref->{$req}) || ($hashref->{$req} != $required{$req})) {
            return(0);
        }
    }
     foreach my $req (keys %required) {
         if ($required{$req} < $hashref->{$req}) {
             $hashref->{$req} -= $required{$req};
         } else {
             delete $hashref->{$req};
         }
     }
     return 1;
}

sub return_items {    
        my $hashref = plugin::var('$itemcount');
        my $client = plugin::val('$client');
        my $name = plugin::val('$name');
        my $items_returned = 0;

        my %ItemHash = (
                0 => [ plugin::val('$item1'), plugin::val('$item1_charges'), plugin::val('$item1_attuned') ],
                1 => [ plugin::val('$item2'), plugin::val('$item2_charges'), plugin::val('$item2_attuned') ],
                2 => [ plugin::val('$item3'), plugin::val('$item3_charges'), plugin::val('$item3_attuned') ],
                3 => [ plugin::val('$item4'), plugin::val('$item4_charges'), plugin::val('$item4_attuned') ],
        );
        
        foreach my $k (keys(%{$hashref}))
        {
                next if($k == 0);
                my $rcount = $hashref->{$k};
                my $r;
                for ($r = 0; $r < 4; $r++)
                {
                        if ($rcount > 0 && $ItemHash{$r}[0] && $ItemHash{$r}[0] == $k)
                        {
                                if ($client)
                                {
                                        $client->SummonItem($k, $ItemHash{$r}[1], $ItemHash{$r}[2]);
                                        quest::say("I have no need for this $name, you can have it back.");
                                        $items_returned = 1;
                                }
                                else
                                {
                                        # This shouldn't be needed, but just in case
                                        quest::summonitem($k, 0);
                                        $items_returned = 1;
                                }
                                $rcount--;
                        }
                }
                delete $hashref->{$k};
        }
        # Return true if items were returned
        return $items_returned;

}

sub mq_process_items {
        my $hashref = shift;
        my $npc = plugin::val('$npc');
        my $trade = undef;
        
        if($npc->EntityVariableExists("_mq_trade")) {
                $trade = decode_eqemu_item_hash($npc->GetEntityVariable("_mq_trade")); 
        } else {
                $trade = {};
        }
        
        foreach my $k (keys(%{$hashref})) {
                next if($k == 0);
                
                if(defined $trade->{$k}) {
                        $trade->{$k} = $trade->{$k} + $hashref->{$k};
                } else {
                        $trade->{$k} = $hashref->{$k};
                }
        }
        
        my $str = encode_eqemu_item_hash($trade);
        $npc->SetEntityVariable("_mq_trade", $str);
}

sub check_mq_handin {
        my %required = @_;
        my $npc = plugin::val('$npc');
        my $trade = undef;
        
        if($npc->EntityVariableExists("_mq_trade")) {
                $trade = decode_eqemu_item_hash($npc->GetEntityVariable("_mq_trade"));
        } else {
                return 0;
        }
        
        foreach my $req (keys %required) {
                if((!defined $trade->{$req}) || ($trade->{$req} < $required{$req})) {
                                return 0;
                }
        }
        
        foreach my $req (keys %required) {
                if ($required{$req} < $trade->{$req}) {
                        $trade->{$req} -= $required{$req};
                } else {
                        delete $trade->{$req};
                }
        }
        
        $npc->SetEntityVariable("_mq_trade", encode_eqemu_item_hash($trade));
        return 1;
}

sub clear_mq_handin {
        my $npc = plugin::val('$npc');
        $npc->SetEntityVariable("_mq_trade", "");
}

sub encode_eqemu_item_hash {
        my $hashref = shift;
        my $str = "";
        my $i = 0;
        
        foreach my $k (keys(%{$hashref})) {
                if($i != 0) {
                        $str .= ",";
                } else {
                        $i = 1;
                }
                
                $str .= $k;
                $str .= "=";
                $str .= $hashref->{$k};
        }
        
        return $str;
}

sub decode_eqemu_item_hash {
        my $str = shift;
        my $hashref = { };
        
        my @vals = split(/,/, $str);
        my $val_len = @vals;
        for(my $i = 0; $i < $val_len; $i++) {
                my @subval = split(/=/, $vals[$i]);
                my $subval_len = @subval;
                if($subval_len == 2) {
                        my $key = $subval[0];
                        my $value = $subval[1];
                        
                        $hashref->{$key} = $value;
                }
        }
        
        return $hashref;
}
Reply With Quote