-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent using bucket on faction's claim (#1763)
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 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
65 changes: 65 additions & 0 deletions
65
src/main/kotlin/com/dansplugins/factionsystem/listener/PlayerBucketListener.kt
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,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) | ||
} | ||
} |