Book Meta

Discussion in 'Plugin Development' started by dmulloy2, Feb 26, 2013.

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

    dmulloy2

    Hello, I am trying to have my plugin (SwornRPG) create a book with a player's coordinates on death. Below is the code i am trying to use.
    Code:
        @EventHandler(priority = EventPriority.MONITOR)
        public void onEntityDeath(EntityDeathEvent event)
        {
            Entity ent = event.getEntity();
            if(ent instanceof Player)
            {
                Player player = (Player)event.getEntity();
                double x = (int) Math.floor(player.getLocation().getX());
                double y = (int) Math.floor(player.getLocation().getY());
                double z = (int) Math.floor(player.getLocation().getZ());
                ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
                BookMeta meta = (BookMeta)book.getItemMeta();
                List<String> pages = new ArrayList<String>();
                pages.add(1, null);
                meta.setTitle("DeathCoords");
                meta.setAuthor("SwornRPG");
                meta.setPages(pages);
                meta.setPage(1, "You died at " + x + " " + y + " " + z);
                book.setItemMeta(meta);
                player.getInventory().addItem(book);
            }
        }
    
    This does not throw any errors in eclipse, but it does on my test server:
    Code:
    2013-02-26 17:22:55 [SEVERE] Could not pass event PlayerDeathEvent to SwornRPG v0.9.7
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:479)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:464)
        at org.bukkit.craftbukkit.v1_4_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:332)
        at net.minecraft.server.v1_4_R1.EntityPlayer.die(EntityPlayer.java:258)
        at net.minecraft.server.v1_4_R1.EntityLiving.damageEntity(EntityLiving.java:771)
        at net.minecraft.server.v1_4_R1.EntityHuman.damageEntity(EntityHuman.java:618)
        at net.minecraft.server.v1_4_R1.EntityPlayer.damageEntity(EntityPlayer.java:311)
        at org.bukkit.craftbukkit.v1_4_R1.entity.CraftLivingEntity.damage(CraftLivingEntity.java:190)
        at org.bukkit.craftbukkit.v1_4_R1.entity.CraftLivingEntity.damage(CraftLivingEntity.java:175)
        at com.earth2me.essentials.PlayerExtension.damage(PlayerExtension.java)
        at com.earth2me.essentials.commands.Commandsuicide.run(Commandsuicide.java:21)
        at com.earth2me.essentials.commands.EssentialsCommand.run(EssentialsCommand.java:96)
        at com.earth2me.essentials.Essentials.onCommandEssentials(Essentials.java:407)
        at com.earth2me.essentials.Essentials.onCommand(Essentials.java:313)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
        at org.bukkit.craftbukkit.v1_4_R1.CraftServer.dispatchCommand(CraftServer.java:536)
        at net.minecraft.server.v1_4_R1.PlayerConnection.handleCommand(PlayerConnection.java:995)
        at net.minecraft.server.v1_4_R1.PlayerConnection.chat(PlayerConnection.java:911)
        at net.minecraft.server.v1_4_R1.PlayerConnection.a(PlayerConnection.java:855)
        at net.minecraft.server.v1_4_R1.Packet3Chat.handle(Packet3Chat.java:44)
        at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:174)
        at net.minecraft.server.v1_4_R1.PlayerConnection.d(PlayerConnection.java:115)
        at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
        at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:66)
        at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:590)
        at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:228)
        at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:486)
        at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:420)
        at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
        at java.util.ArrayList.rangeCheckForAdd(Unknown Source)
        at java.util.ArrayList.add(Unknown Source)
        at net.dmulloy2.swornrpg.listeners.PlayerListener.onEntityDeath(PlayerListener.java:366)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 31 more
    What might I be doing wrong?
     
  2. Offline

    Milkywayz

    My first question would be why your testing your bukkit plugin on a spigot server?

    Also it's a problem with how your handling the pages. I'll edit this post and tell you how to fix once I go find how I did pages in one of my other plugins.

    EDIT: [How I handled books in my minewriter plugin]
    Code:
    package net.minewriter;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    import org.json.JSONObject;
     
    public class Book extends ItemStack {
     
        private String title;
        private String url;
        private String author;
        private int id;
        private List<String> pages = new ArrayList<String>();
        private List<String> lore = new ArrayList<String>();
        private BookMeta meta;
        private JSONObject json;
     
        public Book(String urlpath) throws Exception {
            super(Material.WRITTEN_BOOK);
            url = urlpath;
            json = JsonReader.read(url);
            title = json.getString("Title");
            author = json.getString("Author");
            id = json.getInt("ID");
            meta = (BookMeta) getItemMeta();
            for (String s : delimit(json.getString("Content"), 256)) {
                pages.add(ChatColor.translateAlternateColorCodes('$', s));
            }
            meta.setAuthor(author);
            meta.setTitle(title);
            meta.setPages(pages);
            meta.setLore(lore);
            setItemMeta(meta);
        }
     
    //Breaks up a long string into pages
        public String[] delimit(String str, int chunk) {
            int size = (int) Math.ceil((double) str.length() / chunk);
            String[] arr = new String[size];
            int dex = 0;
            for (int i = 0; i < str.length(); i = i + chunk) {
                if (str.length() - i < chunk) {
                    arr[dex++] = str.substring(i);
                } else {
                    arr[dex++] = str.substring(i, i + chunk);
                }
            }
            return arr;
        }
     
        public String getTitle() {
            return title;
        }
     
        public String getAuthor() {
            return author;
        }
     
        public int getID() {
            return id;
        }
     
        public boolean isBanned(MineReader plugin) {
            return plugin.getConf().isBanned(this);
        }
    }
    Obviously don't copy and paste, just implement what you need. Credit in your plugin would be nice if you do end up implementing this, if you don't want to give credit thats fine too.
     
  3. Offline

    mastermustard

    Code:
        at net.dmulloy2.swornrpg.listeners.PlayerListener.onEntityDeath(PlayerListener.java:366)
      
    whats that line
     
  4. Offline

    dmulloy2

    pages.add(1, null);
    Which, truthfully, is where I'm going wrong

    How are you adding text to the pages?
    And I personally use spigot for my server, as well as the server i develop for, but all the plugins are cross compatible (not that spigot makes any plugins)

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

    Milkywayz

    Setting the BookMeta pages to the delimited string, and then called setItemMeta() from the super class.
     
  6. Offline

    dmulloy2

    Code:
      /**
        * Books on Player Deaths
        * Creds to Milkywayz (BukkitDev Staff) for helping me on this :3
        */
        @EventHandler(priority = EventPriority.MONITOR)
        public void onEntityDeath(EntityDeathEvent event)
        {
            Entity ent = event.getEntity();
            if(ent instanceof Player)
            {
                Player player = (Player)event.getEntity();
                double x = (int) Math.floor(player.getLocation().getX());
                double y = (int) Math.floor(player.getLocation().getY());
                double z = (int) Math.floor(player.getLocation().getZ());
                ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
                BookMeta meta = (BookMeta)book.getItemMeta();
                pages.add("You died at " + x + " " + y + " " + z);
                meta.setTitle("DeathCoords");
                meta.setAuthor("SwornRPG");
                meta.setPages(pages);
                book.setItemMeta(meta);
                player.getInventory().addItem(book);
            }
        }
    
    This doesn't throw any errors when a player dies, but it also doesnt give them the book
     
  7. Offline

    Technius

    First, make sure the event is registered. Two, it might be better to give it to them when they respawn(add their names to a list when they die and remove the name when they respawn).
     
  8. Offline

    dmulloy2

    How would I give the book to them if it is in a different event?

    Well I got it to work. Just gave it a second delay and it worked like a charm (no matter how long you were dead for)
    Here's the code that did it all
    Code:
        /**
        * Books on Player Deaths
        * Creds to Milkywayz (BukkitDev Staff) for helping me on this :3
        */
        @EventHandler(priority = EventPriority.MONITOR)
        public void onEntityDeath(EntityDeathEvent event)
        {
            Entity ent = event.getEntity();
            if(ent instanceof Player)
            {
                final Player player = (Player)event.getEntity();
                String coordsplayer = player.getName();
                double x = (int) Math.floor(player.getLocation().getX());
                double y = (int) Math.floor(player.getLocation().getY());
                double z = (int) Math.floor(player.getLocation().getZ());
                final ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
                BookMeta meta = (BookMeta)book.getItemMeta();
                pages.add("You died at " + x + " " + y + " " + z);
                meta.setTitle("DeathCoords");
                meta.setAuthor("SwornRPG");
                meta.setPages(pages);
                book.setItemMeta(meta);
                coordslist.add(coordsplayer);
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
                {
                    @Override
                    public void run()
                    {
                        InventoryWorkaround.addItems(player.getInventory(), book);
                    }               
                },20);
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  9. Offline

    Technius

    It would be like this:
    Code:
    ArrayList<String> give = new ArrayList<String>();
     
    @EventHandler
    public void death(EntityDeathEvent event)
    {
        if(event.getEntity() instanceof Player)
        {
            Player player = (Player)event.getEntity();
            //Your code here
            give.add(player.getName());
        }
    }
     
    @EventHandler
    public void respawn(PlayerRespawnEvent event)
    {
        if(give.contains(event.getPlayer().getName())
        {
            //Your code here
            give.remove(event.getPlayer().getName());
        }
    }
    
     
  10. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    This forum is for Bukkit plugin development, not forks of Bukkit.
     
Thread Status:
Not open for further replies.

Share This Page