Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/conditions/WiredConditionNotFurniHaveFurni.java

179 lines
6.0 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.items.interactions.wired.conditions;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredCondition;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.rooms.RoomTile;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.rooms.RoomUnit;
import com.eu.habbo.habbohotel.users.HabboItem;
2019-03-18 02:22:00 +01:00
import com.eu.habbo.habbohotel.wired.WiredConditionOperator;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.wired.WiredConditionType;
import com.eu.habbo.habbohotel.wired.WiredHandler;
import com.eu.habbo.messages.ClientMessage;
import com.eu.habbo.messages.ServerMessage;
import gnu.trove.set.hash.THashSet;
import java.sql.ResultSet;
import java.sql.SQLException;
2019-05-26 20:14:53 +02:00
public class WiredConditionNotFurniHaveFurni extends InteractionWiredCondition {
2018-07-06 15:30:00 +02:00
public static final WiredConditionType type = WiredConditionType.NOT_FURNI_HAVE_FURNI;
private boolean all;
private THashSet<HabboItem> items;
2019-05-26 20:14:53 +02:00
public WiredConditionNotFurniHaveFurni(ResultSet set, Item baseItem) throws SQLException {
2018-07-06 15:30:00 +02:00
super(set, baseItem);
2018-09-28 21:25:00 +02:00
this.items = new THashSet<>();
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public WiredConditionNotFurniHaveFurni(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) {
2018-07-06 15:30:00 +02:00
super(id, userId, item, extradata, limitedStack, limitedSells);
2018-09-28 21:25:00 +02:00
this.items = new THashSet<>();
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
2018-07-06 15:30:00 +02:00
this.refresh();
2019-05-26 20:14:53 +02:00
if (this.items.isEmpty())
2018-07-06 15:30:00 +02:00
return true;
if(this.all) {
return this.items.stream().allMatch(item -> {
double minZ = item.getZ() + Item.getCurrentHeight(item);
THashSet<RoomTile> occupiedTiles = room.getLayout().getTilesAt(room.getLayout().getTile(item.getX(), item.getY()), item.getBaseItem().getWidth(), item.getBaseItem().getLength(), item.getRotation());
return occupiedTiles.stream().noneMatch(tile -> room.getItemsAt(tile).stream().anyMatch(matchedItem -> matchedItem != item && matchedItem.getZ() >= minZ));
});
}
else {
return this.items.stream().anyMatch(item -> {
double minZ = item.getZ() + Item.getCurrentHeight(item);
THashSet<RoomTile> occupiedTiles = room.getLayout().getTilesAt(room.getLayout().getTile(item.getX(), item.getY()), item.getBaseItem().getWidth(), item.getBaseItem().getLength(), item.getRotation());
return occupiedTiles.stream().noneMatch(tile -> room.getItemsAt(tile).stream().anyMatch(matchedItem -> matchedItem != item && matchedItem.getZ() >= minZ));
});
2018-07-06 15:30:00 +02:00
}
}
@Override
2019-05-26 20:14:53 +02:00
public String getWiredData() {
2018-07-06 15:30:00 +02:00
this.refresh();
2019-03-18 02:22:00 +01:00
StringBuilder data = new StringBuilder((this.all ? "1" : "0") + ":");
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.items)
2019-03-18 02:22:00 +01:00
data.append(item.getId()).append(";");
2018-07-06 15:30:00 +02:00
2019-03-18 02:22:00 +01:00
return data.toString();
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public void loadWiredData(ResultSet set, Room room) throws SQLException {
2018-07-06 15:30:00 +02:00
this.items.clear();
String[] data = set.getString("wired_data").split(":");
2019-05-26 20:14:53 +02:00
if (data.length >= 1) {
2018-07-06 15:30:00 +02:00
this.all = (data[0].equals("1"));
2019-05-26 20:14:53 +02:00
if (data.length == 2) {
2018-07-06 15:30:00 +02:00
String[] items = data[1].split(";");
2019-05-26 20:14:53 +02:00
for (String s : items) {
HabboItem item = room.getHabboItem(Integer.parseInt(s));
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (item != null)
2018-07-06 15:30:00 +02:00
this.items.add(item);
}
}
}
}
@Override
2019-05-26 20:14:53 +02:00
public void onPickUp() {
2018-07-06 15:30:00 +02:00
this.all = false;
this.items.clear();
}
@Override
2019-05-26 20:14:53 +02:00
public WiredConditionType getType() {
2018-07-06 15:30:00 +02:00
return type;
}
@Override
2019-05-26 20:14:53 +02:00
public void serializeWiredData(ServerMessage message, Room room) {
2018-07-06 15:30:00 +02:00
this.refresh();
message.appendBoolean(false);
message.appendInt(WiredHandler.MAXIMUM_FURNI_SELECTION);
message.appendInt(this.items.size());
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.items)
2018-07-06 15:30:00 +02:00
message.appendInt(item.getId());
message.appendInt(this.getBaseItem().getSpriteId());
message.appendInt(this.getId());
message.appendString("");
message.appendInt(1);
message.appendInt(this.all ? 1 : 0);
message.appendInt(0);
message.appendInt(this.getType().code);
message.appendInt(0);
message.appendInt(0);
}
@Override
2019-05-26 20:14:53 +02:00
public boolean saveData(ClientMessage packet) {
2018-07-06 15:30:00 +02:00
packet.readInt();
this.all = packet.readInt() == 1;
packet.readString();
2020-09-15 19:38:31 +02:00
int count = packet.readInt();
if (count > Emulator.getConfig().getInt("hotel.wired.furni.selection.count")) return false;
this.items.clear();
2018-07-06 15:30:00 +02:00
Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId());
2019-05-26 20:14:53 +02:00
if (room != null) {
for (int i = 0; i < count; i++) {
2018-07-06 15:30:00 +02:00
HabboItem item = room.getHabboItem(packet.readInt());
2019-05-26 20:14:53 +02:00
if (item != null)
2018-07-06 15:30:00 +02:00
this.items.add(item);
}
return true;
}
return false;
}
2019-05-26 20:14:53 +02:00
private void refresh() {
2018-09-28 21:25:00 +02:00
THashSet<HabboItem> items = new THashSet<>();
2018-07-06 15:30:00 +02:00
Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId());
2019-05-26 20:14:53 +02:00
if (room == null) {
2018-07-06 15:30:00 +02:00
items.addAll(this.items);
2019-05-26 20:14:53 +02:00
} else {
for (HabboItem item : this.items) {
2018-07-06 15:30:00 +02:00
if (room.getHabboItem(item.getId()) == null)
items.add(item);
}
}
2019-05-26 20:14:53 +02:00
for (HabboItem item : items) {
2018-07-06 15:30:00 +02:00
this.items.remove(item);
}
}
2019-03-18 02:22:00 +01:00
@Override
2019-05-26 20:14:53 +02:00
public WiredConditionOperator operator() {
// NICE TRY BUT THAT'S NOT HOW IT WORKS. NOTHING IN HABBO IS AN "OR" CONDITION - EVERY CONDITION MUST BE SUCCESS FOR THE STACK TO EXECUTE, BUT LET'S LEAVE IT IMPLEMENTED FOR PLUGINS TO USE.
//return this.all ? WiredConditionOperator.AND : WiredConditionOperator.OR;
return WiredConditionOperator.AND;
2019-03-18 02:22:00 +01:00
}
2018-07-06 15:30:00 +02:00
}