Skip to content

Commit 0c4e58d

Browse files
committed
Prevent creating POS in protected land
1 parent 02eaa42 commit 0c4e58d

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/main/java/pro/cloudnode/smp/bankaccounts/BankConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,11 @@ public int invoiceNotifyInterval() {
650650
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.pos-create-business-only")));
651651
}
652652

653+
// messages.errors.pos-protected-land
654+
public @NotNull Component messagesErrorsPosProtectedLand() {
655+
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.pos-protected-land")));
656+
}
657+
653658
// messages.errors.disallowed-characters
654659
public @NotNull Component messagesErrorsDisallowedCharacters(final @NotNull Set<@NotNull String> characters) {
655660
return MiniMessage.miniMessage().deserialize(

src/main/java/pro/cloudnode/smp/bankaccounts/commands/POSCommand.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package pro.cloudnode.smp.bankaccounts.commands;
22

33
import org.bukkit.block.Block;
4+
import org.bukkit.block.BlockFace;
45
import org.bukkit.block.BlockState;
56
import org.bukkit.block.Chest;
67
import org.bukkit.command.CommandSender;
78
import org.bukkit.entity.Player;
9+
import org.bukkit.event.Event;
10+
import org.bukkit.event.block.Action;
11+
import org.bukkit.event.player.PlayerInteractEvent;
812
import org.bukkit.inventory.DoubleChestInventory;
913
import org.jetbrains.annotations.NotNull;
1014
import org.jetbrains.annotations.Nullable;
@@ -19,6 +23,7 @@
1923
import java.util.ArrayList;
2024
import java.util.Arrays;
2125
import java.util.Date;
26+
import java.util.List;
2227
import java.util.Optional;
2328
import java.util.Set;
2429

@@ -71,6 +76,8 @@ public boolean execute(final @NotNull CommandSender sender, final @NotNull Strin
7176
if (block.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAsyncFailed());
7277
if (!(block.get() instanceof final @NotNull Chest chest))
7378
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsPosNotChest());
79+
if (!canOpenChest(player, chest))
80+
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsPosProtectedLand());
7481
if (chest.getInventory() instanceof DoubleChestInventory)
7582
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsPosDoubleChest());
7683
if (chest.getInventory().isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsPosEmpty());
@@ -88,6 +95,31 @@ public boolean execute(final @NotNull CommandSender sender, final @NotNull Strin
8895
return sendMessage(sender, BankAccounts.getInstance().config().messagesPosCreated(pos));
8996
}
9097

98+
private static boolean canOpenChest(final @NotNull Player player, final @NotNull Chest chest) {
99+
return BankAccounts.runOnMain(() -> {
100+
final @NotNull Block block = chest.getBlock();
101+
102+
final @NotNull PlayerInteractEvent event = new PlayerInteractEvent(
103+
player,
104+
Action.RIGHT_CLICK_BLOCK,
105+
player.getInventory().getItemInMainHand(),
106+
block,
107+
getTargetedFace(player).orElse(BlockFace.NORTH)
108+
);
109+
110+
BankAccounts.getInstance().getServer().getPluginManager().callEvent(event);
111+
return event.useInteractedBlock() != Event.Result.DENY;
112+
}).orElse(false);
113+
}
114+
115+
private static @NotNull Optional<@NotNull BlockFace> getTargetedFace(final @NotNull Player player) {
116+
final @NotNull List<@NotNull Block> lastTwoTargetBlocks = player.getLastTwoTargetBlocks(null, 5);
117+
if (lastTwoTargetBlocks.size() != 2 || !lastTwoTargetBlocks.get(1).getType().isOccluding()) return Optional.empty();
118+
final @NotNull Block targetBlock = lastTwoTargetBlocks.get(1);
119+
final @NotNull Block adjacentBlock = lastTwoTargetBlocks.get(0);
120+
return Optional.ofNullable(targetBlock.getFace(adjacentBlock));
121+
}
122+
91123
@Override
92124
public @NotNull ArrayList<@NotNull String> tab(final @NotNull CommandSender sender, final @NotNull String @NotNull [] args) {
93125
final @NotNull ArrayList<@NotNull String> suggestions = new ArrayList<>();

src/main/resources/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ messages:
352352
no-card: "<red>(!) You must hold your bank card to use this.</red>"
353353
pos-items-changed: "<red>(!) The items in the chest have changed. POS cancelled.</red>"
354354
pos-create-business-only: "<red>(!) You can only create a POS with a business account.</red>"
355+
# Cannot create POS due to land protection
356+
pos-protected-land: "<red>(!) You don't own this chest; you cannot create a POS here."
355357
# Provided string includes disallowed characters
356358
# Placeholder: <characters> - the disallowed characters
357359
disallowed-characters: "<red>(!) The provided string contains disallowed characters: <gray><characters></gray></red>"

0 commit comments

Comments
 (0)