WiredTriggerHabboWalkOffFurni now saves as JSON

This commit is contained in:
Remco 2021-01-04 21:18:02 +01:00
parent 19b1b187a5
commit c6af5baa70

View File

@ -14,14 +14,13 @@ import gnu.trove.set.hash.THashSet;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class WiredTriggerHabboWalkOffFurni extends InteractionWiredTrigger {
public static final WiredTriggerType type = WiredTriggerType.WALKS_OFF_FURNI;
private THashSet<HabboItem> items;
private String message = "";
public WiredTriggerHabboWalkOffFurni(ResultSet set, Item baseItem) throws SQLException {
super(set, baseItem);
@ -45,55 +44,49 @@ public class WiredTriggerHabboWalkOffFurni extends InteractionWiredTrigger {
@Override
public String getWiredData() {
StringBuilder wiredData = new StringBuilder(super.getDelay() + ":\t:");
if (!this.items.isEmpty()) {
List<HabboItem> toRemove = new ArrayList<>(0);
for (HabboItem item : this.items) {
if (item.getRoomId() == this.getRoomId()) {
wiredData.append(item.getId()).append(";");
} else {
toRemove.add(item);
}
}
this.items.removeAll(toRemove);
} else
wiredData.append("\t");
return wiredData.toString();
return WiredHandler.getGsonBuilder().create().toJson(new WiredTriggerFurniStateToggled.JsonData(
this.items.stream().map(HabboItem::getId).collect(Collectors.toList())
));
}
@Override
public void loadWiredData(ResultSet set, Room room) throws SQLException {
this.items.clear();
String wiredData = set.getString("wired_data");
if (wiredData.split(":").length >= 3) {
super.setDelay(Integer.valueOf(wiredData.split(":")[0]));
this.message = wiredData.split(":")[1];
if (!wiredData.split(":")[2].equals("\t")) {
for (String s : wiredData.split(":")[2].split(";")) {
if (s.isEmpty())
continue;
if (wiredData.startsWith("{")) {
JsonData data = WiredHandler.getGsonBuilder().create().fromJson(wiredData, JsonData.class);
for (Integer id: data.itemIds) {
HabboItem item = room.getHabboItem(id);
if (item != null) {
this.items.add(item);
}
}
} else {
if (wiredData.split(":").length >= 3) {
super.setDelay(Integer.parseInt(wiredData.split(":")[0]));
try {
HabboItem item = room.getHabboItem(Integer.valueOf(s));
if (!wiredData.split(":")[2].equals("\t")) {
for (String s : wiredData.split(":")[2].split(";")) {
if (s.isEmpty())
continue;
if (item != null)
this.items.add(item);
} catch (Exception e) {
try {
HabboItem item = room.getHabboItem(Integer.parseInt(s));
if (item != null)
this.items.add(item);
} catch (Exception e) {
}
}
}
}
}
}
@Override
public void onPickUp() {
this.items.clear();
this.message = "";
}
@Override
@ -126,7 +119,7 @@ public class WiredTriggerHabboWalkOffFurni extends InteractionWiredTrigger {
}
message.appendInt(this.getBaseItem().getSpriteId());
message.appendInt(this.getId());
message.appendString(this.message);
message.appendString("");
message.appendInt(0);
message.appendInt(0);
message.appendInt(this.getType().code);
@ -154,4 +147,12 @@ public class WiredTriggerHabboWalkOffFurni extends InteractionWiredTrigger {
public boolean isTriggeredByRoomUnit() {
return true;
}
static class JsonData {
List<Integer> itemIds;
public JsonData(List<Integer> itemIds) {
this.itemIds = itemIds;
}
}
}