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-06-09 22:54:01 +02:00
|
|
|
import com.Acrobot.ChestShop.Permission;
|
2011-09-01 18:59:15 +02:00
|
|
|
import com.Acrobot.ChestShop.Protection.Plugins.Default;
|
2012-04-16 16:34:00 +02:00
|
|
|
import com.Acrobot.ChestShop.Protection.Security;
|
2011-05-21 19:55:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Shop.ShopManagement;
|
2011-07-24 16:56:18 +02:00
|
|
|
import com.Acrobot.ChestShop.Signs.restrictedSign;
|
2011-07-05 19:08:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
|
|
|
import com.Acrobot.ChestShop.Utils.uSign;
|
2011-12-16 17:20:09 +01:00
|
|
|
import org.bukkit.GameMode;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
2011-05-29 13:25:25 +02:00
|
|
|
import org.bukkit.block.Chest;
|
2011-05-15 18:16:25 +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;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.event.block.Action;
|
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
2012-03-01 22:03:59 +01:00
|
|
|
import org.bukkit.inventory.Inventory;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2012-02-16 19:09:37 +01:00
|
|
|
public class playerInteract implements Listener {
|
2012-04-19 17:12:49 +02:00
|
|
|
private static final HashMap<Player, Long> timeOfTheLatestSignClick = new HashMap<Player, Long>();
|
2011-12-16 17:20:09 +01:00
|
|
|
public static int interval = 100;//Minimal interval between transactions
|
2011-08-13 12:08:34 +02:00
|
|
|
|
2012-02-16 19:09:37 +01:00
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
|
|
public static void onPlayerInteract(PlayerInteractEvent event) {
|
2011-05-15 18:16:25 +02:00
|
|
|
Action action = event.getAction();
|
2011-07-23 21:00:47 +02:00
|
|
|
if (action != Action.LEFT_CLICK_BLOCK && action != Action.RIGHT_CLICK_BLOCK) return;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
Block block = event.getClickedBlock();
|
2011-07-23 21:00:47 +02:00
|
|
|
Player player = event.getPlayer();
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-06-11 17:36:55 +02:00
|
|
|
if (Config.getBoolean(Property.USE_BUILT_IN_PROTECTION) && block.getType() == Material.CHEST) {
|
2012-04-19 16:37:33 +02:00
|
|
|
Default protection = Security.getDefaultProtection();
|
2011-08-13 12:08:34 +02:00
|
|
|
if (!hasAdminPermissions(player) && (protection.isProtected(block) && !protection.canAccess(player, block))) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
|
2011-05-15 18:16:25 +02:00
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-22 15:56:06 +02:00
|
|
|
if (!uSign.isSign(block)) return;
|
2011-07-05 19:08:55 +02:00
|
|
|
Sign sign = (Sign) block.getState();
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
if (player.getItemInHand() != null && player.getItemInHand().getType() == Material.SIGN) return;
|
2011-08-13 12:08:34 +02:00
|
|
|
if (!uSign.isValid(sign) || !enoughTimeHasPassed(player) || player.isSneaking()) return;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
if (Config.getBoolean(Property.IGNORE_CREATIVE_MODE) && player.getGameMode() == GameMode.CREATIVE) {
|
2011-12-16 17:20:09 +01:00
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-19 17:12:49 +02:00
|
|
|
timeOfTheLatestSignClick.put(player, System.currentTimeMillis());
|
2011-07-23 21:00:47 +02:00
|
|
|
|
2011-09-06 19:01:57 +02:00
|
|
|
if (action == Action.RIGHT_CLICK_BLOCK) event.setCancelled(true);
|
2011-07-23 21:00:47 +02:00
|
|
|
|
2012-04-07 12:44:58 +02:00
|
|
|
if (uSign.canAccess(player, sign)) {
|
|
|
|
if (!Config.getBoolean(Property.ALLOW_SIGN_CHEST_OPEN)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action != Action.LEFT_CLICK_BLOCK || !Config.getBoolean(Property.ALLOW_LEFT_CLICK_DESTROYING)) {
|
|
|
|
showChestGUI(player, block);
|
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
return;
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
if (restrictedSign.isRestrictedShop(sign) && !restrictedSign.canAccess(sign, player)) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
|
|
|
|
return;
|
2011-06-23 23:25:34 +02:00
|
|
|
}
|
2011-09-06 19:01:57 +02:00
|
|
|
|
2011-06-11 17:36:55 +02:00
|
|
|
Action buy = (Config.getBoolean(Property.REVERSE_BUTTONS) ? Action.LEFT_CLICK_BLOCK : Action.RIGHT_CLICK_BLOCK);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-09-29 20:29:39 +02:00
|
|
|
if (action == buy) ShopManagement.buy(sign, player);
|
|
|
|
else ShopManagement.sell(sign, player);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
2011-09-06 19:01:57 +02:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
private static boolean enoughTimeHasPassed(Player player) {
|
2012-04-19 17:12:49 +02:00
|
|
|
return !timeOfTheLatestSignClick.containsKey(player) || (System.currentTimeMillis() - timeOfTheLatestSignClick.get(player)) >= interval;
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean hasAdminPermissions(Player player) {
|
|
|
|
return Permission.has(player, Permission.ADMIN) || Permission.has(player, Permission.MOD);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void showChestGUI(Player player, Block block) {
|
|
|
|
Chest chest = uBlock.findChest(block);
|
|
|
|
if (chest == null) { //Sorry, no chest found
|
|
|
|
player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED));
|
|
|
|
return;
|
|
|
|
}
|
2012-04-16 16:34:00 +02:00
|
|
|
|
|
|
|
if (!hasAdminPermissions(player) && !Security.canAccess(player, block)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-07 12:41:20 +02:00
|
|
|
Inventory chestInv = chest.getInventory();
|
2012-03-01 22:03:59 +01:00
|
|
|
player.openInventory(chestInv);
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|