Merge branch '730-vending-machine-bug' into 'dev'

Vending machines fixed

See merge request morningstar/Arcturus-Community!285
This commit is contained in:
Harmonic 2020-09-06 07:36:35 -04:00
commit 7949d835a8
3 changed files with 124 additions and 107 deletions

View File

@ -29,7 +29,7 @@ public class InteractionEffectVendingMachine extends InteractionVendingMachine {
} }
@Override @Override
public void giveVendingMachineItem(Habbo habbo, Room room) { public void giveVendingMachineItem(Room room, RoomUnit unit) {
room.giveEffect(habbo.getRoomUnit(), this.getBaseItem().getRandomVendingItem(), 30); room.giveEffect(unit, this.getBaseItem().getRandomVendingItem(), 30);
} }
} }

View File

@ -4,6 +4,7 @@ import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.rooms.Room; import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.rooms.RoomTile; import com.eu.habbo.habbohotel.rooms.RoomTile;
import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.Habbo;
import gnu.trove.set.hash.THashSet;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -18,7 +19,18 @@ public class InteractionNoSidesVendingMachine extends InteractionVendingMachine
} }
@Override @Override
public RoomTile getRequiredTile(Habbo habbo, Room room) { public THashSet<RoomTile> getActivatorTiles(Room room) {
return habbo.getRoomUnit().getClosestAdjacentTile(this.getX(), this.getY(), true);
THashSet<RoomTile> tiles = new THashSet<RoomTile>();
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;
} }
} }

View File

@ -6,18 +6,23 @@ import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.rooms.*; import com.eu.habbo.habbohotel.rooms.*;
import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.users.HabboGender; 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.items.FloorItemUpdateComposer;
import com.eu.habbo.messages.outgoing.rooms.users.RoomUserStatusComposer; import com.eu.habbo.messages.outgoing.rooms.users.RoomUserStatusComposer;
import com.eu.habbo.threading.runnables.RoomUnitGiveHanditem; import com.eu.habbo.threading.runnables.RoomUnitGiveHanditem;
import com.eu.habbo.threading.runnables.RoomUnitWalkToLocation; import com.eu.habbo.threading.runnables.RoomUnitWalkToLocation;
import com.eu.habbo.util.pathfinding.Rotation; import com.eu.habbo.util.pathfinding.Rotation;
import gnu.trove.set.hash.THashSet;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
public class InteractionVendingMachine extends InteractionDefault { public class InteractionVendingMachine extends HabboItem {
public InteractionVendingMachine(ResultSet set, Item baseItem) throws SQLException { public InteractionVendingMachine(ResultSet set, Item baseItem) throws SQLException {
super(set, baseItem); super(set, baseItem);
this.setExtradata("0"); this.setExtradata("0");
@ -28,6 +33,65 @@ public class InteractionVendingMachine extends InteractionDefault {
this.setExtradata("0"); this.setExtradata("0");
} }
public THashSet<RoomTile> getActivatorTiles(Room room) {
THashSet<RoomTile> tiles = new THashSet<RoomTile>();
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<RoomTile> 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 @Override
public void onClick(GameClient client, Room room, Object[] objects) throws Exception { public void onClick(GameClient client, Room room, Object[] objects) throws Exception {
super.onClick(client, room, objects); super.onClick(client, room, objects);
@ -36,81 +100,49 @@ public class InteractionVendingMachine extends InteractionDefault {
return; return;
} }
RoomTile tile = this.getRequiredTile(client.getHabbo(), room); RoomUnit unit = client.getHabbo().getRoomUnit();
if (tile != null && THashSet<RoomTile> activatorTiles = getActivatorTiles(room);
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()))) { if(activatorTiles.size() == 0)
room.updateHabbo(client.getHabbo()); return;
this.rotateToMachine(client.getHabbo().getRoomUnit());
client.getHabbo().getRoomUnit().removeStatus(RoomUnitStatus.MOVE);
room.scheduledComposers.add(new RoomUserStatusComposer(client.getHabbo().getRoomUnit()).compose());
}
super.onClick(client, room, new Object[]{"TOGGLE_OVERRIDE"}); boolean inActivatorSpace = false;
this.setExtradata("1"); for(RoomTile tile : activatorTiles) {
room.scheduledComposers.add(new FloorItemUpdateComposer(this).compose()); if(unit.getCurrentLocation().is(tile.x, tile.y)) {
inActivatorSpace = true;
}
}
this.runGiveItemThread(client, room); if(!inActivatorSpace) {
} else { RoomTile tileToWalkTo = null;
if (!tile.isWalkable() && tile.state != RoomTileState.SIT && tile.state != RoomTileState.LAY) { for(RoomTile tile : activatorTiles) {
for (RoomTile t : room.getLayout().getTilesAround(room.getLayout().getTile(this.getX(), this.getY()))) { if(room.getLayout().tileWalkable(tile.x, tile.y) && (tileToWalkTo == null || tileToWalkTo.distance(unit.getCurrentLocation()) > tile.distance(unit.getCurrentLocation()))) {
if (t != null && t.isWalkable()) { tileToWalkTo = tile;
tile = t;
break;
}
} }
} }
RoomTile finalTile = tile; if(tileToWalkTo != null) {
client.getHabbo().getRoomUnit().setGoalLocation(tile); List<Runnable> onSuccess = new ArrayList<Runnable>();
List<Runnable> onFail = new ArrayList<Runnable>();
Emulator.getThreading().run(new RoomUnitWalkToLocation(client.getHabbo().getRoomUnit(), tile, room, () -> { onSuccess.add(() -> {
this.setExtradata("1"); tryInteract(client, room, unit);
room.scheduledComposers.add(new FloorItemUpdateComposer(this).compose()); });
try { unit.setGoalLocation(tileToWalkTo);
super.onClick(client, room, new Object[]{"TOGGLE_OVERRIDE"}); Emulator.getThreading().run(new RoomUnitWalkToLocation(unit, tileToWalkTo, room, onSuccess, onFail));
} catch (Exception e) { }
e.printStackTrace(); }
} else {
useVendingMachine(client, room, unit);
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));
} }
} }
private void runGiveItemThread(GameClient client, Room room) { @Override
try { public void onWalk(RoomUnit roomUnit, Room room, Object[] objects) throws Exception {
ScheduledFuture thread = Emulator.getThreading().run(() -> {
Emulator.getThreading().run(this, 1000);
this.giveVendingMachineItem(client.getHabbo(), room);
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 @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 @Override
public boolean isUsable() { public boolean isUsable() {
return true; return true;
} }
private void rotateToMachine(RoomUnit unit) { private void rotateToMachine(Room room, RoomUnit unit) {
if (unit.getCurrentLocation().getState() != RoomTileState.OPEN) {
// if sitting on a chair or laying on a bed, skip rotating altogether
return;
}
RoomUserRotation rotation = RoomUserRotation.values()[Rotation.Calculate(unit.getX(), unit.getY(), this.getX(), this.getY())]; RoomUserRotation rotation = RoomUserRotation.values()[Rotation.Calculate(unit.getX(), unit.getY(), this.getX(), this.getY())];
boolean onlyHead = false;
switch (unit.getBodyRotation()) { if(Math.abs(unit.getBodyRotation().getValue() - rotation.getValue()) > 1) {
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 {
unit.setRotation(rotation); 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);
}
} }