Delete EventGroup.java, make InstanceEvent an interface

This commit is contained in:
TheMode 2021-06-02 09:21:13 +02:00
parent 0b6c7b0b0e
commit 096e1de9b5
8 changed files with 62 additions and 66 deletions

View File

@ -1,27 +0,0 @@
package net.minestom.server.event;
import net.minestom.server.event.handler.EventHandler;
import org.jetbrains.annotations.NotNull;
public class EventGroup implements ListenerAttach {
private final EventListener<?>[] listeners;
protected EventGroup(@NotNull EventListener<?>... listeners) {
this.listeners = listeners;
}
@Override
public void attachTo(@NotNull EventHandler handler) {
for (EventListener<?> listener : listeners) {
listener.attachTo(handler);
}
}
@Override
public void detachFrom(@NotNull EventHandler handler) {
for (EventListener<?> listener : listeners) {
listener.detachFrom(handler);
}
}
}

View File

@ -1,23 +0,0 @@
package net.minestom.server.event;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
public class InstanceEvent implements Event {
protected final Instance instance;
public InstanceEvent(@NotNull Instance instance) {
this.instance = instance;
}
/**
* Gets the instance.
*
* @return instance
*/
@NotNull
public Instance getInstance() {
return instance;
}
}

View File

@ -2,7 +2,8 @@ package net.minestom.server.event.instance;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.event.CancellableEvent; import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.InstanceEvent; import net.minestom.server.event.trait.EntityEvent;
import net.minestom.server.event.trait.InstanceEvent;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -10,17 +11,23 @@ import org.jetbrains.annotations.NotNull;
* Called by an Instance when an entity is added to it. * Called by an Instance when an entity is added to it.
* Can be used attach data. * Can be used attach data.
*/ */
public class AddEntityToInstanceEvent extends InstanceEvent implements CancellableEvent { public class AddEntityToInstanceEvent implements InstanceEvent, EntityEvent, CancellableEvent {
private final Instance instance;
private final Entity entity; private final Entity entity;
private boolean cancelled; private boolean cancelled;
public AddEntityToInstanceEvent(@NotNull Instance instance, @NotNull Entity entity) { public AddEntityToInstanceEvent(@NotNull Instance instance, @NotNull Entity entity) {
super(instance); this.instance = instance;
this.entity = entity; this.entity = entity;
} }
@Override
public @NotNull Instance getInstance() {
return instance;
}
/** /**
* Entity being added. * Entity being added.
* *

View File

@ -1,22 +1,28 @@
package net.minestom.server.event.instance; package net.minestom.server.event.instance;
import net.minestom.server.event.InstanceEvent; import net.minestom.server.event.trait.InstanceEvent;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Called when a chunk in an instance is loaded. * Called when a chunk in an instance is loaded.
*/ */
public class InstanceChunkLoadEvent extends InstanceEvent { public class InstanceChunkLoadEvent implements InstanceEvent {
private final Instance instance;
private final int chunkX, chunkZ; private final int chunkX, chunkZ;
public InstanceChunkLoadEvent(@NotNull Instance instance, int chunkX, int chunkZ) { public InstanceChunkLoadEvent(@NotNull Instance instance, int chunkX, int chunkZ) {
super(instance); this.instance = instance;
this.chunkX = chunkX; this.chunkX = chunkX;
this.chunkZ = chunkZ; this.chunkZ = chunkZ;
} }
@Override
public @NotNull Instance getInstance() {
return instance;
}
/** /**
* Gets the chunk X. * Gets the chunk X.
* *

View File

@ -1,22 +1,28 @@
package net.minestom.server.event.instance; package net.minestom.server.event.instance;
import net.minestom.server.event.InstanceEvent; import net.minestom.server.event.trait.InstanceEvent;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Called when a chunk in an instance is unloaded. * Called when a chunk in an instance is unloaded.
*/ */
public class InstanceChunkUnloadEvent extends InstanceEvent { public class InstanceChunkUnloadEvent implements InstanceEvent {
private final Instance instance;
private final int chunkX, chunkZ; private final int chunkX, chunkZ;
public InstanceChunkUnloadEvent(@NotNull Instance instance, int chunkX, int chunkZ) { public InstanceChunkUnloadEvent(@NotNull Instance instance, int chunkX, int chunkZ) {
super(instance); this.instance = instance;
this.chunkX = chunkX; this.chunkX = chunkX;
this.chunkZ = chunkZ; this.chunkZ = chunkZ;
} }
@Override
public @NotNull Instance getInstance() {
return instance;
}
/** /**
* Gets the chunk X. * Gets the chunk X.
* *
@ -34,5 +40,4 @@ public class InstanceChunkUnloadEvent extends InstanceEvent {
public int getChunkZ() { public int getChunkZ() {
return chunkZ; return chunkZ;
} }
} }

View File

@ -1,21 +1,27 @@
package net.minestom.server.event.instance; package net.minestom.server.event.instance;
import net.minestom.server.event.InstanceEvent; import net.minestom.server.event.trait.InstanceEvent;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Called when an instance processes a tick. * Called when an instance processes a tick.
*/ */
public class InstanceTickEvent extends InstanceEvent { public class InstanceTickEvent implements InstanceEvent {
private final Instance instance;
private final int duration; private final int duration;
public InstanceTickEvent(@NotNull Instance instance, long time, long lastTickAge) { public InstanceTickEvent(@NotNull Instance instance, long time, long lastTickAge) {
super(instance); this.instance = instance;
this.duration = (int) (time - lastTickAge); this.duration = (int) (time - lastTickAge);
} }
@Override
public @NotNull Instance getInstance() {
return instance;
}
/** /**
* Gets the duration of the tick in ms. * Gets the duration of the tick in ms.
* *

View File

@ -2,24 +2,31 @@ package net.minestom.server.event.instance;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.event.CancellableEvent; import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.InstanceEvent; import net.minestom.server.event.trait.EntityEvent;
import net.minestom.server.event.trait.InstanceEvent;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Called by an Instance when an entity is removed from it. * Called by an Instance when an entity is removed from it.
*/ */
public class RemoveEntityFromInstanceEvent extends InstanceEvent implements CancellableEvent { public class RemoveEntityFromInstanceEvent implements InstanceEvent, EntityEvent, CancellableEvent {
private final Instance instance;
private final Entity entity; private final Entity entity;
private boolean cancelled; private boolean cancelled;
public RemoveEntityFromInstanceEvent(@NotNull Instance instance, @NotNull Entity entity) { public RemoveEntityFromInstanceEvent(@NotNull Instance instance, @NotNull Entity entity) {
super(instance); this.instance = instance;
this.entity = entity; this.entity = entity;
} }
@Override
public @NotNull Instance getInstance() {
return instance;
}
/** /**
* Gets the entity being removed. * Gets the entity being removed.
* *

View File

@ -0,0 +1,15 @@
package net.minestom.server.event.trait;
import net.minestom.server.event.Event;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
public interface InstanceEvent extends Event {
/**
* Gets the instance.
*
* @return instance
*/
@NotNull Instance getInstance();
}