Skip to content

Commit

Permalink
Prevent using bucket on faction's claim (#1763)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tems-py authored Nov 5, 2023
1 parent af5b7d8 commit bdbd4c4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Alternatively, you can use the External API, the documentation for which can be
| [Deej](https://github.com/Mr-Deej) | Added checks to several commands |
| VoChiDanh | Refactored parts the PersistentData class in an attempt to resolve java compatibility issues |
| Kyrenic | Implemented contiguous claims config option |
| [Tems](https://github.com/Tems-py) | Fixed claim protection issues |

### Translators
| Name | Language(s) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import com.dansplugins.factionsystem.listener.EntityDamageListener
import com.dansplugins.factionsystem.listener.EntityExplodeListener
import com.dansplugins.factionsystem.listener.InventoryMoveItemListener
import com.dansplugins.factionsystem.listener.LingeringPotionSplashListener
import com.dansplugins.factionsystem.listener.PlayerBucketListener
import com.dansplugins.factionsystem.listener.PlayerDeathListener
import com.dansplugins.factionsystem.listener.PlayerInteractEntityListener
import com.dansplugins.factionsystem.listener.PlayerInteractListener
Expand Down Expand Up @@ -320,6 +321,7 @@ class MedievalFactions : JavaPlugin() {
EntityExplodeListener(this),
InventoryMoveItemListener(this),
LingeringPotionSplashListener(this),
PlayerBucketListener(this),
PlayerDeathListener(this),
PlayerInteractListener(this),
PlayerJoinListener(this),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.dansplugins.factionsystem.listener
import com.dansplugins.factionsystem.MedievalFactions
import com.dansplugins.factionsystem.area.MfBlockPosition
import com.dansplugins.factionsystem.player.MfPlayer
import dev.forkhandles.result4k.onFailure
import org.bukkit.ChatColor
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerBucketEmptyEvent
import org.bukkit.event.player.PlayerBucketEvent
import org.bukkit.event.player.PlayerBucketFillEvent
import java.util.logging.Level

class PlayerBucketListener(private val plugin: MedievalFactions) : Listener {
fun handleProtection(event: PlayerBucketEvent) {
val gateService = plugin.services.gateService
val blockPosition = MfBlockPosition.fromBukkitBlock(event.block)
val gates = gateService.getGatesAt(blockPosition)
if (gates.isNotEmpty()) {
event.isCancelled = true
event.player.sendMessage("${ChatColor.RED}${plugin.language["CannotPlaceBlockInGate"]}")
return
}

val claimService = plugin.services.claimService
val claim = claimService.getClaim(event.block.chunk) ?: return
val factionService = plugin.services.factionService
val claimFaction = factionService.getFaction(claim.factionId) ?: return
val playerService = plugin.services.playerService
val mfPlayer = playerService.getPlayer(event.player)
if (mfPlayer == null) {
event.isCancelled = true
plugin.server.scheduler.runTaskAsynchronously(
plugin,
Runnable {
playerService.save(MfPlayer(plugin, event.player)).onFailure {
event.player.sendMessage("${ChatColor.RED}${plugin.language["BlockPlaceFailedToSavePlayer"]}")
plugin.logger.log(Level.SEVERE, "Failed to save player: ${it.reason.message}", it.reason.cause)
return@Runnable
}
}
)
return
}

if (!claimService.isInteractionAllowed(mfPlayer.id, claim)) {
if (mfPlayer.isBypassEnabled && event.player.hasPermission("mf.bypass")) {
event.player.sendMessage("${ChatColor.RED}${plugin.language["FactionTerritoryProtectionBypassed"]}")
} else {
event.isCancelled = true
event.player.sendMessage("${ChatColor.RED}${plugin.language["CannotBreakBlockInFactionTerritory", claimFaction.name]}")
}
}
}

@EventHandler
fun playerBuckeEmptytEvent(event: PlayerBucketEmptyEvent) {
handleProtection(event)
}

@EventHandler
fun playerBucketFillEvent(event: PlayerBucketFillEvent) {
handleProtection(event)
}
}

0 comments on commit bdbd4c4

Please sign in to comment.