Commit 80413b83 authored by Amosar's avatar Amosar

Improve clock count and compatibility with older spigot version

parent 5882f0df
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.block.Block;
import org.bukkit.block.data.Powerable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
......@@ -17,38 +13,23 @@ public class ComparatorListener implements Listener {
public void onRedstoneComparatorClock(BlockRedstoneEvent e) {
if (Util.checkIgnoreWorldsAndRegions(e.getBlock()))
return;
if (checkTypeAndItemPowered(e.getBlock())) {
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.getNumberOfClock() >= Main.getInstance().getConfig().getInt("MaxPulses")) {
Util.removeRedstoneClock(e.getBlock());
} else {
redstoneClock.addOneToClock();
}
}
if (checkType(e.getBlock())) {
if (e.getOldCurrent() == 0) {
Util.checkAndUpdateRedstoneClockState(e.getBlock());
}
}
}
private boolean checkTypeAndItemPowered(Block block) {
private boolean checkType(Block block) {
boolean result = false;
try {
if (block.getType() == Material.COMPARATOR) {
Powerable powerable = (Powerable) block.getBlockData();
if (powerable.isPowered()) {
result = true;
}
result = true;
}
} catch (NoSuchFieldError e) {
//1.12.2 and older version compatibility
if (block.getType() == Material.getMaterial("REDSTONE_COMPARATOR_ON")) {
if (block.getType() == Material.getMaterial("REDSTONE_COMPARATOR_OFF")
|| block.getType() == Material.getMaterial("REDSTONE_COMPARATOR_ON")) {
result = true;
}
}
......
......@@ -4,8 +4,6 @@ import com.trafalcraft.antiRedstoneClock.Main;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClock;
import com.trafalcraft.antiRedstoneClock.object.RedstoneClockController;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.Powerable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
......@@ -17,7 +15,7 @@ public class ObserverListener implements Listener {
public void onRedstoneComparatorClock(BlockRedstoneEvent e) {
if (Util.checkIgnoreWorldsAndRegions(e.getBlock()))
return;
if (checkTypeAndItemPowered(e.getBlock())) {
if (e.getBlock().getType() == Material.OBSERVER && e.getOldCurrent() == 0) {
if (!RedstoneClockController.contains(e.getBlock().getLocation())) {
try {
RedstoneClockController.addRedstone(e.getBlock().getLocation());
......@@ -42,15 +40,4 @@ public class ObserverListener implements Listener {
}
}
private boolean checkTypeAndItemPowered(Block block) {
boolean result = false;
if (block.getType() == Material.OBSERVER) {
Powerable powerable = (Powerable) block.getBlockData();
if (powerable.isPowered()) {
result = true;
}
}
return result;
}
}
\ No newline at end of file
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.block.Block;
import org.bukkit.block.data.Powerable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
......@@ -17,42 +13,24 @@ public class RedstoneListener implements Listener {
public void onRedstoneClock(BlockRedstoneEvent e) {
if (Util.checkIgnoreWorldsAndRegions(e.getBlock()))
return;
if (e.getBlock().getType() == Material.REDSTONE_WIRE) {
if (e.getBlock().getType() == Material.REDSTONE_WIRE
|| checkRepeaterType(e.getBlock())) {
if (e.getOldCurrent() == 0) {
Util.checkAndUpdateRedstoneClockState(e.getBlock());
}
} else if (checkTypeAndItemPowered(e.getBlock())) {
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.getNumberOfClock() >= Main.getInstance().getConfig().getInt("MaxPulses")) {
Util.removeRedstoneClock(e.getBlock());
} else {
redstoneClock.addOneToClock();
}
}
}
}
}
private boolean checkTypeAndItemPowered(Block block) {
private boolean checkRepeaterType(Block block) {
boolean result = false;
try {
if (block.getType() == Material.REPEATER) {
Powerable powerable = (Powerable) block.getBlockData();
if (powerable.isPowered()) {
result = true;
}
result = true;
}
} catch (NoSuchFieldError e) {
//1.12.2 and older version compatibility
if (block.getType() == Material.getMaterial("DIODE_BLOCK_ON")) {
if (block.getType() == Material.getMaterial("DIODE_BLOCK_ON")
|| block.getType() == Material.getMaterial("DIODE_BLOCK_OFF")) {
result = true;
}
}
......
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