Skip to content

Commit

Permalink
Fix is delete bug
Browse files Browse the repository at this point in the history
- update prepackaged schematics
  • Loading branch information
Mohron committed Jun 18, 2019
1 parent a54547a commit 73a912b
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 44 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
- Added `/is entity` command for detailed entity information
- Added support for flat world preset codes (_block ID portion only_) for region generation
- See https://minecraft.gamepedia.com/Superflat#Preset_code_format for more details
- Added new schematics:
- Stoneblock 2
- SkyFactory 4
- Removed outdated schematics:
- Garden of Glass
- SkyExchange
- Fixed schematics sometimes not generating at the intended height. The height set will be the height the player is at when standing on the lowest block of a schematic.
- Fixed Nucleus Integration commands not registering after a reload
- Fixed admin island expansion (`/is info`) bug where old clickable text can be used to expand outside the region
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ plugins {
id "signing"
id "ninja.miserable.blossom" version "1.0.1"
id "com.github.johnrengelman.shadow" version "5.0.0"
id "org.spongepowered.plugin" version "0.8.1"
id "org.spongepowered.plugin" version "0.9.0"
id "com.qixalite.spongestart2" version "4.0.0"
id "io.franzbecker.gradle-lombok" version "1.14"
id "io.franzbecker.gradle-lombok" version "3.1.0"
id "org.sonarqube" version "2.6"
}

Expand Down Expand Up @@ -60,7 +60,7 @@ dependencies {
}

