2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop.Listeners;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-10-11 13:15:53 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
2012-01-25 16:32:34 +01:00
|
|
|
import com.Acrobot.ChestShop.Config.Language;
|
2011-10-11 13:15:53 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2012-01-25 16:32:34 +01:00
|
|
|
import com.Acrobot.ChestShop.Economy.Economy;
|
2011-06-09 22:54:01 +02:00
|
|
|
import com.Acrobot.ChestShop.Permission;
|
2012-01-09 22:39:38 +01:00
|
|
|
import com.Acrobot.ChestShop.Signs.restrictedSign;
|
2011-07-05 19:08:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2012-05-10 16:32:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uName;
|
2011-07-05 19:08:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uSign;
|
2011-10-11 13:15:53 +02:00
|
|
|
import org.bukkit.Material;
|
2011-06-09 22:54:01 +02:00
|
|
|
import org.bukkit.block.Block;
|
2011-09-29 20:29:39 +02:00
|
|
|
import org.bukkit.block.BlockFace;
|
2011-06-09 22:54:01 +02:00
|
|
|
import org.bukkit.block.Sign;
|
|
|
|
import org.bukkit.entity.Player;
|
2012-02-16 19:09:37 +01:00
|
|
|
import org.bukkit.event.EventHandler;
|
2012-04-07 12:41:20 +02:00
|
|
|
import org.bukkit.event.EventPriority;
|
2012-02-16 19:09:37 +01:00
|
|
|
import org.bukkit.event.Listener;
|
2011-06-09 22:54:01 +02:00
|
|
|
import org.bukkit.event.block.BlockBreakEvent;
|
2011-07-23 21:00:47 +02:00
|
|
|
import org.bukkit.event.block.BlockPistonExtendEvent;
|
|
|
|
import org.bukkit.event.block.BlockPistonRetractEvent;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.material.Directional;
|
2011-09-29 20:29:39 +02:00
|
|
|
import org.bukkit.material.PistonBaseMaterial;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-10-11 13:15:53 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2012-05-10 16:32:25 +02:00
|
|
|
public class BlockBreak implements Listener {
|
2011-07-24 16:56:18 +02:00
|
|
|
public static boolean cancellingBlockBreak(Block block, Player player) {
|
2011-12-01 14:02:58 +01:00
|
|
|
if (block == null) return false;
|
2012-01-09 22:39:38 +01:00
|
|
|
|
2011-12-01 14:02:58 +01:00
|
|
|
if (uSign.isSign(block)) block.getState().update(); //Show the text immediately
|
2011-07-24 16:56:18 +02:00
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
if (restrictedSign(block)) return !restrictedSign.canDestroy(player, uBlock.findRestrictedSign(block));
|
2011-06-09 22:54:01 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
Sign sign = uBlock.findValidShopSign(block, (player != null ? uName.stripName(player.getName()) : null));
|
2011-12-01 14:02:58 +01:00
|
|
|
if (!isCorrectSign(sign, block)) return false; //It's not a correct shop sign, so don't cancel it
|
2012-03-11 22:56:51 +01:00
|
|
|
if (playerIsNotOwner(player, sign)) return !isAdmin(player); //If the player isn't the owner or an admin - cancel!
|
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
if (weShouldReturnMoney() && !Permission.has(player, Permission.NOFEE)) {
|
2012-01-25 16:32:34 +01:00
|
|
|
float refundPrice = Config.getFloat(Property.SHOP_REFUND_PRICE);
|
2012-05-10 16:32:25 +02:00
|
|
|
Economy.add(uName.getName(sign.getLine(0)), refundPrice); //Add some money
|
2012-01-25 16:32:34 +01:00
|
|
|
player.sendMessage(Config.getLocal(Language.SHOP_REFUNDED).replace("%amount", Economy.formatBalance(refundPrice)));
|
|
|
|
}
|
2012-03-11 22:56:51 +01:00
|
|
|
|
2011-12-01 14:02:58 +01:00
|
|
|
return false; //Player is the owner, so we don't want to cancel this :)
|
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2012-03-11 22:56:51 +01:00
|
|
|
private static boolean isAdmin(Player p) {
|
|
|
|
return p != null && (Permission.has(p, Permission.ADMIN) || Permission.has(p, Permission.MOD));
|
|
|
|
}
|
2011-12-01 14:02:58 +01:00
|
|
|
|
|
|
|
private static boolean weShouldReturnMoney() {
|
|
|
|
//We should return money when it's turned on in config, obviously
|
|
|
|
return Config.getFloat(Property.SHOP_REFUND_PRICE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean restrictedSign(Block block) {
|
2012-01-09 22:39:38 +01:00
|
|
|
return uBlock.findRestrictedSign(block) != null;
|
2011-07-23 21:00:47 +02:00
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
|
2012-04-07 12:41:20 +02:00
|
|
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
2012-02-16 19:09:37 +01:00
|
|
|
public static void onBlockBreak(BlockBreakEvent event) {
|
2011-07-23 21:00:47 +02:00
|
|
|
if (cancellingBlockBreak(event.getBlock(), event.getPlayer())) event.setCancelled(true);
|
|
|
|
}
|
2011-07-02 20:34:14 +02:00
|
|
|
|
2011-09-06 19:01:57 +02:00
|
|
|
private static boolean isCorrectSign(Sign sign, Block block) {
|
2012-05-10 16:32:25 +02:00
|
|
|
return sign != null && (sign.getBlock().equals(block) || uBlock.getAttachedFace(sign).equals(block));
|
2011-08-26 23:12:32 +02:00
|
|
|
}
|
|
|
|
|
2011-09-06 19:01:57 +02:00
|
|
|
private static boolean playerIsNotOwner(Player player, Sign sign) {
|
2012-05-10 16:32:25 +02:00
|
|
|
return player == null || (!uName.stripName(player.getName()).equals(sign.getLine(0))
|
2012-01-09 22:39:38 +01:00
|
|
|
&& !Permission.otherName(player, sign.getLine(0)));
|
2011-08-26 23:12:32 +02:00
|
|
|
}
|
|
|
|
|
2012-04-07 12:41:20 +02:00
|
|
|
@EventHandler(ignoreCancelled = true)
|
2012-02-16 19:09:37 +01:00
|
|
|
public static void onBlockPistonExtend(BlockPistonExtendEvent event) {
|
2011-10-11 13:15:53 +02:00
|
|
|
for (Block b : getExtendBlocks(event)) {
|
2011-07-24 16:56:18 +02:00
|
|
|
if (cancellingBlockBreak(b, null)) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
}
|
|
|
|
}
|
2011-07-23 21:00:47 +02:00
|
|
|
|
2012-04-07 12:41:20 +02:00
|
|
|
@EventHandler(ignoreCancelled = true)
|
2012-02-16 19:09:37 +01:00
|
|
|
public static void onBlockPistonRetract(BlockPistonRetractEvent event) {
|
2011-09-22 15:56:06 +02:00
|
|
|
if (cancellingBlockBreak(getRetractBlock(event), null)) event.setCancelled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Block getRetractBlock(BlockPistonRetractEvent event) {
|
2011-09-29 20:29:39 +02:00
|
|
|
Block block = getRetractLocationBlock(event);
|
|
|
|
return (block != null && !uSign.isSign(block) ? block : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Those are fixes for CraftBukkit's piston bug, where piston appears not to be a piston.
|
|
|
|
private static BlockFace getPistonDirection(Block block) {
|
2012-05-10 16:32:25 +02:00
|
|
|
return block.getState().getData() instanceof PistonBaseMaterial ? ((Directional) block.getState().getData()).getFacing() : null;
|
2011-09-29 20:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Block getRetractLocationBlock(BlockPistonRetractEvent event) {
|
|
|
|
BlockFace pistonDirection = getPistonDirection(event.getBlock());
|
|
|
|
return pistonDirection != null ? event.getBlock().getRelative((pistonDirection), 2).getLocation().getBlock() : null;
|
2011-07-23 21:00:47 +02:00
|
|
|
}
|
2011-10-11 13:15:53 +02:00
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
private static List<Block> getExtendBlocks(BlockPistonExtendEvent event) {
|
2011-10-11 13:15:53 +02:00
|
|
|
BlockFace pistonDirection = getPistonDirection(event.getBlock());
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
if (pistonDirection == null) {
|
|
|
|
return new ArrayList<Block>();
|
|
|
|
}
|
|
|
|
|
2011-10-11 13:15:53 +02:00
|
|
|
Block piston = event.getBlock();
|
2012-05-10 16:32:25 +02:00
|
|
|
List<Block> pushedBlocks = new ArrayList<Block>();
|
|
|
|
|
|
|
|
for (int currentBlock = 1; currentBlock < event.getLength() + 1; currentBlock++) {
|
|
|
|
Block block = piston.getRelative(pistonDirection, currentBlock);
|
2011-10-11 13:15:53 +02:00
|
|
|
Material blockType = block.getType();
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
if (blockType == Material.AIR) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pushedBlocks.add(block);
|
2011-10-11 13:15:53 +02:00
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
return pushedBlocks;
|
2011-10-11 13:15:53 +02:00
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|