|
| 1 | +package me.EtienneDx.RealEstate; |
| 2 | + |
| 3 | +import org.bukkit.event.EventHandler; |
| 4 | +import org.bukkit.event.Listener; |
| 5 | +import org.bukkit.plugin.PluginManager; |
| 6 | + |
| 7 | +import me.EtienneDx.RealEstate.Transactions.BoughtTransaction; |
| 8 | +import me.EtienneDx.RealEstate.Transactions.Transaction; |
| 9 | +import me.ryanhamshire.GriefPrevention.Claim; |
| 10 | +import me.ryanhamshire.GriefPrevention.ClaimPermission; |
| 11 | +import me.ryanhamshire.GriefPrevention.events.ClaimDeletedEvent; |
| 12 | +import me.ryanhamshire.GriefPrevention.events.ClaimPermissionCheckEvent; |
| 13 | + |
| 14 | +public class ClaimPermissionListener implements Listener { |
| 15 | + void registerEvents() |
| 16 | + { |
| 17 | + PluginManager pm = RealEstate.instance.getServer().getPluginManager(); |
| 18 | + |
| 19 | + pm.registerEvents(this, RealEstate.instance); |
| 20 | + } |
| 21 | + |
| 22 | + @EventHandler |
| 23 | + public void onClaimPermission(ClaimPermissionCheckEvent event) { |
| 24 | + Transaction transaction = RealEstate.transactionsStore.getTransaction(event.getClaim()); |
| 25 | + // we only have to remove the owner's access, the rest is handled by GP |
| 26 | + if( |
| 27 | + // if there is a transaction and the player is the owner |
| 28 | + transaction != null && |
| 29 | + ( |
| 30 | + event.getCheckedUUID().equals(transaction.getOwner()) || |
| 31 | + (event.getClaim().isAdminClaim() && event.getCheckedPlayer().hasPermission("griefprevention.adminclaims")) |
| 32 | + ) && |
| 33 | + transaction instanceof BoughtTransaction && |
| 34 | + ((BoughtTransaction)transaction).getBuyer() != null |
| 35 | + ) { |
| 36 | + switch(event.getRequiredPermission()) { |
| 37 | + case Edit: |
| 38 | + event.setDenialReason(() -> Messages.getMessage(RealEstate.instance.messages.msgErrorClaimInTransactionCantEdit)); |
| 39 | + break; |
| 40 | + case Access: |
| 41 | + event.setDenialReason(() -> Messages.getMessage(RealEstate.instance.messages.msgErrorClaimInTransactionCantAccess)); |
| 42 | + break; |
| 43 | + case Build: |
| 44 | + event.setDenialReason(() -> Messages.getMessage(RealEstate.instance.messages.msgErrorClaimInTransactionCantBuild)); |
| 45 | + break; |
| 46 | + case Inventory: |
| 47 | + event.setDenialReason(() -> Messages.getMessage(RealEstate.instance.messages.msgErrorClaimInTransactionCantInventory)); |
| 48 | + break; |
| 49 | + case Manage: |
| 50 | + event.setDenialReason(() -> Messages.getMessage(RealEstate.instance.messages.msgErrorClaimInTransactionCantManage)); |
| 51 | + break; |
| 52 | + default: |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if(event.getRequiredPermission() == ClaimPermission.Edit || event.getRequiredPermission() == ClaimPermission.Manage) { |
| 58 | + for (Claim child : event.getClaim().children) { |
| 59 | + Transaction tr = RealEstate.transactionsStore.getTransaction(child); |
| 60 | + if(tr != null && |
| 61 | + tr instanceof BoughtTransaction && |
| 62 | + ((BoughtTransaction)tr).getBuyer() != null |
| 63 | + ) { |
| 64 | + event.setDenialReason(() -> Messages.getMessage(RealEstate.instance.messages.msgErrorSubclaimInTransaction)); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // more of a safety measure, normally it shouldn't be needed |
| 71 | + @EventHandler |
| 72 | + public void onClaimDeleted(ClaimDeletedEvent event) { |
| 73 | + Transaction tr = RealEstate.transactionsStore.getTransaction(event.getClaim()); |
| 74 | + if(tr != null) tr.tryCancelTransaction(null, true); |
| 75 | + for (Claim child : event.getClaim().children) { |
| 76 | + tr = RealEstate.transactionsStore.getTransaction(child); |
| 77 | + if(tr != null) tr.tryCancelTransaction(null, true); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments