Reputation: 809
I am in the process of creating a spigot plugin (using Java and Bukkit language) that will allow me to store the coordinates of players in minecraft (Java edition) in real time.
I want to use a 'scheduler' to do this with a 'repeating task' structure.
I have the following code:
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin)this, (Runnable)new Runnable() {
@Override
public void run() {
if (main.this.stopRepeater) {
main.this.logToFile(thePlayer, thePlayer.getLocation());
}
}
}, 0L, 20L);
}
However, I am not 100% sure what role the '@Override' and 'new Runnable()' parts of the code are actually playing here. This is the first time I'm using Java/Bukkit/Spigot for a project.
I am aware that 'new Runnable()' is used to create a new runnable thread of code, and that '@Override' method is used to override the parent class - but what is the 'parent class' in this case above?
I haven't been able to find a clear explanation for this as different sites say different things.
I would be so grateful if somebody could clarify this for me!
Upvotes: 0
Views: 334
Reputation: 3605
I am in the process of creating a spigot plugin (using Java and Bukkit language)
Bukkit is not a language; it's an API for Minecraft. Spigot is another API for Minecraft built on top of Bukkit.
The @Override
line is an annotation and has no functional bearing on the code.
From the docs:
@Override annotation informs the compiler that the element is meant to override an element declared in a superclass.
You are overriding the run()
method from Runnable. The Runnable class is an interface, and the run()
method is abstract, meaning it has no body. Therefore, its implementation must be defined by the subclass unless itself is abstract.
The code you are attempting to execute is dangerous as it will perform a File I/O operation every 20 ticks (1 second) on the main thread (sync). This will likely lead to lag spikes and harm the user's experience.
Spigot does a decent beginner's tutorial, which you should read. In addition, there are plenty of other resources online to learn about Java programming, which you should utilise.
Upvotes: 2