Skip to content

Commit

Permalink
Added support for ice
Browse files Browse the repository at this point in the history
  • Loading branch information
ceskyDJ committed Apr 28, 2020
1 parent 0eb66f2 commit 2e2445f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 41 deletions.
55 changes: 51 additions & 4 deletions src/com/leetzilantonis/netherwater/BlockBreakListener.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
package com.leetzilantonis.netherwater;

/**
* Created by Michal ŠMAHEL (ceskyDJ) on 4/28/20.
*/
public class BlockBreakListener {
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;

import java.util.Objects;

public class BlockBreakListener implements Listener {
private final NetherWater plugin;

public BlockBreakListener(NetherWater plugin) {
this.plugin = plugin;
}

@EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
this.plugin.dump("Block break event has been handled.");
this.plugin.dump("- World: " + Objects.requireNonNull(event.getBlock()).getWorld().getName() + " (type: " + event.getBlock().getWorld().getEnvironment() + ")");
this.plugin.dump("- Player: " + event.getPlayer());

Player player = event.getPlayer();

if (event.getBlock().getType() != Material.ICE) {
return;
}

// Silk touch has different behaviour
if (event.getPlayer().getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH)) {
return;
}

// Ice to water change doesn't apply to creative game players
if (player.getGameMode() == GameMode.CREATIVE) {
return;
}

// Check general conditions for using this plugin (world type, player permissions, world height etc.)
if (!plugin.canBeUsedThisPlugin(player, event.getBlock())) {
return;
}

// Cancel native event actions
event.setCancelled(true);

// Replace ice for watter block
event.getBlock().setType(Material.WATER);
}
}
50 changes: 41 additions & 9 deletions src/com/leetzilantonis/netherwater/NetherWater.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void onEnable() {
}

this.getServer().getPluginManager().registerEvents(new WaterPlaceListener(this), this);
this.getServer().getPluginManager().registerEvents(new BlockBreakListener(this), this);
this.getCommand("nwreload").setExecutor(new NWReloadCommand(this));

this.getLogger().info("Plugin loaded successfully");
Expand All @@ -53,14 +54,24 @@ private WorldGuardPlugin getWorldGuard() throws PluginNotFoundException {
return (WorldGuardPlugin) plugin;
}

public boolean canBuild(Player p, Block b) {
public ConfigManager getConfigManager() {
return this.configManager;
}

public void dump(String message) {
if (this.configManager.isDebugOn()) {
this.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[NetherWater] " + message);
}
}

public boolean canBuild(Player player, Block block) {
if (this.worldGuard == null) {
return true;
}

LocalPlayer wgPlayer = this.worldGuard.wrapPlayer(p);
LocalPlayer wgPlayer = this.worldGuard.wrapPlayer(player);
World weWorld = wgPlayer.getWorld();
Location location = new Location(weWorld, b.getX(), b.getY(), b.getZ());
Location location = new Location(weWorld, block.getX(), block.getY(), block.getZ());
RegionContainer regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionQuery regionQuery = regionContainer.createQuery();

Expand All @@ -69,13 +80,34 @@ public boolean canBuild(Player p, Block b) {
return canBypass || regionQuery.testState(location, wgPlayer, Flags.BUILD);
}

public ConfigManager getConfigManager() {
return this.configManager;
}
public boolean canBeUsedThisPlugin(Player player, Block block) {
org.bukkit.World world = block.getWorld();

public void dump(String message) {
if (this.configManager.isDebugOn()) {
this.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[NetherWater] " + message);
if (world.getEnvironment() != org.bukkit.World.Environment.NETHER) {
return false;
}

if (!(player.hasPermission("netherwater.use." + world.getName()) || player.hasPermission("netherwater.use.*"))) {
return false;
}

if (this.configManager.getDisabledWorlds().contains(world.getName()) && !player.hasPermission("netherwater.world.bypass")) {
return false;
}

if (!this.canBuild(player, block)) {
return false;
}

int y = block.getY();
if (y > this.configManager.getMaxHeight()) {
return false;
}

if (y < this.configManager.getMinHeight()) {
return false;
}

return true;
}
}
31 changes: 4 additions & 27 deletions src/com/leetzilantonis/netherwater/WaterPlaceListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -15,12 +14,9 @@

public class WaterPlaceListener implements Listener {
private final NetherWater plugin;
private final ConfigManager configManager;

public WaterPlaceListener(NetherWater plugin) {
this.plugin = plugin;

this.configManager = this.plugin.getConfigManager();
}

@EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = true)
Expand All @@ -39,34 +35,15 @@ public void onPlayerInteract(PlayerInteractEvent event) {
return;
}

World world = event.getClickedBlock().getWorld();
Player player = event.getPlayer();

if (world.getEnvironment() != Environment.NETHER) {
return;
}
Block selectedBlock = event.getClickedBlock().getRelative(event.getBlockFace());

if (event.getItem() == null || event.getItem().getType() != Material.WATER_BUCKET) {
return;
}

if (!(player.hasPermission("netherwater.use." + world.getName()) || player.hasPermission("netherwater.use.*"))) {
return;
}

if (this.configManager.getDisabledWorlds().contains(world.getName()) && !player.hasPermission("netherwater.world.bypass")) {
return;
}

if (!plugin.canBuild(player, event.getClickedBlock().getRelative(event.getBlockFace())))
return;

int y = event.getClickedBlock().getRelative(event.getBlockFace()).getY();
if (y > this.configManager.getMaxHeight()) {
return;
}

if (y < this.configManager.getMinHeight()) {
// Check general conditions for using this plugin (world type, player permissions, world height etc.)
if (!this.plugin.canBeUsedThisPlugin(player, selectedBlock)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: NetherWater
version: 1.0.6
version: 1.0.7
description: Allow players to use water in the nether
authors: [Lee Tzilantonis, ceskyDJ]

Expand Down

0 comments on commit 2e2445f

Please sign in to comment.