Clearer paster (#1953)

* WIP - make easier to understand.

* Small refactor of paster to make it easier to understand

* Fix tabs to spaces. Sorry - new editor!

* Fix tabs to spaces

* Fix tab to spaces
This commit is contained in:
tastybento 2022-03-19 16:19:31 +00:00 committed by GitHub
parent 51dbca0f99
commit 6796fceee8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 94 deletions

View File

@ -134,38 +134,43 @@ public class BlueprintPaster {
this.location = island.getProtectionCenter().toVector().subtract(off).toLocation(world); this.location = island.getProtectionCenter().toVector().subtract(off).toLocation(world);
} }
private record Bits(Map<Vector, BlueprintBlock> blocks,
Map<Vector, BlueprintBlock> attached,
Map<Vector, List<BlueprintEntity>> entities,
Iterator<Entry<Vector, BlueprintBlock>> it,
Iterator<Entry<Vector, BlueprintBlock>> it2,
Iterator<Entry<Vector, List<BlueprintEntity>>> it3,
int pasteSpeed) {}
/** /**
* The main pasting method * The main pasting method
*/ */
public CompletableFuture<Boolean> paste() { public CompletableFuture<Boolean> paste() {
CompletableFuture<Boolean> result = new CompletableFuture<>(); CompletableFuture<Boolean> result = new CompletableFuture<>();
// Iterators for the various maps to paste // Iterators for the various maps to paste
Map<Vector, BlueprintBlock> blocks = blueprint.getBlocks() == null ? Collections.emptyMap() : blueprint.getBlocks(); final Map<Vector, BlueprintBlock> blocks = blueprint.getBlocks() == null ? Collections.emptyMap() : blueprint.getBlocks();
Map<Vector, BlueprintBlock> attached = blueprint.getAttached() == null ? Collections.emptyMap() : blueprint.getAttached(); final Map<Vector, BlueprintBlock> attached = blueprint.getAttached() == null ? Collections.emptyMap() : blueprint.getAttached();
Map<Vector, List<BlueprintEntity>> entities = blueprint.getEntities() == null ? Collections.emptyMap() : blueprint.getEntities(); final Map<Vector, List<BlueprintEntity>> entities = blueprint.getEntities() == null ? Collections.emptyMap() : blueprint.getEntities();
Iterator<Entry<Vector, BlueprintBlock>> it = blocks.entrySet().iterator();
Iterator<Entry<Vector, BlueprintBlock>> it2 = attached.entrySet().iterator();
Iterator<Entry<Vector, List<BlueprintEntity>>> it3 = entities.entrySet().iterator();
// Initial state & speed // Initial state & speed
pasteState = PasteState.CHUNK_LOAD; pasteState = PasteState.CHUNK_LOAD;
final int pasteSpeed = plugin.getSettings().getPasteSpeed();
// If this is an island OVERWORLD paste, get the island owner. // If this is an island OVERWORLD paste, get the island owner.
final Optional<User> owner = Optional.ofNullable(island) final Optional<User> owner = Optional.ofNullable(island)
.filter(i -> location.getWorld().getEnvironment().equals(World.Environment.NORMAL)) .filter(i -> location.getWorld().getEnvironment().equals(World.Environment.NORMAL))
.map(i -> User.getInstance(i.getOwner())); .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 -> { owner.ifPresent(user -> tellOwner(user, blocks.size(), attached.size(), entities.size(), plugin.getSettings().getPasteSpeed()));
// Estimated time: Bits bits = new Bits(blocks, attached, entities,
double total = (double) blocks.size() + attached.size() + entities.size(); blocks.entrySet().iterator(), attached.entrySet().iterator(), entities.entrySet().iterator(),
BigDecimal time = BigDecimal.valueOf(total / (pasteSpeed * 20.0D) + (chunkLoadTime / 1000.0D)).setScale(1, RoundingMode.UP); plugin.getSettings().getPasteSpeed());
user.sendMessage("commands.island.create.pasting.estimated-time", TextVariables.NUMBER, String.valueOf(time.doubleValue())); pastingTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> pasterTask(result, owner, bits), 0L, 1L);
// We're pasting blocks!
user.sendMessage("commands.island.create.pasting.blocks", TextVariables.NUMBER, String.valueOf(blocks.size() + attached.size())); return result;
}); }
private void pasterTask(CompletableFuture<Boolean> result, Optional<User> owner, Bits bits) {
final int pasteSpeed = plugin.getSettings().getPasteSpeed();
pastingTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
long timer = System.currentTimeMillis(); long timer = System.currentTimeMillis();
int count = 0; int count = 0;
if (pasteState.equals(PasteState.CHUNK_LOAD)) { if (pasteState.equals(PasteState.CHUNK_LOAD)) {
@ -179,32 +184,32 @@ public class BlueprintPaster {
} }
}); });
} }
while (pasteState.equals(PasteState.BLOCKS) && count < pasteSpeed && it.hasNext()) { while (pasteState.equals(PasteState.BLOCKS) && count < pasteSpeed && bits.it.hasNext()) {
pasteBlock(location, it.next()); pasteBlock(location, bits.it.next());
count++; count++;
} }
while (pasteState.equals(PasteState.ATTACHMENTS) && count < pasteSpeed && it2.hasNext()) { while (pasteState.equals(PasteState.ATTACHMENTS) && count < pasteSpeed && bits.it2.hasNext()) {
pasteBlock(location, it2.next()); pasteBlock(location, bits.it2.next());
count++; count++;
} }
while (pasteState.equals(PasteState.ENTITIES) && count < pasteSpeed && it3.hasNext()) { while (pasteState.equals(PasteState.ENTITIES) && count < pasteSpeed && bits.it3.hasNext()) {
pasteEntity(location, it3.next()); pasteEntity(location, bits.it3.next());
count++; count++;
} }
// STATE SHIFT // STATE SHIFT
if (pasteState.equals(PasteState.BLOCKS) && !it.hasNext()) { if (pasteState.equals(PasteState.BLOCKS) && !bits.it.hasNext()) {
// Blocks done // Blocks done
// Next paste attachments // Next paste attachments
pasteState = PasteState.ATTACHMENTS; pasteState = PasteState.ATTACHMENTS;
} }
else if (pasteState.equals(PasteState.ATTACHMENTS) && !it2.hasNext()) { else if (pasteState.equals(PasteState.ATTACHMENTS) && !bits.it2.hasNext()) {
// Attachments done. Next paste entities // Attachments done. Next paste entities
pasteState = PasteState.ENTITIES; pasteState = PasteState.ENTITIES;
if (entities.size() != 0) { if (bits.entities.size() != 0) {
owner.ifPresent(user -> user.sendMessage("commands.island.create.pasting.entities", TextVariables.NUMBER, String.valueOf(entities.size()))); owner.ifPresent(user -> user.sendMessage("commands.island.create.pasting.entities", TextVariables.NUMBER, String.valueOf(bits.entities.size())));
} }
} }
else if (pasteState.equals(PasteState.ENTITIES) && !it3.hasNext()) { else if (pasteState.equals(PasteState.ENTITIES) && !bits.it3.hasNext()) {
pasteState = PasteState.DONE; pasteState = PasteState.DONE;
owner.ifPresent(user -> user.sendMessage("commands.island.create.pasting.done")); owner.ifPresent(user -> user.sendMessage("commands.island.create.pasting.done"));
} }
@ -215,16 +220,22 @@ public class BlueprintPaster {
clipboard.setPos1(pos1); clipboard.setPos1(pos1);
clipboard.setPos2(pos2); clipboard.setPos2(pos2);
} }
result.complete(true);
pasteState = PasteState.CANCEL; pasteState = PasteState.CANCEL;
result.complete(true);
} else if (pasteState.equals(PasteState.CANCEL)) { } else if (pasteState.equals(PasteState.CANCEL)) {
// This state makes sure the follow-on task only ever runs once // This state makes sure the follow-on task only ever runs once
pastingTask.cancel(); pastingTask.cancel();
result.complete(true); result.complete(true);
} }
}, 0L, 1L); }
return result; private void tellOwner(User user, int blocksSize, int attachedSize, int entitiesSize, int pasteSpeed) {
// Estimated time:
double total = (double) blocksSize + attachedSize + entitiesSize;
BigDecimal time = BigDecimal.valueOf(total / (pasteSpeed * 20.0D) + (chunkLoadTime / 1000.0D)).setScale(1, RoundingMode.UP);
user.sendMessage("commands.island.create.pasting.estimated-time", TextVariables.NUMBER, String.valueOf(time.doubleValue()));
// We're pasting blocks!
user.sendMessage("commands.island.create.pasting.blocks", TextVariables.NUMBER, String.valueOf(blocksSize + attachedSize));
} }
private void pasteBlock(Location location, Entry<Vector, BlueprintBlock> entry) { private void pasteBlock(Location location, Entry<Vector, BlueprintBlock> entry) {