diff --git a/pom.xml b/pom.xml index 9656eee82..df7eceada 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ me.lokka30 LevelledMobs - 3.2.0 b531 + 3.2.1 b536 jar LevelledMobs diff --git a/src/main/java/me/lokka30/levelledmobs/commands/LevelledMobsCommand.java b/src/main/java/me/lokka30/levelledmobs/commands/LevelledMobsCommand.java index 282872213..54d13c277 100644 --- a/src/main/java/me/lokka30/levelledmobs/commands/LevelledMobsCommand.java +++ b/src/main/java/me/lokka30/levelledmobs/commands/LevelledMobsCommand.java @@ -13,7 +13,10 @@ import org.bukkit.command.TabCompleter; import org.jetbrains.annotations.NotNull; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; /** * This class handles the command execution of '/levelledmobs'. @@ -76,9 +79,6 @@ public boolean onCommand(@NotNull final CommandSender sender, final Command comm case "summon": summonSubcommand.parseSubcommand(main, sender, label, args); break; - case "test": - test(sender, args); - break; default: sendMainUsage(sender, label); break; @@ -90,10 +90,6 @@ public boolean onCommand(@NotNull final CommandSender sender, final Command comm return true; } - private void test(@NotNull final CommandSender sender, final String @NotNull [] args) { - - } - private void sendMainUsage(@NotNull final CommandSender sender, final String label) { List mainUsage = main.messagesCfg.getStringList("command.levelledmobs.main-usage"); mainUsage = Utils.replaceAllInList(mainUsage, "%prefix%", main.configUtils.getPrefix()); diff --git a/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropBase.java b/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropBase.java index af09ab1ee..69003140f 100644 --- a/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropBase.java +++ b/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropBase.java @@ -33,8 +33,8 @@ public CustomDropBase(@NotNull final CustomDropsDefaults defaults) { public int maxLevel; public int priority; public int maxDropGroup; - public Integer minPlayerLevel; - public Integer maxPlayerLevel; + public int minPlayerLevel; + public int maxPlayerLevel; public double chance; public boolean playerCausedOnly; public boolean noSpawner; diff --git a/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsDefaults.java b/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsDefaults.java index a27a94818..eb7cd20db 100644 --- a/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsDefaults.java +++ b/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsDefaults.java @@ -29,8 +29,8 @@ public class CustomDropsDefaults { public int maxLevel; public int customModelData; public int maxDropGroup; - public Integer minPlayerLevel; - public Integer maxPlayerLevel; + public int minPlayerLevel; + public int maxPlayerLevel; public double chance; public double equippedSpawnChance; public Double overallChance; @@ -48,6 +48,8 @@ public CustomDropsDefaults() { this.amount = 1; this.minLevel = -1; this.maxLevel = -1; + this.minPlayerLevel = -1; + this.maxPlayerLevel = -1; this.customModelData = -1; this.priority = 0; this.equippedSpawnChance = 0.0; diff --git a/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsHandler.java b/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsHandler.java index 004af2c4d..5445a74d0 100644 --- a/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsHandler.java +++ b/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsHandler.java @@ -585,7 +585,7 @@ private boolean isMobWearingItem(final ItemStack item, final @NotNull LivingEnti } private boolean madePlayerLevelRequirement(final @NotNull CustomDropProcessingInfo info, final CustomDropBase dropBase){ - if (!info.equippedOnly && (dropBase.minPlayerLevel != null || dropBase.maxPlayerLevel != null)){ + if (!info.equippedOnly && (dropBase.minPlayerLevel > -1 || dropBase.maxPlayerLevel > -1)){ // check if the variable result has been cached already and use it if so final String variableToUse = Utils.isNullOrEmpty(dropBase.playerLevelVariable) ? "%level%" : dropBase.playerLevelVariable; @@ -598,17 +598,17 @@ private boolean madePlayerLevelRequirement(final @NotNull CustomDropProcessingIn info.playerLevelVariableCache.put(variableToUse, levelToUse); } - if (dropBase.minPlayerLevel != null && levelToUse < dropBase.minPlayerLevel || - dropBase.maxPlayerLevel != null && levelToUse > dropBase.maxPlayerLevel){ + if (dropBase.minPlayerLevel > -1 && levelToUse < dropBase.minPlayerLevel || + dropBase.maxPlayerLevel > -1 && levelToUse > dropBase.maxPlayerLevel){ if (ymlHelper.getStringSet(main.settingsCfg, "debug-misc").contains("CUSTOM_DROPS")){ if (dropBase instanceof CustomDropItem) { info.addDebugMessage(String.format( - "&8 - &7Mob: &b%s&7, item: %s, lvl-source: %s, minlvl: %s, maxlvl: %s player level criteria not met", + "&8 - &7Mob: &b%s&7, item: %s, lvl-src: %s, minlvl: %s, maxlvl: %s player level criteria not met", info.lmEntity.getTypeName(), ((CustomDropItem) dropBase).getMaterial(), levelToUse, dropBase.minPlayerLevel, dropBase.maxPlayerLevel)); } else { info.addDebugMessage(String.format( - "&8 - &7Mob: &b%s&7, (customCommand), lvl-source: %s, minlvl: %s, maxlvl: %s player level criteria not met", + "&8 - &7Mob: &b%s&7, (customCommand), lvl-src: %s, minlvl: %s, maxlvl: %s player level criteria not met", info.lmEntity.getTypeName(), levelToUse, dropBase.minPlayerLevel, dropBase.maxPlayerLevel)); } } diff --git a/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsParser.java b/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsParser.java index ac7d4b960..eb076604e 100644 --- a/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsParser.java +++ b/src/main/java/me/lokka30/levelledmobs/customdrops/CustomDropsParser.java @@ -331,8 +331,9 @@ private void parseCustomDropsAttributes(@NotNull final CustomDropBase dropBase, dropBase.permissions.addAll(ymlHelper.getStringSet(cs, "permission")); dropBase.minLevel = ymlHelper.getInt(cs,"minlevel", this.defaults.minLevel); dropBase.maxLevel = ymlHelper.getInt(cs,"maxlevel", this.defaults.maxLevel); - dropBase.minPlayerLevel = ymlHelper.getInt2(cs, "min-player-level", this.defaults.minPlayerLevel); - dropBase.maxPlayerLevel = ymlHelper.getInt2(cs, "max-player-level", this.defaults.maxPlayerLevel); + + dropBase.minPlayerLevel = ymlHelper.getInt(cs, "min-player-level", this.defaults.minPlayerLevel); + dropBase.maxPlayerLevel = ymlHelper.getInt(cs, "max-player-level", this.defaults.maxPlayerLevel); dropBase.playerLevelVariable = ymlHelper.getString(cs, "player-level-variable", this.defaults.playerLevelVariable); dropBase.playerCausedOnly = ymlHelper.getBoolean(cs,"player-caused", this.defaults.playerCausedOnly); dropBase.maxDropGroup = ymlHelper.getInt(cs,"maxdropgroup", this.defaults.maxDropGroup); @@ -375,7 +376,7 @@ else if (singleCommand != null) final CustomDropItem item = (CustomDropItem) dropBase; checkEquippedChance(item, cs); - parseItemFlags(item, ymlHelper.getString(cs,"itemflags"), dropInstance); + parseItemFlags(item, cs, dropInstance); item.onlyDropIfEquipped = ymlHelper.getBoolean(cs, "only-drop-if-equipped", this.defaults.onlyDropIfEquipped); item.priority = ymlHelper.getInt(cs,"priority", this.defaults.priority); item.noMultiplier = ymlHelper.getBoolean(cs,"nomultiplier", this.defaults.noMultiplier); @@ -503,21 +504,34 @@ private void applyMetaAttributes(@NotNull final CustomDropItem item){ item.getItemStack().setItemMeta(meta); } - private void parseItemFlags(final CustomDropItem item, final String itemFlags, final CustomDropInstance dropInstance){ - if (Utils.isNullOrEmpty(itemFlags)) return; - List flagList = new LinkedList<>(); + private void parseItemFlags(final CustomDropItem item, final ConfigurationSection cs, final CustomDropInstance dropInstance){ + if (cs == null) return; + + List flagList = cs.getStringList(ymlHelper.getKeyNameFromConfig(cs, "item_flags")); + String itemFlags = null; + + if (flagList.isEmpty()) { + itemFlags = ymlHelper.getString(cs, "itemflags"); + if (Utils.isNullOrEmpty(itemFlags)) + itemFlags = ymlHelper.getString(cs, "item_flags"); + } + + if (flagList.isEmpty() && Utils.isNullOrEmpty(itemFlags)) return; + final List results = new LinkedList<>(); + final List flagsToParse = flagList.isEmpty() ? + List.of(itemFlags.replace(',',';').split(";")) : flagList; - for (final String flag : itemFlags.replace(',',';').split(";")){ + for (final String flag : flagsToParse){ try { ItemFlag newFlag = ItemFlag.valueOf(flag.trim().toUpperCase()); - flagList.add(newFlag); + results.add(newFlag); } catch (Exception e) { Utils.logger.warning(String.format("Invalid itemflag: %s, item: %s, mobOrGroup: %s", flag, item.getMaterial().name(), dropInstance.getMobOrGroupName())); } } - if (flagList.size() > 0) item.itemFlags = flagList; + if (!results.isEmpty()) item.itemFlags = results; } private void checkEquippedChance(final CustomDropItem item, @NotNull final ConfigurationSection cs){ @@ -688,12 +702,12 @@ private void showCustomDropsDebugInfo(final StringBuilder sbMain){ sb.append("&r"); } - if (baseItem.minPlayerLevel != null){ + if (baseItem.minPlayerLevel > -1){ sb.append(", minPL: &b"); sb.append(baseItem.minPlayerLevel); sb.append("&r"); } - if (baseItem.maxPlayerLevel != null){ + if (baseItem.maxPlayerLevel > -1){ sb.append(", maxPL: &b"); sb.append(baseItem.maxPlayerLevel); sb.append("&r"); diff --git a/src/main/java/me/lokka30/levelledmobs/listeners/EntityDamageListener.java b/src/main/java/me/lokka30/levelledmobs/listeners/EntityDamageListener.java index 4e8214d4b..74d677540 100644 --- a/src/main/java/me/lokka30/levelledmobs/listeners/EntityDamageListener.java +++ b/src/main/java/me/lokka30/levelledmobs/listeners/EntityDamageListener.java @@ -111,6 +111,19 @@ private void processRangedDamage(@NotNull final EntityDamageByEntityEvent event) final Projectile projectile = (Projectile) event.getDamager(); if (projectile.getShooter() == null) return; + + if (projectile.getShooter() instanceof Player && event.getEntity() instanceof LivingEntity){ + final LivingEntityWrapper lmEntity = LivingEntityWrapper.getInstance((LivingEntity) event.getEntity(), main); + if (lmEntity.isLevelled() && main.rulesManager.getRule_CreatureNametagVisbility(lmEntity).contains(NametagVisibilityEnum.ATTACKED)) { + if (lmEntity.playersNeedingNametagCooldownUpdate == null) + lmEntity.playersNeedingNametagCooldownUpdate = new HashSet<>(); + lmEntity.playersNeedingNametagCooldownUpdate.add((Player) projectile.getShooter()); + main.levelManager.updateNametag_WithDelay(lmEntity); + } + lmEntity.free(); + return; + } + if (!(projectile.getShooter() instanceof LivingEntity)) return; final LivingEntityWrapper shooter = LivingEntityWrapper.getInstance((LivingEntity) projectile.getShooter(), main); diff --git a/src/main/java/me/lokka30/levelledmobs/listeners/EntityDeathListener.java b/src/main/java/me/lokka30/levelledmobs/listeners/EntityDeathListener.java index a7398ba3f..3df2cfc27 100644 --- a/src/main/java/me/lokka30/levelledmobs/listeners/EntityDeathListener.java +++ b/src/main/java/me/lokka30/levelledmobs/listeners/EntityDeathListener.java @@ -56,7 +56,7 @@ public void onDeath(@NotNull final EntityDeathEvent event) { lmEntity.deathCause = damage.getCause(); if (lmEntity.getLivingEntity().getKiller() != null && main.placeholderApiIntegration != null) - main.placeholderApiIntegration.putEntityDeath(lmEntity.getLivingEntity().getKiller(), lmEntity); + main.placeholderApiIntegration.putPlayerOrMobDeath(lmEntity.getLivingEntity().getKiller(), lmEntity); if (lmEntity.isLevelled()) { diff --git a/src/main/java/me/lokka30/levelledmobs/listeners/EntitySpawnListener.java b/src/main/java/me/lokka30/levelledmobs/listeners/EntitySpawnListener.java index abe08c0c7..09b722aef 100644 --- a/src/main/java/me/lokka30/levelledmobs/listeners/EntitySpawnListener.java +++ b/src/main/java/me/lokka30/levelledmobs/listeners/EntitySpawnListener.java @@ -243,10 +243,20 @@ public void preprocessMob(final @NotNull LivingEntityWrapper lmEntity, @NotNull if (!lmEntity.reEvaluateLevel) lmEntity.setSpawnReason(spawnReason); + else if (main.configUtils.playerLevellingEnabled){ + synchronized (lmEntity.getLivingEntity().getPersistentDataContainer()){ + if (lmEntity.getPDC().has(main.namespaced_keys.playerLevelling_Id, PersistentDataType.STRING)) + lmEntity.getPDC().remove(main.namespaced_keys.playerLevelling_Id); + } + lmEntity.setPlayerForLevelling(null); + } final HashSet additionalLevelInfo = new HashSet<>(Collections.singletonList(additionalInfo)); final LevellableState levellableState = getLevellableState(lmEntity, event); if (levellableState == LevellableState.ALLOWED) { + if (lmEntity.reEvaluateLevel && main.configUtils.playerLevellingEnabled) + updateMobForPlayerLevelling(lmEntity); + main.levelInterface.applyLevelToMob(lmEntity, main.levelInterface.generateLevel(lmEntity), false, false, additionalLevelInfo); } else { diff --git a/src/main/java/me/lokka30/levelledmobs/listeners/EntityTargetListener.java b/src/main/java/me/lokka30/levelledmobs/listeners/EntityTargetListener.java index af956411d..6c4358cc4 100644 --- a/src/main/java/me/lokka30/levelledmobs/listeners/EntityTargetListener.java +++ b/src/main/java/me/lokka30/levelledmobs/listeners/EntityTargetListener.java @@ -8,9 +8,7 @@ import me.lokka30.levelledmobs.misc.LivingEntityWrapper; import me.lokka30.levelledmobs.misc.NametagTimerChecker; import me.lokka30.levelledmobs.misc.QueueItem; -import me.lokka30.levelledmobs.misc.Utils; import me.lokka30.levelledmobs.rules.NametagVisibilityEnum; -import org.bukkit.Utility; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; diff --git a/src/main/java/me/lokka30/levelledmobs/listeners/PlayerDeathListener.java b/src/main/java/me/lokka30/levelledmobs/listeners/PlayerDeathListener.java index a04b7421a..c1063da0e 100644 --- a/src/main/java/me/lokka30/levelledmobs/listeners/PlayerDeathListener.java +++ b/src/main/java/me/lokka30/levelledmobs/listeners/PlayerDeathListener.java @@ -9,6 +9,7 @@ import me.lokka30.levelledmobs.misc.Utils; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -17,6 +18,7 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Listens for when a player dies @@ -40,11 +42,24 @@ public PlayerDeathListener(final LevelledMobs main){ */ @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) public void onPlayerDeath(@NotNull final PlayerDeathEvent event) { - if (event.getDeathMessage() == null) return; + final LivingEntityWrapper lmEntity = getPlayersKiller(event); + + if (lmEntity == null){ + main.placeholderApiIntegration.putPlayerOrMobDeath(event.getEntity(), null); + return; + } + + main.placeholderApiIntegration.putPlayerOrMobDeath(event.getEntity(), lmEntity); + lmEntity.free(); + } + + @Nullable + private LivingEntityWrapper getPlayersKiller(@NotNull final PlayerDeathEvent event){ + if (event.getDeathMessage() == null) return null; final EntityDamageEvent entityDamageEvent = event.getEntity().getLastDamageCause(); if (entityDamageEvent == null || entityDamageEvent.isCancelled() || !(entityDamageEvent instanceof EntityDamageByEntityEvent)) - return; + return null; final Entity damager = ((EntityDamageByEntityEvent) entityDamageEvent).getDamager(); LivingEntity killer; @@ -52,22 +67,21 @@ public void onPlayerDeath(@NotNull final PlayerDeathEvent event) { if (damager instanceof Projectile) killer = (LivingEntity) ((Projectile) damager).getShooter(); else if (!(damager instanceof LivingEntity)) - return; + return null; else killer = (LivingEntity) damager; - if (killer == null || Utils.isNullOrEmpty(killer.getName())) return; - - if (!main.levelManager.isLevelled(killer)) return; + if (killer == null || Utils.isNullOrEmpty(killer.getName()) || killer instanceof Player) return null; final LivingEntityWrapper lmKiller = LivingEntityWrapper.getInstance(killer, main); + if (!lmKiller.isLevelled()) + return lmKiller; + final String deathMessage = main.levelManager.getNametag(lmKiller, true); - if (Utils.isNullOrEmpty(deathMessage) || "disabled".equalsIgnoreCase(deathMessage)) { - lmKiller.free(); - return; - } + if (Utils.isNullOrEmpty(deathMessage) || "disabled".equalsIgnoreCase(deathMessage)) + return lmKiller; event.setDeathMessage(Utils.replaceEx(event.getDeathMessage(), killer.getName(), deathMessage)); - lmKiller.free(); + return lmKiller; } } diff --git a/src/main/java/me/lokka30/levelledmobs/listeners/PlayerJoinListener.java b/src/main/java/me/lokka30/levelledmobs/listeners/PlayerJoinListener.java index a8423eec8..a3883039c 100644 --- a/src/main/java/me/lokka30/levelledmobs/listeners/PlayerJoinListener.java +++ b/src/main/java/me/lokka30/levelledmobs/listeners/PlayerJoinListener.java @@ -67,6 +67,8 @@ private void onPlayerQuitEvent(final PlayerQuitEvent event){ synchronized (NametagTimerChecker.nametagTimer_Lock) { main.nametagTimerChecker.nametagCooldownQueue.remove(event.getPlayer()); } + if (main.placeholderApiIntegration != null) + main.placeholderApiIntegration.removePlayer(event.getPlayer()); } @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) diff --git a/src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java b/src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java index c191e2512..ab9efa703 100644 --- a/src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java +++ b/src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java @@ -194,6 +194,7 @@ private int[] getPlayerLevels(final @NotNull LivingEntityWrapper lmEntity){ if (levelSource < 1) levelSource = 1; final int[] results = new int[]{ 1, 1}; String tierMatched = null; + final String capDisplay = options.levelCap == null ? "" : "cap: " + options.levelCap + ", "; if (options.usePlayerMaxLevel) { results[0] = levelSource; @@ -217,8 +218,8 @@ private int[] getPlayerLevels(final @NotNull LivingEntityWrapper lmEntity){ if (!foundMatch) { Utils.debugLog(main, DebugType.PLAYER_LEVELLING, String.format( - "mob: %s, player: %s, lvl-source: %s, source-after-scale: %s, no tiers matched", - lmEntity.getNameIfBaby(), player.getName(), origLevelSource, levelSource)); + "mob: %s, player: %s, lvl-src: %s, lvl-scale: %s, %sno tiers matched", + lmEntity.getNameIfBaby(), player.getName(), origLevelSource, levelSource, capDisplay)); return null; } } @@ -232,12 +233,12 @@ private int[] getPlayerLevels(final @NotNull LivingEntityWrapper lmEntity){ if (tierMatched == null) { Utils.debugLog(main, DebugType.PLAYER_LEVELLING, String.format( - "mob: %s, player: %s, lvl-source: %s, source-after-scale: %s, result: %s", - lmEntity.getNameIfBaby(), player.getName(), origLevelSource, levelSource, Arrays.toString(results))); + "mob: %s, player: %s, lvl-src: %s, lvl-scale: %s, %sresult: %s", + lmEntity.getNameIfBaby(), player.getName(), origLevelSource, levelSource, capDisplay, Arrays.toString(results))); } else { Utils.debugLog(main, DebugType.PLAYER_LEVELLING, String.format( - "mob: %s, player: %s, lvl-source: %s, source-after-scale: %s, tier: %s, result: %s", - lmEntity.getNameIfBaby(), player.getName(), origLevelSource, levelSource, tierMatched, Arrays.toString(results))); + "mob: %s, player: %s, lvl-src: %s, lvl-scale: %s, tier: %s, %sresult: %s", + lmEntity.getNameIfBaby(), player.getName(), origLevelSource, levelSource, tierMatched, capDisplay, Arrays.toString(results))); } return results; @@ -780,7 +781,7 @@ private void checkLevelledEntity(@NotNull final LivingEntityWrapper lmEntity, @N } private boolean doesMobNeedRelevelling(final @NotNull LivingEntity mob, final @NotNull Player player){ - if (main.playerLevellingEntities.containsKey(mob)){ + if (main.playerLevellingMinRelevelTime > 0 && main.playerLevellingEntities.containsKey(mob)){ final Instant lastCheck = main.playerLevellingEntities.get(mob); final Duration duration = Duration.between(lastCheck, Instant.now()); @@ -788,8 +789,8 @@ private boolean doesMobNeedRelevelling(final @NotNull LivingEntity mob, final @N } String playerId; - main.playerLevellingEntities.put(mob, Instant.now()); - if (main.playerLevellingMinRelevelTime <= 0) return false; + if (main.playerLevellingMinRelevelTime > 0) + main.playerLevellingEntities.put(mob, Instant.now()); synchronized (mob.getPersistentDataContainer()) { if (!mob.getPersistentDataContainer().has(main.namespaced_keys.playerLevelling_Id, PersistentDataType.STRING)) @@ -798,7 +799,8 @@ private boolean doesMobNeedRelevelling(final @NotNull LivingEntity mob, final @N playerId = mob.getPersistentDataContainer().get(main.namespaced_keys.playerLevelling_Id, PersistentDataType.STRING); } - if (playerId == null || !player.getUniqueId().toString().equals(playerId)) + if (playerId == null && main.playerLevellingMinRelevelTime <= 0) return true; + else if (playerId == null || !player.getUniqueId().toString().equals(playerId)) return true; return !player.getUniqueId().toString().equals(playerId); diff --git a/src/main/java/me/lokka30/levelledmobs/managers/PlaceholderApiIntegration.java b/src/main/java/me/lokka30/levelledmobs/managers/PlaceholderApiIntegration.java index c4a6db618..9b94c5e9a 100644 --- a/src/main/java/me/lokka30/levelledmobs/managers/PlaceholderApiIntegration.java +++ b/src/main/java/me/lokka30/levelledmobs/managers/PlaceholderApiIntegration.java @@ -6,11 +6,11 @@ import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.lokka30.levelledmobs.LevelledMobs; +import me.lokka30.levelledmobs.misc.LastMobKilledInfo; import me.lokka30.levelledmobs.misc.LivingEntityWrapper; -import me.lokka30.levelledmobs.misc.Utils; -import me.lokka30.microlib.MessageUtils; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Map; import java.util.TreeMap; @@ -26,18 +26,32 @@ public class PlaceholderApiIntegration extends PlaceholderExpansion { public PlaceholderApiIntegration(final LevelledMobs main) { this.main = main; - this.lastKilledEntitiesByPlayer = new TreeMap<>(); + this.mobsByPlayerTracking = new TreeMap<>(); } private final LevelledMobs main; - private final Map lastKilledEntitiesByPlayer; + private final Map mobsByPlayerTracking; - public void putEntityDeath(final @NotNull Player player, final LivingEntityWrapper lmEntity) { - this.lastKilledEntitiesByPlayer.put(player.getUniqueId(), lmEntity); + public void putPlayerOrMobDeath(final @NotNull Player player, final @Nullable LivingEntityWrapper lmEntity) { + LastMobKilledInfo mobInfo = this.mobsByPlayerTracking.get(player.getUniqueId()); + if (mobInfo == null){ + mobInfo = new LastMobKilledInfo(); + this.mobsByPlayerTracking.put(player.getUniqueId(), mobInfo); + } + + mobInfo.entityLevel = lmEntity != null && lmEntity.isLevelled() ? + lmEntity.getMobLevel() : null; + + mobInfo.entityName = lmEntity != null ? + main.levelManager.getNametag(lmEntity, false) : null; } public void playedLoggedOut(final @NotNull Player player){ - this.lastKilledEntitiesByPlayer.remove(player.getUniqueId()); + this.mobsByPlayerTracking.remove(player.getUniqueId()); + } + + public void removePlayer(final @NotNull Player player){ + this.mobsByPlayerTracking.remove(player.getUniqueId()); } @Override @@ -69,7 +83,7 @@ public boolean canRegister(){ public String onPlaceholderRequest(final Player player, final @NotNull String identifier){ if (player == null) return ""; - if ("level".equalsIgnoreCase(identifier)) + if ("mob-lvl".equalsIgnoreCase(identifier)) return getLevelFromPlayer(player); else if ("displayname".equalsIgnoreCase(identifier)) return getDisplaynameFromPlayer(player); @@ -78,27 +92,20 @@ else if ("displayname".equalsIgnoreCase(identifier)) } @NotNull - private String getLevelFromPlayer(final Player player){ - if (!this.lastKilledEntitiesByPlayer.containsKey(player.getUniqueId())) return ""; + private String getLevelFromPlayer(final @NotNull Player player){ + if (!this.mobsByPlayerTracking.containsKey(player.getUniqueId())) return ""; - final LivingEntityWrapper lmEntity = this.lastKilledEntitiesByPlayer.get(player.getUniqueId()); - if (!lmEntity.isLevelled()) return "0"; - return lmEntity.getMobLevel() + ""; + final LastMobKilledInfo mobInfo = this.mobsByPlayerTracking.get(player.getUniqueId()); + return mobInfo.entityLevel == null ? + "" : mobInfo.entityLevel + ""; } @NotNull - private String getDisplaynameFromPlayer(final Player player){ - if (!this.lastKilledEntitiesByPlayer.containsKey(player.getUniqueId())) return ""; - - final LivingEntityWrapper lmEntity = this.lastKilledEntitiesByPlayer.get(player.getUniqueId()); - - if (lmEntity.getLivingEntity().getCustomName() != null) - return lmEntity.getLivingEntity().getCustomName(); + private String getDisplaynameFromPlayer(final @NotNull Player player){ + if (!this.mobsByPlayerTracking.containsKey(player.getUniqueId())) return ""; - final boolean useCustomNameForNametags = main.helperSettings.getBoolean(main.settingsCfg, "use-customname-for-mob-nametags"); - final String overridenName = main.rulesManager.getRule_EntityOverriddenName(lmEntity, useCustomNameForNametags); - return overridenName == null ? - Utils.capitalize(lmEntity.getTypeName().replaceAll("_", " ")) : - MessageUtils.colorizeAll(overridenName); + final LastMobKilledInfo mobInfo = this.mobsByPlayerTracking.get(player.getUniqueId()); + return mobInfo == null || mobInfo.entityName == null ? + "" : mobInfo.entityName; } } diff --git a/src/main/java/me/lokka30/levelledmobs/misc/LastMobKilledInfo.java b/src/main/java/me/lokka30/levelledmobs/misc/LastMobKilledInfo.java new file mode 100644 index 000000000..69e8b0f4a --- /dev/null +++ b/src/main/java/me/lokka30/levelledmobs/misc/LastMobKilledInfo.java @@ -0,0 +1,6 @@ +package me.lokka30.levelledmobs.misc; + +public class LastMobKilledInfo { + public Integer entityLevel; + public String entityName; +} diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 6e39426c4..ed9635e35 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -214,7 +214,7 @@ command: info: about: - ' ' - - '&b&lLevelledMobs &fv%version%&7 by lokka30 & stumper66' + - '&b&lLevelledMobs &fv%version%&7 by lokka30, stumper66 and Oathkeeper' - '&7&o%description%' - ' ' - '&7This version of the plugin supports: &fMinecraft %supportedVersions%'