Skip to content

Commit

Permalink
Merge pull request #320 from lokka30/3.2-dev
Browse files Browse the repository at this point in the history
3.2 dev
  • Loading branch information
lokka30 authored Nov 5, 2021
2 parents e447614 + 1950080 commit a2247c2
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 59 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.2.3 b546</version>
<version>3.2.4 b557</version>
<packaging>jar</packaging>

<name>LevelledMobs</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ private void forceRelevel(final CommandSender sender, final String label, @NotNu
int worldCount = 0;
int entityCount = 0;

main.reloadLM(sender);

for (final World world : Bukkit.getWorlds()) {
worldCount++;
for (final Entity entity : world.getEntities()) {
Expand All @@ -138,13 +140,15 @@ private void forceRelevel(final CommandSender sender, final String label, @NotNu
entityCount++;
final LivingEntityWrapper lmEntity = LivingEntityWrapper.getInstance((LivingEntity) entity, main);
lmEntity.reEvaluateLevel = true;
lmEntity.isRulesForceAll = true;
lmEntity.wasPreviouslyLevelled = lmEntity.isLevelled();
main._mobsQueueManager.addToQueue(new QueueItem(lmEntity, null));
lmEntity.free();
}
}

sender.sendMessage(MessageUtils.colorizeAll(String.format(
"%s Checked &b%s&7 mobs in &b%s&7 world(s)",
"%s Rules Reprocessed for &b%s&7 mobs in &b%s&7 world(s)",
label, entityCount, worldCount)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public void preprocessMob(final @NotNull LivingEntityWrapper lmEntity, @NotNull

if (!lmEntity.reEvaluateLevel)
lmEntity.setSpawnReason(spawnReason);
else if (main.configUtils.playerLevellingEnabled){
else if (main.configUtils.playerLevellingEnabled && lmEntity.isRulesForceAll){
synchronized (lmEntity.getLivingEntity().getPersistentDataContainer()){
if (lmEntity.getPDC().has(main.namespaced_keys.playerLevelling_Id, PersistentDataType.STRING))
lmEntity.getPDC().remove(main.namespaced_keys.playerLevelling_Id);
Expand All @@ -254,8 +254,14 @@ else if (main.configUtils.playerLevellingEnabled){
final HashSet<AdditionalLevelInformation> additionalLevelInfo = new HashSet<>(Collections.singletonList(additionalInfo));
final LevellableState levellableState = getLevellableState(lmEntity, event);
if (levellableState == LevellableState.ALLOWED) {
if (lmEntity.reEvaluateLevel && main.configUtils.playerLevellingEnabled)
updateMobForPlayerLevelling(lmEntity);
if (lmEntity.reEvaluateLevel && main.configUtils.playerLevellingEnabled) {
final Object syncObj = new Object();
final BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() { updateMobForPlayerLevelling(lmEntity); }
};
runnable.runTask(main);
}

main.levelInterface.applyLevelToMob(lmEntity, main.levelInterface.generateLevel(lmEntity),
false, false, additionalLevelInfo);
Expand All @@ -266,13 +272,15 @@ else if (main.configUtils.playerLevellingEnabled){
// Check if the mob is already levelled - if so, remove their level
if (lmEntity.isLevelled())
main.levelInterface.removeLevel(lmEntity);

else if (lmEntity.isBabyMob()) {
// add a tag so we can potentially level the mob when/if it ages
synchronized (lmEntity.getLivingEntity().getPersistentDataContainer()) {
lmEntity.getPDC().set(main.namespaced_keys.wasBabyMobKey, PersistentDataType.INTEGER, 1);
}
}

if (lmEntity.wasPreviouslyLevelled)
main.levelManager.updateNametag(lmEntity);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ 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.12")) {
if (!p.getDescription().getVersion().startsWith("4.12") && !p.getDescription().getVersion().startsWith("5.")) {
final NamespacedKey mmKey = new NamespacedKey(p, "type");
synchronized (lmEntity.getLivingEntity().getPersistentDataContainer()) {
return lmEntity.getPDC().has(mmKey, PersistentDataType.STRING);
Expand All @@ -153,7 +153,7 @@ public static String getMythicMobInternalName(@NotNull final LivingEntityWrapper
final Plugin p = Bukkit.getPluginManager().getPlugin("MythicMobs");
if (p == null) return "";

if (!p.getDescription().getVersion().startsWith("4")) {
if (!p.getDescription().getVersion().startsWith("4.12") && !p.getDescription().getVersion().startsWith("5.")) {
// MM version 5 must use this method for internal name detection
final NamespacedKey mmKey = new NamespacedKey(p, "type");
synchronized (lmEntity.getLivingEntity().getPersistentDataContainer()) {
Expand All @@ -179,7 +179,7 @@ public static String getMythicMobInternalName(@NotNull final LivingEntityWrapper
return "";
}

public static LevellableState checkAllExternalCompats(final LivingEntityWrapper lmEntity, final LevelledMobs main){
public static LevellableState checkAllExternalCompats(final LivingEntityWrapper lmEntity, final @NotNull LevelledMobs main){
final Map<ExternalCompatibilityManager.ExternalCompatibility, Boolean> compatRules = main.rulesManager.getRule_ExternalCompatibility(lmEntity);

LevellableState result = LevellableState.ALLOWED;
Expand Down Expand Up @@ -280,26 +280,28 @@ public static boolean isMobOfMythicMobs(final LivingEntityWrapper lmEntity) {
* @param lmEntity mob to check
* @return if EliteMobs compatibility enabled and entity is from EliteMobs
*/
public static boolean isMobOfEliteMobs(final LivingEntityWrapper lmEntity) {
final boolean isExternalType1 =
lmEntity.getLivingEntity().hasMetadata("Elitemob");
final boolean isExternalType2 =
lmEntity.getLivingEntity().hasMetadata("Elitemobs_NPC");
final boolean isExternalType3 =
lmEntity.getLivingEntity().hasMetadata("Supermob");

if (isExternalType1) lmEntity.setMobExternalType(ExternalCompatibility.ELITE_MOBS);
else if (isExternalType2) lmEntity.setMobExternalType(ExternalCompatibility.ELITE_MOBS_NPCS);
else if (isExternalType3) lmEntity.setMobExternalType(ExternalCompatibility.ELITE_MOBS_SUPER_MOBS);

return (isExternalType1 || isExternalType2 || isExternalType3);
public static boolean isMobOfEliteMobs(final @NotNull LivingEntityWrapper lmEntity) {
final Plugin p = Bukkit.getPluginManager().getPlugin("EliteMobs");
if (p != null){
boolean isEliteMob;
synchronized (lmEntity.getLivingEntity().getPersistentDataContainer()) {
isEliteMob = lmEntity.getPDC().has(new NamespacedKey(p, "EliteMobsCullable"), PersistentDataType.STRING);
}

if (isEliteMob){
lmEntity.setMobExternalType(ExternalCompatibility.ELITE_MOBS);
return true;
}
}

return false;
}

/**
* @param lmEntity mob to check
* @return if InfernalMobs compatibility enabled and entity is from InfernalMobs
*/
public static boolean isMobOfInfernalMobs(final LivingEntityWrapper lmEntity) {
public static boolean isMobOfInfernalMobs(final @NotNull LivingEntityWrapper lmEntity) {
final boolean isExternalType = lmEntity.getLivingEntity().hasMetadata("infernalMetadata");

if (isExternalType) lmEntity.setMobExternalType(ExternalCompatibility.INFERNAL_MOBS);
Expand All @@ -311,23 +313,23 @@ public static boolean isMobOfInfernalMobs(final LivingEntityWrapper lmEntity) {
* @param lmEntity mob to check
* @return if Citizens compatibility enabled and entity is from Citizens
*/
public static boolean isMobOfCitizens(final LivingEntityWrapper lmEntity) {
public static boolean isMobOfCitizens(final @NotNull LivingEntityWrapper lmEntity) {
final boolean isExternalType = isMobOfCitizens(lmEntity.getLivingEntity());

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

return isExternalType;
}

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

/**
* @param lmEntity mob to check
* @return if Shopkeepers compatibility enabled and entity is from Shopkeepers
*/
public static boolean isMobOfShopkeepers(final LivingEntityWrapper lmEntity) {
public static boolean isMobOfShopkeepers(final @NotNull LivingEntityWrapper lmEntity) {
final boolean isExternalType = lmEntity.getLivingEntity().hasMetadata("shopkeeper");

if (isExternalType) lmEntity.setMobExternalType(ExternalCompatibility.SHOPKEEPERS);
Expand Down
35 changes: 17 additions & 18 deletions src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public int getLevelledExpDrops(@NotNull final LivingEntityWrapper lmEntity, fina

@Nullable
public String getNametag(final LivingEntityWrapper lmEntity, final boolean isDeathNametag) {
final String nametag = isDeathNametag ? main.rulesManager.getRule_Nametag_CreatureDeath(lmEntity) : main.rulesManager.getRule_Nametag(lmEntity);
String nametag = isDeathNametag ? main.rulesManager.getRule_Nametag_CreatureDeath(lmEntity) : main.rulesManager.getRule_Nametag(lmEntity);
if ("disabled".equalsIgnoreCase(nametag) || "none".equalsIgnoreCase(nametag)) return null;

final boolean useCustomNameForNametags = main.helperSettings.getBoolean(main.settingsCfg, "use-customname-for-mob-nametags");
Expand All @@ -446,12 +446,15 @@ public String getNametag(final LivingEntityWrapper lmEntity, final boolean isDea
else
return lmEntity.getLivingEntity().getCustomName(); // CustomName can be null, that is meant to be the case.
}
if (!lmEntity.isLevelled())
nametag = "";

return updateNametag(lmEntity, nametag, useCustomNameForNametags);
}

@NotNull
public String updateNametag(final LivingEntityWrapper lmEntity, @NotNull String nametag, final boolean useCustomNameForNametags) {
if ("".equals(nametag)) return nametag;
final String overridenName = main.rulesManager.getRule_EntityOverriddenName(lmEntity, useCustomNameForNametags);

String displayName = overridenName == null ?
Expand Down Expand Up @@ -570,6 +573,9 @@ public String replaceStringPlaceholders(final String nametag, @NotNull final Liv
result = result.replace("%y%", lmEntity.getLivingEntity().getLocation().getBlockY() + "");
result = result.replace("%z%", lmEntity.getLivingEntity().getLocation().getBlockZ() + "");

if (result.contains("%") && ExternalCompatibilityManager.hasPAPI_Installed())
result = ExternalCompatibilityManager.getPAPI_Placeholder(null, result);

result = MessageUtils.colorizeAll(result);

return result;
Expand Down Expand Up @@ -690,6 +696,7 @@ private void runNametagCheck_aSync(final @NotNull Map<Player,List<Entity>> entit
}

boolean useResetTimer = false;
if (lmEntity.getLivingEntity() == null) continue;
final List<NametagVisibilityEnum> nametagVisibilityEnums = main.rulesManager.getRule_CreatureNametagVisbility(lmEntity);
final int nametagVisibleTime = lmEntity.getNametagCooldownTime();
if (nametagVisibleTime > 0 &&
Expand Down Expand Up @@ -769,6 +776,7 @@ private void checkEntityForPlayerLevelling(final @NotNull LivingEntityWrapper lm
}

private void checkLevelledEntity(@NotNull final LivingEntityWrapper lmEntity, @NotNull final Player player){
if (lmEntity.getLivingEntity() == null || !lmEntity.getLivingEntity().isValid()) return;
final double maxDistance = Math.pow(128, 2); // square the distance we are using Location#distanceSquared. This is because it is faster than Location#distance since it does not need to sqrt which is taxing on the CPU.
final Location location = player.getLocation();

Expand All @@ -778,14 +786,13 @@ private void checkLevelledEntity(@NotNull final LivingEntityWrapper lmEntity, @N
} else if (lmEntity.isMobTamed() && main.rulesManager.getRule_MobTamedStatus(lmEntity) == MobTamedStatus.NOT_TAMED) {
// mob is tamed with a level but the rules don't allow it, remove the level
main.levelInterface.removeLevel(lmEntity);
} else {
if (!main.helperSettings.getBoolean(main.settingsCfg, "use-customname-for-mob-nametags", false) &&
location.getWorld() != null &&
location.getWorld().equals(lmEntity.getWorld()) &&
lmEntity.getLocation().distanceSquared(location) <= maxDistance) {
//if within distance, update nametag.
main.nametagQueueManager_.addToQueue(new QueueItem(lmEntity, main.levelManager.getNametag(lmEntity, false), Collections.singletonList(player)));
}
} else if (lmEntity.getLivingEntity().isValid() &&
!main.helperSettings.getBoolean(main.settingsCfg, "use-customname-for-mob-nametags", false) &&
location.getWorld() != null &&
location.getWorld().equals(lmEntity.getWorld()) &&
lmEntity.getLocation().distanceSquared(location) <= maxDistance) {
//if within distance, update nametag.
main.nametagQueueManager_.addToQueue(new QueueItem(lmEntity, main.levelManager.getNametag(lmEntity, false), Collections.singletonList(player)));
}
}

Expand Down Expand Up @@ -1053,12 +1060,8 @@ public void applyLevelToMob(@NotNull final LivingEntityWrapper lmEntity, int lev
&& lmEntity.getLivingEntity().getVehicle() instanceof LivingEntity){
// entity is a passenger. grab the level from the "vehicle" entity
final LivingEntityWrapper vehicle = LivingEntityWrapper.getInstance((LivingEntity) lmEntity.getLivingEntity().getVehicle(), main);
if (vehicle.isLevelled()) {
Utils.logger.info(lmEntity.getNameIfBaby() + " got level " + vehicle.getMobLevel() + " from vehicle " + vehicle.getNameIfBaby());
if (vehicle.isLevelled())
level = vehicle.getMobLevel();
}
else
Utils.logger.info(lmEntity.getNameIfBaby() + " vehicle was unlevelled: " + vehicle.getNameIfBaby());

vehicle.free();
}
Expand Down Expand Up @@ -1151,10 +1154,6 @@ else if (lmEntity.getLivingEntity() instanceof Horse)
lmEntity.inUseCount.getAndIncrement();
applyAttribs.runTask(main);

// final List<NametagVisibilityEnum> nametagVisibilityEnums = main.rulesManager.getRule_CreatureNametagVisbility(lmEntity);
// if (nametagVisibilityEnums.contains(NametagVisibilityEnum.TARGETED))
// getPlayersNearMob(lmEntity);

if (!skipLM_Nametag)
main.levelManager.updateNametag_WithDelay(lmEntity);
main.levelManager.applyLevelledEquipment(lmEntity, lmEntity.getMobLevel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import me.lokka30.levelledmobs.LevelledMobs;
import me.lokka30.levelledmobs.misc.*;
import me.lokka30.levelledmobs.rules.NametagVisibilityEnum;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
Expand Down Expand Up @@ -68,6 +69,7 @@ public void stop(){
}

public void addToQueue(final @NotNull QueueItem item) {
if (Bukkit.getOnlinePlayers().size() == 0) return;
if (item.lmEntity.getLivingEntity() == null ||
main.rulesManager.getRule_CreatureNametagVisbility(item.lmEntity).contains(NametagVisibilityEnum.DISABLED))
return;
Expand All @@ -88,7 +90,7 @@ private void main() throws InterruptedException{

final int nametagTimerResetTime = item.lmEntity.getNametagCooldownTime();

if (nametagTimerResetTime > 0) {
if (nametagTimerResetTime > 0 && !"".equals(item.nametag)) {
synchronized (NametagTimerChecker.nametagTimer_Lock) {
final Map<Player, WeakHashMap<LivingEntity, Instant>> nametagCooldownQueue = main.nametagTimerChecker.getNametagCooldownQueue();

Expand Down
Loading

0 comments on commit a2247c2

Please sign in to comment.