Setting amount of times a certain thing has to be done.

Discussion in 'Plugin Development' started by HaCkTiC_HaWkZ, Dec 21, 2011.

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

    HaCkTiC_HaWkZ

    Hi,

    I am wandering if there is a way to set an amount of times a certain thing has to be done.
    for example

    the player needs to destroy 20 blocks of clay
    but once the player reaches said 20 blocks the next amount needed is 40? or this event only happens once.

    or as another example,
    the player needs to kill 50 cavespiders, this too only needs to happen once or to be set to double the amount once reached.....

    I'am kind of a noob when it comes to special functions and things to do with entities...... so any help will be appreciated :D

    Thanks,
    Lewis
     
  2. You just create a HashMap (Stores a value with a key, so you can store the number of times something has happened). For example, here's the cavespider thingy:
    Create the hashmap:
    Map<String, Integer> cavespiderKills = new HashMap<String, Integer>();
    Then, in onEntityDeathEvent
    Code:java
    1.  
    2. public void onEntityDeath(EntityDeathEvent event) {
    3. Entity dead = event.getEntity();
    4. if (!(dead instanceof CaveSpider)) {
    5. return;
    6. }
    7. if (dead.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
    8. EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) dead.getLastDamageCause();
    9. Entity killer = e.getDamager();
    10. if (killer instanceof Player) {
    11. Player PKiller = (Player) killer;
    12. if (!(cavespiderKills.containsKey(PKiller.getName())) {
    13. cavespiderKills.put(PKiller.getName(), 1);
    14. } else {
    15. cavespiderKills.put(PKiller.getName(), cavespiderKills.get(PKiller.getName()) + 1);
    16. }
    17. if (cavespiderKills.get(PKiller.getName()) == 50) {
    18. // Do whatever
    19. }
    20. }
    21. }
    22.  

    This code might have errors, but hopefully you understand :)
    Couple of things to note:
    • Why use the players name instead of the player object? Because the player object is updated. For example, what happens when the player logs off? Or when the player respawns?
    • Depending on how much persistence you need, you could also use the player's entity ID instead of its name, which in turn is faster as checking primitives is faster than checking objects
     
    HaCkTiC_HaWkZ likes this.
  3. Offline

    HaCkTiC_HaWkZ


    Waking up and seeing this you have made my day :D
    Thank you so much for the quick reply,

    I also do understand what you mean :D thank you you have helped me out a lot I shall get started working on this, I seen something on the wiki for plugin dev on hash maps it will also help me understand how to make them and use them just like the broadcasting system i got that uses the external file :)

    I guess i would have to do this code layout for all the quests i have? :)

    Thank you a lot again really appreciated

    public Map<Key, DataType> HashMapName = new HashMap<Key, Datatype>();

    public void onEntityDeath(EntityDeathEvent event) {
    Entity dead = event.getEntity();
    if (!(dead instanceof CaveSpider)) {
    return;
    }
    if (dead.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
    EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) dead.getLastDamageCause();
    Entity killer = e.getDamager();
    if (killer instanceof Player) {
    Player PKiller = (Player) killer;
    if (!(cavespiderKills.containsKey(PKiller.getName())) { < --- get an error here for some reason......
    cavespiderKills.put(PKiller.getName(), 1);
    } else {
    cavespiderKills.put(PKiller.getName(), cavespiderKills.get(PKiller.getName()) + 1);
    }
    if (cavespiderKills.get(PKiller.getName()) == 50) {
    public Map<Player, Boolean> CaveSpiderQuest = new HashMap<Player, Boolean>();

    Is what i have so far and for my CaveSpiderQuest name too
    I am also getting it for the Kills (cavespiderKills) but im sure once i make the hash map

    public Map<String, Integer> CaveSpiderQuest = new HashMap<String, Integer>(); cavespiderKills("kills", 10); int datavalue = 0; if (CaveSpiderQuest.containsKey(argument) 10 i am guessing datavalue = cavespiderKills.get(argument); else { try { datavalue = Integer.parseInt(argument); } catch (Exception e) { ; } }


    Correct me if im wrong please, also sorry but tis because i am mearly a n00b :L and as i said entities are my weak side :'(
    and so are hashmaps by what i can tell so far :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  4. @tips48: We have a getKiller() now, so here's your code updated with it:
    Code:java
    1.  
    2. public void onEntityDeath(EntityDeathEvent event) {
    3. Entity dead = event.getEntity();
    4. if (!(dead instanceof CaveSpider)) {
    5. return;
    6. }
    7. Player killer = ((LivingEntity)dead).getKiller()
    8. if(killer == null)
    9. return;
    10. if (!(cavespiderKills.containsKey(killer.getName())) {
    11. cavespiderKills.put(killer.getName(), 1);
    12. } else {
    13. cavespiderKills.put(killer.getName(), cavespiderKills.get(killer.getName()) + 1);
    14. }
    15. if (cavespiderKills.get(killer.getName()) == 50) {
    16. // Do whatever
    17. }
    18. }
    19. }
    20.  

    :)
     
    HaCkTiC_HaWkZ likes this.
  5. Offline

    HaCkTiC_HaWkZ

    I tried the code but get killer gets highlighted when i import livingentity......
    :'( hash maps are too complex i think i might have to read that code and the code on bukkit development alot over and over again..... where do i put the code, because i put it in my player listener

    do i need to make a new class for it? also what formate file would i need to save said config code to? YAML? or just a normal text file like the broadcasting system

    Thanks for the reply it was appreciated :)
     
  6. You're using eclipse, right? What does it tell you if you move the mouse cursor on getKiller() ? If it tells you there is no such method: Update you're bukkit.jar
    You put it in your entity listener.
    You can use yaml, but a flat file would probably be better/faster.
     
  7. Offline

    HaCkTiC_HaWkZ

    Yes sir, Eclipse is what i use it tells me, The method getKiller() is undefined for the type LivingEntity
    and i have the latest 1.0.1 R2 build :)
    i also dont have a entity listner so i shall make a new one

    ill do this


    pm.registerEvent(Event.Type.PLAYER_INTERACT, this.entityListener, Event.Priority.Normal, this);

    and put it on in enable, and

    public final SystemNPCEntityListener entityrListener = new SystemNPCEntityListener(this);

    im thinking i might have to change this to something else.....

    //----

    so everything goes into the entity listener
    and the config file which is used

    Data Value Lookups

    public Map<String, Integer> wool_colors = new HashMap<String, Integer>(); // Run this on plugin startup (ideally reading from a file instead of copied out row by row): wool_colors("orange", 1); wool_colors("magenta", 2); wool_colors("light blue", 3); ... wool_colors("black", 15); // Run this in response to user commands - turn "green" into 13 int datavalue = 0; if (wool_colors.containsKey(argument) datavalue = wool_colors.get(argument); else { try { datavalue = Integer.parseInt(argument); } catch (Exception e) { ; } }
    for example

    is put inside it? and than the entity listener opens this config file and looks at the numbers ?
    ....im getting somewhere now, i think i might need a strong Tea or Coffee lol i went to bed at 8 AM and now its 12:36 so half brain dead :D

    I appreciate this help so much :)

    package me.hacktichawkz.systemnpc;

    import java.util.HashMap;
    import java.util.Map;

    import org.bukkit.entity.CaveSpider;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.EntityListener;

    public class SystemNPCEntityListener extends EntityListener {
    public static SystemNPC plugin;

    public SystemNPCEntityListener(SystemNPC instance) {
    }

    public Map<Key, DataType> CaveSpiderQuest = new HashMap<Key, Datatype>();


    public void onEntityDeath(EntityDeathEvent event) {
    Entity dead = event.getEntity();
    if (!(dead instanceof CaveSpider)) {
    return;
    }
    Player killer = ((LivingEntity) dead).getKiller();
    if(killer == null)
    return;
    if (!(cavespiderKills.containsKey(killer.getName())) {
    cavespiderKills.put(killer.getName(), 1);
    } else {
    cavespiderKills.put(killer.getName(), cavespiderKills.get(killer.getName()) + 1);
    }
    if (cavespiderKills.get(killer.getName()) == 50) {
    // Do whatever
    }
    }
    }

    it highlights the DataType, it gives me an option to put it as Data, DataLine, DataSource...... it goes on

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  8. Your key object is a String, your value (DataType) object is an Integer, so it has to be:
    Code:java
    1. public Map<String, Integer> CaveSpiderQuest = new HashMap<String, Integer>();
    That's strange. The java docs tell it's there. I'll have a look into it.

    BTW: If you just need to store the HashMap you can serialize it and write it to disk. That is fast but the file won't be human readable:
    Code:java
    1. try
    2. {
    3. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("path/to/your.file"));
    4. out.writeObject(CaveSpiderQuest);
    5. out.flush();
    6. out.close();
    7. }
    8. catch(Exception e)
    9. {
    10. getServer().getLogger().info("["+getDescription().getName()+"] can't write savefile!");
    11. e.printStackTrace();
    12. }

    and to read it:
    Code:java
    1. try
    2. {
    3. File f = new File("path/to/your.file");
    4. if(!f.exists())
    5. {
    6. (new File("path/to")).mkdir();
    7. f.createNewFile();
    8. }
    9. else
    10. {
    11. Object o = in.readObject();
    12. in.close();
    13. if(o == null || !(o instanceof HashMap<String, Integer>))
    14. {
    15. log.info("["+pdf.getName()+"] ERROR: can't read savefile!");
    16. getServer.getPluginManager().disablePlugin(this);
    17. return;
    18. }
    19. CaveSpiderQuest = (HashMap<String, Integer>)o;
    20. }
    21. }
    22. catch(Exception e)
    23. {
    24. log.info("["+pdf.getName()+"] can't read savefile!");
    25. e.printStackTrace();
    26. s.getPluginManager().disablePlugin(this);
    27. return;
    28. }


    //EDIT: Checked getKiller() with the latest RB of bukkit (there is no R2...) - works fine:
    [​IMG]

    //EDIT²: Checked with the newest snapshot, too (there is a R2 :D) - works fine, too...
     
  9. Offline

    HaCkTiC_HaWkZ

    package me.hacktichawkz.systemnpc;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.i:confused:bjectInputStream;
    import java.i:confused:bjectOutputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Logger;

    import javax.activation.DataSource;
    import javax.xml.crypto.Data;

    import org.bukkit.entity.CaveSpider;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.EntityListener;

    import com.sun.corba.se.impl.oa.poa.ActiveObjectMap.Key;



    public class SystemNPCEntityListener extends EntityListener {
    public static SystemNPC plugin;

    public SystemNPCEntityListener(SystemNPC instance) {
    }

    public Map<String, Integer> CaveSpiderQuest = new HashMap<String, Integer>();

    public void onEntityDeath(EntityDeathEvent event) {
    Entity dead = event.getEntity();
    Player PKiller = (Player) PKiller;
    if (!(dead instanceof CaveSpider)) {
    return;
    }
    Player killer = ((LivingEntity)dead).getName();
    if(killer == null)
    return;
    if (!(cavespiderKills.containsKey(PKiller.getName())) {
    cavespiderKills.put(PKiller.getName(), 1);
    } else {
    cavespiderKills.put(killer.getName(), cavespiderKills.get(killer.getName()) + 1);
    }
    if (cavespiderKills.get(killer.getName()) == 50) {
    // Do whatever
    }
    try
    {
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("path/to/your.file"));
    out.writeObject(CaveSpiderQuest);
    out.flush();
    out.close();
    }
    catch(Exception e)
    {
    getServer().getLogger().info("["+getDescription().getName()+"] can't write savefile!");
    e.printStackTrace();
    }
    }
    try
    {
    File f = new File("path/to/your.file");
    if(!f.exists())
    {
    (new File("path/to")).mkdir();
    f.createNewFile();
    }
    else
    {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
    Object o = in.readObject();
    in.close();
    if(o == null || !(o instanceof HashMap<String, Integer>))
    {
    Logger.info("["+pdfile.getName()+"] ERROR: can't read savefile!");
    getServer.getPluginManager().disablePlugin(this);
    return;
    }
    CaveSpiderQuest = (HashMap<String, Integer>)o;
    }
    }
    catch(Exception e)
    {
    Logger.info("["+pdfile.getName()+"] can't read savefile!");
    e.printStackTrace();
    s.getPluginManager().disablePlugin(this);
    return;
    }
    }

    is my code now :D thank you still got a few errors but i will try fixing it :)

    >_> im still hash map n00b but hey we all learn in the end :D thank you for all your help matey :)
     
  10. Fine. :)

    Just two things: You probably shoudln't save every time a player kills an entity and/or put the
    Code:
    out.flush();
    out.close();
    into an async task to prevent lag.

    Also you don't have to read the file after you saved it. You have to read it only once: In onEnable() ;)
     
    HaCkTiC_HaWkZ likes this.
  11. Offline

    HaCkTiC_HaWkZ


    Thanks :) and i have changed it to enable :D thank you for all your help :')
     
  12. Ahh, thanks...I remember seeing it added but didn't remember what it was called..Thanks :)
    @HaCkTiC_HaWkZ Glad I made your day :p
     
  13. Offline

    HaCkTiC_HaWkZ

    xD i am still trying to figure it out, but i shall probably do it tomorrow :) enough trying to get it workin for one day :D :)

    thank you for all your help @tips48 and @V10lator
     
  14. Offline

    HaCkTiC_HaWkZ

    Hey i am back, thank you for all the help i managed to do the entitylistener, so when the player kills 50 spiders he is rewarded a prize and given a message saying he completed a quest..... but i cant complie it due to an error with the get description :')

    Cannot make a static reference to the non-static method getDescription() from the type
    JavaPlugin

    i already have in my main class which i put the coding in

    Heres the coding from the error up


    Code:
    package me.hacktichawkz.systemnpc;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Logger;
    import org.bukkit.inventory.ItemStack;
    
    import me.hacktichawkz.systemnpc.SystemNPCPlayerListener;
    import me.hacktichawkz.systemnpc.SystemNPCEntityListener;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginDescriptionFile;
    
    public class SystemNPC extends JavaPlugin
    {
     
        public static SystemNPC plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        public final static Logger log = Logger.getLogger("CaveSpiderQuest");
        public final SystemNPCPlayerListener playerListener = new SystemNPCPlayerListener(this);
        public final SystemNPCEntityListener entityListener = new SystemNPCEntityListener(this);
    
        public static int currentline = 0;
        public static int tid =0;
        public static int running = 1;
        public static long interval = 300;
    
        @Override
        public void onDisable(){
           PluginDescriptionFile pdFile = this.getDescription();
           this.logger.info(pdFile.getName() + " Is now disabled.");
    
        }
     
        @Override
        public void onEnable(){
           PluginDescriptionFile pdFile = this.getDescription();
           PluginManager pm = getServer().getPluginManager();
           pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Normal, this);
           pm.registerEvent(Event.Type.PLAYER_INTERACT, this.entityListener, Event.Priority.Normal, this);
           this.logger.info( "[" + pdFile.getName() + "]" + " version by HaCkTiC_HaWkZ " + pdFile.getVersion() + " Is enabled. ");
           tid = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
               public void run() {
                   try {
                        broadcastMessage("plugins/Broadcast/Messages.txt");
                   }  catch (IOException e) {
    
                   }
               }
           }, 3, interval * 20);
        }
        public static void broadcastMessage(String fileName) throws IOException {
                   FileInputStream fs;
                   fs = new FileInputStream(fileName);
                   BufferedReader br = new BufferedReader(new InputStreamReader(fs));
                   for(int i = 0; i < currentline; ++i)
                       br.readLine();
                   String line = br.readLine();
                   line = line.replaceAll("&f", ChatColor.WHITE + "");
                   line = line.replaceAll("&e", ChatColor.YELLOW + "");
                   line = line.replaceAll("&d", ChatColor.LIGHT_PURPLE + "");
                   line = line.replaceAll("&a", ChatColor.GREEN + "");
                   Bukkit.getServer().broadcastMessage(ChatColor.LIGHT_PURPLE + "[Broadcast] " + ChatColor.WHITE + line);
                   LineNumberReader lnr = new LineNumberReader(new FileReader( new File(fileName)));
                   lnr.skip(Long.MAX_VALUE);
                   int lastline = lnr.getLineNumber();
                   if(currentline + 1 == lastline + 1) {
                       currentline = 0;
                   } else {
                       currentline++; }
                  Map<String, Integer> CaveSpiderQuest = new HashMap<String, Integer>();
    
                   try
                      {
                        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("plugins/SystemNPC/CaveSpiderQuest.file"));
                        out.writeObject(CaveSpiderQuest);
                      }
                      catch(Exception e)
                      {
                        Bukkit.getServer().getLogger().info("["+getDescription().getName()+"] can't write savefile!");
                        e.printStackTrace();
                      }
        }

    What am i doing wrong?

    @ i also tried to do other alternatives i know but they didnt seem to work....

    My Entity Listener is being in a special state aswell.. idk why either i did as i was told too..... here i the entire coding i did on it,

    Code:
    package me.hacktichawkz.systemnpc;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.entity.CaveSpider;
    import org.bukkit.event.entity.EntityListener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class SystemNPCEntityListener extends PlayerListener {
    
       public static SystemNPC plugin;
       public SystemNPCEntityListener(SystemNPC instance) {
        }
    
        @SuppressWarnings("deprecation")
        public void onPlayerChat(PlayerChatEvent chat){
           Player p = chat.getPlayer();
           String message = chat.getMessage();
           String message_lower = message.toLowerCase();
           ChatColor GREEN = ChatColor.GREEN;
           ChatColor BLUE = ChatColor.BLUE;
           ChatColor BLACK = ChatColor.BLACK;
           ChatColor AQUA = ChatColor.AQUA;
           ChatColor GOLD = ChatColor.GOLD;
           ChatColor GRAY = ChatColor.GRAY;
           ChatColor LIGHT_PURPLE = ChatColor.LIGHT_PURPLE;
           ChatColor DARK_RED = ChatColor.DARK_RED;
           ChatColor DARK_PURPLE = ChatColor.DARK_PURPLE;
           ChatColor RED = ChatColor.RED;
           ChatColor WHITE = ChatColor.WHITE;
    }
        public Map<String, Integer> CaveSpiderQuest = new HashMap<String, Integer>();
    
        public void onEntityDeath(EntityDeathEvent event) {
            Entity dead = event.getEntity();
            if (!(dead instanceof CaveSpider)) {
            return;
            }
            Player killer = ((LivingEntity)dead).getKiller();
            if(killer == null)
              return;
            if (!(CaveSpiderQuest.containsKey(killer.getName())));{
            cavespiderKills.put(killer.getName(), 1);
            } else {
            cavespiderKills.put(killer.getName(), cavespiderKills.get(killer.getName()) + 1);
            }
            if (cavespiderKills.get(killer.getName()) == 50) {
                   PlayerInventory inventory = killer.getInventory();
                   ItemStack woodsword = new ItemStack(Material.WOOD_SWORD, 1);
                   inventory.addItem(woodsword);
                   killer.sendMessage(ChatColor.GREEN + "[Quest] " + ChatColor.GOLD + " You Completed a quest!" + ChatColor.LIGHT_PURPLE + " Rewarded > 1 Wood Sword" );
            }
            }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  15. Change your constructor from
    Code:java
    1.  
    2. public SystemNPCEntityListener(SystemNPC instance) {
    3. }
    4.  

    To this:
    Code:java
    1.  
    2. public SystemNPCEntityListener(SystemNPC plugin) {
    3. this.plugin = plugin;
    4. }
    5.  

    Also, your problem is this line (In the main class, in the broadcastMessage() method)
    Code:java
    1.  
    2. Bukkit.getServer().getLogger().info("["+getDescription().getName()+"] can't write savefile!");
    3.  

    You get the error because you are trying to use a non-static method (getDescription) in a static context (broadcastMessage). I suggest removing the static modifier of broadcastMessage if at all possible.
    Peace!
    - tips
     
  16. Offline

    HaCkTiC_HaWkZ

    Thanks alot :) i shall try this now :D and see what happens.

    i feel so dumb :L
    i shall make the server broadcaster a seperate plugin i guess :D that way it wont interfer

    ok changed the coding it told me to make a private static due to the fact it coloured plugin red so it is now


    Code:
    package me.hacktichawkz.systemnpc;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.entity.CaveSpider;
    import org.bukkit.event.entity.EntityListener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class SystemNPCEntityListener extends PlayerListener {
    
        private SystemNPC plugin;
    
        public SystemNPCEntityListener(SystemNPC plugin) {
        this.plugin = plugin;
            }
    
        public Map<String, Integer> CaveSpiderQuest = new HashMap<String, Integer>();
    
        public void onEntityDeath(EntityDeathEvent event) {
            Entity dead = event.getEntity();
            if (!(dead instanceof CaveSpider)) {
            return;
            }
            Player killer = ((LivingEntity)dead).getKiller();
            if(killer == null)
              return;
            if (!(CaveSpiderQuest.containsKey(killer.getName())));{
            cavespiderKills.put(killer.getName(), 1);
            } else {
            cavespiderKills.put(killer.getName(), cavespiderKills.get(killer.getName()) + 1);
            }
            if (cavespiderKills.get(killer.getName()) == 50) {
                   PlayerInventory inventory = killer.getInventory();
                   ItemStack woodsword = new ItemStack(Material.WOOD_SWORD, 1);
                   inventory.addItem(woodsword);
                   killer.sendMessage(ChatColor.GREEN + "[Quest] " + ChatColor.GOLD + " You Completed a quest!" + ChatColor.LIGHT_PURPLE + " Rewarded > 1 Wood Sword" );
            }
            }
    }
    and cavespiderKills is still underlined wrong...... idk how to fix it :'(

    and my main class is all fine now i removed the broadcaster it should complie right when the spiderkills is fixed

    i was thinking maybe declaring it as a stat?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  17. I'll bet it's cause you map is called CaveSpiderQuest, not cavespiderKills. ;)
     
  18. Offline

    HaCkTiC_HaWkZ


    I changed it and it worked :D and it told me to delete the } Else { so i did and now the coding is

    Code:
    package me.hacktichawkz.systemnpc;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.entity.CaveSpider;
    import org.bukkit.event.entity.EntityListener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class SystemNPCEntityListener extends PlayerListener {
    	private SystemNPC plugin;
    
    	public SystemNPCEntityListener(SystemNPC plugin) {
    	this.plugin = plugin;
    		}
    
        public Map<String, Integer> CaveSpiderQuest = new HashMap<String, Integer>();
    
        public void onEntityDeath(EntityDeathEvent event) {
            Entity dead = event.getEntity();
            if (!(dead instanceof CaveSpider)) {
            return;
            }
            Player killer = ((LivingEntity)dead).getKiller();
            if(killer == null)
              return;
            if (!(CaveSpiderQuest.containsKey(killer.getName())));{
            CaveSpiderQuest.put(killer.getName(), 1);
            CaveSpiderQuest.put(killer.getName(), CaveSpiderQuest.get(killer.getName()) + 1);
            }
            if (CaveSpiderQuest.get(killer.getName()) == 1) {
                   PlayerInventory inventory = killer.getInventory();
                   ItemStack woodsword = new ItemStack(Material.WOOD_SWORD, 1);
                   inventory.addItem(woodsword);
                   killer.sendMessage(ChatColor.GREEN + "[Quest] " + ChatColor.GOLD + " You Completed a quest!" + ChatColor.LIGHT_PURPLE + " Rewarded > 1 Wood Sword" );
            }
            }
    }
    Thank you :)..... i shall try it if not i did make some progress :) thanks :D

    its also giving me 5 warnings.....

    @ the warnings are

    Severity and Description Path Resource Location Creation Time Id
    The field SystemNPCEntityListener.plugin is never read locally SystemNPC/src/me/hacktichawkz/systemnpc SystemNPCEntityListener.java line 23 1324962623807 401
    The import org.bukkit.event.entity.EntityListener is never used SystemNPC/src/me/hacktichawkz/systemnpc SystemNPCEntityListener.java line 16 1324962623806 400
    The import org.bukkit.event.Event is never used SystemNPC/src/me/hacktichawkz/systemnpc SystemNPCEntityListener.java line 11 1324962623806 397
    The import org.bukkit.event.player.PlayerChatEvent is never used SystemNPC/src/me/hacktichawkz/systemnpc SystemNPCEntityListener.java line 12 1324962623806 398
    The import org.bukkit.event.player.PlayerEvent is never used SystemNPC/src/me/hacktichawkz/systemnpc SystemNPCEntityListener.java line 13 1324962623806 399

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  19. You can remove all the imports in yellow...As for the other one, you can ignore it for now, or add a @SurpressWarnings("unused") above it
     
  20. Offline

    HaCkTiC_HaWkZ

    ah thanks alot :D and i jst seen a creeper on the server so ill try it with that changed the coding and changed the thing to 1 from 50 so ill kill it see what happens :D

    thank you alot for the help much appriciated

    >_> it killed me but i just found out my commands are not working :O

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args) {
    		    readCommand((Player) sender, commandLable);
    			readCommand1((Player) sender, commandLable);
    			readCommand2((Player) sender, commandLable);
    		    readCommand3((Player) sender, commandLable);
    			readCommand4((Player) sender, commandLable);
    	        readCommand5((Player) sender, commandLable);
    			readCommand6((Player) sender, commandLable);
    		    readCommand7((Player) sender, commandLable);
    			readCommand8((Player) sender, commandLable);
    			readCommand9((Player) sender, commandLable);
    		    readCommand10((Player) sender, commandLable);
    			readCommand11((Player) sender, commandLable);
    	        readCommand12((Player) sender, commandLable);
    			readCommand13((Player) sender, commandLable);
    		    readCommand14((Player) sender, commandLable);
    			readCommand15((Player) sender, commandLable);
    			readCommand16((Player) sender, commandLable);
    			readCommand17((Player) sender, commandLable);
    			readCommand18((Player) sender, commandLable);
    			readCommand19((Player) sender, commandLable);
    			readCommand20((Player) sender, commandLable);
    			readCommand21((Player) sender, commandLable);
    			readCommand22((Player) sender, commandLable);
    			return false;
    	}
    
    	public void readCommand(Player player, String command) {
    		if(command.equalsIgnoreCase("creative")){
    			player.setGameMode(GameMode.CREATIVE);
    			player.sendMessage(ChatColor.GREEN + "[Server] " + ChatColor.GRAY + " Now entering " + ChatColor.GOLD + " Creative mode!");
    			}
    			else if(command.equalsIgnoreCase("survival")){
    				player.setGameMode(GameMode.SURVIVAL);
    				player.sendMessage(ChatColor.GREEN + "[Server] " + ChatColor.GRAY + " Now entering " + ChatColor.GOLD + " Survival mode!"); }
    			}
    
    and each command was readCommand and a number next to it

    readCommand1
    readCommand2
    etc

    it all worked while having the broadcast system :'(

    omg i am such a n00b

    ok i killed a creeper and nothing happened..... it was suppost to give me the wooden sword and the message but it did not :( i fail epiclly

    ok looks like i fixed my systemnpc problem so the broadcast system should work too,...... but i think i know why its not letting me get my rewards when i kill something

    Code:
    package me.hacktichawkz.systemnpc;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Creeper;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.entity.CaveSpider;
    import org.bukkit.event.entity.EntityListener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class SystemNPCEntityListener extends PlayerListener {
    	private SystemNPC plugin;
    
    	public SystemNPCEntityListener(SystemNPC plugin) {
    	this.plugin = plugin;
    		}
    
        public Map<String, Integer> CaveSpiderQuest = new HashMap<String, Integer>();
    
        public void onEntityDeath(EntityDeathEvent event) {
            Entity dead = event.getEntity();
            if (!(dead instanceof CaveSpider)) {
            return;
            }
            Player killer = ((LivingEntity)dead).getKiller();
            if(killer == null)
              return;
            if (!(CaveSpiderQuest.containsKey(killer.getName())));{
            CaveSpiderQuest.put(killer.getName(), 1);
            } else {
            CaveSpiderQuest.put(killer.getName(), CaveSpiderQuest.get(killer.getName()) + 1);
            }
            if (CaveSpiderQuest.get(killer.getName()) == 1) {
                   PlayerInventory inventory = killer.getInventory();
                   ItemStack woodsword = new ItemStack(Material.WOOD_SWORD, 1);
                   inventory.addItem(woodsword);
                   killer.sendMessage(ChatColor.GREEN + "[Quest] " + ChatColor.GOLD + " You Completed a quest!" + ChatColor.LIGHT_PURPLE + " Rewarded > 1 Wood Sword" );
            }
            }
    }
    the } else { statement is being underlined and it says

    Severity and Description Path Resource Location Creation Time Id
    Syntax error on token "else", delete this token SystemNPC/src/me/hacktichawkz/systemnpc SystemNPCEntityListener.java line 44 1324966018953 464

    ........

    i tried changing it into ways i know would work but....it did not :(

    Ok

    will try this out this way, i did some code changing and use @tips48 's code which the } else { method worked

    and now the code looks like this

    Code:
    package me.hacktichawkz.systemnpc;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.entity.CaveSpider;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class SystemNPCEntityListener extends PlayerListener {
    	@SuppressWarnings("unused")
    	private SystemNPC plugin;
    
    	public SystemNPCEntityListener(SystemNPC plugin) {
    	this.plugin = plugin;
    		}
    
        public Map<String, Integer> CaveSpiderQuest = new HashMap<String, Integer>();
    
        public void onEntityDeath(EntityDeathEvent event) {
        	Entity dead = event.getEntity();
        	if (!(dead instanceof CaveSpider)) {
        	return;
        	}
        	Player killer = ((LivingEntity)dead).getKiller();
        	if(killer == null)
        	  return;
        	if (!(CaveSpiderQuest.containsKey(killer.getName()))) {
        	CaveSpiderQuest.put(killer.getName(), 1);
        	} else {
        	CaveSpiderQuest.put(killer.getName(), CaveSpiderQuest.get(killer.getName()) + 1);
        	}
        	if (CaveSpiderQuest.get(killer.getName()) == 1) {
        	// Do whatever
        		  PlayerInventory inventory = killer.getInventory();
                  ItemStack woodsword = new ItemStack(Material.WOOD_SWORD, 1);
                  inventory.addItem(woodsword);
                  killer.sendMessage(ChatColor.GREEN + "[Quest] " + ChatColor.GOLD + " You Completed a quest!" + ChatColor.LIGHT_PURPLE + " Rewarded > 1 Wood Sword" );
        	}
        	}
        	}
    
    ok got it to work with the broadcasting system and the commands (y)

    i also seen i forgot the read file in enable..... silly me :L

    got all of the coding to work except for this line

    if(o == null || !(o instanceof HashMap<String, Integer>))

    .....

    i have no idea how to fix this..... i am 110% stuck

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

    HappyPikachu


    Er... you might want to sit back and read some more tutorials. For now, try this for cmds:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. if (sender instanceof Player & cmd.getName().equalsIgnoreCase("creative")) {
    3. Player p = (Player) sender;
    4. p.setGameMode(GameMode.CREATIVE);
    5. p.sendMessage(ChatColor.GREEN + "[Server] " + ChatColor.GRAY + "Now entering " + ChatColor.GOLD + "Creative mode!");
    6. } else if (sender instanceof Player & cmd.getName().equalsIgnoreCase("survival")) {
    7. Player p = (Player) sender;
    8. p.setGameMode(GameMode.SURVIVAL);
    9. p.sendMessage(ChatColor.GREEN + "[Server] " + ChatColor.GRAY + "Now entering " + ChatColor.GOLD + "Survival mode!");
    10. }
    11. }
     
  22. Offline

    HaCkTiC_HaWkZ


    I know, but still all i have done so far was my own coding i am getting to the point where i want to make it from good to epic

    and the commands that i used work perfectly, i had to change them to that so i could use broadcast system and other commands :)

    Thanks for the example i am thinking about just a commands plugin so i shall keep that in mind so the commands are either true or false (boolean) i do pay attention :D
    for now im sticking to commands1-??? :)
     
  23. Offline

    yannkaiser16

    I don't understand ... Why is your 'entityListener' extending a 'PlayerListener' ?
    :)
     
    tips48 likes this.
  24. When a command returns false, the usage message is shown (The message in plugin.yml). When a command returns true, the usage message is not shown
     
  25. Offline

    HaCkTiC_HaWkZ

    :O omg i must have pressed ctrl z again >_> thank you for pointing that out sometimes i hate when i undo typing

    Thanks :D did not know this, always focused on the True and False bit :) now i know thanks for that :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
Thread Status:
Not open for further replies.

Share This Page