From 909047010062389a0fae67f46ea238bc64d46590 Mon Sep 17 00:00:00 2001 From: Remco Date: Sun, 3 Jan 2021 17:33:27 +0100 Subject: [PATCH] Fireworks working like Habbo --- .../interactions/InteractionFireworks.java | 132 ++++++++++++++---- .../com/eu/habbo/habbohotel/rooms/Room.java | 8 ++ 2 files changed, 115 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionFireworks.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionFireworks.java index 645c0d4e..bc8385c5 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionFireworks.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionFireworks.java @@ -5,14 +5,28 @@ import com.eu.habbo.habbohotel.achievements.AchievementManager; import com.eu.habbo.habbohotel.gameclients.GameClient; import com.eu.habbo.habbohotel.items.Item; import com.eu.habbo.habbohotel.rooms.Room; +import com.eu.habbo.habbohotel.rooms.RoomLayout; +import com.eu.habbo.habbohotel.rooms.RoomTile; import com.eu.habbo.habbohotel.rooms.RoomUnit; -import com.eu.habbo.habbohotel.users.HabboItem; -import com.eu.habbo.messages.ServerMessage; +import com.eu.habbo.habbohotel.users.Habbo; +import com.eu.habbo.habbohotel.wired.WiredEffectType; +import com.eu.habbo.threading.runnables.RoomUnitWalkToLocation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class InteractionFireworks extends InteractionDefault { + + private static final Logger LOGGER = LoggerFactory.getLogger(InteractionFireworks.class); + + private static final String STATE_EMPTY = "0"; // Not used since the removal of pixels + private static final String STATE_CHARGED = "1"; + private static final String STATE_EXPLOSION = "2"; -public class InteractionFireworks extends HabboItem { public InteractionFireworks(ResultSet set, Item baseItem) throws SQLException { super(set, baseItem); } @@ -26,35 +40,103 @@ public class InteractionFireworks extends HabboItem { return false; } - @Override - public boolean isWalkable() { - return this.getBaseItem().allowWalk(); - } - - @Override - public void onWalk(RoomUnit roomUnit, Room room, Object[] objects) throws Exception { - - } - - @Override - public void serializeExtradata(ServerMessage serverMessage) { - serverMessage.appendInt((this.isLimited() ? 256 : 0)); - serverMessage.appendString(this.getExtradata()); - super.serializeExtradata(serverMessage); //Design flaw ;( - } - + /** + * Checked in Habbo on 2021-01-03 + * - Fireworks should be charged to be able to detonate them + * - Habbos with Rights can detonate fireworks from anywhere in a room + * - Habbos without rights have to walk to an adjecent tile to be able to detonate (see Iteraction Switch) + * - Wired can always detonate fireworks + */ @Override public void onClick(GameClient client, Room room, Object[] objects) throws Exception { - super.onClick(client, room, objects); + if (room == null) + return; - if (client != null && this.getExtradata().equalsIgnoreCase("2")) //2 explodes I think (0 = empty, 1 = charged, 2 = effect) - { - AchievementManager.progressAchievement(client.getHabbo(), Emulator.getGameEnvironment().getAchievementManager().getAchievement("FireworksCharger")); + // Wireds can always detonate fireworks if charged + if (objects.length >= 2 && objects[1] instanceof WiredEffectType && objects[1] == WiredEffectType.TOGGLE_STATE) { + if (this.getExtradata().equalsIgnoreCase(STATE_CHARGED)) { + super.onClick(client, room, objects); + + if (this.getExtradata().equalsIgnoreCase(STATE_EXPLOSION)) { + this.reCharge(room); + } + } + + return; + } + + if (client == null) + return; + + // Habbos without rights have to walk to an adjecent tile to be able to detonate the fireworks + if (!this.canToggle(client.getHabbo(), room)) { + RoomTile closestTile = null; + for (RoomTile tile : room.getLayout().getTilesAround(room.getLayout().getTile(this.getX(), this.getY()))) { + if (tile.isWalkable() && (closestTile == null || closestTile.distance(client.getHabbo().getRoomUnit().getCurrentLocation()) > tile.distance(client.getHabbo().getRoomUnit().getCurrentLocation()))) { + closestTile = tile; + } + } + + if (closestTile != null && !closestTile.equals(client.getHabbo().getRoomUnit().getCurrentLocation())) { + List onSuccess = new ArrayList<>(); + onSuccess.add(() -> { + try { + this.onClick(client, room, objects); + } catch (Exception e) { + e.printStackTrace(); + } + }); + + client.getHabbo().getRoomUnit().setGoalLocation(closestTile); + Emulator.getThreading().run(new RoomUnitWalkToLocation(client.getHabbo().getRoomUnit(), closestTile, room, onSuccess, new ArrayList<>())); + } + } + + if (this.getExtradata().equalsIgnoreCase(STATE_CHARGED)) { + super.onClick(client, room, objects); + + if (this.getExtradata().equalsIgnoreCase(STATE_EXPLOSION)) + { + this.reCharge(room); + AchievementManager.progressAchievement(client.getHabbo(), Emulator.getGameEnvironment().getAchievementManager().getAchievement("FireworksCharger")); + } } } @Override public boolean allowWiredResetState() { - return true; + return false; + } + + @Override + public void onPlace(Room room) { + super.onPlace(room); + this.setExtradata(STATE_CHARGED); + } + + @Override + public boolean canToggle(Habbo habbo, Room room) { + return room.hasRights(habbo) || RoomLayout.tilesAdjecent( + room.getLayout().getTile(this.getX(), this.getY()), + habbo.getRoomUnit().getCurrentLocation() + ); + } + + private void reCharge(Room room) { + // Default = 5000, Nuclear Firework should have 10000 in its custom params according to Habbo + int explodeDuration = 5000; + if (!this.getBaseItem().getCustomParams().isEmpty()) { + try { + explodeDuration = Integer.parseInt(this.getBaseItem().getCustomParams()); + } catch (NumberFormatException e) { + LOGGER.error("Incorrect customparams (" + this.getBaseItem().getCustomParams() + ") for base item ID (" + this.getBaseItem().getId() + ") of type (" + this.getBaseItem().getName() + ")"); + } + } + + Emulator.getThreading().run(() -> { + this.setExtradata(STATE_CHARGED); + this.needsUpdate(true); + room.updateItemState(this); + }, explodeDuration); } } diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java index d0704c82..7a955925 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java @@ -383,6 +383,11 @@ public class Room implements Comparable, ISerialize, Runnable { this.updateItem(item); } } + + for (HabboItem item : this.roomSpecialTypes.getItemsOfType(InteractionFireworks.class)) { + item.setExtradata("1"); + this.updateItem(item); + } } Emulator.getPluginManager().fireEvent(new RoomLoadedEvent(this)); @@ -2419,7 +2424,10 @@ public class Room implements Comparable, ISerialize, Runnable { this.roomSpecialTypes.addUndefined(item); } else if (item instanceof InteractionSnowboardSlope) { this.roomSpecialTypes.addUndefined(item); + } else if (item instanceof InteractionFireworks) { + this.roomSpecialTypes.addUndefined(item); } + } }