139 lines
4.2 KiB
Java
139 lines
4.2 KiB
Java
package ee.lunasqu.interaqqt.triggers;
|
|
|
|
import ee.lunasqu.interaqqt.Main;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class BlockTriggers {
|
|
File triggerConfigFile;
|
|
FileConfiguration triggerConfig;
|
|
|
|
List<BlockTrigger> triggers = new ArrayList<BlockTrigger>();
|
|
public void addBlock (String name, Location block, String mqttTrigger, int type, String data) {
|
|
triggers.add(new BlockTrigger(name, block, mqttTrigger, type, data));
|
|
}
|
|
|
|
public void addBlock (BlockTrigger trigger) {
|
|
if(triggers.contains(trigger)) return;
|
|
triggers.add(trigger);
|
|
this.saveConfiguration();
|
|
}
|
|
|
|
public void removeBlock (BlockTrigger trigger) {
|
|
triggers.remove(trigger);
|
|
this.saveConfiguration();
|
|
}
|
|
|
|
public void removeBlock (String name) {
|
|
BlockTrigger trigger = this.getTriggerByName(name);
|
|
if (trigger == null) return;
|
|
this.removeBlock(trigger);
|
|
}
|
|
|
|
public BlockTrigger getTriggerByName (String name) {
|
|
for (BlockTrigger t : triggers) {
|
|
if (t.getName().equals(name)) return t;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public List<BlockTrigger> getTriggersByLocation (Location block) {
|
|
List<BlockTrigger> newList = new ArrayList<BlockTrigger>();
|
|
for (BlockTrigger t : triggers) {
|
|
if (t.getBlock().equals(block)) newList.add(t);
|
|
}
|
|
return newList;
|
|
}
|
|
|
|
public List<BlockTrigger> getTriggerList () {
|
|
return triggers;
|
|
}
|
|
|
|
public void loadConfiguration() {
|
|
triggerConfigFile = new File(Main.plugin.getDataFolder(), "triggers.yml");
|
|
if (!triggerConfigFile.exists()) {
|
|
triggerConfigFile.getParentFile().mkdirs();
|
|
Main.plugin.saveResource("triggers.yml", false);
|
|
}
|
|
|
|
triggerConfig = new YamlConfiguration();
|
|
try {
|
|
triggerConfig.load(triggerConfigFile);
|
|
} catch (IOException | InvalidConfigurationException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
Map<String, Object> triggerMap = triggerConfig.getValues(true);
|
|
for (Object trig : triggerMap.values()) {
|
|
triggers.add((BlockTrigger) trig);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
try {
|
|
triggerConfig.save(triggerConfigFile);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private boolean trigger (BlockTrigger triggerData, int power) {
|
|
String send = null;
|
|
String data = triggerData.getData();
|
|
int type = triggerData.getType();
|
|
|
|
if (data.equals("power")) {
|
|
send = String.valueOf(power);
|
|
} else {
|
|
send = data;
|
|
}
|
|
|
|
if (type == 0 && power == 0) return false;
|
|
if (type == 1 && power > 0) return false;
|
|
|
|
// Binary value
|
|
if (data.equals("binary")) {
|
|
if (type == 0 && power > 0) {
|
|
send = "1";
|
|
} else if (type == 1 && power == 0) {
|
|
send = "0";
|
|
} else if (type == 2) {
|
|
if (power > 0) send = "1";
|
|
else send = "0";
|
|
}
|
|
}
|
|
|
|
if (send == null) return false;
|
|
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;
|
|
}
|
|
}
|