Skip to content

Commit

Permalink
Merge pull request #326 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 14, 2021
2 parents a2247c2 + aa4f709 commit 00e8df7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 48 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.4 b557</version>
<version>3.2.5 b564</version>
<packaging>jar</packaging>

<name>LevelledMobs</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public void onEntitySpawn(@NotNull final EntitySpawnEvent event) {
return;
}

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

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

Expand Down Expand Up @@ -206,6 +209,7 @@ public void run() {
public void preprocessMob(final @NotNull LivingEntityWrapper lmEntity, @NotNull final Event event){
if (!lmEntity.reEvaluateLevel && lmEntity.isLevelled())
return;
if (lmEntity.getLivingEntity() == null) return;

LevelledMobSpawnReason spawnReason = LevelledMobSpawnReason.DEFAULT;
AdditionalLevelInformation additionalInfo = AdditionalLevelInformation.NOT_APPLICABLE;
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1198,9 +1198,34 @@ private void getPlayersNearMob(final @NotNull LivingEntityWrapper lmEntity){
* @return if the mob is levelled or not
*/
public boolean isLevelled(@NotNull final LivingEntity livingEntity) {
synchronized (livingEntity.getPersistentDataContainer()) {
return livingEntity.getPersistentDataContainer().has(main.namespaced_keys.levelKey, PersistentDataType.INTEGER);
boolean hadError = false;
boolean succeeded = false;
boolean isLevelled = false;

for (int i = 0; i < 2; i++) {
try {
synchronized (livingEntity.getPersistentDataContainer()) {
isLevelled = livingEntity.getPersistentDataContainer().has(main.namespaced_keys.levelKey, PersistentDataType.INTEGER);
}
succeeded = true;
break;
}
catch (ConcurrentModificationException ignored) {
hadError = true;
try
{ Thread.sleep(10); }
catch (InterruptedException ignored2) { return false; }
}
}

if (hadError) {
if (succeeded)
Utils.logger.warning("Got ConcurrentModificationException in LevelManager checking entity isLevelled, succeeded on retry");
else
Utils.logger.warning("Got ConcurrentModificationException (2x) in LevelManager checking entity isLevelled");
}

return isLevelled;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,22 @@ private void main() throws InterruptedException{
final QueueItem item = queue.poll(200, TimeUnit.MILLISECONDS);
if (item == null) continue;

if (item.lmEntity.getLivingEntity() != null) {
if (!item.lmEntity.getIsPopulated()) continue;
if (!item.lmEntity.getShouldShowLM_Nametag()) continue;
main.levelManager.entitySpawnListener.preprocessMob(item.lmEntity, item.event);
String lastEntityType = null;
try {
if (item.lmEntity.getLivingEntity() != null) {
if (!item.lmEntity.getIsPopulated()) continue;
if (!item.lmEntity.getShouldShowLM_Nametag()) continue;
lastEntityType = item.lmEntity.getNameIfBaby();
main.levelManager.entitySpawnListener.preprocessMob(item.lmEntity, item.event);
}
}
catch (Exception e){
Utils.logger.error("Got exception while processing " + (lastEntityType != null ? lastEntityType : "(unknown)"));
e.printStackTrace();
}
finally {
item.lmEntity.free();
}

item.lmEntity.free();
}

isRunning = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,60 +88,74 @@ private void main() throws InterruptedException{
continue;
}

final int nametagTimerResetTime = item.lmEntity.getNametagCooldownTime();
String lastEntityType = null;
try {
lastEntityType = item.lmEntity.getNameIfBaby();
processItem(item);
}
catch (Exception e){
Utils.logger.error("Got exception while processing nametag updates on " + (lastEntityType != null ? lastEntityType : "(unknown)"));
e.printStackTrace();
}
finally {
item.lmEntity.free();
}
}

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

if (item.lmEntity.playersNeedingNametagCooldownUpdate != null) {
// record which players should get the cooldown for this mob
// public Map<Player, WeakHashMap<LivingEntity, Instant>> nametagCooldownQueue;
for (final Player player : item.lmEntity.playersNeedingNametagCooldownUpdate) {
if (!nametagCooldownQueue.containsKey(player)) continue;
private void processItem(final @NotNull QueueItem item){
final int nametagTimerResetTime = item.lmEntity.getNametagCooldownTime();

nametagCooldownQueue.get(player).put(item.lmEntity.getLivingEntity(), Instant.now());
main.nametagTimerChecker.cooldownTimes.put(item.lmEntity.getLivingEntity(), item.lmEntity.getNametagCooldownTime());
}
if (nametagTimerResetTime > 0 && !"".equals(item.nametag)) {
synchronized (NametagTimerChecker.nametagTimer_Lock) {
final Map<Player, WeakHashMap<LivingEntity, Instant>> nametagCooldownQueue = main.nametagTimerChecker.getNametagCooldownQueue();

// if any players already have a cooldown on this mob then don't remove the cooldown
for (final Player player : nametagCooldownQueue.keySet()){
if (item.lmEntity.playersNeedingNametagCooldownUpdate.contains(player)) continue;
if (item.lmEntity.playersNeedingNametagCooldownUpdate != null) {
// record which players should get the cooldown for this mob
// public Map<Player, WeakHashMap<LivingEntity, Instant>> nametagCooldownQueue;
for (final Player player : item.lmEntity.playersNeedingNametagCooldownUpdate) {
if (!nametagCooldownQueue.containsKey(player)) continue;

if (nametagCooldownQueue.get(player).containsKey(item.lmEntity.getLivingEntity()))
item.lmEntity.playersNeedingNametagCooldownUpdate.add(player);
}
nametagCooldownQueue.get(player).put(item.lmEntity.getLivingEntity(), Instant.now());
main.nametagTimerChecker.cooldownTimes.put(item.lmEntity.getLivingEntity(), item.lmEntity.getNametagCooldownTime());
}
else{
// if there's any existing cooldowns we'll use them
for (final Player player : nametagCooldownQueue.keySet()){
if (nametagCooldownQueue.get(player).containsKey(item.lmEntity.getLivingEntity())) {
if (item.lmEntity.playersNeedingNametagCooldownUpdate == null)
item.lmEntity.playersNeedingNametagCooldownUpdate = new HashSet<>();

item.lmEntity.playersNeedingNametagCooldownUpdate.add(player);
}

// if any players already have a cooldown on this mob then don't remove the cooldown
for (final Player player : nametagCooldownQueue.keySet()){
if (item.lmEntity.playersNeedingNametagCooldownUpdate.contains(player)) continue;

if (nametagCooldownQueue.get(player).containsKey(item.lmEntity.getLivingEntity()))
item.lmEntity.playersNeedingNametagCooldownUpdate.add(player);
}
}
else{
// if there's any existing cooldowns we'll use them
for (final Player player : nametagCooldownQueue.keySet()){
if (nametagCooldownQueue.get(player).containsKey(item.lmEntity.getLivingEntity())) {
if (item.lmEntity.playersNeedingNametagCooldownUpdate == null)
item.lmEntity.playersNeedingNametagCooldownUpdate = new HashSet<>();

item.lmEntity.playersNeedingNametagCooldownUpdate.add(player);
}
}
}
}
else if (item.lmEntity.playersNeedingNametagCooldownUpdate != null)
item.lmEntity.playersNeedingNametagCooldownUpdate = null;
}
else if (item.lmEntity.playersNeedingNametagCooldownUpdate != null)
item.lmEntity.playersNeedingNametagCooldownUpdate = null;

synchronized (NametagTimerChecker.entityTarget_Lock){
if (main.nametagTimerChecker.entityTargetMap.containsKey(item.lmEntity.getLivingEntity())){
if (item.lmEntity.playersNeedingNametagCooldownUpdate == null)
item.lmEntity.playersNeedingNametagCooldownUpdate = new HashSet<>();
synchronized (NametagTimerChecker.entityTarget_Lock){
if (main.nametagTimerChecker.entityTargetMap.containsKey(item.lmEntity.getLivingEntity())){
if (item.lmEntity.playersNeedingNametagCooldownUpdate == null)
item.lmEntity.playersNeedingNametagCooldownUpdate = new HashSet<>();

item.lmEntity.playersNeedingNametagCooldownUpdate.add(main.nametagTimerChecker.entityTargetMap.get(item.lmEntity.getLivingEntity()));
}
item.lmEntity.playersNeedingNametagCooldownUpdate.add(main.nametagTimerChecker.entityTargetMap.get(item.lmEntity.getLivingEntity()));
}

updateNametag(item.lmEntity, item.nametag, item.players);
item.lmEntity.free();
}

isRunning = false;
updateNametag(item.lmEntity, item.nametag, item.players);
}

private void updateNametag(final @NotNull LivingEntityWrapper lmEntity, final String nametag, final List<Player> players) {
Expand Down

0 comments on commit 00e8df7

Please sign in to comment.