mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-03-12 22:59:15 +01:00
Refactor to reduce complexity
This commit is contained in:
parent
187132bc1d
commit
1d28979d5c
@ -158,20 +158,71 @@ public class BlueprintPaster {
|
||||
|
||||
final int pasteSpeed = plugin.getSettings().getPasteSpeed();
|
||||
|
||||
long timer = System.currentTimeMillis();
|
||||
int count = 0;
|
||||
if (pasteState.equals(PasteState.CHUNK_LOAD)) {
|
||||
pasteState = PasteState.CHUNK_LOADING;
|
||||
// Load chunk
|
||||
currentTask = Util.getChunkAtAsync(location).thenRun(() -> {
|
||||
pasteState = PasteState.BLOCKS;
|
||||
long duration = System.currentTimeMillis() - timer;
|
||||
if (duration > chunkLoadTime) {
|
||||
chunkLoadTime = duration;
|
||||
}
|
||||
});
|
||||
loadChunk();
|
||||
}
|
||||
else if (pasteState.equals(PasteState.BLOCKS) || pasteState.equals(PasteState.ATTACHMENTS)) {
|
||||
pasteBlocks(bits, count, owner, pasteSpeed);
|
||||
}
|
||||
else if (pasteState.equals(PasteState.ENTITIES)) {
|
||||
pasteEntities(bits, count, owner, pasteSpeed);
|
||||
}
|
||||
else if (pasteState.equals(PasteState.DONE)) {
|
||||
// All done. Cancel task
|
||||
cancelTask(result);
|
||||
} else if (pasteState.equals(PasteState.CANCEL)) {
|
||||
// This state makes sure the follow-on task only ever runs once
|
||||
pastingTask.cancel();
|
||||
result.complete(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void cancelTask(CompletableFuture<Boolean> result) {
|
||||
// Set pos1 and 2 if this was a clipboard paste
|
||||
if (island == null && clipboard != null) {
|
||||
clipboard.setPos1(pos1);
|
||||
clipboard.setPos2(pos2);
|
||||
}
|
||||
pasteState = PasteState.CANCEL;
|
||||
result.complete(true);
|
||||
}
|
||||
|
||||
private void pasteEntities(Bits bits, int count, Optional<User> owner, int pasteSpeed) {
|
||||
if (bits.it3().hasNext()) {
|
||||
Map<Location, List<BlueprintEntity>> entityMap = new HashMap<>();
|
||||
// Paste entities
|
||||
while (count < pasteSpeed) {
|
||||
if (!bits.it3().hasNext()) {
|
||||
break;
|
||||
}
|
||||
Entry<Vector, List<BlueprintEntity>> entry = bits.it3().next();
|
||||
int x = location.getBlockX() + entry.getKey().getBlockX();
|
||||
int y = location.getBlockY() + entry.getKey().getBlockY();
|
||||
int z = location.getBlockZ() + entry.getKey().getBlockZ();
|
||||
Location center = new Location(world, x, y, z).add(new Vector(0.5, 0.5, 0.5));
|
||||
List<BlueprintEntity> entities = entry.getValue();
|
||||
entityMap.put(center, entities);
|
||||
count++;
|
||||
}
|
||||
if (!entityMap.isEmpty()) {
|
||||
currentTask = paster.pasteEntities(island, world, entityMap);
|
||||
}
|
||||
} else {
|
||||
pasteState = PasteState.DONE;
|
||||
|
||||
String dimensionType = switch (location.getWorld().getEnvironment()) {
|
||||
case NETHER -> owner.map(user -> user.getTranslation("general.worlds.nether")).orElse("");
|
||||
case THE_END -> owner.map(user -> user.getTranslation("general.worlds.the-end")).orElse("");
|
||||
default -> owner.map(user -> user.getTranslation("general.worlds.overworld")).orElse("");
|
||||
};
|
||||
|
||||
owner.ifPresent(user -> user.sendMessage("commands.island.create.pasting.dimension-done", "[world]", dimensionType));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void pasteBlocks(Bits bits, int count, Optional<User> owner, int pasteSpeed) {
|
||||
Iterator<Entry<Vector, BlueprintBlock>> it = pasteState.equals(PasteState.BLOCKS) ? bits.it : bits.it2;
|
||||
if (it.hasNext()) {
|
||||
Map<Location, BlueprintBlock> blockMap = new HashMap<>();
|
||||
@ -205,53 +256,21 @@ public class BlueprintPaster {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (pasteState.equals(PasteState.ENTITIES)) {
|
||||
if (bits.it3().hasNext()) {
|
||||
Map<Location, List<BlueprintEntity>> entityMap = new HashMap<>();
|
||||
// Paste entities
|
||||
while (count < pasteSpeed) {
|
||||
if (!bits.it3().hasNext()) {
|
||||
break;
|
||||
}
|
||||
Entry<Vector, List<BlueprintEntity>> entry = bits.it3().next();
|
||||
int x = location.getBlockX() + entry.getKey().getBlockX();
|
||||
int y = location.getBlockY() + entry.getKey().getBlockY();
|
||||
int z = location.getBlockZ() + entry.getKey().getBlockZ();
|
||||
Location center = new Location(world, x, y, z).add(new Vector(0.5, 0.5, 0.5));
|
||||
List<BlueprintEntity> entities = entry.getValue();
|
||||
entityMap.put(center, entities);
|
||||
count++;
|
||||
}
|
||||
if (!entityMap.isEmpty()) {
|
||||
currentTask = paster.pasteEntities(island, world, entityMap);
|
||||
}
|
||||
} else {
|
||||
pasteState = PasteState.DONE;
|
||||
|
||||
String dimensionType = switch (location.getWorld().getEnvironment()) {
|
||||
case NETHER -> owner.map(user -> user.getTranslation("general.worlds.nether")).orElse("");
|
||||
case THE_END -> owner.map(user -> user.getTranslation("general.worlds.the-end")).orElse("");
|
||||
default -> owner.map(user -> user.getTranslation("general.worlds.overworld")).orElse("");
|
||||
};
|
||||
}
|
||||
|
||||
owner.ifPresent(user -> user.sendMessage("commands.island.create.pasting.dimension-done", "[world]", dimensionType));
|
||||
}
|
||||
}
|
||||
else if (pasteState.equals(PasteState.DONE)) {
|
||||
// All done. Cancel task
|
||||
// Set pos1 and 2 if this was a clipboard paste
|
||||
if (island == null && clipboard != null) {
|
||||
clipboard.setPos1(pos1);
|
||||
clipboard.setPos2(pos2);
|
||||
}
|
||||
pasteState = PasteState.CANCEL;
|
||||
result.complete(true);
|
||||
} else if (pasteState.equals(PasteState.CANCEL)) {
|
||||
// This state makes sure the follow-on task only ever runs once
|
||||
pastingTask.cancel();
|
||||
result.complete(true);
|
||||
private void loadChunk() {
|
||||
long timer = System.currentTimeMillis();
|
||||
pasteState = PasteState.CHUNK_LOADING;
|
||||
// Load chunk
|
||||
currentTask = Util.getChunkAtAsync(location).thenRun(() -> {
|
||||
pasteState = PasteState.BLOCKS;
|
||||
long duration = System.currentTimeMillis() - timer;
|
||||
if (duration > chunkLoadTime) {
|
||||
chunkLoadTime = duration;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void tellOwner(User user, int blocksSize, int attachedSize, int entitiesSize, int pasteSpeed) {
|
||||
|
Loading…
Reference in New Issue
Block a user