Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Custom enchants

Comments in 'Plugin Development' started by grpisawesome, Oct 4, 2015.

  1. grpisawesome
    Offline

    grpisawesome New Member

    Joined:
    Dec 23, 2013
    Posts:
    34
    Minecraft User:
    grpisawesome
    Is there any way to make a custom enchants plugin at this point in time I want one for a faction server I'm making.
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    What do you mean by "custom"? Be more specific.
    CraftYourBukkit likes this.
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    $i1 Item::get($id$meta$amount);
    $e1 Enchantment::getEnchantment($eid);
    $e1->setLevel($level);
    $i1->addEnchantment($e1);
  4. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    That's just code on how to add an enchantment to an item. He wants to make custom enchantments.
  5. grpisawesome
    Offline

    grpisawesome New Member

    Joined:
    Dec 23, 2013
    Posts:
    34
    Minecraft User:
    grpisawesome
    Will it allow me to create my own enchantments though
  6. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    You can't create own enchantments. You can use EntityDamageEvent to pretend custom enchantmens
  7. grpisawesome
    Offline

    grpisawesome New Member

    Joined:
    Dec 23, 2013
    Posts:
    34
    Minecraft User:
    grpisawesome
    Like a enchant that set poison to a person when hit with a sword or armour enchant thatcan block damage.
  8. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
  9. grpisawesome
    Offline

    grpisawesome New Member

    Joined:
    Dec 23, 2013
    Posts:
    34
    Minecraft User:
    grpisawesome
    OK but can I add a pretend enchant name to the enchant list on item.
  10. markens123
    Offline

    markens123 New Member

    Joined:
    Oct 15, 2016
    Posts:
    1
    Minecraft User:
    markens123
    Idk if there is a way on pe only on PC I see costom enchant servers.
  11. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Yes, you can but you have to save the "custom" enchantments into a file/database if you want to save them permanently, or if you want to save temporary enchantments you can use arrays.

    Side note: To store the data permanently it is recommended to use a single compressed file, an SQLite database or MySQL/i database. Never use separate files, or yaml files to store data, you're gonna waste a lot of storage space from the machine your server is running on.
    ImagicalGamer likes this.
  12. ImagicalGamer
    Offline

    ImagicalGamer Active Member

    Joined:
    Feb 25, 2016
    Posts:
    141
    Minecraft User:
    ImagicalGamer
    JSON could be used to store enchant data for those who don't know how to use SQLite and do not want to learn. but I agree SQLite or MySQL would be faster.
  13. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    You can use serialize or Json and compress it ;)
  14. RumDaDuMCPE
    Offline

    RumDaDuMCPE New Member

    Joined:
    Oct 18, 2016
    Posts:
    12
    Minecraft User:
    RumDaDuMCPE
    Here's some (not so) simple code:

    PHP:
    <?php

    namespace RumDaDuMCPE\CustomEnchants;

    use 
    pocketmine\plugin\PluginBase;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\{EntityArmorChangeEventEntityDamageByEntityEventEntityDamageEvent};
    use 
    pocketmine\event\player\PlayerDeathEvent;


    use 
    pocketmine\utils\TextFormat as c;

    use 
    pocketmine\Player;
    use 
    pocketmine\Server;

    use 
    pocketmine\entity\Effect;

    use 
    pocketmine\item\enchantment\Enchantment;
    use 
    pocketmine\item\Item;

    class 
    Main extends PluginBase implements Listener{

      public function 
    onEnable(){
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        
    $this->getServer()->getLogger()->notice(c::AQUA "CustomEffects " c::YELLOW "by " c::RED "RumDaDuMCPE" c::YELLOW " has now been enabled!");
      }
        public function 
    onDamage(EntityDamageEvent $e) {
            if (
    $e instanceof EntityDamageByEntityEvent) {
            
    $damager $e->getDamager();
            
    $p $e->getEntity();
            
    $thing $damager->getInventory()->getItemInHand();
            if (
    $thing->getId() === 276 and $thing->getCustomName() === c::AQUA "Snake" c::GRAY "\n50% Chance to cause poison" "\n\n") {
               switch (
    mt_rand(1,5)) {
                    case 
    1:
                        return;
                    case 
    2:
                        return;
                    case 
    3:
                        return;
                    case 
    4:
                        return;
                    case 
    5:
                        
    $p->addEffect(Effect::getEffect(19)->setDuration(13 20)->setAmplifier(1));
            } 
          }
    }
    It checks whether the damager is holding a diamond sword with the custom name: "Snake" and if true, It gives the damager a 50% chance of effecting the target player with poison for 13 seconds. Hope it helps you get an Idea of custom enchants and if this is not what you had meant then I'm sorry.
  15. ImagicalGamer
    Offline

    ImagicalGamer Active Member

    Joined:
    Feb 25, 2016
    Posts:
    141
    Minecraft User:
    ImagicalGamer
    Another way (that's safer) is to add an tag to an item (view Item::addEnchantment()), with the renaming method a player could use an Anvil to rename a sword and bam it now has an OP enchant.
    RumDaDuMCPE likes this.
  16. RumDaDuMCPE
    Offline

    RumDaDuMCPE New Member

    Joined:
    Oct 18, 2016
    Posts:
    12
    Minecraft User:
    RumDaDuMCPE
    How do I add a Tag? Teach me senpai :3
    jasonwynn10 likes this.
  17. RumDaDuMCPE
    Offline

    RumDaDuMCPE New Member

    Joined:
    Oct 18, 2016
    Posts:
    12
    Minecraft User:
    RumDaDuMCPE
    Hey, nevermind! I understand what you mean.
    PHP:
    $tag $item->getNamedTag();
    if(
    $tag instanceof CompoundTag) {
    $item->setNamedTag($tag);
    }
  18. RumDaDuMCPE
    Offline

    RumDaDuMCPE New Member

    Joined:
    Oct 18, 2016
    Posts:
    12
    Minecraft User:
    RumDaDuMCPE
    But then, How may I add the tag?

    PHP:
    <?phpnamespace RumDaDuMCPE\CustomEnchants;

    use 
    pocketmine\plugin\PluginBase;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\{EntityArmorChangeEventEntityDamageByEntityEventEntityDamageEvent};
    use 
    pocketmine\event\player\PlayerDeathEvent;


    use 
    pocketmine\utils\TextFormat as c;

    use 
    pocketmine\Player;
    use 
    pocketmine\Server;

    use 
    pocketmine\entity\Effect;

    use 
    pocketmine\item\enchantment\Enchantment;
    use 
    pocketmine\item\Item;

    class 
    Main extends PluginBase implements Listener{

    public function 
    onEnable(){$this->getServer()->getPluginManager()->registerEvents($this$this);$this->getServer()->getLogger()->notice(c::AQUA "CustomEffects " c::YELLOW "by " c::RED "RumDaDuMCPE" c::YELLOW " has now been enabled!");
    }
    public function 
    onDamage(EntityDamageEvent $e) {
    if (
    $e instanceof EntityDamageByEntityEvent) {$damager $e->getDamager();$p $e->getEntity();$thing $damager->getInventory()->getItemInHand();
    if (
    $thing->getId() === 276 and $thing->getNamedTag() === c::AQUA "Snake" c::GRAY "\n50% Chance to cause poison" "\n\n") {
    switch (
    mt_rand(1,5)) {
    case 
    1:
    return;
    case 
    2:
    return;
    case 
    3:
    return;
    case 
    4:
    return;
    case 
    5:$p->addEffect(Effect::getEffect(19)->setDuration(13 20)->setAmplifier(1));
    }
    }
    public function 
    onJoin(PlayerJoinEvent $e) {
    $p $e->getPlayer();
    $i Item::get(27601);
    $i->setNamedTag(c::AQUA "Snake" c::GRAY "\n50% Chance to cause poison" "\n\n");
    $p->getInventory()->addItem($i);
    }
    would this work?
  19. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Try using this class that I made.

    !!!!NOTICE, for CustomEnchantments class constructor you must set your plugin base class!!!!
    PHP:
    class CustomEnchantments{
        protected static 
    $items = [];
        private 
    $validTags =
        [
            
    'damage',
            
    'effectsOnUse'
        
    ];
        private 
    $base;
        public function 
    __construct(YourPluginBaseClass $base){
            
    $this->base $base;
            
    $this->parseItems();
        }
     
        public static function 
    isCustom(\pocketmine\item\Item $item){
            return isset(
    self::$items[spl_object_hash($item)]);
        }
     
        private function 
    parseItems(){
            
    $items = [];
            
    $contents file_get_contents($this->base->getDataFolder() . 'CustomEnchants.dat');
            
    $contents unserialize(gzdecode($contents));
            foreach(
    $items as $itemName => $data){
                
    self::$items[$itemName] = $data;
            }
        }
     
        public function 
    checkValidTags(&$tags = []){
            foreach(
    $this->validTags as $tag){
                if(empty(
    $tags[$tag])){
                    unset(
    $tags[$tag]);
                }
            }
        }
     
        public function 
    useOn(\pocketmine\Player $p, \pocketmine\item\Item $item){
            if(
    self::isCustom($item)){
                if(!empty(
    self::$items[spl_objet_hash($items)]['effectsOnUse'])){
                    foreach(
    self::$items[spl_objet_hash($items)]['effectsOnUse'] as $effect){
                        
    $p->addEffect($effect);
                    }
                }
                if(!empty(
    $self::$items[spl_objet_hash($items)]['damage'])){
                    
    $p->setHealth($p->getHealth() - intval(self::$items[spl_objet_hash($items)]['damage']));
                }
            }
        }
     
        public static function 
    save(){
            
    file_put_contents($this->base->getDataFolder() . 'CustomEnchants.dat'gzencode(serialize(self::$items)))
        }
     
                               
        public static function 
    addItem(\pocketmine\item\Item $item$tags = []){
            if(isset(
    self::$items[spl_object_hash($item)])){
                
    checkValidTags($tags);
                foreach(
    $tags as $tag => $values){
                    
    self::$items[spl_object_hash($item)][$tag] = $values;
                }
                
    self::save();
            }else{
                
    checkValidTags($tags);
                
    self::$items[spl_object_hash($item)] = [];
                foreach(
    $tags as $tag => $values){
                    
    self::$items[spl_object_hash($item)][$tag] = $values;
                }
                
    self::save();
            }
        }
     
        public static function 
    removeItem(\pocketmine\item\Item $item){
            if(isset(
    self::$items[spl_object_hash($item)])){
                unset(
    self::$items[spl_object_hash($item)]);
                
    self::save();
            }
        }
    }

    PHP:
    //on damage logic

    CustomEnchantments::useOn($event->getPlayer(), $event->getItem());

    //how to add enchanted Item

    $someItem Item::get(276);//Whatever tool you want
    $extraDamage 4;//Extra damage on hit
    $durationSeconds 16;//Seconds in duration
    $effects = [Effect::getEffect(Effect::POISON)->setDuration(20 $durationSeconds )];//Effects to set on damage

    CustomEnchantments::addItem($someItem, ['damage' => $extraDamage'effectsOnUse' => $effects]);

    //Remove item

    CustomEnchantments::removeItem($someItem);
  20. Thunder33345
    Offline

    Thunder33345 Notable Member

    Joined:
    Apr 3, 2014
    Posts:
    755
    Minecraft User:
    Thunder33345
    using NBT to store it? look it up i recon something like THIS could do the trick https://github.com/JackNoordhuis/PocketMine-Plugins/tree/master/ExplosiveArrows

Share This Page

Advertisement