Commit af650e2b authored by Amosar's avatar Amosar

add setting to just detect the redstoneClock

Signed-off-by: Amosar's avatarAmosar <maxi.cass11@gmail.com>
parent 3e6fc05e
#En:Don't touch this value
#Fr: Ne pas modifier cette valeur
version: 0.6
version: 0.7
#En:Maximal number Redstone blink authorized during the "Delay" period
#Fr:Nombre maximal de clignotement de redstone autorise pendant la periode "Delay"
MaxPulses: 150
......@@ -35,6 +35,8 @@ checkedClock:
piston: true
redstoneAndRepeater: true
AutomaticallyBreakDetectedClock: true
Msg:
default:
prefix: '&bAntiRedstoneClock &9&l> &r&b '
......
package com.trafalcraft.antiRedstoneClock;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import com.trafalcraft.antiRedstoneClock.commands.*;
import com.trafalcraft.antiRedstoneClock.listener.ComparatorListener;
import com.trafalcraft.antiRedstoneClock.listener.ObserverListener;
import com.trafalcraft.antiRedstoneClock.listener.PistonListener;
import com.trafalcraft.antiRedstoneClock.listener.RedstoneListener;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClock;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClockController;
import com.trafalcraft.antiRedstoneClock.util.CustomConfig;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
......@@ -17,9 +16,9 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.yaml.snakeyaml.error.YAMLException;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClock;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClockController;
import com.trafalcraft.antiRedstoneClock.util.CustomConfig;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
public class Main extends JavaPlugin{
......@@ -29,81 +28,85 @@ public class Main extends JavaPlugin{
private int delay;
private boolean notifyAdmin;
private boolean DropItems;
private boolean automaticallyBreakDetectedClock;
private String line1;
private String line2;
private String line3;
private String line4;
private static final ArrayList<String> ignoredWorlds = new ArrayList<>();
private static final ArrayList<String> ignoredRegions = new ArrayList<>();
public void onEnable(){
long startTime = System.currentTimeMillis();
instance = this;
plugin = this;
plugin.getConfig().options().copyDefaults(true);
plugin.saveDefaultConfig();
plugin.reloadConfig();
if(!getConfig().getString("version").equals("0.6")){
File f = new File(getPlugin().getDataFolder().getPath()+"//config.yml");
File newFile = new File(f.getPath()+"-"+getConfig().getString("version")+".old");
f.renameTo(newFile);
plugin.getConfig().options().copyDefaults(true);
plugin.saveDefaultConfig();
plugin.reloadConfig();
}
if (!getConfig().getString("version").equals("0.6")) {
File f = new File(getPlugin().getDataFolder().getPath() + "//config.yml");
File newFile = new File(f.getPath() + "-" + getConfig().getString("version") + ".old");
f.renameTo(newFile);
plugin.getConfig().options().copyDefaults(true);
plugin.saveDefaultConfig();
plugin.reloadConfig();
}
try{
CustomConfig.load();
}catch(YAMLException e){
CustomConfig.setDefaultsValues();
CustomConfig.setDefaultsValues();
e.printStackTrace();
}
Bukkit.getServer().getPluginManager().registerEvents(new PlayerListener(), this);
try{
Material checked = Material.REDSTONE_COMPARATOR_OFF;
if(plugin.getConfig().getBoolean("checkedClock.comparator")) {
Bukkit.getServer().getPluginManager().registerEvents(new ComparatorListener(), this);
try {
Material checked = Material.REDSTONE_COMPARATOR_OFF;
if (plugin.getConfig().getBoolean("checkedClock.comparator")) {
Bukkit.getServer().getPluginManager().registerEvents(new ComparatorListener(), this);
}
} catch (java.lang.NoSuchFieldError ignored) {
}
}catch (java.lang.NoSuchFieldError ignored){}
try{
Material checked = Material.OBSERVER;
if(plugin.getConfig().getBoolean("checkedClock.observer")) {
Bukkit.getServer().getPluginManager().registerEvents(new ObserverListener(), this);
try {
Material checked = Material.OBSERVER;
if (plugin.getConfig().getBoolean("checkedClock.observer")) {
Bukkit.getServer().getPluginManager().registerEvents(new ObserverListener(), this);
}
} catch (java.lang.NoSuchFieldError ignored) {
}
}catch (java.lang.NoSuchFieldError ignored){}
if(plugin.getConfig().getBoolean("checkedClock.piston")) {
Bukkit.getServer().getPluginManager().registerEvents(new PistonListener(), this);
}
if(plugin.getConfig().getBoolean("checkedClock.redstoneAndRepeater")) {
Bukkit.getServer().getPluginManager().registerEvents(new RedstoneListener(), this);
}
if (plugin.getConfig().getBoolean("checkedClock.piston")) {
Bukkit.getServer().getPluginManager().registerEvents(new PistonListener(), this);
}
if (plugin.getConfig().getBoolean("checkedClock.redstoneAndRepeater")) {
Bukkit.getServer().getPluginManager().registerEvents(new RedstoneListener(), this);
}
if (!plugin.getConfig().contains("AutomaticallyBreakDetectedClock")) {
plugin.getConfig().set("AutomaticallyBreakDetectedClock", true);
automaticallyBreakDetectedClock = true;
plugin.saveConfig();
}
checkTimer(getDelay());
long endTime = System.currentTimeMillis();
long duration = (endTime - startTime);
this.getLogger().info("Plugin loaded in "+duration+"ms"); //2ms
}
private static void checkTimer(int delay){
Bukkit.getScheduler().scheduleSyncRepeatingTask(instance, new Runnable() {
@Override
public void run() {
private static void checkTimer(int delay){
Bukkit.getScheduler().scheduleSyncRepeatingTask(instance, () -> {
for(RedstoneClock rdc : RedstoneClockController.getAll()){
if(rdc.isEnd()){
RedstoneClockController.removeRedstoneByObject(rdc);
}
}
}
}, 100, 20*delay);
}
}, 100, 20 * delay);
}
public void onDisable(){
}
......@@ -115,15 +118,17 @@ public class Main extends JavaPlugin{
return false;
}
if(args[0].equalsIgnoreCase("Reload")){
Reload.getInstance().performCMD(sender);
Reload.getInstance().performCMD(sender);
} else if(args[0].equalsIgnoreCase("checkList")){
CheckList.getInstance().performCMD(sender, args);
CheckList.getInstance().performCMD(sender, args);
}else if(args[0].equalsIgnoreCase("setMaxPulses")){
SetMaxPulses.getInstance().performCMD(sender, args);
SetMaxPulses.getInstance().performCMD(sender, args);
}else if(args[0].equalsIgnoreCase("SetDelay")){
SetDelay.getInstance().performCMD(sender, args);
SetDelay.getInstance().performCMD(sender, args);
}else if(args[0].equalsIgnoreCase("NotifyAdmin")){
NotifyAdmin.getInstance().performCMD(sender, args);
NotifyAdmin.getInstance().performCMD(sender, args);
} else if (args[0].equalsIgnoreCase("AutoRemoveDetectedClock")) {
AutoRemoveDetectedClock.getInstance().performCMD(sender, args);
}else{
CustomConfig.getHelp((Player) sender);
}
......@@ -143,74 +148,80 @@ public class Main extends JavaPlugin{
public static JavaPlugin getPlugin(){
return plugin;
}
public static int getMaximumPulses(){
public static int getMaximumPulses(){
return instance.maximumPulses;
}
public static void setMaximumPulses(int value){
public static void setMaximumPulses(int value){
instance.maximumPulses = value;
}
public static int getDelay(){
return instance.delay;
}
public static void setDelay(int delay){
public static void setDelay(int delay){
instance.delay = delay;
}
public static boolean isNotifyAdmin(){
public static boolean isNotifyAdmin(){
return instance.notifyAdmin;
}
public static void setNotifyAdmin(boolean value ){
public static void setNotifyAdmin(boolean value ){
instance.notifyAdmin = value;
}
public static boolean isDropItems() {
public static boolean isDropItems() {
return instance.DropItems;
}
public static void setDropItems(boolean dropItems) {
instance.DropItems = dropItems;
}
public static boolean automaticallyDropDetectedItem() {
return instance.automaticallyBreakDetectedClock;
}
public static void setAutomaticallyDropDetectedItem(boolean automaticallyBreakDetectedClock) {
instance.automaticallyBreakDetectedClock = automaticallyBreakDetectedClock;
}
public static String getLine1(){
return instance.line1;
}
public static void setLine1(String value){
instance.line1 = value;
}
public static String getLine2(){
public static String getLine2(){
return instance.line2;
}
public static void setLine2(String value){
instance.line2 = value;
}
public static String getLine3(){
public static String getLine3(){
return instance.line3;
}
public static void setLine3(String value){
instance.line3 = value;
}
public static String getLine4(){
public static String getLine4(){
return instance.line4;
}
public static void setLine4(String value){
instance.line4 = value;
}
public static Collection<String> getIgnoredWorlds(){
public static Collection<String> getIgnoredWorlds(){
return ignoredWorlds;
}
public static Collection<String> getIgnoredRegions(){
return ignoredRegions;
}
}
}
\ No newline at end of file
package com.trafalcraft.antiRedstoneClock.commands;
import com.trafalcraft.antiRedstoneClock.Main;
import com.trafalcraft.antiRedstoneClock.util.CustomConfig;
import org.bukkit.command.CommandSender;
public class AutoRemoveDetectedClock {
private static AutoRemoveDetectedClock ourInstance = new AutoRemoveDetectedClock();
public static AutoRemoveDetectedClock getInstance() {
return ourInstance;
}
private AutoRemoveDetectedClock() {
}
public void performCMD(CommandSender sender, String... args) {
if (args.length == 1) {
if (Main.getInstance().getConfig().getBoolean("AutomaticallyBreakDetectedClock")) {
Main.getInstance().getConfig().set("AutomaticallyBreakDetectedClock", false);
Main.setAutomaticallyDropDetectedItem(false);
Main.getInstance().saveConfig();
sender.sendMessage(CustomConfig.Prefix + CustomConfig.newValueInConfig.toString()
.replace("$setting", "\"AutomaticallyBreakDetectedClock\"")
.replace("$value", "false"));
} else {
Main.getInstance().getConfig().set("AutomaticallyBreakDetectedClock", true);
Main.setAutomaticallyDropDetectedItem(true);
Main.getInstance().saveConfig();
sender.sendMessage(CustomConfig.Prefix + CustomConfig.newValueInConfig.toString()
.replace("$setting", "\"AutomaticallyBreakDetectedClock\"")
.replace("$value", "true"));
}
} else {
if (Boolean.parseBoolean(args[1])) {
Main.getInstance().getConfig().set("AutomaticallyBreakDetectedClock", true);
Main.setAutomaticallyDropDetectedItem(true);
Main.getInstance().saveConfig();
sender.sendMessage(CustomConfig.Prefix + CustomConfig.newValueInConfig.toString()
.replace("$setting", "\"AutomaticallyBreakDetectedClock\"")
.replace("$value", args[1]));
} else if (!Boolean.parseBoolean(args[1])) {
Main.getInstance().getConfig().set("AutomaticallyBreakDetectedClock", false);
Main.setAutomaticallyDropDetectedItem(false);
Main.getInstance().saveConfig();
sender.sendMessage(CustomConfig.Prefix + CustomConfig.newValueInConfig.toString()
.replace("$setting", "\"AutomaticallyBreakDetectedClock\"")
.replace("$value", args[1]));
}
}
}
}
package com.trafalcraft.antiRedstoneClock.listener;
import com.trafalcraft.antiRedstoneClock.Main;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClock;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClockController;
import com.trafalcraft.antiRedstoneClock.util.CustomConfig;
import com.trafalcraft.antiRedstoneClock.util.WorldGuardLink;
......@@ -12,63 +13,93 @@ import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockEvent;
public class Util {
static void checkAndUpdateRedstoneClockState(BlockEvent e) {
if(!RedstoneClockController.contains(e.getBlock().getLocation())){
try {
RedstoneClockController.addRedstone(e.getBlock().getLocation());
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
if(!RedstoneClockController.getRedstoneClock(e.getBlock().getLocation()).isEnd()){
if(RedstoneClockController.getRedstoneClock(e.getBlock().getLocation()).getClock() >= Main.getMaximumPulses()){
removeRedstoneClock(e);
class Util {
static void checkAndUpdateRedstoneClockState(BlockEvent e) {
if(!RedstoneClockController.contains(e.getBlock().getLocation())){
try {
RedstoneClockController.addRedstone(e.getBlock().getLocation());
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
RedstoneClockController.getRedstoneClock(e.getBlock().getLocation()).addOneToClock();
if(!RedstoneClockController.getRedstoneClock(e.getBlock().getLocation()).isEnd()){
if(RedstoneClockController.getRedstoneClock(e.getBlock().getLocation()).getClock() >= Main.getMaximumPulses()){
removeRedstoneClock(e);
}else{
RedstoneClockController.getRedstoneClock(e.getBlock().getLocation()).addOneToClock();
}
}
}
}
}
}
static boolean checkIgnoreWorldsAndRegions(BlockEvent e) {
for(String ignoreWorld: Main.getIgnoredWorlds()){
if(e.getBlock().getWorld().getName().equals(ignoreWorld)){
return true;
}
static boolean checkIgnoreWorldsAndRegions(BlockEvent e) {
for(String ignoreWorld: Main.getIgnoredWorlds()){
if(e.getBlock().getWorld().getName().equals(ignoreWorld)){
return true;
}
}
return WorldGuardLink.checkAllowedRegion(e.getBlock().getLocation());
}
return WorldGuardLink.checkAllowedRegion(e.getBlock().getLocation());
}
static void removeRedstoneClock(BlockEvent e) {
Block b = e.getBlock();
if(Main.isDropItems()){
e.getBlock().breakNaturally();
}else{
e.getBlock().setType(Material.AIR);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() {
static void removeRedstoneClock(BlockEvent e) {
Block b = e.getBlock();
if (Main.automaticallyDropDetectedItem()) {
if (Main.isDropItems()) {
e.getBlock().breakNaturally();
} else {
e.getBlock().setType(Material.AIR);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> {
b.setType(Material.SIGN_POST);
BlockState block = b.getState();
Sign sign = (Sign) block;
sign.setLine(0, Main.getLine1());
sign.setLine(1, Main.getLine2());
sign.setLine(2, Main.getLine3());
sign.setLine(3, Main.getLine4());
sign.update();
Bukkit.getLogger()
.info(CustomConfig.Prefix + CustomConfig.MsgToAdmin.toString()
.replace("$X", b.getX() + "")
.replace("$Y", b.getY() + "").replace("$Z", b.getZ() + "")
.replace("$World", b.getWorld().getName()));
if (Main.isNotifyAdmin()) {
for (Player p : Bukkit.getOnlinePlayers()) {
if (p.isOp() || p.hasPermission("antiRedstoneClock.NotifyAdmin")) {
p.sendMessage(
CustomConfig.Prefix + CustomConfig.MsgToAdmin.toString()
.replace("$X", b.getX() + "")
.replace("$Y", b.getY() + "")
.replace("$Z", b.getZ() + "")
.replace("$World", b.getWorld().getName()));
}
}
}
RedstoneClockController.removeRedstoneByLocation(b.getLocation());
}, 1L);
} else {
RedstoneClock redstoneClock = RedstoneClockController.getRedstoneClock(b.getLocation());
if (!redstoneClock.getDetected()) {
redstoneClock.setDetected(true);
Bukkit.getLogger()
.info(CustomConfig.Prefix + CustomConfig.MsgToAdmin.toString()
.replace("$X", b.getX() + "")
.replace("$Y", b.getY() + "").replace("$Z", b.getZ() + "")
.replace("$World", b.getWorld().getName()));
if (Main.isNotifyAdmin()) {
for (Player p : Bukkit.getOnlinePlayers()) {
if (p.isOp() || p.hasPermission("antiRedstoneClock.NotifyAdmin")) {
p.sendMessage(
CustomConfig.Prefix + CustomConfig.MsgToAdmin.toString()
.replace("$X", b.getX() + "")
.replace("$Y", b.getY() + "")
.replace("$Z", b.getZ() + "")
.replace("$World", b.getWorld().getName()));
}
}
}
@Override
public void run() {
b.setType(Material.SIGN_POST);
BlockState block = b.getState();
Sign sign = (Sign)block;
sign.setLine(0, Main.getLine1());
sign.setLine(1, Main.getLine2());
sign.setLine(2, Main.getLine3());
sign.setLine(3, Main.getLine4());
sign.update();
Bukkit.getLogger().info(CustomConfig.Prefix+CustomConfig.MsgToAdmin.toString().replace("$X", b.getX()+"").replace("$Y", b.getY()+"").replace("$Z", b.getZ()+"").replace("$World", b.getWorld().getName()));
if(Main.isNotifyAdmin()){
for(Player p : Bukkit.getOnlinePlayers()){
if(p.isOp() || p.hasPermission("antiRedstoneClock.NotifyAdmin")){
p.sendMessage(CustomConfig.Prefix+CustomConfig.MsgToAdmin.toString().replace("$X", b.getX()+"").replace("$Y", b.getY()+"").replace("$Z", b.getZ()+"").replace("$World", b.getWorld().getName()));
}
}
}
RedstoneClockController.removeRedstoneByLocation(b.getLocation());
}
}, 1L);
}
}
}
}
\ No newline at end of file
package com.trafalcraft.antiRedstoneClock.object;
import org.bukkit.Location;
import com.trafalcraft.antiRedstoneClock.Main;
import org.bukkit.Location;
public class RedstoneClock {
private final long endTime;
private int clock;
private final Location loc;
//only for comparator
private int value;
private boolean detected;
public RedstoneClock(Location loc){
endTime = System.currentTimeMillis() /1000+Main.getDelay();
clock = 0;
this.loc = loc;
//only for comparator
value = 0;
detected = false;
}
public void addOneToClock(){
clock++;
}
public int getClock(){
return clock;
}
public Location getLocation(){
return loc;
}
//only for comparator
public void updateStatus(int value){
this.value = value;
}
//only for comparator
public int getLastStatus(){
return value;
}
public boolean isEnd(){
return (System.currentTimeMillis() /1000)>=endTime;
}
}
public void setDetected(boolean detected) {
this.detected = detected;
}
public boolean getDetected() {
addOneToClock();
return detected;
}
}
\ No newline at end of file
package com.trafalcraft.antiRedstoneClock.util;
import com.trafalcraft.antiRedstoneClock.Main;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import com.trafalcraft.antiRedstoneClock.Main;
import java.util.Arrays;
public enum CustomConfig {
Prefix("&bAntiRedstoneClock &9&l> &r&b "),
ERROR("&4AntiRedstoneClock &l> &r&c "),
NO_PERMISSIONS("&4ERROR &9&l> &r&bYou don't have permission to do that!"),
Command_Use("&4AntiRedstoneClock &l> &r&cCommand usage: &6/arc $command"),
//Msg
MsgToAdmin("Redstone clock disable in x:$X y:$Y Z:$Z. In the world $World"),
reloadSuccess("Reload Success!"),
unknownCmd("Unknown command. Type \"/help\" for help."),
newValueInConfig("The new value of $setting is $value"),
RedStoneClockListHeader("RedstoneClockList: $page"),
RedStoneClockListFooter(""),
//Exception
duplicate_object("This list already contains this redstone");
static final JavaPlugin plugin = Main.getInstance();
public static void getHelp(Player sender){
sender.sendMessage("");
sender.sendMessage("§3§l-------------AntiRedstoneClock-------------");
sender.sendMessage("§3/arc checkList <Page number> §b- display the active redstoneclock.");
sender.sendMessage("§3/arc setMaxPulses <number> §b- Change the \"MaxPulses\" setting.");
sender.sendMessage("§3/arc SetDelay <number> §b- Change the \"Delay\" setting.");
sender.sendMessage("§3/arc NotifyAdmin <number> §b- change the \"NotifyAdmin\" setting.");
sender.sendMessage("§3/arc Reload §b- To Reload the config file.");
sender.sendMessage(" §3Version: §6" + plugin.getDescription().getVersion());
sender.sendMessage("§3------------------------------------------------");
sender.sendMessage("");
}
private String value;
CustomConfig(String value) {
this.value = value;
}
public String toString(){
return value;
}
private void replaceBy(String value){
this.value = value;
}
public static void setDefaultsValues(){
Main.setMaximumPulses(150);
Main.setDelay(300);
Main.setNotifyAdmin(true);
Main.setLine1("The redstoneClock");
Main.setLine2("are");
Main.setLine3("§4PROHIBITED");
Main.setLine4("");
Main.getInstance().getLogger().warning("An error as occurred in the config.yml please fix it!");
Prefix("&bAntiRedstoneClock &9&l> &r&b "),
ERROR("&4AntiRedstoneClock &l> &r&c "),
NO_PERMISSIONS("&4ERROR &9&l> &r&bYou don't have permission to do that!"),
Command_Use("&4AntiRedstoneClock &l> &r&cCommand usage: &6/arc $command"),
//Msg
MsgToAdmin("Redstone clock disable in x:$X y:$Y Z:$Z. In the world $World"),
reloadSuccess("Reload Success!"),
unknownCmd("Unknown command. Type \"/help\" for help."),
newValueInConfig("The new value of $setting is $value"),
RedStoneClockListHeader("RedstoneClockList: $page"),
RedStoneClockListFooter(""),
//Exception
duplicate_object("This list already contains this redstone");
static final JavaPlugin plugin = Main.getInstance();
public static void getHelp(Player sender) {
sender.sendMessage("");
sender.sendMessage("§3§l-------------AntiRedstoneClock-------------");
sender.sendMessage("§3/arc checkList <Page number> §b- display the active redstoneclock.");
sender.sendMessage("§3/arc setMaxPulses <number> §b- Change the \"MaxPulses\" setting.");
sender.sendMessage("§3/arc SetDelay <number> §b- Change the \"Delay\" setting.");
sender.sendMessage("§3/arc NotifyAdmin <true/false> §b- change the \"NotifyAdmin\" setting.");
sender.sendMessage(
"§3/arc AutoRemoveDetectedClock <true/false> §b- change the \"NotifyAdmin\" setting.");
sender.sendMessage("§3/arc Reload §b- To Reload the config file.");
sender.sendMessage(" §3Version: §6" + plugin.getDescription().getVersion());
sender.sendMessage("§3------------------------------------------------");
sender.sendMessage("");
}
private String value;
CustomConfig(String value) {
this.value = value;
}
public String toString() {
return value;
}
private void replaceBy(String value) {