Plex-FAWE/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java

179 lines
5.9 KiB
Java
Raw Normal View History

2011-01-01 02:36:25 +00:00
/*
* WorldEdit
2012-01-23 05:11:54 +00:00
* Copyright (C) 2012 sk89q <http://www.sk89q.com>
2011-01-01 02:36:25 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2012-01-18 04:35:34 +00:00
*/
2012-01-23 05:11:54 +00:00
// $Id$
2011-01-01 02:36:25 +00:00
package com.sk89q.worldedit.bukkit;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
2012-01-18 04:35:34 +00:00
import org.bukkit.event.Event.Result;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
2011-04-01 23:18:40 +00:00
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerGameModeChangeEvent;
2011-03-27 18:12:35 +00:00
import org.bukkit.event.player.PlayerInteractEvent;
2011-04-01 23:18:40 +00:00
import org.bukkit.event.player.PlayerQuitEvent;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.LocalPlayer;
2011-03-27 18:12:35 +00:00
import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldVector;
2011-01-01 02:36:25 +00:00
/**
* Handles all events thrown in relation to a Player
*/
2012-01-18 04:35:34 +00:00
public class WorldEditListener implements Listener {
2011-01-01 02:36:25 +00:00
private WorldEditPlugin plugin;
private boolean ignoreLeftClickAir = false;
/**
* Called when a player plays an animation, such as an arm swing
*
* @param event Relevant event details
*/
2011-01-01 02:36:25 +00:00
/**
* Construct the object;
*
2011-01-01 02:36:25 +00:00
* @param plugin
*/
2012-01-18 04:35:34 +00:00
public WorldEditListener(WorldEditPlugin plugin) {
2011-01-01 02:36:25 +00:00
this.plugin = plugin;
2012-01-18 04:35:34 +00:00
}
2011-01-01 02:36:25 +00:00
/**
* Called when a player leaves a server
*
* @param event Relevant event details
*/
@EventHandler
2011-04-01 23:18:40 +00:00
public void onPlayerQuit(PlayerQuitEvent event) {
plugin.getWorldEdit().markExpire(plugin.wrapPlayer(event.getPlayer()));
2011-01-01 02:36:25 +00:00
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onGamemode(PlayerGameModeChangeEvent event) {
// this will automatically refresh their sesssion, we don't have to do anything
WorldEdit.getInstance().getSession(plugin.wrapPlayer(event.getPlayer()));
}
2011-01-01 02:36:25 +00:00
/**
* Called when a player attempts to use a command
*
* @param event Relevant event details
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
2011-04-01 23:18:40 +00:00
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
String[] split = event.getMessage().split(" ");
if (split.length > 0) {
split = plugin.getWorldEdit().commandDetection(split);
split[0] = "/" + split[0];
}
final String newMessage = StringUtil.joinString(split, " ");
if (!newMessage.equals(event.getMessage())) {
event.setMessage(newMessage);
plugin.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
if (event.getMessage().length() > 0) {
plugin.getServer().dispatchCommand(event.getPlayer(),
event.getMessage().substring(1));
}
event.setCancelled(true);
}
}
2011-01-03 20:05:41 +00:00
}
2011-03-27 18:12:35 +00:00
/**
2011-03-27 18:12:35 +00:00
* Called when a player interacts
*
* @param event Relevant event details
*/
@EventHandler
2011-03-27 18:12:35 +00:00
public void onPlayerInteract(PlayerInteractEvent event) {
2012-01-18 04:35:34 +00:00
if (event.useItemInHand() == Result.DENY) {
return;
}
final LocalPlayer player = plugin.wrapPlayer(event.getPlayer());
final LocalWorld world = player.getWorld();
final WorldEdit we = plugin.getWorldEdit();
Action action = event.getAction();
if (action == Action.LEFT_CLICK_BLOCK) {
final Block clickedBlock = event.getClickedBlock();
final WorldVector pos = new WorldVector(world, clickedBlock.getX(),
clickedBlock.getY(), clickedBlock.getZ());
2011-08-17 01:39:07 +00:00
if (we.handleBlockLeftClick(player, pos)) {
2011-08-17 01:39:07 +00:00
event.setCancelled(true);
}
if (we.handleArmSwing(player)) {
event.setCancelled(true);
}
if (!ignoreLeftClickAir) {
2011-11-23 01:29:48 +00:00
final int taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
2011-11-23 01:29:48 +00:00
public void run() {
ignoreLeftClickAir = false;
}
}, 2);
if (taskId != -1) {
ignoreLeftClickAir = true;
}
}
} else if (action == Action.LEFT_CLICK_AIR) {
if (ignoreLeftClickAir) {
return;
}
if (we.handleArmSwing(player)) {
event.setCancelled(true);
}
} else if (action == Action.RIGHT_CLICK_BLOCK) {
final Block clickedBlock = event.getClickedBlock();
final WorldVector pos = new WorldVector(world, clickedBlock.getX(),
clickedBlock.getY(), clickedBlock.getZ());
if (we.handleBlockRightClick(player, pos)) {
2011-03-27 18:12:35 +00:00
event.setCancelled(true);
}
if (we.handleRightClick(player)) {
2011-03-27 18:12:35 +00:00
event.setCancelled(true);
}
} else if (action == Action.RIGHT_CLICK_AIR) {
if (we.handleRightClick(player)) {
event.setCancelled(true);
}
}
}
2011-01-01 02:36:25 +00:00
}