Skip to content

Commit

Permalink
Merge pull request #308 from lokka30/3.2-dev
Browse files Browse the repository at this point in the history
3.2 dev
  • Loading branch information
lokka30 authored Oct 15, 2021
2 parents b724b17 + d147a6a commit bc6d356
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 79 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.0 b531</version>
<version>3.2.1 b536</version>
<packaging>jar</packaging>

<name>LevelledMobs</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down Expand Up @@ -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;
Expand All @@ -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<String> mainUsage = main.messagesCfg.getStringList("command.levelledmobs.main-usage");
mainUsage = Utils.replaceAllInList(mainUsage, "%prefix%", main.configUtils.getPrefix());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<ItemFlag> flagList = new LinkedList<>();
private void parseItemFlags(final CustomDropItem item, final ConfigurationSection cs, final CustomDropInstance dropInstance){
if (cs == null) return;

List<String> 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<ItemFlag> results = new LinkedList<>();
final List<String> 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){
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);

main.levelInterface.applyLevelToMob(lmEntity, main.levelInterface.generateLevel(lmEntity),
false, false, additionalLevelInfo);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -40,34 +42,46 @@ 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;

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -780,16 +781,16 @@ 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());

if (duration.toMillis() < main.playerLevellingMinRelevelTime) return false;
}

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))
Expand All @@ -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);
Expand Down
Loading

0 comments on commit bc6d356

Please sign in to comment.