Commit 2f4089d6 authored by Amosar's avatar Amosar
parent a439ae9d
name: antiRedstoneClock
version: 0.10.1
version: 0.10.2
description: Limit the redstoneClock
author: Amosar
website: https://www.spigotmc.org/resources/antiredstoneclock.18557
......
package com.trafalcraft.antiRedstoneClock;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClockController;
import org.bukkit.Material;
import org.bukkit.block.*;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
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 org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
public class PlayerListener implements Listener {
......@@ -30,36 +32,34 @@ public class PlayerListener implements Listener {
}
}
}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;
if(sign.getLine(0).equalsIgnoreCase(Main.getLine1())
&& sign.getLine(1).equalsIgnoreCase(Main.getLine2())
&& sign.getLine(2).equalsIgnoreCase(Main.getLine3())
&& sign.getLine(3).equalsIgnoreCase(Main.getLine4())){
e.setCancelled(true);
e.getBlock().setType(Material.AIR);
}
}
if (e.getBlock().getType() == Material.SIGN || e.getBlock().getType() == Material.SIGN_POST) {
BlockState block = e.getBlock().getState();
Sign sign = (Sign) block;
if (sign.getLine(0).equalsIgnoreCase(Main.getLine1())
&& sign.getLine(1).equalsIgnoreCase(Main.getLine2())
&& sign.getLine(2).equalsIgnoreCase(Main.getLine3())
&& sign.getLine(3).equalsIgnoreCase(Main.getLine4())) {
e.setCancelled(true);
e.getBlock().setType(Material.AIR);
}
}
}
@EventHandler (priority = EventPriority.HIGHEST)
public void onItemDrop(BlockPhysicsEvent e){
if(e.getBlock().getType() == Material.SIGN || e.getBlock().getType() == Material.SIGN_POST){
BlockState block = e.getBlock().getState();
Sign sign = (Sign)block;
if(sign.getLine(0).equalsIgnoreCase(Main.getLine1())
&& sign.getLine(1).equalsIgnoreCase(Main.getLine2())
&& sign.getLine(2).equalsIgnoreCase(Main.getLine3())
&& sign.getLine(3).equalsIgnoreCase(Main.getLine4())
&& e.getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR){
e.setCancelled(true);
e.getBlock().setType(Material.AIR);
}
}
}
if (e.getBlock().getType() == Material.SIGN || e.getBlock().getType() == Material.SIGN_POST) {
BlockState block = e.getBlock().getState();
Sign sign = (Sign) block;
if ((sign.getLine(0).equalsIgnoreCase(Main.getLine1())
&& sign.getLine(1).equalsIgnoreCase(Main.getLine2())
&& sign.getLine(2).equalsIgnoreCase(Main.getLine3())
&& sign.getLine(3).equalsIgnoreCase(Main.getLine4())
&& e.getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR)) {
e.setCancelled(true);
e.getBlock().setType(Material.AIR);
}
}
}
}
......@@ -13,7 +13,7 @@ public class ComparatorListener implements Listener{
@EventHandler
public void onComparatorUpdate(BlockPhysicsEvent e) {
if (e.getBlock().getType() == Material.REDSTONE_COMPARATOR_OFF) {
if (Util.checkIgnoreWorldsAndRegions(e))
if (Util.checkIgnoreWorldsAndRegions(e.getBlock()))
return;
if (!RedstoneClockController.contains(e.getBlock().getLocation())) {
if(e.getBlock().isBlockPowered()
......@@ -35,7 +35,7 @@ public class ComparatorListener implements Listener{
if(status == 0) {
if (!redstoneClock.isEnd()) {
if (redstoneClock.getClock() >= Main.getMaximumPulses()) {
Util.removeRedstoneClock(e);
Util.removeRedstoneClock(e.getBlock());
} else {
redstoneClock.addOneToClock();
}
......
......@@ -13,8 +13,9 @@ public class ObserverListener implements Listener {
if(e.getBlock().getType() == Material.OBSERVER){
Observer obs = (Observer) e.getBlock().getState().getData();
if(obs.isPowered()){
if (Util.checkIgnoreWorldsAndRegions(e)) return;
Util.checkAndUpdateRedstoneClockState(e);
if (Util.checkIgnoreWorldsAndRegions(e.getBlock()))
return;
Util.checkAndUpdateRedstoneClockState(e.getBlock());
}
}
}
......
......@@ -11,13 +11,14 @@ import org.bukkit.event.block.BlockPistonRetractEvent;
public class PistonListener implements Listener {
@EventHandler
public void onPistonExtendEvent(BlockPistonExtendEvent e){
if (Util.checkIgnoreWorldsAndRegions(e)) return;
if (Util.checkIgnoreWorldsAndRegions(e.getBlock()))
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);
Util.removeRedstoneClock(e.getBlock());
} else {
redstoneClock.addOneToClock();
redstoneClock.updateStatus(0);
......
......@@ -13,10 +13,11 @@ public class RedstoneListener implements Listener{
@EventHandler(priority = EventPriority.LOWEST)
public void onRedstoneClock(BlockRedstoneEvent e){
if (Util.checkIgnoreWorldsAndRegions(e)) return;
if (Util.checkIgnoreWorldsAndRegions(e.getBlock()))
return;
if(e.getBlock().getType() == Material.REDSTONE_WIRE){
if(e.getOldCurrent() == 0){
Util.checkAndUpdateRedstoneClockState(e);
Util.checkAndUpdateRedstoneClockState(e.getBlock());
}
}else if(e.getBlock().getType() == Material.DIODE_BLOCK_ON
|| e.getBlock().getType() == Material.REDSTONE_COMPARATOR_ON){
......@@ -32,7 +33,7 @@ public class RedstoneListener implements Listener{
RedstoneClock redstoneClock = RedstoneClockController.getRedstoneClock(e.getBlock().getLocation());
if(!redstoneClock.isEnd()){
if(redstoneClock.getClock() >= Main.getMaximumPulses()){
Util.removeRedstoneClock(e);
Util.removeRedstoneClock(e.getBlock());
}else{
redstoneClock.addOneToClock();
}
......
......@@ -11,90 +11,89 @@ 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;
class Util {
static void checkAndUpdateRedstoneClockState(BlockEvent e) {
if(!RedstoneClockController.contains(e.getBlock().getLocation())){
static void checkAndUpdateRedstoneClockState(Block block) {
if (!RedstoneClockController.contains(block.getLocation())) {
try {
RedstoneClockController.addRedstone(e.getBlock().getLocation());
RedstoneClockController.addRedstone(block.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);
if (!RedstoneClockController.getRedstoneClock(block.getLocation()).isEnd()) {
if (RedstoneClockController.getRedstoneClock(block.getLocation()).getClock() >= Main
.getMaximumPulses()) {
removeRedstoneClock(block);
}else{
RedstoneClockController.getRedstoneClock(e.getBlock().getLocation()).addOneToClock();
RedstoneClockController.getRedstoneClock(block.getLocation()).addOneToClock();
}
}
}
}
static boolean checkIgnoreWorldsAndRegions(BlockEvent e) {
static boolean checkIgnoreWorldsAndRegions(Block block) {
for(String ignoreWorld: Main.getIgnoredWorlds()){
if(e.getBlock().getWorld().getName().equals(ignoreWorld)){
if (block.getWorld().getName().equals(ignoreWorld)) {
return true;
}
}
return WorldGuardLink.checkAllowedRegion(e.getBlock().getLocation());
return WorldGuardLink.checkAllowedRegion(block.getLocation());
}
static void removeRedstoneClock(BlockEvent e) {
Block b = e.getBlock();
static synchronized void removeRedstoneClock(Block block) {
if (Main.automaticallyDropDetectedItem()) {
if (Main.isDropItems()) {
e.getBlock().breakNaturally();
block.breakNaturally();
} else {
e.getBlock().setType(Material.AIR);
block.setType(Material.AIR);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> {
b.setType(Material.SIGN_POST);
BlockState block = b.getState();
Sign sign = (Sign) block;
block.setType(Material.SIGN_POST);
BlockState blockState = block.getState();
Sign sign = (Sign) blockState;
sign.setLine(0, Main.getLine1());
sign.setLine(1, Main.getLine2());
sign.setLine(2, Main.getLine3());
sign.setLine(3, Main.getLine4());
sign.update();
sign.update(false, false);
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()));
.replace("$X", block.getX() + "")
.replace("$Y", block.getY() + "").replace("$Z", block.getZ() + "")
.replace("$World", block.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()));
.replace("$X", block.getX() + "")
.replace("$Y", block.getY() + "")
.replace("$Z", block.getZ() + "")
.replace("$World", block.getWorld().getName()));
}
}
}
RedstoneClockController.removeRedstoneByLocation(b.getLocation());
RedstoneClockController.removeRedstoneByLocation(block.getLocation());
}, 1L);
} else {
RedstoneClock redstoneClock = RedstoneClockController.getRedstoneClock(b.getLocation());
RedstoneClock redstoneClock = RedstoneClockController.getRedstoneClock(block.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()));
.replace("$X", block.getX() + "")
.replace("$Y", block.getY() + "").replace("$Z", block.getZ() + "")
.replace("$World", block.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()));
.replace("$X", block.getX() + "")
.replace("$Y", block.getY() + "")
.replace("$Z", block.getZ() + "")
.replace("$World", block.getWorld().getName()));
}
}
}
......
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