1

I would like to know what is "Magic" near the getInstance(). I found it in an answer of this reply of this question (Setting the block underneath any player to glowstone, then setting it back to the original material) so I would like to know what is Magic as it isn't working and how to fix it. Magic cannot be resolved is what the error says.

package me.deltarift.walkonwater;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class WalkOnWater extends JavaPlugin {

    @EventHandler
    public void onStep(PlayerMoveEvent pme) {
        Player player = pme.getPlayer();
        final Block block = player.getLocation().subtract(0, 1, 0).getBlock();
        final Material type = block.getType();
        if(!type.equals(Material.GLOWSTONE)){
            @SuppressWarnings("deprecation")
            Material m = player.getItemInHand().getType();
            if (m == Material.GLOWSTONE) {
                if(type.isSolid()) {
                    block.setType(Material.GLOWSTONE);
                    Bukkit.getScheduler().runTaskLater(Magic.getInstance(), new Runnable(){
                        public void run(){
                            block.setType(type);
                        }
                    },10L);
                }
            }
        }   
    }   
}
Deltarift
  • 21
  • 1
  • 2
    Magic is the singelton class , whose getInstance method returns the object of Magic itself. – sourabh1024 Aug 06 '17 at 07:49
  • If you don't understand a response to a question, add a comment to clarify the answer rather than creating a new question. This create unnecessary duplicates. Adding in the context of the original question also helps to improve answers for others. – Simon Black Aug 06 '17 at 08:02

2 Answers2

3

The Method Scheduler#runTaskLater(plugin, runnable, delay) takes the Main class of your plugin (The one that extends JavaPlugin) as the first argument so that it can manage the task (i.e cancel it when the plugin is disabled)

However, a static getInstance() to get the main class is not recommended, and you should prefer to use dependency injectionas outlined in this answer.


Not part of the main discussion, but:

Here's some optimization tips for that code:

  1. The PlayerMoveEvent is called even when the mouse is moved, i.e many, MANY times. You should have a check to make sure they're moving into another block, and that they're holding the glowstone first.
  2. As of the Combat Update, Dual Wielding was added, so getItemInHand() was marked deprecated. You should use Player#getInventory().getItemInMainHand(); or Player#getInventory().getItemInOffHand(); to get the item.
  3. As recommended by the wiki, you should use a BukkitRunnable instead of Scheduler#runTaskLater();
Horsey
  • 151
  • 1
  • 13
0

Magic.getInstance() is a static attempt to refer to a main class called Magic in the original user's own plugin. To use this code in your own plugin, just replace Magic.getInstance() with a reference to the proper instance of your own main class.

If this code is in your main class already, you can simply use this like so: Bukkit.getScheduler().runTaskLater(this, new Runnable(){...}

Otherwise, you can pass your main class into the class constructor like so, where "Main" is the name of your main class:

Main plugin;
public class SecondaryClass {
    SecondaryClass(Main plugin) {
        this.plugin = plugin;
    }
}

...and then refer to it like so: Bukkit.getScheduler().runTaskLater(plugin, new Runnable(){...}

Randall Arms
  • 407
  • 1
  • 5
  • 22