lombok {
version = "1.18.4"
version = "1.18.8"
sha256 = ""
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.mohron.skyclaims.permissions.Permissions;
import net.mohron.skyclaims.team.PrivilegeType;
import net.mohron.skyclaims.world.Island;
import net.mohron.skyclaims.world.IslandManager;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandPermissionException;
import org.spongepowered.api.command.CommandResult;
Expand Down Expand Up @@ -104,6 +105,10 @@ private Consumer<CommandSource> getConfirmation(Island island, boolean clear) {

private Consumer<CommandSource> deleteIsland(Island island, boolean clear) {
return src -> {
if (!IslandManager.ISLANDS.containsKey(island.getUniqueId())) {
src.sendMessage(Text.of(island.getName(), TextColors.RED, " has already been deleted!"));
return;
}
if (clear) {
island.clear();
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/net/mohron/skyclaims/permissions/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.mohron.skyclaims.schematic.IslandSchematic;
import org.apache.commons.lang3.StringUtils;
import org.spongepowered.api.service.permission.PermissionService;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.world.biome.BiomeType;

public final class Options {
Expand Down Expand Up @@ -107,9 +108,10 @@ public static int getMaxTeammates(UUID playerUniqueId) {
}

private static String getStringOption(UUID uuid, String option, String defaultValue) {
return !PERMISSION_SERVICE.getUserSubjects().getSubject(uuid.toString()).isPresent()
Optional<Subject> subject = PERMISSION_SERVICE.getUserSubjects().getSubject(uuid.toString());
return !subject.isPresent()
? defaultValue
: PERMISSION_SERVICE.getUserSubjects().getSubject(uuid.toString()).get().getOption(option).orElse(defaultValue);
: subject.get().getOption(option).orElse(defaultValue);
}

private static int getIntOption(UUID uuid, String option, int defaultValue, int min, int max) {
Expand All @@ -118,11 +120,11 @@ private static int getIntOption(UUID uuid, String option, int defaultValue, int
}

private static int getIntOption(UUID uuid, String option, int defaultValue) {
if (!PERMISSION_SERVICE.getUserSubjects().getSubject(uuid.toString()).isPresent()) {
Optional<Subject> subject = PERMISSION_SERVICE.getUserSubjects().getSubject(uuid.toString());
if (!subject.isPresent()) {
return defaultValue;
}
String value = PERMISSION_SERVICE.getUserSubjects().getSubject(uuid.toString()).get().getOption(option)
.orElse(String.valueOf(defaultValue));
String value = subject.get().getOption(option).orElse(String.valueOf(defaultValue));
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/net/mohron/skyclaims/schematic/SchematicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
package net.mohron.skyclaims.schematic;

import com.google.common.collect.Lists;
import com.google.common.io.Resources;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -121,37 +122,38 @@ public boolean save(IslandSchematic schematic) {
new File(directory, schematic.getFileName())
)), schematicData);
} catch (Exception e) {
plugin.getLogger().error("Error saving schematic: ", schematic.getName());
plugin.getLogger().error("Error saving schematic: " + schematic.getName(), e);
return false;
}
return true;
}

private void unpackDefaultSchematics() {
String[] defaultSchematics = {"gardenofglass", "grass", "sand", "skyexchange", "skyfactory", "snow", "wood"};
String[] defaultSchematics = {"grass", "sand", "skyfactory", "skyfactory4", "snow", "stoneblock2", "wood"};
if (!directory.exists() || !directory.isDirectory()) {
try {
boolean success = directory.mkdir();
if (!success) {
throw new IOException();
}
} catch (SecurityException | IOException e) {
plugin.getLogger().error(String.format("Failed to create schematics directory.%n %s", e.getMessage()));
plugin.getLogger().error("Failed to create schematics directory.", e);
}
}
try {
//noinspection ConstantConditions - schemDir.list() is being checked for null
if (directory.list() == null || directory.list().length < 1) {
String[] list = directory.list();
ClassLoader classLoader = getClass().getClassLoader();
if (classLoader != null && (list == null || list.length == 0)) {
for (String name : defaultSchematics) {
File schematic = Paths.get(String.format("%s%s%s.schematic", directory.toPath(), File.separator, name)).toFile();
//noinspection ConstantConditions - Resource will always be included
Resources.asByteSource(this.getClass().getClassLoader()
.getResource(name + SCHEMATIC_FILE_EXT))
.copyTo(com.google.common.io.Files.asByteSink(schematic));
InputStream schematic = classLoader.getResourceAsStream(name + SCHEMATIC_FILE_EXT);
Path target = Paths.get(String.format("%s%s%s.schematic", directory.toPath(), File.separator, name));
if (schematic != null) {
Files.copy(schematic, target);
}
}
}
} catch (SecurityException | IOException e) {
plugin.getLogger().error(String.format("Failed to create default schematic.%n %s", e.getMessage()));
plugin.getLogger().error("Failed to create default schematic.", e);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/mohron/skyclaims/world/Island.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ private void save() {
}

public void clear() {
RegenerateRegionTask regenerateRegionTask = new RegenerateRegionTask(getRegion());
RegenerateRegionTask regenerateRegionTask = new RegenerateRegionTask(getRegion(), spawn.getExtent());
PLUGIN.getGame().getScheduler().createTaskBuilder().async().execute(regenerateRegionTask).submit(PLUGIN);
}

Expand Down
20 changes: 9 additions & 11 deletions src/main/java/net/mohron/skyclaims/world/RegenerateChunkTask.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.mohron.skyclaims.world;

import com.flowpowered.math.vector.Vector3i;
import net.mohron.skyclaims.SkyClaims;
import net.mohron.skyclaims.SkyClaimsTimings;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.tileentity.carrier.TileEntityCarrier;
import org.spongepowered.api.entity.Entity;
Expand All @@ -17,15 +17,13 @@ public class RegenerateChunkTask implements Runnable {
private static final SkyClaims PLUGIN = SkyClaims.getInstance();

private final World world;
private final int x;
private final int z;
private final Vector3i position;
private final BlockState[] blocks;
private final Location<World> spawn;

public RegenerateChunkTask(World world, int x, int z, BlockState[] blocks, Location<World> spawn) {
public RegenerateChunkTask(World world, Vector3i position, BlockState[] blocks, Location<World> spawn) {
this.world = world;
this.x = x;
this.z = z;
this.position = position;
this.blocks = blocks;
this.spawn = spawn;
}
Expand All @@ -35,19 +33,19 @@ public RegenerateChunkTask(World world, int x, int z, BlockState[] blocks, Locat
public void run() {
SkyClaimsTimings.CLEAR_ISLAND.startTimingIfSync();

final Chunk chunk = world.loadChunk(Sponge.getServer().getChunkLayout().forceToChunk(x, 0, z), false)
.orElse(null);
final Chunk chunk = world.loadChunk(position, true).orElse(null);

if (chunk == null) {
PLUGIN.getLogger().error("Failed to load chunk at block {}, 0, {}", x, z);
PLUGIN.getLogger().error("Failed to load chunk {}", position.toString());
return;
}

PLUGIN.getLogger().debug("Began regenerating chunk ({}, {})", x, z);
PLUGIN.getLogger().debug("Began regenerating chunk {}", position.toString());
// Teleport any players to world spawn
chunk.getEntities(e -> e instanceof Player).forEach(e -> e.setLocationSafely(spawn));
// Clear the contents of an tile entity with an inventory
chunk.getTileEntities(e -> e instanceof TileEntityCarrier).forEach(e -> ((TileEntityCarrier) e).getInventory().clear());
// Set the blocks
for (int bx = chunk.getBlockMin().getX(); bx <= chunk.getBlockMax().getX(); bx++) {
for (int bz = chunk.getBlockMin().getZ(); bz <= chunk.getBlockMax().getZ(); bz++) {
for (int by = chunk.getBlockMin().getY(); by <= chunk.getBlockMax().getY(); by++) {
Expand All @@ -60,7 +58,7 @@ public void run() {
// Remove any remaining entities.
chunk.getEntities(e -> !(e instanceof Player)).forEach(Entity::remove);
chunk.unloadChunk();
PLUGIN.getLogger().debug("Finished regenerating chunk ({}, {})", x, z);
PLUGIN.getLogger().debug("Finished regenerating chunk {}", position.toString());

SkyClaimsTimings.CLEAR_ISLAND.stopTimingIfSync();
}
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/net/mohron/skyclaims/world/RegenerateRegionTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package net.mohron.skyclaims.world;

import com.flowpowered.math.vector.Vector3i;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import java.util.List;
Expand All @@ -41,18 +42,21 @@ public class RegenerateRegionTask implements Runnable {
private static final SkyClaims PLUGIN = SkyClaims.getInstance();

private Region region;
private World world;
private Island island;
private IslandSchematic schematic;
private boolean commands;

public RegenerateRegionTask(Region region) {
public RegenerateRegionTask(Region region, World world) {
this.region = region;
this.world = world;
this.island = null;
this.commands = false;
}

public RegenerateRegionTask(Island island, IslandSchematic schematic, boolean commands) {
this.region = island.getRegion();
this.world = island.getWorld();
this.island = island;
this.schematic = schematic;
this.commands = commands;
Expand All @@ -66,18 +70,16 @@ public void run() {

Stopwatch sw = Stopwatch.createStarted();

PLUGIN.getLogger().info("Using preset code '{}' to regenerate region.", config.getPresetCode());
String preset = schematic != null && schematic.getPreset().isPresent()
? schematic.getPreset().get()
: config.getPresetCode();
final BlockState[] blocks = FlatWorldUtil.getBlocksSafely(preset);
PLUGIN.getLogger().info("Using preset code '{}' to regenerate region.", preset);

regenerateChunks(blocks, config.getSpawn());
regenerateChunks(preset, config.getSpawn());

sw.stop();

PLUGIN.getLogger().info("Finished regenerating region ({}, {}) in {}s.", region.getX(), region.getZ(),
sw.elapsed(TimeUnit.SECONDS));
PLUGIN.getLogger().info("Finished regenerating region ({}, {}) in {}s.", region.getX(), region.getZ(), sw.elapsed(TimeUnit.SECONDS));

if (island != null) {
if (commands) {
Expand All @@ -93,20 +95,19 @@ public void run() {
}
}

private void regenerateChunks(BlockState[] blocks, Location<World> spawn) {
private void regenerateChunks(String preset, Location<World> spawn) {
SpongeExecutorService executor = Sponge.getScheduler().createSyncExecutor(PLUGIN);
BlockState[] blocks = FlatWorldUtil.getBlocksSafely(preset);
int progress = 0;
for (int x = region.getLesserBoundary().getX(); x < region.getGreaterBoundary().getX(); x += 16) {
List<CompletableFuture<Void>> tasks = Lists.newArrayList();
for (int z = region.getLesserBoundary().getZ(); z < region.getGreaterBoundary().getZ(); z += 16) {
tasks.add(CompletableFuture.runAsync(
new RegenerateChunkTask(island.getWorld(), x, z, blocks, spawn), executor)
);
Vector3i position = Sponge.getServer().getChunkLayout().forceToChunk(x,0, z);
tasks.add(CompletableFuture.runAsync(new RegenerateChunkTask(world, position, blocks, spawn), executor));
}
try {
CompletableFuture.allOf(tasks.toArray(new CompletableFuture[32])).join();
PLUGIN.getLogger().info("Regenerating region {}, {} {}% complete", region.getX(), region.getZ(),
Math.round(++progress / 32f * 100));
PLUGIN.getLogger().info("Regenerating region {}, {} {}% complete", region.getX(), region.getZ(), Math.round(++progress / 32f * 100));
} catch (RuntimeException e) {
PLUGIN.getLogger().error("Could not regenerate chunk.", e);
}
Expand Down
Binary file removed src/main/resources/gardenofglass.schematic
Binary file not shown.
Binary file removed src/main/resources/skyexchange.schematic
Binary file not shown.
Binary file added src/main/resources/skyfactory4.schematic
Binary file not shown.

0 comments on commit 73a912b

Please sign in to comment.