Skip to content

Commit

Permalink
Merge pull request #347 from lokka30/3.3-dev
Browse files Browse the repository at this point in the history
3.3 dev
  • Loading branch information
lokka30 authored Dec 28, 2021
2 parents b00a5fc + f7d6982 commit c07ced6
Show file tree
Hide file tree
Showing 23 changed files with 223 additions and 69 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Non-redistributables
# Why? We respect other developer's work.
/libs

# IntelliJ IDE
# Why? Different developers use different IDE settings.
/target/
Expand All @@ -18,3 +14,6 @@ LevelledMobs.iml
/.idea/
.idea/jarRepositories.xml
.idea/discord.xml

# Mac OS X
._.git
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.3.1 b587</version>
<version>3.3.2 b596</version>
<packaging>jar</packaging>

<name>LevelledMobs</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.lokka30.levelledmobs.customdrops;

public enum CauseOfDeathEnum {
BLOCK_EXPLOSION,
CONTACT,
CRAMMING,
CUSTOM,
DRAGON_BREATH,
DROWNING,
DRYOUT,
ENTITY_ATTACK,
ENTITY_EXPLOSION,
ENTITY_SWEEP_ATTACK,
FALL,
FALLING_BLOCK,
FIRE,
FIRE_TICK,
FLY_INTO_WALL,
FREEZE,
HOT_FLOOR,
LAVA,
LIGHTNING,
MAGIC,
MELTING,
POISON,
PROJECTILE,
STARVATION,
SUFFOCATION,
SUICIDE,
THORNS,
VOID,
WITHER,
PLAYER_CAUSED
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class CustomCommand extends CustomDropBase {
final public List<String> commands;
@NotNull
final Map<String, String> rangedEntries;
public int delay;

public CustomCommand cloneItem() {
CustomCommand copy = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class CustomDropBase implements Cloneable {
String playerLevelVariable;
final public List<String> permissions;
final Set<String> excludedMobs = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
CachedModalList<EntityDamageEvent.DamageCause> causeOfDeathReqs;
CachedModalList<CauseOfDeathEnum> causeOfDeathReqs;

public int getAmount(){
return this.amount;
Expand Down Expand Up @@ -104,7 +104,7 @@ public CustomDropBase cloneItem() {
copy = (CustomDropBase) super.clone();
if (this.causeOfDeathReqs != null)
//noinspection unchecked
copy.causeOfDeathReqs = (CachedModalList<EntityDamageEvent.DamageCause>) this.causeOfDeathReqs.clone();
copy.causeOfDeathReqs = (CachedModalList<CauseOfDeathEnum>) this.causeOfDeathReqs.clone();
} catch (final Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CustomDropProcessingInfo {
boolean equippedOnly;
boolean deathByFire;
boolean wasKilledByPlayer;
EntityDamageEvent.DamageCause deathCause;
CauseOfDeathEnum deathCause;
boolean doNotMultiplyDrops;
boolean hasOverride;
boolean hasCustomDropId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CustomDropsDefaults {
String playerLevelVariable;
public final List<String> permissions;
final List<String> overallPermissions;
CachedModalList<EntityDamageEvent.DamageCause> causeOfDeathReqs;
CachedModalList<CauseOfDeathEnum> causeOfDeathReqs;

CustomDropsDefaults() {
// these are the defaults of the defaults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -96,9 +97,9 @@ public CustomDropResult getCustomItemDrops(final LivingEntityWrapper lmEntity, f
processingInfo.wasKilledByPlayer = false;

if (lmEntity.getLivingEntity().getLastDamageCause() != null)
processingInfo.deathCause = lmEntity.getLivingEntity().getLastDamageCause().getCause();
processingInfo.deathCause = CauseOfDeathEnum.valueOf(lmEntity.getLivingEntity().getLastDamageCause().getCause().toString().toUpperCase());

processingInfo.addition = BigDecimal.valueOf(main.mobDataManager.getAdditionsForLevel(lmEntity, Addition.CUSTOM_ITEM_DROP, 0.0))
processingInfo.addition = BigDecimal.valueOf(main.mobDataManager.getAdditionsForLevel(lmEntity, Addition.CUSTOM_ITEM_DROP, 2))
.setScale(0, RoundingMode.HALF_DOWN).intValueExact(); // truncate double to int

processingInfo.doNotMultiplyDrops = main.rulesManager.getRule_CheckIfNoDropMultiplierEntitiy(lmEntity);
Expand All @@ -111,7 +112,6 @@ public CustomDropResult getCustomItemDrops(final LivingEntityWrapper lmEntity, f
}

if (!equippedOnly && isCustomDropsDebuggingEnabled()) {

final String mobLevel = lmEntity.getMobLevel() > 0 ? "&r (level " + lmEntity.getMobLevel() + ")" : "";
processingInfo.addDebugMessage("&7Custom drops for &b" + lmEntity.getNameIfBaby() + mobLevel);
processingInfo.addDebugMessage("&8- &7Groups: &b" + String.join("&7, &b", lmEntity.getApplicableGroups()) + "&7.");
Expand Down Expand Up @@ -317,17 +317,7 @@ private void getDropsFromCustomDropItem(@NotNull final CustomDropProcessingInfo
if (!info.equippedOnly && dropBase.playerCausedOnly && (dropBase.causeOfDeathReqs == null || dropBase.causeOfDeathReqs.isEmpty()) && !info.wasKilledByPlayer) return;
if (dropBase.noSpawner && info.isSpawner) return;

if (dropBase.causeOfDeathReqs != null && (info.deathCause == null || !Utils.isDamageCauseInModalList(dropBase.causeOfDeathReqs, info.deathCause))){
if (isCustomDropsDebuggingEnabled()) {
final String itemName = dropBase instanceof CustomDropItem ?
((CustomDropItem) dropBase).getMaterial().name() : "(command)";
info.addDebugMessage(String.format(
"&8 - &7item: &b%s&7, death-cause: &b%s&7, death-cause-req: &b%s&7, dropped: &bfalse&7.",
itemName, info.deathCause, dropBase.causeOfDeathReqs)
);
}
return;
}
if (shouldDenyDeathCause(dropBase, info)) return;

if (!madePlayerLevelRequirement(info, dropBase)) return;

Expand Down Expand Up @@ -542,6 +532,28 @@ else if (dropBase instanceof CustomCommand) {
info.newDrops.add(newItem);
}

private boolean shouldDenyDeathCause(final @NotNull CustomDropBase dropBase, final @NotNull CustomDropProcessingInfo info){
if (dropBase.causeOfDeathReqs == null || info.deathCause == null) return false;

if (info.wasKilledByPlayer && Utils.isDamageCauseInModalList(dropBase.causeOfDeathReqs, CauseOfDeathEnum.PLAYER_CAUSED))
return false;

if (!Utils.isDamageCauseInModalList(dropBase.causeOfDeathReqs, info.deathCause)){
if (isCustomDropsDebuggingEnabled()) {
final String itemName = dropBase instanceof CustomDropItem ?
((CustomDropItem) dropBase).getMaterial().name() : "(command)";
info.addDebugMessage(String.format(
"&8 - &7item: &b%s&7, death-cause: &b%s&7, death-cause-req: &b%s&7, dropped: &bfalse&7.",
itemName, info.deathCause, dropBase.causeOfDeathReqs)
);
}

return true;
}

return false;
}

private boolean checkDropPermissions(final @NotNull CustomDropProcessingInfo info, final @NotNull CustomDropBase dropBase){
if (info.equippedOnly || dropBase.permissions.isEmpty()) return true;

Expand Down Expand Up @@ -680,11 +692,27 @@ private void executeCommand(@NotNull final CustomCommand customCommand, @NotNull

Utils.debugLog(main, DebugType.CUSTOM_COMMANDS, debugCommand + command);

for (int i = 0; i < timesToRun; i++)
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
if (customCommand.delay > 0) {
final String commandToRun = command;
final int finalTimesToRun = timesToRun;
final BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
executeTheCommand(commandToRun, finalTimesToRun);
}
};
runnable.runTaskLater(main, customCommand.delay);
}
else
executeTheCommand(command, timesToRun);
}
}

private void executeTheCommand(final String command, final int timesToRun){
for (int i = 0; i < timesToRun; i++)
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
}

@NotNull
private String processRangedCommand(@NotNull String command, final @NotNull CustomCommand cc){
if (cc.rangedEntries.isEmpty()) return command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ else if (singleCommand != null)
customCommand.commands.add(singleCommand);

customCommand.commandName = ymlHelper.getString(cs,"name");
customCommand.delay = ymlHelper.getInt(cs, "delay", 0);
parseRangedVariables(customCommand, cs);

if (customCommand.commands.isEmpty())
Expand Down Expand Up @@ -446,11 +447,11 @@ else if (singleCommand != null)
}

@NotNull
private CachedModalList<EntityDamageEvent.DamageCause> buildCachedModalListOfDamageCause(final ConfigurationSection cs,
final CachedModalList<EntityDamageEvent.DamageCause> defaultValue) {
private CachedModalList<CauseOfDeathEnum> buildCachedModalListOfDamageCause(final ConfigurationSection cs,
final CachedModalList<CauseOfDeathEnum> defaultValue) {
if (cs == null) return defaultValue;

final CachedModalList<EntityDamageEvent.DamageCause> cachedModalList = new CachedModalList<>();
final CachedModalList<CauseOfDeathEnum> cachedModalList = new CachedModalList<>();
final Object simpleStringOrArray = cs.get(ymlHelper.getKeyNameFromConfig(cs, "cause-of-death"));
ConfigurationSection cs2 = null;
List<String> useList = null;
Expand Down Expand Up @@ -480,7 +481,7 @@ else if (simpleStringOrArray instanceof String)
continue;
}
try {
final EntityDamageEvent.DamageCause cause = EntityDamageEvent.DamageCause.valueOf(item.trim().toUpperCase());
final CauseOfDeathEnum cause = CauseOfDeathEnum.valueOf(item.trim().toUpperCase());
cachedModalList.allowedList.add(cause);
} catch (final IllegalArgumentException ignored) {
Utils.logger.warning("Invalid damage cause: " + item);
Expand All @@ -497,7 +498,7 @@ else if (simpleStringOrArray instanceof String)
continue;
}
try {
final EntityDamageEvent.DamageCause cause = EntityDamageEvent.DamageCause.valueOf(item.trim().toUpperCase());
final CauseOfDeathEnum cause = CauseOfDeathEnum.valueOf(item.trim().toUpperCase());
cachedModalList.excludedList.add(cause);
} catch (final IllegalArgumentException ignored) {
Utils.logger.warning("Invalid damage cause: " + item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
Expand Down Expand Up @@ -50,13 +51,14 @@ private LivingEntityWrapper getPlayersKiller(@NotNull final PlayerDeathEvent eve
return null;

final Entity damager = ((EntityDamageByEntityEvent) entityDamageEvent).getDamager();
final LivingEntity killer;
LivingEntity killer = null;

if (damager instanceof Projectile)
killer = (LivingEntity) ((Projectile) damager).getShooter();
else if (!(damager instanceof LivingEntity))
return null;
else
if (damager instanceof Projectile) {
final Projectile projectile = (Projectile) damager;
if (projectile.getShooter() instanceof LivingEntity)
killer = (LivingEntity) projectile.getShooter();
}
else if (damager instanceof LivingEntity)
killer = (LivingEntity) damager;

if (killer == null || Utils.isNullOrEmpty(killer.getName()) || killer instanceof Player) return null;
Expand All @@ -77,13 +79,8 @@ else if (!(damager instanceof LivingEntity))
private void updateDeathMessage(@NotNull final PlayerDeathEvent event, final String mobName){
final TranslatableComponent tc = (TranslatableComponent) event.deathMessage();
if (tc == null) return;
String playerKilled = null;

for (final net.kyori.adventure.text.Component com : tc.args()){
if (com instanceof TextComponent)
playerKilled = ((TextComponent) com).content();
}

final String playerKilled = extractPlayerName(tc);
if (playerKilled == null) return;

final TextComponent tcMobName = LegacyComponentSerializer.legacySection().deserialize(mobName);
Expand All @@ -93,4 +90,33 @@ private void updateDeathMessage(@NotNull final PlayerDeathEvent event, final Str

event.deathMessage(newCom);
}

@Nullable
private String extractPlayerName(final @NotNull TranslatableComponent tc){
String playerKilled = null;

for (final net.kyori.adventure.text.Component com : tc.args()){
if (com instanceof TextComponent) {
final TextComponent tc2 = (TextComponent) com;
playerKilled = tc2.content();

if (playerKilled.isEmpty() && tc2.hoverEvent() != null) {
// in rare cases the above method returns a empty string
// we'll extract the player name from the hover event
final HoverEvent<?> he = tc2.hoverEvent();
if (he == null || !(he.value() instanceof HoverEvent.ShowEntity)) return null;

final HoverEvent.ShowEntity se = (HoverEvent.ShowEntity) he.value();

if (se.name() instanceof TextComponent){
final TextComponent tc3 = (TextComponent) se.name();
playerKilled = tc3.content();
}
}
}
}

return playerKilled != null && playerKilled.isEmpty() ?
null : playerKilled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public enum ExternalCompatibility {
// PlaceholderAPI plugin
PLACEHOLDER_API,

SIMPLE_PETS
SIMPLE_PETS,

ELITE_BOSSES
}

/* Store any external namespaced keys with null values by default */
Expand Down Expand Up @@ -113,6 +115,17 @@ private static boolean isMobOfSimplePets(@NotNull final LivingEntityWrapper lmEn
return SimplePets.isPetEntity(lmEntity.getLivingEntity());
}

private static boolean isMobOfEliteBosses(@NotNull final LivingEntityWrapper lmEntity){
final Plugin plugin = Bukkit.getPluginManager().getPlugin("EliteBosses");
if (plugin == null) return false;

for (final MetadataValue meta : lmEntity.getLivingEntity().getMetadata("EliteBosses")) {
if (meta.asInt() > 0) return true;
}

return false;
}

public static boolean isMythicMob(@NotNull final LivingEntityWrapper lmEntity) {
final Plugin p = Bukkit.getPluginManager().getPlugin("MythicMobs");
if (p == null) return false;
Expand Down Expand Up @@ -203,6 +216,10 @@ static LevellableState checkAllExternalCompats(final LivingEntityWrapper lmEntit
result == LevellableState.ALLOWED)
result = LevellableState.DENIED_CONFIGURATION_COMPATIBILITY_SIMPLEPETS;

if (isMobOfEliteBosses(lmEntity) && !isExternalCompatibilityEnabled(ExternalCompatibility.ELITE_BOSSES, compatRules) &&
result == LevellableState.ALLOWED)
result = LevellableState.DENIED_CONFIGURATION_COMPATIBILITY_ELITE_BOSSES;

return result;
}

Expand Down
Loading

0 comments on commit c07ced6

Please sign in to comment.