Skip to content

Commit

Permalink
Optimize gang kill condition check
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Dec 21, 2024
1 parent 88f3047 commit 1b2baa2
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.sayandev.sayanvanish.api.SayanVanishAPI;
import org.sayandev.sayanvanish.bukkit.api.BukkitUser;
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI;

import org.bukkit.entity.Player;

import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class EntityDeath implements Listener {

Expand Down Expand Up @@ -65,27 +63,27 @@ void onEntityDeath(EntityDeathEvent e) {
} else {
// Killed by mob
Entity ent = pm.getLastEntityDamager();
String mobName = EntityUtil.getConfigNodeByEntity(ent);
int radius = Gangs.getInstance().getConfig().getInt("Gang.Mobs." + mobName + ".Radius");
int amount = Gangs.getInstance().getConfig().getInt("Gang.Mobs." + mobName + ".Amount");

boolean gangKill = false;

if (Gangs.getInstance().getConfig().getBoolean("Gang.Enabled")) {
String mobName = EntityUtil.getConfigNodeByEntity(ent);
int radius = Gangs.getInstance().getConfig().getInt("Gang.Mobs." + mobName + ".Radius");
int amount = Gangs.getInstance().getConfig().getInt("Gang.Mobs." + mobName + ".Amount");

int totalMobEntities = 0;
// Dreeam TODO: need move to EntityUtil
Predicate<Entity> isNotDragonParts = entity -> !entity.toString().contains("EnderDragonPart"); // Exclude EnderDragonPart
List<Entity> entities = player.getNearbyEntities(radius, radius, radius).stream()
.filter(isNotDragonParts).collect(Collectors.toList());
List<Entity> nearbyEntities = player.getNearbyEntities(radius, radius, radius);

for (Entity entity : entities) {
if (entity.getType().equals(ent.getType())) {
totalMobEntities++;
for (Entity entity : nearbyEntities) {
if (entity.toString().contains("EnderDragonPart")) { // Exclude EnderDragonPart
continue;
}
}

if (totalMobEntities >= amount) {
gangKill = true;
if (entity.getType().equals(ent.getType())) {
if (++totalMobEntities >= amount) {
gangKill = true;
break;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class PlayerUntag implements Listener {

Expand All @@ -36,24 +33,26 @@ public void untagPlayer(PlayerUntagEvent e) {

if (!reason.equals(UntagReason.QUIT)) return;

int radius = Gangs.getInstance().getConfig().getInt("Gang.Mobs.player.Radius");
int amount = Gangs.getInstance().getConfig().getInt("Gang.Mobs.player.Amount");
boolean gangKill = false;

if (Gangs.getInstance().getConfig().getBoolean("Gang.Enabled")) {
int radius = Gangs.getInstance().getConfig().getInt("Gang.Mobs.player.Radius");
int amount = Gangs.getInstance().getConfig().getInt("Gang.Mobs.player.Amount");

int totalMobEntities = 0;
Predicate<Entity> isNotDragonParts = entity -> !entity.toString().contains("EnderDragonPart"); // Exclude EnderDragonPart
List<Entity> entities = player.getNearbyEntities(radius, radius, radius).stream()
.filter(isNotDragonParts).collect(Collectors.toList());
List<Entity> nearbyEntities = player.getNearbyEntities(radius, radius, radius);

for (Entity entity : entities) {
if (entity.getType().equals(EntityType.PLAYER)) {
totalMobEntities++;
for (Entity entity : nearbyEntities) {
if (entity.toString().contains("EnderDragonPart")) { // Exclude EnderDragonPart
continue;
}
}

if (totalMobEntities >= amount) {
gangKill = true;
if (entity instanceof Player) {
if (++totalMobEntities >= amount) {
gangKill = true;
break;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.mrshawn.deathmessages.utils;

import dev.mrshawn.deathmessages.DeathMessages;

public class PlatformUtil {

//public static final boolean IS_FOLIA = DeathMessages.getInstance().foliaLib.isFolia();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.sayandev.sayanvanish.api.SayanVanishAPI;
import org.sayandev.sayanvanish.bukkit.api.BukkitUser;
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI;

import org.bukkit.entity.Player;

import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import dev.mrshawn.deathmessages.config.EntityDeathMessages;
import dev.mrshawn.deathmessages.enums.MobType;
import dev.mrshawn.deathmessages.utils.EntityUtil;
import dev.mrshawn.deathmessages.utils.Util;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.LivingEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import dev.mrshawn.deathmessages.config.EntityDeathMessages;
import dev.mrshawn.deathmessages.enums.MobType;
import dev.mrshawn.deathmessages.utils.EntityUtil;
import dev.mrshawn.deathmessages.utils.Util;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.AreaEffectCloud;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class EntityDeath implements Listener {

Expand Down Expand Up @@ -65,27 +63,27 @@ void onEntityDeath(EntityDeathEvent e) {
} else {
// Killed by mob
Entity ent = pm.getLastEntityDamager();
String mobName = EntityUtil.getConfigNodeByEntity(ent);
int radius = Gangs.getInstance().getConfig().getInt("Gang.Mobs." + mobName + ".Radius");
int amount = Gangs.getInstance().getConfig().getInt("Gang.Mobs." + mobName + ".Amount");

boolean gangKill = false;

if (Gangs.getInstance().getConfig().getBoolean("Gang.Enabled")) {
String mobName = EntityUtil.getConfigNodeByEntity(ent);
int radius = Gangs.getInstance().getConfig().getInt("Gang.Mobs." + mobName + ".Radius");
int amount = Gangs.getInstance().getConfig().getInt("Gang.Mobs." + mobName + ".Amount");

int totalMobEntities = 0;
// Dreeam TODO: need move to EntityUtil
Predicate<Entity> isNotDragonParts = entity -> !entity.toString().contains("EnderDragonPart"); // Exclude EnderDragonPart
List<Entity> entities = player.getNearbyEntities(radius, radius, radius).stream()
.filter(isNotDragonParts).collect(Collectors.toList());
List<Entity> nearbyEntities = player.getNearbyEntities(radius, radius, radius);

for (Entity entity : entities) {
if (entity.getType().equals(ent.getType())) {
totalMobEntities++;
for (Entity entity : nearbyEntities) {
if (entity.toString().contains("EnderDragonPart")) { // Exclude EnderDragonPart
continue;
}
}

if (totalMobEntities >= amount) {
gangKill = true;
if (entity.getType().equals(ent.getType())) {
if (++totalMobEntities >= amount) {
gangKill = true;
break;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class PlayerUntag implements Listener {

Expand All @@ -36,24 +33,26 @@ public void untagPlayer(PlayerUntagEvent e) {

if (!reason.equals(UntagReason.QUIT)) return;

int radius = Gangs.getInstance().getConfig().getInt("Gang.Mobs.player.Radius");
int amount = Gangs.getInstance().getConfig().getInt("Gang.Mobs.player.Amount");
boolean gangKill = false;

if (Gangs.getInstance().getConfig().getBoolean("Gang.Enabled")) {
int radius = Gangs.getInstance().getConfig().getInt("Gang.Mobs.player.Radius");
int amount = Gangs.getInstance().getConfig().getInt("Gang.Mobs.player.Amount");

int totalMobEntities = 0;
Predicate<Entity> isNotDragonParts = entity -> !entity.toString().contains("EnderDragonPart"); // Exclude EnderDragonPart
List<Entity> entities = player.getNearbyEntities(radius, radius, radius).stream()
.filter(isNotDragonParts).collect(Collectors.toList());
List<Entity> nearbyEntities = player.getNearbyEntities(radius, radius, radius);

for (Entity entity : entities) {
if (entity.getType().equals(EntityType.PLAYER)) {
totalMobEntities++;
for (Entity entity : nearbyEntities) {
if (entity.toString().contains("EnderDragonPart")) { // Exclude EnderDragonPart
continue;
}
}

if (totalMobEntities >= amount) {
gangKill = true;
if (entity instanceof Player) {
if (++totalMobEntities >= amount) {
gangKill = true;
break;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.mrshawn.deathmessages.utils;

import dev.mrshawn.deathmessages.DeathMessages;

public class PlatformUtil {

//public static final boolean IS_FOLIA = DeathMessages.getInstance().foliaLib.isFolia();
Expand Down

0 comments on commit 1b2baa2

Please sign in to comment.