2011-02-07 21:42:14 +01:00
package com.bukkit.mcteam.factions.listeners ;
2011-02-06 13:36:11 +01:00
import org.bukkit.Material ;
import org.bukkit.block.Block ;
import org.bukkit.block.BlockDamageLevel ;
import org.bukkit.entity.Player ;
import org.bukkit.event.block.BlockDamageEvent ;
import org.bukkit.event.block.BlockInteractEvent ;
import org.bukkit.event.block.BlockListener ;
import org.bukkit.event.block.BlockPlaceEvent ;
2011-02-07 21:42:14 +01:00
import com.bukkit.mcteam.factions.Factions ;
2011-02-06 13:36:11 +01:00
import com.bukkit.mcteam.factions.entities.* ;
2011-02-07 21:42:14 +01:00
import com.bukkit.mcteam.factions.util.* ;
2011-02-06 13:36:11 +01:00
public class FactionsBlockListener extends BlockListener {
public Factions plugin ;
public FactionsBlockListener ( Factions plugin ) {
this . plugin = plugin ;
}
@Override
public void onBlockPlace ( BlockPlaceEvent event ) {
if ( event . isCancelled ( ) ) {
return ; // Alright. lets listen to that.
}
if ( ! this . playerCanBuildDestroyBlock ( event . getPlayer ( ) , event . getBlock ( ) , " build " ) ) {
event . setCancelled ( true ) ;
}
}
@Override
public void onBlockDamage ( BlockDamageEvent event ) {
if ( event . isCancelled ( ) ) {
return ; // Alright. lets listen to that.
}
2011-02-26 10:33:31 +01:00
if ( event . getDamageLevel ( ) = = BlockDamageLevel . STOPPED & & ! this . playerCanBuildDestroyBlock ( event . getPlayer ( ) , event . getBlock ( ) , " destroy " ) ) {
2011-02-06 13:36:11 +01:00
event . setCancelled ( true ) ;
}
}
public boolean playerCanBuildDestroyBlock ( Player player , Block block , String action ) {
Coord coord = Coord . parseCoord ( block ) ;
2011-02-13 11:18:08 +01:00
Faction otherFaction = Board . get ( player . getWorld ( ) ) . getFactionAt ( coord ) ;
2011-02-06 13:36:11 +01:00
2011-03-04 17:42:41 +01:00
if ( otherFaction = = null | | otherFaction . id = = 0 ) {
2011-02-06 13:36:11 +01:00
return true ; // This is no faction territory. You may build or break stuff here.
}
Follower me = Follower . get ( player ) ;
Faction myFaction = me . getFaction ( ) ;
// Cancel if we are not in our own territory
if ( myFaction ! = otherFaction ) {
2011-02-12 18:05:05 +01:00
me . sendMessage ( Conf . colorSystem + " You can't " + action + " in the territory of " + otherFaction . getTag ( myFaction ) ) ;
2011-02-26 10:33:31 +01:00
//otherFaction.sendMessage(me.getNameAndRelevant(otherFaction)+Conf.colorSystem+" tried to "+action+" "+TextUtil.getMaterialName(block.getType())+" in your territory");
2011-02-06 13:36:11 +01:00
return false ;
}
return true ;
}
@Override
public void onBlockInteract ( BlockInteractEvent event ) {
if ( event . isCancelled ( ) ) {
return ; // Alright. lets listen to that.
}
if ( ! ( event . getEntity ( ) instanceof Player ) ) {
// So far mobs does not interact with the environment :P
return ;
}
Block block = event . getBlock ( ) ;
Player player = ( Player ) event . getEntity ( ) ;
if ( ! canPlayerUseRightclickBlock ( player , block ) ) {
event . setCancelled ( true ) ;
}
}
public boolean canPlayerUseRightclickBlock ( Player player , Block block ) {
Material material = block . getType ( ) ;
// We only care about some material types.
if ( ! Conf . territoryProtectedMaterials . contains ( material ) ) {
return true ;
}
Follower me = Follower . get ( player ) ;
Faction myFaction = me . getFaction ( ) ;
Coord blockCoord = Coord . from ( block . getLocation ( ) ) ;
2011-02-13 11:18:08 +01:00
Faction otherFaction = Board . get ( player . getWorld ( ) ) . getFactionAt ( blockCoord ) ;
2011-02-06 13:36:11 +01:00
2011-03-04 17:42:41 +01:00
if ( otherFaction ! = null & & otherFaction . id ! = 0 & & myFaction ! = otherFaction ) {
2011-02-12 18:05:05 +01:00
me . sendMessage ( Conf . colorSystem + " You can't use " + TextUtil . getMaterialName ( material ) + " in the territory of " + otherFaction . getTag ( myFaction ) ) ;
2011-02-26 10:33:31 +01:00
//otherFaction.sendMessage(me.getNameAndRelevant(otherFaction)+Conf.colorSystem+" tried to use "+TextUtil.getMaterialName(material)+" in your territory");
2011-02-06 13:36:11 +01:00
return false ;
}
return true ;
}
}