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;
|
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-05-29 13:25:25 +02:00
|
|
|
import net.minecraft.server.IInventory;
|
|
|
|
import net.minecraft.server.InventoryLargeChest;
|
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;
|
2011-05-29 13:25:25 +02:00
|
|
|
import org.bukkit.craftbukkit.entity.CraftPlayer;
|
|
|
|
import org.bukkit.craftbukkit.inventory.CraftInventory;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.block.Action;
|
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
|
|
|
import org.bukkit.event.player.PlayerListener;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2011-05-29 13:25:25 +02:00
|
|
|
public class playerInteract extends PlayerListener {
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
private static final HashMap<Player, Long> lastTransactionTime = new HashMap<Player, Long>(); //Last player's transaction
|
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
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public 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) {
|
2011-07-02 20:34:14 +02:00
|
|
|
Default protection = new Default();
|
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
|
|
|
|
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
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
if (Config.getBoolean(Property.IGNORE_CREATIVE_MODE) && player.getGameMode() == GameMode.CREATIVE){
|
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
lastTransactionTime.put(player, System.currentTimeMillis());
|
|
|
|
|
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-01-09 22:39:38 +01:00
|
|
|
if (uSign.canAccess(player, sign)) {
|
2011-09-22 15:56:06 +02:00
|
|
|
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) {
|
|
|
|
return !lastTransactionTime.containsKey(player) || (System.currentTimeMillis() - lastTransactionTime.get(player)) >= interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
IInventory inventory = ((CraftInventory) chest.getInventory()).getInventory();
|
|
|
|
chest = uBlock.findNeighbor(chest);
|
|
|
|
|
2011-09-23 14:07:20 +02:00
|
|
|
if (chest != null) inventory = new InventoryLargeChest("Large chest", inventory, ((CraftInventory) chest.getInventory()).getInventory());
|
2011-08-13 12:08:34 +02:00
|
|
|
|
|
|
|
((CraftPlayer) player).getHandle().a(inventory); //Show inventory on the screen
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|