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.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class WiredConditionMatchStatePosition extends InteractionWiredCondition { public class WiredConditionMatchStatePosition extends InteractionWiredCondition {
public static final WiredConditionType type = WiredConditionType.MATCH_SSHOT; public static final WiredConditionType type = WiredConditionType.MATCH_SSHOT;
@ -138,25 +140,28 @@ public class WiredConditionMatchStatePosition extends InteractionWiredCondition
@Override @Override
public String getWiredData() { public String getWiredData() {
StringBuilder data = new StringBuilder(this.settings.size() + ":"); return WiredHandler.getGsonBuilder().create().toJson(new JsonData(
this.state,
if (this.settings.isEmpty()) { this.position,
data.append("\t;"); this.direction,
} else { new ArrayList<>(this.settings)
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();
} }
@Override @Override
public void loadWiredData(ResultSet set, Room room) throws SQLException { 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(":");
int itemCount = Integer.parseInt(data[0]);
String[] items = data[1].split(";"); String[] items = data[1].split(";");
@ -164,13 +169,14 @@ public class WiredConditionMatchStatePosition extends InteractionWiredCondition
String[] stuff = items[i].split("-"); String[] stuff = items[i].split("-");
if (stuff.length >= 5) 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]))); 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.state = data[2].equals("1");
this.direction = data[3].equals("1"); this.direction = data[3].equals("1");
this.position = data[4].equals("1"); this.position = data[4].equals("1");
} }
}
@Override @Override
public void onPickUp() { public void onPickUp() {
@ -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;
}
}
} }