Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent using bucket on faction's claim #1763

Merged
merged 4 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}