org.bukkit.event.entity.EntityEvent Java Examples

The following examples show how to use org.bukkit.event.entity.EntityEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Talisman.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private static Player getPlayerByEventType(Event e) {
    if (e instanceof EntityDeathEvent) {
        return ((EntityDeathEvent) e).getEntity().getKiller();
    }
    else if (e instanceof BlockBreakEvent) {
        return ((BlockBreakEvent) e).getPlayer();
    }
    else if (e instanceof PlayerEvent) {
        return ((PlayerEvent) e).getPlayer();
    }
    else if (e instanceof EntityEvent) {
        return (Player) ((EntityEvent) e).getEntity();
    }
    else if (e instanceof EnchantItemEvent) {
        return ((EnchantItemEvent) e).getEnchanter();
    }

    return null;
}
 
Example #2
Source File: ListenerServiceTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldAllowAuthenticatedPlayer() {
    // given
    String playerName = "Bobby";
    Player player = mockPlayerWithName(playerName);
    given(playerCache.isAuthenticated(playerName)).willReturn(true);
    EntityEvent event = mock(EntityEvent.class);
    given(event.getEntity()).willReturn(player);

    // when
    boolean result = listenerService.shouldCancelEvent(event);

    // then
    assertThat(result, equalTo(false));
    verify(playerCache).isAuthenticated(playerName);
    verifyNoInteractions(dataSource);
}
 
Example #3
Source File: ListenerServiceTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldDenyUnLoggedPlayer() {
    // given
    String playerName = "Tester";
    Player player = mockPlayerWithName(playerName);
    given(playerCache.isAuthenticated(playerName)).willReturn(false);
    EntityEvent event = mock(EntityEvent.class);
    given(event.getEntity()).willReturn(player);

    // when
    boolean result = listenerService.shouldCancelEvent(event);

    // then
    assertThat(result, equalTo(true));
    verify(playerCache).isAuthenticated(playerName);
    // makes sure the setting is checked first = avoid unnecessary DB operation
    verifyNoInteractions(dataSource);
}
 
Example #4
Source File: ListenerServiceTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldAllowUnloggedPlayerForOptionalRegistration() {
    // given
    String playerName = "myPlayer1";
    Player player = mockPlayerWithName(playerName);
    given(playerCache.isAuthenticated(playerName)).willReturn(false);
    given(settings.getProperty(RegistrationSettings.FORCE)).willReturn(false);
    EntityEvent event = mock(EntityEvent.class);
    given(event.getEntity()).willReturn(player);
    listenerService.reload(settings);

    // when
    boolean result = listenerService.shouldCancelEvent(event);

    // then
    assertThat(result, equalTo(false));
    verify(playerCache).isAuthenticated(playerName);
    verify(dataSource).isAuthAvailable(playerName);
}
 
Example #5
Source File: ListenerServiceTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldAllowUnrestrictedName() {
    // given
    String playerName = "Npc2";
    Player player = mockPlayerWithName(playerName);
    EntityEvent event = mock(EntityEvent.class);
    given(event.getEntity()).willReturn(player);
    given(validationService.isUnrestricted(playerName)).willReturn(true);

    // when
    boolean result = listenerService.shouldCancelEvent(event);

    // then
    assertThat(result, equalTo(false));
    verifyNoInteractions(dataSource);
}
 
Example #6
Source File: ListenerServiceTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldAllowNpcPlayer() {
    // given
    String playerName = "other_npc";
    Player player = mockPlayerWithName(playerName);
    EntityEvent event = mock(EntityEvent.class);
    given(event.getEntity()).willReturn(player);
    given(player.hasMetadata("NPC")).willReturn(true);

    // when
    boolean result = listenerService.shouldCancelEvent(event);

    // then
    assertThat(result, equalTo(false));
    verify(player).hasMetadata("NPC");
}
 
Example #7
Source File: EventUtil.java    From mcspring-boot with MIT License 5 votes vote down vote up
public static CommandSender getSender(Event event) {
    if (event instanceof PlayerEvent) {
        return ((PlayerEvent) event).getPlayer();
    } else if (event instanceof ServerCommandEvent) {
        return ((ServerCommandEvent) event).getSender();
    } else if (event instanceof EntityEvent) {
        val entityEvent = (EntityEvent) event;
        return entityEvent.getEntity() instanceof Player ? entityEvent.getEntity() : null;
    }
    return getInferredSender(event);
}
 
