001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.util; 020 021import com.plotsquared.core.configuration.Settings; 022import com.plotsquared.core.plot.Plot; 023import com.plotsquared.core.plot.flag.PlotFlag; 024import com.plotsquared.core.plot.flag.implementations.DoneFlag; 025import org.checkerframework.checker.nullness.qual.NonNull; 026 027import static com.plotsquared.core.util.entity.EntityCategories.CAP_ANIMAL; 028import static com.plotsquared.core.util.entity.EntityCategories.CAP_ENTITY; 029import static com.plotsquared.core.util.entity.EntityCategories.CAP_MISC; 030import static com.plotsquared.core.util.entity.EntityCategories.CAP_MOB; 031import static com.plotsquared.core.util.entity.EntityCategories.CAP_MONSTER; 032import static com.plotsquared.core.util.entity.EntityCategories.CAP_VEHICLE; 033 034/** 035 * Entity related general utility methods 036 */ 037public class EntityUtil { 038 039 private EntityUtil() { 040 throw new UnsupportedOperationException( 041 "This is a utility class and cannot be instantiated"); 042 } 043 044 private static int capNumeral(final @NonNull String flagName) { 045 return switch (flagName) { 046 case "mob-cap" -> CAP_MOB; 047 case "hostile-cap" -> CAP_MONSTER; 048 case "animal-cap" -> CAP_ANIMAL; 049 case "vehicle-cap" -> CAP_VEHICLE; 050 case "misc-cap" -> CAP_MISC; 051 default -> CAP_ENTITY; 052 }; 053 } 054 055 @SuppressWarnings("unchecked") 056 public static boolean checkEntity(Plot plot, PlotFlag<Integer, ?>... flags) { 057 if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { 058 return true; 059 } 060 int[] mobs = null; 061 for (PlotFlag<Integer, ?> flag : flags) { 062 final int i = capNumeral(flag.getName()); 063 int cap = plot.getFlag(flag); 064 if (cap == Integer.MAX_VALUE) { 065 continue; 066 } 067 if (cap == 0) { 068 return true; 069 } 070 if (mobs == null) { 071 mobs = plot.countEntities(); 072 } 073 if (mobs[i] >= cap) { 074 plot.setMeta("EntityCount", mobs); 075 plot.setMeta("EntityCountTime", System.currentTimeMillis()); 076 plot.debug("Prevented spawning of mob because it would exceed " + flag.getName()); 077 return true; 078 } 079 } 080 if (mobs != null) { 081 for (PlotFlag<Integer, ?> flag : flags) { 082 final int i = capNumeral(flag.getName()); 083 mobs[i]++; 084 } 085 plot.setMeta("EntityCount", mobs); 086 plot.setMeta("EntityCountTime", System.currentTimeMillis()); 087 } 088 return false; 089 } 090 091}