Solved -solved-

Discussion in 'Plugin Development' started by Quaro, Feb 11, 2014.

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

    Quaro

    -solved. thnx derpiee.
     
  2. Offline

    DragonzSlayer

    May I suggest using something like "<yourplugin>.Events.HeroSword" etc.
    And then do your onRightClick(), onLeftClick()

    Or <yourplugin>.Events.SpecialSwords, <yourplugin>.Events.SpecialPicks, etc.
    And have your custom Items in each class, sorted by the items type?

    Just some ideas! :)
     
  3. Offline

    Blah1

    You'll need some way to identify each sword. A lore or display name works best.
     
  4. Offline

    Quaro

    Code examples would be great.
     
  5. Offline

    Derpiee

  6. Offline

    Quaro

    Derpiee Thanks.

    I read it, and understood a bit. But would be much faster, if you could give me example like main class "Scroll" (item that you right click and it does something) , that i could use it to extend it. Would be great if i could get full example. (Main class and the sub(item) class).

    I made abstract main, and sub class, that has "onRightClick" method. How should i call method, from each class based on item.

    Should i create hashmap, and store all item instances, and key would be its displayName?

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

    HariHD

    Micius One thing I learnt from this forum is no code will be written.
     
  8. Offline

    Quaro

    HariHD I wrote it by my self, thnx to Derpiee, now i need suggestion, how to finish it.

    How should i call events (onRightClick) from each item class, based on the item. I had idea, to store all items instances to hashmap, and use key it's unique displayname. That i could point in event to the class.
     
  9. Offline

    Derpiee

    I wrote a quick example

    Main.java
    Code:java
    1.  
    2. package com.derpiee.swords;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin implements Listener{
    12.  
    13. public void onEnable() {
    14. //Register Events and call addSwords
    15. Bukkit.getPluginManager().registerEvents(this, this);
    16. addSwords();
    17. }
    18.  
    19. public void addSwords() {
    20. //Add the subclasses to the arraylist
    21. Sword.swords.add(new AmazingSword());
    22. }
    23.  
    24. @EventHandler
    25. public void onPlayerHitPlayer(EntityDamageByEntityEvent event) {
    26. //Check if the one that caused the damage is a player
    27. if(event.getDamager() instanceof Player) {
    28. //Grab the one that caused the damage
    29. Player p = (Player) event.getDamager();
    30. //Iterate through all the swords
    31. for(Sword sword : Sword.swords) {
    32. //Check to see if the item in hand is one of the sword types we have
    33. if(p.getItemInHand().getType().equals(sword.getSwordType())){
    34. //Checks to see if the item has item meta(Custom name)
    35. if(p.getItemInHand().hasItemMeta()){
    36. //Checks to see if the display name of the item is one that we have in our Swords
    37. if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(sword.getName())) {
    38. //Call the method onPlayerHit
    39. sword.onPlayerHit(event);
    40. }
    41. }
    42. }
    43. }
    44. }
    45.  
    46. }
    47. }
    48.  

    Sword.java
    Code:java
    1.  
    2. package com.derpiee.swords;
    3.  
    4. import java.util.ArrayList;
    5.  
    6. import org.bukkit.Material;
    7. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    8.  
    9. public abstract class Sword {
    10.  
    11. //Sword Name
    12. public String name;
    13.  
    14. //Sword Type (Diamond, Iron, Wood, etc..)
    15. public Material swordType;
    16.  
    17. //Array List containing all of the subclasses
    18. public static ArrayList<Sword> swords = new ArrayList<Sword>();
    19.  
    20. public Sword(String name, Material swordType) {
    21. this.name = name;
    22. this.swordType = swordType;
    23. }
    24.  
    25. //Abstract method for when hitting an entity
    26. public abstract void onPlayerHit(EntityDamageByEntityEvent event);
    27.  
    28. //Get the sword name
    29. public String getName() {
    30. return name;
    31. }
    32.  
    33. //Get the sword type
    34. public Material getSwordType() {
    35. return swordType;
    36. }
    37. }
    38.  

    AmazingSword.java
    Code:java
    1.  
    2. package com.derpiee.swords;
    3.  
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    7.  
    8. public class AmazingSword extends Sword {
    9.  
    10. public AmazingSword() {
    11. super("Amazing", Material.DIAMOND_SWORD);
    12. }
    13.  
    14. @Override
    15. public void onPlayerHit(EntityDamageByEntityEvent event) {
    16. //Get the damaged entity
    17. Entity entity = event.getEntity();
    18.  
    19. //Create an explosion (0F so the explosion does no damage to the world)
    20. entity.getWorld().createExplosion(entity.getLocation(), 0F);
    21. }
    22.  
    23. }
    24.  
     
Thread Status:
Not open for further replies.

Share This Page