@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); verifyZeroInteractions(dataSource); }
@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 verifyZeroInteractions(dataSource); }
@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); }
@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)); verifyZeroInteractions(dataSource); }
@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"); }
private LoggableCreatureEvent getLoggableEntityEvent(EntityEventAction action, EntityEvent event) { final Entity entity = event.getEntity(); final Location location = entity.getLocation(); final World world = entity.getWorld(); Point3d coordinates = new Point3d(location.getX(), location.getY(), location.getZ()); LoggableCreatureEvent entityEvent = new LoggableCreatureEvent(world.getFullTime(), minecraft_server, world.getName(), action); LivingEntity spCreature = new LivingEntity(); spCreature.setType("creature"); spCreature.setCurrentHealth(((org.bukkit.entity.LivingEntity)entity).getHealth()); spCreature.setMaxHealth(((org.bukkit.entity.LivingEntity)entity).getMaxHealth()); spCreature.setLocation(coordinates); for (PotionEffect potion : (((org.bukkit.entity.LivingEntity)entity).getActivePotionEffects())) { spCreature.addPotions(potion.getType().getName() + ":" + potion.getAmplifier()); } if (event.getEntityType() == EntityType.SKELETON) { Skeleton skeleton = (org.bukkit.entity.Skeleton) event.getEntity(); spCreature.setName(skeleton.getSkeletonType() + "_SKELETON"); } else { spCreature.setName(event.getEntityType().name()); } entityEvent.setEntity(spCreature); return entityEvent; }
@Override @Nullable protected Entity[] get(final Event e) { final Entity[] one = (Entity[]) Array.newInstance(type.getType(), 1); final Entity entity = ((EntityEvent) e).getEntity(); if (type.isInstance(entity)) { one[0] = entity; return one; } return null; }
@Override protected void onEntityEvent(EntityEvent event) { Entity entity = event.getEntity(); if (entity != null) { callEvent(entity, event); } else if (event instanceof EntityExplodeEvent) { callEvent(((EntityExplodeEvent) event).getLocation(), event); } else { Msg.debug("Failed to forward bukkit EntityEvent event because it has no entity."); } }
public void on(Object e) { if (!(e instanceof Event)) return; Event event = (Event) e; onEvent(event); if (event instanceof PlayerEvent) { onPlayerEvent((PlayerEvent)event); } else if (event instanceof BlockEvent) { onBlockEvent((BlockEvent)event); } else if (event instanceof HangingEvent) { onHangingEvent((HangingEvent)event); } else if (event instanceof InventoryEvent) { onInventoryEvent((InventoryEvent) event); } else if (event instanceof VehicleEvent) { onVehicleEvent((VehicleEvent) event); } else if (event instanceof EntityEvent) { onEntityEvent((EntityEvent) event); } }
/** * 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()); } }
@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)); }
@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)); }
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)) ) ) ); }
@Override public Boolean evaluate(Event event) { if (!(event instanceof EntityDamageEvent)) { switch (cause) { /* Actor Type */ 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; /* Block action */ case PUNCH: return event instanceof BlockDamageEvent; case TRAMPLE: return event instanceof PlayerMoveEvent; case MINE: return event instanceof BlockBreakEvent; case EXPLOSION: return event instanceof EntityExplodeEvent; default: return null; } } else { /* Damage Type */ 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); default: return null; } } }
protected void callHandlers(String eventName, EntityEvent event) { callHandlers(eventName, event, event.getEntity()); }
@EventHandler(priority = EventPriority.MONITOR) public void onEntityEvent(EntityEvent evt) { checkLocation(evt.getEntity().getLocation(), evt); }
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; }
/** * 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); }
/** * Return true if the world of the entity event is the configured affected * world. * * @param event an entity-related event. * @return true if the world of the entity event is the configured affected * world. */ public boolean isAffectedWorld(EntityEvent event) { return isAffectedWorld(event.getEntity().getLocation().getWorld()); }
/** * Invoked whenever a registered entity event is called. * * <p>Intended for optional override.</p> * * @param event The called event. */ protected void onEntityEvent(EntityEvent event) {}
/** * 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); }