Skip to content

Commit

Permalink
feat: Реализация новых событий
Browse files Browse the repository at this point in the history
- Добавлено событие EntityDeath.
- Добавлены события ExpAdd и ExpLevelAdd.
- Добавлено событие EntityPickUpDrop.
- Добавлено событие BlockChanged (требуется уточнение).
- Добавлено событие ExpOrbsSpawned.
- Исправлены идентификаторы причин урона для EntityHurt.
- Событие Explosion может быть вызвано и для блоков.
  • Loading branch information
MaXFeeD committed Dec 25, 2023
1 parent 58e9ee0 commit b45ffd1
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 93 deletions.
32 changes: 19 additions & 13 deletions src/main/java/com/reider745/api/CallbackHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ private static class ThreadRegion extends Thread {
private final ArrayList<ICallbackApply> applys = new ArrayList<>();

private final Level getRegion() {
if(region == null)
if (region == null) {
System.out.println("isNull: true");
}
return region;
}

Expand Down Expand Up @@ -66,21 +67,22 @@ public static void registerCallback(Type type) {
}

public static void init() {
for (Type type : Type.values())
for (Type type : Type.values()) {
registerCallback(type);
}
}

private static class ThreadCallback extends Thread {
protected final ICallbackApply apply;

protected boolean prevent_status;
protected boolean isPrevented;

public boolean isPrevent() {
return prevent_status;
return isPrevented;
}

public void prevent() {
this.prevent_status = true;
this.isPrevented = true;
}

public ThreadCallback(ICallbackApply apply) {
Expand All @@ -100,14 +102,16 @@ public ThreadCallbackEvent(Event event, ICallbackApply apply, boolean isPrevente

@Override
public void run() {
if (!event.isCancelled() || isPrevented){
if (isPrevented || !event.isCancelled()) {
apply.apply();
if(isPrevent())
if (isPrevent()) {
event.setCancelled();
}
}
}
}

@SuppressWarnings("unused")
private static class ThreadCallbackController extends ThreadCallback {
protected final HookController event;

Expand All @@ -133,30 +137,32 @@ public static void apply(Event event, ICallbackApply apply, boolean isPrevented)
Thread thread = new ThreadCallbackEvent(event, apply, isPrevented);
thread.setName(event.getEventName());
thread.start();

while (thread.isAlive()) {
java.lang.Thread.yield();
}
}

public static void prevent() {
Thread thread = Thread.currentThread();

if (thread instanceof ThreadCallback threadCallback)
if (thread instanceof ThreadCallback threadCallback) {
threadCallback.prevent();
}
}

public static boolean isPrevent() {
Thread thread = Thread.currentThread();

if (thread instanceof ThreadCallback threadCallback)
if (thread instanceof ThreadCallback threadCallback) {
return threadCallback.isPrevent();
}
return false;
}

public static Level getForCurrentThread() {
Thread thread = Thread.currentThread();

if (thread instanceof ThreadRegion threadCallback)
if (thread instanceof ThreadRegion threadCallback) {
return threadCallback.getRegion();
}
return null;
}
}
Loading

0 comments on commit b45ffd1

Please sign in to comment.