Poisoned Arrows (Arrows and potions)

Discussion in 'Plugin Development' started by valon750, Jan 3, 2013.

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

    valon750

    Hello once again Bukkit dwellers, it is I, Valon, with another question regarding plugin development.

    I got what I think is a great idea, which could really enhance many servers, as well as my own. Issue is, I can't figure out some of the functions, as in, how to implement it in code.

    What I'd love to do is have it so that players could craft together an arrow and a potion, to make an arrow, which carries the effect of the arrow. Then when used, if it does so hit a player or enemy, the thing hit will receive the potion effect, as if it were a poison tipped dart.

    Now I've tried to think of various different methods of how I could achieve this, but I'm having no luck. I really am hoping there's a way I can possibly add an arrow and potion together in crafting, then retrieve an arrow which could have a different name, such as "Poisoned Arrow (effect). So I've spent today learning a bit about crafting, but really only learned the basics. I want to avoid using commands to give the arrow the effort, as it steers away from the basic survival skills style.

    Could someone enlighten me on how I would go about doing this? I can picture the process, that being crafting, then having the... EntityHitEvent? Then possibly if it could check the name of the entity, that being the arrow, for if it mentioned a certain effect, then apply the according effect to the hit player/mob/thing.

    A lot to ask I know, but I wouldn't ask if I didn't really need the help :/
     
  2. Offline

    fireblast709

    I would say save the Potions name and amplifier in the lore :3
    Code:java
    1. ItemStack poison1 = new ItemStack(Material.ARROW, 1);
    2. ArrayList<String> pes = new ArrayList<String>();
    3. pes.add("Poison I");
    4. ItemMeta im = poison1.getItemMeta();
    5. im.setLore(pes);
    6. poison1.setItemMeta(im);
    7. ShapelessRecipe srPoison1 = new ShapelessRecipe(poison1);
    8. srPoison1.addIngredient(amount, Material.POTION, data_of_poison_I);
    9. srPoison1.addIngredient(1, Material.ARROW);
    10. Bukkit.addRecipe(srPoison1);
    or something of the sorts
     
    Ne0nx3r0 likes this.
  3. Offline

    CubixCoders

    You can always check If they have a spider eye or something in their inventory when they shoot, if they do and it hits something, remove the spider eye and poison the target, that wouldn't involve crafting though
     
  4. Offline

    fireblast709

    Though crafting arrows (as you can have multiple ingredients in your inventory) is imo more awesome :3
     
    Muxon likes this.
  5. Offline

    valon750

    Huh, alright then, just going to come out and say that that went right over my head :D

    Yeah, I'd just love to be able to be hidden in the forest, on top of the trees, just preparing my bow and arrows to gather some food.


    It does make some sense luckily, mind you it's 2:30am.

    I'll give it a shot when I get up, I may need help when it comes to having the mob effected by the potion effect, but that bit shouldn't take as long.

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

    CubixCoders

    Its easy for mobs, same method for players ;P
     
  7. Offline

    mythcaptor

    Is there an easy way to stack multiple instances of the same item with custom metadata? I may be doing it wrong, but when I've tried similar things in the past, each arrow would consume it's own inventory space.
     
  8. Offline

    fireblast709

    That is because its NBT data is different, and thus not the same kind of item. Stacking these would mean you lose precious data for either one of the stacks
     
  9. Offline

    valon750


    Alrighty then, I am up and ready to code! Issue is, I have no idea where this code is meant to go :p

    The brain mechanism in my head is not ticking today fireblast!

    This stuff isn't making a bit of sense to me :S

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

    fireblast709

    To put it simply: the stacks are not the same, so you cannot stack them (not the same name and lore)
     
  11. Offline

    valon750

    Oh right that wasn't me that posted that bit :p

    But yeah, I've never gone into stuff like this before so I'm having a hard time setting up the code. I've added the arraylist, that's it :/

    Alright, I'm currently at:

    Code:
    public class PotionPoint extends JavaPlugin {
        private PlayerListener PlayerListener = new PlayerListener(this);
        public ArrayList<String> pes = new ArrayList<String>();
       
        public void onEnable(){
            System.out.println("Plugins Enabled");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.PlayerListener, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
            ShapelessRecipe srPoison1 = new ShapelessRecipe(poison1);
            srPoison1.addIngredient(Material.POTION, data_of_poison_I);
            srPoison1.addIngredient(Material.ARROW);
            Bukkit.addRecipe(srPoison1);
    }
        public void onDisable(){
            System.out.println("Plugin Disabled");
    }
    As you can tell I've not added all of the code yet, so I'm getting issues with "data_of_poison_I" and "ShapelessRecipe(poison1);

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

    fireblast709

    you need to create the ItemStack poison1 (which is the potion you want to apply) and you need to get the datavalue from that ItemStack as data_of_potion_I (which is just the datavalue of the potion you want to use in the recipe)
     
  13. Offline

    valon750

    Hmm, alright then, I'll give adding the rest of the code a shot, I wish I had kept the code from when I was testing the custom crafting :3 May add custom crafting to another plugin some time too... :3

    Alright then, after adding the code I'm at:

    Code:
    public class PotionPoint extends JavaPlugin {
        private PlayerListener PlayerListener = new PlayerListener(this);
        public ArrayList<String> pes = new ArrayList<String>();
       
        public void onEnable(){
            System.out.println("Plugins Enabled");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.PlayerListener, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
           
            ItemStack poison1 = new ItemStack(Material.ARROW, 1);
            ItemMeta im = poison1.getItemMeta();
            im.setLore(pes);
            pes.add("Poison I");
            poison1.setItemMeta(im);
            ShapelessRecipe srPoison1 = new ShapelessRecipe(poison1);
            srPoison1.addIngredient(Material.POTION, data_of_poison_I);
            srPoison1.addIngredient(Material.ARROW);
            Bukkit.addRecipe(srPoison1);
    }
        public void onDisable(){
            System.out.println("Plugin Disabled");
    }
    }
    Trouble is that I'm having issues with "data_of_poison_I" saying it cannot be resolved to a variable. Also, Is it right to be placing this in the main class rather than PlayerListener.java?

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

    fireblast709

    8196 is the datavalue for Poison I
     
  15. Offline

    valon750

    OH!

    I get what you mean now, swap data_of_poison_I for 8196, right, done that now:

    Code:
    public class PotionPoint extends JavaPlugin {
        private PlayerListener PlayerListener = new PlayerListener(this);
        public ArrayList<String> pes = new ArrayList<String>();
       
        public void onEnable(){
            System.out.println("Plugins Enabled");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.PlayerListener, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
           
            ItemStack poison1 = new ItemStack(Material.ARROW, 1);
            ItemMeta im = poison1.getItemMeta();
            im.setLore(pes);
            pes.add("Poison I");
            poison1.setItemMeta(im);
            ShapelessRecipe srPoison1 = new ShapelessRecipe(poison1);
            srPoison1.addIngredient(Material.POTION, 8196);
            srPoison1.addIngredient(Material.ARROW);
            Bukkit.addRecipe(srPoison1);
    }
        public void onDisable(){
            System.out.println("Plugin Disabled");
    }
    }
    Here I am now :3 I just wanted to test it out and see if it would do anything, then I got told that org.bukkit.metadata is never used, which is odd as there's the "setItemMeta" in the script, I assume it means that it's never used after it's set up, such as when a mob/player is hit, is it alright just to export and test it now? Or would you recommend I wait?

    Yeah.. not a good idea I'm getting errors in the console...

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

    fireblast709

    Code:java
    1. public class PotionPoint extends JavaPlugin
    2. {
    3. private PlayerListener playerListener;
    4.  
    5. public void onEnable()
    6. {
    7. this.playerListener = new PlayerListener(this);
    8. getLogger().info("Plugins Enabled");
    9. PluginManager pm = getServer().getPluginManager();
    10. pm.registerEvents(this.playerListener, this);
    11. getConfig().options().copyDefaults(true);
    12. saveConfig();
    13.  
    14. ItemStack poison_I = new ItemStack(Material.ARROW, 1);
    15. ItemMeta im = poison_I.getItemMeta();
    16. ArrayList<String> poison_I_lore = new ArrayList<String>();
    17. poison_I_lore.add("Poison I");
    18. im.setLore(poison_I_lore);
    19. poison_I.setItemMeta(im);
    20. ShapelessRecipe srPoison_I = new ShapelessRecipe(poison_I);
    21. srPoison_I.addIngredient(Material.POTION, 8196);
    22. srPoison_I.addIngredient(Material.ARROW);
    23. Bukkit.addRecipe(srPoison_I);
    24. }
    25.  
    26. public void onDisable()
    27. {
    28. getLogger().info("Plugin Disabled");
    29. }
    30. }
    valon750 changed the code a bit. Btw if you have errors, you should post the stacktrace (obviously after testing the new code).

    A remark from me: if your member variables use the class instance (ie. 'this', or a variable instantiated in the constructor.), instantiate it in the constructor as well. As I did in this code, I moved the PlayerListener init to the constructor.

    A second remark is to not name variables the same as the class (ie. 'PlayerListener PlayerListener'), as this could get confusing with static, and imo is just a bad habit. I renamed it to 'PlayerListener playerListener' so the names are actually different ;3
     
  17. Offline

    valon750

    Ahh! More stuff to put in my brain! Yeah, I picked up the habit of typing that from a tutorial series this person had on youtube :p

    Right, not sure what you mean by "stacktrace", I assume that's the things coming up in the console for the server.

    If so, it is..... this:

    Code:
    18:30:17 [SEVERE] Could not load 'plugins/PotionPoint.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ClassNotFoundException: me.valon.potionpoint.PotionPoint
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:184)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_4_5.CraftServer.loadPlugins(CraftServer.java:229)
        at org.bukkit.craftbukkit.v1_4_5.CraftServer.<init>(CraftServer.java:207)
        at net.minecraft.server.v1_4_5.ServerConfigurationManagerAbstract.<init>(ServerConfigurationManagerAbstract.java:52)
        at net.minecraft.server.v1_4_5.ServerConfigurationManager.<init>(SourceFile:11)
        at net.minecraft.server.v1_4_5.DedicatedServer.init(DedicatedServer.java:111)
        at net.minecraft.server.v1_4_5.MinecraftServer.run(MinecraftServer.java:398)
        at net.minecraft.server.v1_4_5.ThreadServerApplication.run(SourceFile:856)
    Caused by: java.lang.ClassNotFoundException: me.valon.potionpoint.PotionPoint
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader.java:80)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:53)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:173)
        ... 9 more
     
  18. Offline

    fireblast709

    'main' in plugin.yml is invalid
     
  19. Offline

    valon750

    Hmm, not sure of the issue... The main for it is:
    "me.valon.potionpoint.PotionPoint"

    That style seems to work fine for the other 3 plugins I've done :S
     
  20. Offline

    fireblast709

    this is your main in plugin.yml
    Code:
    me.valon.potionpoint.PotionPoint
    what is your package?
     
  21. Offline

    valon750

    me.valon.PotionPoint

    Damn it, it's got capitals.. I bet that'll be the issue
     
  22. Offline

    fireblast709

    yes... yes it is
     
  23. Offline

    valon750

    Awesome job there fireblast, the crafting works fine, it creates an arrow with the lore saying "Poison I" in purple text, which can be stacked with arrows of the same lore, which is perfect!

    So hopefully I should be able to incorporate all the different arrow types I'd like. So, I assume that's all that's needed on the item side? I now need to tackle when a mob/player gets hit?
     
  24. Offline

    fireblast709

    well it would still be needed to set the arrow entity to Poison I when fired, the applying would just be giving the potion effect on EntityDamageByEntity
     
  25. Offline

    valon750

    Hmm,

    I'm not too sure on how I go about doing that, setting the arrow entity to Poison I.

    I assume that when it comes to the EntityDamageByEntity bit, I'll need to do something like getItemMeta right? Not that I understand that bit either :S

    Is there something I need to do when a player shoots the arrow? You mention setting the arrow entity to Poison I when fired, how does one do that?

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

    fireblast709

    The arrow part is indeed the hard part. You would have to get the arrow shot (the ItemStack), read the lore, and either
    • register the UUID in a HashSet/HashMap for that potion effect
    • use the metadata system bukkit supplies (not to confuse with ItemMeta)
    The EntityDamageByEntityEvent depends on what you chose above
     
  27. Offline

    valon750


    Huh, alright then,

    Just going to come out and say it, which would be the easier to understand and go through with?


    Cause I mean I've heard of the term hashmap in the past, but never used it. But the thing that bukkit supplies seems like the better approach considering it's from bukkit.

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

    fireblast709

    just have several HashSets... probably easier for you
     
  29. Offline

    valon750

    Right, well I have no idea what they are, so this is going to be a task and a half :S
     
  30. Offline

    fireblast709

    its worth to learn about them
     
Thread Status:
Not open for further replies.

Share This Page