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
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);
}
}

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.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<RoomTile> getActivatorTiles(Room room) {
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.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");
@ -28,6 +33,65 @@ public class InteractionVendingMachine extends InteractionDefault {
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
public void onClick(GameClient client, Room room, Object[] objects) throws Exception {
super.onClick(client, room, objects);
@ -36,82 +100,50 @@ 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) {
THashSet<RoomTile> activatorTiles = getActivatorTiles(room);
if(activatorTiles.size() == 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());
}
boolean inActivatorSpace = false;
super.onClick(client, room, new Object[]{"TOGGLE_OVERRIDE"});
this.setExtradata("1");
room.scheduledComposers.add(new FloorItemUpdateComposer(this).compose());
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;
}
}
}
RoomTile finalTile = tile;
client.getHabbo().getRoomUnit().setGoalLocation(tile);
Emulator.getThreading().run(new RoomUnitWalkToLocation(client.getHabbo().getRoomUnit(), tile, room, () -> {
this.setExtradata("1");
room.scheduledComposers.add(new FloorItemUpdateComposer(this).compose());
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));
for(RoomTile tile : activatorTiles) {
if(unit.getCurrentLocation().is(tile.x, tile.y)) {
inActivatorSpace = true;
}
}
private void runGiveItemThread(GameClient client, Room room) {
try {
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();
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;
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
if(tileToWalkTo != null) {
List<Runnable> onSuccess = new ArrayList<Runnable>();
List<Runnable> onFail = new ArrayList<Runnable>();
onSuccess.add(() -> {
tryInteract(client, room, unit);
});
unit.setGoalLocation(tileToWalkTo);
Emulator.getThreading().run(new RoomUnitWalkToLocation(unit, tileToWalkTo, room, onSuccess, onFail));
}
}
else {
useVendingMachine(client, room, unit);
}
}
@Override
public void onWalk(RoomUnit roomUnit, Room room, Object[] objects) throws Exception {
}
@Override
public void run() {
@ -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);
}
}