Skip to content

Commit

Permalink
Added an option to not animate the snow falling for performance reasons.
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Mar 4, 2018
1 parent 2cf85b8 commit 2a3647d
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/main/java/com/sk89q/craftbook/mechanics/Snow.java
Expand Up @@ -189,7 +189,7 @@ public void onBlockBreak(BlockBreakEvent event) {

if(realistic) {
BlockFace[] faces = new BlockFace[]{BlockFace.UP, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST};
for(BlockFace dir : faces) Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(event.getBlock().getRelative(dir), 0, event.getBlock().getLocation().toVector().toBlockVector()), fallAnimationSpeed);
for(BlockFace dir : faces) Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(event.getBlock().getRelative(dir), 0, event.getBlock().getLocation().toVector().toBlockVector()), animationTicks);
}
}
}
Expand All @@ -202,7 +202,7 @@ public void onPhysicsUpdate(BlockPhysicsEvent event) {
if(!EventUtil.passesFilter(event))
return;
if((event.getBlock().getType() == Material.SNOW || event.getBlock().getType() == Material.SNOW_BLOCK) && CraftBookPlugin.inst().getRandom().nextInt(10) == 0)
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(event.getBlock(), 0), fallAnimationSpeed);
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(event.getBlock(), 0), animationTicks);
}

@EventHandler(priority = EventPriority.HIGH)
Expand Down Expand Up @@ -250,13 +250,13 @@ public void run () {

if(highest.getWorld().hasStorm() && highest.getType() != Material.ICE) {
if(highest.getTemperature() < 0.15) {
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(highest, 1), fallAnimationSpeed);
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(highest, 1), animationTicks);
}
} else if(meltMode) {
if(highest.getType() == Material.SNOW && meltPartial)
if(highest.getData() == 0) return;
if(highest.getTemperature() > 0.05)
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(highest, -1), fallAnimationSpeed);
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(highest, -1), animationTicks);
}
}

Expand Down Expand Up @@ -294,7 +294,6 @@ private class SnowHandler implements Runnable {

@Override
public void run () {

if(snowblock == null || !queue.contains(snowblock)) return; //Something went wrong here.

queue.remove(snowblock);
Expand All @@ -307,12 +306,12 @@ public void run () {
if(decreaseSnow(block, true))
amount++;
if(amount < 0)
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(block, amount), fallAnimationSpeed);
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(block, amount), animationTicks);
} else {
if(increaseSnow(block, realistic))
amount--;
if(amount > 0)
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(block, amount), fallAnimationSpeed);
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(block, amount), animationTicks);
}
}

Expand Down Expand Up @@ -340,7 +339,8 @@ boolean disperse(Block snow) {
if(queue.contains(new SnowBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ()))) continue;
if(block.getType() == Material.SNOW && snow.getType() == Material.SNOW && block.getData() >= snow.getData() && dir != BlockFace.DOWN && dir != BlockFace.UP) {
if(block.getData() > snow.getData()+1) {
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(block, 0, snow.getLocation().toVector().toBlockVector()), fallAnimationSpeed);
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(block, 0, snow.getLocation().toVector().toBlockVector()),
animationTicks);
}
continue;
}
Expand Down Expand Up @@ -381,7 +381,13 @@ boolean disperse(Block snow) {
}

boolean increaseSnow(Block snow, boolean disperse) {
if (!animateFalling) {
while (snow.getRelative(0, -1, 0).getType() == Material.AIR) {
snow = snow.getRelative(0, -1, 0);
}
}
Block below = snow.getRelative(0, -1, 0);

if(below.getType() != Material.AIR && isReplacable(below)) {
if(below.getType() != Material.SNOW || below.getData() < 0x7)
return increaseSnow(below, disperse);
Expand Down Expand Up @@ -416,7 +422,7 @@ boolean increaseSnow(Block snow, boolean disperse) {
if(isReplacable(snow)) {
snow.setTypeIdAndData(Material.SNOW.getId(), (byte) 0, false);
if(disperse)
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(snow, 0, from), fallAnimationSpeed);
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(snow, 0, from), animationTicks);
return true;
} else
return false;
Expand All @@ -434,7 +440,7 @@ boolean increaseSnow(Block snow, boolean disperse) {
if(allowed) {
snow.setType(Material.SNOW_BLOCK);
if(disperse)
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(snow, 0, from), fallAnimationSpeed);
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(snow, 0, from), animationTicks);
return true;
} else
return false;
Expand All @@ -444,7 +450,7 @@ boolean increaseSnow(Block snow, boolean disperse) {
snow.setData(data, false);

if(disperse)
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(snow, 0, from), fallAnimationSpeed);
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(snow, 0, from), animationTicks);

return true;
}
Expand Down Expand Up @@ -473,7 +479,8 @@ else if(snow.getType() == Material.SNOW_BLOCK)

if(disperse && realistic) {
BlockFace[] faces = new BlockFace[]{BlockFace.UP, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST};
for(BlockFace dir : faces) Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(snow.getRelative(dir), 0, snow.getLocation().toVector().toBlockVector()), fallAnimationSpeed);
for(BlockFace dir : faces) Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(snow.getRelative(dir), 0, snow.getLocation().toVector().toBlockVector()),
animationTicks);
}

return true;
Expand Down Expand Up @@ -519,10 +526,11 @@ public int hashCode() {
private int maxPileHeight;
private boolean jumpTrample;
private List<ItemInfo> realisticReplacables;
private int fallAnimationSpeed;
private int animationTicks;
private boolean freezeWater;
private boolean meltSunlight;
private boolean meltPartial;
private boolean animateFalling;

@Override
public void loadConfiguration (YAMLProcessor config, String path) {
Expand Down Expand Up @@ -557,8 +565,11 @@ public void loadConfiguration (YAMLProcessor config, String path) {
config.setComment(path + "replacable-blocks", "A list of blocks that can be replaced by realistic snow.");
realisticReplacables = ItemInfo.parseListFromString(config.getStringList(path + "replacable-blocks", Arrays.asList("DEAD_BUSH", "LONG_GRASS", "YELLOW_FLOWER", "RED_ROSE", "BROWN_MUSHROOM", "RED_MUSHROOM", "FIRE")));

config.setComment(path + "falldown-animation-speed", "The fall animation speed of realistic snow.");
fallAnimationSpeed = config.getInt(path + "falldown-animation-speed", 5);
config.setComment(path + "animate-falling", "Cause the snow to fall slowly (May be intensive).");
animateFalling = config.getBoolean(path + "animate-falling", false);

config.setComment(path + "falldown-animation-speed", "The animation delay of all snow interactions in ticks.");
animationTicks = config.getInt(path + "falldown-animation-speed", 5);

config.setComment(path + "freeze-water", "Should snow freeze water?");
freezeWater = config.getBoolean(path + "freeze-water", false);
Expand Down

0 comments on commit 2a3647d

Please sign in to comment.