More refactoring

This commit is contained in:
Stankman 2023-06-17 11:25:00 -05:00
parent 94dd34a11e
commit a152105453
18 changed files with 170 additions and 128 deletions

View File

@ -1,6 +1,7 @@
package com.eu.habbo.habbohotel.items.interactions;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
import com.eu.habbo.habbohotel.items.interactions.wired.interfaces.IWiredInteraction;
@ -9,8 +10,11 @@ import com.eu.habbo.habbohotel.rooms.RoomUnit;
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;
import com.eu.habbo.messages.outgoing.MessageComposer;
import com.eu.habbo.messages.outgoing.rooms.items.OneWayDoorStatusMessageComposer;
import com.eu.habbo.messages.outgoing.wired.WiredConditionDataComposer;
import com.eu.habbo.messages.outgoing.wired.WiredEffectDataComposer;
import com.eu.habbo.messages.outgoing.wired.WiredTriggerDataComposer;
import gnu.trove.map.hash.TLongLongHashMap;
import lombok.Getter;
import lombok.Setter;
@ -25,10 +29,6 @@ import java.util.List;
@Slf4j
public abstract class InteractionWired extends InteractionDefault implements IWiredInteraction {
@Getter
@Setter
private String wiredData;
@Getter
@Setter
private WiredSettings wiredSettings;
@ -38,20 +38,111 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
public InteractionWired(ResultSet set, Item baseItem) throws SQLException {
super(set, baseItem);
this.wiredData = "";
this.wiredSettings = new WiredSettings();
this.setExtradata("0");
}
InteractionWired(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) {
super(id, userId, item, extradata, limitedStack, limitedSells);
this.wiredData = "";
this.wiredSettings = new WiredSettings();
this.setExtradata("0");
}
public abstract boolean execute(RoomUnit roomUnit, Room room, Object[] stuff);
/**
* On Room Loading run this, get Wired Data from Database and save it into the item
*
* @param set
* @throws SQLException
*/
public void loadWiredSettings(ResultSet set) throws SQLException {
String wiredData = set.getString("wired_data");
this.wiredSettings = new WiredSettings();
if(wiredData.startsWith("{")) {
this.wiredSettings = WiredHandler.getGsonBuilder().create().fromJson(wiredData, WiredSettings.class);
}
this.loadDefaultParams();
}
/**
*
* When double clicking into the wired, verify items first and load its default parameters
* then create a composer based on it's Wired Settings
*
* @param client
* @param room
* @param objects
* @throws Exception
*/
@Override
public void onClick(GameClient client, Room room, Object[] objects) throws Exception {
this.wiredSettings.getItems(room);
//TODO Im not sure about this, the function is optional on its children maybe later just make it abstract
this.loadDefaultParams();
if (client != null) {
if (room.hasRights(client.getHabbo())) {
MessageComposer composer = null;
if(this instanceof InteractionWiredEffect) {
composer = new WiredEffectDataComposer((InteractionWiredEffect) this, room);
} else if(this instanceof InteractionWiredCondition) {
composer = new WiredConditionDataComposer((InteractionWiredCondition) this, room);
} else if(this instanceof InteractionWiredTrigger) {
composer = new WiredTriggerDataComposer((InteractionWiredTrigger) this, room);
}
client.sendResponse(composer);
this.activateBox(room);
}
}
}
/**
* When click save changes on the wired this executes, reads all the packet
* And updates wired current Wired Settings
*
* @param packet
*/
public void saveWiredSettings(ClientMessage packet) {
int intParamCount = packet.readInt();
List<Integer> integerParams = new ArrayList<>();
for(int i = 0; i < intParamCount; i++)
{
integerParams.add(packet.readInt());
}
this.wiredSettings.setIntegerParams(integerParams);
this.wiredSettings.setStringParam(packet.readString());
int itemCount = packet.readInt();
List<Integer> itemIds = new ArrayList<>();
for(int i = 0; i < itemCount; i++)
{
itemIds.add(packet.readInt());
}
this.wiredSettings.setItemIds(itemIds);
if(this instanceof InteractionWiredEffect) {
this.wiredSettings.setDelay(packet.readInt());
}
this.wiredSettings.setSelectionType(packet.readInt());
}
/**
* This is executed on 3 different situations
* When finishing executing: `saveWiredSettings`, when placing this item on floor, and when picking it up.
* This what it does is converts Wired Settings into a JSON string
* and updates wired_data in the database
*/
@Override
public void run() {
if (this.needsUpdate()) {
@ -62,6 +153,10 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
String wiredData = WiredHandler.getGsonBuilder().setExclusionStrategies(exclusionStrategy).create().toJson(this.wiredSettings);
if(wiredData.equalsIgnoreCase("{}")) {
wiredData = "";
}
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE items SET wired_data = ? WHERE id = ?")) {
if (this.getRoomId() != 0) {
statement.setString(1, wiredData);
@ -77,12 +172,18 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
super.run();
}
/**
* When picking up the wired, all its settings are erased and updated in database
*
* @param room
*/
@Override
public void onPickUp(Room room) {
this.wiredSettings = null;
//TODO not sure about this
this.wiredSettings.dispose();
}
public void loadDefaultParams() {}
public void activateBox(Room room) {
this.activateBox(room, null, 0L);
}
@ -123,7 +224,6 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
long lastTimestamp = this.userExecutionCache.get(roomUnitId);
return timestamp - lastTimestamp >= 100L;
}
}
return true;
}
@ -135,50 +235,4 @@ public abstract class InteractionWired extends InteractionDefault implements IWi
public void addUserExecutionCache(int roomUnitId, long timestamp) {
this.userExecutionCache.put(roomUnitId, timestamp);
}
public void loadWiredSettings(ClientMessage packet, boolean isWiredEffect) {
WiredSettings settings = new WiredSettings();
int intParamCount = packet.readInt();
List<Integer> integerParams = new ArrayList<>();
for(int i = 0; i < intParamCount; i++)
{
integerParams.add(packet.readInt());
}
settings.setIntegerParams(integerParams);
settings.setStringParam(packet.readString());
int itemCount = packet.readInt();
List<Integer> itemIds = new ArrayList<>();
for(int i = 0; i < itemCount; i++)
{
itemIds.add(packet.readInt());
}
settings.setItemIds(itemIds);
if(isWiredEffect)
{
settings.setDelay(packet.readInt());
}
settings.setSelectionType(packet.readInt());
this.wiredSettings = settings;
}
public void loadWiredSettings(ResultSet set, Room room) throws SQLException {
String wiredData = set.getString("wired_data");
WiredSettings settings = new WiredSettings();
if(wiredData.startsWith("{")) {
settings = WiredHandler.getGsonBuilder().create().fromJson(wiredData, WiredSettings.class);
}
this.wiredSettings = settings;
}
}