Example #8
Source File: ExprAttacked.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Nullable
protected Entity[] get(final Event e) {
	final Entity[] one = (Entity[]) Array.newInstance(type.getType(), 1);
	Entity entity;
	if (e instanceof EntityEvent)
		entity = ((EntityEvent) e).getEntity();
	else
		entity = ((VehicleEvent) e).getVehicle();
	if (type.isInstance(entity)) {
		one[0] = entity;
		return one;
	}
	return null;
}
 
Example #9
Source File: EventCancelVerifier.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Mocks, based on the given event, the correct method in {@link ListenerService} to return
 * the provided {@code result}.
 *
 * @param result the result the service should return
 * @param listenerService the service to mock
 * @param event the event
 */
private static void mockShouldCancel(boolean result, ListenerService listenerService, Event event) {
    if (event instanceof PlayerEvent) {
        given(listenerService.shouldCancelEvent((PlayerEvent) event)).willReturn(result);
    } else if (event instanceof EntityEvent) {
        given(listenerService.shouldCancelEvent((EntityEvent) event)).willReturn(result);
    } else {
        throw new IllegalStateException("Found event with unsupported type: " + event.getClass());
    }
}
 
Example #10
Source File: ListenerServiceTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldHandleEventWithNullEntity() {
    // given
    EntityEvent event = mock(EntityEvent.class);
    given(event.getEntity()).willReturn(null);

    // when
    boolean result = listenerService.shouldCancelEvent(event);

    // then
    assertThat(result, equalTo(false));
}
 
Example #11
Source File: ListenerServiceTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldHandleEntityEventWithNonPlayerEntity() {
    // given
    EntityEvent event = mock(EntityEvent.class);
    given(event.getEntity()).willReturn(mock(Entity.class));

    // when
    boolean result = listenerService.shouldCancelEvent(event);

    // then
    assertThat(result, equalTo(false));
}
 
Example #12
Source File: MatchPlayerEventRouter.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
protected Stream<? extends BukkitFacetContext<?>> contexts(Object event) {
    // Try to get some online players from the event, either directly
    // through MatchPlayerEvent, or indirectly through entities.
    final Set<MatchPlayer> players;
    if(event instanceof MatchPlayerEvent) {
        players = ((MatchPlayerEvent) event).players().collect(Collectors.toImmutableSet());
    } else {
        final Set<Entity> entities = new HashSet<>();
        if(event instanceof EntityAction) entities.add(((EntityAction) event).getActor());
        if(event instanceof EntityEvent) entities.add(((EntityEvent) event).getEntity());
        if(event instanceof PlayerAction) entities.add(((PlayerAction) event).getActor());
        if(event instanceof PlayerEvent) entities.add(((PlayerEvent) event).getPlayer());

        players = entities.stream()
                          .flatMap(entity -> Streams.ofNullable(finder.getPlayer(entity)))
                          .collect(Collectors.toImmutableSet());
    }

    // If we have one or more MatchPlayers, return them along with their user contexts
    if(!players.isEmpty()) {
        return Stream.concat(
            players.stream(),
            players.stream().map(player -> player.userContext)
        );
    }

    // If we couldn't derive any online players from the event, try for offline player UUIDs
    final Set<UUID> uuids;
    if(event instanceof MatchUserEvent) {
        uuids = ((MatchUserEvent) event).users().collect(Collectors.toImmutableSet());
    } else if(event instanceof UserEvent) {
        uuids = ImmutableSet.of(((UserEvent) event).getUser().uuid());
    } else {
        return Stream.empty();
    }

    // Restrict to a specific match, if possible
    final Stream<Match> matches = finder.match((Event) event)
                                        .map(Stream::of)
                                        .orElseGet(() -> finder.currentMatches().stream());

    // Search the selected matches for both users and players
    // with the selected UUIDs.
    return matches.flatMap(
        match -> uuids.stream().flatMap(
            uuid -> Stream.concat(
                Optionals.stream(match.player(uuid)),
                Optionals.stream(match.userContext(uuid))
            )
        )
    );
}
 
