Mys 1.3.6 Lootbox Mod Using Explode

Forum
Last Post
Threads / Messages

KittHaven

Member
Member
Joined
May 21, 2013
Messages
478
Points
28
Age
25
Location
Devon, UK
Mysidian Dollar
8,292
Hi! I really liked the lootbox mod here but I really wanted to be able to use the value field in the DB and not need to make more fields just to hold item IDs so I managed to make this mod. You can use it as both a chest or a lootbox, it accepts as many item IDs as you want. So if you only want users to get 1 item from it, you can, or maybe you want users to randomly be given an item from 5 different options... currently it only gives 1 item at a time but I might add more options eventually.

So, first we need to make a change to the items table in the DB. Currently the value field will only accept 1 integer. We need to change that to VARCHAR. This will also let you get more use out of the value table so in my opinion it's worth doing. You just need to make sure when making items that you put the correct values in.

Open your DB, adopts_items, click structure and change 'value' to this:

2023-11-18 01_22_55-localhost _ MySQL _ mysidia _ adopts_items _ phpMyAdmin 5.2.0 and 10 more ...png

While we're here, we might as well add the new item function. So open adopts_items_functions table. Go to insert a new entry and put this:

2023-11-18 01_37_41-localhost _ MySQL _ mysidia _ adopts_items_functions _ phpMyAdmin 5.2.0 an...png

Now we need to change a few lines in the files as value is sometimes forced to be an integer, so we need to remove that.

Open, controller/admincp/itemcontroller.php and find and replace public function add:

