-
Notifications
You must be signed in to change notification settings - Fork 37
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
58 changed files
with
1,719 additions
and
403 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
51 changes: 51 additions & 0 deletions
51
src/main/java/dev/dubhe/anvilcraft/api/events/EventManager.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,51 @@ | ||
package dev.dubhe.anvilcraft.api.events; | ||
|
||
import dev.dubhe.anvilcraft.AnvilCraft; | ||
import dev.dubhe.anvilcraft.event.SubscribeEvent; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Vector; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Consumer; | ||
|
||
@SuppressWarnings("unused") | ||
public class EventManager { | ||
public final Map<Class<?>, List<Consumer<?>>> EVENT_LISTENER = new ConcurrentHashMap<>(); | ||
|
||
public <E> void listen(Class<E> event, Consumer<E> trigger) { | ||
List<Consumer<?>> triggers = EVENT_LISTENER.getOrDefault(event, new Vector<>()); | ||
triggers.add(trigger); | ||
EVENT_LISTENER.putIfAbsent(event, triggers); | ||
} | ||
|
||
@SuppressWarnings("all") | ||
public <E> void post(E event) { | ||
List<Consumer<?>> triggers = EVENT_LISTENER.getOrDefault(event.getClass(), new Vector<>()); | ||
for (Consumer<?> trigger : triggers) { | ||
((Consumer<E>) trigger).accept(event); | ||
} | ||
} | ||
|
||
public void register(@NotNull Object object) { | ||
for (Method method : object.getClass().getMethods()) { | ||
if (method.getParameterCount() != 1) continue; | ||
SubscribeEvent annotation = method.getAnnotation(SubscribeEvent.class); | ||
if (null == annotation) continue; | ||
Class<?> event = method.getParameterTypes()[0]; | ||
Consumer<?> trigger = (obj) -> { | ||
try { | ||
method.invoke(object, obj); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
AnvilCraft.LOGGER.printStackTrace(e); | ||
} | ||
}; | ||
List<Consumer<?>> triggers = EVENT_LISTENER.getOrDefault(event, new Vector<>()); | ||
triggers.add(trigger); | ||
EVENT_LISTENER.putIfAbsent(event, triggers); | ||
} | ||
} | ||
} |
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
20 changes: 0 additions & 20 deletions
20
src/main/java/dev/dubhe/anvilcraft/data/crafting/ModRecipeTypes.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.