Skip to content

Commit

Permalink
Cache Event Methods, Polish Last PR
Browse files Browse the repository at this point in the history
  • Loading branch information
LOOHP committed Aug 19, 2021
1 parent a0f592d commit 7d82082
Show file tree
Hide file tree
Showing 18 changed files with 261 additions and 109 deletions.
17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.loohp</groupId>
<artifactId>Limbo</artifactId>
<name>Limbo</name>
<version>0.5.3-ALPHA</version>
<version>0.6.0-ALPHA</version>

<description>Standalone Limbo Minecraft Server.</description>
<url>https://github.com/LOOHP/Limbo</url>
Expand All @@ -25,6 +25,21 @@
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.java</exclude>
<exclude>**/*.ttf</exclude>
<exclude>**/*.jar</exclude>
<exclude>**/*.schem</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.ttf</include>
<include>**/*.jar</include>
<include>**/*.schem</include>
</includes>
</resource>
</resources>
<plugins>
Expand Down
45 changes: 26 additions & 19 deletions src/main/java/com/loohp/limbo/Limbo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
Expand All @@ -39,13 +36,9 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.loohp.limbo.events.EventsManager;
import com.loohp.limbo.server.ServerConnection;
import com.loohp.limbo.server.packets.Packet;
import com.loohp.limbo.server.packets.PacketIn;
import com.loohp.limbo.server.packets.PacketOut;
import com.loohp.limbo.commands.CommandSender;
import com.loohp.limbo.consolegui.GUI;
import com.loohp.limbo.events.EventsManager;
import com.loohp.limbo.file.ServerProperties;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.metrics.Metrics;
Expand All @@ -55,6 +48,10 @@
import com.loohp.limbo.plugins.PluginManager;
import com.loohp.limbo.scheduler.LimboScheduler;
import com.loohp.limbo.scheduler.Tick;
import com.loohp.limbo.server.ServerConnection;
import com.loohp.limbo.server.packets.Packet;
import com.loohp.limbo.server.packets.PacketIn;
import com.loohp.limbo.server.packets.PacketOut;
import com.loohp.limbo.utils.CustomStringUtils;
import com.loohp.limbo.utils.ImageUtils;
import com.loohp.limbo.utils.NetworkUtils;
Expand Down Expand Up @@ -296,11 +293,12 @@ public Limbo() throws IOException, ParseException, NumberFormatException, ClassN

File defaultCommandsJar = new File(pluginFolder, "LimboDefaultCmd.jar");
defaultCommandsJar.delete();
console.sendMessage("Downloading limbo default commands module from github...");
ReadableByteChannel rbc = Channels.newChannel(new URL("https://github.com/LOOHP/Limbo/raw/master/modules/LimboDefaultCmd.jar").openStream());
FileOutputStream fos = new FileOutputStream(defaultCommandsJar);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
console.sendMessage("Loading limbo default commands module...");
try (InputStream in = Limbo.class.getClassLoader().getResourceAsStream("LimboDefaultCmd.jar")) {
Files.copy(in, defaultCommandsJar.toPath());
} catch (IOException e) {
e.printStackTrace();
}

pluginManager = new PluginManager(pluginFolder);
try {
Expand Down Expand Up @@ -372,16 +370,25 @@ private World loadDefaultWorld() throws IOException {

if (!schem.exists()) {
console.sendMessage("Schemetic file " + properties.getSchemFileName() + " for world " + properties.getLevelName() + " not found!");
console.sendMessage("Creating default world...");
try (InputStream in = Limbo.class.getClassLoader().getResourceAsStream("spawn.schem")) {
Files.copy(in, schem.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}

try {
World world = Schematic.toWorld(properties.getLevelName().getKey(), Environment.fromNamespacedKey(properties.getLevelDimension()), (CompoundTag) NBTUtil.read(schem).getTag());
console.sendMessage("Loaded world " + properties.getLevelName() + "!");
return world;
} catch (Throwable e) {
console.sendMessage("Unable to load world " + properties.getSchemFileName() + "!");
e.printStackTrace();
console.sendMessage("Server will exit!");
System.exit(1);
return null;
}

World world = Schematic.toWorld(properties.getLevelName().getKey(), Environment.fromNamespacedKey(properties.getLevelDimension()), (CompoundTag) NBTUtil.read(schem).getTag());

console.sendMessage("Loaded world " + properties.getLevelName() + "!");

return world;
}

public void registerWorld(World world) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/loohp/limbo/events/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventHandler {

EventPriority priority() default EventPriority.NORMAL;

}
2 changes: 2 additions & 0 deletions src/main/java/com/loohp/limbo/events/EventPriority.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.loohp.limbo.events;

public enum EventPriority {

LOWEST(0),
LOW(1),
NORMAL(2),
Expand Down Expand Up @@ -34,4 +35,5 @@ public static EventPriority[] getPrioritiesInOrder() {
}
return array;
}

}
36 changes: 21 additions & 15 deletions src/main/java/com/loohp/limbo/events/EventsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,31 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.loohp.limbo.plugins.LimboPlugin;

public class EventsManager {

private List<ListenerPair> listeners;
private Map<Listener, RegisteredCachedListener> cachedListeners;

public EventsManager() {
listeners = new ArrayList<>();
cachedListeners = new ConcurrentHashMap<>();
}

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();
}
}
}
for (Entry<Listener, RegisteredCachedListener> entry : cachedListeners.entrySet()) {
for (Method method : entry.getValue().getListeners(event.getClass(), priority)) {
try {
method.invoke(entry.getKey(), event);
} catch (Exception e) {
System.err.println("Error while passing " + event.getClass().getCanonicalName() + " to the plugin \"" + entry.getValue().getPlugin().getName() + "\"");
e.printStackTrace();
}
}
}
Expand All @@ -39,10 +37,18 @@ public <T extends Event> T callEvent(T event) {

public void registerEvents(LimboPlugin plugin, Listener listener) {
listeners.add(new ListenerPair(plugin, listener));
cachedListeners.put(listener, new RegisteredCachedListener(plugin, listener));
}

public void unregisterAllListeners(LimboPlugin plugin) {
listeners.removeIf(each -> each.plugin.equals(plugin));
listeners.removeIf(each -> {
if (each.plugin.equals(plugin)) {
cachedListeners.remove(each.listener);
return true;
} else {
return false;
}
});
}

protected static class ListenerPair {
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/loohp/limbo/events/RegisteredCachedListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.loohp.limbo.events;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.loohp.limbo.plugins.LimboPlugin;

public class RegisteredCachedListener {

private LimboPlugin plugin;
private Map<Class<? extends Event>, Map<EventPriority, List<Method>>> listeners;

@SuppressWarnings("unchecked")
public RegisteredCachedListener(LimboPlugin plugin, Listener listener) {
this.plugin = plugin;
this.listeners = new ConcurrentHashMap<>();
for (Method method : listener.getClass().getMethods()) {
if (method.isAnnotationPresent(EventHandler.class) && method.getParameterCount() == 1 && Event.class.isAssignableFrom(method.getParameterTypes()[0])) {
Class<? extends Event> eventClass = (Class<? extends Event>) method.getParameterTypes()[0];
listeners.putIfAbsent(eventClass, new ConcurrentHashMap<>());
Map<EventPriority, List<Method>> mapping = listeners.get(eventClass);
EventPriority priority = method.getAnnotation(EventHandler.class).priority();
mapping.putIfAbsent(priority, new ArrayList<>());
List<Method> list = mapping.get(priority);
list.add(method);
}
}
}

public LimboPlugin getPlugin() {
return plugin;
}

public List<Method> getListeners(Class<? extends Event> eventClass, EventPriority priority) {
Map<EventPriority, List<Method>> mapping = listeners.get(eventClass);
if (mapping == null) {
return Collections.emptyList();
}
List<Method> list = mapping.get(priority);
return list == null ? Collections.emptyList() : Collections.unmodifiableList(list);
}

}
25 changes: 16 additions & 9 deletions src/main/java/com/loohp/limbo/file/ServerProperties.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package com.loohp.limbo.file;

import com.google.common.collect.Lists;
import com.loohp.limbo.Limbo;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.utils.GameMode;
import com.loohp.limbo.utils.NamespacedKey;
import com.loohp.limbo.world.World;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;

import javax.imageio.ImageIO;

import com.google.common.collect.Lists;
import com.loohp.limbo.Limbo;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.utils.GameMode;
import com.loohp.limbo.utils.NamespacedKey;
import com.loohp.limbo.world.World;

public class ServerProperties {

public static final String COMMENT = "For explaination of what each of the options does, please visit:\nhttps://github.com/LOOHP/Limbo/blob/master/src/main/resources/server.properties";
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/loohp/limbo/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import java.util.UUID;

import com.loohp.limbo.Limbo;
import com.loohp.limbo.commands.CommandSender;
import com.loohp.limbo.entity.DataWatcher;
import com.loohp.limbo.entity.DataWatcher.WatchableField;
import com.loohp.limbo.entity.DataWatcher.WatchableObjectType;
import com.loohp.limbo.entity.EntityType;
import com.loohp.limbo.entity.LivingEntity;
import com.loohp.limbo.events.player.PlayerChatEvent;
import com.loohp.limbo.events.player.PlayerTeleportEvent;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.server.ClientConnection;
import com.loohp.limbo.server.packets.PacketPlayOutChat;
import com.loohp.limbo.server.packets.PacketPlayOutGameState;
import com.loohp.limbo.server.packets.PacketPlayOutHeldItemChange;
import com.loohp.limbo.server.packets.PacketPlayOutPositionAndLook;
import com.loohp.limbo.server.packets.PacketPlayOutRespawn;
import com.loohp.limbo.commands.CommandSender;
import com.loohp.limbo.entity.DataWatcher;
import com.loohp.limbo.entity.EntityType;
import com.loohp.limbo.entity.LivingEntity;
import com.loohp.limbo.entity.DataWatcher.WatchableField;
import com.loohp.limbo.entity.DataWatcher.WatchableObjectType;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.utils.GameMode;

import net.md_5.bungee.api.chat.BaseComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import java.util.stream.Collectors;

import com.loohp.limbo.Limbo;
import com.loohp.limbo.entity.Entity;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.server.packets.PacketPlayOutEntityDestroy;
import com.loohp.limbo.server.packets.PacketPlayOutEntityMetadata;
import com.loohp.limbo.server.packets.PacketPlayOutLightUpdate;
import com.loohp.limbo.server.packets.PacketPlayOutMapChunk;
import com.loohp.limbo.server.packets.PacketPlayOutSpawnEntity;
import com.loohp.limbo.server.packets.PacketPlayOutSpawnEntityLiving;
import com.loohp.limbo.server.packets.PacketPlayOutUnloadChunk;
import com.loohp.limbo.entity.Entity;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.world.World;

import net.querz.mca.Chunk;
Expand Down
Loading

0 comments on commit 7d82082

Please sign in to comment.