Created
July 3, 2014 23:51
-
-
Save NeatMonster/d5286e439bad103f4256 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.neatmonster.fluids; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import org.bukkit.Material; | |
import org.bukkit.block.Block; | |
import org.bukkit.block.BlockFace; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.block.Action; | |
import org.bukkit.event.player.PlayerInteractEvent; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class Fluids extends JavaPlugin implements Listener { | |
private static final List<BlockFace> FACES = Arrays.asList(BlockFace.DOWN, BlockFace.EAST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST); | |
private static final List<Material> LAVA = Arrays.asList(Material.STATIONARY_LAVA, Material.LAVA); | |
private static final List<Material> WATER = Arrays.asList(Material.STATIONARY_WATER, Material.WATER); | |
private static void collectFluid(final Block anchor, final Map<Block, Material> collected, final List<Material> fluid) { | |
if (fluid.contains(anchor.getType()) && !collected.keySet().contains(anchor)) { | |
collected.put(anchor, anchor.getType()); | |
for (final BlockFace face : FACES) | |
collectFluid(anchor.getRelative(face), collected, fluid); | |
} | |
} | |
private static Material getOpposite(final Material fluid) { | |
switch (fluid) { | |
case LAVA: | |
return Material.WATER; | |
case STATIONARY_LAVA: | |
return Material.STATIONARY_WATER; | |
case STATIONARY_WATER: | |
return Material.STATIONARY_LAVA; | |
case WATER: | |
return Material.LAVA; | |
default: | |
return Material.AIR; | |
} | |
} | |
@Override | |
public void onEnable() { | |
getServer().getPluginManager().registerEvents(this, this); | |
} | |
@EventHandler(ignoreCancelled = true) | |
public void onPlayerInteract(final PlayerInteractEvent event) { | |
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getMaterial() == Material.DAYLIGHT_DETECTOR) { | |
final Block anchor = event.getClickedBlock().getRelative(event.getBlockFace()); | |
if (anchor.getType() == Material.STATIONARY_LAVA || anchor.getType() == Material.STATIONARY_WATER) { | |
final Map<Block, Material> fluidBlocks = new HashMap<Block, Material>(); | |
collectFluid(anchor, fluidBlocks, anchor.getType() == Material.STATIONARY_LAVA ? LAVA : WATER); | |
for (final Block fluidBlock : fluidBlocks.keySet()) | |
fluidBlock.setType(Material.AIR); | |
for (final Entry<Block, Material> fluidBlock : fluidBlocks.entrySet()) | |
fluidBlock.getKey().setType(getOpposite(fluidBlock.getValue())); | |
event.setCancelled(true); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Fluids | |
version: 1.0 | |
author: NeatMonster | |
main: fr.neatmonster.fluids.Fluids |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment