71 lines
2.1 KiB
Java
71 lines
2.1 KiB
Java
package ee.lunasqu.redstoneoutput;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
import org.bukkit.Location;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.block.Block;
|
|
import org.bukkit.block.Sign;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.block.BlockRedstoneEvent;
|
|
|
|
import ee.lunasqu.redstoneoutput.events.ConnectionEstablishedEvent;
|
|
import ee.lunasqu.redstoneoutput.events.DisconnectedEvent;
|
|
|
|
public class EventListener implements Listener {
|
|
private static Material[] SIGNS = {
|
|
Material.ACACIA_SIGN, Material.ACACIA_WALL_SIGN,
|
|
Material.BIRCH_SIGN, Material.BIRCH_WALL_SIGN,
|
|
Material.CRIMSON_SIGN, Material.CRIMSON_WALL_SIGN,
|
|
Material.DARK_OAK_SIGN, Material.DARK_OAK_WALL_SIGN,
|
|
Material.JUNGLE_SIGN, Material.JUNGLE_WALL_SIGN,
|
|
Material.OAK_SIGN, Material.OAK_WALL_SIGN,
|
|
Material.SPRUCE_SIGN, Material.SPRUCE_WALL_SIGN
|
|
};
|
|
|
|
@EventHandler
|
|
public void onRedstone(BlockRedstoneEvent e) {
|
|
Block b = e.getBlock();
|
|
Location[] sides = {
|
|
b.getLocation().add(new Location(b.getWorld(), 0, 1.0, 0)),
|
|
b.getLocation().add(new Location(b.getWorld(), -1.0, 0, 0)),
|
|
b.getLocation().add(new Location(b.getWorld(), 1.0, 0, 0)),
|
|
b.getLocation().add(new Location(b.getWorld(), 0, 0, 1.0)),
|
|
b.getLocation().add(new Location(b.getWorld(), 0, 0, -1.0))
|
|
};
|
|
|
|
Sign sign = null;
|
|
List<Material> checklist = Arrays.asList(EventListener.SIGNS);
|
|
for (Location p : sides) {
|
|
Block test = b.getWorld().getBlockAt(p);
|
|
if (checklist.contains(test.getType())) {
|
|
Sign i = (Sign) test.getState();
|
|
String l = i.getLine(0);
|
|
if (l.equalsIgnoreCase("[Output]")) {
|
|
sign = i;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (sign == null) return;
|
|
String key = sign.getLine(1);
|
|
String value = sign.getLine(2);
|
|
Connection t = Main.plugin.getConnection();
|
|
if (t == null) return;
|
|
t.sendStateChange(e.getNewCurrent(), key, value);
|
|
}
|
|
|
|
@EventHandler
|
|
public void onConnClose(DisconnectedEvent e) {
|
|
Main.plugin.getLogger().info("Connection to remote failed.");
|
|
}
|
|
|
|
@EventHandler
|
|
public void onConnEstab(ConnectionEstablishedEvent e) {
|
|
Main.plugin.getLogger().info("Connection to remote established!");
|
|
}
|
|
}
|