Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.ArrayDeque;
import java.util.Deque;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -28,6 +30,7 @@
import com.player2.playerengine.player2api.Prompts;
import com.player2.playerengine.tasks.base.Task;
import com.player2.playerengine.util.time.TimerGame;
import com.player2.playerengine.tasks.construction.build_structure.StructureFromCode.SetBlockCommand;

import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
Expand Down Expand Up @@ -307,24 +310,25 @@ public boolean isFinished() {
private class BuildFromCode extends Task {
String code;

private TimerGame blockPlaceTimer = new TimerGame(0.1f);
Deque<SetBlockCommand> setBlockQueue = new ArrayDeque<>();

private ExecutorService buildThread;
// outer Option: is done, inner option: is error
Optional<Optional<String>> result = Optional.empty();

public BuildFromCode(String code) {
this.code = code;
this.buildThread = Executors.newSingleThreadExecutor();
synchronized (setBlockQueue) {
setBlockQueue.clear();
}
buildThread.submit(() -> {
StructureFromCode.buildStructureFromCode(code, setBlockData -> {
LOGGER.info("setBlock(x={}, y={}, z={}, blockName={})",
setBlockData.x, setBlockData.y, setBlockData.z, setBlockData.blockName);
ResourceLocation id = ResourceLocation.fromNamespaceAndPath("minecraft", setBlockData.blockName);
Block block = BuiltInRegistries.BLOCK.get(id);
// 3 means send to clients (2) and notify neighbors/update block states (1).
// maybe do 2 if you dont want
// redstone/etc updating/torches falling probably
mod.getWorld().setBlock(new BlockPos(setBlockData.x, setBlockData.y, setBlockData.z),
block.defaultBlockState(), 3);
// Queue up
synchronized (setBlockQueue) {
setBlockQueue.add(setBlockData);
}
}, (errStr) -> {
result = Optional.of(Optional.of(errStr));
}, () -> {
Expand All @@ -349,17 +353,40 @@ protected void onStart() {
protected void onStop(Task var1) {
// TODO Auto-generated method stub
buildThread.shutdownNow();
synchronized (setBlockQueue) {
setBlockQueue.clear();
}
}

@Override
protected Task onTick() {
// TODO Auto-generated method stub

synchronized (setBlockQueue) {
if (setBlockQueue.size() == 0) {
return null;
}
if (!blockPlaceTimer.elapsed()) {
return null;
}
SetBlockCommand setBlockData = setBlockQueue.poll();
LOGGER.info("setBlock(x={}, y={}, z={}, blockName={})",
setBlockData.x, setBlockData.y, setBlockData.z, setBlockData.blockName);
ResourceLocation id = ResourceLocation.fromNamespaceAndPath("minecraft", setBlockData.blockName);
Block block = BuiltInRegistries.BLOCK.get(id);
// 3 means send to clients (2) and notify neighbors/update block states (1).
// maybe do 2 if you dont want
// redstone/etc updating/torches falling probably
mod.getWorld().setBlock(new BlockPos(setBlockData.x, setBlockData.y, setBlockData.z),
block.defaultBlockState(), 3);
blockPlaceTimer.reset();
}
return null;
}

@Override
public boolean isFinished() {
return result.isPresent();
return result.isPresent() && setBlockQueue.size() == 0;
}

@Override
Expand Down