Skip to content

Commit

Permalink
Merge pull request #298 from lokka30/3.1-dev
Browse files Browse the repository at this point in the history
3.1 dev
  • Loading branch information
lokka30 authored Sep 21, 2021
2 parents 4e7b7f9 + 4440b22 commit 5ce959d
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 71 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>me.lokka30</groupId>
<artifactId>LevelledMobs</artifactId>
<version>3.1.7 b508</version>
<version>3.1.8 b517</version>
<packaging>jar</packaging>

<name>LevelledMobs</name>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/me/lokka30/levelledmobs/Companion.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ public boolean loadFiles(final boolean isReload) {
Utils.logger.info("misc debugs enabled: &b" + debugsEnabled);

main.configUtils.load();
main.playerLevellingDistance = main.helperSettings.getDouble(main.settingsCfg, "player-levelling-mob-distance-squared", 150);
main.playerLevellingMinRelevelTime = main.helperSettings.getInt(main.settingsCfg, "player-levelling-relevel-min-time", 5000);

return true;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/me/lokka30/levelledmobs/LevelledMobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class LevelledMobs extends JavaPlugin {
public PlaceholderApiIntegration placeholderApiIntegration;
public boolean migratedFromPre30;
public YmlParsingHelper helperSettings;
public double playerLevellingDistance;
public int playerLevellingMinRelevelTime;

// Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
package me.lokka30.levelledmobs.listeners;

import me.lokka30.levelledmobs.LevelledMobs;
import me.lokka30.levelledmobs.managers.ExternalCompatibilityManager;
import me.lokka30.levelledmobs.misc.*;
import me.lokka30.levelledmobs.rules.LevelledMobSpawnReason;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
Expand All @@ -27,8 +24,8 @@
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.*;
import java.util.stream.Collectors;

/**
* This class handles mob spawning on the server,
Expand Down Expand Up @@ -56,14 +53,18 @@ public void onEntitySpawn(@NotNull final EntitySpawnEvent event) {

if (event instanceof CreatureSpawnEvent && ((CreatureSpawnEvent) event).getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.CUSTOM) &&
!lmEntity.isLevelled()) {

if (main.configUtils.playerLevellingEnabled && lmEntity.getPlayerForLevelling() == null)
updateMobForPlayerLevelling(lmEntity);

delayedAddToQueue(lmEntity, event, 20);
return;
}

if (!processMobSpawns) return;

if (main.configUtils.playerLevellingEnabled)
getClosestPlayer(lmEntity);
if (main.configUtils.playerLevellingEnabled && lmEntity.getPlayerForLevelling() == null)
updateMobForPlayerLevelling(lmEntity);

final int mobProcessDelay = main.helperSettings.getInt(main.settingsCfg, "mob-process-delay", 0);

Expand All @@ -73,38 +74,58 @@ public void onEntitySpawn(@NotNull final EntitySpawnEvent event) {
main._mobsQueueManager.addToQueue(new QueueItem(lmEntity, event));
}

private LevelledMobSpawnReason adaptVanillaSpawnReason(final CreatureSpawnEvent.SpawnReason spawnReason) {
return LevelledMobSpawnReason.valueOf(spawnReason.toString());
}

private void getClosestPlayer(final @NotNull LivingEntityWrapper lmEntity) {
if (lmEntity.getPlayerForLevelling() != null) return;
private void updateMobForPlayerLevelling(final LivingEntityWrapper lmEntity){
final int onlinePlayerCount = Bukkit.getOnlinePlayers().size();
final int checkDistance = main.helperSettings.getInt(main.settingsCfg, "async-task-max-blocks-from-player", 100);
final List<org.bukkit.entity.Player> playerList = onlinePlayerCount <= 10 ?
getPlayersMethod1(lmEntity.getLivingEntity(), checkDistance) :
getPlayersMethod2(lmEntity.getLivingEntity(), checkDistance);

Entity closestPlayer = null;
double closestRange = Double.MAX_VALUE;
final int checkDistance = main.helperSettings.getInt(main.settingsCfg,"async-task-max-blocks-from-player", 100);

for (final Entity entity : lmEntity.getLivingEntity().getNearbyEntities(checkDistance, checkDistance, checkDistance)) {
if (!(entity instanceof Player)) continue;
if (((Player) entity).getGameMode().equals(GameMode.SPECTATOR)) continue;

if (entity.getLocation().getWorld() == null || !entity.getLocation().getWorld().getUID().equals(lmEntity.getWorld().getUID()))
Player closestPlayer = null;
for (final org.bukkit.entity.Player player : playerList) {
if (ExternalCompatibilityManager.isMobOfCitizens(player))
continue;

final double range = entity.getLocation().distanceSquared(lmEntity.getLocation());
if (range < closestRange && range <= main.playerLevellingDistance){
closestPlayer = entity;
closestRange = range;
}
closestPlayer = player;
break;
}

if (closestPlayer != null) {
synchronized (lmEntity.getLivingEntity().getPersistentDataContainer()){
lmEntity.getLivingEntity().getPersistentDataContainer().set(main.levelManager.playerLevelling_Id, PersistentDataType.STRING, closestPlayer.getUniqueId().toString());
}
if (closestPlayer == null) return;

lmEntity.setPlayerForLevelling((Player) closestPlayer);
synchronized (lmEntity.getLivingEntity().getPersistentDataContainer()) {
lmEntity.getPDC().set(main.levelManager.playerLevelling_Id, PersistentDataType.STRING, closestPlayer.getUniqueId().toString());
}

lmEntity.setPlayerForLevelling(closestPlayer);
}

@NotNull
private static List<org.bukkit.entity.Player> getPlayersMethod1(final LivingEntity mob, final int checkDistance){
final double maxDistanceSquared = checkDistance * 4;

return Bukkit.getOnlinePlayers().stream()
.filter(p -> mob.getWorld().equals(p.getWorld()))
.filter(p -> !p.getGameMode().equals(GameMode.SPECTATOR))
.map(p -> Map.entry(mob.getLocation().distanceSquared(p.getLocation()), p))
.filter(e -> e.getKey() <= maxDistanceSquared)
.sorted(Comparator.comparingDouble(Map.Entry::getKey))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}

@NotNull
private static List<org.bukkit.entity.Player> getPlayersMethod2(final LivingEntity mob, final int checkDistance){
return mob.getNearbyEntities(checkDistance, checkDistance, checkDistance).stream()
.filter(e -> e instanceof org.bukkit.entity.Player)
.filter(e -> !((org.bukkit.entity.Player) e).getGameMode().equals(GameMode.SPECTATOR))
.map(e -> Map.entry(mob.getLocation().distanceSquared(e.getLocation()), (org.bukkit.entity.Player) e))
.sorted(Comparator.comparingDouble(Map.Entry::getKey))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}

private LevelledMobSpawnReason adaptVanillaSpawnReason(final CreatureSpawnEvent.SpawnReason spawnReason) {
return LevelledMobSpawnReason.valueOf(spawnReason.toString());
}

private void delayedAddToQueue(final LivingEntityWrapper lmEntity, final Event event, final int delay){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.persistence.PersistentDataType;
Expand Down Expand Up @@ -95,18 +96,22 @@ public static boolean hasProtocolLibInstalled() {
}

public static boolean hasMythicMobsInstalled() {
final Plugin p = Bukkit.getPluginManager().getPlugin("MythicMobs");
if (p != null && p.getDescription().getVersion().startsWith("5"))
return false;
else
return p != null;
return Bukkit.getPluginManager().getPlugin("MythicMobs") != null;
}

public static boolean hasWorldGuardInstalled() {
return Bukkit.getPluginManager().getPlugin("WorldGuard") != null;
}

public static boolean isMythicMob(@NotNull final LivingEntityWrapper lmEntity) {
final Plugin p = Bukkit.getPluginManager().getPlugin("MythicMobs");
if (p == null) return false;

if (!p.getDescription().getVersion().startsWith("4")) {
final NamespacedKey mmKey = new NamespacedKey(p, "type");
return lmEntity.getPDC().has(mmKey, PersistentDataType.STRING);
}

if (lmEntity.getLivingEntity().hasMetadata("mythicmob")){
final List<MetadataValue> metadatas = lmEntity.getLivingEntity().getMetadata("mythicmob");
for (final MetadataValue md : metadatas){
Expand All @@ -126,7 +131,7 @@ public static String getMythicMobInternalName(@NotNull final LivingEntityWrapper

if (!p.getDescription().getVersion().startsWith("4")) {
// MM version 5 must use this method for internal name detection
NamespacedKey mmKey = new NamespacedKey(p, "type");
final NamespacedKey mmKey = new NamespacedKey(p, "type");
if (lmEntity.getPDC().has(mmKey, PersistentDataType.STRING)) {
final String type = lmEntity.getPDC().get(mmKey, PersistentDataType.STRING);
return type == null ? "" : type;
Expand Down Expand Up @@ -274,13 +279,17 @@ public static boolean isMobOfInfernalMobs(final LivingEntityWrapper lmEntity) {
* @return if Citizens compatibility enabled and entity is from Citizens
*/
public static boolean isMobOfCitizens(final LivingEntityWrapper lmEntity) {
final boolean isExternalType = lmEntity.getLivingEntity().hasMetadata("NPC");
final boolean isExternalType = isMobOfCitizens(lmEntity.getLivingEntity());

if (isExternalType) lmEntity.setMobExternalType(ExternalCompatibility.CITIZENS);

return isExternalType;
}

public static boolean isMobOfCitizens(final LivingEntity livingEntity) {
return livingEntity.hasMetadata("NPC");
}

/**
* @param lmEntity mob to check
* @return if Shopkeepers compatibility enabled and entity is from Shopkeepers
Expand Down
46 changes: 20 additions & 26 deletions src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

/**
* Generates levels and manages other functions related to levelling mobs
Expand Down Expand Up @@ -661,7 +662,7 @@ private void runNametagCheck_aSync(final Map<Player,List<Entity>> entitiesPerPla

// Mob must be a livingentity that is ...living.
if (!(entity instanceof LivingEntity) || entity instanceof Player) continue;
LivingEntityWrapper lmEntity = new LivingEntityWrapper((LivingEntity) entity, main);
final LivingEntityWrapper lmEntity = new LivingEntityWrapper((LivingEntity) entity, main);

if (lmEntity.isLevelled()) {
if (main.configUtils.playerLevellingEnabled) {
Expand Down Expand Up @@ -698,34 +699,26 @@ else if (levellableState == LevellableState.ALLOWED)
}

private void checkEntityForPlayerLevelling(final LivingEntityWrapper lmEntity, final List<Player> players){
Player closestPlayer = players.get(0);
double closestDistance = Double.MAX_VALUE;
final LivingEntity mob = lmEntity.getLivingEntity();

if (players.size() > 1) {
for (final Player p : players) {
if (p.getGameMode().equals(GameMode.SPECTATOR)) continue;
if (p.getLocation().getWorld() == null || mob.getLocation().getWorld() == null ||
!p.getLocation().getWorld().getUID().equals(mob.getLocation().getWorld().getUID()))
continue;

double distance = mob.getLocation().distanceSquared(p.getLocation());
if (distance < closestDistance) {
closestPlayer = p;
closestDistance = distance;
}
}
} else {
if (closestPlayer.getGameMode().equals(GameMode.SPECTATOR) ||
closestPlayer.getLocation().getWorld() == null || mob.getLocation().getWorld() == null ||
!closestPlayer.getLocation().getWorld().getUID().equals(mob.getLocation().getWorld().getUID()))
return;

closestDistance = mob.getLocation().distanceSquared(closestPlayer.getLocation());
final List<Player> sortedPlayers = players.stream()
.filter(p -> mob.getWorld().equals(p.getWorld()))
.filter(p -> !p.getGameMode().equals(GameMode.SPECTATOR))
.map(p -> Map.entry(mob.getLocation().distanceSquared(p.getLocation()), p))
.sorted(Comparator.comparingDouble(Map.Entry::getKey))
.map(Map.Entry::getValue)
.collect(Collectors.toList());

Player closestPlayer = null;
for (final Player player : sortedPlayers){
if (ExternalCompatibilityManager.isMobOfCitizens(player))
continue;

closestPlayer = player;
break;
}

if (closestDistance <= main.playerLevellingDistance &&
doesMobNeedRelevelling(mob, closestPlayer)) {
if (closestPlayer == null) return;
if (doesMobNeedRelevelling(mob, closestPlayer)) {

synchronized (mob.getPersistentDataContainer()) {
mob.getPersistentDataContainer().set(main.levelManager.playerLevelling_Id, PersistentDataType.STRING, closestPlayer.getUniqueId().toString());
Expand Down Expand Up @@ -768,6 +761,7 @@ private boolean doesMobNeedRelevelling(final @NotNull LivingEntity mob, final @N

String playerId;
main.playerLevellingEntities.put(mob, Instant.now());
if (main.playerLevellingMinRelevelTime <= 0) return false;

synchronized (mob.getPersistentDataContainer()) {
if (!mob.getPersistentDataContainer().has(main.levelManager.playerLevelling_Id, PersistentDataType.STRING))
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ mobs-multiply-head-drops: false
# || Do not touch this unless a LM developer tells you to.
# || These settings pertian to Player Levelling, obviously
# || this only affects LM if you are using Player Levelling.
player-levelling-mob-distance-squared: 250
player-levelling-relevel-min-time: 5000

# || ADVANCED USERS ONLY
Expand Down

0 comments on commit 5ce959d

Please sign in to comment.