WiredConditionMatchStatePosition now saves as JSON

This commit is contained in:
Remco 2021-01-04 22:28:17 +01:00
parent fe52ad3458
commit e6a5aaa279

View File

@ -15,6 +15,8 @@ import gnu.trove.set.hash.THashSet;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class WiredConditionMatchStatePosition extends InteractionWiredCondition {
public static final WiredConditionType type = WiredConditionType.MATCH_SSHOT;
@ -138,38 +140,42 @@ public class WiredConditionMatchStatePosition extends InteractionWiredCondition
@Override
public String getWiredData() {
StringBuilder data = new StringBuilder(this.settings.size() + ":");
if (this.settings.isEmpty()) {
data.append("\t;");
} else {
for (WiredMatchFurniSetting item : this.settings)
data.append(item.toString()).append(";");
}
data.append(":").append(this.state ? 1 : 0).append(":").append(this.direction ? 1 : 0).append(":").append(this.position ? 1 : 0);
return data.toString();
return WiredHandler.getGsonBuilder().create().toJson(new JsonData(
this.state,
this.position,
this.direction,
new ArrayList<>(this.settings)
));
}
@Override
public void loadWiredData(ResultSet set, Room room) throws SQLException {
String[] data = set.getString("wired_data").split(":");
String wiredData = set.getString("wired_data");
int itemCount = Integer.valueOf(data[0]);
if (wiredData.startsWith("{")) {
JsonData data = WiredHandler.getGsonBuilder().create().fromJson(wiredData, JsonData.class);
this.state = data.state;
this.position = data.position;
this.direction = data.direction;
this.settings.addAll(data.settings);
} else {
String[] data = wiredData.split(":");
String[] items = data[1].split(";");
int itemCount = Integer.parseInt(data[0]);
for (int i = 0; i < itemCount; i++) {
String[] stuff = items[i].split("-");
String[] items = data[1].split(";");
if (stuff.length >= 5)
this.settings.add(new WiredMatchFurniSetting(Integer.valueOf(stuff[0]), stuff[1], Integer.valueOf(stuff[2]), Integer.valueOf(stuff[3]), Integer.valueOf(stuff[4])));
for (int i = 0; i < itemCount; i++) {
String[] stuff = items[i].split("-");
if (stuff.length >= 5)
this.settings.add(new WiredMatchFurniSetting(Integer.parseInt(stuff[0]), stuff[1], Integer.parseInt(stuff[2]), Integer.parseInt(stuff[3]), Integer.parseInt(stuff[4])));
}
this.state = data[2].equals("1");
this.direction = data[3].equals("1");
this.position = data[4].equals("1");
}
this.state = data[2].equals("1");
this.direction = data[3].equals("1");
this.position = data[4].equals("1");
}
@Override
@ -198,4 +204,18 @@ public class WiredConditionMatchStatePosition extends InteractionWiredCondition
}
}
}
static class JsonData {
boolean state;
boolean position;
boolean direction;
List<WiredMatchFurniSetting> settings;
public JsonData(boolean state, boolean position, boolean direction, List<WiredMatchFurniSetting> settings) {
this.state = state;
this.position = position;
this.direction = direction;
this.settings = settings;
}
}
}