Advanced-Portals/src/main/java/com/sekwah/advancedportals/listeners/PortalProtect.java

76 lines
2.8 KiB
Java
Raw Normal View History

package com.sekwah.advancedportals.listeners;
import com.sekwah.advancedportals.AdvancedPortalsPlugin;
import com.sekwah.advancedportals.ConfigAccessor;
import com.sekwah.advancedportals.PluginMessages;
import com.sekwah.advancedportals.portals.Portal;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
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 org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.List;
public class PortalProtect implements Listener {
2016-03-29 13:38:03 +02:00
@SuppressWarnings("unused")
private final AdvancedPortalsPlugin plugin;
2016-03-29 13:38:03 +02:00
// The needed config values will be stored so they are easier to access later
// an example is in the interact event in this if statement if((!UseOnlyServerAxe || event.getItem().getItemMeta().getDisplayName().equals("<22>eP...
private boolean PortalProtect = true;
private int PortalProtectionRadius = 5;
public PortalProtect(AdvancedPortalsPlugin plugin) {
this.plugin = plugin;
2016-03-29 13:38:03 +02:00
ConfigAccessor config = new ConfigAccessor(plugin, "config.yml");
this.PortalProtect = config.getConfig().getBoolean("PortalProtection");
2016-03-29 13:38:03 +02:00
if (PortalProtect) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
}
@EventHandler(priority = EventPriority.HIGH)
2016-03-29 13:38:03 +02:00
public void onBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
if (!player.hasPermission("advancedportals.build")
&& Portal.inPortalRegion(event.getBlock().getLocation(), PortalProtectionRadius)) {
2016-08-01 08:45:24 +02:00
event.setCancelled(true);
player.sendMessage(PluginMessages.customPrefixFail + " You don't have permission to build here!");
}
}
@EventHandler(priority = EventPriority.HIGH)
2016-03-29 13:38:03 +02:00
public void onBlockPlace(BlockPlaceEvent event) {
Player player = event.getPlayer();
if (!player.hasPermission("advancedportals.build")
&& Portal.inPortalRegion(event.getBlock().getLocation(), PortalProtectionRadius)) {
2016-08-01 08:45:24 +02:00
event.setCancelled(true);
player.sendMessage(PluginMessages.customPrefixFail + " You don't have permission to build here!");
}
}
2016-03-29 13:38:03 +02:00
@EventHandler(priority = EventPriority.HIGH)
2016-03-29 13:38:03 +02:00
public void onExplosion(EntityExplodeEvent event) {
List<Block> blockList = event.blockList();
for (int i = 0; i < blockList.size(); i++) {
Block block = blockList.get(i);
2016-08-01 08:45:24 +02:00
if (Portal.inPortalRegion(block.getLocation(), PortalProtectionRadius)) {
blockList.remove(i);
i--;
2016-03-29 13:38:03 +02:00
}
}
}
}