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;
|
2012-03-01 22:03:59 +01:00
|
|
|
import com.Acrobot.ChestShop.Config.MaxPrice;
|
2011-06-11 17:36:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2012-01-25 16:32:34 +01:00
|
|
|
import com.Acrobot.ChestShop.Economy.Economy;
|
2011-05-29 13:25:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Items.Items;
|
|
|
|
import com.Acrobot.ChestShop.Permission;
|
|
|
|
import com.Acrobot.ChestShop.Protection.Security;
|
2011-07-23 21:00:47 +02:00
|
|
|
import com.Acrobot.ChestShop.Signs.restrictedSign;
|
2011-09-22 15:56:06 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.*;
|
2012-03-01 22:03:59 +01:00
|
|
|
import com.Acrobot.ChestShop.Utils.WorldGuard.uWorldGuard;
|
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;
|
2012-02-16 19:09:37 +01:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.Listener;
|
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
|
|
|
|
*/
|
2012-02-16 19:09:37 +01:00
|
|
|
public class signChange implements Listener {
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2012-02-16 19:09:37 +01:00
|
|
|
@EventHandler
|
|
|
|
public static void onSignChange(SignChangeEvent event) {
|
2011-05-29 13:25:25 +02:00
|
|
|
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-05-29 13:25:25 +02:00
|
|
|
} else {
|
2012-01-09 22:39:38 +01:00
|
|
|
if (restrictedSign.isRestricted(line)) {
|
|
|
|
if (!restrictedSign.hasPermission(player, line)) {
|
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);
|
2012-01-09 22:39:38 +01:00
|
|
|
if (!playerIsAdmin && (!uSign.isSign(secondSign) || !uSign.isValid((Sign) secondSign.getState())
|
|
|
|
|| !uSign.canAccess(player, (Sign) secondSign))) 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-12-20 21:39:45 +01:00
|
|
|
event.setLine(3, formatFourthLine(line[3], stock));
|
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-12-01 14:02:58 +01:00
|
|
|
if (!Config.getBoolean(Property.ALLOW_MULTIPLE_SHOPS_AT_ONE_BLOCK) && !Security.canPlaceSign(player, (Sign) signBlock.getState())) {
|
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();
|
2012-03-01 22:03:59 +01:00
|
|
|
boolean cantBuildTowny = uSign.towny != null && !uTowny.canBuild(player, signBlock.getLocation(), chestBlock.getLocation());
|
2011-09-22 15:56:06 +02:00
|
|
|
|
2012-03-01 22:03:59 +01:00
|
|
|
if (!uWorldGuard.canBuildShopHere(signBlock.getLocation()) && cantBuildTowny){
|
2011-09-22 15:56:06 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.TOWNY_CANNOT_CREATE_SHOP_HERE));
|
|
|
|
dropSign(event);
|
|
|
|
return;
|
|
|
|
}
|
2011-06-11 17:36:55 +02:00
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
boolean canAccess = !Security.isProtected(chestBlock) || Security.canAccess(player, chestBlock);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-01 22:03:59 +01:00
|
|
|
float buyPrice = uSign.buyPrice(thirdLine);
|
|
|
|
float sellPrice = uSign.sellPrice(thirdLine);
|
2012-01-09 22:39:38 +01:00
|
|
|
|
2012-03-01 22:03:59 +01:00
|
|
|
if (!playerIsAdmin && (!canCreateShop(player, mat.getId(), buyPrice != -1, sellPrice != -1) || !MaxPrice.canCreate(buyPrice, sellPrice, mat))) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.YOU_CANNOT_CREATE_SHOP));
|
|
|
|
dropSign(event);
|
|
|
|
return;
|
|
|
|
}
|
2012-01-09 22:39:38 +01:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
float shopCreationPrice = Config.getFloat(Property.SHOP_CREATION_PRICE);
|
2012-01-25 16:32:34 +01:00
|
|
|
boolean paid = shopCreationPrice != 0 && !isAdminShop && !Permission.has(player, Permission.NOFEE);
|
2011-08-31 20:57:10 +02:00
|
|
|
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
|
|
|
|
2012-01-25 16:32:34 +01:00
|
|
|
Economy.subtract(player.getName(), shopCreationPrice);
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Config.getBoolean(Property.PROTECT_SIGN_WITH_LWC)) {
|
2011-12-01 14:02:58 +01:00
|
|
|
if (!Security.protect(player.getName(), signBlock)) player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_LWC_PROTECTIONS));
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
|
|
|
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) : ""));
|
2012-01-25 16:32:34 +01:00
|
|
|
|
|
|
|
uHeroes.addHeroExp(player);
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
|
2012-03-01 22:03:59 +01:00
|
|
|
private static boolean canCreateShop(Player player, int ID, boolean buy, boolean sell) {
|
|
|
|
if (Permission.has(player, Permission.SHOP_CREATION_ID + Integer.toString(ID))) return true;
|
|
|
|
|
|
|
|
if (buy && !Permission.has(player, Permission.SHOP_CREATION_BUY)) return false;
|
|
|
|
if (sell && !Permission.has(player, Permission.SHOP_CREATION_SELL)) return false;
|
2011-08-13 12:08:34 +02:00
|
|
|
|
2012-03-01 22:03:59 +01:00
|
|
|
return true;
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String formatThirdLine(String thirdLine) {
|
2011-12-01 14:02:58 +01:00
|
|
|
thirdLine = thirdLine.toUpperCase();
|
2011-08-13 12:08:34 +02:00
|
|
|
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(" ", "");
|
2011-12-01 14:02:58 +01:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
|
|
|
|
return (thirdLine.length() > 15 ? null : thirdLine);
|
|
|
|
}
|
|
|
|
|
2011-12-20 21:39:45 +01:00
|
|
|
private static String formatFourthLine(String fourthLine, ItemStack is) {
|
2011-12-16 17:20:09 +01:00
|
|
|
int index = (fourthLine.indexOf(':') != -1 ? fourthLine.indexOf(':') : 9999);
|
|
|
|
if (fourthLine.indexOf('-') < index && fourthLine.indexOf('-') != -1) index = fourthLine.indexOf('-');
|
|
|
|
|
|
|
|
StringBuilder toReturn = new StringBuilder(3);
|
2011-12-20 21:39:45 +01:00
|
|
|
String matName = fourthLine.split(":|-")[0];
|
|
|
|
matName = matName.trim();
|
|
|
|
if (uNumber.isInteger(matName)) matName = Items.getName(is, false);
|
|
|
|
int iPos = 15 - (fourthLine.length() - index);
|
|
|
|
if (index != 9999 && matName.length() > iPos) matName = matName.substring(0, iPos);
|
|
|
|
if (Items.getItemStack(matName).getType() == is.getType()) toReturn.append(matName);
|
|
|
|
else toReturn.append(is.getTypeId());
|
2011-12-16 17:20:09 +01:00
|
|
|
|
|
|
|
if (index != -1 && index != 9999) toReturn.append(fourthLine.substring(index));
|
2012-01-09 22:39:38 +01:00
|
|
|
return uSign.capitalizeFirst(toReturn.toString(), ' ');
|
2011-08-26 23:12:32 +02:00
|
|
|
}
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
private static boolean formatFirstLine(String line1, Player player) {
|
2011-12-01 14:02:58 +01:00
|
|
|
return line1.isEmpty() ||
|
|
|
|
(!line1.equals(uLongName.stripName(player.getName()))
|
|
|
|
&& !Permission.has(player, Permission.ADMIN)
|
2012-01-09 22:39:38 +01:00
|
|
|
&& !Permission.otherName(player, line1));
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|