Skip to content

Commit

Permalink
make ignored entities configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 5, 2025
1 parent 74cbccd commit 4b707b7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package me.xginko.aef.modules.lagpreventions.agelimits;

import com.cryptomorin.xseries.XEntityType;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import me.xginko.aef.modules.AEFModule;
import me.xginko.aef.utils.ChunkUtil;
import me.xginko.aef.utils.EntityUtil;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;

import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ProjectileAgeLimit extends AEFModule implements Consumer<ScheduledTask>, Listener {

private final Set<EntityType> ignoredTypes;
private final long check_period_in_ticks;
private final int max_alive_time;

Expand All @@ -33,6 +42,27 @@ public ProjectileAgeLimit() {
"(20 ticks = 1 second) Will not touch Ender Pearls");
this.check_period_in_ticks = config.getInt(configPath + ".check-period-seconds", 20,
"How frequently we should check all projectiles for their alive time") * 20L;
List<String> defaults = Stream.of(
XEntityType.FISHING_BOBBER,
XEntityType.ENDER_PEARL,
XEntityType.WITHER_SKULL,
XEntityType.EYE_OF_ENDER)
.filter(XEntityType::isSupported)
.map(XEntityType::get)
.map(Enum::name)
.collect(Collectors.toList());
this.ignoredTypes = config.getList(configPath + ".ignored-types", defaults)
.stream()
.map(configuredType -> {
try {
return EntityType.valueOf(configuredType);
} catch (IllegalArgumentException e) {
notRecognized(EntityType.class, configuredType);
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toCollection(() -> EnumSet.noneOf(EntityType.class)));
}

@Override
Expand Down Expand Up @@ -60,15 +90,12 @@ public void accept(ScheduledTask task) {
plugin.getServer().getRegionScheduler().execute(plugin, world, chunk.getX(), chunk.getZ(), () -> {
for (Entity entity : chunk.getEntities()) {
entity.getScheduler().execute(plugin, () -> {
if (EntityUtil.isProjectile(entity)) {
switch (entity.getType()) {
case ENDER_PEARL, WITHER_SKULL, FISHING_HOOK, ENDER_SIGNAL -> {
return;
}
}
if (entity.getTicksLived() > max_alive_time) {
entity.remove();
}
if (ignoredTypes.contains(entity.getType())) {
return;
}

if (EntityUtil.isProjectile(entity) && entity.getTicksLived() > max_alive_time) {
entity.remove();
}
}, null, 1L);
}
Expand All @@ -84,15 +111,12 @@ private void onChunkLoad(ChunkLoadEvent event) {

for (Entity entity : event.getChunk().getEntities()) {
entity.getScheduler().execute(plugin, () -> {
if (EntityUtil.isProjectile(entity)) {
switch (entity.getType()) {
case ENDER_PEARL, WITHER_SKULL, FISHING_HOOK, ENDER_SIGNAL -> {
return;
}
}
if (entity.getTicksLived() > max_alive_time) {
entity.remove();
}
if (ignoredTypes.contains(entity.getType())) {
return;
}

if (EntityUtil.isProjectile(entity) && entity.getTicksLived() > max_alive_time) {
entity.remove();
}
}, null, 1L);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package me.xginko.aef.modules.lagpreventions.agelimits;

import com.cryptomorin.xseries.XEntityType;
import me.xginko.aef.modules.AEFModule;
import me.xginko.aef.utils.EntityUtil;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.scheduler.BukkitTask;

import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ProjectileAgeLimit extends AEFModule implements Runnable, Listener {

private final Set<EntityType> ignoredTypes;
private final long checkPeriod;
private final int max_alive_time;

Expand All @@ -29,6 +39,27 @@ public ProjectileAgeLimit() {
"(20 ticks = 1 second) Will not touch Ender Pearls");
this.checkPeriod = config.getInt(configPath + ".check-period-seconds", 20,
"How frequently we should check all projectiles for their alive time") * 20L;
List<String> defaults = Stream.of(
XEntityType.FISHING_BOBBER,
XEntityType.ENDER_PEARL,
XEntityType.WITHER_SKULL,
XEntityType.EYE_OF_ENDER)
.filter(XEntityType::isSupported)
.map(XEntityType::get)
.map(Enum::name)
.collect(Collectors.toList());
this.ignoredTypes = config.getList(configPath + ".ignored-types", defaults)
.stream()
.map(configuredType -> {
try {
return EntityType.valueOf(configuredType);
} catch (IllegalArgumentException e) {
notRecognized(EntityType.class, configuredType);
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toCollection(() -> EnumSet.noneOf(EntityType.class)));
}

@Override
Expand All @@ -50,18 +81,12 @@ public void disable() {
public void run() {
for (World world : plugin.getServer().getWorlds()) {
for (Entity entity : world.getEntities()) {
if (EntityUtil.isProjectile(entity)) {
switch (entity.getType()) {
case ENDER_PEARL:
case WITHER_SKULL:
case FISHING_HOOK:
case ENDER_SIGNAL:
continue;
}
if (ignoredTypes.contains(entity.getType())) {
continue;
}

if (entity.getTicksLived() > max_alive_time) {
entity.remove();
}
if (EntityUtil.isProjectile(entity) && entity.getTicksLived() > max_alive_time) {
entity.remove();
}
}
}
Expand All @@ -72,18 +97,12 @@ private void onChunkLoad(ChunkLoadEvent event) {
if (event.isNewChunk()) return;

for (Entity entity : event.getChunk().getEntities()) {
if (EntityUtil.isProjectile(entity)) {
switch (entity.getType()) {
case ENDER_PEARL:
case WITHER_SKULL:
case FISHING_HOOK:
case ENDER_SIGNAL:
continue;
}
if (ignoredTypes.contains(entity.getType())) {
continue;
}

if (entity.getTicksLived() > max_alive_time) {
entity.remove();
}
if (EntityUtil.isProjectile(entity) && entity.getTicksLived() > max_alive_time) {
entity.remove();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "me.xginko"
version = "2.7.4"
version = "2.7.5-SNAPSHOT"
description = "Prevent many exploits that affect anarchy servers."
var url: String? = "github.com/xGinko/AnarchyExploitFixes"

Expand Down

0 comments on commit 4b707b7

Please sign in to comment.