1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

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

Enchanted books (Create/Add Enchantments)

Discussion in 'Snippets' started by ShaneBee, Nov 18, 2018.

  1. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,184
    Likes Received:
    220
    This will allow you to create new enchanted books (even though you can get them in creative) but also ADD enchantments to your books, making super custom books
    Requires Skript-Mirror


    This is the actual command to create the books and/or add enchantments to books
    Code (Skript):
    1. import:
    2.     org.bukkit.enchantments.Enchantment
    3.     org.bukkit.event.server.TabCompleteEvent
    4.     java.util.Arrays
    5.  
    6. command /test <enchantment>:
    7.     trigger:
    8.         enchant player's tool with arg-1
    9.  
    10. command /bookench <text> <enchantment> [<integer=1>]:
    11.     usage: &a/BookEnch &7<&bnew&7/&badd&7> <&benchantment&7> [&6level&7]
    12.     permission: book.ench
    13.     permission message: You do not have permission to use this command
    14.     trigger:
    15.         if arg-1 is "new":
    16.             if arg-3 <= arg-2.getMaxLevel():
    17.                 set {_book} to "enchanted book" parsed as item
    18.                 set {_meta} to {_book}.getItemMeta()
    19.                 set {_ench} to arg-2
    20.                 {_meta}.addStoredEnchant({_ench}, arg-3, false)
    21.                 {_book}.setItemMeta({_meta})
    22.                 give player {_book}
    23.                 if arg-3 >= 2:
    24.                     send "&aNew enchanted book of &b%arg-2% %arg-3% &acreated!"
    25.                 else:
    26.                     send "&aNew enchanted book of &b%arg-2% &acreated!"
    27.             else:
    28.                 send "&cENCHANTMENT LEVEL TOO HIGH"
    29.         else if arg-1 is "add":
    30.             if player's tool is enchanted book:
    31.                 if arg-3 <= arg-2.getMaxLevel():
    32.                     set {_book} to player's tool
    33.                     set {_meta} to {_book}.getItemMeta()
    34.                     set {_ench} to arg-2
    35.                     {_meta}.addStoredEnchant({_ench}, arg-3, false)
    36.                     {_book}.setItemMeta({_meta})
    37.                     if arg-3 >= 2:
    38.                         send "&b%arg-2% %arg-3% &ahas been added to your book"
    39.                     else:
    40.                         send "&b%arg-2% &ahas been added to your book"
    41.                 else:
    42.                     send "&cENCHANTMENT LEVEL TOO HIGH"
    43.             else:
    44.                 send "&cYOU NEED AN ENCAHNTED BOOK TO USE THIS COMMAND"
    This part is the tab completer for when you are creating a book, it'll auto fill in info for you
    Code (Skript):
    1. on TabCompleteEvent:
    2.     if event.getBuffer() contains "/bookench new " or "/bookench add ":
    3.         set {_buff} to event.getBuffer()
    4.         replace "/bookench new " and "/bookench add " with "" in {_buff}
    5.         set {_list} to "%Enchantment.values()%"
    6.         set {_list::*} to {_list} split at ", "
    7.         set {_num} to length of {_buff}
    8.         loop {_list::*}:
    9.             if first {_num} characters of loop-value is {_buff}:
    10.                 add loop-value to {_newbuff::*}
    11.         if {_buff} contains " ":
    12.             if last character of {_buff} is " ":
    13.                 set {_ench} to first (length of {_buff} - 1) characters of {_buff}
    14.                 set {_ench} to {_ench} parsed as enchantment
    15.                 set {_max} to {_ench}.getMaxLevel()
    16.                 event.setCompletions(Arrays.asList("[<max-level=%{_max}%>]"))
    17.         else:
    18.             if size of {_newbuff::*} = 0:
    19.                 event.setCompletions(Arrays.asList({_list::*}))
    20.             else if size of {_newbuff::*} >= 1:
    21.                 event.setCompletions(Arrays.asList({_newbuff::*}))
    22.     else if event.getBuffer() = "/bookench ":
    23.         set {_list::*} to "new" and "add"
    24.         event.setCompletions(Arrays.asList({_list::*}))
    25.     else if event.getBuffer() = "/bookench a" or "/bookench ad":
    26.         event.setCompletions(Arrays.asList("add"))
    27.     else if event.getBuffer() = "/bookench n" or "/bookench ne":
    28.         event.setCompletions(Arrays.asList("new"))
     
    • Like Like x 1
  2. Dabriel

    Dabriel Member

    Joined:
    Aug 13, 2018
    Messages:
    45
    Likes Received:
    5
    This is actually pretty neat.
    can be used in some stuff and it's a good thing to actually look at the code and learn from it. Nice :thumbs_up:
     
  3. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,184
    Likes Received:
    220
    Thanks.
    I kept trying to deal with custom books in the past having no luck, so this system really helped that out. Took a while to create but it works, YAY!
     

Share This Page

Loading...