Skip to content

Commit

Permalink
Fix #405
Browse files Browse the repository at this point in the history
  • Loading branch information
Elikill58 committed Aug 30, 2023
1 parent d2ad018 commit 7cdf603
Show file tree
Hide file tree
Showing 26 changed files with 351 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ public BungeeScheduler(Plugin plugin) {
this.scheduler = plugin.getProxy().getScheduler();
}

@Override
public void run(Runnable task) {
task.run();
}

@Override
public void runRepeating(Consumer<ScheduledTask> task, int delayTicks, int intervalTicks) {
throw new UnsupportedOperationException("Scheduler runRepeating is not support on bungeecord yet.");
}

@Override
public ScheduledTask runRepeating(Runnable task, int delayTicks, int intervalTicks) {
return new BungeeRunnableWrapper(
scheduler.schedule(plugin, task, toMs(delayTicks), toMs(intervalTicks), TimeUnit.MILLISECONDS));
return new BungeeRunnableWrapper(scheduler.schedule(plugin, task, toMs(delayTicks), toMs(intervalTicks), TimeUnit.MILLISECONDS));
}

@Override
Expand Down
16 changes: 1 addition & 15 deletions common/src/com/elikill58/negativity/api/NegativityPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -97,16 +95,13 @@ public class NegativityPlayer {
public double sensitivity = 0.0;
private String clientName, invincibilityReason = "";
private @Nullable ScheduledTask fightCooldownTask;
// one thread per person
private final ExecutorService executor;

public NegativityPlayer(Player p) {
this.p = p;
this.playerId = p.getUniqueId();
this.loginTime = System.currentTimeMillis();
this.clientName = "Not loaded";
this.isBedrockPlayer = BedrockPlayerManager.isBedrockPlayer(p.getUniqueId());
this.executor = Executors.newSingleThreadExecutor((r) -> new Thread(r, "negativity-player-" + p.getName()));

// add processors like this: checkProcessors.add(new
// SpiderExampleCheckProcessor(this));
Expand Down Expand Up @@ -160,15 +155,6 @@ public boolean isBedrockPlayer() {
return isBedrockPlayer;
}

/**
* Get executor for all players content
*
* @return the executor service
*/
public ExecutorService getExecutor() {
return executor;
}

/**
* Check if the player have be detected for the given cheat It also check for
* bypass and TPS drop
Expand Down Expand Up @@ -238,7 +224,7 @@ public String getWhyDetectionNotActive(Cheat c) {
return "Bypass permission";
Adapter ada = Adapter.getAdapter();
if (ada.getConfig().getDouble("tps_alert_stop") > ada.getLastTPS()) // to make TPS go upper
return "Low TPS";
return "Low TPS " + ada.getLastTPS();
Player p = getPlayer();
if (p.getGameMode().equals(GameMode.SPECTATOR))
return "Spectating";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.elikill58.negativity.api.events.packets.PacketReceiveEvent;
import com.elikill58.negativity.api.packets.PacketDirection;
import com.elikill58.negativity.api.packets.packet.NPacket;
import com.elikill58.negativity.universal.Adapter;
import com.elikill58.negativity.universal.Version;

import io.netty.buffer.ByteBuf;
Expand Down Expand Up @@ -49,12 +50,18 @@ public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception
ByteBuf msg = ((ByteBuf) obj).copy();
NPacket packet = NettyHandlerCommon.readPacketFromByteBuf(p, version, direction, msg, "decode");
if(packet != null) {
PacketPreReceiveEvent event = new PacketPreReceiveEvent(packet, p);
EventManager.callEvent(event);
if (event.isCancelled())
return;
super.channelRead(ctx, obj); // call before use
getNegativityPlayer().getExecutor().submit(() -> EventManager.callEvent(new PacketReceiveEvent(packet, p)));
Adapter.getAdapter().getScheduler().runEntity(p, () -> {
PacketPreReceiveEvent event = new PacketPreReceiveEvent(packet, p);
EventManager.callEvent(event);
if (event.isCancelled())
return;
try {
NettyDecoderHandler.super.channelRead(ctx, obj);
} catch (Exception e) {
e.printStackTrace();
} // call before use
EventManager.callEvent(new PacketReceiveEvent(packet, p));
});
} else
super.channelRead(ctx, obj);
msg.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void write(ChannelHandlerContext ctx, Object obj, ChannelPromise promise)
try {
if(obj instanceof ByteBuf) { // firstly start running everything for us
ByteBuf msg = ((ByteBuf) obj).copy();
getNegativityPlayer().getExecutor().submit(() -> {
Adapter.getAdapter().getScheduler().runEntity(p, () -> {
try {
NPacket packet = NettyHandlerCommon.readPacketFromByteBuf(p, version, direction, msg, "encode");
if(packet != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void onMove(PlayerMoveEvent e, NegativityPlayer np, AirJumpData data) {
if (idDownDown.contains("STAIR") || idDown.contains("STAIR") || mDown.isSolid())
return;

Scheduler.getInstance().runDelayed(() -> {
Scheduler.getInstance().runEntityDelayed(p, () -> {
double diffYtoFrom = diffYtoFromBasic - Math.abs(e.getTo().getDirection().getY());
double velY = p.getVelocity().getY();
if (diffYtoFrom - (velY > 0 ? velY : 0) > 0.35 && data.diffY < diffYtoFrom && data.diffY > velY
Expand Down Expand Up @@ -75,7 +75,7 @@ public void onMoveGoingDown(PlayerMoveEvent e, NegativityPlayer np, AirJumpData
return;
Vector oldVel = p.getVelocity();
// TODO soon don't use scheduler
Scheduler.getInstance().runDelayed(() -> {
Scheduler.getInstance().runEntityDelayed(p, () -> {
if (locDownDown.getBlock().getType().getId().contains("STAIR")
|| locDown.getBlock().getType().getId().contains("STAIR"))
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void onBlockBreak(BlockPlaceEvent e) {
int ping = p.getPing(), slot = p.getInventory().getHeldItemSlot();
if (ping > 120)
return;
Scheduler.getInstance().runDelayed(() -> {
Scheduler.getInstance().runEntityDelayed(p, () -> {
Material m = p.getItemInHand().getType(), placed = e.getBlock().getType();
if(BYPASS_TYPES.contains(placed))
return;
Expand Down
41 changes: 41 additions & 0 deletions common/src/com/elikill58/negativity/universal/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@

import org.checkerframework.checker.nullness.qual.Nullable;

import com.elikill58.negativity.api.entity.Entity;

public interface Scheduler {

void run(Runnable task);

default void runEntity(Entity entity, Runnable task) {
run(task);
}

void runRepeating(Consumer<ScheduledTask> task, int delayTicks, int intervalTicks);

/**
Expand All @@ -23,6 +31,14 @@ default ScheduledTask runRepeating(Runnable task, int intervalTicks) {
return runRepeating(task, intervalTicks, null);
}

/**
* Run repeating task each given ticks
*
* @param task the task to run
* @param intervalTicks ticks between each call
* @param name name of task (may be ignored)
* @return the running task
*/
ScheduledTask runRepeating(Runnable task, int intervalTicks, @Nullable String name);

/**
Expand All @@ -34,6 +50,31 @@ default ScheduledTask runRepeating(Runnable task, int intervalTicks) {
*/
ScheduledTask runDelayed(Runnable task, int delayTicks);

/**
* Run repeating task each given ticks according to given entity
*
* @param entity The entity which this task is about
* @param task the task to run
* @param delayTicks delay before starting task
* @param intervalTicks ticks between each task runned
* @return the running task
*/
default ScheduledTask runEntityRepeating(Entity entity, Runnable task, int delayTicks, int intervalTicks) {
return runRepeating(task, delayTicks, intervalTicks);
}

/**
* Run task after waiting given ticks according to given entity
*
* @param entity The entity which this task is about
* @param task task to run
* @param delayTicks ticks before running task
* @return the running task
*/
default ScheduledTask runEntityDelayed(Entity entity, Runnable task, int delayTicks) {
return runDelayed(task, delayTicks);
}

ScheduledTask runRepeatingAsync(Runnable task, Duration delay, Duration interval, @Nullable String name);

static Scheduler getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ public static Object callMethod(Object source, String method) {
return null;
}
}

/**
* Call static method. Return the method return.
*
* @param source the object where we want to run the method
* @param method the name of the method to call
* @return the return of the method called
*/
public static Object callStaticMethod(Class<?> source, String method) {
try {
return source.getDeclaredMethod(method).invoke(null);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* Get the first field which have the searching for class type
Expand Down Expand Up @@ -91,4 +107,13 @@ public static Class<?> getSubClassWithName(Class<?> clazz, String name){
return cl;
return null;
}

public static boolean isClassExist(String clazz) {
try {
Class.forName(clazz);
return true;
} catch (Exception e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public FabricScheduler() {
this.service = Executors.newScheduledThreadPool(1, threadFactory);
}

@Override
public void run(Runnable task) {
task.run(); // not sure about this
}

@Override
public void runRepeating(Consumer<ScheduledTask> task, int delayTicks, int intervalTicks) {
schedule(task, delayTicks, intervalTicks, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class MinestomScheduler implements Scheduler {

private final SchedulerManager sh = MinecraftServer.getSchedulerManager();

@Override
public void run(Runnable task) {
sh.scheduleNextTick(task);
}

@Override
public void runRepeating(Consumer<ScheduledTask> task, int delayTicks, int intervalTicks) {
MinestomNegativity.getInstance().getLogger().error("MinestomScheduler#runRepeating isn't implemented yet.");
Expand Down
1 change: 1 addition & 0 deletions spigot/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version: ${version}
authors: [Elikill58, RedNesto]
description: It's an Advanced AntiCheat Detection, by Elikill58.
api-version: 1.13
folia-supported: true
softdepend: [ProtocolLib, Essentials, WorldGuard, GadgetsMenu, MaxBans, AdvancedBan, LiteBans, ViaVersion, ProtocolSupport, floodgate, UltraPunishments, Geyser-Spigot]

commands:
Expand Down
4 changes: 3 additions & 1 deletion spigot/src/com/elikill58/negativity/spigot/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import com.elikill58.negativity.universal.Adapter;

public class Metrics {

private final Plugin plugin;
Expand Down Expand Up @@ -93,7 +95,7 @@ public Metrics(JavaPlugin plugin, int serviceId) {
boolean logSentData = config.getBoolean("logSentData", false);
boolean logResponseStatusText = config.getBoolean("logResponseStatusText", false);
metricsBase = new MetricsBase("bukkit", serverUUID, serviceId, enabled, this::appendPlatformData, this::appendServiceData,
submitDataTask -> Bukkit.getScheduler().runTask(plugin, submitDataTask), plugin::isEnabled, (message, error) -> this.plugin.getLogger().log(Level.WARNING, message, error),
submitDataTask -> Adapter.getAdapter().getScheduler().run(submitDataTask), plugin::isEnabled, (message, error) -> this.plugin.getLogger().log(Level.WARNING, message, error),
(message) -> this.plugin.getLogger().log(Level.INFO, message), logErrors, logSentData, logResponseStatusText);
}

Expand Down
8 changes: 5 additions & 3 deletions spigot/src/com/elikill58/negativity/spigot/SpigotAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import com.elikill58.negativity.spigot.impl.location.SpigotWorld;
import com.elikill58.negativity.spigot.impl.plugin.SpigotExternalPlugin;
import com.elikill58.negativity.spigot.nms.SpigotVersionAdapter;
import com.elikill58.negativity.spigot.scheduler.FoliaScheduler;
import com.elikill58.negativity.spigot.scheduler.SpigotScheduler;
import com.elikill58.negativity.spigot.utils.Utils;
import com.elikill58.negativity.universal.Adapter;
import com.elikill58.negativity.universal.Platform;
Expand Down Expand Up @@ -65,7 +67,7 @@ public SpigotAdapter(JavaPlugin pl) {
pl.getDataFolder().toPath().resolve("lang"), "Negativity", "CheatHover");
this.logger = new JavaLoggerAdapter(pl.getLogger());
this.itemRegistrar = new SpigotItemRegistrar();
this.scheduler = new SpigotScheduler(pl);
this.scheduler = SubPlatform.getSubPlatform().equals(SubPlatform.FOLIA) ? new FoliaScheduler(pl) : new SpigotScheduler(pl);
}

@Override
Expand Down Expand Up @@ -115,7 +117,7 @@ public NegativityAccountManager getAccountManager() {

@Override
public void runConsoleCommand(String cmd) {
Bukkit.getScheduler().callSyncMethod(pl, () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd));
getScheduler().run(() -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd));
}

@Override
Expand Down Expand Up @@ -251,7 +253,7 @@ public List<ExternalPlugin> getDependentPlugins() {

@Override
public void runSync(Runnable call) {
Bukkit.getScheduler().runTask(pl, call);
getScheduler().run(call);
}

@Override
Expand Down
Loading

0 comments on commit 7cdf603

Please sign in to comment.