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 int i; 046 switch (flagName) { 047 case "mob-cap": 048 i = CAP_MOB; 049 break; 050 case "hostile-cap": 051 i = CAP_MONSTER; 052 break; 053 case "animal-cap": 054 i = CAP_ANIMAL; 055 break; 056 case "vehicle-cap": 057 i = CAP_VEHICLE; 058 break; 059 case "misc-cap": 060 i = CAP_MISC; 061 break; 062 case "entity-cap": 063 default: 064 i = CAP_ENTITY; 065 } 066 return i; 067 } 068 069 @SuppressWarnings("unchecked") 070 public static boolean checkEntity(Plot plot, PlotFlag<Integer, ?>... flags) { 071 if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { 072 return true; 073 } 074 int[] mobs = null; 075 for (PlotFlag<Integer, ?> flag : flags) { 076 final int i = capNumeral(flag.getName()); 077 int cap = plot.getFlag(flag); 078 if (cap == Integer.MAX_VALUE) { 079 continue; 080 } 081 if (cap == 0) { 082 return true; 083 } 084 if (mobs == null) { 085 mobs = plot.countEntities(); 086 } 087 if (mobs[i] >= cap) { 088 plot.setMeta("EntityCount", mobs); 089 plot.setMeta("EntityCountTime", System.currentTimeMillis()); 090 plot.debug("Prevented spawning of mob because it would exceed " + flag.getName()); 091 return true; 092 } 093 } 094 if (mobs != null) { 095 for (PlotFlag<Integer, ?> flag : flags) { 096 final int i = capNumeral(flag.getName()); 097 mobs[i]++; 098 } 099 plot.setMeta("EntityCount", mobs); 100 plot.setMeta("EntityCountTime", System.currentTimeMillis()); 101 } 102 return false; 103 } 104 105}