LagAssist is a plugin that provides tools to prevent, analyse or resolve lag.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.6 KiB

package cx.sfy.LagAssist;
import java.util.SplittableRandom;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockRedstoneEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import cx.sfy.LagAssist.utils.V1_11;
import cx.sfy.LagAssist.utils.VersionMgr;
import cx.sfy.LagAssist.utils.WorldMgr;
public class Redstone implements Listener {
SplittableRandom sr = new SplittableRandom();
public static boolean redstoneculler;
private static boolean destructives;
private static int chance;
private static int ticks;
private static BukkitTask br;
public static void Enabler(boolean reload) {
redstoneculler = false;
destructives = Main.config.getBoolean("redstone-culler.destructive.enabled");
chance = Main.config.getInt("redstone-culler.chance");
ticks = Main.config.getInt("redstone-culler.ticks");
if (!reload) {
Main.p.getServer().getPluginManager().registerEvents(new Redstone(), Main.p);
}
Bukkit.getLogger().info(" §e[§a✔§e] §fRedstone Culler.");
}
public static void CullRedstone() {
if (!redstoneculler) {
redstoneculler = true;
setTimer();
} else if (Bukkit.getScheduler().isCurrentlyRunning(br.getTaskId())) {
br.cancel();
setTimer();
}
}
private static void setTimer() {
br = new BukkitRunnable() {
@Override
public void run() {
redstoneculler = false;
if (VersionMgr.isV1_11()) {
V1_11.ObserverBreaker();
}
}
}.runTaskLater(Main.p, ticks);
}
@EventHandler
public void RedstoneCuller(BlockRedstoneEvent e) {
if (!redstoneculler) {
return;
}
if (WorldMgr.isBlacklisted(e.getBlock().getWorld())) {
return;
}
e.setNewCurrent(0);
if (chance < 0) {
return;
}
Block b = e.getBlock();
if (!Main.config.getStringList("redstone-culler.affected-materials").contains(b.getType().toString().toUpperCase())) {
return;
}
int rand = sr.nextInt(100);
if (rand > chance) {
return;
}
b.setType(Material.AIR);
}
@EventHandler
public void ObserverCuller(BlockPhysicsEvent e) {
if (redstoneculler) {
if (WorldMgr.isBlacklisted(e.getBlock().getWorld())) {
return;
}
if (VersionMgr.isV1_11()) {
if (destructives) {
V1_11.ObserverAdd(e.getBlock());
}
if (V1_11.isObserver(e.getBlock())) {
e.setCancelled(true);
}
}
}
}
}