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

192 lines
6.3 KiB
Java
Raw Normal View History

2011-01-01 02:36:25 +00:00
// $Id$
/*
* WorldEdit
2012-01-18 04:35:34 +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
*/
2011-01-01 02:36:25 +00:00
package com.sk89q.worldedit.bukkit;
import com.sk89q.util.StringUtil;
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;
import org.bukkit.event.player.PlayerChatEvent;
2011-04-01 23:18:40 +00:00
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
2011-03-27 18:12:35 +00:00
import org.bukkit.event.player.PlayerInteractEvent;
2012-01-18 04:35:34 +00:00
import org.bukkit.event.player.PlayerJoinEvent;
2011-04-01 23:18:40 +00:00
import org.bukkit.event.player.PlayerQuitEvent;
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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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;
private final static Pattern cuipattern = Pattern.compile("u00a74u00a75u00a73u00a74([^|]*)\\|?(.*)");
/**
* 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;
*
* @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
}
2012-01-18 04:35:34 +00:00
@EventHandler(event = PlayerJoinEvent.class, priority = EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent event) {
plugin.wrapPlayer(event.getPlayer()).dispatchCUIHandshake();
2011-01-01 02:36:25 +00:00
}
2011-01-01 02:36:25 +00:00
/**
* Called when a player leaves a server
*
* @param event Relevant event details
*/
2012-01-18 04:35:34 +00:00
@EventHandler(event = PlayerQuitEvent.class)
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
}
/**
* Called when a player attempts to use a command
*
* @param event Relevant event details
*/
2012-01-18 04:35:34 +00:00
@EventHandler(event = PlayerCommandPreprocessEvent.class, priority = EventPriority.LOW)
2011-04-01 23:18:40 +00:00
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
if (event.isCancelled()) {
2011-08-17 01:39:07 +00:00
return;
}
String[] split = event.getMessage().split(" ");
if (split.length > 0) {
split = plugin.getWorldEdit().commandDetection(split);
split[0] = "/" + split[0];
}
event.setMessage(StringUtil.joinString(split, " "));
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
*/
2012-01-18 04:35:34 +00:00
@EventHandler(event = PlayerInteractEvent.class)
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() {
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);
}
}
}
2012-01-18 04:35:34 +00:00
@EventHandler(event = PlayerChatEvent.class)
public void onPlayerChat(PlayerChatEvent event) {
if (event.isCancelled()) {
return;
}
Matcher matcher = cuipattern.matcher(event.getMessage());
if (matcher.find()) {
String type = matcher.group(1);
String args = matcher.group(2);
if( type.equals("v") ) {
try {
plugin.getSession(event.getPlayer()).setCUIVersion(Integer.parseInt(args));
event.setCancelled(true);
} catch( NumberFormatException e ) {
}
}
}
}
2011-01-01 02:36:25 +00:00
}