Commit 45eed4b5 authored by Amosar's avatar Amosar

Add AntiRedstoneClock plugin

parent dfb1567f
version: 0.2
#Nombre maximal de clignotement de redstone autorise pendant la periode "Delay"
MaxImpulsion: 150
#Temp (en secondes) avant de reset la premiere variable
Delay: 300
#Faut t'il prevenir un admin?
NotifyAdmins: true
#ecrire le texte a mettre dans les panneauxs
Sign:
Line1: 'Les boucles de'
Line2: 'redstone'
Line3: 'sont'
Line4: '&4Interdite!'
IgnoreWorlds: redstoneWorld/survie
Msg:
default:
prefix: '&bAntiRedstoneClock &9&l> &r&b '
error: '&4AntiRedstoneClock &l> &r&c '
no_permission: '&4Erreur &9&l> &r&bYou dont have permission to do that!'
command_use: '&4SnowBallWar &l> &r&cCommand usage: &6/arc $commande'
message:
MsgToAdmin: 'Redstone clock disable in x:$X y:$Y Z:$Z. In the world $World'
unknownCmd: 'Unknown command. Type "/help" for help.'
reloadSuccess: 'Reload Success!'
Exception:
duplicate_object: 'This list already contains this redstone'
name: antiRedstoneClock
version: 0.1
description: Limit the redstoneClock
author: Amosar
main: com.trafalcraft.antiRedstoneClock.Main
commands:
antiRedstoneClock:
description: Base command for the antiRedstoneClock plugin.
aliases: [arc]
\ No newline at end of file
package com.trafalcraft.antiRedstoneClock;
import java.util.ArrayList;
import java.util.Collection;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
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;
public class Main extends JavaPlugin{
private RedstoneClockController rdc;
private static Main instance;
private static JavaPlugin plugin;
private int maxImpulsions;
private int delay;
private boolean notifyAdmin;
private String line1;
private String line2;
private String line3;
private String line4;
private static ArrayList<String> ignoreWorld = new ArrayList<String>();
public void onEnable(){
long startTime = System.nanoTime();
instance = this;
plugin = this;
this.rdc = new RedstoneClockController();
Bukkit.getServer().getPluginManager().registerEvents(new PlayerListener(), this);
checkTimer();
plugin.getConfig().options().copyDefaults(true);
plugin.saveDefaultConfig();
plugin.reloadConfig();
//if(instance.getConfig().getInt("version") != 0.1){
//plus tard
//}
line1 = "";
line2 = "";
line3 = "";
line4 = "";
//PlayerListener pl = new PlayerListener();
//pl.onRedstoneClock(null);
try{
CustomConfig.load();
String sIgnoreWorld = getPlugin().getConfig().getString("IgnoreWorlds");
for(int i = 0;i<sIgnoreWorld.split("/").length;i++){
ignoreWorld.add(sIgnoreWorld.split("/")[i]);
}
}catch(YAMLException e){
setMaxImpulsions(150);
setDelay(300);
setNotifyAdmin(true);
setLine1("The redstoneClock");
setLine2("are");
setLine3("§4PROHIBITED");
setLine4("");
instance.getLogger().warning("An error as occured in the config.yml please fix it!");
e.printStackTrace();
}
long endTime = System.nanoTime();
long duration = (endTime - startTime);
//redstoneOverload();
this.getLogger().info("Plugin chargé en "+duration/1000000+"ms"); //2ms
}
private void checkTimer(){
Bukkit.getScheduler().scheduleSyncRepeatingTask(instance, new Runnable() {
@Override
public void run() {
for(RedstoneClock brdc : getRDC().getAll()){
if(brdc.getMinutes() > brdc.getEndTimerInMinutes()){
getRDC().removeRedstoneByObject(brdc);
}
}
}
}, 0, 20*Main.getDelay());
}
public void onDisable(){
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[]args){
//Player p = (Player) sender;
if(cmd.getName().equalsIgnoreCase("antiredstoneclock")){
if(sender.isOp() || sender.hasPermission("antiRedstoneClock.Admin")){
if(args[0].equalsIgnoreCase("reload")){
try{
getPlugin().reloadConfig();
CustomConfig.load();
sender.sendMessage(CustomConfig.Prefix+CustomConfig.reloadSuccess.toString());
}catch(YAMLException e){
if(sender instanceof Player){
sender.sendMessage(CustomConfig.ERREUR+"An error as occured in the config.yml please check the log!");
}
instance.getLogger().warning("An error as occured in the config.yml please fix it!");
e.printStackTrace();
}
}/*else if(args[0].equalsIgnoreCase("help")){
if(sender instanceof Player){
CustomConfig.getHelp((Player) sender);
}
}*/
}else{
sender.sendMessage(CustomConfig.unknownCmd.toString());
}
}else{
sender.sendMessage(CustomConfig.unknownCmd.toString());
}
return false;
}
public static Main getInstance(){
return instance;
} //if(instance.getConfig().getInt("version") != 0.1){
//plus tard
//}
public static JavaPlugin getPlugin(){
return plugin;
}
public static RedstoneClockController getRDC(){
return instance.rdc;
}
public static int getMaxImpulsions(){
return instance.maxImpulsions;
}
public static void setMaxImpulsions(int value){
instance.maxImpulsions = value;
}
public static int getDelay(){
return instance.delay;
}
public static void setDelay(int delay){
instance.delay = delay;
}
public static boolean getNotifyAdmin(){
return instance.notifyAdmin;
}
public static void setNotifyAdmin(boolean value ){
instance.notifyAdmin = value;
}
public static String getLine1(){
return instance.line1;
}
public static void setLine1(String value){
instance.line1 = value;
}
public static String getLine2(){
return instance.line2;
}
public static void setLine2(String value){
instance.line2 = value;
}
public static String getLine3(){
return instance.line3;
}
public static void setLine3(String value){
instance.line3 = value;
}
public static String getLine4(){
return instance.line4;
}
public static void setLine4(String value){
instance.line4 = value;
}
public static Collection<String> getAllowedWorld(){
return ignoreWorld;
}
}
package com.trafalcraft.antiRedstoneClock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockRedstoneEvent;
import com.sk89q.worldguard.bukkit.WGBukkit;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.trafalcraft.antiRedstoneClock.util.CustomConfig;
public class PlayerListener implements Listener {
@EventHandler (priority = EventPriority.LOWEST)
public void onRedstoneClock(BlockRedstoneEvent e){
for(String ignoreWorld:Main.getAllowedWorld()){
if(e.getBlock().getWorld().getName().equals(ignoreWorld)){
return;
}
}
if(e.getBlock().getType() == Material.REDSTONE_WIRE){
if(e.getOldCurrent() == 0){
if(!Main.getRDC().contains(e.getBlock().getLocation())){
try {
Main.getRDC().addRedstone(e.getBlock().getLocation());
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
if(Main.getRDC().getRedstoneClock(e.getBlock().getLocation()).getMinutes() <= Main.getRDC().getRedstoneClock(e.getBlock().getLocation()).getEndTimerInMinutes()){
if(Main.getRDC().getRedstoneClock(e.getBlock().getLocation()).getBoucle() >= Main.getMaxImpulsions()){
e.getBlock().setType(Material.SIGN_POST);
BlockState block = e.getBlock().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();
Main.getInstance().getLogger().info(CustomConfig.Prefix+CustomConfig.MsgToAdmin.toString().replace("$X", e.getBlock().getX()+"").replace("$Y", e.getBlock().getY()+"").replace("$Z", e.getBlock().getZ()+"").replace("$World", e.getBlock().getWorld().getName()));
//Main.getInstance().getLogger().info("Boucle de redstone désactivé au coordoonée x:"+e.getBlock().getX()+" y:"+e.getBlock().getY()+" z:"+e.getBlock().getZ()+". Dans le monde:"+e.getBlock().getWorld().getName());
if(Main.getNotifyAdmin()){
for(Player p : Bukkit.getOnlinePlayers()){
if(p.isOp() || p.hasPermission("antiRedstoneClock.notifyAdmin")){
p.sendMessage(CustomConfig.Prefix+CustomConfig.MsgToAdmin.toString().replace("$X", e.getBlock().getX()+"").replace("$Y", e.getBlock().getY()+"").replace("$Z", e.getBlock().getZ()+"").replace("$World", e.getBlock().getWorld().getName()));
}
}
}
Main.getRDC().removeRedstoneByLocation(e.getBlock().getLocation());
}else{
Main.getRDC().getRedstoneClock(e.getBlock().getLocation()).addBoucle();
}
}else{
if(Main.getRDC().getRedstoneClock(e.getBlock().getLocation()).getBoucle() >= Main.getMaxImpulsions()){
e.getBlock().setType(Material.SIGN_POST);
BlockState block = e.getBlock().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", e.getBlock().getX()+"").replace("$Y", e.getBlock().getY()+"").replace("$Z", e.getBlock().getZ()+"").replace("$World", e.getBlock().getWorld().getName()));
if(Main.getNotifyAdmin()){
for(Player p : Bukkit.getOnlinePlayers()){
if(p.isOp() || p.hasPermission("antiRedstoneClock.notifyAdmin")){
p.sendMessage(CustomConfig.Prefix+CustomConfig.MsgToAdmin.toString().replace("$X", e.getBlock().getX()+"").replace("$Y", e.getBlock().getY()+"").replace("$Z", e.getBlock().getZ()+"").replace("$World", e.getBlock().getWorld().getName()));
}
}
}
}
Main.getRDC().removeRedstoneByLocation(e.getBlock().getLocation());
}
}
}
}else if(e.getBlock().getType() == Material.DIODE_BLOCK_ON){
if(!Main.getRDC().contains(e.getBlock().getLocation())){
try {
Main.getRDC().addRedstone(e.getBlock().getLocation());
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
if(Main.getRDC().getRedstoneClock(e.getBlock().getLocation()).getMinutes() <= Main.getRDC().getRedstoneClock(e.getBlock().getLocation()).getEndTimerInMinutes()){
if(Main.getRDC().getRedstoneClock(e.getBlock().getLocation()).getBoucle() >= Main.getMaxImpulsions()){
final Location b1 = e.getBlock().getLocation();
b1.setY(e.getBlock().getY()-1);
b1.getBlock().setType(Material.AIR);
final Block b2 = e.getBlock();
b2.getDrops().clear();
b2.setType(Material.AIR);
b2.getDrops().clear();
Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() {
@Override
public void run() {
b1.getBlock().setType(Material.DIRT);
b2.setType(Material.SIGN_POST);
BlockState block = b2.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", b2.getX()+"").replace("$Y", b2.getY()+"").replace("$Z", b2.getZ()+"").replace("$World", b2.getWorld().getName()));
if(Main.getNotifyAdmin()){
for(Player p : Bukkit.getOnlinePlayers()){
if(p.isOp() || p.hasPermission("antiRedstoneClock.notifyAdmin")){
p.sendMessage(CustomConfig.Prefix+CustomConfig.MsgToAdmin.toString().replace("$X", b2.getX()+"").replace("$Y", b2.getY()+"").replace("$Z", b2.getZ()+"").replace("$World", b2.getWorld().getName()));
}
}
}
Main.getRDC().removeRedstoneByLocation(b2.getLocation());
}
}, 5L);
}else{
Main.getRDC().getRedstoneClock(e.getBlock().getLocation()).addBoucle();
}
}else{
if(Main.getRDC().getRedstoneClock(e.getBlock().getLocation()).getBoucle() >= Main.getMaxImpulsions()){
e.getBlock().setType(Material.SIGN_POST);
BlockState block = e.getBlock().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", e.getBlock().getX()+"").replace("$Y", e.getBlock().getY()+"").replace("$Z", e.getBlock().getZ()+"").replace("$World", e.getBlock().getWorld().getName()));
if(Main.getNotifyAdmin()){
for(Player p : Bukkit.getOnlinePlayers()){
if(p.isOp() || p.hasPermission("antiRedstoneClock.notifyAdmin")){
p.sendMessage(CustomConfig.Prefix+CustomConfig.MsgToAdmin.toString().replace("$X", e.getBlock().getX()+"").replace("$Y", e.getBlock().getY()+"").replace("$Z", e.getBlock().getZ()+"").replace("$World", e.getBlock().getWorld().getName()));
}
}
}
}
Main.getRDC().removeRedstoneByLocation(e.getBlock().getLocation());
}
}
}
//Main.getInstance().getLogger().info("Plugin chargé en "+duration/1000000+"ms"); //2ms
}
@EventHandler (priority = EventPriority.HIGHEST)
public void onPlayerBreakRedstone(BlockBreakEvent e){
if(e.getBlock().getType() == Material.REDSTONE_WIRE || e.getBlock().getType() == Material.DIODE_BLOCK_ON){
if(Main.getRDC().contains(e.getBlock().getLocation())){
Main.getRDC().removeRedstoneByLocation(e.getBlock().getLocation());
}
}
}
}
package com.trafalcraft.antiRedstoneClock.object;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.trafalcraft.antiRedstoneClock.Main;
public class RedstoneClock {
//Date initTime = SystemClockFactory.getDatetime();
private static SimpleDateFormat formater = new SimpleDateFormat("mm");
private String initTime;
private int boucle;
int minutes;
public RedstoneClock(){
initTime = formater.format(new Date());
minutes = Integer.parseInt(initTime);
boucle = 0;
}
public void addBoucle(){
boucle++;
}
public int getBoucle(){
return boucle;
}
public int getMinutes(){
return minutes;
}
public int getEndTimerInMinutes(){
String endTime = formater.format(new Date());
int endMinutes = Integer.parseInt(endTime)+(Main.getDelay()/60);
return endMinutes;
}
}
package com.trafalcraft.antiRedstoneClock.object;
import java.util.Collection;
import java.util.Map;
import org.bukkit.Location;
import com.google.common.collect.Maps;
import com.trafalcraft.antiRedstoneClock.util.CustomConfig;
public class RedstoneClockController {
private final Map<Location, RedstoneClock> activeMap = Maps.newHashMap();
public void addRedstone(Location location) throws Exception{
if(contains(location)){
throw new Exception(CustomConfig.ERREUR+CustomConfig.duplicate_object.toString());
}else{
activeMap.put(location, new RedstoneClock());
return;
}
}
public boolean contains(Location location){
if(this.activeMap.containsKey(location)){
return true;
}
return false;
}
public void removeRedstoneByLocation(Location location){
if(this.activeMap.containsKey(location)){
activeMap.remove(location);
}
}
public void removeRedstoneByObject(RedstoneClock rc){
if(this.activeMap.containsKey(rc)){
activeMap.remove(rc);
}
}
public RedstoneClock getRedstoneClock(Location location){
return activeMap.get(location);
}
public int size(Location location){
return activeMap.size();
}
public Collection<RedstoneClock> getAll() {
return activeMap.values();
}
}
package com.trafalcraft.antiRedstoneClock.util;
import com.trafalcraft.antiRedstoneClock.Main;
public enum CustomConfig {
Prefix("&bAntiRedstoneClock &9&l> &r&b "),
ERREUR("&4AntiRedstoneClock &l> &r&c "),
NO_PERMISSIONS("&4Erreur &9&l> &r&bYou dont have permission to do that!"),
Command_Use("&4SnowBallWar &l> &r&cCommand usage: &6/arc $commande"),
//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."),
//Exception
duplicate_object("This list already contains this redstone");
/*static JavaPlugin plugin = Main.getInstance();
public static void getHelp(Player sender){
sender.sendMessage("");
sender.sendMessage("§3§l-------------------AntiRedstoneClock-------------------");
sender.sendMessage("§3/arc setup <nom de l'arene> §b- crée l'arène.");
sender.sendMessage("§3/arc spawn<numero> §b- Configurer le lieu de spawn des joueurs.");
sender.sendMessage(" §3Version: §6" + plugin.getDescription().getVersion());
sender.sendMessage("§3------------------------------------------------");
sender.sendMessage("");
}*/
private String value;
private CustomConfig(String value) {
this.value = value;
}
public String toString(){
return value;
}
public void replaceby(String value){
this.value = value;
}
public static void load(){
Prefix.replaceby(Main.getPlugin().getConfig().getString("Msg.default.prefix").replace("&", "§"));
ERREUR.replaceby(Main.getPlugin().getConfig().getString("Msg.default.error").replace("&", "§"));
NO_PERMISSIONS.replaceby(Main.getPlugin().getConfig().getString("Msg.default.no_permission").replace("&", "§"));
Command_Use.replaceby(Main.getPlugin().getConfig().getString("Msg.default.command_use").replace("&", "§"));
MsgToAdmin.replaceby(Main.getPlugin().getConfig().getString("Msg.message.MsgToAdmin").replace("&", "§"));
reloadSuccess.replaceby(Main.getPlugin().getConfig().getString("Msg.message.reloadSuccess").replace("&", "§"));
unknownCmd.replaceby(Main.getPlugin().getConfig().getString("Msg.message.unknownCmd").replace("&", "§"));
duplicate_object.replaceby(Main.getPlugin().getConfig().getString("Msg.Exception.duplicate_object").replace("&", "§"));
Main.setMaxImpulsions(Main.getPlugin().getConfig().getInt("MaxImpulsion"));
Main.setDelay(Main.getPlugin().getConfig().getInt("Delay"));
Main.setNotifyAdmin(Main.getPlugin().getConfig().getBoolean("NotifyAdmins"));
Main.setLine1(Main.getPlugin().getConfig().getString("Sign.Line1").replace("&", "§"));
Main.setLine2(Main.getPlugin().getConfig().getString("Sign.Line2").replace("&", "§"));
Main.setLine3(Main.getPlugin().getConfig().getString("Sign.Line3").replace("&", "§"));
Main.setLine4(Main.getPlugin().getConfig().getString("Sign.Line4").replace("&", "§"));
}
/*public static void DefaultMsg(){
//default
Main.getPlugin().getConfig().set("Msg.default.prefix", Prefix.toString().replace("§", "&"));
Main.getPlugin().getConfig().set("Msg.default.error", ERREUR.toString().replace("§", "&"));
Main.getPlugin().getConfig().set("Msg.default.no_permission", NO_PERMISSIONS.toString().replace("§", "&"));
Main.getPlugin().getConfig().set("Msg.default.command_use", Command_Use.toString().replace("§", "&"));
Main.getPlugin().getConfig().set("Msg.message.MsgToAdmin", MsgToAdmin.toString().replace("§", "&"));
Main.getPlugin().getConfig().set("Msg.Exception.duplicate_object", duplicate_object.toString().replace("§", "&"));
}*/
/* void set(String value) {
//this.value = value;
}
*/
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment