Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectJoinTeam.java

172 lines
5.8 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.items.interactions.wired.effects;
import com.eu.habbo.Emulator;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.games.Game;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.games.GameTeamColors;
import com.eu.habbo.habbohotel.games.wired.WiredGame;
import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredEffect;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredTrigger;
2022-11-16 16:56:06 +01:00
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
2018-07-06 15:30:00 +02:00
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.WiredEffectType;
2020-11-16 12:12:24 +01:00
import com.eu.habbo.habbohotel.wired.WiredHandler;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.messages.ClientMessage;
import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.messages.incoming.wired.WiredSaveException;
2018-07-06 15:30:00 +02:00
import gnu.trove.procedure.TObjectProcedure;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
2019-05-26 20:14:53 +02:00
public class WiredEffectJoinTeam extends InteractionWiredEffect {
2018-07-06 15:30:00 +02:00
public static final WiredEffectType type = WiredEffectType.JOIN_TEAM;
private GameTeamColors teamColor = GameTeamColors.RED;
2019-05-26 20:14:53 +02:00
public WiredEffectJoinTeam(ResultSet set, Item baseItem) throws SQLException {
2018-07-06 15:30:00 +02:00
super(set, baseItem);
}
2019-05-26 20:14:53 +02:00
public WiredEffectJoinTeam(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);
}
@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
Habbo habbo = room.getHabbo(roomUnit);
2019-05-26 20:14:53 +02:00
if (habbo != null) {
WiredGame game = (WiredGame) room.getGameOrCreate(WiredGame.class);
2018-07-06 15:30:00 +02:00
if (habbo.getHabboInfo().getGamePlayer() != null && habbo.getHabboInfo().getCurrentGame() != null && (habbo.getHabboInfo().getCurrentGame() != WiredGame.class || (habbo.getHabboInfo().getCurrentGame() == WiredGame.class && habbo.getHabboInfo().getGamePlayer().getTeamColor() != this.teamColor))) {
// remove from current game
Game currentGame = room.getGame(habbo.getHabboInfo().getCurrentGame());
currentGame.removeHabbo(habbo);
}
2018-07-06 15:30:00 +02:00
if(habbo.getHabboInfo().getGamePlayer() == null) {
game.addHabbo(habbo, this.teamColor);
2018-07-06 15:30:00 +02:00
}
return true;
2018-07-06 15:30:00 +02:00
}
return false;
}
@Override
2019-05-26 20:14:53 +02:00
public String getWiredData() {
2020-11-16 12:12:24 +01:00
return WiredHandler.getGsonBuilder().create().toJson(new JsonData(this.teamColor, this.getDelay()));
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 {
2020-11-16 12:12:24 +01:00
String wiredData = set.getString("wired_data");
2018-07-06 15:30:00 +02:00
2020-11-16 12:12:24 +01:00
if(wiredData.startsWith("{")) {
JsonData data = WiredHandler.getGsonBuilder().create().fromJson(wiredData, JsonData.class);
this.setDelay(data.delay);
this.teamColor = data.team;
}
else {
String[] data = set.getString("wired_data").split("\t");
if (data.length >= 1) {
this.setDelay(Integer.valueOf(data[0]));
2018-07-06 15:30:00 +02:00
2020-11-16 12:12:24 +01:00
if (data.length >= 2) {
this.teamColor = GameTeamColors.values()[Integer.valueOf(data[1])];
}
2018-07-06 15:30:00 +02:00
}
2020-11-16 12:12:24 +01:00
this.needsUpdate(true);
2018-07-06 15:30:00 +02:00
}
}
@Override
2019-05-26 20:14:53 +02:00
public void onPickUp() {
2018-07-06 15:30:00 +02:00
this.teamColor = GameTeamColors.RED;
this.setDelay(0);
}
@Override
2019-05-26 20:14:53 +02:00
public WiredEffectType 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
message.appendBoolean(false);
message.appendInt(5);
message.appendInt(0);
message.appendInt(this.getBaseItem().getSpriteId());
message.appendInt(this.getId());
message.appendString("");
message.appendInt(1);
2019-06-10 18:06:22 +02:00
message.appendInt(this.teamColor.type);
2018-07-06 15:30:00 +02:00
message.appendInt(0);
message.appendInt(this.getType().code);
message.appendInt(this.getDelay());
2019-05-26 20:14:53 +02:00
if (this.requiresTriggeringUser()) {
2018-07-06 15:30:00 +02:00
List<Integer> invalidTriggers = new ArrayList<>();
2019-05-26 20:14:53 +02:00
room.getRoomSpecialTypes().getTriggers(this.getX(), this.getY()).forEach(new TObjectProcedure<InteractionWiredTrigger>() {
2018-07-06 15:30:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public boolean execute(InteractionWiredTrigger object) {
if (!object.isTriggeredByRoomUnit()) {
2018-07-06 15:30:00 +02:00
invalidTriggers.add(object.getBaseItem().getSpriteId());
}
return true;
}
});
message.appendInt(invalidTriggers.size());
2019-05-26 20:14:53 +02:00
for (Integer i : invalidTriggers) {
2018-07-06 15:30:00 +02:00
message.appendInt(i);
}
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
message.appendInt(0);
}
}
@Override
2022-11-16 16:56:06 +01:00
public boolean saveData(WiredSettings settings, GameClient gameClient) throws WiredSaveException {
if(settings.getIntParams().length < 1) throw new WiredSaveException("invalid data");
int team = settings.getIntParams()[0];
if(team < 1 || team > 4)
throw new WiredSaveException("Team is invalid");
2022-11-16 16:56:06 +01:00
int delay = settings.getDelay();
if(delay > Emulator.getConfig().getInt("hotel.wired.max_delay", 20))
throw new WiredSaveException("Delay too long");
this.teamColor = GameTeamColors.values()[team];
this.setDelay(delay);
2018-07-06 15:30:00 +02:00
return true;
}
@Override
2019-05-26 20:14:53 +02:00
public boolean requiresTriggeringUser() {
2018-07-06 15:30:00 +02:00
return true;
}
2020-11-16 12:12:24 +01:00
static class JsonData {
GameTeamColors team;
int delay;
public JsonData(GameTeamColors team, int delay) {
this.team = team;
this.delay = delay;
}
}
2018-07-06 15:30:00 +02:00
}