Change Book Contents

Discussion in 'Plugin Development' started by JamesS237, Aug 10, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    JamesS237

    Hi All,
    I'm Making A Plugin To Give New Users "Instruction Manuals".

    I have some code, but I need to give the user a book and then change its contents:

    Code:
    public class Manuals
    {
        private String author;
        private String title;
        private String[] pages;
       
        public void onDisable() {
            // TODO: Place any custom disable code here.
            System.out.println(this + " is now disabled!");
        }
     
        public void onEnable() {
            // TODO: Place any custom enable code here, such as registering events
            System.out.println(this + " is now enabled!");
        }
     
        public void Book(ItemStack bookItem){
            NBTTagCompound bookData = ((CraftItemStack) bookItem).getHandle().tag;
           
            this.author = bookData.getString("author");
            this.title = bookData.getString("title");
                   
            NBTTagList nPages = bookData.getList("pages");
     
            String[] sPages = new String[nPages.size()];
            for(int i = 0;i<nPages.size();i++)
            {
                sPages[i] = nPages.get(i).toString();
            }
                   
            this.pages = sPages;
        }
     
        void Book(String title, String author, String[] pages) {
            this.title = title;
            this.author = author;
            this.pages = pages;
        }
       
        public String getAuthor()
        {
            return author;
        }
     
        public void setAuthor(String sAuthor)
        {
            author = sAuthor;
        }
       
        public String getTitle()
        {
            return title;
        }
       
        public String[] getPages()
        {
            return pages;
        }
     
        public ItemStack generateItemStack(){
            CraftItemStack newbook = new CraftItemStack(Material.WRITTEN_BOOK);
           
            NBTTagCompound newBookData = new NBTTagCompound();
           
            newBookData.setString("author",author);
            newBookData.setString("title",title);
                   
            NBTTagList nPages = new NBTTagList();
            for(int i = 0;i<pages.length;i++)
            { 
                nPages.add(new NBTTagString(pages[i],pages[i]));
            }
           
            newBookData.set("pages", nPages);
     
            newbook.getHandle().tag = newBookData;
           
            return (ItemStack) newbook;
        }
    And I Am Wondering How To Implement This Code.

    Thanks,

    JamesS237 [diamond]
     
  2. Offline

    tommykins20

    Sorry for this late reply, I was looking at this earlier but forgot to respond but by looking at the code, it seems like it shall be fairly simple to use.
    Code:java
    1. String[] pages = {"Page 1", "Page 2"};
    2. Book("title", "author", pages);
    3. player.getInventory().addItem(generateItemStack());

    It's pretty much simple as this!
     
    JamesS237 likes this.
  3. Offline

    JamesS237

    I am having a bit of trouble:

    Im Getting A ClassNotFound Exception:

    Code:java
    1.  
    2. package me.jamess237.instuction.manuals;
    3.  
    4. import net.minecraft.server.NBTTagCompound;
    5. import net.minecraft.server.NBTTagList;
    6. import net.minecraft.server.NBTTagString;
    7.  
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.craftbukkit.inventory.CraftItemStack;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15.  
    16. public class Manuals
    17. {
    18. private String author;
    19. private String title;
    20. private String[] pages;
    21.  
    22. public void onDisable() {
    23. // TODO: Place any custom disable code here.
    24. System.out.println(this + " is now disabled!");
    25. }
    26.  
    27. public void onEnable() {
    28. // TODO: Place any custom enable code here, such as registering events
    29. System.out.println(this + " is now enabled!");
    30.  
    31. }
    32.  
    33. public void Book(ItemStack bookItem){
    34. NBTTagCompound bookData = ((CraftItemStack) bookItem).getHandle().tag;
    35.  
    36. this.author = bookData.getString("author");
    37. this.title = bookData.getString("title");
    38.  
    39. NBTTagList nPages = bookData.getList("pages");
    40.  
    41. String[] sPages = new String[nPages.size()];
    42. for(int i = 0;i<nPages.size();i++)
    43. {
    44. sPages[I] = nPages.get(i).toString();[/I]
    45. [I] }[/I]
    46.  
    47. [I] this.pages = sPages;[/I]
    48. [I] }[/I]
    49.  
    50. [I] void Book(String title, String author, String[] pages) {[/I]
    51. [I] this.title = title;[/I]
    52. [I] this.author = author;[/I]
    53. [I] this.pages = pages;[/I]
    54. [I] }[/I]
    55.  
    56. [I] public String getAuthor()[/I]
    57. [I] {[/I]
    58. [I] return author;[/I]
    59. [I] }[/I]
    60.  
    61. [I] public void setAuthor(String sAuthor)[/I]
    62. [I] {[/I]
    63. [I] author = sAuthor;[/I]
    64. [I] }[/I]
    65.  
    66. [I] public String getTitle()[/I]
    67. [I] {[/I]
    68. [I] return title;[/I]
    69. [I] }[/I]
    70.  
    71. [I] public String[] getPages()[/I]
    72. [I] {[/I]
    73. [I] return pages;[/I]
    74. [I] }[/I]
    75.  
    76. [I] public ItemStack generateItemStack(){[/I]
    77. [I] CraftItemStack newbook = new CraftItemStack(Material.WRITTEN_BOOK);[/I]
    78.  
    79. [I] NBTTagCompound newBookData = new NBTTagCompound();[/I]
    80.  
    81. [I] newBookData.setString("author",author);[/I]
    82. [I] newBookData.setString("title",title);[/I]
    83.  
    84. [I] NBTTagList nPages = new NBTTagList();[/I]
    85. [I] for(int i = 0;i<pages.length;i++)[/I]
    86. [I] { [/I]
    87. [I] nPages.add(new NBTTagString(pages[I],pages[I]));[/I][/I][/I]
    88. [I] }[/I]
    89.  
    90. [I] newBookData.set("pages", nPages);[/I]
    91.  
    92. [I] newbook.getHandle().tag = newBookData;[/I]
    93.  
    94. [I] return (ItemStack) newbook;[/I]
    95. [I] }[/I]
    96.  
    97.  
    98. [I]@EventHandler[/I]
    99. [I]public void OnPlayerJoin(PlayerJoinEvent event){[/I]
    100. [I]Player player = event.getPlayer();[/I]
    101.  
    102. [I]if (!(player.hasPlayedBefore())) {[/I]
    103. [I] String[] pages = {"Page 1", "Page 2"};[/I]
    104. [I] Book("title", "author", pages);[/I]
    105. [I] player.getInventory().addItem(generateItemStack());[/I]
    106. [I] player.sendMessage(ChatColor.YELLOW + "You Were Just Given An Instruction Manual!");[/I]
    107. [I] } [/I]
    108. [I]}[/I]
    109.  
    110.  
    111.  
    112. [I]}[/I]
    113. [I][/I]


    How Can I Fix This
     
  4. Offline

    travja

    Many people have been having problems with ClassNotFoundException... What seems to have worked is making a new project and pasting the code.
     
    ZeusAllMighty11 likes this.
  5. Offline

    JamesS237

    Tried It, I'm Just Getting This Error:

    2012-08-11 11:35:01 [SEVERE] Could not load 'plugins\Manuals.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ClassNotFoundException: me.jamess237.instructionmanuals
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:155)
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
    at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:222)
    at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:565)
    at org.bukkit.Bukkit.reload(Bukkit.java:183)
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:21)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
    at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:488)
    at net.minecraft.server.DedicatedServer.ah(DedicatedServer.java:248)
    at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
    at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:476)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.ClassNotFoundException: me.jamess237.instructionmanuals
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:41)
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:29)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:144)
    ... 14 more
     
  6. Offline

    zack6849

    thats a class not found exception try what travja said
     
  7. Offline

    JamesS237

    I Did, Didnt Work :(

    Fixed It. I Had An Incorrect plugin.yml. (facepalm)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  8. Offline

    tommykins20

    lol I noticed the incorrect "main:" thing but I just woke up.
     
Thread Status:
Not open for further replies.

Share This Page