Skip to content

Commit

Permalink
First attempt to fix the roads task
Browse files Browse the repository at this point in the history
  • Loading branch information
remmintan committed Mar 16, 2024
1 parent d2a95e1 commit 17a0271
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
public interface ITasksCreator extends IServerManager {

ITask createCutTreesTask(UUID uuid, List<BlockPos> treeRoots, List<BlockPos> positions);
default ITask createRoadsTask(UUID digUuid, TaskType type, UUID placeUuid, List<BlockPos> blocks, Item itemInHand) {
return createRoadsTask(digUuid, type, placeUuid, blocks, itemInHand, () -> {});
}
ITask createRoadsTask(UUID digUuid, TaskType type, UUID placeUuid, List<BlockPos> blocks, Item itemInHand, Runnable onComplete);

ITask createRoadsTask(UUID digUuid, TaskType type, UUID placeUuid, List<BlockPos> blocks, Item itemInHand);

ITask createSelectionTask(UUID id, TaskType taskType, BlockPos start, BlockPos end, ServerSelectionType selectionType, HitResult hitResult, List<BlockPos> positions, ServerPlayerEntity player);

Expand Down
14 changes: 9 additions & 5 deletions src/main/java/org/minefortress/tasks/RoadsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ public class RoadsTask implements ITask {
private final int totalParts;
private int finishedParts = 0;

private final Runnable onComplete;
private final List<Runnable> taskFinishListeners = new ArrayList<>();

public RoadsTask(UUID id, TaskType taskType, List<BlockPos> blocks, Item item, Runnable onComplete) {
public RoadsTask(UUID id, TaskType taskType, List<BlockPos> blocks, Item item) {
this.id = id;
this.taskType = taskType;
this.onComplete = onComplete;
this.blocks = blocks;
this.item = item;
this.totalParts = prepareParts();
Expand Down Expand Up @@ -120,14 +119,19 @@ public void returnPart(Pair<BlockPos, BlockPos> partStartAndEnd) {
public void finishPart(ITaskPart part, IWorkerPawn colonist) {
final ServerWorld world = colonist.getServerWorld();
finishedParts++;
if(taskParts.isEmpty() && finishedParts == totalParts){
if(taskParts.isEmpty() && totalParts <= finishedParts){
world.getPlayers().stream().findAny().ifPresent(player -> {
FortressServerNetworkHelper.send(player, FortressChannelNames.FINISH_TASK, new ClientboundTaskExecutedPacket(this.getId()));
});
onComplete.run();
taskFinishListeners.forEach(Runnable::run);
}
}

@Override
public void addFinishListener(Runnable listener) {
taskFinishListeners.add(listener);
}

@Override
public List<TaskInformationDto> toTaskInformationDto() {
if(taskType == TaskType.BUILD){
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/minefortress/tasks/TasksCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public ITask createCutTreesTask(UUID uuid, List<BlockPos> treeRoots, List<BlockP
}

@Override
public ITask createRoadsTask(UUID digUuid, TaskType type, UUID placeUuid, List<BlockPos> blocks, Item itemInHand, Runnable onComplete) {
return new RoadsTask(placeUuid, type, blocks, itemInHand, onComplete);
public ITask createRoadsTask(UUID digUuid, TaskType type, UUID placeUuid, List<BlockPos> blocks, Item itemInHand) {
return new RoadsTask(placeUuid, type, blocks, itemInHand);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public void handle(MinecraftServer server, ServerPlayerEntity player) {
if(manager.isSurvival())
resourceManager.reserveItems(placeUuid, Collections.singletonList(resourceManager.createItemInfo(item, blocks.size())));

final var digTask = tasksCreator.createRoadsTask(digUuid, TaskType.REMOVE, placeUuid, blocks, item, onDigComplete);
final var digTask = tasksCreator.createRoadsTask(digUuid, TaskType.REMOVE, placeUuid, blocks, item);
digTask.addFinishListener(onDigComplete);
taskManager.addTask(digTask, provider, manager, selectedPawns, player);
}
}

0 comments on commit 17a0271

Please sign in to comment.