2012-05-10 16:32:25 +02:00
|
|
|
package com.Acrobot.ChestShop.Plugins;
|
|
|
|
|
2019-08-22 14:09:54 +02:00
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
2012-11-23 21:04:13 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Messages;
|
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2020-05-02 16:57:42 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
|
2012-06-08 15:28:36 +02:00
|
|
|
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;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Security;
|
2020-05-02 16:57:42 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2012-05-10 16:32:25 +02:00
|
|
|
import com.griefcraft.lwc.LWC;
|
|
|
|
import com.griefcraft.model.Protection;
|
2020-05-02 16:57:42 +02:00
|
|
|
import com.griefcraft.modules.limits.LimitsModule;
|
2020-05-04 23:33:34 +02:00
|
|
|
import com.griefcraft.modules.limits.LimitsV2;
|
2013-03-16 18:32:41 +01:00
|
|
|
import com.griefcraft.scripting.event.LWCProtectionRegisterEvent;
|
2020-02-15 18:10:18 +01:00
|
|
|
import com.griefcraft.scripting.event.LWCProtectionRegistrationPostEvent;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.block.Block;
|
2020-05-04 23:33:34 +02:00
|
|
|
import org.bukkit.block.BlockState;
|
2018-10-23 20:04:18 +02:00
|
|
|
import org.bukkit.block.Container;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.block.Sign;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.Event;
|
|
|
|
import org.bukkit.event.EventHandler;
|
2020-09-24 17:54:36 +02:00
|
|
|
import org.bukkit.event.EventPriority;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
|
2020-05-04 23:33:34 +02:00
|
|
|
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.OTHER_BREAK;
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class LightweightChestProtection implements Listener {
|
2020-05-02 16:57:42 +02:00
|
|
|
private final LWC lwc;
|
|
|
|
private final LimitsModule limitsModule;
|
2020-05-04 23:33:34 +02:00
|
|
|
private final LimitsV2 limitsV2;
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2012-06-25 17:16:24 +02:00
|
|
|
public LightweightChestProtection() {
|
|
|
|
this.lwc = LWC.getInstance();
|
2020-05-02 16:57:42 +02:00
|
|
|
this.limitsModule = (LimitsModule) lwc.getModuleLoader().getModule(LimitsModule.class);
|
2020-05-04 23:33:34 +02:00
|
|
|
this.limitsV2 = (LimitsV2) lwc.getModuleLoader().getModule(LimitsV2.class);
|
2020-01-15 22:55:40 +01:00
|
|
|
try {
|
|
|
|
if (Properties.PROTECT_SIGN_WITH_LWC)
|
|
|
|
Protection.Type.valueOf(Properties.LWC_SIGN_PROTECTION_TYPE.name());
|
|
|
|
if (Properties.PROTECT_CHEST_WITH_LWC)
|
|
|
|
Protection.Type.valueOf(Properties.LWC_CHEST_PROTECTION_TYPE.name());
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
ChestShop.getBukkitLogger().warning("Your installed LWC version doesn't seem to support the configured protection type! " + e.getMessage());
|
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 16:57:42 +02:00
|
|
|
@EventHandler(ignoreCancelled = true)
|
|
|
|
public void onPreShopCreation(PreShopCreationEvent event) {
|
|
|
|
if (Properties.LWC_LIMITS_BLOCK_CREATION) {
|
|
|
|
if (Properties.PROTECT_SIGN_WITH_LWC) {
|
2020-05-04 23:33:34 +02:00
|
|
|
if (isAtLimit(event.getPlayer(), event.getSign())) {
|
|
|
|
event.setOutcome(OTHER_BREAK);
|
2020-05-02 16:57:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Properties.PROTECT_CHEST_WITH_LWC) {
|
|
|
|
Container container = uBlock.findConnectedContainer(event.getSign());
|
2020-05-04 23:33:34 +02:00
|
|
|
if (container != null && isAtLimit(event.getPlayer(), container)) {
|
|
|
|
event.setOutcome(OTHER_BREAK);
|
2020-05-02 16:57:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:33:34 +02:00
|
|
|
private boolean isAtLimit(Player player, BlockState blockState) {
|
|
|
|
LWCProtectionRegisterEvent protectionEvent = new LWCProtectionRegisterEvent(player, blockState.getBlock());
|
|
|
|
limitsModule.onRegisterProtection(protectionEvent);
|
|
|
|
limitsV2.onRegisterProtection(protectionEvent);
|
|
|
|
return protectionEvent.isCancelled();
|
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
@EventHandler
|
|
|
|
public static void onShopCreation(ShopCreatedEvent event) {
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
Sign sign = event.getSign();
|
2018-10-23 20:04:18 +02:00
|
|
|
Container connectedContainer = event.getContainer();
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2020-06-28 19:00:09 +02:00
|
|
|
Messages.Message message = null;
|
2012-11-23 21:04:13 +01:00
|
|
|
if (Properties.PROTECT_SIGN_WITH_LWC) {
|
2020-02-15 18:20:55 +01:00
|
|
|
if (Security.protect(player, sign.getBlock(), event.getOwnerAccount() != null ? event.getOwnerAccount().getUuid() : player.getUniqueId(), Properties.LWC_SIGN_PROTECTION_TYPE)) {
|
|
|
|
message = Messages.PROTECTED_SHOP_SIGN;
|
|
|
|
} else {
|
|
|
|
message = Messages.NOT_ENOUGH_PROTECTIONS;
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:20:55 +01:00
|
|
|
if (Properties.PROTECT_CHEST_WITH_LWC && connectedContainer != null) {
|
|
|
|
if (Security.protect(player, connectedContainer.getBlock(), event.getOwnerAccount() != null ? event.getOwnerAccount().getUuid() : player.getUniqueId(), Properties.LWC_CHEST_PROTECTION_TYPE)) {
|
|
|
|
message = Messages.PROTECTED_SHOP;
|
|
|
|
} else if (message == null) {
|
|
|
|
message = Messages.NOT_ENOUGH_PROTECTIONS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message != null) {
|
2020-06-28 19:00:09 +02:00
|
|
|
message.sendWithPrefix(player);
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:54:36 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGH)
|
2012-05-10 16:32:25 +02:00
|
|
|
public void onProtectionCheck(ProtectionCheckEvent event) {
|
2020-09-24 17:54:36 +02:00
|
|
|
if (event.getResult() == Event.Result.DENY && !Properties.TURN_OFF_DEFAULT_PROTECTION_WHEN_PROTECTED_EXTERNALLY) {
|
2012-05-10 16:32:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Block block = event.getBlock();
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
|
|
|
Protection protection = lwc.findProtection(block);
|
|
|
|
|
|
|
|
if (protection == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-16 01:15:59 +02:00
|
|
|
if (!lwc.canAccessProtection(player, protection) && !protection.getType().name().equals("DONATION") && !protection.getType().name().equals("DISPLAY")) {
|
2012-05-10 16:32:25 +02:00
|
|
|
event.setResult(Event.Result.DENY);
|
2020-09-24 17:54:36 +02:00
|
|
|
} else if (Properties.TURN_OFF_DEFAULT_PROTECTION_WHEN_PROTECTED_EXTERNALLY) {
|
|
|
|
event.setResult(Event.Result.ALLOW);
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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();
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2013-03-16 18:32:41 +01:00
|
|
|
if (player == null) {
|
2012-05-10 16:32:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = block.getX();
|
|
|
|
int y = block.getY();
|
|
|
|
int z = block.getZ();
|
|
|
|
|
|
|
|
String worldName = block.getWorld().getName();
|
|
|
|
|
2012-12-31 02:46:42 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-15 22:55:40 +01:00
|
|
|
Protection.Type type = Protection.Type.PRIVATE;
|
|
|
|
switch (event.getType()) {
|
|
|
|
case PUBLIC:
|
|
|
|
type = Protection.Type.PUBLIC;
|
|
|
|
break;
|
|
|
|
case DONATION:
|
|
|
|
type = Protection.Type.DONATION;
|
|
|
|
break;
|
|
|
|
case DISPLAY:
|
|
|
|
try {
|
|
|
|
type = Protection.Type.valueOf("DISPLAY");
|
|
|
|
} catch (IllegalArgumentException ignored) {}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-22 14:09:54 +02:00
|
|
|
Protection protection = null;
|
|
|
|
try {
|
2020-05-04 23:33:34 +02:00
|
|
|
int blockId = com.griefcraft.cache.BlockCache.getInstance().getBlockId(block);
|
|
|
|
if (blockId < 0) {
|
|
|
|
return;
|
2019-08-22 14:09:54 +02:00
|
|
|
}
|
2020-05-04 23:33:34 +02:00
|
|
|
protection = lwc.getPhysicalDatabase().registerProtection(blockId, type, worldName, event.getProtectionOwner().toString(), "", x, y, z);
|
|
|
|
} catch (LinkageError e) {
|
|
|
|
ChestShop.getBukkitLogger().warning(
|
|
|
|
"Incompatible LWC version installed! (" + lwc.getPlugin().getName() + " v" + lwc.getVersion() + ") \n" +
|
|
|
|
"Block cache/type id error: " + e.getMessage()
|
|
|
|
);
|
2019-08-22 14:09:54 +02:00
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
if (protection != null) {
|
|
|
|
event.setProtected(true);
|
2020-02-15 18:10:18 +01:00
|
|
|
protection.removeCache();
|
|
|
|
lwc.getProtectionCache().addProtection(protection);
|
|
|
|
lwc.getModuleLoader().dispatchEvent(new LWCProtectionRegistrationPostEvent(protection));
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2018-10-23 20:04:18 +02:00
|
|
|
if (event.getContainer() == null || !Properties.REMOVE_LWC_PROTECTION_AUTOMATICALLY) {
|
2013-10-27 16:49:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-23 20:04:18 +02:00
|
|
|
Protection chestProtection = lwc.findProtection(event.getContainer().getBlock());
|
2013-10-27 16:49:48 +01:00
|
|
|
|
|
|
|
if (chestProtection != null) {
|
|
|
|
chestProtection.remove();
|
|
|
|
}
|
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|