First two hooks working, updated sample

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2010-12-26 02:20:05 +00:00
parent 42ef5c31b7
commit a3ff0f1177
8 changed files with 53 additions and 23 deletions

View file

@ -0,0 +1,28 @@
package com.dinnerbone.bukkit.sample;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerQuitEvent;
/**
* Handle events for all Player related events
* @author Dinnerbone
*/
public class SamplePlayerListener extends PlayerListener {
private final SamplePlugin plugin;
public SamplePlayerListener(SamplePlugin instance) {
plugin = instance;
}
@Override
public void onPlayerJoin(PlayerEvent event) {
System.out.println(event.getPlayer().getName() + " joined the server! :D");
}
@Override
public void onPlayerQuit(PlayerQuitEvent event) {
System.out.println(event.getPlayer().getName() + " left the server! :'(");
}
}

View file

@ -3,6 +3,8 @@ package com.dinnerbone.bukkit.sample;
import java.io.File; import java.io.File;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.player.PlayerEvent.EventType;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -13,9 +15,14 @@ import org.bukkit.plugin.java.JavaPlugin;
* @author Dinnerbone * @author Dinnerbone
*/ */
public class SamplePlugin extends JavaPlugin { public class SamplePlugin extends JavaPlugin {
private final SamplePlayerListener playerListener = new SamplePlayerListener(this);
public SamplePlugin(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader) { public SamplePlugin(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader) {
super(pluginLoader, instance, desc, plugin, cLoader); super(pluginLoader, instance, desc, plugin, cLoader);
System.out.println("Johnny five is alive!"); System.out.println("Johnny five is alive!");
registerEvents();
} }
public void onDisable() { public void onDisable() {
@ -25,4 +32,9 @@ public class SamplePlugin extends JavaPlugin {
public void onEnable() { public void onEnable() {
System.out.println("Hello world!"); System.out.println("Hello world!");
} }
private void registerEvents() {
getServer().getPluginManager().registerEvent(EventType.Join, playerListener, Priority.Normal, this);
getServer().getPluginManager().registerEvent(EventType.Quit, playerListener, Priority.Normal, this);
}
} }

View file

@ -8,10 +8,10 @@ import org.bukkit.event.Event;
/** /**
* Represents a player related event * Represents a player related event
*/ */
public abstract class PlayerEvent extends Event { public class PlayerEvent extends Event {
private final Player player; private final Player player;
protected PlayerEvent(final Server server, final Player who) { public PlayerEvent(final Server server, final Player who) {
super(server); super(server);
player = who; player = who;
} }

View file

@ -1,14 +0,0 @@
package org.bukkit.event.player;
import org.bukkit.Player;
import org.bukkit.Server;
/**
* Handles all event arguments in relation to a player joining a server
*/
public class PlayerJoinEvent extends PlayerEvent {
public PlayerJoinEvent(final Server server, final Player player) {
super(server, player);
}
}

View file

@ -6,19 +6,21 @@ import org.bukkit.event.Listener;
/** /**
* Handles all events thrown in relation to a Player * Handles all events thrown in relation to a Player
*/ */
public abstract class PlayerListener implements Listener { public class PlayerListener implements Listener {
private PlayerListener() { public PlayerListener() {
} }
/** /**
* Called when a player joins a server * Called when a player joins a server
* @param event Relevant event details * @param event Relevant event details
*/ */
public abstract void onPlayerJoin(PlayerJoinEvent event); public void onPlayerJoin(PlayerEvent event) {
}
/** /**
* Called when a player leaves a server * Called when a player leaves a server
* @param event Relevant event details * @param event Relevant event details
*/ */
public abstract void onPlayerQuit(PlayerQuitEvent event); public void onPlayerQuit(PlayerQuitEvent event) {
}
} }

View file

@ -15,7 +15,6 @@ import java.util.regex.Matcher;
import org.bukkit.Server; import org.bukkit.Server;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.event.Event.Priority; import org.bukkit.event.Event.Priority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerListener;
@ -202,6 +201,9 @@ public final class SimplePluginManager implements PluginManager {
position++; position++;
} }
} else {
listeners = new ArrayList<RegisteredListener>();
playerListeners.put(type, listeners);
} }
listeners.add(position, new RegisteredListener(listener, priority, plugin)); listeners.add(position, new RegisteredListener(listener, priority, plugin));

View file

@ -11,7 +11,7 @@ import org.bukkit.plugin.PluginLoader;
* Represents a Java plugin * Represents a Java plugin
*/ */
public abstract class JavaPlugin implements Plugin { public abstract class JavaPlugin implements Plugin {
private boolean isEnabled = false; private boolean isEnabled = true;
private final PluginLoader loader; private final PluginLoader loader;
private final Server server; private final Server server;
private final File file; private final File file;

View file

@ -82,7 +82,7 @@ public final class JavaPluginLoader implements PluginLoader {
switch (type) { switch (type) {
case Join: case Join:
trueListener.onPlayerJoin((PlayerJoinEvent)event); trueListener.onPlayerJoin((PlayerEvent)event);
break; break;
case Quit: case Quit:
trueListener.onPlayerQuit((PlayerQuitEvent)event); trueListener.onPlayerQuit((PlayerQuitEvent)event);