View File

@ -19,16 +19,6 @@ public abstract class InteractionWiredCondition extends InteractionWired {
super(id, userId, item, extradata, limitedStack, limitedSells);
}
@Override
public void onClick(GameClient client, Room room, Object[] objects) {
if (client != null) {
if (room.hasRights(client.getHabbo())) {
client.sendResponse(new WiredConditionDataComposer(this, room));
this.activateBox(room);
}
}
}
public abstract WiredConditionType getType();
public WiredConditionOperator operator() {

View File

@ -48,16 +48,6 @@ public abstract class InteractionWiredEffect extends InteractionWired implements
return blockedTriggers;
}
@Override
public void onClick(GameClient client, Room room, Object[] objects) throws Exception {
if (client != null) {
if (room.hasRights(client.getHabbo())) {
client.sendResponse(new WiredEffectDataComposer(this, room));
this.activateBox(room);
}
}
}
public boolean requiresTriggeringUser() {
return false;
}

View File

@ -45,18 +45,7 @@ public abstract class InteractionWiredTrigger extends InteractionWired implement
return blockedEffects;
}
@Override
public void onClick(GameClient client, Room room, Object[] objects) {
if (client != null) {
if (room.hasRights(client.getHabbo())) {
client.sendResponse(new WiredTriggerDataComposer(this, room));
this.activateBox(room);
}
}
}
public boolean isTriggeredByRoomUnit() {
return false;
}
}

View File

