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

126 lines
4.4 KiB
Java
Raw Normal View History

package com.bukkit.mcteam.factions.listeners;
2011-02-06 13:36:11 +01:00
import java.util.*;
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;
import com.bukkit.mcteam.factions.Coord;
import com.bukkit.mcteam.factions.FPlayer;
import com.bukkit.mcteam.factions.Faction;
import com.bukkit.mcteam.factions.Factions;
2011-02-06 13:36:11 +01:00
import com.bukkit.mcteam.factions.entities.*;
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) {
// debug
//event.getPlayer().sendMessage("Block placed: " + event.getBlockPlaced().getTypeId() + " Block clicked: " + event.getBlock().getTypeId() + "(" + event.getBlock().getType().toString() + ")");
2011-02-06 13:36:11 +01:00
if (event.isCancelled()) {
return; // Alright. lets listen to that.
}
if ( ! this.playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock(), "build")) {
event.setCancelled(true);
}
}
//special cases, check for destruction of: torch, redstone torch (on & off), repeater (on & off), redstonewire, sapling, crops, sugar cane
2011-03-18 17:33:23 +01:00
private static Set<Integer> specialBlocks = new HashSet<Integer>(Arrays.asList(
new Integer[] {50, 75, 76, 93, 94, 55, 6, 59, 83}
));
2011-02-06 13:36:11 +01:00
@Override
public void onBlockDamage(BlockDamageEvent event) {
// debug
//event.getPlayer().sendMessage("Block damaged: " + event.getBlock().getTypeId() + " (" + event.getBlock().getType().toString() + ")");
2011-02-06 13:36:11 +01:00
if (event.isCancelled()) {
return; // Alright. lets listen to that.
}
boolean badBlock = event.getDamageLevel() == BlockDamageLevel.STOPPED || specialBlocks.contains(new Integer(event.getBlock().getTypeId()));
if (badBlock && ! 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
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.
}
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) {
me.sendMessage(Conf.colorSystem+"You can't "+action+" in the territory of "+otherFaction.getTag(myFaction));
//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();
// debug
//player.sendMessage("Block interacted: " + event.getBlock().getTypeId() + "(" + event.getBlock().getType().toString() + ")");
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();
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
if (otherFaction != null && otherFaction.id != 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;
}
}