Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix & Adding Auto Check / Instant Kill #74

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.15.1-R0.1-SNAPSHOT</version>
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/uk/antiperson/stackmob/StackMob.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.event.Listener;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import uk.antiperson.stackmob.commands.Commands;
import uk.antiperson.stackmob.config.EntityTranslation;
import uk.antiperson.stackmob.config.MainConfig;
import uk.antiperson.stackmob.entity.EntityManager;
import uk.antiperson.stackmob.entity.StackEntity;
import uk.antiperson.stackmob.entity.traits.TraitManager;
import uk.antiperson.stackmob.hook.HookManager;
import uk.antiperson.stackmob.listeners.*;
Expand All @@ -24,6 +30,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;

public class StackMob extends JavaPlugin {
Expand Down Expand Up @@ -94,9 +101,61 @@ public void onEnable() {
if (metrics.isEnabled()) {
getLogger().info("bStats anonymous data collection has been enabled!");
}
if(getMainConfig().isAutoCheckEnabled())
this.checkMob();
itemTools = new ItemTools(this);
}

private void checkMob()
{
StackMob plugin = this;
new BukkitRunnable() {
@Override
public void run() {
AtomicInteger n = new AtomicInteger();
for(World world:Bukkit.getWorlds())
{
plugin.getLogger().info("Checking stack for mob in world " + world.getName());
for (Entity entity : world.getEntities()) {
if (!(entity instanceof Mob)) {
continue;
}
LivingEntity livingEntity = (LivingEntity) entity;
plugin.getServer().getScheduler().runTask(plugin, () -> {
if (plugin.getMainConfig().isEntityBlacklisted(livingEntity)) {
return;
}
if (plugin.getEntityManager().isStackedEntity(livingEntity)) {
return;
}
StackEntity original = plugin.getEntityManager().getStackEntity(livingEntity);
Integer[] searchRadius = plugin.getMainConfig().getStackRadius(entity.getType());
for (Entity nearbyEntity : entity.getNearbyEntities(searchRadius[0], searchRadius[1], searchRadius[2])) {
if (!(nearbyEntity instanceof Mob)) {
continue;
}
StackEntity nearby = plugin.getEntityManager().getStackEntity((LivingEntity) nearbyEntity);
if (plugin.getMainConfig().getStackThresholdEnabled(nearbyEntity.getType()) && nearby.getSize() == 1) {
continue;
}
if (!original.checkNearby(nearby)) {
continue;
}
if (nearby.merge(original)) {
return;
}
}
original.setSize(1);
plugin.getHookManager().onSpawn(original);
n.getAndIncrement();
});
}
}
plugin.getLogger().info(n.toString() + " entities found for stacking");
}
}.runTaskTimer(this, 0, getMainConfig().getAutoCheckInterval());
}

private void loadConfig() {
try {
getMainConfig().load();
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/uk/antiperson/stackmob/config/MainConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import uk.antiperson.stackmob.StackMob;
import uk.antiperson.stackmob.entity.death.DeathType;
import uk.antiperson.stackmob.entity.TagMode;
Expand Down Expand Up @@ -33,6 +34,10 @@ public Integer[] getStackRadius(EntityType type) {
return getList(type, "stack.merge-range").asIntList().toArray(new Integer[2]);
}

public boolean isAutoCheckEnabled(){ return getBoolean("check-task.enable"); }

public int getAutoCheckInterval(){ return getInt("check-task.interval"); }

public int getStackInterval() {
return getInt("stack.interval");
}
Expand Down Expand Up @@ -172,6 +177,12 @@ public boolean isEntityBlacklisted(LivingEntity entity, CreatureSpawnEvent.Spawn
}
return isWorldBlacklisted(entity.getType(), entity.getWorld());
}
public boolean isEntityBlacklisted(LivingEntity entity) {
if (getList(entity.getType(), "types-blacklist").contains(entity.getType().toString())) {
return true;
}
return isWorldBlacklisted(entity.getType(), entity.getWorld());
}

public DeathType getDeathType(LivingEntity dead) {
for (String key : getDeathSection(dead)) {
Expand All @@ -187,6 +198,11 @@ public DeathType getDeathType(LivingEntity dead) {
}
throw new UnsupportedOperationException("Configuration error - unable to determine death type!");
}
public boolean isInstantDamage(EntityDamageEvent.DamageCause dc)
{
ConfigList configDamage = this.getList("instant-death-cause");
return configDamage.contains(dc.name());
}

private Collection<String> getDeathSection(LivingEntity dead) {
TreeMap<Integer, String> array = new TreeMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void onSpawn(StackEntity spawned) {
}
}, 5);
}
spawned.getEntity().getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(maxHealth);
spawned.getEntity().setHealth(maxHealth - leftOverDamage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import uk.antiperson.stackmob.entity.Drops;
import uk.antiperson.stackmob.entity.StackEntity;
import uk.antiperson.stackmob.entity.death.DeathMethod;
import uk.antiperson.stackmob.entity.death.KillAll;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
Expand All @@ -32,7 +33,11 @@ public void onStackDeath(EntityDeathEvent event) {
if (stackEntity.getSize() == 1) {
return;
}
DeathMethod deathMethod = calculateDeath(stackEntity);
DeathMethod deathMethod;
if(sm.getMainConfig().isInstantDamage(stackEntity.getEntity().getLastDamageCause().getCause()))
deathMethod = new KillAll(sm, stackEntity);
else
deathMethod = calculateDeath(stackEntity);
int deathStep = Math.min(stackEntity.getSize(), deathMethod.calculateStep());
if (deathStep > 0 && deathStep < stackEntity.getSize()) {
StackEntity spawned = stackEntity.duplicate();
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ stack:
enabled: true
amount: 5

check-task:
# Enable auto task check all entities on your map for stacking
enable: true
# Interval between each check in tick (20 Tick = 1 Second)
interval: 6000

# Names of worlds where there should be no stacking
worlds-blacklist: []
worlds-blacklist-invert: false
Expand Down Expand Up @@ -149,6 +155,16 @@ death:
reason-blacklist-invert: false
type-blacklist: []
type-blacklist-invert: false
# All event instant kill mob stack (Less than default cause many bug in automatic farm)
instant-death-cause:
- BLOCK_EXPLOSION
- SUICIDE
- FALL
- FALLING_BLOCK
- CONTACT
- LAVA
- MAGIC
- SUFFOCATION

# Multiply entity drops on entity death.
drops:
Expand Down