Skip to content

Commit

Permalink
Merge pull request #7 from Hebbinkpro/v1.1.0
Browse files Browse the repository at this point in the history
PiggyCustomEnchants support
  • Loading branch information
Hebbinkpro committed Dec 19, 2020
2 parents 4d9d6b9 + 0c794f1 commit deef23d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
4 changes: 2 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: MagicCrates
description: Add crates to your server
author: Hebbinkpro
version: 1.0.8
version: 1.1.0
main: Hebbinkpro\MagicCrates\Main
api: 3.14.2
mcpe-protocol: [389,390,407,408,419,422]
mcpe-protocol: [407,408,419,422]

permissions:
magiccrates.cmd:
Expand Down
19 changes: 10 additions & 9 deletions resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#delay: The delay before the item animation begins
#types: your crate types
#every type needs the following format, you can add as much as you want types, items and item enchantments
#for commands you can use the following formatting:
# delay: The delay before the item animation begins
# types: your crate types
# every type needs the following format, you can add as much as you want types, items and item enchantments
# PiggyCustomEnchants support! Just enter the enchantment in the same format as a default enchantment
# for commands you can use the following formatting:
# - {player} for the player
# - {reward} for the reward
# - {crate} for the crate type
#The item change is the change to get that item from the crate
# The item change is the change to get that item from the crate
# <type>:
# name: <the text that appears above the created crates>
# rewards:
Expand All @@ -15,16 +16,16 @@
# amount: <amount of the item>
# lore: <custom item lore>
# enchantments:
# - id: <enchantment_id>
# - name: <enchantment_name> - from default or custom enchant
# level: <echantment_level>
# change: <the change to get the item>
# commands:
# - <command>
# - <second command>
# - etc...


delay: 1

types:
common:
name: §eCommon §6Crate
Expand All @@ -36,7 +37,7 @@ types:
lore: Special Diamond
enchantments:
- name: efficiency
level: 500
level: 1
change: 1
- name: Garbage
id: 3
Expand All @@ -45,5 +46,5 @@ types:
lore: Garbage
change: 10
commands:
- say {player} won {reward} from na {crate}
- say {player} won {reward} from a {crate}
- givemoney {player} 100
42 changes: 26 additions & 16 deletions src/Hebbinkpro/MagicCrates/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Hebbinkpro\MagicCrates;

use DaPigGuy\PiggyCustomEnchants\CustomEnchantManager;
use Hebbinkpro\MagicCrates\forms\CrateForm;
use Hebbinkpro\MagicCrates\tasks\CreateEntityTask;
use pocketmine\Player;
Expand Down Expand Up @@ -96,28 +97,24 @@ public function onInteractChest(PlayerInteractEvent $e){
return;
}
}

if($item->getId() === ItemIds::PAPER ){
if(!in_array("§6Magic§cCrates §7Key - " . $crateType, $item->getLore()) or $item->getCustomName() != "§e" . $crateType . " §r§dCrate Key"){
$player->sendMessage("[§6Magic§cCrates§r] §cUse a crate key to open this §e$crateType §r§ccrate");
$e->setCancelled();
return;
}
//create the key
$key = new Item(ItemIds::PAPER);
$key->setCustomName("§e" . $crateType . " §r§dCrate Key");
$key->setLore(["§6Magic§cCrates §7Key - " . $crateType]);
$key->addEnchantment(new EnchantmentInstance(Enchantment::getEnchantment(Enchantment::UNBREAKING), 1));


//check if a new reward can be add
if(!$player->getInventory()->canAddItem(new Item(298,0))){
$player->sendMessage("[§6Magic§cCrates§r] §cYour inventory is full, come back later when your inventory is cleared!");
$e->setCancelled();
return;
}

//remove the key from the inventory
$player->getInventory()->removeItem($key);

//remove item
$item->setCount(1);

This comment has been minimized.

Copy link
@jasonw4331

jasonw4331 Dec 19, 2020

Why remove the whole stack when you could just $item->pop(); it off the top?

$player->getInventory()->removeItem($item);

}else{
$player->sendMessage("[§6Magic§cCrates§r] §cUse a crate key to open this §e$crateType §r§ccrate");
Expand Down Expand Up @@ -162,25 +159,35 @@ public function onInteractChest(PlayerInteractEvent $e){
$item = new Item($id, $meta, $name);
$item->setCustomName($name);
$item->setLore([$lore, "\n§a$crateType §r§6Crate", "§7Pickup: §cfalse"]);

$ce = $this->main->getServer()->getPluginManager()->getPlugin("PiggyCustomEnchants");
foreach ($enchantments as $ench){
$ename = $ench["name"];
$eName = $ench["name"];
$lvl = intval($ench["level"]);
$enchantment = Enchantment::getEnchantmentByName($ename);

// get enchantment
$enchantment = Enchantment::getEnchantmentByName($eName);
// if enchantment doesn't exists in pmmp, check if it's a PiggyCustomEnchant (if piggycustomenchants exists as plugin in your server)
if(is_null($enchantment) and !is_null($ce) and $ce->isEnabled()){
$enchantment = CustomEnchantManager::getEnchantmentByName($eName);
}

// apply enchantment
if($enchantment instanceof Enchantment){
$item->addEnchantment(new EnchantmentInstance($enchantment, $lvl));
}
}

//create spawn position
// create spawn position
$spawnX = $bX + 0.5;
$spawnY = $bY + 1;
$spawnZ = $bZ + 0.5;
$spawnPos = new Vector3($spawnX, $spawnY, $spawnZ);

//set crate in opening state
// set crate in opening state
$this->main->openCrates[$crateKey] = $player->getName();

//create nbt
// create nbt
$itemTag = $item->nbtSerialize();
$itemTag->setName("Item");
$nbt= Entity::createBaseNBT($spawnPos);
Expand All @@ -193,12 +200,15 @@ public function onInteractChest(PlayerInteractEvent $e){
$nbt->setShort("CrateKey", $crateKey);
$nbt->setTag($itemTag);

//create entity
// create entity
$delay = $this->config->get("delay") * 20;
if(!is_int($delay)){
$delay = 0;
}

// open crate
$this->main->getScheduler()->scheduleDelayedTask(new CreateEntityTask($name, $bLevel, $nbt, $count), $delay);
$player->sendMessage("§eYou are opening a $crateType crate...");

$e->setCancelled();
}
Expand Down

0 comments on commit deef23d

Please sign in to comment.