PHP:
public function add(){
        $mysidia = Registry::get("mysidia");       
        if($mysidia->input->post("submit")){
            $this->dataValidate();
            $imageurl = ($mysidia->input->post("existingimageurl") == "none") ? $mysidia->input->post("imageurl") : $mysidia->input->post("existingimageurl");       
            $mysidia->db->insert("items", ["category" => $mysidia->input->post("category"), "itemname" => $mysidia->input->post("itemname"), "description" => $mysidia->input->post("description"), "imageurl" => $imageurl, "function" => $mysidia->input->post("function"), "target" => $mysidia->input->post("target"), "value" => $mysidia->input->post("value"),
                                           "shop" => (int)$mysidia->input->post("shop"), "price" => (int)$mysidia->input->post("price"), "chance" => (int)$mysidia->input->post("chance"), "cap" => (int)$mysidia->input->post("cap"), "tradable" => $mysidia->input->post("tradable"), "consumable" => $mysidia->input->post("consumable")]);       
        }

Next, in the same file, find and replace public function edit:

PHP:
public function edit($id = NULL){
        $mysidia = Registry::get("mysidia");       
        if(!$id) return $this->index();
        try{
            $item = new Item($id);
            if($mysidia->input->post("submit")){
                 $this->dataValidate();
                $imageurl = ($mysidia->input->post("existingimageurl") == "none") ? $mysidia->input->post("imageurl") : $mysidia->input->post("existingimageurl");
                $mysidia->db->update("items", ["category" => $mysidia->input->post("category"), "itemname" => $mysidia->input->post("itemname"), "description" => $mysidia->input->post("description"), "imageurl" => $imageurl, "function" => $mysidia->input->post("function"), "target" => $mysidia->input->post("target"), "value" => $mysidia->input->post("value"),
                                               "shop" => (int)$mysidia->input->post("shop"), "price" => (int)$mysidia->input->post("price"), "chance" => (int)$mysidia->input->post("chance"), "cap" => (int)$mysidia->input->post("cap"), "tradable" => $mysidia->input->post("tradable"), "consumable" => $mysidia->input->post("consumable")], "id='{$item->getID()}'");             
            }
            $this->setField("item", $item);
        }
        catch(Exception $e){
            throw new InvalidIDException("nonexist");
        }
    }

All that does is replace:

"value" => (int)$mysidia->input->post("value")

to:

"value" => $mysidia->input->post("value")

Which will allow you to have value be whatever you need it to be.

Next, open model/domainmodel/itemfunction.php. At the top, find:

PHP:
protected $validFunctions = ["Valuable", "Level1", "Level2", "Level3", "Click1", "Click2", "Breed1", "Breed2", "Alts1", "Alts2", "Name1", "Name2"];

Replace it with:

PHP:
protected $validFunctions = ["Valuable", "Level1", "Level2", "Level3", "Click1", "Click2", "Breed1", "Breed2", "Alts1", "Alts2", "Name1", "Name2", "Lootbox"];

Next, scroll down and add this function among the others:

PHP:
protected function applyLootbox(OwnedItem $item, OwnedAdoptable $adopt){
        $mysidia = Registry::get("mysidia");
        $loot = explode(',', $item->getValue());
        $num_loot = count($loot);
        if ($num_loot == 1){
            $reward = $item->getValue();
            $itemReward = new OwnedItem($reward, $mysidia->user->getID());
            if($itemReward->isOverCap(1)) throw new ItemException("It appears that you cannot add one more of item {$itemReward->getItemname()} to your inventory, its quantity has already exceeded the upper limit.");               
            $itemReward->add(1, $mysidia->user->getID());
            return "You opened the {$item->getItemname()} and received {$itemReward->getItemname()}!";
        }
        else{
            shuffle($loot);
            $reward = $loot[0];
            $itemReward = new OwnedItem($reward, $mysidia->user->getID());
            if($itemReward->isOverCap(1)) throw new ItemException("It appears that you cannot add one more of item {$itemReward->getItemname()} to your inventory, its quantity has already exceeded the upper limit.");               
            $itemReward->add(1, $mysidia->user->getID());
            return "You opened the {$item->getItemname()} and received {$itemReward->getItemname()}!";
        }
    }

Essentially, if you only have 1 item ID in 'value' it will give the user that item. If there are multiple item IDs in 'value' it will shuffle them and choose 1 to give the user.

Now you can create a new item that is a lootbox! When putting in the value, you need to put the item IDs that you want the user to get. Separate them by a comma.

Example:

2023-11-18 01_39_28-Mysidia Adoptables v1.3.6 and 10 more pages - Personal - Microsoft​ Edge.png

This item will randomly give the user items with the IDs 2, 3, and 4. You can also just have one ID if you want the item to only contain 1.

2023-11-18 01_01_16-Mysidia Adoptables v1.3.6 and 12 more pages - Personal - Microsoft​ Edge.png

Currently you need to choose an adoptable to open it with you but that's a limitation of Mysidia right now as user items aren't implemented yet.

Have fun, anyway!
 
Last edited:
How to add a random amount to the reward

If you want your users to be able to receive more than 1 of the item, you can replace public function applyLootbox with this:

PHP:
protected function applyLootbox(OwnedItem $item, OwnedAdoptable $adopt){
        $mysidia = Registry::get("mysidia");
        $loot = explode(',', $item->getValue());
        $num_loot = count($loot);
        $rewardAmount = rand(1,10);
        if ($num_loot == 1){
            $reward = $item->getValue();
            $itemReward = new OwnedItem($reward, $mysidia->user->getID());
            if($itemReward->isOverCap(1)) throw new ItemException("It appears that you cannot add one more of item {$itemReward->getItemname()} to your inventory, its quantity has already exceeded the upper limit.");              
            $itemReward->add($rewardAmount, $mysidia->user->getID());
            return "You opened the {$item->getItemname()} and received {$rewardAmount}x {$itemReward->getItemname()}!";
        }
        else{
            shuffle($loot);
            $reward = $loot[0];
            $itemReward = new OwnedItem($reward, $mysidia->user->getID());
            if($itemReward->isOverCap(1)) throw new ItemException("It appears that you cannot add one more of item {$itemReward->getItemname()} to your inventory, its quantity has already exceeded the upper limit.");              
            $itemReward->add($rewardAmount, $mysidia->user->getID());
            return "You opened the {$item->getItemname()} and received {$rewardAmount}x {$itemReward->getItemname()}!";
        }
    }

Change $rewardAmount = rand(1,10); to whatever you want the random amount to be.

Limitations: If you have the same lootbox dropping from itself it doesn't actually give you the item, no matter how many you try to have it give. However, I don't really see much need for you to do this... so I'm not sure I'll look into why. :LOL: If you want a lootbox to drop another lootbox I'd say just make another one so it has a different ID and isn't the one currently being used lol.
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,122
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Latest Posts

Top