Support multiple triggers in one block

This commit is contained in:
Evert Prants 2020-10-04 16:51:50 +03:00
parent 9346bc07e3
commit 343a86b44c
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
5 changed files with 22 additions and 20 deletions

View File

@ -1,7 +0,0 @@
package ee.lunasqu.interaqqt;
public interface IConnection {
public boolean sendStateChange(String key, String value);
public boolean isAlive();
public void die();
}

View File

@ -5,7 +5,7 @@ import ee.lunasqu.interaqqt.events.DisconnectedEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.*;
public class MQTTConnection implements IConnection { public class MQTTConnection {
IMqttClient publisher; IMqttClient publisher;
public MQTTConnection (String host, String id) { public MQTTConnection (String host, String id) {
@ -24,7 +24,6 @@ public class MQTTConnection implements IConnection {
Bukkit.getPluginManager().callEvent(new ConnectionEstablishedEvent()); Bukkit.getPluginManager().callEvent(new ConnectionEstablishedEvent());
} }
@Override
public boolean sendStateChange(String key, String value) { public boolean sendStateChange(String key, String value) {
if (!this.isAlive()) return false; if (!this.isAlive()) return false;
MqttMessage msg = new MqttMessage(value.getBytes()); MqttMessage msg = new MqttMessage(value.getBytes());
@ -39,12 +38,10 @@ public class MQTTConnection implements IConnection {
return true; return true;
} }
@Override
public boolean isAlive() { public boolean isAlive() {
return this.publisher.isConnected(); return this.publisher.isConnected();
} }
@Override
public void die() { public void die() {
if (this.publisher != null && this.publisher.isConnected()) { if (this.publisher != null && this.publisher.isConnected()) {
try { try {

View File

@ -15,7 +15,7 @@ public class Main extends JavaPlugin {
public static Main plugin; public static Main plugin;
private FileConfiguration config; private FileConfiguration config;
private IConnection service; private MQTTConnection service;
private BlockTriggers triggers; private BlockTriggers triggers;
private RightClickAdder adder; private RightClickAdder adder;
@ -27,7 +27,7 @@ public class Main extends JavaPlugin {
return triggers; return triggers;
} }
public IConnection getConnection() { public MQTTConnection getConnection() {
if (service == null || !service.isAlive()) return null; if (service == null || !service.isAlive()) return null;
return service; return service;
} }

View File

@ -34,6 +34,7 @@ public class RightClickAdder implements Listener {
} }
} }
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) { public void onPlayerLeave(PlayerQuitEvent event) {
if (triggerHashMap.containsKey(event.getPlayer().getUniqueId())) { if (triggerHashMap.containsKey(event.getPlayer().getUniqueId())) {
triggerHashMap.remove(event.getPlayer().getUniqueId()); triggerHashMap.remove(event.getPlayer().getUniqueId());

View File

@ -46,11 +46,12 @@ public class BlockTriggers {
return null; return null;
} }
public BlockTrigger getTriggerByLocation (Location block) { public List<BlockTrigger> getTriggersByLocation (Location block) {
List<BlockTrigger> newList = new ArrayList<BlockTrigger>();
for (BlockTrigger t : triggers) { for (BlockTrigger t : triggers) {
if (t.getBlock().equals(block)) return t; if (t.getBlock().equals(block)) newList.add(t);
} }
return null; return newList;
} }
public List<BlockTrigger> getTriggerList () { public List<BlockTrigger> getTriggerList () {
@ -79,6 +80,8 @@ public class BlockTriggers {
public void saveConfiguration () { public void saveConfiguration () {
if (triggerConfig == null) this.loadConfiguration(); if (triggerConfig == null) this.loadConfiguration();
triggerConfig = new YamlConfiguration();
Map<String,BlockTrigger> triggerMap = new HashMap<String,BlockTrigger>(); Map<String,BlockTrigger> triggerMap = new HashMap<String,BlockTrigger>();
for (BlockTrigger trigger : triggers) { for (BlockTrigger trigger : triggers) {
triggerConfig.set(trigger.getName(), trigger); triggerConfig.set(trigger.getName(), trigger);
@ -91,10 +94,7 @@ public class BlockTriggers {
} }
} }
public boolean triggerLocation (Location block, int power) { private boolean trigger (BlockTrigger triggerData, int power) {
BlockTrigger triggerData = getTriggerByLocation(block);
if (triggerData == null) return false;
String send = null; String send = null;
String data = triggerData.getData(); String data = triggerData.getData();
int type = triggerData.getType(); int type = triggerData.getType();
@ -124,4 +124,15 @@ public class BlockTriggers {
Main.plugin.getConnection().sendStateChange(triggerData.getMqttTrigger(), send); Main.plugin.getConnection().sendStateChange(triggerData.getMqttTrigger(), send);
return true; return true;
} }
public boolean triggerLocation (Location block, int power) {
List<BlockTrigger> blockTriggerList = getTriggersByLocation(block);
if (blockTriggerList.isEmpty()) return false;
for (BlockTrigger blockTrigger : blockTriggerList) {
trigger(blockTrigger, power);
}
return true;
}
} }