WiredSettings Serialization on Database

This commit is contained in:
Stankman 2023-06-15 19:06:18 -05:00
parent 7cf3e3e863
commit 00a81ed5c3
13 changed files with 97 additions and 135 deletions

View File

@ -3,11 +3,11 @@ package com.eu.habbo.habbohotel.items.interactions;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
import com.eu.habbo.habbohotel.items.interactions.wired.effects.WiredEffectWhisper;
import com.eu.habbo.habbohotel.items.interactions.wired.interfaces.IWiredInteraction;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.rooms.RoomUnit;
import com.eu.habbo.habbohotel.users.HabboItem;
import com.eu.habbo.habbohotel.wired.WiredExclusionStrategy;
import com.eu.habbo.habbohotel.wired.WiredHandler;
import com.eu.habbo.messages.ClientMessage;
import com.eu.habbo.messages.incoming.wired.WiredSaveException;
@ -40,7 +40,7 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
private long cooldown;
private final TLongLongHashMap userExecutionCache = new TLongLongHashMap(3);
InteractionWired(ResultSet set, Item baseItem) throws SQLException {
public InteractionWired(ResultSet set, Item baseItem) throws SQLException {
super(set, baseItem);
this.items = new THashSet<>();
this.wiredData = "";
@ -57,17 +57,14 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
}
public abstract boolean execute(RoomUnit roomUnit, Room room, Object[] stuff);
public abstract boolean saveData() throws WiredSaveException;
@Override
public void run() {
if (this.needsUpdate()) {
String wiredData = this.getWiredData();
WiredExclusionStrategy exclusionStrategy = new WiredExclusionStrategy(this.wiredSettings);
if (wiredData == null) {
wiredData = "";
}
String wiredData = WiredHandler.getGsonBuilder().setExclusionStrategies(exclusionStrategy).create().toJson(this.wiredSettings);
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE items SET wired_data = ? WHERE id = ?")) {
if (this.getRoomId() != 0) {
@ -105,7 +102,6 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
return 50L;
}
public boolean canExecute(long newMillis) {
return newMillis - this.cooldown >= this.requiredCooldown();
}
@ -143,7 +139,7 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
this.userExecutionCache.put(roomUnitId, timestamp);
}
public WiredSettings loadWiredSettings(ClientMessage packet, boolean isWiredEffect) {
public void loadWiredSettings(ClientMessage packet, boolean isWiredEffect) {
WiredSettings settings = new WiredSettings();
int intParamCount = packet.readInt();
@ -157,8 +153,6 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
settings.setIntegerParams(integerParams);
settings.setStringParam(packet.readString());
System.out.println(settings.getStringParam());
int itemCount = packet.readInt();
int[] itemIds = new int[itemCount];
@ -177,29 +171,17 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
settings.setSelectionType(packet.readInt());
this.wiredSettings = settings;
return this.wiredSettings;
}
public void loadWiredSettings(ResultSet set, Room room) throws SQLException {
WiredSettings settings = new WiredSettings();
String wiredData = set.getString("wired_data");
// if(wiredData.startsWith("{")) {
// WiredEffectWhisper.JsonData data = WiredHandler.getGsonBuilder().create().fromJson(wiredData, WiredEffectWhisper.JsonData.class);
// this.getWiredSettings().setDelay(data.delay);
// this.message = data.message;
// }
// else {
// this.message = "";
//
// if (wiredData.split("\t").length >= 2) {
// super.setDelay(Integer.parseInt(wiredData.split("\t")[0]));
// this.message = wiredData.split("\t")[1];
// }
//
// this.needsUpdate(true);
// }
WiredSettings settings = new WiredSettings();
if(wiredData.startsWith("{")) {
settings = WiredHandler.getGsonBuilder().create().fromJson(wiredData, WiredSettings.class);
}
this.wiredSettings = settings;
}
}

View File

@ -1,9 +1,10 @@
package com.eu.habbo.habbohotel.items.interactions.wired;
import com.eu.habbo.habbohotel.items.interactions.wired.interfaces.IWiredSettings;
import lombok.Getter;
import lombok.Setter;
public class WiredSettings {
public class WiredSettings implements IWiredSettings {
@Getter
@Setter
private int[] integerParams;

View File

@ -38,9 +38,6 @@ public class WiredConditionGroupMember extends InteractionWiredCondition {
return "";
}
@Override
public void loadWiredSettings(ResultSet set, Room room) {}
@Override
public WiredConditionType getType() {
return type;

View File

@ -23,7 +23,7 @@ public class WiredEffectAlert extends WiredEffectWhisper {
Habbo habbo = room.getHabbo(roomUnit);
if (habbo != null) {
habbo.alert(this.message
habbo.alert(this.getWiredSettings().getStringParam()
.replace("%online%", Emulator.getGameEnvironment().getHabboManager().getOnlineCount() + "")
.replace("%username%", habbo.getHabboInfo().getUsername())
.replace("%roomsloaded%", Emulator.getGameEnvironment().getRoomManager().loadedRoomsCount() + ""));

View File

@ -21,7 +21,7 @@ public class WiredEffectGiveEffect extends WiredEffectWhisper {
int effectId;
try {
effectId = Integer.parseInt(this.message);
effectId = Integer.parseInt(this.getWiredSettings().getStringParam());
} catch (Exception e) {
return false;
}

View File

@ -20,7 +20,7 @@ public class WiredEffectGiveHandItem extends WiredEffectWhisper {
@Override
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
try {
int itemId = Integer.parseInt(this.message);
int itemId = Integer.parseInt(this.getWiredSettings().getStringParam());
Habbo habbo = room.getHabbo(roomUnit);

View File

@ -20,8 +20,6 @@ import java.sql.SQLException;
public class WiredEffectWhisper extends InteractionWiredEffect {
public static final WiredEffectType type = WiredEffectType.SHOW_MESSAGE;
protected String message = "";
public WiredEffectWhisper(ResultSet set, Item baseItem) throws SQLException {
super(set, baseItem);
}
@ -31,31 +29,18 @@ public class WiredEffectWhisper extends InteractionWiredEffect {
}
@Override
public boolean saveData() throws WiredSaveException {
String message = this.getWiredSettings().getStringParam();
//TODO Removed ability to `If user has rights of SUPER WIRED can override these two lines`
message = Emulator.getGameEnvironment().getWordFilter().filter(message, null);
message = message.substring(0, Math.min(message.length(), Emulator.getConfig().getInt("hotel.wired.message.max_length", 100)));
int delay = this.getWiredSettings().getDelay();
if(delay > Emulator.getConfig().getInt("hotel.wired.max_delay", 20))
throw new WiredSaveException("Delay too long");
this.message = message;
this.getWiredSettings().setDelay(delay);
public boolean saveData() {
return true;
}
@Override
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
if (this.message.length() > 0) {
if (this.getWiredSettings().getStringParam().length() > 0) {
if (roomUnit != null) {
Habbo habbo = room.getHabbo(roomUnit);
if (habbo != null) {
String msg = this.message.replace("%user%", habbo.getHabboInfo().getUsername()).replace("%online_count%", Emulator.getGameEnvironment().getHabboManager().getOnlineCount() + "").replace("%room_count%", Emulator.getGameEnvironment().getRoomManager().getActiveRooms().size() + "");
String msg = this.getWiredSettings().getStringParam().replace("%user%", habbo.getHabboInfo().getUsername()).replace("%online_count%", Emulator.getGameEnvironment().getHabboManager().getOnlineCount() + "").replace("%room_count%", Emulator.getGameEnvironment().getRoomManager().getActiveRooms().size() + "");
habbo.getClient().sendResponse(new WhisperMessageComposer(new RoomChatMessage(msg, habbo, habbo, RoomChatMessageBubbles.WIRED)));
Emulator.getThreading().run(() -> WiredHandler.handle(WiredTriggerType.SAY_SOMETHING, roomUnit, room, new Object[]{ msg }));
@ -66,7 +51,7 @@ public class WiredEffectWhisper extends InteractionWiredEffect {
}
} else {
for (Habbo h : room.getHabbos()) {
h.getClient().sendResponse(new WhisperMessageComposer(new RoomChatMessage(this.message.replace("%user%", h.getHabboInfo().getUsername()).replace("%online_count%", Emulator.getGameEnvironment().getHabboManager().getOnlineCount() + "").replace("%room_count%", Emulator.getGameEnvironment().getRoomManager().getActiveRooms().size() + ""), h, h, RoomChatMessageBubbles.WIRED)));
h.getClient().sendResponse(new WhisperMessageComposer(new RoomChatMessage(this.getWiredSettings().getStringParam().replace("%user%", h.getHabboInfo().getUsername()).replace("%online_count%", Emulator.getGameEnvironment().getHabboManager().getOnlineCount() + "").replace("%room_count%", Emulator.getGameEnvironment().getRoomManager().getActiveRooms().size() + ""), h, h, RoomChatMessageBubbles.WIRED)));
}
return true;
@ -75,11 +60,6 @@ public class WiredEffectWhisper extends InteractionWiredEffect {
return false;
}
@Override
public String getWiredData() {
return WiredHandler.getGsonBuilder().create().toJson(new JsonData(this.message, this.getWiredSettings().getDelay()));
}
@Override
public WiredEffectType getType() {
return type;
@ -89,14 +69,4 @@ public class WiredEffectWhisper extends InteractionWiredEffect {
public boolean requiresTriggeringUser() {
return true;
}
static class JsonData {
String message;
int delay;
public JsonData(String message, int delay) {
this.message = message;
this.delay = delay;
}
}
}

View File

@ -0,0 +1,17 @@
package com.eu.habbo.habbohotel.items.interactions.wired.interfaces;
import lombok.Getter;
import lombok.Setter;
public interface IWiredSettings {
int[] getIntegerParams();
void setIntegerParams(int[] value);
String getStringParam();
void setStringParam(String value);
int[] getItems();
void setItems(int[] value);
int getDelay();
void setDelay(int value);
int getSelectionType();
void setSelectionType(int value);
}

View File

@ -16,54 +16,37 @@ import java.sql.SQLException;
public class WiredTriggerHabboSaysKeyword extends InteractionWiredTrigger {
private static final WiredTriggerType type = WiredTriggerType.SAY_SOMETHING;
private boolean ownerOnly = false;
private String key = "";
private static int PARAM_OWNER_ONLY = 0;
public WiredTriggerHabboSaysKeyword(ResultSet set, Item baseItem) throws SQLException {
super(set, baseItem);
}
public WiredTriggerHabboSaysKeyword(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) {
super(id, userId, item, extradata, limitedStack, limitedSells);
public WiredTriggerHabboSaysKeyword(int id, int userId, Item item, String extraData, int limitedStack, int limitedSells) {
super(id, userId, item, extraData, limitedStack, limitedSells);
}
@Override
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
if (this.key.length() > 0) {
if (stuff[0] instanceof String) {
if (((String) stuff[0]).toLowerCase().contains(this.key.toLowerCase())) {
Habbo habbo = room.getHabbo(roomUnit);
return !this.ownerOnly || (habbo != null && room.getOwnerId() == habbo.getHabboInfo().getId());
}
if (this.getWiredSettings().getStringParam().isEmpty()) {
return false;
}
boolean ownerOnly = this.getWiredSettings().getIntegerParams()[PARAM_OWNER_ONLY] == 1;
if (stuff[0] instanceof String) {
if (((String) stuff[0]).toLowerCase().contains(this.getWiredSettings().getStringParam().toLowerCase())) {
Habbo habbo = room.getHabbo(roomUnit);
return !ownerOnly || (habbo != null && room.getOwnerId() == habbo.getHabboInfo().getId());
}
}
return false;
}
@Override
public String getWiredData() {
return WiredHandler.getGsonBuilder().create().toJson(new JsonData(
this.ownerOnly,
this.key
));
}
@Override
public void loadWiredSettings(ResultSet set, Room room) throws SQLException {
String wiredData = set.getString("wired_data");
if (wiredData.startsWith("{")) {
JsonData data = WiredHandler.getGsonBuilder().create().fromJson(wiredData, JsonData.class);
this.ownerOnly = data.ownerOnly;
this.key = data.key;
} else {
String[] data = wiredData.split("\t");
if (data.length == 2) {
this.ownerOnly = data[0].equalsIgnoreCase("1");
this.key = data[1];
}
}
return "";
}
@Override
@ -73,10 +56,6 @@ public class WiredTriggerHabboSaysKeyword extends InteractionWiredTrigger {
@Override
public boolean saveData() {
if(this.getWiredSettings().getIntegerParams().length < 1) return false;
this.ownerOnly = this.getWiredSettings().getIntegerParams()[0] == 1;
this.key = this.getWiredSettings().getStringParam();
return true;
}
@ -84,14 +63,4 @@ public class WiredTriggerHabboSaysKeyword extends InteractionWiredTrigger {
public boolean isTriggeredByRoomUnit() {
return true;
}
static class JsonData {
boolean ownerOnly;
String key;
public JsonData(boolean ownerOnly, String key) {
this.ownerOnly = ownerOnly;
this.key = key;
}
}
}

View File

@ -0,0 +1,35 @@
package com.eu.habbo.habbohotel.wired;
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
public class WiredExclusionStrategy implements ExclusionStrategy {
private WiredSettings wiredSettings;
public WiredExclusionStrategy(WiredSettings settings) {
this.wiredSettings = settings;
}
@Override
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
String fieldName = fieldAttributes.getName();
switch (fieldName) {
case "stringParam":
return this.wiredSettings.getStringParam().isEmpty();
case "integerParams":
return this.wiredSettings.getIntegerParams().length == 0;
case "delay":
return this.wiredSettings.getDelay() == 0;
case "items":
return this.wiredSettings.getItems().length == 0;
case "selectionType":
default:
return true;
}
}
@Override
public boolean shouldSkipClass(Class<?> aClass) {
return false;
}
}

View File

@ -29,12 +29,9 @@ public class UpdateActionEvent extends MessageHandler {
}
effect.loadWiredSettings(this.packet, true);
if (effect.saveData()) {
this.client.sendResponse(new WiredSavedComposer());
effect.needsUpdate(true);
Emulator.getThreading().run(effect);
}
this.client.sendResponse(new WiredSavedComposer());
effect.needsUpdate(true);
Emulator.getThreading().run(effect);
}
catch (WiredSaveException e) {
this.client.sendResponse(new WiredValidationErrorComposer(e.getMessage()));

View File

@ -27,12 +27,9 @@ public class UpdateConditionEvent extends MessageHandler {
}
condition.loadWiredSettings(this.packet, true);
if (condition.saveData()) {
this.client.sendResponse(new WiredSavedComposer());
condition.needsUpdate(true);
Emulator.getThreading().run(condition);
}
this.client.sendResponse(new WiredSavedComposer());
condition.needsUpdate(true);
Emulator.getThreading().run(condition);
}
catch (WiredSaveException e) {
this.client.sendResponse(new WiredValidationErrorComposer(e.getMessage()));

View File

@ -27,12 +27,9 @@ public class UpdateTriggerEvent extends MessageHandler {
}
trigger.loadWiredSettings(this.packet, false);
if (trigger.saveData()) {
this.client.sendResponse(new WiredSavedComposer());
trigger.needsUpdate(true);
Emulator.getThreading().run(trigger);
}
this.client.sendResponse(new WiredSavedComposer());
trigger.needsUpdate(true);
Emulator.getThreading().run(trigger);
}
catch (WiredSaveException e) {
this.client.sendResponse(new WiredValidationErrorComposer(e.getMessage()));