How do i make a custom enchant

Discussion in 'Development' started by Besher, Feb 22, 2021.

  1. Besher

    Besher Spider Jockey

    Messages:
    47
    I have looked everywhere but i cant find anything that is usefull that can help me
     
  2. minijaham

    minijaham Zombie Pigman

    Messages:
    749
    GitHub:
    minijaham
    I don't think anyone wants to sit here and explain every process of making a custom enchant, but I will because I'm nice enough to do that!!!!!!! :smile:


    First step is to register your own enchantment.
    This is how I did it to keep things organized!

    1. Create Main class and CustomEnchantment class!
    PHP:

    use pocketmine\plugin\PluginBase;
    // We'll use these later
    use pocketmine\item\Item;
    use 
    pocketmine\nbt\NBT;
    use 
    pocketmine\utils\TextFormat as C;
    use 
    pocketmine\command\{
        
    CommandCommandSenderConsoleCommandSender
    };
    use 
    pocketmine\item\enchantment\{
        
    EnchantmentEnchantmentInstance
    };
    use 
    pocketmine\nbt\tag\{
        
    CompoundTagShortTagListTag
    };

    class 
    Main extends PluginBase{

        public function 
    onEnable(): void{
            new 
    CustomEnchantment();
    }
    Then go to CustomEnchantment.php file you created.

    PHP:
    use pocketmine\item\enchantment\Enchantment;
    use 
    HealthStealer;

    class 
    CustomEnchantment {

        public const 
    HEALTHSTEALER 100// Enchant ID
        // repeat with different number to create more enchants

        
    public function __construct(){
            
    $this->init();
        }
      
        public function 
    init(){
            
    Enchantment::registerEnchantment(new Enchantment(self::HEALTHSTEALER"Health Stealer"Enchantment::RARITY_COMMONEnchantment::SLOT_SWORDEnchantment::SLOT_NONE5));
            new 
    HealthStealer();
            
    // repeat. I'm pretty sure you know what to do now.
        
    }
    }
    Then create a file named HealthStealer.php which will work as our listener.

    PHP:
    use pocketmine\Player;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\{
        
    EntityDamageEventEntityDamageByEntityEvent
    };

    use 
    MainClass;

    class 
    HealthStealer implements Listener {

        public function 
    __construct() {
            
    MainClass::get()->getServer()->getPluginManager()->registerEvents($thisMainClass::get());
        }

        public function 
    onDamage(EntityDamageEvent $event): void {
            if(
    $event instanceof EntityDamageByEntityEvent){
                
    $damager $event->getDamager();
                
    $entity $event->getEntity();

                if(
    $event->isCancelled()) return; // Prevent CE working even when on attack cooldown.
                
    if($damager->getInventory()->getItemInHand()->getEnchantment(100)) { // Checks for the enchantment
                    
    $chance rand(1200); // You can add a simple activation chance using this!
                    
    if ($chance === 198) { // You can add a simple activation chance using this!
                        
    $damager->setHealth($damager->getHealth() + $damager->getMaxHealth() ? $damager->getMaxHealth() : $damager->getHealth() + 5);
                        
    $damager->sendMessage("§7>> §cYou stole health from ".$entity->getName()."!");
                    }
                }
            }
        }
    }
    Now let's display those enchantments!

    Back to your main file!

    PHP:
      
        
    public function onCommand(CommandSender $senderCommand $cmdstring $label, array $args): bool{
            if(
    $cmd->getName() == "customenchant"){
                if(
    count($args) < 2){
                    
    $sender->sendMessage("Usage: /ceenchant <player> <enchantment> <level>");
                    return 
    false;
                }
                if(!
    $this->getServer()->getPlayer($args[0])){
                    
    $sender->sendMessage("Player "$args[0] . " cannot be found.");
                    return 
    false;
                }
                
    $player $this->getServer()->getPlayer($args[0]);
                
    $inv $player->getInventory();

                if(
    $inv->getItemInHand() === null){
                    
    $player->sendMessage("You must hold an item to proceed.");
                    
    $sender->sendMessage($player->getName() . " Is not holding an item!");
                    return 
    false;
                }
                if(
    is_numeric($args[1])){
                    
    $enchantment Enchantment::getEnchantment((int) $args[1]);
                }else{
                    
    $enchantment Enchantment::getEnchantmentByName($args[1]);
                }
                if(
    $args[1] < 100 || !$enchantment instanceof Enchantment){
                    
    $sender->sendMessage("That enchantment cannot be found.");
                    return 
    false;
                }

                
    $item $inv->getItemInHand();
                
    $this->addEnchantment($item, new EnchantmentInstance($enchantment, (int) ($args[2] ?? 1)), $enchantment);
                
    $inv->setItemInHand($item);
            }
        }
        public function 
    addEnchantment(Item $itemEnchantmentInstance $enchantmentEnchantment $e): void{
            if(
    $item->hasEnchantment($enchantment->getId())){
                
    $lvl $item->getEnchantment($enchantment->getId())->getLevel();
                
    $lore $item->getLore();
                
    $name array_search($e->getName() . " " $lvl$lore);
                unset(
    $lore[$name]);
                
    $item->setLore($lore);
                
    $item->removeEnchantment($enchantment->getId());
            }

            
    $found false;
            
    $ench $item->getNamedTagEntry(Item::TAG_ENCH);

            if(!(
    $ench instanceof ListTag)){
                
    $ench = new ListTag(Item::TAG_ENCH, [], NBT::TAG_Compound);
            }else{
                foreach(
    $ench as $k => $entry){
                    if(
    $entry->getShort("id") == $enchantment->getId()){
                        
    $nbt = new CompoundTag("", [
                            new 
    ShortTag("id"$enchantment->getId()),
                            new 
    ShortTag("lvl"$enchantment->getLevel())
                        ]);
                        
    $ench->set($k$nbt);
                        
    $item->setLore(array_merge($item->getLore(), [$e->getName() . " " $this->roman($enchantment->getLevel())]));
                        
    $found true;
                        break;
                    }
                }
            }

            if(!
    $found){
                
    $nbt = new CompoundTag("", [
                    new 
    ShortTag("id"$enchantment->getId()),
                    new 
    ShortTag("lvl"$enchantment->getLevel())
                ]);
                
    $ench->push($nbt);
                
    $item->setLore(array_merge($item->getLore(), [$e->getName() . " " $this->roman($enchantment->getLevel())]));
            }
            
    $item->setNamedTagEntry($ench);
        }
      
      
        public function 
    roman(int $lvl): string{
            
    $string "";
            
    $romans = [
                
    "C" => 100,
                
    "L" => 50,
                
    "XL" => 40,
                
    "X" => 10,
                
    "IX" => 9,
                
    "VIII" => 8,
                
    "VII" => 7,
                
    "VI" => 6,
                
    "V" => 5,
                
    "IV" => 4,
                
    "III" => 3,
                
    "II" => 2,
                
    "I" => 1
            
    ];

            while(
    $lvl 0){
                foreach(
    $romans as $roman => $int){
                    if(
    $lvl >= $int){
                        
    $lvl -= $int;
                        
    $string .= $roman;
                        break;
                    }
                }
            }
            return 
    $string;
        }
    Simplest way to create custom enchant! :D
     
    Primus, IvanCraft623 and Besher like this.
  3. Besher

    Besher Spider Jockey

    Messages:
    47
    I love you
     
    Primus likes this.
  4. IvanCraft623

    IvanCraft623 Slime

    Messages:
    96
    GitHub:
    IvanCraft623
    woww, you are amazing. I didn't know how to do it, thank you!
     
    Primus likes this.
  5. minijaham

    minijaham Zombie Pigman

    Messages:
    749
    GitHub:
    minijaham
    No problem guys!
     
    Primus likes this.
  6. Mcbeany

    Mcbeany Spider Jockey

    Messages:
    33
    GitHub:
    mcbeany
    There's one more, easier way
    You can use PiggyCustomEnchant as an API
     
    minijaham and Primus like this.
  7. minijaham

    minijaham Zombie Pigman

    Messages:
    749
    GitHub:
    minijaham
    True, but it can be confusing for noob developers >:[
     
    Mcbeany likes this.
  8. Kayllon

    Kayllon Silverfish

    Messages:
    24
    hello could you help me? I wanted to make a Custom Enchant Plugin, but for version 0.15.10 or 1.1.5, DaPiggyGuy's api is very complex so I wanted to know if you could explain it to me ( in the versions I mentioned the enchantment api is different :/ )

    Why I use such an old version? Performance and still have a good amount of players playing lol
     
  9. minijaham

    minijaham Zombie Pigman

    Messages:
    749
    GitHub:
    minijaham
    I have no clue of what API it had back then.
    Mind sending me a link to it?
     
    Agent likes this.
  10. Kayllon

    Kayllon Silverfish

    Messages:
    24
  11. minijaham

    minijaham Zombie Pigman

    Messages:
    749
    GitHub:
    minijaham
    Could you upload this as a github repo? I want to read the files without having to download it..
     
    Agent likes this.
  12. minijaham

    minijaham Zombie Pigman

    Messages:
    749
    GitHub:
    minijaham
    and no. As long as I can understand what the version is doing, I can help you through!
    If you have discord, please add me: minijaham#3259
     
    Agent likes this.
  13. PiloudeDakar

    PiloudeDakar Witch

    Messages:
    55
    GitHub:
    PiloudeDakar
  14. PiloudeDakar

    PiloudeDakar Witch

    Messages:
    55
    GitHub:
    PiloudeDakar
    I think you mistaken with the plugin version. The plugin version is different of the API version.
     
  15. Kayllon

    Kayllon Silverfish

    Messages:
    24
    Bro I described above why I'm using an old version :)
     
  16. Kayllon

    Kayllon Silverfish

    Messages:
    24
    I sent the request to Discord, https://github.com/GenisysPro/GenisysPro esta é a api que estou usando no momento
     
  17. minijaham

    minijaham Zombie Pigman

    Messages:
    749
    GitHub:
    minijaham
    Agent likes this.
  18. Kayllon

    Kayllon Silverfish

    Messages:
    24
    nah relax, i understand :)
     
  19. minijaham

    minijaham Zombie Pigman

    Messages:
    749
    GitHub:
    minijaham
    I cannot test if the method works, since I'm unable to get on the version of minecraft, but here.

    Simply register the enchantment first:
    PHP:
    const TYPE_WEAPON_ENCHANTNAME;

    Enchantment::registerEnchantment(self::TYPE_WEAPON_ENCHANTNAME"Vampire"Enchantment::RARITY_COMMONEnchantment::ACTIVATION_EQUIPEnchantment::SLOT_SWORD);
    new 
    CustomEnchantName();
    Then, implement the enchantment:
    PHP:
    class CustomEnchantName implements Listener {
        public function 
    __construct() {
            
    MainClass::get()->getServer()->getPluginManager()->registerEvents($thisMainClass::get());
        }
        public function 
    onDamage(EntityDamageEvent $event) : void // the event can vary depending on what you're trying to do
            
    if($event instanceof EntityDamageByEntityEvent){
                
    // code
            
    }
        }
    }
    Now, when you're trying to enchant the custom enchant:
    PHP:
    if($item->hasEnchantment($enchantment->getId())) { // If the item has this enchantment
          
    $level $item->getEnchantment($enchant)->getLevel();
          
    $lore $item->getLore();
          
    $name array_search($enchant->getName() . " " $this->convert($level), $lore);
          unset(
    $lore[$name]);
          
    $item->setLore($lore);
          
    $item->removeEnchantment($enchant);
       }
       
    $item->setLore(array_merge($item->getLore(), [$enchant->getName() . " " $this->convert($enchantment->getLevel())]));
       }
        public function 
    convert(int $lvl) : string // converts the enchantment level to roman numeral
        
    {
            
    $string "";
            
    $romans = [
                
    'M' => 1000,
                
    'CM' => 900,
                
    'D' => 500,
                
    'CD' => 400,
                
    'C' => 100,
                
    'XC' => 90,
                
    'L' => 50,
                
    'XL' => 40,
                
    'X' => 10,
                
    'IX' => 9,
                
    'V' => 5,
                
    'IV' => 4,
                
    'I' => 1
            
    ];

            while(
    $lvl 0){
                foreach(
    $romans as $roman => $int){
                    if(
    $lvl >= $int){
                        
    $lvl -= $int;
                        
    $string .= $roman;
                        break;
                    }
                }
            }
            return 
    $string;
        }
    Do something like this. I can't really figure out how I can make this work in this version :l

    perhaps,...someone else can continue with this and help?
     
    Agent likes this.
  20. minijaham

    minijaham Zombie Pigman

    Messages:
    749
    GitHub:
    minijaham
    You should be able to figure out how to do things now :l
    Just follow the general structure I made
     
    Agent likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.