Skip to content

Commit

Permalink
Add generation sound effect and particles (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
BONNe committed Oct 11, 2019
1 parent 9fdd483 commit 5a1f298
Showing 1 changed file with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package world.bentobox.magiccobblestonegenerator.listeners;


import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
Expand All @@ -10,6 +14,8 @@
import org.bukkit.event.block.BlockFromToEvent;


import java.util.Random;

import world.bentobox.magiccobblestonegenerator.StoneGeneratorAddon;


Expand Down Expand Up @@ -89,7 +95,14 @@ public void onBlockFromToEvent(BlockFromToEvent event)
if (this.canGenerateStone(liquid, eventToBlock))
{
// Return from here at any case. Even if could not manage to replace stone.
event.setCancelled(this.addon.getGenerator().isReplacementGenerated(eventToBlock, true));

if (this.addon.getGenerator().isReplacementGenerated(eventToBlock, true))
{
// sound when lava transforms to cobble
this.playEffects(eventToBlock);
event.setCancelled(true);
}

return;
}

Expand All @@ -102,7 +115,12 @@ public void onBlockFromToEvent(BlockFromToEvent event)
if (liquid.equals(Material.LAVA) && this.canLavaGenerateCobblestone(eventToBlock, event.getFace()))
{
// Lava is generating cobblestone into eventToBlock place
event.setCancelled(this.addon.getGenerator().isReplacementGenerated(eventToBlock));
if (this.addon.getGenerator().isReplacementGenerated(eventToBlock, true))
{
// sound when lava transforms to cobble
this.playEffects(eventToBlock);
event.setCancelled(true);
}
}
else if (liquid.equals(Material.WATER))
{
Expand All @@ -115,15 +133,54 @@ else if (liquid.equals(Material.WATER))
// Water flow should not be cancelled even if replacement is generated, as replacement block will
// never be in the flow block, as it will always be next block.

this.addon.getGenerator().isReplacementGenerated(replacedBlock);
event.setCancelled(false);
if (this.addon.getGenerator().isReplacementGenerated(replacedBlock, true))
{
// sound when lava transforms to cobble
this.playEffects(replacedBlock);
}
}
}

// End of process... no generation for you!!
}


/**
* This method plays sound effect and adds particles to new block.
* @param block block placement where particle must be generated.
*/
private void playEffects(Block block)
{
final double blockX = block.getX();
final double blockY = block.getY();
final double blockZ = block.getZ();

// Run everything in new task
Bukkit.getServer().getScheduler().runTask(this.addon.getPlugin(), () -> {
// Play sound for spawning block
block.getWorld().playSound(block.getLocation(),
Sound.BLOCK_FIRE_EXTINGUISH,
SoundCategory.BLOCKS,
0.5F,
2.6F + (this.random.nextFloat() - this.random.nextFloat()) * 0.8F);

// This spawns 8 large smoke particles.
for (int counter = 0; counter < 8; ++counter)
{
block.getWorld().spawnParticle(Particle.SMOKE_LARGE,
blockX + Math.random(),
blockY + 1 + Math.random(),
blockZ + Math.random(),
1,
0,
0,
0,
0);
}
});
}


// ---------------------------------------------------------------------
// Section: Private Methods
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -376,4 +433,9 @@ private boolean isFlowingLavaBlock(Block block)
* Main addon class.
*/
private StoneGeneratorAddon addon;

/**
* A bit of randomness
*/
public final Random random = new Random();
}

0 comments on commit 5a1f298

Please sign in to comment.