-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent creating POS in protected land (#150)
- Loading branch information
Showing
4 changed files
with
96 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/pro/cloudnode/smp/bankaccounts/events/POSOpen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package pro.cloudnode.smp.bankaccounts.events; | ||
|
||
import org.bukkit.block.Block; | ||
import org.bukkit.block.Chest; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.Event; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.block.Action; | ||
import org.bukkit.event.player.PlayerInteractEvent; | ||
import org.bukkit.inventory.DoubleChestInventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import pro.cloudnode.smp.bankaccounts.Account; | ||
import pro.cloudnode.smp.bankaccounts.BankAccounts; | ||
import pro.cloudnode.smp.bankaccounts.POS; | ||
import pro.cloudnode.smp.bankaccounts.Permissions; | ||
|
||
import java.util.Optional; | ||
|
||
public final class POSOpen implements Listener { | ||
@EventHandler | ||
public void openPOS(final @NotNull PlayerInteractEvent event) { | ||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; | ||
final @Nullable Block block = event.getClickedBlock(); | ||
if (block == null) return; | ||
if (!(block.getState() instanceof final @NotNull Chest chest)) return; | ||
if (chest.getInventory().isEmpty()) return; | ||
if (chest.getInventory() instanceof DoubleChestInventory) return; | ||
|
||
final @NotNull Optional<POS> pos = POS.get(block); | ||
if (pos.isEmpty()) return; | ||
|
||
event.setUseInteractedBlock(Event.Result.DENY); | ||
|
||
final @NotNull Player player = event.getPlayer(); | ||
if (player.getUniqueId().equals(pos.get().seller.owner.getUniqueId())) { | ||
POS.openOwnerGui(player, chest, pos.get()); | ||
return; | ||
} | ||
if (!player.hasPermission(Permissions.POS_USE)) { | ||
player.sendMessage(BankAccounts.getInstance().config().messagesErrorsPosNoPermission()); | ||
return; | ||
} | ||
final @NotNull ItemStack heldItem = player.getInventory().getItemInMainHand(); | ||
if (heldItem.getType() != BankAccounts.getInstance().config().instrumentsMaterial()) { | ||
player.sendMessage(BankAccounts.getInstance().config().messagesErrorsNoCard()); | ||
return; | ||
} | ||
final @NotNull Optional<@NotNull Account> account = Account.get(heldItem); | ||
if (account.isEmpty()) player.sendMessage(BankAccounts.getInstance().config().messagesErrorsPosInvalidCard()); | ||
else { | ||
if (!player.hasPermission(Permissions.POS_USE_OTHER) && !account.get().owner.getUniqueId() | ||
.equals(player.getUniqueId())) { | ||
player.sendMessage(BankAccounts.getInstance().config().messagesErrorsNotAccountOwner()); | ||
return; | ||
} | ||
POS.openBuyGui(player, chest, pos.get(), account.get()); | ||
} | ||
} | ||
} |
56 changes: 0 additions & 56 deletions
56
src/main/java/pro/cloudnode/smp/bankaccounts/events/PlayerInteract.java
This file was deleted.
Oops, something went wrong.