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
@ -143,7 +143,7 @@ public class BlueprintPaster {
|
||||
// If this is an island OVERWORLD paste, get the island owner.
|
||||
final Optional<User> owner = Optional.ofNullable(island).map(i -> User.getInstance(i.getOwner()));
|
||||
|
||||
// Tell the owner we're pasting blocks and how much time it might take
|
||||
// Tell the owner we're pasting blocks and how much time it might take
|
||||
owner.ifPresent(user -> tellOwner(user, blocks.size(), attached.size(), entities.size(), plugin.getSettings().getPasteSpeed()));
|
||||
Bits bits = new Bits(blocks, attached, entities,
|
||||
blocks.entrySet().iterator(), attached.entrySet().iterator(), entities.entrySet().iterator(),
|
||||
@ -158,95 +158,19 @@ 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)) {
|
||||
Iterator<Entry<Vector, BlueprintBlock>> it = pasteState.equals(PasteState.BLOCKS) ? bits.it : bits.it2;
|
||||
if (it.hasNext()) {
|
||||
Map<Location, BlueprintBlock> blockMap = new HashMap<>();
|
||||
// Paste blocks
|
||||
while (count < pasteSpeed) {
|
||||
if (!it.hasNext()) {
|
||||
break;
|
||||
}
|
||||
Entry<Vector, BlueprintBlock> entry = it.next();
|
||||
Location pasteTo = location.clone().add(entry.getKey());
|
||||
// pos1 and pos2 update
|
||||
updatePos(pasteTo);
|
||||
|
||||
BlueprintBlock block = entry.getValue();
|
||||
blockMap.put(pasteTo, block);
|
||||
count++;
|
||||
}
|
||||
if (!blockMap.isEmpty()) {
|
||||
currentTask = paster.pasteBlocks(island, world, blockMap);
|
||||
}
|
||||
} else {
|
||||
if (pasteState.equals(PasteState.BLOCKS)) {
|
||||
// Blocks done
|
||||
// Next paste attachments
|
||||
pasteState = PasteState.ATTACHMENTS;
|
||||
} else {
|
||||
// Attachments done. Next paste entities
|
||||
pasteState = PasteState.ENTITIES;
|
||||
if (bits.entities.size() != 0) {
|
||||
owner.ifPresent(user -> user.sendMessage("commands.island.create.pasting.entities", TextVariables.NUMBER, String.valueOf(bits.entities.size())));
|
||||
}
|
||||
}
|
||||
}
|
||||
pasteBlocks(bits, count, owner, pasteSpeed);
|
||||
}
|
||||
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));
|
||||
}
|
||||
pasteEntities(bits, count, owner, pasteSpeed);
|
||||
}
|
||||
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);
|
||||
cancelTask(result);
|
||||
} else if (pasteState.equals(PasteState.CANCEL)) {
|
||||
// This state makes sure the follow-on task only ever runs once
|
||||
pastingTask.cancel();
|
||||
@ -254,6 +178,101 @@ public class BlueprintPaster {
|
||||
}
|
||||
}
|
||||
|
||||
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<>();
|
||||
// Paste blocks
|
||||
while (count < pasteSpeed) {
|
||||
if (!it.hasNext()) {
|
||||
break;
|
||||
}
|
||||
Entry<Vector, BlueprintBlock> entry = it.next();
|
||||
Location pasteTo = location.clone().add(entry.getKey());
|
||||
// pos1 and pos2 update
|
||||
updatePos(pasteTo);
|
||||
|
||||
BlueprintBlock block = entry.getValue();
|
||||
blockMap.put(pasteTo, block);
|
||||
count++;
|
||||
}
|
||||
if (!blockMap.isEmpty()) {
|
||||
currentTask = paster.pasteBlocks(island, world, blockMap);
|
||||
}
|
||||
} else {
|
||||
if (pasteState.equals(PasteState.BLOCKS)) {
|
||||
// Blocks done
|
||||
// Next paste attachments
|
||||
pasteState = PasteState.ATTACHMENTS;
|
||||
} else {
|
||||
// Attachments done. Next paste entities
|
||||
pasteState = PasteState.ENTITIES;
|
||||
if (bits.entities.size() != 0) {
|
||||
owner.ifPresent(user -> user.sendMessage("commands.island.create.pasting.entities", TextVariables.NUMBER, String.valueOf(bits.entities.size())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
// Estimated time:
|
||||
double total = (double) blocksSize + attachedSize + entitiesSize;
|
||||
|
Loading…
Reference in New Issue
Block a user