2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop.Listeners;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
2011-06-11 17:36:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Language;
|
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2011-08-13 12:08:34 +02:00
|
|
|
import com.Acrobot.ChestShop.Economy;
|
2011-05-29 13:25:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Items.Items;
|
|
|
|
import com.Acrobot.ChestShop.Permission;
|
2011-09-01 18:59:15 +02:00
|
|
|
import com.Acrobot.ChestShop.Protection.Plugins.Default;
|
2011-05-29 13:25:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Protection.Security;
|
2011-07-23 21:00:47 +02:00
|
|
|
import com.Acrobot.ChestShop.Signs.restrictedSign;
|
2011-07-05 19:08:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2011-07-23 21:00:47 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uLongName;
|
2011-07-05 19:08:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uNumber;
|
|
|
|
import com.Acrobot.ChestShop.Utils.uSign;
|
2011-05-29 13:25:25 +02:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
2011-06-23 23:25:34 +02:00
|
|
|
import org.bukkit.block.BlockFace;
|
2011-05-29 13:25:25 +02:00
|
|
|
import org.bukkit.block.Chest;
|
2011-06-23 23:25:34 +02:00
|
|
|
import org.bukkit.block.Sign;
|
2011-05-29 13:25:25 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.event.block.BlockListener;
|
2011-05-29 13:25:25 +02:00
|
|
|
import org.bukkit.event.block.SignChangeEvent;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2011-05-29 13:25:25 +02:00
|
|
|
public class signChange extends BlockListener {
|
|
|
|
|
|
|
|
public void onSignChange(SignChangeEvent event) {
|
|
|
|
Block signBlock = event.getBlock();
|
|
|
|
String[] line = event.getLines();
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
boolean isAlmostReady = uSign.isValidPreparedSign(line);
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
ItemStack stock = Items.getItemStack(line[3]);
|
|
|
|
Material mat = stock == null ? null : stock.getType();
|
|
|
|
|
|
|
|
boolean playerIsAdmin = Permission.has(player, Permission.ADMIN);
|
|
|
|
|
|
|
|
|
|
|
|
if (isAlmostReady) {
|
2011-06-11 17:36:55 +02:00
|
|
|
if (mat == null) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.INCORRECT_ITEM_ID));
|
|
|
|
dropSign(event);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-13 12:08:34 +02:00
|
|
|
if (!canCreateShop(player, mat.getId())) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.YOU_CANNOT_CREATE_SHOP));
|
2011-06-09 22:54:01 +02:00
|
|
|
dropSign(event);
|
|
|
|
return;
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
} else {
|
2011-07-23 21:00:47 +02:00
|
|
|
if (restrictedSign.isRestricted(event.getLines())) {
|
2011-07-02 20:34:14 +02:00
|
|
|
if (!playerIsAdmin) {
|
2011-06-23 23:25:34 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
|
|
|
|
dropSign(event);
|
|
|
|
return;
|
|
|
|
}
|
2011-07-23 21:00:47 +02:00
|
|
|
Block secondSign = signBlock.getRelative(BlockFace.DOWN);
|
2011-08-13 12:08:34 +02:00
|
|
|
if (!uSign.isSign(secondSign) || !uSign.isValid((Sign) secondSign.getState())) dropSign(event);
|
2011-06-23 23:25:34 +02:00
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
if (formatFirstLine(line[0], player)) event.setLine(0, uLongName.stripName(player.getName()));
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
String thirdLine = formatThirdLine(line[2]);
|
|
|
|
if (thirdLine == null) {
|
|
|
|
dropSign(event);
|
|
|
|
player.sendMessage(Config.getLocal(Language.YOU_CANNOT_CREATE_SHOP));
|
|
|
|
return;
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
2011-08-13 12:08:34 +02:00
|
|
|
event.setLine(2, thirdLine);
|
2011-08-26 23:12:32 +02:00
|
|
|
event.setLine(3, formatFourthLine(line[3], mat));
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-07-05 19:08:55 +02:00
|
|
|
Chest chest = uBlock.findChest(signBlock);
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-08-26 23:12:32 +02:00
|
|
|
boolean isAdminShop = uSign.isAdminShop(event.getLine(0));
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!isAdminShop) {
|
|
|
|
if (chest == null) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED));
|
2011-05-29 13:25:25 +02:00
|
|
|
dropSign(event);
|
|
|
|
return;
|
|
|
|
} else if (!playerIsAdmin) {
|
2011-07-02 20:34:14 +02:00
|
|
|
if (!Security.canPlaceSign(player, signBlock)) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.ANOTHER_SHOP_DETECTED));
|
2011-05-29 13:25:25 +02:00
|
|
|
dropSign(event);
|
|
|
|
return;
|
|
|
|
}
|
2011-06-11 17:36:55 +02:00
|
|
|
|
|
|
|
Block chestBlock = chest.getBlock();
|
2011-08-15 11:46:01 +02:00
|
|
|
boolean canAccess = !Security.isProtected(chestBlock) || Security.canAccess(player, chestBlock);
|
2011-06-11 17:36:55 +02:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
if (!(Security.protection instanceof Default) && canAccess) {
|
2011-07-02 20:34:14 +02:00
|
|
|
Default protection = new Default();
|
2011-08-13 12:08:34 +02:00
|
|
|
if (protection.isProtected(chestBlock) && !protection.canAccess(player, chestBlock))
|
2011-07-02 20:34:14 +02:00
|
|
|
canAccess = false;
|
2011-06-11 17:36:55 +02:00
|
|
|
}
|
2011-07-02 20:34:14 +02:00
|
|
|
|
|
|
|
if (!canAccess) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.CANNOT_ACCESS_THE_CHEST));
|
|
|
|
dropSign(event);
|
|
|
|
return;
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
float shopCreationPrice = Config.getFloat(Property.SHOP_CREATION_PRICE);
|
2011-08-31 20:57:10 +02:00
|
|
|
boolean paid = shopCreationPrice != 0 && !isAdminShop;
|
|
|
|
if (paid) {
|
|
|
|
if (!Economy.hasEnough(player.getName(), shopCreationPrice)) {
|
2011-08-13 12:08:34 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_MONEY));
|
|
|
|
dropSign(event);
|
|
|
|
return;
|
2011-06-11 17:36:55 +02:00
|
|
|
}
|
2011-08-13 12:08:34 +02:00
|
|
|
|
|
|
|
Economy.substract(player.getName(), shopCreationPrice);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config.getBoolean(Property.PROTECT_SIGN_WITH_LWC)) {
|
|
|
|
Security.protect(player.getName(), signBlock);
|
|
|
|
}
|
|
|
|
if (Config.getBoolean(Property.PROTECT_CHEST_WITH_LWC) && chest != null && Security.protect(player.getName(), chest.getBlock())) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.PROTECTED_SHOP));
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
uLongName.saveName(player.getName());
|
2011-08-31 20:57:10 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.SHOP_CREATED) + (paid ? " - " + Economy.formatBalance(shopCreationPrice) : ""));
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
private static boolean canCreateShop(Player player, boolean isAdmin, int ID) {
|
|
|
|
return isAdmin ||
|
|
|
|
Permission.has(player, Permission.SHOP_CREATION) ||
|
|
|
|
Permission.has(player, Permission.SHOP_CREATION.toString() + '.' + ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean canCreateShop(Player player, int ID) {
|
|
|
|
return canCreateShop(player, Permission.has(player, Permission.ADMIN), ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String formatThirdLine(String thirdLine) {
|
|
|
|
String[] split = thirdLine.split(":");
|
|
|
|
if (uNumber.isFloat(split[0])) thirdLine = "B " + thirdLine;
|
|
|
|
if (split.length == 2 && uNumber.isFloat(split[1])) thirdLine = thirdLine + " S";
|
|
|
|
if (thirdLine.length() > 15) thirdLine = thirdLine.replace(" ", "");
|
|
|
|
|
|
|
|
return (thirdLine.length() > 15 ? null : thirdLine);
|
|
|
|
}
|
|
|
|
|
2011-08-31 20:57:10 +02:00
|
|
|
private static String formatFourthLine(String fourthLine, Material material) {
|
2011-08-26 23:12:32 +02:00
|
|
|
String[] split = fourthLine.split(":");
|
|
|
|
if (uNumber.isInteger(split[0])) {
|
|
|
|
String materialLine = material.name();
|
2011-08-31 20:57:10 +02:00
|
|
|
if (split.length == 2) {
|
2011-08-26 23:12:32 +02:00
|
|
|
int maxLength = (14 - split[1].length()); //15 - length - 1
|
|
|
|
if (materialLine.length() > maxLength) materialLine = materialLine.substring(0, maxLength);
|
|
|
|
materialLine = materialLine + ':' + split[1];
|
|
|
|
}
|
|
|
|
return materialLine;
|
|
|
|
}
|
|
|
|
return fourthLine;
|
|
|
|
}
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
private static boolean formatFirstLine(String line1, Player player) {
|
|
|
|
return line1.isEmpty() ||
|
|
|
|
(!line1.equals(uLongName.stripName(player.getName())) && !Permission.has(player, Permission.ADMIN));
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static void dropSign(SignChangeEvent event) {
|
2011-05-29 13:25:25 +02:00
|
|
|
event.setCancelled(true);
|
|
|
|
|
|
|
|
Block block = event.getBlock();
|
|
|
|
block.setType(Material.AIR);
|
|
|
|
block.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(Material.SIGN, 1));
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|