Added EventHandler#removeEventCallback

This commit is contained in:
Felix Cravic 2020-06-01 17:11:43 +02:00
parent b1e86adb23
commit 45e8ba2f03
5 changed files with 43 additions and 1 deletions

View File

@ -496,6 +496,15 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
this.eventCallbacks.put(eventClass, callbacks);
}
@Override
public <E extends Event> void removeEventCallback(Class<E> eventClass, EventCallback<E> eventCallback) {
Check.notNull(eventClass, "Event class cannot be null");
Check.notNull(eventCallback, "Event callback cannot be null");
List<EventCallback> callbacks = getEventCallbacks(eventClass);
callbacks.remove(eventCallback);
this.eventCallbacks.put(eventClass, callbacks);
}
@Override
public <E extends Event> List<EventCallback> getEventCallbacks(Class<E> eventClass) {
Check.notNull(eventClass, "Event class cannot be null");

View File

@ -1680,7 +1680,7 @@ public class Player extends LivingEntity {
}
/**
* All packets in the queue are executed in the {@link #update()} method
* All packets in the queue are executed in the {@link #update(long)} method
* It is used internally to add all received packet from the client
* Could be used to "simulate" a received packet, but to use at your own risk
*

View File

@ -3,6 +3,9 @@ package net.minestom.server.event.entity;
import net.minestom.server.entity.ItemEntity;
import net.minestom.server.event.CancellableEvent;
/**
* Called when two {@link ItemEntity} are merging their ItemStack together to form a sole entity
*/
public class EntityItemMergeEvent extends CancellableEvent {
private ItemEntity source;
@ -13,10 +16,24 @@ public class EntityItemMergeEvent extends CancellableEvent {
this.merged = merged;
}
/**
* Get the entity who is receiving {@link #getMerged()} ItemStack
* <p>
* This can be used to get the final ItemEntity position
*
* @return the source ItemEntity
*/
public ItemEntity getSource() {
return source;
}
/**
* Get the entity who will be merged
* <p>
* This entity will be removed after the event
*
* @return the merged ItemEntity
*/
public ItemEntity getMerged() {
return merged;
}

View File

@ -17,6 +17,15 @@ public interface EventHandler {
*/
<E extends Event> void addEventCallback(Class<E> eventClass, EventCallback<E> eventCallback);
/**
* Remove an event callback
*
* @param eventClass the event class
* @param eventCallback the event callback
* @param <E> the event type
*/
<E extends Event> void removeEventCallback(Class<E> eventClass, EventCallback<E> eventCallback);
/**
* @param eventClass
* @param <E>

View File

@ -425,6 +425,13 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
this.eventCallbacks.put(eventClass, callbacks);
}
@Override
public <E extends Event> void removeEventCallback(Class<E> eventClass, EventCallback<E> eventCallback) {
List<EventCallback> callbacks = getEventCallbacks(eventClass);
callbacks.remove(eventCallback);
this.eventCallbacks.put(eventClass, callbacks);
}
@Override
public <E extends Event> List<EventCallback> getEventCallbacks(Class<E> eventClass) {
return eventCallbacks.getOrDefault(eventClass, new CopyOnWriteArrayList<>());