Skip to content

Commit

Permalink
Add check to all tasks that contains info that task was fully finished
Browse files Browse the repository at this point in the history
  • Loading branch information
remmintan committed Mar 20, 2024
1 parent 24b0f62 commit 5194988
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ default void prepareTask() {}
default void addFinishListener(Runnable listener) {}
List<TaskInformationDto> toTaskInformationDto();

boolean taskFullyFinished();

}
9 changes: 9 additions & 0 deletions src/main/java/org/minefortress/tasks/AbstractTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,21 @@ public void prepareTask() {
@Override
public void finishPart(ITaskPart part, IWorkerPawn colonsit) {
completedParts++;
if(completedParts > totalParts) {
throw new IllegalStateException("Completed parts cannot be greater than total parts");
}

if(parts.isEmpty() && totalParts <= completedParts) {
colonsit.getMasterPlayer().ifPresent(this::sendFinishTaskNotificationToPlayer);
taskFinishListeners.forEach(Runnable::run);
}
}

@Override
public boolean taskFullyFinished() {
return totalParts <= completedParts;
}

@Override
public List<TaskInformationDto> toTaskInformationDto() {
final var blocks = new ArrayList<BlockPos>();
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/org/minefortress/tasks/CutTreesTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,26 @@ public void returnPart(Pair<BlockPos, BlockPos> partStartAndEnd) {
@Override
public void finishPart(ITaskPart part, IWorkerPawn colonist) {
final ServerWorld world = colonist.getServerWorld();
if(part != null && part.getStartAndEnd() != null && part.getStartAndEnd().getFirst() != null) {
final BlockPos root = part.getStartAndEnd().getFirst();
final Optional<TreeBlocks> treeOpt = TreeHelper.getTreeBlocks(root.up(), world);
if(treeOpt.isPresent()) {
final TreeBlocks tree = treeOpt.get();
TreeHelper.removeTheRestOfATree(colonist, tree, world);
}
final BlockPos root = part.getStartAndEnd().getFirst();
final Optional<TreeBlocks> treeOpt = TreeHelper.getTreeBlocks(root.up(), world);
if(treeOpt.isPresent()) {
final TreeBlocks tree = treeOpt.get();
TreeHelper.removeTheRestOfATree(colonist, tree, world);
}

removedRoots++;
if(treeRoots.isEmpty() && removedRoots <= totalRootCount) {
world.getPlayers().stream().findAny().ifPresent(player -> {
FortressServerNetworkHelper.send(player, FortressChannelNames.FINISH_TASK, new ClientboundTaskExecutedPacket(this.getId()));
});
if(removedRoots > totalRootCount) {
throw new IllegalStateException("Removed more roots than total roots");
}

if(treeRoots.isEmpty() && removedRoots == totalRootCount) {
world.getPlayers().stream().findAny().ifPresent(player -> FortressServerNetworkHelper.send(player, FortressChannelNames.FINISH_TASK, new ClientboundTaskExecutedPacket(this.getId())));
}
}

@Override
public boolean taskFullyFinished() {
return removedRoots == totalRootCount;
}

@Override
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/minefortress/tasks/RoadsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ public void returnPart(Pair<BlockPos, BlockPos> partStartAndEnd) {
public void finishPart(ITaskPart part, IWorkerPawn colonist) {
final ServerWorld world = colonist.getServerWorld();
finishedParts++;
if(taskParts.isEmpty() && totalParts <= finishedParts){
world.getPlayers().stream().findAny().ifPresent(player -> {
FortressServerNetworkHelper.send(player, FortressChannelNames.FINISH_TASK, new ClientboundTaskExecutedPacket(this.getId()));
});
if(finishedParts > totalParts)
throw new IllegalStateException("Finished more parts than total parts");

if(taskParts.isEmpty() && totalParts == finishedParts){
world.getPlayers().stream().findAny().ifPresent(player -> FortressServerNetworkHelper.send(player, FortressChannelNames.FINISH_TASK, new ClientboundTaskExecutedPacket(this.getId())));
taskFinishListeners.forEach(Runnable::run);
}
}
Expand All @@ -134,4 +135,9 @@ public void addFinishListener(Runnable listener) {
public List<TaskInformationDto> toTaskInformationDto() {
return List.of(new TaskInformationDto(id, blocks, getTaskType()));
}

@Override
public boolean taskFullyFinished() {
return finishedParts == totalParts;
}
}

0 comments on commit 5194988

Please sign in to comment.