下面列出了怎么用org.bukkit.entity.Monster的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean isEntityProtected(Entity entity) {
if (entity.getType() == null) {
if (entity instanceof Monster) {
return false;
}
return true;
}
// ignore monsters
final String name = entity.getType().getName() == null ? entity.getType().name().toLowerCase() : entity.getType().getName();
final GDEntityType type = EntityTypeRegistryModule.getInstance().getById(name).orElse(null);
if (type == null) {
return true;
}
final String creatureType = type.getEnumCreatureTypeId();
if (creatureType == null) {
return true;
}
if (creatureType.contains("monster")) {
return false;
}
return true;
}
private void cleanupEntity(Entity entity) {
// Check if entity is a mob, since they despawn on chunk unload.
if (entity instanceof Monster) {
sm.getCache().remove(entity.getUniqueId());
StackTools.removeSize(entity);
return;
}
// Add to storage
if (StackTools.hasValidData(entity)) {
int stackSize = StackTools.getSize(entity);
StackTools.removeSize(entity);
if (stackSize <= 1 && stackSize != GlobalValues.NO_STACKING) {
return;
}
if (sm.getCustomConfig().getBoolean("remove-chunk-unload")) {
entity.remove();
return;
}
sm.getCache().put(entity.getUniqueId(), stackSize);
}
}
@Override
public void test() throws Exception {
if(!NBTInjector.isInjected())return;
if (!Bukkit.getWorlds().isEmpty()) {
World world = Bukkit.getWorlds().get(0);
try {
if (!world.getEntitiesByClasses(Animals.class, Monster.class).isEmpty()) {
Entity ent = world.getEntitiesByClasses(Animals.class, Monster.class).iterator().next();
ent = NBTInjector.patchEntity(ent);
NBTCompound comp = NBTInjector.getNbtData(ent);
comp.setString("Hello", "World");
NBTEntity nbtent = new NBTEntity(ent);
if (!nbtent.toString().contains("__extraData:{Hello:\"World\"}")) {
throw new NbtApiException("Custom Data did not save to the Entity!");
}
comp.removeKey("Hello");
}
} catch (Exception ex) {
throw new NbtApiException("Wasn't able to use NBTEntities!", ex);
}
}
}
@Override
public void test() throws Exception {
if(MinecraftVersion.getVersion().getVersionId() < MinecraftVersion.MC1_14_R1.getVersionId())return;
if (!Bukkit.getWorlds().isEmpty()) {
World world = Bukkit.getWorlds().get(0);
try {
if (!world.getEntitiesByClasses(Animals.class, Monster.class).isEmpty()) {
Entity ent = world.getEntitiesByClasses(Animals.class, Monster.class).iterator().next();
NBTEntity nbtEnt = new NBTEntity(ent);
NBTCompound comp = nbtEnt.getPersistentDataContainer();
comp.setString("Hello", "World");
NBTEntity nbtent = new NBTEntity(ent);
if (!nbtent.toString().contains("Hello:\"World\"")) {
throw new NbtApiException("Custom Data did not save to the Entity!");
}
comp.removeKey("Hello");
}
} catch (Exception ex) {
throw new NbtApiException("Wasn't able to use NBTEntities!", ex);
}
}
}
/**
* Removes all unnamed {@link Monster}'s at the given {@link Location}.
* @param target Location to remove unnamed monsters.
*/
public void removeCreatures(@Nullable final Location target) {
if (!Settings.island_removeCreaturesByTeleport || target == null || target.getWorld() == null) {
return;
}
final int px = target.getBlockX();
final int py = target.getBlockY();
final int pz = target.getBlockZ();
for (int x = -1; x <= 1; ++x) {
for (int z = -1; z <= 1; ++z) {
Chunk chunk = target.getWorld().getChunkAt(
new Location(target.getWorld(), (px + x * 16), py, (pz + z * 16)));
Arrays.stream(chunk.getEntities())
.filter(entity -> entity instanceof Monster)
.filter(entity -> entity.getCustomName() == null)
.forEach(Entity::remove);
}
}
}
public CreatureType getCreatureType(EntityType entityType) {
if (Monster.class.isAssignableFrom(entityType.getEntityClass())
|| WaterMob.class.isAssignableFrom(entityType.getEntityClass())
|| Slime.class.isAssignableFrom(entityType.getEntityClass())
|| Ghast.class.isAssignableFrom(entityType.getEntityClass())
) {
return CreatureType.MONSTER;
} else if (Animals.class.isAssignableFrom(entityType.getEntityClass())) {
return CreatureType.ANIMAL;
} else if (Villager.class.isAssignableFrom(entityType.getEntityClass())) {
return CreatureType.VILLAGER;
} else if (Golem.class.isAssignableFrom(entityType.getEntityClass())) {
return CreatureType.GOLEM;
}
return CreatureType.UNKNOWN;
}
@EventHandler(priority = EventPriority.NORMAL)
public void onChunkLoad(ChunkLoadEvent event) {
for (Entity e : event.getChunk().getEntities()) {
if (e instanceof Monster) {
e.remove();
return;
}
if (e instanceof IronGolem) {
e.remove();
return;
}
}
}
@EventHandler(ignoreCancelled = true)
void onEntityDamage(EntityDamageEvent event) {
Entity entity = event.getEntity();
// block damaging of shopkeepers
if (plugin.isShopkeeper(entity)) {
event.setCancelled(true);
if (event instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent evt = (EntityDamageByEntityEvent) event;
if (evt.getDamager() instanceof Monster) {
Monster monster = (Monster) evt.getDamager();
// reset target, future targeting should get prevented somewhere else:
if (entity.equals(monster.getTarget())) {
monster.setTarget(null);
}
}
}
}
}
/**
* Handles extra XP dropped from mobs
*
* @param event
*/
@EventHandler
public void onMobDeath(EntityDeathEvent event) {
if (!(event.getEntity() instanceof Monster)) return;
Monster monster = (Monster) event.getEntity();
Player killer = monster.getKiller();
if (killer == null) return;
Guild guild = guildHandler.getGuild(killer);
if (guild == null) {
return;
}
double xp = event.getDroppedExp();
double multiplier = guild.getTier().getMobXpMultiplier();
event.setDroppedExp((int) Math.round(xp * multiplier));
}
/**
* Killing mobs gives enhancement stone.
*
* @param e
*/
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onKilling(EntityDeathEvent e) {
// If the killed entity is a monster
if (e.getEntity() instanceof Monster) {
if (e.getEntity().getKiller() instanceof Player) {
Player player = e.getEntity().getKiller();
if (DropManager.killingChance > random.nextDouble()) {
DropManager.randomDrop(player, DropManager.killingLootTable);
}
}
}
}
public static void setSize(Entity entity, int newSize){
currentEntities.put(entity.getUniqueId(), newSize);
if (!(entity instanceof Monster)) {
if (newSize <= 1 && newSize != GlobalValues.NO_STACKING) {
persistentEntities.remove(entity.getUniqueId());
return;
}
persistentEntities.add(entity.getUniqueId());
}
}
@EventHandler
public void onEntityTarget(EntityTargetLivingEntityEvent event) {
if(event.getTarget() instanceof Player && event.getEntity() instanceof Monster){
if(StackTools.hasSizeMoreThanOne(event.getEntity())){
if(!sm.getCustomConfig().getStringList("no-targeting.types-blacklist")
.contains(event.getEntityType().toString())) {
event.setCancelled(true);
}
}
}
}
@EventHandler(ignoreCancelled = true)
public void onEntityDieKill(EntityDeathEvent event) {
if (BukkitConfigParties.KILLS_ENABLE
&& event.getEntity().getKiller() != null) {
Player killer = event.getEntity().getKiller();
PartyPlayerImpl ppKiller = plugin.getPlayerManager().getPlayer(killer.getUniqueId());
if (!ppKiller.getPartyName().isEmpty()) {
PartyImpl party = plugin.getPartyManager().getParty(ppKiller.getPartyName());
boolean gotKill = false;
if (BukkitConfigParties.KILLS_MOB_HOSTILE
&& event.getEntity() instanceof Monster)
gotKill = true;
else if (BukkitConfigParties.KILLS_MOB_NEUTRAL
&& event.getEntity() instanceof Animals)
gotKill = true;
else if (BukkitConfigParties.KILLS_MOB_PLAYERS
&& event.getEntity() instanceof Player)
gotKill = true;
if (gotKill) {
party.setKills(party.getKills() + 1);
party.updateParty();
plugin.getLoggerManager().logDebug(PartiesConstants.DEBUG_KILL_ADD
.replace("{party}", party.getName())
.replace("{player}", killer.getName()), true);
}
}
}
}
/**
* Initializes the game state, and sets up defaults
*/
public void startGame() {
Bukkit.getPluginManager().callEvent(new GameStartEvent());
final String MAPS_PATH = PLUGIN.getConfiguration().getSettingsConfig().getString("MapsPath");
final String ARENAS_PATH = PLUGIN.getConfiguration().getSettingsConfig().getString("ArenasPath");
this.mapSelection = VOTE_MANAGER.getWinningMap();
this.gameFile = new GameFile(ARENAS_PATH, mapSelection + ".yml");
this.arenaWorld = Bukkit.createWorld(new WorldCreator(MAPS_PATH + gameFile.getConfig().getString("World")));
this.arenaWorld.setSpawnFlags(false, false);
this.arenaWorld.setGameRuleValue("doMobSpawning", "false");
this.spawns = gameFile.getLocations(arenaWorld, "Spawns");
this.doors = gameFile.getDoors(arenaWorld);
this.checkpoints = gameFile.getCheckpoints(arenaWorld);
this.nukeRoom = gameFile.getLocation(arenaWorld, "Nukeroom");
this.handleStart();
VOTE_MANAGER.resetVotes();
Bukkit.getConsoleSender().sendMessage(Utils.color("&6Start game method"));
// Clear entities
for (Entity entity : arenaWorld.getEntities()) {
if (!(entity instanceof Player) && (entity instanceof Animals || entity instanceof Monster)) {
entity.remove();
}
}
}
public CreatureType getCreatureType(LivingEntity creature) {
if (creature instanceof Monster
|| creature instanceof WaterMob
|| creature instanceof Slime
|| creature instanceof Ghast) {
return CreatureType.MONSTER;
} else if (creature instanceof Animals) {
return CreatureType.ANIMAL;
} else if (creature instanceof Villager) {
return CreatureType.VILLAGER;
} else if (creature instanceof Golem) {
return CreatureType.GOLEM;
}
return CreatureType.UNKNOWN;
}
private void cancelMobDamage(EntityDamageByEntityEvent event) {
if (killAnimalsEnabled && event.getEntity() instanceof Animals) {
event.setCancelled(true);
} else if (killMonstersEnabled && event.getEntity() instanceof Monster) {
event.setCancelled(true);
}
}
/**
* Runs repeating tasks to deliver acid damage to mobs, etc.
* @param plugin - ASkyBlock plugin object - ASkyBlock plugin
*/
public AcidTask(final ASkyBlock plugin) {
this.plugin = plugin;
// Initialize water item list
itemsInWater = new HashSet<>();
// This part will kill monsters if they fall into the water
// because it
// is acid
if (Settings.mobAcidDamage > 0D || Settings.animalAcidDamage > 0D) {
plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
List<Entity> entList = ASkyBlock.getIslandWorld().getEntities();
for (Entity current : entList) {
if (plugin.isOnePointEight() && current instanceof Guardian) {
// Guardians are immune to acid too
continue;
}
if ((current instanceof Monster) && Settings.mobAcidDamage > 0D) {
if ((current.getLocation().getBlock().getType() == Material.WATER)
|| (current.getLocation().getBlock().getType() == Material.STATIONARY_WATER)) {
((Monster) current).damage(Settings.mobAcidDamage);
}
} else if ((current instanceof Animals) && Settings.animalAcidDamage > 0D) {
if ((current.getLocation().getBlock().getType() == Material.WATER)
|| (current.getLocation().getBlock().getType() == Material.STATIONARY_WATER)) {
if (!current.getType().equals(EntityType.CHICKEN) || Settings.damageChickens) {
((Animals) current).damage(Settings.animalAcidDamage);
}
}
}
}
}, 0L, 20L);
}
runAcidItemRemovalTask();
}
/**
* Removes monsters around location l
*
* @param l location
*/
public void removeMobs(final Location l) {
if (!inWorld(l)) {
return;
}
//plugin.getLogger().info("DEBUG: removing mobs");
// Don't remove mobs if at spawn
if (this.isAtSpawn(l)) {
//plugin.getLogger().info("DEBUG: at spawn!");
return;
}
final int px = l.getBlockX();
final int py = l.getBlockY();
final int pz = l.getBlockZ();
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
final Chunk c = l.getWorld().getChunkAt(new Location(l.getWorld(), px + x * 16, py, pz + z * 16));
if (c.isLoaded()) {
for (final Entity e : c.getEntities()) {
//plugin.getLogger().info("DEBUG: " + e.getType());
// Don't remove if the entity is an NPC or has a name tag
if (e.getCustomName() != null || e.hasMetadata("NPC"))
continue;
if (e instanceof Monster && !Settings.mobWhiteList.contains(e.getType())) {
e.remove();
}
}
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onMobKill(EntityDeathEvent event) {
Player killer = event.getEntity().getKiller(); //The killer is a player
Entity mob = event.getEntity();
if (mob == null || mob instanceof Player) {
return;
}
if (killer == null) {
return;
}
QPlayer qPlayer = QuestsAPI.getPlayerManager().getPlayer(killer.getUniqueId(), true);
QuestProgressFile questProgressFile = qPlayer.getQuestProgressFile();
for (Quest quest : super.getRegisteredQuests()) {
if (questProgressFile.hasStartedQuest(quest)) {
QuestProgress questProgress = questProgressFile.getQuestProgress(quest);
for (Task task : quest.getTasksOfType(super.getType())) {
TaskProgress taskProgress = questProgress.getTaskProgress(task.getId());
if (taskProgress.isCompleted()) {
continue;
}
boolean hostilitySpecified = false;
boolean hostile = false;
if (task.getConfigValue("hostile") != null) {
hostilitySpecified = true;
hostile = (boolean) task.getConfigValue("hostile");
}
if (hostilitySpecified) {
if (!hostile && !(mob instanceof Animals)) {
continue;
} else if (hostile && !(mob instanceof Monster)) {
continue;
}
}
int mobKillsNeeded = (int) task.getConfigValue("amount");
int progressKills;
if (taskProgress.getProgress() == null) {
progressKills = 0;
} else {
progressKills = (int) taskProgress.getProgress();
}
taskProgress.setProgress(progressKills + 1);
if (((int) taskProgress.getProgress()) >= mobKillsNeeded) {
taskProgress.setCompleted(true);
}
}
}
}
}
@Test
public void testGetEntity() {
List<Entity> testList = new ArrayList<>();
ArmorStand fakeArmorStand = mock(ArmorStand.class);
Cow fakeCow = mock(Cow.class);
Evoker fakeEvoker = mock(Evoker.class);
Guardian fakeGuardian = mock(Guardian.class);
Pig fakePig = mock(Pig.class);
PigZombie fakePigZombie = mock(PigZombie.class);
Pillager fakePillager = mock(Pillager.class);
Sheep fakeSheep = mock(Sheep.class);
Skeleton fakeSkeleton = mock(Skeleton.class);
Turtle fakeTurtle = mock(Turtle.class);
Villager fakeVillager = mock(Villager.class);
WanderingTrader fakeWanderingTrader = mock(WanderingTrader.class);
testList.add(fakeArmorStand);
testList.add(fakeCow);
testList.add(fakeEvoker);
testList.add(fakeGuardian);
testList.add(fakePig);
testList.add(fakePigZombie);
testList.add(fakePillager);
testList.add(fakeSheep);
testList.add(fakeSkeleton);
testList.add(fakeTurtle);
testList.add(fakeVillager);
testList.add(fakeWanderingTrader);
List<Sheep> sheepList = EntityUtil.getEntity(testList, Sheep.class);
assertEquals(1, sheepList.size());
assertEquals(fakeSheep, sheepList.get(0));
List<Animals> animalsList = EntityUtil.getAnimals(testList);
assertEquals(4, animalsList.size());
assertTrue(animalsList.contains(fakeCow));
assertTrue(animalsList.contains(fakePig));
assertTrue(animalsList.contains(fakeSheep));
assertTrue(animalsList.contains(fakeTurtle));
List<Monster> monsterList = EntityUtil.getMonsters(testList);
assertEquals(5, monsterList.size());
assertTrue(monsterList.contains(fakeEvoker));
assertTrue(monsterList.contains(fakeGuardian));
assertTrue(monsterList.contains(fakePigZombie));
assertTrue(monsterList.contains(fakePillager));
assertTrue(monsterList.contains(fakeSkeleton));
List<NPC> npcList = EntityUtil.getNPCs(testList);
assertEquals(2, npcList.size());
assertTrue(npcList.contains(fakeVillager));
assertTrue(npcList.contains(fakeWanderingTrader));
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onLingeringPotionDamage(final EntityDamageByEntityEvent e) {
if (!IslandGuard.inWorld(e.getEntity().getLocation())) {
return;
}
if (e.getEntity() == null || e.getEntity().getUniqueId() == null) {
return;
}
if (e.getCause().equals(DamageCause.ENTITY_ATTACK) && thrownPotions.containsKey(e.getDamager().getEntityId())) {
UUID attacker = thrownPotions.get(e.getDamager().getEntityId());
// Self damage
if (attacker.equals(e.getEntity().getUniqueId())) {
return;
}
Island island = plugin.getGrid().getIslandAt(e.getEntity().getLocation());
boolean inNether = false;
if (e.getEntity().getWorld().equals(ASkyBlock.getNetherWorld())) {
inNether = true;
}
// Monsters being hurt
if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime || e.getEntity() instanceof Squid) {
// Normal island check
if (island != null && island.getMembers().contains(attacker)) {
// Members always allowed
return;
}
if (actionAllowed(attacker, e.getEntity().getLocation(), SettingsFlag.HURT_MONSTERS)) {
return;
}
// Not allowed
e.setCancelled(true);
return;
}
// Mobs being hurt
if (e.getEntity() instanceof Animals || e.getEntity() instanceof IronGolem || e.getEntity() instanceof Snowman
|| e.getEntity() instanceof Villager) {
if (island != null && (island.getIgsFlag(SettingsFlag.HURT_MOBS) || island.getMembers().contains(attacker))) {
return;
}
e.setCancelled(true);
return;
}
// Establish whether PVP is allowed or not.
boolean pvp = false;
if ((inNether && island != null && island.getIgsFlag(SettingsFlag.NETHER_PVP) || (!inNether && island != null && island.getIgsFlag(SettingsFlag.PVP)))) {
pvp = true;
}
// Players being hurt PvP
if (e.getEntity() instanceof Player) {
if (pvp) {
} else {
e.setCancelled(true);
}
}
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onFishing(final PlayerFishEvent e) {
if (DEBUG) {
plugin.getLogger().info("Player fish event " + e.getEventName());
plugin.getLogger().info("Player fish event " + e.getCaught());
}
if (e.getCaught() == null)
return;
Player p = e.getPlayer();
if (!inWorld(p)) {
return;
}
if (p.isOp() || VaultHelper.checkPerm(p, Settings.PERMPREFIX + "mod.bypassprotect")) {
// You can do anything if you are Op of have the bypass
return;
}
// Handle rods
Island island = plugin.getGrid().getProtectedIslandAt(e.getCaught().getLocation());
// PVP check
if (e.getCaught() instanceof Player) {
// Check if this is the player who is holding the rod
if (e.getCaught().equals(e.getPlayer())) {
if (DEBUG)
plugin.getLogger().info("DEBUG: player cught themselves!");
return;
}
if (island == null
&& (e.getCaught().getWorld().getEnvironment().equals(Environment.NORMAL)
&& !Settings.defaultWorldSettings.get(SettingsFlag.PVP))
|| ((e.getCaught().getWorld().getEnvironment().equals(Environment.NETHER)
&& !Settings.defaultWorldSettings.get(SettingsFlag.NETHER_PVP)))) {
Util.sendMessage(e.getPlayer(), ChatColor.RED + plugin.myLocale(e.getPlayer().getUniqueId()).targetInNoPVPArea);
e.setCancelled(true);
e.getHook().remove();
return;
}
if (island != null && ((e.getCaught().getWorld().getEnvironment().equals(Environment.NORMAL) && !island.getIgsFlag(SettingsFlag.PVP))
|| (e.getCaught().getWorld().getEnvironment().equals(Environment.NETHER) && !island.getIgsFlag(SettingsFlag.NETHER_PVP)))) {
Util.sendMessage(e.getPlayer(), ChatColor.RED + plugin.myLocale(e.getPlayer().getUniqueId()).targetInNoPVPArea);
e.setCancelled(true);
e.getHook().remove();
return;
}
}
if (!plugin.getGrid().playerIsOnIsland(e.getPlayer())) {
if (e.getCaught() instanceof Animals) {
if (island == null && !Settings.defaultWorldSettings.get(SettingsFlag.HURT_MOBS)) {
Util.sendMessage(e.getPlayer(), ChatColor.RED + plugin.myLocale(e.getPlayer().getUniqueId()).islandProtected);
e.setCancelled(true);
e.getHook().remove();
return;
}
if (island != null) {
if ((!island.getIgsFlag(SettingsFlag.HURT_MOBS) && !island.getMembers().contains(p.getUniqueId()))) {
Util.sendMessage(e.getPlayer(), ChatColor.RED + plugin.myLocale(e.getPlayer().getUniqueId()).islandProtected);
e.setCancelled(true);
e.getHook().remove();
return;
}
}
}
// Monster protection
if (e.getCaught() instanceof Monster || e.getCaught() instanceof Squid || e.getCaught() instanceof Slime) {
if (island == null && !Settings.defaultWorldSettings.get(SettingsFlag.HURT_MONSTERS)) {
Util.sendMessage(e.getPlayer(), ChatColor.RED + plugin.myLocale(e.getPlayer().getUniqueId()).islandProtected);
e.setCancelled(true);
e.getHook().remove();
return;
}
if (island != null) {
if ((!island.getIgsFlag(SettingsFlag.HURT_MONSTERS) && !island.getMembers().contains(p.getUniqueId()))) {
Util.sendMessage(e.getPlayer(), ChatColor.RED + plugin.myLocale(e.getPlayer().getUniqueId()).islandProtected);
e.setCancelled(true);
e.getHook().remove();
}
}
}
}
}
/**
* Checks for splash damage. If there is any to any affected entity and it's not allowed, it won't work on any of them.
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onSplashPotionSplash(final PotionSplashEvent e) {
if (DEBUG) {
plugin.getLogger().info(e.getEventName());
plugin.getLogger().info("splash entity = " + e.getEntity());
plugin.getLogger().info("splash entity type = " + e.getEntityType());
plugin.getLogger().info("splash affected entities = " + e.getAffectedEntities());
//plugin.getLogger().info("splash hit entity = " + e.getHitEntity());
}
if (!IslandGuard.inWorld(e.getEntity().getLocation())) {
return;
}
// Try to get the shooter
Projectile projectile = e.getEntity();
if (DEBUG)
plugin.getLogger().info("splash shooter = " + projectile.getShooter());
if (projectile.getShooter() != null && projectile.getShooter() instanceof Player) {
Player attacker = (Player)projectile.getShooter();
// Run through all the affected entities
for (LivingEntity entity: e.getAffectedEntities()) {
if (DEBUG)
plugin.getLogger().info("DEBUG: affected splash entity = " + entity);
// Self damage
if (attacker.equals(entity)) {
if (DEBUG)
plugin.getLogger().info("DEBUG: Self damage from splash potion!");
continue;
}
Island island = plugin.getGrid().getIslandAt(entity.getLocation());
boolean inNether = false;
if (entity.getWorld().equals(ASkyBlock.getNetherWorld())) {
inNether = true;
}
// Monsters being hurt
if (entity instanceof Monster || entity instanceof Slime || entity instanceof Squid) {
// Normal island check
if (island != null && island.getMembers().contains(attacker.getUniqueId())) {
// Members always allowed
continue;
}
if (actionAllowed(attacker, entity.getLocation(), SettingsFlag.HURT_MONSTERS)) {
continue;
}
// Not allowed
Util.sendMessage(attacker, ChatColor.RED + plugin.myLocale(attacker.getUniqueId()).islandProtected);
e.setCancelled(true);
return;
}
// Mobs being hurt
if (entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman
|| entity instanceof Villager) {
if (island != null && (island.getIgsFlag(SettingsFlag.HURT_MOBS) || island.getMembers().contains(attacker.getUniqueId()))) {
continue;
}
if (DEBUG)
plugin.getLogger().info("DEBUG: Mobs not allowed to be hurt. Blocking");
Util.sendMessage(attacker, ChatColor.RED + plugin.myLocale(attacker.getUniqueId()).islandProtected);
e.setCancelled(true);
return;
}
// Establish whether PVP is allowed or not.
boolean pvp = false;
if ((inNether && island != null && island.getIgsFlag(SettingsFlag.NETHER_PVP) || (!inNether && island != null && island.getIgsFlag(SettingsFlag.PVP)))) {
if (DEBUG) plugin.getLogger().info("DEBUG: PVP allowed");
pvp = true;
}
// Players being hurt PvP
if (entity instanceof Player) {
if (pvp) {
if (DEBUG) plugin.getLogger().info("DEBUG: PVP allowed");
} else {
if (DEBUG) plugin.getLogger().info("DEBUG: PVP not allowed");
Util.sendMessage(attacker, ChatColor.RED + plugin.myLocale(attacker.getUniqueId()).targetInNoPVPArea);
e.setCancelled(true);
return;
}
}
}
}
}
/**
* Prevents mobs spawning naturally at spawn or in an island
*
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onNaturalMobSpawn(final CreatureSpawnEvent e) {
// if grid is not loaded yet, return.
if (plugin.getGrid() == null) {
return;
}
// If not in the right world, return
if (!IslandGuard.inWorld(e.getEntity())) {
return;
}
// Deal with natural spawning
if (e.getSpawnReason().equals(SpawnReason.NATURAL)
|| e.getSpawnReason().equals(SpawnReason.CHUNK_GEN)
|| e.getSpawnReason().equals(SpawnReason.DEFAULT)
|| e.getSpawnReason().equals(SpawnReason.MOUNT)
|| e.getSpawnReason().equals(SpawnReason.JOCKEY)
|| e.getSpawnReason().equals(SpawnReason.NETHER_PORTAL)) {
if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime) {
if (!actionAllowed(e.getLocation(), SettingsFlag.MONSTER_SPAWN)) {
if (DEBUG3)
plugin.getLogger().info("Natural monster spawn cancelled.");
// Mobs not allowed to spawn
e.setCancelled(true);
return;
}
} else if (e.getEntity() instanceof Animals) {
if (!actionAllowed(e.getLocation(), SettingsFlag.MOB_SPAWN)) {
// Animals are not allowed to spawn
if (DEBUG2)
plugin.getLogger().info("Natural animal spawn cancelled.");
e.setCancelled(true);
return;
}
}
}
if (DEBUG2) {
plugin.getLogger().info("Mob spawn allowed " + e.getEventName());
plugin.getLogger().info(e.getSpawnReason().toString());
plugin.getLogger().info(e.getEntityType().toString());
}
}