Book Meta

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

Thread Status:
Not open for further replies.
  1. 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 (Text):
    1.     @EventHandler(priority = EventPriority.MONITOR)
    2.     public void onEntityDeath(EntityDeathEvent event)
    3.     {
    4.         Entity ent = event.getEntity();
    5.         if(ent instanceof Player)
    6.         {
    7.             Player player = (Player)event.getEntity();
    8.             double x = (int) Math.floor(player.getLocation().getX());
    9.             double y = (int) Math.floor(player.getLocation().getY());
    10.             double z = (int) Math.floor(player.getLocation().getZ());
    11.             ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
    12.             BookMeta meta = (BookMeta)book.getItemMeta();
    13.             List<String> pages = new ArrayList<String>();
    14.             pages.add(1, null);
    15.             meta.setTitle("DeathCoords");
    16.             meta.setAuthor("SwornRPG");
    17.             meta.setPages(pages);
    18.             meta.setPage(1, "You died at " + x + " " + y + " " + z);
    19.             book.setItemMeta(meta);
    20.             player.getInventory().addItem(book);
    21.         }
    22.     }
    23.  
    This does not throw any errors in eclipse, but it does on my test server:
    Code (Text):
    1. 2013-02-26 17:22:55 [SEVERE] Could not pass event PlayerDeathEvent to SwornRPG v0.9.7
    2. org.bukkit.event.EventException
    3.     at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    4.     at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    5.     at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:479)
    6.     at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:464)
    7.     at org.bukkit.craftbukkit.v1_4_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:332)
    8.     at net.minecraft.server.v1_4_R1.EntityPlayer.die(EntityPlayer.java:258)
    9.     at net.minecraft.server.v1_4_R1.EntityLiving.damageEntity(EntityLiving.java:771)
    10.     at net.minecraft.server.v1_4_R1.EntityHuman.damageEntity(EntityHuman.java:618)
    11.     at net.minecraft.server.v1_4_R1.EntityPlayer.damageEntity(EntityPlayer.java:311)
    12.     at org.bukkit.craftbukkit.v1_4_R1.entity.CraftLivingEntity.damage(CraftLivingEntity.java:190)
    13.     at org.bukkit.craftbukkit.v1_4_R1.entity.CraftLivingEntity.damage(CraftLivingEntity.java:175)
    14.     at com.earth2me.essentials.PlayerExtension.damage(PlayerExtension.java)
    15.     at com.earth2me.essentials.commands.Commandsuicide.run(Commandsuicide.java:21)
    16.     at com.earth2me.essentials.commands.EssentialsCommand.run(EssentialsCommand.java:96)
    17.     at com.earth2me.essentials.Essentials.onCommandEssentials(Essentials.java:407)
    18.     at com.earth2me.essentials.Essentials.onCommand(Essentials.java:313)
    19.     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    20.     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    21.     at org.bukkit.craftbukkit.v1_4_R1.CraftServer.dispatchCommand(CraftServer.java:536)
    22.     at net.minecraft.server.v1_4_R1.PlayerConnection.handleCommand(PlayerConnection.java:995)
    23.     at net.minecraft.server.v1_4_R1.PlayerConnection.chat(PlayerConnection.java:911)
    24.     at net.minecraft.server.v1_4_R1.PlayerConnection.a(PlayerConnection.java:855)
    25.     at net.minecraft.server.v1_4_R1.Packet3Chat.handle(Packet3Chat.java:44)
    26.     at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:174)
    27.     at net.minecraft.server.v1_4_R1.PlayerConnection.d(PlayerConnection.java:115)
    28.     at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
    29.     at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:66)
    30.     at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:590)
    31.     at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:228)
    32.     at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:486)
    33.     at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:420)
    34.     at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    35. Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    36.     at java.util.ArrayList.rangeCheckForAdd(Unknown Source)
    37.     at java.util.ArrayList.add(Unknown Source)
    38.     at net.dmulloy2.swornrpg.listeners.PlayerListener.onEntityDeath(PlayerListener.java:366)
    39.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    40.     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    41.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    42.     at java.lang.reflect.Method.invoke(Unknown Source)
    43.     at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    44.     ... 31 more
    What might I be doing wrong?
     
  2. 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 (Text):
    1. package net.minewriter;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.inventory.meta.BookMeta;
    10. import org.json.JSONObject;
    11.  
    12. public class Book extends ItemStack {
    13.  
    14.     private String title;
    15.     private String url;
    16.     private String author;
    17.     private int id;
    18.     private List<String> pages = new ArrayList<String>();
    19.     private List<String> lore = new ArrayList<String>();
    20.     private BookMeta meta;
    21.     private JSONObject json;
    22.  
    23.     public Book(String urlpath) throws Exception {
    24.         super(Material.WRITTEN_BOOK);
    25.         url = urlpath;
    26.         json = JsonReader.read(url);
    27.         title = json.getString("Title");
    28.         author = json.getString("Author");
    29.         id = json.getInt("ID");
    30.         meta = (BookMeta) getItemMeta();
    31.         for (String s : delimit(json.getString("Content"), 256)) {
    32.             pages.add(ChatColor.translateAlternateColorCodes('$', s));
    33.         }
    34.         meta.setAuthor(author);
    35.         meta.setTitle(title);
    36.         meta.setPages(pages);
    37.         meta.setLore(lore);
    38.         setItemMeta(meta);
    39.     }
    40.  
    41. //Breaks up a long string into pages
    42.     public String[] delimit(String str, int chunk) {
    43.         int size = (int) Math.ceil((double) str.length() / chunk);
    44.         String[] arr = new String[size];
    45.         int dex = 0;
    46.         for (int i = 0; i < str.length(); i = i + chunk) {
    47.             if (str.length() - i < chunk) {
    48.                 arr[dex++] = str.substring(i);
    49.             } else {
    50.                 arr[dex++] = str.substring(i, i + chunk);
    51.             }
    52.         }
    53.         return arr;
    54.     }
    55.  
    56.     public String getTitle() {
    57.         return title;
    58.     }
    59.  
    60.     public String getAuthor() {
    61.         return author;
    62.     }
    63.  
    64.     public int getID() {
    65.         return id;
    66.     }
    67.  
    68.     public boolean isBanned(MineReader plugin) {
    69.         return plugin.getConf().isBanned(this);
    70.     }
    71. }
    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. Code (Text):
    1.  
    2.     at net.dmulloy2.swornrpg.listeners.PlayerListener.onEntityDeath(PlayerListener.java:366)
    3.  
    whats that line
     
  4. pages.add(1, null);
    Which, truthfully, is where I'm going wrong
     
  5. 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)
     
  6. Setting the BookMeta pages to the delimited string, and then called setItemMeta() from the super class.
     
  7. Code (Text):
    1.   /**
    2.     * Books on Player Deaths
    3.     * Creds to Milkywayz (BukkitDev Staff) for helping me on this :3
    4.     */
    5.     @EventHandler(priority = EventPriority.MONITOR)
    6.     public void onEntityDeath(EntityDeathEvent event)
    7.     {
    8.         Entity ent = event.getEntity();
    9.         if(ent instanceof Player)
    10.         {
    11.             Player player = (Player)event.getEntity();
    12.             double x = (int) Math.floor(player.getLocation().getX());
    13.             double y = (int) Math.floor(player.getLocation().getY());
    14.             double z = (int) Math.floor(player.getLocation().getZ());
    15.             ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
    16.             BookMeta meta = (BookMeta)book.getItemMeta();
    17.             pages.add("You died at " + x + " " + y + " " + z);
    18.             meta.setTitle("DeathCoords");
    19.             meta.setAuthor("SwornRPG");
    20.             meta.setPages(pages);
    21.             book.setItemMeta(meta);
    22.             player.getInventory().addItem(book);
    23.         }
    24.     }
    25.  
    This doesn't throw any errors when a player dies, but it also doesnt give them the book
     
  8. 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).
     
  9. How would I give the book to them if it is in a different event?
     
  10. 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 (Text):
    1.     /**
    2.     * Books on Player Deaths
    3.     * Creds to Milkywayz (BukkitDev Staff) for helping me on this :3
    4.     */
    5.     @EventHandler(priority = EventPriority.MONITOR)
    6.     public void onEntityDeath(EntityDeathEvent event)
    7.     {
    8.         Entity ent = event.getEntity();
    9.         if(ent instanceof Player)
    10.         {
    11.             final Player player = (Player)event.getEntity();
    12.             String coordsplayer = player.getName();
    13.             double x = (int) Math.floor(player.getLocation().getX());
    14.             double y = (int) Math.floor(player.getLocation().getY());
    15.             double z = (int) Math.floor(player.getLocation().getZ());
    16.             final ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
    17.             BookMeta meta = (BookMeta)book.getItemMeta();
    18.             pages.add("You died at " + x + " " + y + " " + z);
    19.             meta.setTitle("DeathCoords");
    20.             meta.setAuthor("SwornRPG");
    21.             meta.setPages(pages);
    22.             book.setItemMeta(meta);
    23.             coordslist.add(coordsplayer);
    24.             plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    25.             {
    26.                 @Override
    27.                 public void run()
    28.                 {
    29.                     InventoryWorkaround.addItems(player.getInventory(), book);
    30.                 }              
    31.             },20);
    32.         }
    33.     }
     
  11. It would be like this:
    Code (Text):
    1.  
    2. ArrayList<String> give = new ArrayList<String>();
    3.  
    4. @EventHandler
    5. public void death(EntityDeathEvent event)
    6. {
    7.     if(event.getEntity() instanceof Player)
    8.     {
    9.         Player player = (Player)event.getEntity();
    10.         //Your code here
    11.         give.add(player.getName());
    12.     }
    13. }
    14.  
    15. @EventHandler
    16. public void respawn(PlayerRespawnEvent event)
    17. {
    18.     if(give.contains(event.getPlayer().getName())
    19.     {
    20.         //Your code here
    21.         give.remove(event.getPlayer().getName());
    22.     }
    23. }
    24.  
     
  12. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı Retired Staff

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

Share This Page