I believe I have an Infinite loop in my Plugin, Help please?

Discussion in 'Plugin Development' started by Slymansyman, Jun 23, 2013.

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

    Slymansyman

    Hi, I'm trying to create a plugin where you type /im and it will enable instant mining. Where you can punch a block and it will turn to air. I get spammed with lots of things when The server starts..
    Code:
    Could not load 'plugins\InstantMine.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.StackOverflowError
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_5_R3.CraftServer.loadPlugins(CraftServer.java:239)
        at org.bukkit.craftbukkit.v1_5_R3.CraftServer.<init>(CraftServer.java:217)
        at net.minecraft.server.v1_5_R3.PlayerList.<init>(PlayerList.java:55)
        at net.minecraft.server.v1_5_R3.DedicatedPlayerList.<init>(SourceFile:11)
        at net.minecraft.server.v1_5_R3.DedicatedServer.init(DedicatedServer.java:106)
        at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:382)
        at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.StackOverflowError
        at java.util.Hashtable.get(Unknown Source)
        at java.util.logging.LogManager$LoggerContext.findLogger(Unknown Source)
        at java.util.logging.LogManager.getLogger(Unknown Source)
        at java.util.logging.LogManager.demandLogger(Unknown Source)
        at java.util.logging.Logger.demandLogger(Unknown Source)
        at java.util.logging.Logger.getLogger(Unknown Source)
        at me.slymansyman.InstantMine.InstantMine.<init>(InstantMine.java:17)
        at me.slymansyman.InstantMine.InstantMineListener.<init>(InstantMineListener.java:10)
    This is my current java code.

    The main file which is called InstantMine.
    Code:
    package me.slymansyman.InstantMine;
     
    import java.awt.Event;
    import java.util.ArrayList;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class InstantMine extends JavaPlugin implements Listener {
       
       
            private final Logger log = Logger.getLogger("Minecraft");
            private final InstantMineListener blockListener = new InstantMineListener(this);
            public final ArrayList<Player> InstantMineUsers = new ArrayList<Player>();
        @Override
            public void onEnable(){
               
            log.info("Instantmine Has been Enabled!");
            PluginManager pm = getServer().getPluginManager();
            getServer().getPluginManager().registerEvents(new InstantMine(), this);
           
     
        }
        @Override
            public void onDisable(){
            log.info("Instantmine Has been Disabled!");
        }
            @Override
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                if(commandLabel.equalsIgnoreCase("IM"))
                    toggleBlockChanger(sender);
               
                return true;
           
            }
        private void toggleBlockChanger(CommandSender sender)
        {    if(!enabled ((Player) sender)){
            InstantMineUsers.add((Player) sender);
            ((Player) sender).sendMessage(ChatColor.BLUE + "Instant Mine Has been enabled!");
        }
        else
            {InstantMineUsers.remove((Player) sender);
        ((Player) sender).sendMessage(ChatColor.RED + "Instant Mine Has been enabled!");}
        }
        public boolean enabled(Player player)
        {
            return InstantMineUsers.contains(player);
        }
       
    }
     
    
    And then I have my Listener class called InstantMineListener

    Code:
    package me.slymansyman.InstantMine;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockDamageEvent;
     
    public class InstantMineListener extends InstantMine implements Listener{
     
        public static InstantMine plugin;
     
        public InstantMineListener(InstantMine instance)
        {
            plugin = instance;
        }
        @EventHandler
        public void onBlockDamage(BlockDamageEvent event)
        {
            if(plugin.enabled(event.getPlayer()))
                event.getBlock().setTypeId(0);
        }
     
    }
    
    Could you please help me? :) I'm Very Very new to the Plugin world, However I wish to start, and have a lot of dedication towards it.
     
  2. Offline

    Shzylo

    For your InstantMineListener, use a PlayerInteractEvent, not a BlockDamageEvent. Get if the player left clicks, and get the type of block then drop that block type.
     
  3. Offline

    CubieX

    Code:
     PluginManager pm = getServer().getPluginManager();
            
    This line is pointless.

    Code:
    getServer().getPluginManager().registerEvents(new InstantMine(), this);
    Don't make a new instance of your main class. You have to state your Listener class here.
    Code:
    getServer().getPluginManager().registerEvents(blockListener, this);
    Another thing:
    Code:
    public class InstantMine extends JavaPlugin implements Listener {
    Your main class does currently not implement "Listener", so do not state it here.
    This belongs to your InstantMineListener class.

    Code:
    public class InstantMineListener extends InstantMine implements Listener{
    Your listener does not need to "extend InstantMine", unless you are really going to override some of it's methods.
    (which I doubt you want to)
     
  4. Offline

    Slymansyman

    I currently hadn't thought about drops, I want them, but have no idea how to do that. Later today I'll be back on my computer and I'll try to work it out.

    Right, So it works however It does not give drops. Any idea how to fix that please?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  5. Offline

    Shzylo

    Code:
    Block b = event.getClickedBlock();
    Action act = event.getAction();
     
    if(b != null) {
      if(act == Action.RIGHT_CLICK_BLOCK) {
        b.breakNaturally();
      }
    }
     
  6. Offline

    Slymansyman

    Okay that works fine, However I would like it to only drop if they left click with cake.
    Code:
        @EventHandler
        public void onPlayerInteractBlock(PlayerInteractEvent event){
       
            if(event.getPlayer().getItemInHand().getType() == Material.CAKE.getId()){
               
                Block b = event.getClickedBlock();
                Action act = event.getAction();
               
                if(b != null) {
                  if(act == Action.LEFT_CLICK_BLOCK) {
                    b.breakNaturally();
                
    Thats it so far however I get an error with "getId"
     
  7. Offline

    Shzylo

    You don't need the .getId()
     
  8. Offline

    Slymansyman

    When it's like this:
    Code:
    if(event.getPlayer().getItemInHand().getType() == Material.CAKE)
    I get an error saying Incompatible operand types.
     
  9. Offline

    Techy4198

    this is how i would do it. i do stuff like this by getting all the variables seperately and shoving them into one final short line of code.
    Code:
    Block b = event.getBlock()
    Location l = new Location(b.getLocation());
    World w = b.getWorld();
    Player p = event.getPlayer();
     
    if(p.getGameMode() == GameMode.SURVIVAL){
        for(Itemstack i : b.getDrops()){
          w.dropItem(l, i);
        }
    }
    also i see when you disable instant mine it will say it has been enabled, might wanna fix that :)
     
  10. Offline

    Slymansyman

    How would I do it so that it would only work if the item in your hand is cake? Or a wooden Pickaxe?

    Oh and when I tried out the plugin I found that xD
     
  11. Offline

    Shzylo

    Oh, on your if statement ( if(player.getItemInHand().getType == Material.CAKE ) add .getInventory before .getItemInHand().

    That will check if cake is in your hand, and then put in the brackets for that if statement for what you want it to do when it is in your hand.
     
  12. Offline

    Slymansyman

    Thabks for all of your help btw.
    I now have this and "player cannot be resolved"
    Code:
        @EventHandler
        public void onPlayerInteractBlock(PlayerInteractEvent event){
            Block b = event.getClickedBlock();
            Action act = event.getAction();
            Player p = event.getPlayer();
            if(player.getInventory().getItemInHand().getType() == Material.CAKE){
                if(b != null) {
                  if(act == Action.LEFT_CLICK_BLOCK) {
                    b.breakNaturally();
    Okay, Thank you so much everyone. I have got it all to work <3 The issue above I had imported the wrong Material.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  13. Offline

    Alex5657

    The line is not pointless. He is saving the PluginManager object in a variable to directly refer to it instead of getting it every single time he wants to make a reference to it. Also, its called "hard-code" if you dont do that.
     
  14. Offline

    Shzylo

    Alex5657 he said it was pointless because he didn't even use the variable. It just sat there unused.

    See?
    PluginManager pm = getServer().getPluginManager();
    getServer().getPluginManager().registerEvents(new InstantMine(), this);
     
    CubieX likes this.
  15. Offline

    Alex5657

    Ah lol. My mistake. Then there is no point in saving it.
     
  16. Offline

    Techy4198

    for 'player cannot be resolved' if you havent fixed it yet, change it to player to p
     
Thread Status:
Not open for further replies.

Share This Page