<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.skelril.skree.content.zone;

import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.projectile.Projectile;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.cause.entity.spawn.SpawnCause;
import org.spongepowered.api.event.cause.entity.spawn.SpawnType;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.event.entity.SpawnEntityEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import java.util.Optional;
import java.util.function.Function;

public class ZoneNaturalSpawnBlocker<T> extends ZoneApplicableListener<T> {
  public ZoneNaturalSpawnBlocker(Function<Location<World>, Optional<T>> applicabilityFunct) {
    super(applicabilityFunct);
  }

  @Listener
  public void onEntitySpawn(SpawnEntityEvent event, @First SpawnCause spawnCause) {
    for (Entity entity : event.getEntities()) {
      if (isApplicable(entity)) {
        SpawnType spawnType = spawnCause.getType();
        if (spawnType == SpawnTypes.CUSTOM || spawnType == SpawnTypes.WORLD_SPAWNER) {
          /* SpongeCommon/679 */
          if (entity.getType() != EntityTypes.EXPERIENCE_ORB && entity.getType() != EntityTypes.PRIMED_TNT && !(entity instanceof Projectile)) {
            event.setCancelled(true);
          }
        }

        /* SpongeCommon/584 */
        if (spawnType == SpawnTypes.DROPPED_ITEM && entity.getType() == EntityTypes.ZOMBIE) {
          event.setCancelled(true);
        }
        break;
      }
    }
  }
}<commit_msg>Rewrote the ZoneNaturalSpawnBlocker to be more effective and less dependent on the accuracy of the cause tracker<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.skelril.skree.content.zone;

import com.google.common.collect.Lists;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.ExperienceOrb;
import org.spongepowered.api.entity.Item;
import org.spongepowered.api.entity.explosive.PrimedTNT;
import org.spongepowered.api.entity.projectile.Projectile;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.cause.entity.spawn.SpawnCause;
import org.spongepowered.api.event.cause.entity.spawn.SpawnType;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.event.entity.SpawnEntityEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public class ZoneNaturalSpawnBlocker<T> extends ZoneApplicableListener<T> {
  public ZoneNaturalSpawnBlocker(Function<Location<World>, Optional<T>> applicabilityFunct) {
    super(applicabilityFunct);
  }

  private static List<Class<? extends Entity>> LEGAL_ENTITY_CLASSES = Lists.newArrayList(
      ExperienceOrb.class, PrimedTNT.class, Projectile.class, Item.class
  );

  private static boolean isLegalSpawn(Entity entity) {
    boolean legalSpawn = false;

    for (Class<? extends Entity> clazz : LEGAL_ENTITY_CLASSES) {
      if (clazz.isInstance(entity)) {
        legalSpawn = true;
      }
    }

    return legalSpawn;
  }

  @Listener
  public void onEntitySpawn(SpawnEntityEvent event, @First SpawnCause spawnCause) {
    for (Entity entity : event.getEntities()) {
      if (isApplicable(entity)) {
        SpawnType spawnType = spawnCause.getType();

        if (spawnType != SpawnTypes.PLUGIN && !isLegalSpawn(entity)) {
          event.setCancelled(true);
        }

        break;
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.10806881511994185, 'fraction_numerical': 0.003392294645020596, 'mean_word_length': 4.003636363636364, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3752844', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1321, 'n_words': 264}