Commit 294c82f9 authored by Amosar's avatar Amosar

Add features and bugfix

parent bddec231
#En:Don't touch this value
#Fr: Ne pas modifier cette valeur
version: 0.5
version: 0.6
#En:Maximal number Redstone blink authorized during the "Delay" period
#Fr:Nombre maximal de clignotement de redstone autorise pendant la periode "Delay"
MaxPulses: 150
......@@ -29,6 +29,12 @@ IgnoreWorlds: redstoneWorld/survival
#Fr: La list des Regions sur lequel le plugin est desactive separe par des "/", mettre un faut non si aucun
IgnoreRegions: redstone/admins
checkedClock:
comparator: true
observer: true
piston: true
redstoneAndRepeater: true
Msg:
default:
prefix: '&bAntiRedstoneClock &9&l> &r&b '
......
name: antiRedstoneClock
version: 0.8
version: 0.9
description: Limit the redstoneClock
author: Amosar
website: https://www.spigotmc.org/resources/antiredstoneclock.18557
main: com.trafalcraft.antiRedstoneClock.Main
......
......@@ -5,7 +5,12 @@ 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 org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
......@@ -37,13 +42,12 @@ public class Main extends JavaPlugin{
instance = this;
plugin = this;
Bukkit.getServer().getPluginManager().registerEvents(new PlayerListener(), this);
plugin.getConfig().options().copyDefaults(true);
plugin.saveDefaultConfig();
plugin.reloadConfig();
if(!getConfig().getString("version").equals("0.5")){
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);
......@@ -58,6 +62,26 @@ public class Main extends JavaPlugin{
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);
}
}catch (java.lang.NoSuchFieldError ignored){}
try{
Material checked = Material.OBSERVER;
if(plugin.getConfig().getBoolean("checkedClock.observer")) {
Bukkit.getServer().getPluginManager().registerEvents(new ObserverListener(), this);
}
}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);
}
checkTimer(getDelay());
long endTime = System.currentTimeMillis();
......@@ -81,7 +105,6 @@ public class Main extends JavaPlugin{
}
public void onDisable(){
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[]args){
......
package com.trafalcraft.antiRedstoneClock;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClock;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.*;
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.*;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClockController;
import com.trafalcraft.antiRedstoneClock.util.CustomConfig;
import com.trafalcraft.antiRedstoneClock.util.WorldGuardLink;
import org.bukkit.material.Observer;
public class PlayerListener implements Listener {
@EventHandler (priority = EventPriority.LOWEST)
public void onRedstoneClock(BlockRedstoneEvent e){
if (checkIgnoreWorldsAndRegions(e)) return;
if(e.getBlock().getType() == Material.REDSTONE_WIRE){
if(e.getOldCurrent() == 0){
checkAndUpdateRedstoneClockState(e);
}
}else if(e.getBlock().getType() == Material.DIODE_BLOCK_ON
|| e.getBlock().getType() == Material.REDSTONE_COMPARATOR_ON){
if(!RedstoneClockController.contains(e.getBlock().getLocation())){
try {
RedstoneClockController.addRedstone(e.getBlock().getLocation());
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
RedstoneClock redstoneClock = RedstoneClockController.getRedstoneClock(e.getBlock().getLocation());
if(!redstoneClock.isEnd()){
if(redstoneClock.getClock() >= Main.getMaximumPulses()){
final Block b = e.getBlock();
if(Main.isDropItems()){
e.getBlock().breakNaturally();
}else{
e.getBlock().setType(Material.AIR);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() {
@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);
}else{
redstoneClock.addOneToClock();
}
}
}
}
}
@EventHandler
public void onObserverUpdate(BlockPhysicsEvent e){
if(e.getBlock().getType() == Material.OBSERVER){
Observer obs = (Observer) e.getBlock().getState().getData();
if(obs.isPowered()){
if (checkIgnoreWorldsAndRegions(e)) return;
checkAndUpdateRedstoneClockState(e);
}
}
}
@EventHandler
public void onComparatorUpdate(BlockPhysicsEvent e){
if(e.getBlock().getType() == Material.REDSTONE_COMPARATOR_OFF){
if (checkIgnoreWorldsAndRegions(e)) return;
org.bukkit.material.Comparator comparator = (org.bukkit.material.Comparator) e.getBlock().getState().getData();
if(!RedstoneClockController.contains(e.getBlock().getLocation())){
try {
RedstoneClockController.addRedstone(e.getBlock().getLocation());
} catch (Exception e1) {
e1.printStackTrace();
}
}else {
RedstoneClock redstoneClock = RedstoneClockController.getRedstoneClock(e.getBlock().getLocation());
byte comparatorData = comparator.getData();
if(redstoneClock.getLastStatus() != comparatorData){
if (!redstoneClock.isEnd()) {
if (redstoneClock.getClock() >= Main.getMaximumPulses()) {
removeRedstoneClock(e);
} else {
if(redstoneClock.getLastStatus() > comparatorData) {
redstoneClock.addOneToClock();
}
redstoneClock.updateStatus(comparatorData);
}
}
}
}
}
}
private 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);
}else{
RedstoneClockController.getRedstoneClock(e.getBlock().getLocation()).addOneToClock();
}
}
}
}
private boolean checkIgnoreWorldsAndRegions(BlockEvent e) {
for(String ignoreWorld: Main.getIgnoredWorlds()){
if(e.getBlock().getWorld().getName().equals(ignoreWorld)){
return true;
}
}
return WorldGuardLink.checkAllowedRegion(e.getBlock().getLocation());
}
private void removeRedstoneClock(BlockEvent e) {
if (Main.isDropItems()) {
e.getBlock().breakNaturally();
}
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()));
if (Main.isNotifyAdmin()) {
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()));
@EventHandler (priority = EventPriority.HIGHEST)
public void onPlayerBreakRedstone(BlockBreakEvent e) {
try{
if (e.getBlock().getType() == Material.REDSTONE_WIRE
|| e.getBlock().getType() == Material.DIODE_BLOCK_ON
|| e.getBlock().getType() == Material.DIODE_BLOCK_OFF
|| e.getBlock().getType() == Material.PISTON_BASE
|| e.getBlock().getType() == Material.PISTON_EXTENSION
|| e.getBlock().getType() == Material.PISTON_MOVING_PIECE
|| e.getBlock().getType() == Material.PISTON_STICKY_BASE
|| e.getBlock().getType() == Material.REDSTONE_COMPARATOR
|| e.getBlock().getType() == Material.REDSTONE_COMPARATOR_OFF
|| e.getBlock().getType() == Material.REDSTONE_COMPARATOR_ON
|| e.getBlock().getType() == Material.OBSERVER) {
if (RedstoneClockController.contains(e.getBlock().getLocation())) {
RedstoneClockController.removeRedstoneByLocation(e.getBlock().getLocation());
}
}
}
RedstoneClockController.removeRedstoneByLocation(e.getBlock().getLocation());
}
@EventHandler (priority = EventPriority.HIGHEST)
public void onPlayerBreakRedstone(BlockBreakEvent e){
if(e.getBlock().getType() == Material.REDSTONE_WIRE
|| e.getBlock().getType() == Material.DIODE_BLOCK_ON
|| e.getBlock().getType() == Material.DIODE_BLOCK_OFF
|| e.getBlock().getType() == Material.REDSTONE_COMPARATOR
|| e.getBlock().getType() == Material.REDSTONE_COMPARATOR_OFF
|| e.getBlock().getType() == Material.REDSTONE_COMPARATOR_ON
|| e.getBlock().getType() == Material.OBSERVER){
if(RedstoneClockController.contains(e.getBlock().getLocation())){
RedstoneClockController.removeRedstoneByLocation(e.getBlock().getLocation());
}
}
}catch(java.lang.NoSuchFieldError ignored){}
if(e.getBlock().getType() == Material.SIGN || e.getBlock().getType() == Material.SIGN_POST){
BlockState block = e.getBlock().getState();
Sign sign = (Sign)block;
......
......@@ -17,7 +17,7 @@ public class SetMaxPulses {
public void performCMD(CommandSender sender, String... args){
try{
Main.setMaximumPulses(Integer.parseInt(args[1]));
Main.getInstance().getConfig().set("MaxImpulsion", Main.getMaximumPulses());
Main.getInstance().getConfig().set("MaxPulses", Main.getMaximumPulses());
Main.getInstance().saveConfig();
sender.sendMessage(CustomConfig.Prefix+CustomConfig.newValueInConfig.toString().replace("$setting", "\"MaxPulses\"").replace("$value", args[1]));
}catch(NumberFormatException e){
......
package com.trafalcraft.antiRedstoneClock.listener;
import com.trafalcraft.antiRedstoneClock.Main;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClock;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClockController;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
public class ComparatorListener implements Listener{
@EventHandler
public void onComparatorUpdate(BlockPhysicsEvent e){
if(e.getBlock().getType() == Material.REDSTONE_COMPARATOR_OFF){
if (Util.checkIgnoreWorldsAndRegions(e)) return;
org.bukkit.material.Comparator comparator = (org.bukkit.material.Comparator) e.getBlock().getState().getData();
if(!RedstoneClockController.contains(e.getBlock().getLocation())){
try {
RedstoneClockController.addRedstone(e.getBlock().getLocation());
} catch (Exception e1) {
e1.printStackTrace();
}
}else {
RedstoneClock redstoneClock = RedstoneClockController.getRedstoneClock(e.getBlock().getLocation());
byte comparatorData = comparator.getData();
if(redstoneClock.getLastStatus() != comparatorData){
if (!redstoneClock.isEnd()) {
if (redstoneClock.getClock() >= Main.getMaximumPulses()) {
Util.removeRedstoneClock(e);
} else {
if(redstoneClock.getLastStatus() > comparatorData) {
redstoneClock.addOneToClock();
}
redstoneClock.updateStatus(comparatorData);
}
}
}
}
}
}
}
package com.trafalcraft.antiRedstoneClock.listener;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.material.Observer;
public class ObserverListener implements Listener {
@EventHandler
public void onObserverUpdate(BlockPhysicsEvent e){
if(e.getBlock().getType() == Material.OBSERVER){
Observer obs = (Observer) e.getBlock().getState().getData();
if(obs.isPowered()){
if (Util.checkIgnoreWorldsAndRegions(e)) return;
Util.checkAndUpdateRedstoneClockState(e);
}
}
}
}
package com.trafalcraft.antiRedstoneClock.listener;
import com.trafalcraft.antiRedstoneClock.Main;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClock;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClockController;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
public class PistonListener implements Listener {
@EventHandler
public void onPistonExtendEvent(BlockPistonExtendEvent e){
if (Util.checkIgnoreWorldsAndRegions(e)) return;
if(RedstoneClockController.contains(e.getBlock().getLocation())){
RedstoneClock redstoneClock = RedstoneClockController.getRedstoneClock(e.getBlock().getLocation());
if(redstoneClock.getLastStatus() == 1 ) {
if (!redstoneClock.isEnd()) {
if (redstoneClock.getClock() >= Main.getMaximumPulses()) {
Util.removeRedstoneClock(e);
} else {
redstoneClock.addOneToClock();
redstoneClock.updateStatus(0);
}
}
}
}else{
try {
RedstoneClockController.addRedstone(e.getBlock().getLocation());
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
@EventHandler
public void onPistonRetractEvent(BlockPistonRetractEvent e){
if(RedstoneClockController.contains(e.getBlock().getLocation())){
RedstoneClock redstoneClock = RedstoneClockController.getRedstoneClock(e.getBlock().getLocation());
redstoneClock.updateStatus(1);
}
}
}
package com.trafalcraft.antiRedstoneClock.listener;
import com.trafalcraft.antiRedstoneClock.Main;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClock;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClockController;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockRedstoneEvent;
public class RedstoneListener implements Listener{
@EventHandler(priority = EventPriority.LOWEST)
public void onRedstoneClock(BlockRedstoneEvent e){
if (Util.checkIgnoreWorldsAndRegions(e)) return;
if(e.getBlock().getType() == Material.REDSTONE_WIRE){
if(e.getOldCurrent() == 0){
Util.checkAndUpdateRedstoneClockState(e);
}
}else if(e.getBlock().getType() == Material.DIODE_BLOCK_ON
|| e.getBlock().getType() == Material.REDSTONE_COMPARATOR_ON){
if(!RedstoneClockController.contains(e.getBlock().getLocation())){
try {
RedstoneClockController.addRedstone(e.getBlock().getLocation());
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
RedstoneClock redstoneClock = RedstoneClockController.getRedstoneClock(e.getBlock().getLocation());
if(!redstoneClock.isEnd()){
if(redstoneClock.getClock() >= Main.getMaximumPulses()){
Util.removeRedstoneClock(e);
}else{
redstoneClock.addOneToClock();
}
}
}
}
}
}
package com.trafalcraft.antiRedstoneClock.listener;
import com.trafalcraft.antiRedstoneClock.Main;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClockController;
import com.trafalcraft.antiRedstoneClock.util.CustomConfig;
import com.trafalcraft.antiRedstoneClock.util.WorldGuardLink;
import org.bukkit.Bukkit;
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.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);
}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;
}
}
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() {
@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);
}
}
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