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-05-29 13:25:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Items.Items;
|
|
|
|
import com.Acrobot.ChestShop.Permission;
|
2011-06-11 17:36:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Protection.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-07-05 19:08:55 +02:00
|
|
|
Boolean isAlmostReady = uSign.isValidPreparedSign(event.getLines());
|
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-07-01 14:12:26 +02:00
|
|
|
if (!(playerIsAdmin ||
|
2011-07-02 20:34:14 +02:00
|
|
|
Permission.has(player, Permission.SHOP_CREATION) ||
|
2011-07-01 14:12:26 +02:00
|
|
|
(Permission.has(player, Permission.SHOP_CREATION + "." + mat.getId()) &&
|
2011-07-02 20:34:14 +02:00
|
|
|
!Permission.has(player, Permission.EXCLUDE_ITEM + "." + mat.getId())))) {
|
2011-05-29 13:25:25 +02:00
|
|
|
|
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-07-05 19:08:55 +02:00
|
|
|
if (!uSign.isSign(secondSign) || !uSign.isValid((Sign) secondSign.getState())) {
|
2011-06-23 23:25:34 +02:00
|
|
|
dropSign(event);
|
|
|
|
}
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-05 19:08:55 +02:00
|
|
|
Boolean isReady = uSign.isValid(line);
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
if (line[0].isEmpty() || (!line[0].startsWith(player.getName()) && !Permission.has(player, Permission.ADMIN))) {
|
2011-07-24 16:56:18 +02:00
|
|
|
event.setLine(0, uLongName.stripName(player.getName()));
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
line = event.getLines();
|
2011-07-23 21:00:47 +02:00
|
|
|
|
2011-07-05 19:08:55 +02:00
|
|
|
boolean isAdminShop = uSign.isAdminShop(line[0]);
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
if (!isReady) {
|
|
|
|
int prices = line[2].split(":").length;
|
|
|
|
String oldLine = line[2];
|
|
|
|
if (prices == 1) {
|
|
|
|
event.setLine(2, "B " + oldLine);
|
|
|
|
} else {
|
|
|
|
event.setLine(2, "B " + oldLine + " S");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String[] split = line[3].split(":");
|
2011-07-05 19:08:55 +02:00
|
|
|
if (uNumber.isInteger(split[0])) {
|
2011-05-29 13:25:25 +02:00
|
|
|
String matName = mat.name();
|
|
|
|
if (split.length == 2) {
|
|
|
|
int length = matName.length();
|
|
|
|
int maxLength = (15 - split[1].length() - 1);
|
|
|
|
if (length > maxLength) {
|
|
|
|
matName = matName.substring(0, maxLength);
|
|
|
|
}
|
|
|
|
event.setLine(3, matName + ':' + split[1]);
|
|
|
|
} else {
|
|
|
|
event.setLine(3, matName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-05 19:08:55 +02:00
|
|
|
Chest chest = uBlock.findChest(signBlock);
|
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
|
|
|
|
2011-07-02 20:34:14 +02:00
|
|
|
boolean canAccess = true;
|
2011-06-11 17:36:55 +02:00
|
|
|
Block chestBlock = chest.getBlock();
|
|
|
|
|
2011-07-02 20:34:14 +02:00
|
|
|
if (Security.isProtected(chestBlock) && !Security.canAccess(player, chestBlock)) {
|
|
|
|
canAccess = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(Security.protection instanceof Default)) {
|
|
|
|
Default protection = new Default();
|
|
|
|
if (protection.isProtected(chestBlock) && !protection.canAccess(player, chestBlock)) {
|
|
|
|
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-06-19 23:52:36 +02:00
|
|
|
if (Config.getBoolean(Property.PROTECT_CHEST_WITH_LWC) && chest != null && Security.protect(player.getName(), chest.getBlock())) {
|
2011-07-02 20:34:14 +02:00
|
|
|
if (Config.getBoolean(Property.PROTECT_SIGN_WITH_LWC)) {
|
2011-06-11 17:36:55 +02:00
|
|
|
Security.protect(player.getName(), signBlock);
|
|
|
|
}
|
|
|
|
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-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.SHOP_CREATED));
|
2011-05-29 13:25:25 +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
|
|
|
}
|