How to make a method for a HashMap

Discussion in 'Plugin Development' started by KvackPack, Jun 3, 2011.

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

    KvackPack

    I, I'm trying to make a /item command and I've got problems.
    First of all, I've made a HashMap to carry all the words for items instead of ex.(46). Then they only need to write /item TNT.

    But how do you make a method or something to carry it?

    Code:
    HashMap<String,Integer> itemlist = new HashMap<String,Integer>();
    
    
    //so where do i put the following?
    
    
         itemlist.put("STONE",1);
         itemlist.put("GRASS",2);
         itemlist.put("DIRT",3);
         itemlist.put("COBBLESTONE",4);
         itemlist.put("PLANK",5);
         itemlist.put("WOOD",5);
         itemlist.put("SAPLING",6);
     
  2. Depends, but I guess in your case I would put them into your "onEnable" function, to sort of "initialize" those.
     
  3. Offline

    KvackPack

    Ok, thanks.
    By the way, when i did that there wheren't any syntacs errors anymore for that
     
  4. If want it organised make a function, lets say:
    Code:
    private void initializeBlockNames()
    {
    // ALL THE BLOCKS
    }
    
    public void onEnable()
    {
    // Call your function here
    this.initializeBlockNames();
    
    }
     
  5. @KvackPack So basically your trying to make a /give command?
     
  6. Id say, just use AdminCmd or PlgEssentials, but well everones gotta start somewhere.
     
  7. Offline

    KvackPack

    where should i put it? :/

    Im also getting errors by trying to launch it on bukkit

    Code:
    10:51:21 [ALLVARLIG] Could not load 'plugins\PowerItem.jar' in folder 'plugins':
    
    while scanning for the next token
    found character         '\t' that cannot start any token
     in "<reader>", line 8, column 1:
    
        ^
    
            at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.ja
    va:360)
            at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:18
    3)
            at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(Par
    serImpl.java:564)
            at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:163)
            at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:148)
            at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java
    :228)
            at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:160)
            at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java
    :230)
            at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:160)
            at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java
    :230)
            at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:160)
            at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:12
    2)
            at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:105)
    
            at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseCons
    tructor.java:124)
            at org.yaml.snakeyaml.Yaml.load(Yaml.java:264)
            at org.yaml.snakeyaml.Yaml.load(Yaml.java:250)
            at org.bukkit.plugin.PluginDescriptionFile.<init>(PluginDescriptionFile.
    java:30)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:68)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:194)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:117)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:103)
            at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:232)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:219)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:146)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:285)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    Heres The Code:

    Code:
    package com.KvackPack.PowerItem;
    
    import java.util.HashMap;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    public class PowerItemItemCommando implements CommandExecutor {
         private final PowerItem plugin;
         public PowerItemItemCommando(PowerItem instance) {
          plugin = instance;
         }
    
         @Override
         public boolean onCommand(CommandSender player, Command command, String label, String[] split){
             if (!(player instanceof Player))
                 return true;
    
             Player Player = (Player)player;
    
             if(split.length == 1) {
                 Player.getInventory().addItem(new ItemStack(Integer.parseInt(split[0]), 64));
                 Player.sendMessage("Given A Stack Of Desired Items");
             }
             return true;
         }
    
         HashMap<String,Integer> itemlist = new HashMap<String,Integer>();
         private void initializeBlockNames(){
             itemlist.put("STONE",1);
             itemlist.put("GRASS",2);
             itemlist.put("DIRT",3);
             itemlist.put("COBBLESTONE",4);
             itemlist.put("PLANK",5);
             itemlist.put("WOOD",5);
             itemlist.put("SAPLING",6);
             itemlist.put("PLANT",6);
             itemlist.put("BEDROCK",7);
             itemlist.put("WATER",8);
             itemlist.put("STILL WATER",9);
             itemlist.put("LAVA",10);
             itemlist.put("STILL LAVA",11);
             itemlist.put("SAND",12);
             itemlist.put("GRAVEL",13);
             itemlist.put("GOLD ORE",14);
             itemlist.put("IRON ORE",15);
             itemlist.put("COAL ORE",16);
             itemlist.put("LOG",17);
             itemlist.put("TREE",17);
             itemlist.put("LEAF",18);
             itemlist.put("LEAVES",18);
             itemlist.put("SPONGE",19);
             itemlist.put("GLASS",20);
             itemlist.put("LAPIS ORE",21);
             itemlist.put("LAPIS LAZULI ORE",21);
             itemlist.put("LAPIS BLOCK",22);
             itemlist.put("LAPIS LAZULI BLOCK",22);
             itemlist.put("DISPENSER",23);
             itemlist.put("EJECTOR",23);
             itemlist.put("SANDSTONE",24);
             itemlist.put("NOTE BLOCK",25);
             itemlist.put("BED BLOCK",26);
             itemlist.put("POWERED RAIL",27);
             itemlist.put("POWERED TRACK",27);
             itemlist.put("POWERED MINECART TRACK",27);
             itemlist.put("POWERED MINECART RAIL",27);
             itemlist.put("DETECTOR RAIL",28);
             itemlist.put("DETECTOR TRACK",28);
             itemlist.put("COBWEB",30);
             itemlist.put("SPIDER WEB",30);
             itemlist.put("WILD GRASS",31);
             itemlist.put("TALL GRASS",31);
             itemlist.put("DEAD SHRUBS",32);
             itemlist.put("WOOL",35);
             itemlist.put("CLOTH",35);
             itemlist.put("DANDELION",37);
             itemlist.put("YELLOW FLOWER",37);
             itemlist.put("ROSE",38);
             itemlist.put("RED FLOWER",38);
             itemlist.put("BROWN MUSHROOM",39);
             itemlist.put("RED MUSHROOM",40);
             itemlist.put("GOLD BLOCK",41);
             itemlist.put("IRON BLOCK",42);
             itemlist.put("DOUBLE STEP",43);
             itemlist.put("DOUBLE SLAB",43);
             itemlist.put("STEP",44);
             itemlist.put("SLAB",44);
             itemlist.put("BRICK BLOCK",45);
             itemlist.put("BRICK WALL",45);
             itemlist.put("TNT",46);
             itemlist.put("DYNAMITE",46);
             itemlist.put("BOOKSHELF",47);
             itemlist.put("MOSS STONE",48);
             itemlist.put("MOSSY COBBLESTONE",48);
             itemlist.put("MOSSY STONE",48);
             itemlist.put("MOSS COBBLESTONE",48);
         }
    
         public void onEnable(){
         this.initializeBlockNames();
         }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 16, 2016
  8. The Problem is your plugin.yml.
    Make sure it does not contain TABS.

    As for the Code, I guess you should check one some of the Basic Java/Plugin
    Tutorials before diving into making a Plugin.

    1. You have no Pluginclass that extends JavaPlugin.
    2. The CommandExecutor Class should to be a Second one.
    3. The HashMap should be declared as "public" or "private".
     
  9. Offline

    KvackPack

    Ok, thanks

    EDIT: Same error again
    Here is the .yml

    name: PowerItem
    main: com.KvackPack.PowerItem.PowerItem
    version: 1.0
    commands:
    /item:
    description: Item Spawn
    usage: /item
     
  10. As I said, you dont have any mainclass.
    Have a look in here:
    Sample Plugin
     
  11. Offline

    KvackPack

    public class PowerItem extends JavaPlugin

    Im getting an error for PowerItem. It says:
    "The public type Poweritem must be defined in its own file"

    I have to classes, one called PowerItem and one PowerItemItemCommando (which I had problem with)

    Still getting that error for the .yml file

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

    DreadKyller

    you can not have more that one class in the same .java file unless it in inside the main class. also the class name must be the name of the java file. post your top line in the script please, the one that says
    PHP:
    package blah.blah.somethingorwhatever
     
Thread Status:
Not open for further replies.

Share This Page