Skip to content

Commit

Permalink
1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wode490390 committed Oct 9, 2019
1 parent 23cc960 commit 4b40fff
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 96 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cn.wode490390.nukkit</groupId>
<artifactId>antixray</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<name>AntiXray</name>
<description>Anti X-Ray cheat plugin for Nukkit</description>
<inceptionYear>2018</inceptionYear>
Expand Down Expand Up @@ -66,7 +66,6 @@
</includes>
</artifactSet>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
</configuration>
<executions>
<execution>
Expand Down
109 changes: 62 additions & 47 deletions src/main/java/cn/wode490390/nukkit/antixray/AntiXray.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import cn.nukkit.event.Listener;
import cn.nukkit.event.block.BlockUpdateEvent;
import cn.nukkit.event.level.ChunkUnloadEvent;
import cn.nukkit.event.level.LevelUnloadEvent;
import cn.nukkit.event.player.PlayerChunkRequestEvent;
import cn.nukkit.level.GlobalBlockPalette;
import cn.nukkit.level.Level;
Expand All @@ -35,6 +36,7 @@
import cn.nukkit.network.protocol.UpdateBlockPacket;
import cn.nukkit.plugin.PluginBase;
import cn.nukkit.scheduler.AsyncTask;
import cn.nukkit.utils.Config;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
Expand All @@ -47,12 +49,11 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
Expand All @@ -77,7 +78,7 @@ public class AntiXray extends PluginBase implements Listener {
List<Integer> filters;
private List<String> worlds;

private final Map<Level, WorldHandler> handlers = new ConcurrentHashMap<>();
private final Map<Level, WorldHandler> handlers = new HashMap<>();

@Override
public void onEnable() {
Expand All @@ -87,81 +88,86 @@ public void onEnable() {

}
this.saveDefaultConfig();
Config config = this.getConfig();
String node = "scan-height-limit";
try {
this.height = this.getConfig().getInt(node, 64);
this.height = config.getInt(node, 64);
} catch (Exception e) {
this.height = 64;
this.logLoadException(node);
this.logLoadException(node, e);
}
if (this.height < 1) {
this.height = 1;
} else if (this.height > 255) {
this.height = 255;
}
node = "memory-cache";
try {
this.memoryCache = this.getConfig().getBoolean(node, true);
} catch (Exception e) {
this.memoryCache = true;
this.logLoadException(node);
}
node = "cache-chunks"; //compatible
try {
this.memoryCache = this.getConfig().getBoolean(node, true);
} catch (Exception e) {
this.memoryCache = true;
this.logLoadException(node);
if (config.exists(node)) {
try {
this.memoryCache = config.getBoolean(node, true);
} catch (Exception e) {
this.memoryCache = true;
this.logLoadException(node, e);
}
} else { //compatible
node = "cache-chunks";
try {
this.memoryCache = config.getBoolean(node, true);
} catch (Exception e) {
this.memoryCache = true;
this.logLoadException("memory-cache", e);
}
}
node = "local-cache";
try {
this.localCache = this.getConfig().getBoolean(node, true);
this.localCache = config.getBoolean(node, true);
} catch (Exception e) {
this.localCache = true;
this.logLoadException(node);
this.logLoadException(node, e);
}
node = "obfuscator-mode";
try {
this.mode = this.getConfig().getBoolean(node, true);
this.mode = config.getBoolean(node, true);
} catch (Exception e) {
this.mode = true;
this.logLoadException(node);
this.logLoadException(node, e);
}
node = "overworld-fake-block";
try {
this.fake_o = this.getConfig().getInt(node, 1);
this.fake_o = config.getInt(node, 1);
} catch (Exception e) {
this.fake_o = 1;
this.logLoadException(node);
this.logLoadException(node, e);
}
node = "nether-fake-block";
try {
this.fake_n = this.getConfig().getInt(node, 87);
this.fake_n = config.getInt(node, 87);
} catch (Exception e) {
this.fake_n = 87;
this.logLoadException(node);
this.logLoadException(node, e);
}
node = "protect-worlds";
try {
this.worlds = this.getConfig().getStringList(node);
this.worlds = config.getStringList(node);
} catch (Exception e) {
this.worlds = new ArrayList<>();
this.logLoadException(node);
this.logLoadException(node, e);
}
node = "ores";
try {
this.ores = this.getConfig().getIntegerList(node);
this.ores = config.getIntegerList(node);
} catch (Exception e) {
this.ores = new ArrayList<>();
this.logLoadException(node);
this.logLoadException(node, e);
}
node = "filters";
try {
this.filters = this.getConfig().getIntegerList(node);
this.filters = config.getIntegerList(node);
} catch (Exception e) {
this.filters = new ArrayList<>();
this.logLoadException(node);
this.logLoadException(node, e);
}

if (!this.worlds.isEmpty() && !this.ores.isEmpty()) {
if (this.localCache) {
CACHE_DIR = new File(this.getDataFolder(), "cache");
Expand All @@ -187,23 +193,25 @@ public void onEnable() {

@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerChunkRequest(PlayerChunkRequestEvent event) {
AntiXrayTimings.BlockUpdateEventTimer.startTiming();
Player player = event.getPlayer();
Level level = player.getLevel();
if (!player.hasPermission(PERMISSION_WHITELIST) && this.worlds.contains(level.getName()) && player.getLoaderId() > 0) {
event.setCancelled();
this.handlers.putIfAbsent(level, new WorldHandler(this, level));
this.handlers.get(level).requestChunk(event.getChunkX(), event.getChunkZ(), player);
WorldHandler handler = this.handlers.get(level);
if (handler == null) {
handler = new WorldHandler(this, level);
handler.start();
this.handlers.put(level, handler);
}
handler.requestChunk(event.getChunkX(), event.getChunkZ(), player);
}
AntiXrayTimings.BlockUpdateEventTimer.stopTiming();
}

@EventHandler
public void onBlockUpdate(BlockUpdateEvent event) {
Position position = event.getBlock();
Level level = position.getLevel();
if (this.worlds.contains(level.getName())) {
AntiXrayTimings.BlockUpdateEventTimer.startTiming();
List<UpdateBlockPacket> packets = new ArrayList<>();
for (Vector3 vector : new Vector3[]{
position.add(1),
Expand All @@ -222,7 +230,7 @@ public void onBlockUpdate(BlockUpdateEvent event) {
UpdateBlockPacket packet = new UpdateBlockPacket();
try {
packet.blockRuntimeId = GlobalBlockPalette.getOrCreateRuntimeId(level.getFullBlock(x, y, z));
} catch (NoSuchElementException tryAgain) {
} catch (Exception tryAgain) {
try {
packet.blockRuntimeId = GlobalBlockPalette.getOrCreateRuntimeId(level.getBlockIdAt(x, y, z), 0);
} catch (Exception ex) {
Expand All @@ -242,7 +250,16 @@ public void onBlockUpdate(BlockUpdateEvent event) {
.forEach(player -> players.add(player));
this.getServer().batchPackets(players.toArray(new Player[0]), packets.toArray(new UpdateBlockPacket[0]));
}
AntiXrayTimings.BlockUpdateEventTimer.stopTiming();
}
}

@EventHandler
public void onLevelUnload(LevelUnloadEvent event) {
Level level = event.getLevel();
WorldHandler handler = this.handlers.get(level);
if (handler != null) {
handler.interrupt();
this.handlers.remove(level);
}
}

Expand Down Expand Up @@ -281,8 +298,8 @@ byte[] readCache(long hash) {
return null;
}

private void logLoadException(String node) {
this.getLogger().alert("An error occurred while reading the configuration '" + node + "'. Use the default value.");
private void logLoadException(String node, Exception ex) {
this.getLogger().alert("Failed to get the configuration '" + node + "'. Use the default value.", ex);
}

private static boolean deleteFolder(File file) {
Expand All @@ -305,13 +322,10 @@ private CleanerListener() {

@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
Level level = event.getLevel();
if (worlds.contains(level.getName())) {
AntiXrayTimings.ChunkUnloadEventTimer.startTiming();
WorldHandler handler = AntiXray.this.handlers.get(event.getLevel());
if (handler != null) {
FullChunk chunk = event.getChunk();
handlers.putIfAbsent(level, new WorldHandler(AntiXray.this, level));
handlers.get(level).clearCache(chunk.getX(), chunk.getZ());
AntiXrayTimings.ChunkUnloadEventTimer.stopTiming();
handler.clearCache(chunk.getX(), chunk.getZ());
}
}
}
Expand All @@ -336,6 +350,7 @@ public void onRun() {
deleteFolder(file);
file.createNewFile();
}

try (DeflaterOutputStream outputStream = new DeflaterOutputStream(new BufferedOutputStream(new FileOutputStream(file)), new Deflater(Deflater.BEST_COMPRESSION, true)); InputStream inputStream = new ByteArrayInputStream(this.buffer)) {
byte[] temp = new byte[1024];
int length;
Expand All @@ -345,7 +360,7 @@ public void onRun() {
outputStream.finish();
}
} catch (IOException e) {
getLogger().debug("Unable to save cache file", e);
AntiXray.this.getLogger().debug("Unable to save cache file", e);
}
}
}
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/cn/wode490390/nukkit/antixray/AntiXrayTimings.java

This file was deleted.

Loading

0 comments on commit 4b40fff

Please sign in to comment.