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

Rewrite proximity engine #295

Merged
merged 13 commits into from
Jun 11, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@ public interface AdvancedConfig {

int obfuscationWorkerThreads();

boolean hasObfuscationTimeout();

int obfuscationTimeout();

int proximityHiderThreads();

int proximityDefaultBucketSize();

int proximityThreadCheckInterval();

boolean hasProximityPlayerCheckInterval();

int proximityPlayerCheckInterval();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import net.imprex.orebfuscator.cache.ObfuscationCache;
import net.imprex.orebfuscator.config.OrebfuscatorConfig;
import net.imprex.orebfuscator.obfuscation.ObfuscationSystem;
import net.imprex.orebfuscator.proximityhider.ProximityHider;
import net.imprex.orebfuscator.proximityhider.ProximityListener;
import net.imprex.orebfuscator.player.OrebfuscatorPlayerMap;
import net.imprex.orebfuscator.proximityhider.ProximityDirectorThread;
import net.imprex.orebfuscator.proximityhider.ProximityPacketListener;
import net.imprex.orebfuscator.util.HeightAccessor;
import net.imprex.orebfuscator.util.OFCLogger;
Expand All @@ -28,10 +28,11 @@ public class Orebfuscator extends JavaPlugin implements Listener {
private final Thread mainThread = Thread.currentThread();

private OrebfuscatorConfig config;
private OrebfuscatorPlayerMap playerMap;
private UpdateSystem updateSystem;
private ObfuscationCache obfuscationCache;
private ObfuscationSystem obfuscationSystem;
private ProximityHider proximityHider;
private ProximityDirectorThread proximityThread;
private ProximityPacketListener proximityPacketListener;

@Override
Expand All @@ -53,6 +54,8 @@ public void onEnable() {
// Load configurations
this.config = new OrebfuscatorConfig(this);

this.playerMap = new OrebfuscatorPlayerMap(this);

// register cleanup listener
HeightAccessor.registerListener(this);

Expand All @@ -69,13 +72,11 @@ public void onEnable() {
this.obfuscationSystem = new ObfuscationSystem(this);

// Load proximity hider
this.proximityHider = new ProximityHider(this);
this.proximityThread = new ProximityDirectorThread(this);
if (this.config.proximityEnabled()) {
this.proximityHider.start();
this.proximityThread.start();

this.proximityPacketListener = new ProximityPacketListener(this);

this.getServer().getPluginManager().registerEvents(new ProximityListener(this), this);
}

// Load packet listener
Expand Down Expand Up @@ -110,9 +111,9 @@ public void onDisable() {
this.obfuscationSystem.shutdown();
}

if (this.config != null && this.config.proximityEnabled() && this.proximityPacketListener != null && this.proximityHider != null) {
if (this.config != null && this.config.proximityEnabled() && this.proximityPacketListener != null && this.proximityThread != null) {
this.proximityPacketListener.unregister();
this.proximityHider.close();
this.proximityThread.close();
}

this.getServer().getScheduler().cancelTasks(this);
Expand All @@ -138,6 +139,10 @@ public OrebfuscatorConfig getOrebfuscatorConfig() {
return this.config;
}

public OrebfuscatorPlayerMap getPlayerMap() {
return playerMap;
}

public UpdateSystem getUpdateSystem() {
return updateSystem;
}
Expand All @@ -150,10 +155,6 @@ public ObfuscationSystem getObfuscationSystem() {
return obfuscationSystem;
}

public ProximityHider getProximityHider() {
return this.proximityHider;
}

public ProximityPacketListener getProximityPacketListener() {
return this.proximityPacketListener;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.imprex.orebfuscator.cache;

import java.util.concurrent.CompletionStage;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.bukkit.Bukkit;
Expand Down Expand Up @@ -51,7 +51,7 @@ private void onRemoval(RemovalNotification<ChunkPosition, ObfuscationResult> not
}
}

public CompletionStage<ObfuscationResult> get(ObfuscationRequest request) {
public CompletableFuture<ObfuscationResult> get(ObfuscationRequest request) {
ChunkPosition key = request.getPosition();

ObfuscationResult cacheChunk = this.cache.getIfPresent(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ public class OrebfuscatorAdvancedConfig implements AdvancedConfig {
private boolean useAsyncPacketListener = true;
private int maxMillisecondsPerTick = 10;
private int protocolLibThreads = -1;

private int obfuscationWorkerThreads = -1;
private int obfuscationTimeout = 10_000;

private int proximityHiderThreads = -1;
private int proximityDefaultBucketSize = 50;
private int proximityThreadCheckInterval = 50;
private int proximityPlayerCheckInterval = 5000;

private boolean protocolLibThreadsSet = false;
private boolean obfuscationWorkerThreadsSet = false;
private boolean hasObfuscationTimeout = false;
private boolean proximityHiderThreadsSet = false;
private boolean hasProximityPlayerCheckInterval = true;

public void deserialize(ConfigurationSection section) {
this.verbose = section.getBoolean("verbose", false);
Expand All @@ -33,8 +41,26 @@ public void deserialize(ConfigurationSection section) {
this.obfuscationWorkerThreads = section.getInt("obfuscationWorkerThreads", -1);
this.obfuscationWorkerThreadsSet = (this.obfuscationWorkerThreads > 0);

this.obfuscationTimeout = section.getInt("obfuscationTimeout", -1);
this.hasObfuscationTimeout = (this.obfuscationTimeout > 0);

this.proximityHiderThreads = section.getInt("proximityHiderThreads", -1);
this.proximityHiderThreadsSet = (this.proximityHiderThreads > 0);

this.proximityDefaultBucketSize = section.getInt("proximityDefaultBucketSize", 50);
if (proximityDefaultBucketSize <= 0) {
throw new RuntimeException(
"proximityDefaultBucketSize has to be bigger then 0, value: " + this.proximityDefaultBucketSize);
}

this.proximityThreadCheckInterval = section.getInt("proximityThreadCheckInterval", 50);
if (this.proximityThreadCheckInterval <= 0) {
throw new RuntimeException(
"proximityThreadCheckInterval has to be bigger then 0, value: " + this.proximityThreadCheckInterval);
}

this.proximityPlayerCheckInterval = section.getInt("proximityPlayerCheckInterval", 5000);
this.hasProximityPlayerCheckInterval = (this.proximityPlayerCheckInterval > 0);
}

public void initialize() {
Expand All @@ -44,7 +70,6 @@ public void initialize() {
this.proximityHiderThreads = (int) (proximityHiderThreadsSet ? proximityHiderThreads : Math.ceil(availableThreads / 2f));

OFCLogger.setVerboseLogging(this.verbose);
OFCLogger.debug("advanced.maxMillisecondsPerTick = " + this.maxMillisecondsPerTick);
OFCLogger.debug("advanced.protocolLibThreads = " + this.protocolLibThreads);
OFCLogger.debug("advanced.obfuscationWorkerThreads = " + this.obfuscationWorkerThreads);
OFCLogger.debug("advanced.proximityHiderThreads = " + this.proximityHiderThreads);
Expand All @@ -55,8 +80,14 @@ public void serialize(ConfigurationSection section) {
section.set("useAsyncPacketListener", this.useAsyncPacketListener);
section.set("maxMillisecondsPerTick", this.maxMillisecondsPerTick);
section.set("protocolLibThreads", this.protocolLibThreadsSet ? this.protocolLibThreads : -1);

section.set("obfuscationWorkerThreads", this.obfuscationWorkerThreadsSet ? this.obfuscationWorkerThreads : -1);
section.set("obfuscationTimeout", this.hasObfuscationTimeout ? this.obfuscationTimeout : -1);

section.set("proximityHiderThreads", this.proximityHiderThreadsSet ? this.proximityHiderThreads : -1);
section.set("proximityDefaultBucketSize", this.proximityDefaultBucketSize);
section.set("proximityThreadCheckInterval", this.proximityThreadCheckInterval);
section.set("proximityPlayerCheckInterval", this.hasProximityPlayerCheckInterval ? this.proximityPlayerCheckInterval : -1);
}

@Override
Expand All @@ -79,8 +110,38 @@ public int obfuscationWorkerThreads() {
return this.obfuscationWorkerThreads;
}

@Override
public boolean hasObfuscationTimeout() {
return this.hasObfuscationTimeout;
}

@Override
public int obfuscationTimeout() {
return this.obfuscationTimeout;
}

@Override
public int proximityHiderThreads() {
return this.proximityHiderThreads;
}

@Override
public int proximityDefaultBucketSize() {
return this.proximityDefaultBucketSize;
}

@Override
public int proximityThreadCheckInterval() {
return this.proximityThreadCheckInterval;
}

@Override
public boolean hasProximityPlayerCheckInterval() {
return this.hasProximityPlayerCheckInterval;
}

@Override
public int proximityPlayerCheckInterval() {
return this.proximityPlayerCheckInterval;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();

if (this.config.general().bypassNotification() && PermissionUtil.canDeobfuscate(player)) {
if (this.config.general().bypassNotification() && PermissionUtil.canBypassObfuscate(player)) {
player.sendMessage(
"[§bOrebfuscator§f]§7 You bypass Orebfuscator because you have the 'orebfuscator.bypass' permission.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package net.imprex.orebfuscator.obfuscation;

import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import com.comphenix.protocol.PacketType;
Expand All @@ -11,23 +12,25 @@

import net.imprex.orebfuscator.Orebfuscator;
import net.imprex.orebfuscator.chunk.ChunkStruct;
import net.imprex.orebfuscator.config.AdvancedConfig;
import net.imprex.orebfuscator.config.OrebfuscatorConfig;
import net.imprex.orebfuscator.proximityhider.ProximityPlayerManager;
import net.imprex.orebfuscator.player.OrebfuscatorPlayer;
import net.imprex.orebfuscator.player.OrebfuscatorPlayerMap;
import net.imprex.orebfuscator.util.BlockPos;
import net.imprex.orebfuscator.util.OFCLogger;
import net.imprex.orebfuscator.util.PermissionUtil;

public abstract class ObfuscationListener extends PacketAdapter {

private final OrebfuscatorConfig config;
private final ProximityPlayerManager proximityManager;
private final OrebfuscatorPlayerMap playerMap;
private final ObfuscationSystem obfuscationSystem;

public ObfuscationListener(Orebfuscator orebfuscator) {
super(orebfuscator, PacketType.Play.Server.MAP_CHUNK);

this.config = orebfuscator.getOrebfuscatorConfig();
this.proximityManager = orebfuscator.getProximityHider().getPlayerManager();
this.playerMap = orebfuscator.getPlayerMap();
this.obfuscationSystem = orebfuscator.getObfuscationSystem();
}

Expand Down Expand Up @@ -55,7 +58,14 @@ public void onPacketSending(PacketEvent event) {

this.preChunkProcessing(event);

this.obfuscationSystem.obfuscate(struct).whenComplete((chunk, throwable) -> {
AdvancedConfig advancedConfig = this.config.advanced();

CompletableFuture<ObfuscationResult> future = this.obfuscationSystem.obfuscate(struct);
if (advancedConfig.hasObfuscationTimeout()) {
future = future.orTimeout(advancedConfig.obfuscationTimeout(), TimeUnit.MILLISECONDS);
}

future.whenComplete((chunk, throwable) -> {
if (throwable != null) {
this.completeExceptionally(event, struct, throwable);
} else if (chunk != null) {
Expand All @@ -69,7 +79,7 @@ public void onPacketSending(PacketEvent event) {
}

private boolean shouldNotObfuscate(Player player) {
return PermissionUtil.canDeobfuscate(player) || !config.world(player.getWorld()).needsObfuscation();
return PermissionUtil.canBypassObfuscate(player) || !config.world(player.getWorld()).needsObfuscation();
}

private void completeExceptionally(PacketEvent event, ChunkStruct struct, Throwable throwable) {
Expand All @@ -86,12 +96,20 @@ private void complete(PacketEvent event, ChunkStruct struct, ObfuscationResult c
struct.removeBlockEntityIf(blockEntities::contains);
}

Player player = event.getPlayer();
this.proximityManager.addAndLockChunk(player, struct.chunkX, struct.chunkZ, chunk.getProximityBlocks());
final OrebfuscatorPlayer player = this.playerMap.get(event.getPlayer());
if (player != null) {
// event.getNetworkMarker().addPostListener(new PacketPostAdapter(this.plugin) {
//
// @Override
// public void onPostEvent(PacketEvent event) {
// System.out.println("post-2: " + struct.chunkX + " " + struct.chunkZ);
// player.addChunk(struct.chunkX, struct.chunkZ, chunk.getProximityBlocks());
// }
//
// });
player.addChunk(struct.chunkX, struct.chunkZ, chunk.getProximityBlocks());
}

Bukkit.getScheduler().runTask(this.plugin, () -> {
this.postChunkProcessing(event);
this.proximityManager.unlockChunk(player, struct.chunkX, struct.chunkZ);
});
this.postChunkProcessing(event);
}
}
Loading