Example #13
Source File: CauseFilter.java    From CardinalPGM with MIT License 4 votes vote down vote up
private Boolean evaluate(Event event) {
    if (!(event instanceof EntityDamageEvent)) {
        switch (cause) {
            case WORLD:
                return event instanceof WorldEvent;
            case LIVING:
                return event instanceof EntityEvent && ((EntityEvent) event).getEntity() instanceof LivingEntity;
            case MOB:
                return event instanceof EntityEvent && ((EntityEvent) event).getEntity() instanceof Creature;
            case PLAYER:
                return event instanceof PlayerEvent || event instanceof BlockPlaceEvent || event instanceof BlockBreakEvent;

            case PUNCH:
                return event instanceof PlayerInteractEvent
                        && ((PlayerInteractEvent) event).getAction().equals(Action.LEFT_CLICK_BLOCK);
            case TRAMPLE:
                return event instanceof PlayerMoveEvent;
            case MINE:
                return event instanceof BlockBreakEvent;

            case EXPLOSION:
                return event instanceof EntityExplodeEvent;
        }
    } else {
        EntityDamageEvent.DamageCause damageCause = ((EntityDamageEvent) event).getCause();
        switch (cause) {
            case MELEE:
                return damageCause.equals(EntityDamageEvent.DamageCause.ENTITY_ATTACK);
            case PROJECTILE:
                return damageCause.equals(EntityDamageEvent.DamageCause.PROJECTILE);
            case POTION:
                return damageCause.equals(EntityDamageEvent.DamageCause.MAGIC)
                        || damageCause.equals(EntityDamageEvent.DamageCause.POISON)
                        || damageCause.equals(EntityDamageEvent.DamageCause.WITHER)
                        || damageCause.equals(EntityDamageEvent.DamageCause.DRAGON_BREATH);
            case EXPLOSION:
                return damageCause.equals(EntityDamageEvent.DamageCause.BLOCK_EXPLOSION)
                        || damageCause.equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION);
            case COMBUSTION:
                return damageCause.equals(EntityDamageEvent.DamageCause.FIRE)
                        || damageCause.equals(EntityDamageEvent.DamageCause.FIRE_TICK)
                        || damageCause.equals(EntityDamageEvent.DamageCause.MELTING)
                        || damageCause.equals(EntityDamageEvent.DamageCause.LAVA)
                        || damageCause.equals(EntityDamageEvent.DamageCause.HOT_FLOOR);
            case FALL:
                return damageCause.equals(EntityDamageEvent.DamageCause.FALL);
            case GRAVITY:
                return damageCause.equals(EntityDamageEvent.DamageCause.FALL)
                        || damageCause.equals(EntityDamageEvent.DamageCause.VOID);
            case VOID:
                return damageCause.equals(EntityDamageEvent.DamageCause.VOID);
            case SQUASH:
                return damageCause.equals(EntityDamageEvent.DamageCause.FALLING_BLOCK);
            case SUFFOCATION:
                return damageCause.equals(EntityDamageEvent.DamageCause.SUFFOCATION);
            case DROWNING:
                return damageCause.equals(EntityDamageEvent.DamageCause.DROWNING);
            case STARVATION:
                return damageCause.equals(EntityDamageEvent.DamageCause.STARVATION);
            case LIGHTNING:
                return damageCause.equals(EntityDamageEvent.DamageCause.LIGHTNING);
            case CACTUS:
                return damageCause.equals(EntityDamageEvent.DamageCause.CONTACT);
            case THORNS:
                return damageCause.equals(EntityDamageEvent.DamageCause.THORNS);
        }
    }
    return null;
}
 
Example #14
Source File: EventFilters.java    From helper with MIT License 2 votes vote down vote up
/**
 * Returns a predicate which only returns true if the entity has a given metadata key
 *
 * @param key the metadata key
 * @param <T> the event type
 * @return a predicate which only returns true if the entity has a given metadata key
 */
@Nonnull
public static <T extends EntityEvent> Predicate<T> entityHasMetadata(MetadataKey<?> key) {
    return e -> Metadata.provideForEntity(e.getEntity()).has(key);
}
 
Example #15
Source File: ListenerService.java    From AuthMeReloaded with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns whether an event should be canceled (for unauthenticated, non-NPC players).
 *
 * @param event the event to process
 * @return true if the event should be canceled, false otherwise
 */
public boolean shouldCancelEvent(EntityEvent event) {
    Entity entity = event.getEntity();
    return shouldCancelEvent(entity);
}