Solved Code Not Working

Discussion in 'Spigot Plugin Development' started by JunkdudeHD, Apr 29, 2017.

  1. Code isnt working, no errors or anything.

    Listener2.java
    Code (Text):
    package net.flutterflies.template;

    import net.flutterflies.template.util.Language;
    import net.flutterflies.template.util.Settings;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.*;
    import org.bukkit.event.block.BlockEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.block.BlockBreakEvent;
    import java.io.File;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.HumanEntity;
    //import org.bukkit.item.Item;
    //import org.bukkit.event.listener;
    import org.bukkit.event.EventHandler;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.AnvilInventory;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.InventoryView;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.event.EventPriority;
    import java.util.Arrays;
    import java.util.List;
    import org.bukkit.event.Listener;

    public class Listener2 implements Listener{

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        Block block = event.getBlock();
        World world = block.getWorld();
        //Player player = event.getPlayer();
        String[] s = {"IRON_AXE", "STONE_AXE"};
        ItemStack hand = player.getInventory().getItemInHand();
        //.equals

        for(String str:s /*s is the array*/) {
            if(hand.equals(str)) {
                player.sendMessage("lit");
            }
        }

    }
    }
    Template.java
    Code (Text):

    package net.flutterflies.template;

    import net.flutterflies.template.util.Language;
    import net.flutterflies.template.util.Settings;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.*;
    import org.bukkit.event.block.BlockEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.block.BlockBreakEvent;
    import java.io.File;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.HumanEntity;
    //import org.bukkit.item.Item;
    //import org.bukkit.event.listener;
    import org.bukkit.event.EventHandler;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.AnvilInventory;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.InventoryView;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.event.EventPriority;
    import java.util.Arrays;
    import java.util.List;
    import org.bukkit.event.Listener;
    import net.flutterflies.template.Listener2;

    public class Template extends JavaPlugin{
        private static Template plugin;

        /**
         * The instance of the wrapper class for the plugin's
         * configuration file.
         */
        private Settings settings;

        /**
         * The instance of the wrapper class for the plugin's
         * language localization file
         */
        private Language language;

         public static Plugin getPlugin() {
            return plugin;
        }

        /**
         * Method called when the plugin gets enabled on the server.
         * Initializes all aspects of the plugin.
         */
        @Override
        public void onEnable() {
            initFiles();
            registerListeners();
            registerCommands();
            plugin = this;
            settings = new Settings(getConfig());
            language = new Language(getDataFolder());
            registerEvents(this, new Listener2());
           // Bukkit.getServer().getPluginManager().registerEvents(this new Listener2(this));
        }

        //Much eaisier then registering events in 10 diffirent methods
        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
        for (Listener listener : listeners) {
        Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
        }
        }


        /**
         * Method called when the plugin gets disabled. Safely stops all of the plugins processes.
         */
        @Override
        public void onDisable() {

        }

        /**
         * Make sure all of the necessary plugin files exist in the plugin's data folder.
         */
        private void initFiles() {
            saveDefaultConfig();

            File lang = new File(getDataFolder(), "lang.yml");
            if(!lang.exists()) {
                saveResource("lang.yml", false);
            }
        }

        /**
         * Register all of the listeners used by the plugin.
         */
        private void registerListeners() {

        }

        /**
         * Register all of the command executors and tab completers for the plugin's commands
         */
        private void registerCommands() {

        }

        /**
         * Get the instance of the plugin's config wrapper.
         *
         * @return The plugin's config wrapper.
         */
        public Settings getSettings() {
            return settings;
        }

        /**
         * Get the instance of the plugin's lang file wrapper.
         *
         * @return the plugin's lang wrapper.
         */
        public Language getLanguage() {
            return language;
        }


    //EVENTS GO UNDER HERE

    }
     
     
    #1 JunkdudeHD, Apr 29, 2017
    Last edited: Apr 29, 2017
  2. In your register listeners method in Template.java you never register the events for Listener2.java




    Sent from my iPhone using Tapatalk
     
  3. 1) You are not registering your events
    2) Why did you implement Listener for your Template class?
     
  4. Isnt that what this line is this.getServer().getPluginManager().registerEvents(new Listener2(), this);
     
  5. Yeah you did register but you didn't put it in your register method in the template.

    If you're trying to get if the player has a stone axe you're kind of going about it wrong.

    After you get the item in hand,

    Do if hand==null
    Return;

    This ensures you won't get console errors when the player punches a block to death.

    Then simply do

    If hand.getType() == Material.STONE_AXE || hand.getType() == Material.IRON_AXE


    Sent from my iPhone using Tapatalk
     
  6. Here i updated my template code
     
  7. Code (Text):
    //Much eaisier then registering events in 10 diffirent methods
        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
        for (Listener listener : listeners) {
        Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
        }
        }
     
    Don't think this would work. Register the events individually.
     
  8. How do i do that
     
  9. Do
    Code (Text):
    Bukkit.getPluginManager.registerEvent(LISTENER, \main plugin\)