Skip to content

Instantly share code, notes, and snippets.

@J0B10
Last active June 11, 2018 14:27
Show Gist options
  • Save J0B10/e73b194a099ddff5568899a2e0bb8461 to your computer and use it in GitHub Desktop.
Save J0B10/e73b194a099ddff5568899a2e0bb8461 to your computer and use it in GitHub Desktop.
Prevents generation of any blocks by lava and water merging
/**
* Copyright 2018 Jonas Blocher
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.player.PlayerInteractEvent;
/**
* Created on 22.04.2018.
*
* @author Jonas Blocher
*/
public class ObsidianPreventer implements Listener {
@EventHandler
public void onPlaceFluid(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
Material type;
if (event.getItem() == null) return;
if (event.getItem().getType() == Material.WATER_BUCKET) {
type = Material.WATER;
} else if (event.getItem().getType() == Material.LAVA_BUCKET) {
type = Material.LAVA;
} else return;
Block block = event.getClickedBlock().getRelative(event.getBlockFace());
if (generates(type, block)) {
event.setCancelled(true);
return;
} else {
BlockFace[] nesw = {BlockFace.DOWN, BlockFace.UP, BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST};
for (BlockFace face : nesw) {
if (generates(type, block.getRelative(face))) {
System.out.println(face);
event.setCancelled(true);
return;
}
}
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void disableLavaWaterBlockCreation(BlockFromToEvent event) {
if (event.isCancelled()) return;
if (!event.getBlock().isLiquid()) return;
Material type = event.getBlock().getType();
Block to = event.getToBlock();
if (generates(event.getBlock(), to)) {
event.setCancelled(true);
return;
} else {
BlockFace[] nesw = {BlockFace.DOWN, BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST};
for (BlockFace face : nesw) {
if (generates(event.getBlock(), to.getRelative(face))) {
event.setCancelled(true);
return;
}
}
}
}
private boolean generates(Block from, Block to) {
if (!to.isLiquid()) return false;
return generates(from.getType(), to.getType());
}
private boolean generates(Material from, Block to) {
if (!to.isLiquid()) return false;
return generates(from, to.getType());
}
private boolean generates(Material from, Material to) {
if (from == Material.STATIONARY_WATER) from = Material.WATER;
else if (from == Material.STATIONARY_LAVA) from = Material.LAVA;
if (to == Material.STATIONARY_WATER) to = Material.WATER;
else if (to == Material.STATIONARY_LAVA) to = Material.LAVA;
return from != to;
}
}
@J0B10
Copy link
Author

J0B10 commented Jun 11, 2018

The file was coded to get this working as fast as possible, therfore it's a bit messy. I'm sorry for that.
Maybe at some point I'll rework it and add some comments but for now it just works^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment