diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionEffectVendingMachine.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionEffectVendingMachine.java index eccba568..c0d42279 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionEffectVendingMachine.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionEffectVendingMachine.java @@ -29,7 +29,7 @@ public class InteractionEffectVendingMachine extends InteractionVendingMachine { } @Override - public void giveVendingMachineItem(Habbo habbo, Room room) { - room.giveEffect(habbo.getRoomUnit(), this.getBaseItem().getRandomVendingItem(), 30); + public void giveVendingMachineItem(Room room, RoomUnit unit) { + room.giveEffect(unit, this.getBaseItem().getRandomVendingItem(), 30); } } \ No newline at end of file diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionNoSidesVendingMachine.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionNoSidesVendingMachine.java index 367d500e..f2463700 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionNoSidesVendingMachine.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionNoSidesVendingMachine.java @@ -4,6 +4,7 @@ import com.eu.habbo.habbohotel.items.Item; import com.eu.habbo.habbohotel.rooms.Room; import com.eu.habbo.habbohotel.rooms.RoomTile; import com.eu.habbo.habbohotel.users.Habbo; +import gnu.trove.set.hash.THashSet; import java.sql.ResultSet; import java.sql.SQLException; @@ -18,7 +19,18 @@ public class InteractionNoSidesVendingMachine extends InteractionVendingMachine } @Override - public RoomTile getRequiredTile(Habbo habbo, Room room) { - return habbo.getRoomUnit().getClosestAdjacentTile(this.getX(), this.getY(), true); + public THashSet getActivatorTiles(Room room) { + + THashSet tiles = new THashSet(); + for(int x = -1; x <= 1; x++) { + for(int y = -1; y <= 1; y++) { + RoomTile tile = room.getLayout().getTile((short)(this.getX() + x), (short)(this.getY() + y)); + if(tile != null) { + tiles.add(tile); + } + } + } + + return tiles; } } diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionVendingMachine.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionVendingMachine.java index 04511206..be0f5cba 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionVendingMachine.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionVendingMachine.java @@ -6,18 +6,23 @@ import com.eu.habbo.habbohotel.items.Item; import com.eu.habbo.habbohotel.rooms.*; import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.HabboGender; +import com.eu.habbo.habbohotel.users.HabboItem; +import com.eu.habbo.messages.ServerMessage; import com.eu.habbo.messages.outgoing.rooms.items.FloorItemUpdateComposer; import com.eu.habbo.messages.outgoing.rooms.users.RoomUserStatusComposer; import com.eu.habbo.threading.runnables.RoomUnitGiveHanditem; import com.eu.habbo.threading.runnables.RoomUnitWalkToLocation; import com.eu.habbo.util.pathfinding.Rotation; +import gnu.trove.set.hash.THashSet; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ScheduledFuture; -public class InteractionVendingMachine extends InteractionDefault { +public class InteractionVendingMachine extends HabboItem { public InteractionVendingMachine(ResultSet set, Item baseItem) throws SQLException { super(set, baseItem); this.setExtradata("0"); @@ -27,6 +32,65 @@ public class InteractionVendingMachine extends InteractionDefault { super(id, userId, item, extradata, limitedStack, limitedSells); this.setExtradata("0"); } + + public THashSet getActivatorTiles(Room room) { + THashSet tiles = new THashSet(); + tiles.add(getSquareInFront(room.getLayout(), this)); + tiles.add(room.getLayout().getTile(this.getX(), this.getY())); + return tiles; + } + + @Override + public void serializeExtradata(ServerMessage serverMessage) { + serverMessage.appendInt((this.isLimited() ? 256 : 0)); + serverMessage.appendString(this.getExtradata()); + + super.serializeExtradata(serverMessage); + } + + private void tryInteract(GameClient client, Room room, RoomUnit unit) { + THashSet activatorTiles = getActivatorTiles(room); + + if(activatorTiles.size() == 0) + return; + + boolean inActivatorSpace = false; + + for(RoomTile tile : activatorTiles) { + if(unit.getCurrentLocation().is(unit.getX(), unit.getY())) { + inActivatorSpace = true; + } + } + + if(inActivatorSpace) { + useVendingMachine(client, room, unit); + } + } + + private void useVendingMachine(GameClient client, Room room, RoomUnit unit) { + this.setExtradata("1"); + room.updateItem(this); + + if(!unit.isWalking()) { + this.rotateToMachine(room, unit); + } + + Emulator.getThreading().run(() -> { + + giveVendingMachineItem(room, unit); + + if (this.getBaseItem().getEffectM() > 0 && client.getHabbo().getHabboInfo().getGender() == HabboGender.M) + room.giveEffect(client.getHabbo(), this.getBaseItem().getEffectM(), -1); + if (this.getBaseItem().getEffectF() > 0 && client.getHabbo().getHabboInfo().getGender() == HabboGender.F) + room.giveEffect(client.getHabbo(), this.getBaseItem().getEffectF(), -1); + + Emulator.getThreading().run(this, 500); + }, 1500); + } + + public void giveVendingMachineItem(Room room, RoomUnit unit) { + Emulator.getThreading().run(new RoomUnitGiveHanditem(unit, room, this.getBaseItem().getRandomVendingItem())); + } @Override public void onClick(GameClient client, Room room, Object[] objects) throws Exception { @@ -36,81 +100,49 @@ public class InteractionVendingMachine extends InteractionDefault { return; } - RoomTile tile = this.getRequiredTile(client.getHabbo(), room); + RoomUnit unit = client.getHabbo().getRoomUnit(); - if (tile != null && - tile.equals(client.getHabbo().getRoomUnit().getCurrentLocation())) { - if (!this.getExtradata().equals("0") || this.getExtradata().length() != 0) { - return; - } - - if (!client.getHabbo().getRoomUnit().hasStatus(RoomUnitStatus.SIT) && (!client.getHabbo().getRoomUnit().hasStatus(RoomUnitStatus.MOVE) || tile.equals(client.getHabbo().getRoomUnit().getGoal()))) { - room.updateHabbo(client.getHabbo()); - this.rotateToMachine(client.getHabbo().getRoomUnit()); - client.getHabbo().getRoomUnit().removeStatus(RoomUnitStatus.MOVE); - room.scheduledComposers.add(new RoomUserStatusComposer(client.getHabbo().getRoomUnit()).compose()); - } + THashSet activatorTiles = getActivatorTiles(room); - super.onClick(client, room, new Object[]{"TOGGLE_OVERRIDE"}); + if(activatorTiles.size() == 0) + return; - this.setExtradata("1"); - room.scheduledComposers.add(new FloorItemUpdateComposer(this).compose()); + boolean inActivatorSpace = false; - this.runGiveItemThread(client, room); - } else { - if (!tile.isWalkable() && tile.state != RoomTileState.SIT && tile.state != RoomTileState.LAY) { - for (RoomTile t : room.getLayout().getTilesAround(room.getLayout().getTile(this.getX(), this.getY()))) { - if (t != null && t.isWalkable()) { - tile = t; - break; - } + for(RoomTile tile : activatorTiles) { + if(unit.getCurrentLocation().is(tile.x, tile.y)) { + inActivatorSpace = true; + } + } + + if(!inActivatorSpace) { + RoomTile tileToWalkTo = null; + for(RoomTile tile : activatorTiles) { + if(room.getLayout().tileWalkable(tile.x, tile.y) && (tileToWalkTo == null || tileToWalkTo.distance(unit.getCurrentLocation()) > tile.distance(unit.getCurrentLocation()))) { + tileToWalkTo = tile; } } - RoomTile finalTile = tile; - client.getHabbo().getRoomUnit().setGoalLocation(tile); + if(tileToWalkTo != null) { + List onSuccess = new ArrayList(); + List onFail = new ArrayList(); - Emulator.getThreading().run(new RoomUnitWalkToLocation(client.getHabbo().getRoomUnit(), tile, room, () -> { - this.setExtradata("1"); - room.scheduledComposers.add(new FloorItemUpdateComposer(this).compose()); + onSuccess.add(() -> { + tryInteract(client, room, unit); + }); - try { - super.onClick(client, room, new Object[]{"TOGGLE_OVERRIDE"}); - } catch (Exception e) { - e.printStackTrace(); - } - - Emulator.getThreading().run(() -> client.getHabbo().getRoomUnit().setMoveBlockingTask(Emulator.getThreading().run(() -> { - if (client.getHabbo().getRoomUnit().getCurrentLocation().equals(finalTile)) { - this.rotateToMachine(client.getHabbo().getRoomUnit()); - room.sendComposer(new RoomUserStatusComposer(client.getHabbo().getRoomUnit()).compose()); - } - - this.runGiveItemThread(client, room); - }, 300)), 250); - }, null)); + unit.setGoalLocation(tileToWalkTo); + Emulator.getThreading().run(new RoomUnitWalkToLocation(unit, tileToWalkTo, room, onSuccess, onFail)); + } + } + else { + useVendingMachine(client, room, unit); } } - private void runGiveItemThread(GameClient client, Room room) { - try { - ScheduledFuture thread = Emulator.getThreading().run(() -> { - Emulator.getThreading().run(this, 1000); - this.giveVendingMachineItem(client.getHabbo(), room); + @Override + public void onWalk(RoomUnit roomUnit, Room room, Object[] objects) throws Exception { - if (this.getBaseItem().getEffectM() > 0 && client.getHabbo().getHabboInfo().getGender() == HabboGender.M) - room.giveEffect(client.getHabbo(), this.getBaseItem().getEffectM(), -1); - if (this.getBaseItem().getEffectF() > 0 && client.getHabbo().getHabboInfo().getGender() == HabboGender.F) - room.giveEffect(client.getHabbo(), this.getBaseItem().getEffectF(), -1); - }, 500); - - if (thread.isDone()) { - thread.get(); - } - - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); - } } @Override @@ -125,54 +157,27 @@ public class InteractionVendingMachine extends InteractionDefault { } } + @Override + public boolean canWalkOn(RoomUnit roomUnit, Room room, Object[] objects) { + return true; + } + + @Override + public boolean isWalkable() { + return this.getBaseItem().allowWalk(); + } + @Override public boolean isUsable() { return true; } - private void rotateToMachine(RoomUnit unit) { - if (unit.getCurrentLocation().getState() != RoomTileState.OPEN) { - // if sitting on a chair or laying on a bed, skip rotating altogether - return; - } - + private void rotateToMachine(Room room, RoomUnit unit) { RoomUserRotation rotation = RoomUserRotation.values()[Rotation.Calculate(unit.getX(), unit.getY(), this.getX(), this.getY())]; - boolean onlyHead = false; - switch (unit.getBodyRotation()) { - case NORTH_EAST: - if (rotation.equals(RoomUserRotation.NORTH) || rotation.equals(RoomUserRotation.EAST)) - onlyHead = true; - break; - - case NORTH_WEST: - if (rotation.equals(RoomUserRotation.NORTH) || rotation.equals(RoomUserRotation.WEST)) - onlyHead = true; - break; - - case SOUTH_EAST: - if (rotation.equals(RoomUserRotation.SOUTH) || rotation.equals(RoomUserRotation.EAST)) - onlyHead = true; - break; - - case SOUTH_WEST: - if (rotation.equals(RoomUserRotation.SOUTH) || rotation.equals(RoomUserRotation.WEST)) - onlyHead = true; - break; - } - - if (onlyHead) { - unit.setHeadRotation(rotation); - } else { + if(Math.abs(unit.getBodyRotation().getValue() - rotation.getValue()) > 1) { unit.setRotation(rotation); + unit.statusUpdate(true); } } - - public void giveVendingMachineItem(Habbo habbo, Room room) { - Emulator.getThreading().run(new RoomUnitGiveHanditem(habbo.getRoomUnit(), room, this.getBaseItem().getRandomVendingItem())); - } - - public RoomTile getRequiredTile(Habbo habbo, Room room) { - return getSquareInFront(room.getLayout(), this); - } }