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:
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:
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:
Next, in the same file, find and replace public function edit:
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:
Replace it with:
Next, scroll down and add this function among the others:
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:
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.
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!
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:

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:

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:

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.

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: