-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
151 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...pring-bukkit/src/main/java/kr/junhyung/springbukkit/listener/BukkitListenerRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package kr.junhyung.springbukkit.listener; | ||
|
||
public interface BukkitListenerRegistrar { | ||
void registerEventHandler(Object listenerInstance); | ||
} |
71 changes: 71 additions & 0 deletions
71
...bukkit/src/main/java/kr/junhyung/springbukkit/listener/SimpleBukkitListenerRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package kr.junhyung.springbukkit.listener; | ||
|
||
import org.bukkit.event.*; | ||
import org.bukkit.plugin.EventExecutor; | ||
import org.bukkit.plugin.Plugin; | ||
import org.bukkit.plugin.PluginManager; | ||
import org.springframework.aop.framework.AopProxyUtils; | ||
import org.springframework.core.MethodIntrospector; | ||
import org.springframework.core.annotation.AnnotatedElementUtils; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Parameter; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class SimpleBukkitListenerRegistrar implements BukkitListenerRegistrar { | ||
private final Plugin plugin; | ||
private final PluginManager pluginManager; | ||
|
||
public SimpleBukkitListenerRegistrar(Plugin plugin, PluginManager pluginManager) { | ||
this.plugin = plugin; | ||
this.pluginManager = pluginManager; | ||
} | ||
|
||
public void registerEventHandler(Object listenerInstance) { | ||
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(listenerInstance); | ||
Set<Method> annotatedMethods = MethodIntrospector.selectMethods(targetClass, (Method method) -> AnnotatedElementUtils.isAnnotated(method, EventHandler.class)); | ||
if (annotatedMethods.isEmpty()) { | ||
return; | ||
} | ||
for (Method method : annotatedMethods) { | ||
List<Parameter> methodParameters = Arrays.stream(method.getParameters()).toList(); | ||
if (methodParameters.size() != 1) { | ||
throw new IllegalStateException("A event handler can only have one event parameter."); | ||
} | ||
Parameter methodParameter = methodParameters.stream().findFirst().get(); | ||
if (methodParameter.getType().isAssignableFrom(Event.class)) { | ||
throw new IllegalStateException("first parameter of event handler must be event."); | ||
} | ||
@SuppressWarnings("unchecked") | ||
Class<? extends Event> event = (Class<? extends Event>) methodParameter.getType(); | ||
|
||
EventHandler eventHandler = AnnotationUtils.getAnnotation(method, EventHandler.class); | ||
EventExecutor eventExecutor = bakeEventExecutor(listenerInstance, event, method); | ||
Objects.requireNonNull(eventHandler); | ||
registerEventHandler(event, eventHandler, plugin, eventExecutor); | ||
} | ||
} | ||
|
||
private void registerEventHandler(Class<? extends Event> event, EventHandler eventHandler, Plugin plugin, EventExecutor eventExecutor) { | ||
EventPriority priority = eventHandler.priority(); | ||
boolean ignoreCancelled = eventHandler.ignoreCancelled(); | ||
pluginManager.registerEvent(event, new Listener() {}, priority, eventExecutor, plugin, ignoreCancelled); | ||
} | ||
|
||
private static EventExecutor bakeEventExecutor(Object listenerInstance, Class<? extends Event> eventClass, Method method) { | ||
return (unused, event) -> { | ||
try { | ||
if (eventClass.isAssignableFrom(event.getClass())) { | ||
method.invoke(listenerInstance, event); | ||
} | ||
} catch (InvocationTargetException | IllegalAccessException exception) { | ||
throw new EventException(exception.getCause()); | ||
} | ||
}; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...t/spring-bukkit/src/main/java/kr/junhyung/springbukkit/serializer/BukkitDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package kr.junhyung.springbukkit.serializer; | ||
|
||
import org.bukkit.configuration.serialization.ConfigurationSerializable; | ||
import org.bukkit.util.io.BukkitObjectInputStream; | ||
import org.springframework.core.serializer.Deserializer; | ||
import org.springframework.lang.NonNull; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class BukkitDeserializer implements Deserializer<ConfigurationSerializable> { | ||
@Override | ||
public @NonNull ConfigurationSerializable deserialize(@NonNull InputStream inputStream) throws IOException { | ||
try (BukkitObjectInputStream bukkitObjectInputStream = new BukkitObjectInputStream(inputStream)) { | ||
return (ConfigurationSerializable) bukkitObjectInputStream.readObject(); | ||
} catch (ClassNotFoundException exception) { | ||
throw new IOException(exception); | ||
} | ||
} | ||
|
||
@Override | ||
public @NonNull ConfigurationSerializable deserializeFromByteArray(@NonNull byte[] serialized) throws IOException { | ||
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serialized)) { | ||
return deserialize(byteArrayInputStream); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
...kkit/src/main/java/kr/junhyung/springbukkit/serializer/BukkitSerializerConfiguration.java
This file was deleted.
Oops, something went wrong.
5 changes: 2 additions & 3 deletions
5
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
kr.junhyung.springbukkit.BukkitAutoConfiguration | ||
kr.junhyung.springbukkit.listener.BukkitListenerConfiguration | ||
kr.junhyung.springbukkit.serializer.BukkitSerializerConfiguration | ||
kr.junhyung.springbukkit.SpringBukkitAutoConfiguration | ||
kr.junhyung.springbukkit.listener.BukkitListenerAutoConfiguration |