2012-05-10 16:32:25 +02:00
|
|
|
package com.Acrobot.ChestShop;
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.BlockUtil;
|
2012-11-23 21:04:13 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2017-07-01 18:14:41 +02:00
|
|
|
import com.Acrobot.ChestShop.Database.Account;
|
2019-05-07 00:49:16 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
|
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.Signs.ChestShopSign;
|
2018-10-23 20:04:18 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2019-05-07 00:49:16 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.BlockFace;
|
|
|
|
import org.bukkit.block.Sign;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.Event;
|
|
|
|
|
2019-08-22 15:03:27 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2021-01-28 17:18:42 +01:00
|
|
|
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState;
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class Security {
|
2012-06-08 15:28:36 +02:00
|
|
|
private static final BlockFace[] SIGN_CONNECTION_FACES = {BlockFace.UP, BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH};
|
|
|
|
private static final BlockFace[] BLOCKS_AROUND = {BlockFace.UP, BlockFace.DOWN, BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH};
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2014-04-12 14:22:34 +02:00
|
|
|
public static boolean protect(Player player, Block block) {
|
2019-08-22 15:03:27 +02:00
|
|
|
return protect(player, block, player.getUniqueId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean protect(Player player, Block block, UUID protectionOwner) {
|
2020-01-15 22:55:40 +01:00
|
|
|
return protect(player, block, protectionOwner, Type.PRIVATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean protect(Player player, Block block, UUID protectionOwner, Type type) {
|
|
|
|
ProtectBlockEvent event = new ProtectBlockEvent(block, player, protectionOwner, type);
|
2012-05-10 16:32:25 +02:00
|
|
|
ChestShop.callEvent(event);
|
|
|
|
|
|
|
|
return event.isProtected();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canAccess(Player player, Block block) {
|
2012-09-12 12:35:48 +02:00
|
|
|
return canAccess(player, block, false);
|
|
|
|
}
|
|
|
|
|
2014-04-12 13:28:29 +02:00
|
|
|
public static boolean canAccess(Player player, Block block, boolean ignoreDefaultProtection) {
|
|
|
|
ProtectionCheckEvent event = new ProtectionCheckEvent(block, player, ignoreDefaultProtection);
|
2012-05-10 16:32:25 +02:00
|
|
|
ChestShop.callEvent(event);
|
|
|
|
|
|
|
|
return event.getResult() != Event.Result.DENY;
|
|
|
|
}
|
|
|
|
|
2014-04-12 13:28:29 +02:00
|
|
|
public static boolean canPlaceSign(Player player, Sign sign) {
|
|
|
|
Block baseBlock = BlockUtil.getAttachedBlock(sign);
|
|
|
|
|
|
|
|
if (!Properties.ALLOW_MULTIPLE_SHOPS_AT_ONE_BLOCK && anotherShopFound(baseBlock, sign.getBlock(), player)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return canBePlaced(player, sign.getBlock());
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
|
2014-04-12 13:28:29 +02:00
|
|
|
private static boolean canBePlaced(Player player, Block sign) {
|
2012-06-08 15:28:36 +02:00
|
|
|
for (BlockFace face : BLOCKS_AROUND) {
|
2014-04-12 13:28:29 +02:00
|
|
|
Block block = sign.getRelative(face);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2018-10-23 20:04:18 +02:00
|
|
|
if (!uBlock.couldBeShopContainer(block)) {
|
2012-05-10 16:32:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!canAccess(player, block)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-12 13:28:29 +02:00
|
|
|
private static boolean anotherShopFound(Block baseBlock, Block signBlock, Player player) {
|
|
|
|
for (BlockFace face : SIGN_CONNECTION_FACES) {
|
|
|
|
Block block = baseBlock.getRelative(face);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2014-04-12 13:28:29 +02:00
|
|
|
if (block.equals(signBlock) || !BlockUtil.isSign(block)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2021-01-28 17:18:42 +01:00
|
|
|
Sign sign = (Sign) getState(block, false);
|
2014-04-12 13:28:29 +02:00
|
|
|
|
|
|
|
if (!ChestShopSign.isValid(sign) || !BlockUtil.getAttachedBlock(sign).equals(baseBlock)) {
|
2012-06-08 15:28:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2019-05-07 00:49:16 +02:00
|
|
|
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(sign.getLine(ChestShopSign.NAME_LINE));
|
|
|
|
Bukkit.getPluginManager().callEvent(accountQueryEvent);
|
|
|
|
Account account = accountQueryEvent.getAccount();
|
2017-07-01 18:14:41 +02:00
|
|
|
if (account != null && !account.getUuid().equals(player.getUniqueId())) {
|
2012-05-10 16:32:25 +02:00
|
|
|
return true;
|
2014-04-12 13:28:29 +02:00
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-15 22:55:40 +01:00
|
|
|
|
|
|
|
public enum Type {
|
|
|
|
PUBLIC,
|
|
|
|
PRIVATE,
|
|
|
|
DONATION,
|
|
|
|
DISPLAY
|
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|