Fixed thread flood in Vending Machines

This commit is contained in:
Dome 2020-07-07 07:16:58 -04:00 committed by Harmonic
parent a14ff42fe6
commit 15e24f3114

View File

@ -6,8 +6,6 @@ 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;
@ -17,6 +15,7 @@ import com.eu.habbo.util.pathfinding.Rotation;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture;
public class InteractionVendingMachine extends InteractionDefault { public class InteractionVendingMachine extends InteractionDefault {
public InteractionVendingMachine(ResultSet set, Item baseItem) throws SQLException { public InteractionVendingMachine(ResultSet set, Item baseItem) throws SQLException {
@ -32,83 +31,85 @@ public class InteractionVendingMachine extends InteractionDefault {
@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);
if (client == null) {
return;
}
if (client != null) { RoomTile tile = this.getRequiredTile(client.getHabbo(), room);
RoomTile tile = this.getRequiredTile(client.getHabbo(), room);
if (tile != null) { if (tile != null &&
if (tile.equals(client.getHabbo().getRoomUnit().getCurrentLocation())) { tile.equals(client.getHabbo().getRoomUnit().getCurrentLocation())) {
if (this.getExtradata().equals("0") || this.getExtradata().length() == 0) { if (!this.getExtradata().equals("0") || this.getExtradata().length() != 0) {
if (!client.getHabbo().getRoomUnit().hasStatus(RoomUnitStatus.SIT) && (!client.getHabbo().getRoomUnit().hasStatus(RoomUnitStatus.MOVE) || tile.equals(client.getHabbo().getRoomUnit().getGoal()))) { return;
room.updateHabbo(client.getHabbo()); }
this.rotateToMachine(client.getHabbo().getRoomUnit());
client.getHabbo().getRoomUnit().removeStatus(RoomUnitStatus.MOVE); if (!client.getHabbo().getRoomUnit().hasStatus(RoomUnitStatus.SIT) && (!client.getHabbo().getRoomUnit().hasStatus(RoomUnitStatus.MOVE) || tile.equals(client.getHabbo().getRoomUnit().getGoal()))) {
room.scheduledComposers.add(new RoomUserStatusComposer(client.getHabbo().getRoomUnit()).compose()); room.updateHabbo(client.getHabbo());
} 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"}); super.onClick(client, room, new Object[]{"TOGGLE_OVERRIDE"});
this.setExtradata("1"); this.setExtradata("1");
room.scheduledComposers.add(new FloorItemUpdateComposer(this).compose()); room.scheduledComposers.add(new FloorItemUpdateComposer(this).compose());
Emulator.getThreading().run(() -> { this.runGiveItemThread(client, room);
Emulator.getThreading().run(this, 1000); } else {
this.giveVendingMachineItem(client.getHabbo(), room); 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 (this.getBaseItem().getEffectM() > 0 && client.getHabbo().getHabboInfo().getGender() == HabboGender.M) if (t != null && t.isWalkable()) {
room.giveEffect(client.getHabbo(), this.getBaseItem().getEffectM(), -1); tile = t;
if (this.getBaseItem().getEffectF() > 0 && client.getHabbo().getHabboInfo().getGender() == HabboGender.F) break;
room.giveEffect(client.getHabbo(), this.getBaseItem().getEffectF(), -1);
}, 500);
} }
} 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());
}
try {
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).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}, 300));
}, 250);
}, null));
} }
} }
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));
}
}
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();
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} }
} }