Adding a hashtable to a plugin

Discussion in 'Plugin Development' started by Hohahihehu, Jan 22, 2011.

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

    Hohahihehu

    I'm really not very experienced with either java or bukkit, so could someone tell me exactly what I would need to do to add a hashtable into my plugin and have it recognise it properly?

    Ty vm!
     
  2. Offline

    matjam

  3. Offline

    Hohahihehu

    I have a hashtable made, I just want to know how to set up my plugin so that my BlockListener can search through the hashtable. If there's another way to do it, or if I'm completely not understanding this, could someone please explain to me how? Preferably in a step-by-step way, as I'm quite a noob to java. I can figure out how to set up the logics of the program, but when it comes to imports and extensions and so on I don't know what I need to put in there.
     
  4. Offline

    matjam

    Yeah its more of a java question than a Bukkit question.

    When you create the instance of your BlockListener, you pass it the plugin.

    Code:
    private final MyPluginBlockListener blockListener = new MyPluginBlockListener(this);
    The "this" refers to that specific instance of the plugin class that you create.

    Make your Hashtable a public member of your plugin class, and then refer to it inside the BlockListener with plugin.myHashtable.

    There is probably other ways, such as using a singleton, but I think for most people's needs the above is fine.
     
  5. Offline

    Hohahihehu

    Ok, I'm probably doing this wrong. Sorry that I'm asking java questions on the bukkit forum, but this is all I need to finish my plugin. This is what I have in my code, based on some tutorial, plus my best attempt at replicating what you told me. Does this look right?

    Code:
    public class RuneCaster extends JavaPlugin {
        private final RuneCasterPlayerListener playerListener = new RuneCasterPlayerListener(this);
        private final RuneCasterBlockListener blockListener = new RuneCasterBlockListener(this);
        private RuneCasterHashtable = new HashTable(this);
     
  6. Offline

    eisental

    should be
    Code:
        private RuneCasterHashtable = new Hashtable(this);
    Hashtable not HashTable. Besides, I think it's supposed to be better to use a HashMap instead though i'm not sure why.
     
  7. Offline

    Hohahihehu

    Thank you guys for your help, I should have it working within a day or so!

    Also, making a plugin for my very first time using Java probably isn't the smartest thing for my sanity, even if I've done a bit of programming in other languages.
     
  8. Offline

    DinoEntrails

    I am also a noob when it comes to Java, so I was wondering how you put values into the hash table and how you grab the values back? What is the point of using a hashtable vs an array or other storage?
     
  9. Offline

    Afforess

    Don't use a hash table or a hash map! They are not multiple thread safe. (If you have more than one CPU, and both CPU's try to access them at the same time, undefined behavior will occur).

    Use a ConcurrentHashMap.
     
  10. Offline

    Hohahihehu

    Thank you Afforess! I don't want my plugin to accidentally someone's entire server. I will switch it over.
     
Thread Status:
Not open for further replies.

Share This Page