Reputation: 473
I'm trying to call a return type method from another class but I keep getting the error that it is undefined for the type of the class. Here is the code I'm trying to call:
public MyTask() {
id = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin,this,plugin.getConfig().getInt("StunDuration") * 20);
if(id == -1)
{
plugin.getLogger().warning("BLARG");
}
}
How would I call that method from another class?
public class DamageListener implements Listener{
private antirelog plugin;
public DamageListener (antirelog plugin) {
this.plugin = plugin;
}
Player realplayer1;
Player realplayer2;
boolean playeradd = false;
private MyTask task;
public static boolean isDamaged = false;
public static boolean timerTask = true;
static Set<Player> Damagelist = Collections.newSetFromMap(new WeakHashMap<Player,Boolean>());
@EventHandler
public void damage (EntityDamageEvent event) {
Entity victim = event.getEntity();
if (event instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent edbeEvent = (EntityDamageByEntityEvent) event;
if(edbeEvent.getDamager() instanceof Player && victim instanceof Player) {
EntityDamageByEntityEvent edbeEvent1 = (EntityDamageByEntityEvent) event;
Entity attacker = edbeEvent1.getDamager();
Player player = (Player) victim;
realplayer1 = player;
Player player2 = (Player) attacker;
realplayer2 = player2;
if(Damagelist.contains(realplayer1) || Damagelist.contains(realplayer2)) {
isDamaged = true;
timerTask = false;
System.out.println("Cancel");
MyTask();
} else {
Damagelist.add(realplayer1);
Damagelist.add(realplayer2);
isDamaged = true;
Pause ps = new Pause(plugin);
MyTask();
}
}
}
Heres the MyTask class.
public class MyTask implements Runnable
{
private antirelog plugin;
private int id = -1;
/**
* Generic constructor
* @param Plugin task is associated with
* @return
*/
public MyTask (antirelog plugin) {
this.plugin = plugin;
}
public void MyTask() {
id = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin,this,plugin.getConfig().getInt("StunDuration") * 20);
if(id == -1)
{
plugin.getLogger().warning("BLARG");
}
}
/**
* Do stuff when scheduler tells task to run
*/
@Override
public void run()
{
if (DamageListener.timerTask == true) {
DamageListener dl = new DamageListener(plugin);
dl.Then();
}
else if (DamageListener.timerTask == false) {
plugin.getServer().getScheduler().cancelTask(id);
DamageListener.timerTask = true;
}
}
/**
* Remove task from scheduler
* @return True if successfully stopped. Else false.
*/
public boolean stopTask()
{
if(id != -1)
{
plugin.getServer().getScheduler().cancelTask(id);
return true;
}
return false;
}
}
Upvotes: 0
Views: 195
Reputation: 26720
Since MyTask()
is a constructor, for the class MyTask
, to be instantiated in another class, it must be prefixed with the new
keyword, like so:
...
isDamaged = true;
Pause ps = new Pause(plugin);
task = new MyTask(plugin); //Note the `new` keyword
...
Also lets fix some other things too.
public MyTask (antirelog plugin)
{
this.plugin = plugin;
if (plugin != null) {
id = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin,this,plugin.getConfig().getInt("StunDuration") * 20);
if (id == -1)
{
plugin.getLogger().warning("BLARG");
}
}
}
//public MyTask() //Remove the void, this is a constructor
//{
// this(null); //Call the extended constructor
//}
Upvotes: 2