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.eclipse.paho.client.mqttv3.*;
public class MQTTConnection implements IConnection {
public class MQTTConnection {
IMqttClient publisher;
public MQTTConnection (String host, String id) {
@ -24,7 +24,6 @@ public class MQTTConnection implements IConnection {
Bukkit.getPluginManager().callEvent(new ConnectionEstablishedEvent());
}
@Override
public boolean sendStateChange(String key, String value) {
if (!this.isAlive()) return false;
MqttMessage msg = new MqttMessage(value.getBytes());
@ -39,12 +38,10 @@ public class MQTTConnection implements IConnection {
return true;
}
@Override
public boolean isAlive() {
return this.publisher.isConnected();
}
@Override
public void die() {
if (this.publisher != null && this.publisher.isConnected()) {
try {

View File

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

View File

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

View File

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