org.bukkit.event.player.PlayerEvent Java Examples

The following examples show how to use org.bukkit.event.player.PlayerEvent. 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: ListenerServiceTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
// This simply forwards to shouldCancelEvent(Player), so the rest is already tested
public void shouldHandlePlayerEvent() {
    // given
    String playerName = "example";
    Player player = mockPlayerWithName(playerName);
    PlayerEvent event = new TestPlayerEvent(player);
    given(playerCache.isAuthenticated(playerName)).willReturn(true);

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

    // then
    assertThat(result, equalTo(false));
    verify(playerCache).isAuthenticated(playerName);
    verifyNoInteractions(dataSource);
}
 
Example #2
Source File: AchievementHunter.java    From UhcCore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onEnable() {
    if (UhcCore.getVersion() < 12){
        type = Type.ACHIEVEMENTS;
    }else{
        type = Type.ADVANCEMENTS;
    }

    try {
        event = (Class<? extends PlayerEvent>) Class.forName(type.event);
    }catch (ClassNotFoundException | ClassCastException ex){
        ex.printStackTrace();
        getScenarioManager().removeScenario(Scenario.ACHIEVEMENTHUNTER);
    }

    Bukkit.getPluginManager().registerEvent(event, this, EventPriority.NORMAL, this, UhcCore.getPlugin());
}
 
Example #3
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 #4
Source File: ListenerServiceTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldHandlePlayerEventWithNullPlayer() {
    // given
    PlayerEvent event = new TestPlayerEvent(null);

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

    // then
    assertThat(result, equalTo(false));
}
 
Example #5
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 #6
Source File: AchievementHunter.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void execute(Listener listener, Event event){
    if (getGameManager().getGameState() == GameState.WAITING){
        return;
    }

    System.out.println("event!");

    if (type == Type.ACHIEVEMENTS){
        addHeart(((PlayerEvent) event).getPlayer());
    }else{
        handleAdvancementEvent((PlayerEvent) event);
    }
}
 
Example #7
Source File: EventReplacer.java    From ScoreboardStats with MIT License 5 votes vote down vote up
private void executeUnsafe(ReplacerAPI replaceManager, T event) {
    String variable = replacer.getVariable();
    for (Function<T, Integer> function : scoreFunctions) {
        int newScore = function.apply(event);
        if (replacer.isGlobal()) {
            for (Player player : Bukkit.getOnlinePlayers()) {
                replaceManager.forceUpdate(player, variable, newScore);
            }
        } else if (event instanceof PlayerEvent) {
            replaceManager.forceUpdate(((PlayerEvent) event).getPlayer(), variable, newScore);
        }
    }
}
 
Example #8
Source File: JoinRequester.java    From HeavySpleef with GNU General Public License v3.0 5 votes vote down vote up
private void handleQuit(PlayerEvent event) {
	SpleefPlayer player = heavySpleef.getSpleefPlayer(event.getPlayer());
	if (!checking.containsKey(player)) {
		return;
	}
	
	handleFail(player, FailCause.QUIT, null);
}
 
Example #9
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 #10
Source File: BukkitImageListener.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
private void handleInteract(PlayerEvent event, Entity entity, boolean primary) {
    handleInteract(event, event.getPlayer(), entity, primary);
}
 
Example #11
Source File: FlagAntiCamping.java    From HeavySpleef with GNU General Public License v3.0 4 votes vote down vote up
private void handlePlayerLeave(PlayerEvent event) {
	SpleefPlayer player = heavySpleef.getSpleefPlayer(event.getPlayer());
	resetPlayerData(player);
}
 
Example #12
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 #13
Source File: InventoryEvent.java    From MCAuthenticator with GNU General Public License v3.0 4 votes vote down vote up
private boolean auth(PlayerEvent e) {
    User user = instance.getCache().get(e.getPlayer().getUniqueId());
    return user != null && user.authenticated();
}
 
Example #14
Source File: AchievementHunter.java    From UhcCore with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isValidAdvancement(PlayerEvent event){
    org.bukkit.event.player.PlayerAdvancementDoneEvent advancementEvent = (org.bukkit.event.player.PlayerAdvancementDoneEvent) event;
    NamespacedKey key = advancementEvent.getAdvancement().getKey();
    System.out.println(key.getKey());
    return key.getKey().startsWith("story/");
}
 
Example #15
Source File: AchievementHunter.java    From UhcCore with GNU General Public License v3.0 4 votes vote down vote up
private void handleAdvancementEvent(PlayerEvent event){
    if (isValidAdvancement(event)){
        addHeart(event.getPlayer());
    }
}
 
Example #16
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 #17
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(PlayerEvent event) {
    Player player = event.getPlayer();
    return shouldCancelEvent(player);
}
 
Example #18
Source File: EventFilters.java    From helper with MIT License 2 votes vote down vote up
/**
 * Returns a predicate which only returns true if the player has the given permission
 *
 * @param permission the permission
 * @param <T> the event type
 * @return a predicate which only returns true if the player has the given permission
 */
@Nonnull
public static <T extends PlayerEvent> Predicate<T> playerHasPermission(String permission) {
    return e -> e.getPlayer().hasPermission(permission);
}
 
Example #19
Source File: EventFilters.java    From helper with MIT License 2 votes vote down vote up
/**
 * Returns a predicate which only returns true if the player has a given metadata key
 *
 * @param key the metadata key
 * @param <T> the event type
 * @return a predicate which only returns true if the player has a given metadata key
 */
@Nonnull
public static <T extends PlayerEvent> Predicate<T> playerHasMetadata(MetadataKey<?> key) {
    return e -> Metadata.provideForPlayer(e.getPlayer()).has(key);
}