linking to hashmap in main from listener

Discussion in 'Plugin Development' started by kamakarzy, May 8, 2013.

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

    kamakarzy

    exactly what title says i want to add a event.getClickedBlock().getLocation() from my playerlistener to a hashmap that located in main class
     
  2. Offline

    repsor

    And what's the problem?
     
  3. Offline

    teunie75

    1. The hashmap may not be private
    2. Register your listener by *.registerEvents(new classname(this), this);
    3. Create a constuctor in your listener like this:
    Code:
    private MainClass plugin
    public Listener(MainClass plugin){
    this.plugin = plugin;
    }
    
    4. Access your hashmap with: plugin.hashmap.something
     
  4. Offline

    kamakarzy

    teunie75 i tried that i seem to have everything right but i cnt access it


    Main
    Code:
        public class Main extends JavaPlugin {
     
     
                public void onDisable() {}
     
                public void onEnable() {
                    PluginManager pm = this.getServer().getPluginManager();
                    pm.registerEvents(new playerListener(this), this);
                    pm.registerEvents(new blockListener(this), this);
                    HashMap<Block, Location> hashmap = new HashMap<Block, Location>();
                }
        }
    
    listener
    Code:
    public class playerListener implements Listener {
     
        private Main plugin;
        public playerListener(Main instance) {
            plugin = instance;
        }
     
        @EventHandler
        public void playerplant(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Location loc = event.getClickedBlock().getLocation();
            if (loc.equals(Material.SOIL)) {
                if (player.getItemInHand().equals(Material.RAW_BEEF)) {
                    player.setItemInHand(null);
                    plugin.hashmap.put(loc);
     
  5. Offline

    teunie75

    kamakarzy Try to put your code where you create your hashmap above the onDisable()
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    Windy Day kamakarzy
    It does not need to be static, but it does need to be a field. If you don't want your class to be a field, then it will need to be a local variable. In fact making it a static field can open up you to a whole host of issues, so best just to avoid it.
     
  7. Offline

    socram8888

    {{Why are you using a HashMap to link blocks to their location? Using block.getLocation() is way faster, because every block internally stores its own location. If you are using it to store a list of blocks, you should use a HashSet<Block> or HashSet<Location>

    Now on topic, you only have to pass the Listener the main plugin instance, which you should have a getter. Something like this:
    Code:
    public class ExamplePlugin extends JavaPlugin {
    private Set<Block> blocks;
    
    public void onEnable() {
    blocks = new HashSet<Block>();
    new ExampleListener(this);
    }
    
    public Set<Block> getBlocks() {
    return blocks;
    };
    
    }
    
    ExampleListener:
    Code:
    public class ExampleListener implements Listener {
    private ExamplePlugin plugin;
    public ExampleListener(ExamplePlugin plugin) {
    this.plugin = plugin;
    plugin.getServer().getPluginManager().registerEvents(plugin, this);
    }
    
    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
    Block brokenBlock = event.getBlock();
    Set<Block> blocks = plugin.getBlocks();
    if (blocks.contains(brokenBlock)) {
    plugin.getLogger().info("Somebody has tried to break a protected block!");
    event.setCancelled(true);
    }
    }
    }
    
    You could actually use a public variable rather than a getter, but this is the standard way to get variables from other classes
     
  8. Offline

    kamakarzy

    socram8888 Sagacious_Zed i need the location of a block i right click on with a peice of raw beef because i need to use the player listener and block listener at the same time and tbh i have no idea about how to do what i want to do but its trial and error

    if you want to know this is what im trying to do i plant a raw porkchop in soil (hoe'd dirt) and after the same amount of time as it takes to grow a tree and it grows a piglet :)
     
Thread Status:
Not open for further replies.

Share This Page