Skip to content

Commit

Permalink
Merge pull request #335 from lokka30/3.2-dev
Browse files Browse the repository at this point in the history
3.2 dev
  • Loading branch information
lokka30 authored Dec 1, 2021
2 parents 00e8df7 + 7806d98 commit 35a283c
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ LevelledMobs.iml
.project
/.settings
.idea/jarRepositories.xml
.idea/discord.xml
3 changes: 2 additions & 1 deletion .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.5 b564</version>
<version>3.2.6 b576</version>
<packaging>jar</packaging>

<name>LevelledMobs</name>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/me/lokka30/levelledmobs/Companion.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;

import javax.annotation.Nullable;
import java.io.*;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -47,13 +49,15 @@ public class Companion {

Companion(final LevelledMobs main) {
this.main = main;
this.recentlyJoinedPlayers = new WeakHashMap<>();
this.updateResult = new LinkedList<>();
buildUniversalGroups();
this.metricsInfo = new MetricsInfo(main);
this.spawner_CopyIds = new LinkedList<>();
this.spawner_InfoIds = new LinkedList<>();
}

final private WeakHashMap<Player, Instant> recentlyJoinedPlayers;
public HashSet<EntityType> groups_HostileMobs;
public HashSet<EntityType> groups_AquaticMobs;
public HashSet<EntityType> groups_PassiveMobs;
Expand All @@ -64,6 +68,7 @@ public class Companion {
public boolean playerInteractListenerIsRegistered;
final private PluginManager pluginManager = Bukkit.getPluginManager();
final private MetricsInfo metricsInfo;
final static private Object playerLogonTimes_Lock = new Object();

void checkWorldGuard() {
// Hook into WorldGuard
Expand Down Expand Up @@ -376,4 +381,23 @@ private void buildUniversalGroups(){
EntityType.TURTLE
).collect(Collectors.toCollection(HashSet::new));
}

public void addRecentlyJoinedPlayer(final Player player){
synchronized (playerLogonTimes_Lock){
recentlyJoinedPlayers.put(player, Instant.now());
}
}

@Nullable
public Instant getRecentlyJoinedPlayerLogonTime(final Player player){
synchronized (playerLogonTimes_Lock){
return recentlyJoinedPlayers.get(player);
}
}

public void removeRecentlyJoinedPlayer(final Player player){
synchronized (playerLogonTimes_Lock){
recentlyJoinedPlayers.remove(player);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -55,6 +56,7 @@ public void onEntitySpawn(@NotNull final EntitySpawnEvent event) {
if (event instanceof CreatureSpawnEvent && ((CreatureSpawnEvent) event).getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.CUSTOM) &&
!lmEntity.isLevelled()) {

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

Expand All @@ -68,8 +70,8 @@ public void onEntitySpawn(@NotNull final EntitySpawnEvent event) {
return;
}

if (event instanceof CreatureSpawnEvent && ((CreatureSpawnEvent) event).getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.SPAWNER))
lmEntity.setSpawnReason(LevelledMobSpawnReason.SPAWNER);
if (event instanceof CreatureSpawnEvent)
lmEntity.setSpawnReason(adaptVanillaSpawnReason(((CreatureSpawnEvent) event).getSpawnReason()));

if (main.configUtils.playerLevellingEnabled && lmEntity.getPlayerForLevelling() == null)
updateMobForPlayerLevelling(lmEntity);
Expand Down Expand Up @@ -101,6 +103,12 @@ private void updateMobForPlayerLevelling(final @NotNull LivingEntityWrapper lmEn
}

if (closestPlayer == null) return;
// if player has been logged in for less than 5 seconds then ignore
final Instant logonTime = main.companion.getRecentlyJoinedPlayerLogonTime(closestPlayer);
if (logonTime != null) {
if (Utils.getMillisecondsFromInstant(logonTime) < 5000L) return;
main.companion.removeRecentlyJoinedPlayer(closestPlayer);
}

synchronized (lmEntity.getLivingEntity().getPersistentDataContainer()) {
lmEntity.getPDC().set(main.namespaced_keys.playerLevelling_Id, PersistentDataType.STRING, closestPlayer.getUniqueId().toString());
Expand Down Expand Up @@ -245,9 +253,7 @@ public void preprocessMob(final @NotNull LivingEntityWrapper lmEntity, @NotNull
} else if (event instanceof ChunkLoadEvent)
additionalInfo = AdditionalLevelInformation.FROM_CHUNK_LISTENER;

if (!lmEntity.reEvaluateLevel)
lmEntity.setSpawnReason(spawnReason);
else if (main.configUtils.playerLevellingEnabled && lmEntity.isRulesForceAll){
if (lmEntity.reEvaluateLevel && 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 @@ -262,8 +268,12 @@ else if (main.configUtils.playerLevellingEnabled && lmEntity.isRulesForceAll){
final Object syncObj = new Object();
final BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() { updateMobForPlayerLevelling(lmEntity); }
public void run() {
updateMobForPlayerLevelling(lmEntity);
lmEntity.free();
}
};
lmEntity.inUseCount.getAndIncrement();
runnable.runTask(main);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public PlayerJoinListener(final LevelledMobs main) {

@EventHandler(priority = EventPriority.MONITOR)
public void onJoin(@NotNull final PlayerJoinEvent event) {
main.companion.addRecentlyJoinedPlayer(event.getPlayer());
main.nametagTimerChecker.addPlayerToQueue(new PlayerQueueItem(event.getPlayer(), true));
parseCompatibilityChecker(event.getPlayer());
parseUpdateChecker(event.getPlayer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jetbrains.annotations.Nullable;
import simplepets.brainsynder.api.plugin.SimplePets;

import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -337,9 +338,9 @@ public static boolean isMobOfShopkeepers(final @NotNull LivingEntityWrapper lmEn
return isExternalType;
}

@Nullable
@NotNull
public static List<String> getWGRegionsAtLocation(@NotNull final LivingEntityInterface lmInterface){
if (!ExternalCompatibilityManager.hasWorldGuardInstalled()) return null;
if (!ExternalCompatibilityManager.hasWorldGuardInstalled()) return Collections.emptyList();

return WorldGuardIntegration.getWorldGuardRegionsForLocation(lmInterface);
}
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ private int[] getPlayerLevels(final @NotNull LivingEntityWrapper lmEntity){
String tierMatched = null;
final String capDisplay = options.levelCap == null ? "" : "cap: " + options.levelCap + ", ";

if (options.usePlayerMaxLevel) {
if (usePlayerMax) {
results[0] = levelSource;
results[1] = results[0];
} else if (options.matchPlayerLevel) {
} else if (matchPlayerLvl) {
results[1] = levelSource;
} else {
boolean foundMatch = false;
Expand Down Expand Up @@ -266,11 +266,15 @@ else if (variableToUse.equalsIgnoreCase("%world_time_ticks%"))
if (ExternalCompatibilityManager.hasPAPI_Installed()) {
PAPIResult = ExternalCompatibilityManager.getPAPI_Placeholder(player, variableToUse);
if (Utils.isNullOrEmpty(PAPIResult)) {
Utils.logger.warning("Got blank result for '" + variableToUse + "' from PAPI");
final Location l = player.getLocation();
Utils.debugLog(main, DebugType.PLAYER_LEVELLING, String.format("Got blank result for '%s' from PAPI. Player %s at %s,%s,%s in %s",
variableToUse, player.getName(), l.getBlockX(), l.getBlockY(), l.getBlockZ(), player.getWorld().getName()));
usePlayerLevel = true;
}
if (!Utils.isDouble(PAPIResult)) {
Utils.logger.warning("Got invalid number for '" + variableToUse + "' from PAPI");
else if (!Utils.isDouble(PAPIResult)) {
final Location l = player.getLocation();
Utils.debugLog(main, DebugType.PLAYER_LEVELLING, String.format("Got invalid number for '%s', result: '%s' from PAPI. Player %s at %s,%s,%s in %s",
variableToUse, PAPIResult, player.getName(), l.getBlockX(), l.getBlockY(), l.getBlockZ(), player.getWorld().getName()));
usePlayerLevel = true;
}
} else {
Expand Down Expand Up @@ -763,6 +767,13 @@ private void checkEntityForPlayerLevelling(final @NotNull LivingEntityWrapper lm
if (closestPlayer == null)
return;

// if player has been logged in for less than 5 seconds then ignore
final Instant logonTime = main.companion.getRecentlyJoinedPlayerLogonTime(closestPlayer);
if (logonTime != null) {
if (Utils.getMillisecondsFromInstant(logonTime) < 5000L) return;
main.companion.removeRecentlyJoinedPlayer(closestPlayer);
}

if (doesMobNeedRelevelling(mob, closestPlayer)) {

synchronized (mob.getPersistentDataContainer()) {
Expand Down Expand Up @@ -1125,6 +1136,7 @@ public void run() {
main.levelManager.applyLevelledAttributes(lmEntity, Addition.ATTRIBUTE_ATTACK_KNOCKBACK);
main.levelManager.applyLevelledAttributes(lmEntity, Addition.ATTRIBUTE_FLYING_SPEED);
main.levelManager.applyLevelledAttributes(lmEntity, Addition.ATTRIBUTE_KNOCKBACK_RESISTANCE);
main.levelManager.applyLevelledAttributes(lmEntity, Addition.ATTRIBUTE_FOLLOW_RANGE);

if (lmEntity.getLivingEntity() instanceof Zombie)
main.levelManager.applyLevelledAttributes(lmEntity, Addition.ATTRIBUTE_ZOMBIE_SPAWN_REINFORCEMENTS);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/me/lokka30/levelledmobs/misc/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Duration;
import java.time.Instant;
import java.util.*;

/**
Expand Down Expand Up @@ -281,4 +283,8 @@ public static boolean isBiomeInModalList(@NotNull final CachedModalList<Biome> l

return list.isBlacklist() || list.allowedList.contains(biome);
}

public static long getMillisecondsFromInstant(final Instant instant){
return Duration.between(instant, Instant.now()).toMillis();
}
}
14 changes: 7 additions & 7 deletions src/main/java/me/lokka30/levelledmobs/rules/RulesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public int getRule_MobMinLevel(@NotNull final LivingEntityInterface lmInterface)
}

public int getRule_MobMaxLevel(@NotNull final LivingEntityInterface lmInterface){
int maxLevel = 10;
int maxLevel = 0;

for (final RuleInfo ruleInfo : lmInterface.getApplicableRules()) {
if (ruleInfo.restrictions_MaxLevel != null) maxLevel = ruleInfo.restrictions_MaxLevel;
Expand Down Expand Up @@ -644,12 +644,12 @@ private boolean isRuleApplicable_Entity(final LivingEntityWrapper lmEntity, @Not
if (ri.conditions_WGRegions != null){
boolean isInList = false;
final List<String> wgRegions = ExternalCompatibilityManager.getWGRegionsAtLocation(lmInterface);
if (wgRegions != null) {
for (final String regionName : wgRegions) {
if (ri.conditions_WGRegions.isEnabledInList(regionName, null)) {
isInList = true;
break;
}
if (wgRegions.isEmpty()) wgRegions.add("(none)");

for (final String regionName : wgRegions) {
if (ri.conditions_WGRegions.isEnabledInList(regionName, null)) {
isInList = true;
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ private void parseCustomBiomeGroups(final ConfigurationSection cs){
@NotNull
private RuleInfo parseDefaults(final ConfigurationSection cs) {
this.parsingInfo = new RuleInfo("defaults");
parsingInfo.restrictions_MinLevel = 1;
parsingInfo.restrictions_MaxLevel = 10;
parsingInfo.restrictions_MinLevel = 0;
parsingInfo.restrictions_MaxLevel = 0;
parsingInfo.conditions_MobCustomnameStatus = MobCustomNameStatus.EITHER;
parsingInfo.conditions_MobTamedStatus = MobTamedStatus.EITHER;
parsingInfo.babyMobsInheritAdultSetting = true;
Expand Down

0 comments on commit 35a283c

Please sign in to comment.