Limits.java 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package me.km.snuviscript;
  2. import java.util.HashMap;
  3. import net.minecraft.entity.EntityType;
  4. import net.minecraft.world.server.ServerWorld;
  5. public class Limits {
  6. private final HashMap<EntityType, Integer> limits = new HashMap<>();
  7. private final HashMap<EntityType, Integer> currentAmount = new HashMap<>();
  8. public void setLimit(EntityType type, int limit) {
  9. limits.put(type, limit);
  10. }
  11. public void removeLimit(EntityType type) {
  12. limits.remove(type);
  13. }
  14. public void clearLimits() {
  15. limits.clear();
  16. }
  17. public void tick(Iterable<ServerWorld> worlds) {
  18. currentAmount.replaceAll((type, amount) -> 0);
  19. for(ServerWorld w : worlds) {
  20. w.getEntities().forEach(ent -> add(ent.getType()));
  21. }
  22. }
  23. private void add(EntityType type) {
  24. currentAmount.compute(type, (entType, amount) -> amount == null ? 1 : amount + 1);
  25. }
  26. public boolean isAllowedToSpawn(EntityType type) {
  27. Integer limit = limits.get(type);
  28. if(limit == null) {
  29. return true;
  30. }
  31. Integer amount = currentAmount.get(type);
  32. if(amount == null) {
  33. return true;
  34. }
  35. return amount < limit;
  36. }
  37. }