ChestShop-3/src/main/java/com/Acrobot/ChestShop/Plugins/LightweightChestProtection.java

129 lines
3.9 KiB
Java
Raw Normal View History

package com.Acrobot.ChestShop.Plugins;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.Protection.ProtectBlockEvent;
import com.Acrobot.ChestShop.Events.Protection.ProtectionCheckEvent;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
2013-10-27 16:49:48 +01:00
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Security;
import com.griefcraft.lwc.LWC;
import com.griefcraft.model.Protection;
2013-03-16 18:32:41 +01:00
import com.griefcraft.scripting.event.LWCProtectionRegisterEvent;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
/**
* @author Acrobot
*/
public class LightweightChestProtection implements Listener {
private LWC lwc;
2012-06-25 17:16:24 +02:00
public LightweightChestProtection() {
this.lwc = LWC.getInstance();
}
@EventHandler
public static void onShopCreation(ShopCreatedEvent event) {
Player player = event.getPlayer();
Sign sign = event.getSign();
Container connectedContainer = event.getContainer();
if (Properties.PROTECT_SIGN_WITH_LWC) {
2014-04-12 14:22:34 +02:00
if (!Security.protect(player, sign.getBlock())) {
player.sendMessage(Messages.prefix(Messages.NOT_ENOUGH_PROTECTIONS));
}
}
if (Properties.PROTECT_CHEST_WITH_LWC && connectedContainer != null && Security.protect(player, connectedContainer.getBlock())) {
player.sendMessage(Messages.prefix(Messages.PROTECTED_SHOP));
}
}
@EventHandler
public void onProtectionCheck(ProtectionCheckEvent event) {
if (event.getResult() == Event.Result.DENY) {
return;
}
Block block = event.getBlock();
Player player = event.getPlayer();
Protection protection = lwc.findProtection(block);
if (protection == null) {
return;
}
if (!lwc.canAccessProtection(player, protection) || protection.getType() == Protection.Type.DONATION) {
event.setResult(Event.Result.DENY);
}
}
@EventHandler
public void onBlockProtect(ProtectBlockEvent event) {
if (event.isProtected()) {
return;
}
Block block = event.getBlock();
2014-04-12 14:22:34 +02:00
Player player = event.getPlayer();
2013-03-16 18:32:41 +01:00
if (player == null) {
return;
}
int x = block.getX();
int y = block.getY();
int z = block.getZ();
String worldName = block.getWorld().getName();
Protection existingProtection = lwc.getPhysicalDatabase().loadProtection(worldName, x, y, z);
if (existingProtection != null) {
event.setProtected(true);
return;
}
2013-03-16 18:32:41 +01:00
LWCProtectionRegisterEvent protectionEvent = new LWCProtectionRegisterEvent(player, block);
lwc.getModuleLoader().dispatchEvent(protectionEvent);
if (protectionEvent.isCancelled()) {
return;
}
// TODO: Update to new API once LWC is updated
Protection protection = lwc.getPhysicalDatabase().registerProtection(block.getType().getId(), Protection.Type.PRIVATE, worldName, player.getUniqueId().toString(), "", x, y, z);
if (protection != null) {
event.setProtected(true);
}
}
2013-10-27 16:49:48 +01:00
@EventHandler
public void onShopRemove(ShopDestroyedEvent event) {
Protection signProtection = lwc.findProtection(event.getSign().getBlock());
if (signProtection != null) {
signProtection.remove();
}
if (event.getContainer() == null || !Properties.REMOVE_LWC_PROTECTION_AUTOMATICALLY) {
2013-10-27 16:49:48 +01:00
return;
}
Protection chestProtection = lwc.findProtection(event.getContainer().getBlock());
2013-10-27 16:49:48 +01:00
if (chestProtection != null) {
chestProtection.remove();
}
}
}