Saber-Factions/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java

103 lines
3.1 KiB
Java
Raw Normal View History

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-03-18 17:33:23 +01:00
import com.bukkit.mcteam.factions.Board;
import com.bukkit.mcteam.factions.Conf;
2011-03-22 17:20:21 +01:00
import com.bukkit.mcteam.factions.FLocation;
2011-03-18 17:33:23 +01:00
import com.bukkit.mcteam.factions.FPlayer;
import com.bukkit.mcteam.factions.Faction;
2011-03-22 17:20:21 +01:00
import com.bukkit.mcteam.factions.util.TextUtil;
2011-02-06 13:36:11 +01:00
public class FactionsBlockListener extends BlockListener {
@Override
public void onBlockPlace(BlockPlaceEvent event) {
if (event.isCancelled()) {
2011-03-22 17:20:21 +01:00
return;
2011-02-06 13:36:11 +01:00
}
2011-03-22 17:20:21 +01:00
2011-02-06 13:36:11 +01:00
if ( ! this.playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock(), "build")) {
event.setCancelled(true);
}
}
2011-02-06 13:36:11 +01:00
@Override
public void onBlockDamage(BlockDamageEvent event) {
if (event.isCancelled()) {
2011-03-23 12:00:38 +01:00
return;
2011-02-06 13:36:11 +01:00
}
2011-03-23 12:00:38 +01:00
boolean blockDestroyed = event.getDamageLevel() == BlockDamageLevel.STOPPED || Conf.instaDestroyMaterials.contains(event.getBlock().getType());
if (blockDestroyed && ! 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) {
2011-03-22 17:20:21 +01:00
Faction otherFaction = Board.getFactionAt(new FLocation(block));
2011-02-06 13:36:11 +01:00
2011-03-23 12:00:38 +01:00
if (otherFaction.getId() == 0) {
2011-02-06 13:36:11 +01:00
return true; // This is no faction territory. You may build or break stuff here.
}
2011-03-18 17:33:23 +01:00
FPlayer me = FPlayer.get(player);
2011-02-06 13:36:11 +01:00
Faction myFaction = me.getFaction();
// Cancel if we are not in our own territory
if (myFaction != otherFaction) {
2011-03-22 17:20:21 +01:00
me.sendMessage("You can't "+action+" in the territory of "+otherFaction.getTag(myFaction));
2011-02-06 13:36:11 +01:00
return false;
}
return true;
}
@Override
public void onBlockInteract(BlockInteractEvent event) {
if (event.isCancelled()) {
2011-03-22 17:20:21 +01:00
return;
2011-02-06 13:36:11 +01:00
}
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();
2011-02-06 13:36:11 +01:00
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;
}
2011-03-18 17:33:23 +01:00
FPlayer me = FPlayer.get(player);
2011-02-06 13:36:11 +01:00
Faction myFaction = me.getFaction();
2011-03-22 17:20:21 +01:00
Faction otherFaction = Board.getFactionAt(new FLocation(block));
2011-02-06 13:36:11 +01:00
2011-03-22 17:20:21 +01:00
if (otherFaction != null && otherFaction.getId() != 0 && myFaction != otherFaction) {
me.sendMessage(Conf.colorSystem+"You can't use "+TextUtil.getMaterialName(material)+" in the territory of "+otherFaction.getTag(myFaction));
//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;
}
}