Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cube Drawing updates #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#!/usr/bin/env bash
mvn clean install
mv target/terraform-minecraft-plugin-1.0.jar ~/Desktop/server/plugins
if [ -d ~/Desktop/Server/plugins ];then
cp target/terraform-minecraft-plugin-1.0.jar ~/Desktop/Server/plugins
fi

if [ -d ./spigot/plugins ];then
cp target/terraform-minecraft-plugin-1.0.jar ./spigot/plugins
fi
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
Expand Down
15 changes: 15 additions & 0 deletions server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
if [ ! -d spigot ];then
mkdir spigot
cd spigot
curl -o BuildTools.jar https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar
java -jar BuildTools.jar
cd -
fi

bash build.sh
cd spigot
rm -rf world
rm -rf world_nether
rm -rf world_the_end
rm -rf plugins/Terraform/*
java -jar spigot-1.13.2.jar
9 changes: 9 additions & 0 deletions src/main/java/com/swinkler/terraform/models/shape/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Shape {
private String id;
private Location location;
private ShapeType shapeType;
private boolean hollow=false;
private String material;
private Map<String,Integer> dimensions;
private List<String> previousData;
Expand Down Expand Up @@ -88,6 +89,14 @@ public void setStatus(ResourceStatus status) {
this.status = status;
}

public boolean getHollow() {
return hollow;
}

public void setHollow(boolean hollow) {
this.hollow = hollow;
}

@JsonIgnore
public Material getBukkitMaterial(){
return Material.valueOf(material);
Expand Down
48 changes: 43 additions & 5 deletions src/main/java/com/swinkler/terraform/provider/ProviderUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,52 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.List;
import com.google.common.collect.Lists;

public class ProviderUtility {

public static Future<Block> scheduleSetBlock(Block block, Material material, int ticks){
return ProviderUtility.scheduleSetBlock(block,material,ticks,null);
public static void scheduleSetBlock(Block block, Material material, int ticks){
ProviderUtility.scheduleSetBlock(block,material,ticks,null);
}

public static Future<Block> scheduleSetBlock(Block block, Material material, int ticks,Particle particle){
public static void scheduleSetBlocks(List<Block> blocks, Material material){
ProviderUtility.scheduleSetBlocks(blocks,material, null);
}

private static int createSeconds=5;
private static int ticksPerSecond=20;
private static int skipTicks=4;
private static double createTicks=(createSeconds*ticksPerSecond)/skipTicks;
public static void scheduleSetBlocks(List<Block> blocks, Material material, Particle particle){
int batchSize = (int)(blocks.size()/createTicks)+1;
int i = 0;
for (List<Block> batchBlocks : Lists.partition(blocks, batchSize)) {
scheduleSetBlocks(batchBlocks, material, i, particle);
i+=skipTicks;
}
}

public static void scheduleSetBlocks(List<Block> blocks, Material material, int ticks, Particle particle){
Runnable task = () -> {
for (int i = 0; i < blocks.size(); i++) {
Block block = blocks.get(i);
World world = block.getWorld();
Location location = block.getLocation();
BlockState blockState = block.getState();
blockState.setType(material);
if (particle != null){
world.spawnParticle(particle,location,1);
}
blockState.update(true);
}
};

Bukkit.getScheduler().scheduleSyncDelayedTask(BukkitService.getInstance(),task,ticks);
}


public static void scheduleSetBlock(Block block, Material material, int ticks,Particle particle){
Location location = block.getLocation();
World world = block.getWorld();
Runnable task = () -> {
Expand All @@ -34,7 +72,7 @@ public static Future<Block> scheduleSetBlock(Block block, Material material, int
};

Bukkit.getScheduler().scheduleSyncDelayedTask(BukkitService.getInstance(),task,ticks);
return wrapFuture(block,ticks);
//return wrapFuture(block,ticks);
}

public static void scheduleTask(Runnable runnable,int ticks){
Expand Down Expand Up @@ -62,7 +100,7 @@ public static Future<org.bukkit.entity.Entity> scheduleEntity(Entity entity, int
public static <T> Future<T> wrapFuture(T t,int ticks){
ExecutorService executor = Executors.newSingleThreadExecutor();
return executor.submit(()->{
Thread.sleep(ticks*50);
Thread.sleep(ticks);
return t;
});
}
Expand Down
50 changes: 29 additions & 21 deletions src/main/java/com/swinkler/terraform/provider/ResourceCube.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class ResourceCube implements Resource<ShapeRequest, Shape> {
public Shape create(ShapeRequest shapeRequest) {
// Instantiate new shape and set attributes from request data
Shape shape = new Shape(shapeRequest);

boolean hollow = shape.getHollow();
// Place blocks in world
ArrayList<Block> placedBlocks = placeBlocks(shape);
ArrayList<Block> placedBlocks = placeBlocks(shape, hollow);
List<Material> previousData = placedBlocks.stream().map(b -> {
return b.getBlockData().getMaterial();
}).collect(Collectors.toList());
Expand All @@ -36,7 +36,7 @@ public Shape create(ShapeRequest shapeRequest) {
ProviderUtility.scheduleTask(() -> {
shape.setStatus(ResourceStatus.Ready);
shapeDAO.updateShape(shape);
}, placedBlocks.size());
}, (int)0.005*placedBlocks.size()*2);

return shape;
}
Expand All @@ -45,13 +45,14 @@ public Shape read(String shapeId) {
BukkitService.getLogger().info(shapeId);
ShapeDAO shapeDao = ShapeDAO.getInstance();
Shape shape = shapeDao.getShape(shapeId);
boolean hollow = shape.getHollow();
//quit early if not found
if (shape==null){
return null;
}
CubeDimensions cubeDimensions = new CubeDimensions(shape.getDimensions());
Location location = shape.getBukkitLocation();
ArrayList<Block> blocks = getRegionBlocks(cubeDimensions, location);
ArrayList<Block> blocks = getRegionBlocks(cubeDimensions, location, hollow);
Material material = shape.getBukkitMaterial();
//if the materials dont match then update the shape data to effectively taint the resource
for (Block block: blocks){
Expand All @@ -68,13 +69,14 @@ public Shape read(String shapeId) {
public Shape update(String shapeId, ShapeRequest shapeRequest) {
ShapeDAO shapeDAO = ShapeDAO.getInstance();
Shape oldShape = shapeDAO.getShape(shapeId);
removeBlocks(oldShape);
removeBlocks(oldShape, true);
Shape shape = new Shape(shapeRequest);
boolean hollow = shape.getHollow();
shape.setPreviousData(oldShape.getPreviousData());
shape.setId(oldShape.getId());
shape.setStatus(ResourceStatus.Updating);
shapeDAO.updateShape(shape);
ArrayList<Block> placedBlocks = placeBlocks(shape);
ArrayList<Block> placedBlocks = placeBlocks(shape, hollow);

//Update state when the resource has finished creating
int ticks = 2*placedBlocks.size();
Expand All @@ -88,7 +90,7 @@ public Shape update(String shapeId, ShapeRequest shapeRequest) {

public void delete(String shapeId) {
Shape shape = read(shapeId);
removeBlocks(shape);
removeBlocks(shape, true);

//Update state when the resource has finished creating
int ticks = shape.getPreviousData().size();
Expand All @@ -101,44 +103,50 @@ public void delete(String shapeId) {
}


public ArrayList<Block> getRegionBlocks(CubeDimensions cubeDimensions, Location location) {
public ArrayList<Block> getRegionBlocks(CubeDimensions cubeDimensions, Location location, boolean hollow) {
int length = cubeDimensions.getLengthX();
int lengthDir = length < 0 ? -1 : 1;
int width = cubeDimensions.getWidthZ();
int widthDir = width < 0 ? -1 : 1;
int height = cubeDimensions.getHeightY();
int heightDir = height < 0 ? -1 : 1;
length *= lengthDir;
width *= widthDir;
height *= heightDir;
World w = BukkitService.getWorld();
ArrayList<Block> blocks = new ArrayList<Block>();
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
for (int k = 0; k < height; k++) {
Location blockLocation = location.clone().add(i,k,j);

for (int y = 0; y < height; y++) {
for (int x = 0; x < length; x++) {
for (int z = 0; z < width; z++) {
if (hollow && (z != 0 && z != width-1) && (x != 0 && x != length-1) && (y > 0 && y < height-1))
continue;
Location blockLocation = location.clone().add(x*lengthDir,y*heightDir,z*widthDir);
blocks.add(w.getBlockAt(blockLocation));
}
}
}
return blocks;
}

public ArrayList<Block> placeBlocks(Shape shape) {
public ArrayList<Block> placeBlocks(Shape shape, boolean hollow) {
CubeDimensions cubeDimensions = new CubeDimensions(shape.getDimensions());
Location location = shape.getBukkitLocation();
ArrayList<Block> blocks = getRegionBlocks(cubeDimensions, location);
ArrayList<Block> blocks = getRegionBlocks(cubeDimensions, location, hollow);
Material material = shape.getBukkitMaterial();
for (int i = 0; i < blocks.size(); i++) {
Block block = blocks.get(i);
ProviderUtility.scheduleSetBlock(block, material, i,Particle.EXPLOSION_NORMAL);
}
ProviderUtility.scheduleSetBlocks(blocks, material, Particle.EXPLOSION_NORMAL);
return blocks;
}

public void removeBlocks(Shape shape) {
public void removeBlocks(Shape shape, boolean hollow) {
CubeDimensions cubeDimensions = new CubeDimensions(shape.getDimensions());
Location location = shape.getBukkitLocation();
ArrayList<Block> blocks = getRegionBlocks(cubeDimensions, location);
ArrayList<Block> blocks = getRegionBlocks(cubeDimensions, location, hollow);
ArrayList<Material> previousData = new ArrayList<Material>(shape.getBukkitPreviousData());
for (int i = 0; i < blocks.size(); i++) {
Block block = blocks.get(i);
Material material = previousData.get(i);
ProviderUtility.scheduleSetBlock(block, material, i);
ProviderUtility.scheduleSetBlock(block, material, 0);
}
}
}