This commit is contained in:
LOOHP
2021-04-06 21:14:05 +08:00
parent 05cfdb73a9
commit 6d14314bd9
126 changed files with 191394 additions and 0 deletions
@@ -0,0 +1,9 @@
package com.loohp.limbo.events;
public interface Cancellable {
public void setCancelled(boolean cancelled);
public boolean isCancelled();
}
@@ -0,0 +1,5 @@
package com.loohp.limbo.events;
public abstract class Event {
}
@@ -0,0 +1,14 @@
package com.loohp.limbo.events;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventHandler {
EventPriority priority() default EventPriority.NORMAL;
}
@@ -0,0 +1,37 @@
package com.loohp.limbo.events;
public enum EventPriority {
LOWEST(0),
LOW(1),
NORMAL(2),
HIGH(3),
HIGHEST(4),
MONITOR(5);
int order;
EventPriority(int order) {
this.order = order;
}
public int getOrder() {
return order;
}
public static EventPriority getByOrder(int order) {
for (EventPriority each : EventPriority.values()) {
if (each.getOrder() == order) {
return each;
}
}
return null;
}
public static EventPriority[] getPrioritiesInOrder() {
EventPriority[] array = new EventPriority[EventPriority.values().length];
for (int i = 0; i < array.length; i++) {
array[i] = EventPriority.getByOrder(i);
}
return array;
}
}
@@ -0,0 +1,58 @@
package com.loohp.limbo.events;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import com.loohp.limbo.plugins.LimboPlugin;
public class EventsManager {
private List<ListenerPair> listeners;
public EventsManager() {
listeners = new ArrayList<>();
}
public <T extends Event> T callEvent(T event) {
for (EventPriority priority : EventPriority.getPrioritiesInOrder()) {
for (ListenerPair entry : listeners) {
Listener listener = entry.listener;
for (Method method : listener.getClass().getMethods()) {
if (method.isAnnotationPresent(EventHandler.class)) {
if (method.getAnnotation(EventHandler.class).priority().equals(priority)) {
if (method.getParameterCount() == 1 && method.getParameterTypes()[0].equals(event.getClass())) {
try {
method.invoke(listener, event);
} catch (Exception e) {
System.err.println("Error while passing " + event.getClass().getCanonicalName() + " to the plugin \"" + entry.plugin.getName() + "\"");
e.printStackTrace();
}
}
}
}
}
}
}
return event;
}
public void registerEvents(LimboPlugin plugin, Listener listener) {
listeners.add(new ListenerPair(plugin, listener));
}
public void unregisterAllListeners(LimboPlugin plugin) {
listeners.removeIf(each -> each.plugin.equals(plugin));
}
protected static class ListenerPair {
public LimboPlugin plugin;
public Listener listener;
public ListenerPair(LimboPlugin plugin, Listener listener) {
this.plugin = plugin;
this.listener = listener;
}
}
}
@@ -0,0 +1,5 @@
package com.loohp.limbo.events;
public interface Listener {
}
@@ -0,0 +1,45 @@
package com.loohp.limbo.events.player;
import com.loohp.limbo.events.Cancellable;
import com.loohp.limbo.player.Player;
public class PlayerChatEvent extends PlayerEvent implements Cancellable {
private String prefix;
private String message;
private boolean cancelled;
public PlayerChatEvent(Player player, String prefix, String message, boolean cancelled) {
super(player);
this.prefix = prefix;
this.message = message;
this.cancelled = cancelled;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public boolean isCancelled() {
return cancelled;
}
}
@@ -0,0 +1,18 @@
package com.loohp.limbo.events.player;
import com.loohp.limbo.events.Event;
import com.loohp.limbo.player.Player;
public class PlayerEvent extends Event {
private Player player;
public PlayerEvent(Player player) {
this.player = player;
}
public Player getPlayer() {
return player;
}
}
@@ -0,0 +1,22 @@
package com.loohp.limbo.events.player;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.player.Player;
public class PlayerJoinEvent extends PlayerEvent {
private Location spawnLocation;
public PlayerJoinEvent(Player player, Location spawnLoc) {
super(player);
spawnLocation = spawnLoc;
}
public Location getSpawnLocation() {
return spawnLocation;
}
public void setSpawnLocation(Location spawnLocation) {
this.spawnLocation = spawnLocation;
}
}
@@ -0,0 +1,43 @@
package com.loohp.limbo.events.player;
import com.loohp.limbo.events.Cancellable;
import com.loohp.limbo.events.Event;
import com.loohp.limbo.server.ClientConnection;
import net.md_5.bungee.api.chat.BaseComponent;
public class PlayerLoginEvent extends Event implements Cancellable {
private ClientConnection connection;
private boolean cancelled;
private BaseComponent[] cancelReason;
public PlayerLoginEvent(ClientConnection connection, boolean cancelled, BaseComponent... cancelReason) {
this.connection = connection;
this.cancelled = cancelled;
this.cancelReason = cancelReason;
}
public ClientConnection getConnection() {
return connection;
}
public BaseComponent[] getCancelReason() {
return cancelReason;
}
public void setCancelReason(BaseComponent... cancelReason) {
this.cancelReason = cancelReason;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public boolean isCancelled() {
return cancelled;
}
}
@@ -0,0 +1,87 @@
package com.loohp.limbo.events.player;
import com.loohp.limbo.events.Cancellable;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.player.Player;
/**
* Holds information for player movement events
*/
public class PlayerMoveEvent extends PlayerEvent implements Cancellable {
private boolean cancel = false;
private Location from;
private Location to;
public PlayerMoveEvent(Player player, Location from, Location to) {
super(player);
this.from = from;
this.to = to;
}
/**
* Gets the cancellation state of this event. A cancelled event will not
* be executed in the server, but will still pass to other plugins
* <p>
* If a move or teleport event is cancelled, the player will be moved or
* teleported back to the Location as defined by getFrom(). This will not
* fire an event
*
* @return true if this event is cancelled
*/
@Override
public boolean isCancelled() {
return cancel;
}
/**
* Sets the cancellation state of this event. A cancelled event will not
* be executed in the server, but will still pass to other plugins
* <p>
* If a move or teleport event is cancelled, the player will be moved or
* teleported back to the Location as defined by getFrom(). This will not
* fire an event
*
* @param cancel true if you wish to cancel this event
*/
@Override
public void setCancelled(boolean cancel) {
this.cancel = cancel;
}
/**
* Gets the location this player moved from
*
* @return Location the player moved from
*/
public Location getFrom() {
return from;
}
/**
* Sets the location to mark as where the player moved from
*
* @param from New location to mark as the players previous location
*/
public void setFrom(Location from) {
this.from = from;
}
/**
* Gets the location this player moved to
*
* @return Location the player moved to
*/
public Location getTo() {
return to;
}
/**
* Sets the location that this player will move to
*
* @param to New Location this player will move to
*/
public void setTo(Location to) {
this.to = to;
}
}
@@ -0,0 +1,11 @@
package com.loohp.limbo.events.player;
import com.loohp.limbo.player.Player;
public class PlayerQuitEvent extends PlayerEvent {
public PlayerQuitEvent(Player player) {
super(player);
}
}
@@ -0,0 +1,33 @@
package com.loohp.limbo.events.player;
import com.loohp.limbo.events.Cancellable;
import com.loohp.limbo.player.Player;
public class PlayerSelectedSlotChangeEvent extends PlayerEvent implements Cancellable {
private boolean cancel = false;
private byte slot;
public PlayerSelectedSlotChangeEvent(Player player, byte slot) {
super(player);
this.slot = slot;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancel = cancelled;
}
@Override
public boolean isCancelled() {
return cancel;
}
public byte getSlot() {
return slot;
}
public void setSlot(byte slot) {
this.slot = slot;
}
}
@@ -0,0 +1,12 @@
package com.loohp.limbo.events.player;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.player.Player;
public class PlayerTeleportEvent extends PlayerMoveEvent {
public PlayerTeleportEvent(Player player, Location from, Location to) {
super(player, from, to);
}
}
@@ -0,0 +1,82 @@
package com.loohp.limbo.events.status;
import java.awt.image.BufferedImage;
import com.loohp.limbo.events.Event;
import com.loohp.limbo.server.ClientConnection;
import net.md_5.bungee.api.chat.BaseComponent;
public class StatusPingEvent extends Event {
private ClientConnection connection;
private String version;
private int protocol;
private BaseComponent[] motd;
private int maxPlayers;
private int playersOnline;
private BufferedImage favicon;
public StatusPingEvent(ClientConnection connection, String version, int protocol, BaseComponent[] motd, int maxPlayers, int playersOnline, BufferedImage favicon) {
this.connection = connection;
this.version = version;
this.protocol = protocol;
this.motd = motd;
this.maxPlayers = maxPlayers;
this.playersOnline = playersOnline;
this.favicon = favicon;
}
public ClientConnection getConnection() {
return connection;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public int getProtocol() {
return protocol;
}
public void setProtocol(int protocol) {
this.protocol = protocol;
}
public BaseComponent[] getMotd() {
return motd;
}
public void setMotd(BaseComponent[] motd) {
this.motd = motd;
}
public int getMaxPlayers() {
return maxPlayers;
}
public void setMaxPlayers(int maxPlayers) {
this.maxPlayers = maxPlayers;
}
public int getPlayersOnline() {
return playersOnline;
}
public void setPlayersOnline(int playersOnline) {
this.playersOnline = playersOnline;
}
public BufferedImage getFavicon() {
return favicon;
}
public void setFavicon(BufferedImage favicon) {
this.favicon = favicon;
}
}