ChestShop-3/com/Acrobot/ChestShop/Listeners/playerInteract.java

95 lines
3.6 KiB
Java
Raw Normal View History

2011-05-15 19:33:03 +02:00
package com.Acrobot.ChestShop.Listeners;
2011-06-09 22:54:01 +02:00
import com.Acrobot.ChestShop.Config.Config;
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;
import com.Acrobot.ChestShop.Protection.Default;
import com.Acrobot.ChestShop.Shop.ShopManagement;
import com.Acrobot.ChestShop.Signs.restrictedSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import com.Acrobot.ChestShop.Utils.uLongName;
import com.Acrobot.ChestShop.Utils.uSign;
2011-05-29 13:25:25 +02:00
import net.minecraft.server.IInventory;
import net.minecraft.server.InventoryLargeChest;
import org.bukkit.Material;
import org.bukkit.block.Block;
2011-05-29 13:25:25 +02:00
import org.bukkit.block.Chest;
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;
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;
/**
* @author Acrobot
*/
2011-05-29 13:25:25 +02:00
public class playerInteract extends PlayerListener {
private static final HashMap<Player, Long> lastTransactionTime = new HashMap<Player, Long>();
private static final int interval = 100;
2011-05-29 13:25:25 +02:00
public void onPlayerInteract(PlayerInteractEvent event) {
Action action = event.getAction();
if (action != Action.LEFT_CLICK_BLOCK && action != Action.RIGHT_CLICK_BLOCK) return;
2011-05-29 13:25:25 +02:00
Block block = event.getClickedBlock();
Player player = event.getPlayer();
if (Config.getBoolean(Property.USE_BUILT_IN_PROTECTION) && block.getType() == Material.CHEST) {
2011-07-02 20:34:14 +02:00
Default protection = new Default();
if (!Permission.has(player, Permission.ADMIN) && !Permission.has(player, Permission.MOD) && (protection.isProtected(block) && !protection.canAccess(player, block))) {
player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
event.setCancelled(true);
return;
}
}
if (!uSign.isSign(block)) return;
Sign sign = (Sign) block.getState();
2011-05-29 13:25:25 +02:00
if (!uSign.isValid(sign) || lastTransactionTime.containsKey(player) && (System.currentTimeMillis() - lastTransactionTime.get(player)) < interval || player.isSneaking()) return;
2011-05-29 13:25:25 +02:00
lastTransactionTime.put(player, System.currentTimeMillis());
String playerName = uLongName.stripName(player.getName());
2011-05-29 13:25:25 +02:00
if (playerName.equals(sign.getLine(0))) {
Chest chest1 = uBlock.findChest(sign);
2011-06-09 22:54:01 +02:00
if (chest1 == null) {
player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED));
2011-06-09 22:54:01 +02:00
return;
}
IInventory inventory = ((CraftInventory) chest1.getInventory()).getInventory();
Chest chest2 = uBlock.findNeighbor(chest1);
2011-06-09 22:54:01 +02:00
if (chest2 != null) {
IInventory iInv2 = ((CraftInventory) chest2.getInventory()).getInventory();
inventory = new InventoryLargeChest(player.getName() + "'s Shop", inventory, iInv2);
2011-05-29 13:25:25 +02:00
}
((CraftPlayer) player).getHandle().a(inventory);
2011-06-09 22:54:01 +02:00
return;
2011-05-29 13:25:25 +02:00
}
if (restrictedSign.isRestrictedShop(sign) && !restrictedSign.canAccess(sign, player)) {
player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
return;
}
2011-06-09 22:54:01 +02:00
Action buy = (Config.getBoolean(Property.REVERSE_BUTTONS) ? Action.LEFT_CLICK_BLOCK : Action.RIGHT_CLICK_BLOCK);
2011-05-29 13:25:25 +02:00
if (action == buy) {
ShopManagement.buy(sign, player);
2011-05-29 13:25:25 +02:00
} else {
ShopManagement.sell(sign, player);
}
}
}