@ -2,6 +2,7 @@ package com.eu.habbo.habbohotel.items.interactions.wired;
import com.eu.habbo.habbohotel.items.interactions.wired.interfaces.IWiredSettings;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.users.HabboItem;
import gnu.trove.map.hash.THashMap;
import gnu.trove.set.hash.THashSet;
@ -29,7 +30,7 @@ public class WiredSettings implements IWiredSettings {
@Getter
@Setter
private int selectionType;
private transient int selectionType;
public WiredSettings() {
this.itemIds = new ArrayList<>();
@ -42,11 +43,15 @@ public class WiredSettings implements IWiredSettings {
public THashSet<HabboItem> getItems(Room room) {
THashSet<HabboItem> items = new THashSet<>();
for(int itemId : this.itemIds) {
HabboItem item = room.getHabboItem(itemId);
if(this.itemIds.size() == 0) {
return items;
}
for(int i = 0; i < this.itemIds.size(); i++) {
HabboItem item = room.getHabboItem(this.itemIds.get(i));
if(item == null || item.getRoomId() == 0) {
this.itemIds.remove(itemId);
this.itemIds.remove(i);
continue;
}
@ -55,4 +60,12 @@ public class WiredSettings implements IWiredSettings {
return items;
}
public void dispose() {
this.integerParams.clear();
this.itemIds.clear();
this.stringParam = "";
this.delay = 0;
this.selectionType = 0;
}
}

View File

@ -32,6 +32,14 @@ public class WiredEffectMoveRotateFurni extends InteractionWiredEffect implement
super(id, userId, item, extradata, limitedStack, limitedSells);
}
@Override
public void loadDefaultParams() {
if(this.getWiredSettings().getIntegerParams().size() == 0) {
this.getWiredSettings().getIntegerParams().add(0);
this.getWiredSettings().getIntegerParams().add(0);
}
}
@Override
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
if(this.getWiredSettings().getItemIds().isEmpty()) {

View File

@ -4,8 +4,6 @@ import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.rooms.RoomUnit;
public interface IWiredInteraction {
String getWiredData();
void setWiredData(String value);
WiredSettings getWiredSettings();
void setWiredSettings(WiredSettings value);
boolean execute(RoomUnit roomUnit, Room room, Object[] stuff);

View File

@ -2,18 +2,16 @@ package com.eu.habbo.habbohotel.items.interactions.wired.triggers;
import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredTrigger;
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.rooms.RoomUnit;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.wired.WiredHandler;
import com.eu.habbo.habbohotel.wired.WiredTriggerType;
import com.eu.habbo.messages.ServerMessage;
import java.sql.ResultSet;
import java.sql.SQLException;
public class WiredTriggerHabboEntersRoom extends InteractionWiredTrigger {
public final int PARAM_ANY_USER = 0;
public WiredTriggerHabboEntersRoom(ResultSet set, Item baseItem) throws SQLException {
super(set, baseItem);
}
@ -24,7 +22,9 @@ public class WiredTriggerHabboEntersRoom extends InteractionWiredTrigger {
@Override
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
if(this.getWiredSettings().getStringParam().isEmpty()) {
boolean anyUser = this.getWiredSettings().getIntegerParams().get(PARAM_ANY_USER) == 0;
if(this.getWiredSettings().getStringParam().isEmpty() && !anyUser || anyUser) {
return true;
}
@ -37,6 +37,13 @@ public class WiredTriggerHabboEntersRoom extends InteractionWiredTrigger {
return false;
}
@Override
public void loadDefaultParams() {
if(this.getWiredSettings().getIntegerParams().size() == 0) {
this.getWiredSettings().getIntegerParams().add(0);
}
}
@Override
public WiredTriggerType getType() {
return WiredTriggerType.ENTER_ROOM;

View File

@ -24,6 +24,13 @@ public class WiredTriggerHabboSaysKeyword extends InteractionWiredTrigger {
super(id, userId, item, extraData, limitedStack, limitedSells);
}
@Override
public void loadDefaultParams() {
if(this.getWiredSettings().getIntegerParams().size() == 0) {
this.getWiredSettings().getIntegerParams().add(0);
}
}
@Override
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
if (this.getWiredSettings().getStringParam().isEmpty()) {

View File

@ -32,23 +32,14 @@ public class WiredTriggerRepeater extends InteractionWiredTrigger implements ICy
}
@Override
public void cycle(Room room) {
if(this.getWiredSettings() == null) {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection()) {
try (PreparedStatement statement = connection.prepareStatement("SELECT id, wired_data FROM items WHERE room_id = ? AND wired_data<>''")) {
statement.setInt(1, room.getId());
try (ResultSet set = statement.executeQuery()) {
while (set.next()) {
this.loadWiredSettings(set, room);
}
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
public void loadDefaultParams() {
if(this.getWiredSettings().getIntegerParams().size() == 0) {
this.getWiredSettings().getIntegerParams().add(1);
}
}
@Override
public void cycle(Room room) {
this.counter += 500;
if (this.counter >= this.getWiredSettings().getIntegerParams().get(PARAM_REPEAT_TIME) * 500) {
this.counter = 0;

View File

@ -14,9 +14,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
public class WiredTriggerRepeaterLong extends InteractionWiredTrigger implements ICycleable, WiredTriggerReset {
public static final int DEFAULT_DELAY = 10 * 5000;
private static final int PARAM_REPEAT_TIME = 0;
private int repeatTime = DEFAULT_DELAY;
private int counter = 0;
public WiredTriggerRepeaterLong(ResultSet set, Item baseItem) throws SQLException {
@ -32,6 +30,13 @@ public class WiredTriggerRepeaterLong extends InteractionWiredTrigger implements
return true;
}
@Override
public void loadDefaultParams() {
if(this.getWiredSettings().getIntegerParams().size() == 0) {
this.getWiredSettings().getIntegerParams().add(1);
}
}
@Override
public void cycle(Room room) {
this.counter += 500;

View File

@ -498,7 +498,7 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
HabboItem item = this.getHabboItem(set.getInt("id"));
if (item instanceof InteractionWired interactionWired) {
interactionWired.loadWiredSettings(set, this);
interactionWired.loadWiredSettings(set);
}
} catch (SQLException e) {
log.error(CAUGHT_SQL_EXCEPTION, e);

View File

@ -1,9 +1,7 @@
package com.eu.habbo.messages.incoming.wired;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.items.interactions.InteractionWired;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredEffect;
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
import com.eu.habbo.habbohotel.permissions.Permission;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.messages.incoming.MessageHandler;
@ -28,7 +26,7 @@ public class UpdateActionEvent extends MessageHandler {
throw new WiredSaveException(String.format("Wired effect with item id %s not found in room", itemId));
}
effect.loadWiredSettings(this.packet, true);
effect.saveWiredSettings(this.packet);
this.client.sendResponse(new WiredSavedComposer());
effect.needsUpdate(true);
Emulator.getThreading().run(effect);

View File

@ -1,9 +1,7 @@
package com.eu.habbo.messages.incoming.wired;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.items.interactions.InteractionWired;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredCondition;
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
import com.eu.habbo.habbohotel.permissions.Permission;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.messages.incoming.MessageHandler;
@ -26,7 +24,7 @@ public class UpdateConditionEvent extends MessageHandler {
throw new WiredSaveException(String.format("Wired condition with item id %s not found in room", itemId));
}
condition.loadWiredSettings(this.packet, true);
condition.saveWiredSettings(this.packet);
this.client.sendResponse(new WiredSavedComposer());
condition.needsUpdate(true);
Emulator.getThreading().run(condition);

View File

@ -1,9 +1,7 @@
package com.eu.habbo.messages.incoming.wired;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.items.interactions.InteractionWired;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredTrigger;
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
import com.eu.habbo.habbohotel.permissions.Permission;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.messages.incoming.MessageHandler;
@ -26,7 +24,7 @@ public class UpdateTriggerEvent extends MessageHandler {
throw new WiredSaveException(String.format("Wired trigger with item id %s not found in room", itemId));
}
trigger.loadWiredSettings(this.packet, false);
trigger.saveWiredSettings(this.packet);
this.client.sendResponse(new WiredSavedComposer());
trigger.needsUpdate(true);
Emulator.getThreading().run(trigger);

View File

@ -16,7 +16,6 @@ public class WiredConditionDataComposer extends MessageComposer {
@Override
protected ServerMessage composeInternal() {
this.response.init(Outgoing.wiredConditionDataComposer);
// this.condition.serializeWiredData(this.response, this.room); @DEPRECATED
this.response.appendBoolean(false);
this.response.appendInt(WiredHandler.MAXIMUM_FURNI_SELECTION);

View File

@ -17,8 +17,6 @@ public class WiredEffectDataComposer extends MessageComposer {
protected ServerMessage composeInternal() {
this.response.init(Outgoing.wiredEffectDataComposer);
// this.effect.serializeWiredData(this.response, this.room); @DEPRECATED
this.response.appendBoolean(false);
this.response.appendInt(WiredHandler.MAXIMUM_FURNI_SELECTION);
this.response.appendInt(this.effect.getWiredSettings().getItemIds().size());

View File

@ -16,7 +16,6 @@ public class WiredTriggerDataComposer extends MessageComposer {
@Override
protected ServerMessage composeInternal() {
this.response.init(Outgoing.wiredTriggerDataComposer);
// this.trigger.serializeWiredData(this.response, this.room); @DEPRECATED
this.response.appendBoolean(false);
this.response.appendInt(WiredHandler.MAXIMUM_FURNI_SELECTION);