/
WorldEditListener.java
176 lines (151 loc) · 5.73 KB
/
WorldEditListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* WorldEdit
* Copyright (C) 2012 sk89q <http://www.sk89q.com> and contributors
*
* 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/>.
*/
// $Id$
package com.sk89q.worldedit.spout;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldVector;
import org.spout.api.Spout;
import org.spout.api.chat.ChatArguments;
import org.spout.api.chat.ChatSection;
import org.spout.api.event.EventHandler;
import org.spout.api.event.Listener;
import org.spout.api.event.Order;
import org.spout.api.event.player.PlayerInteractEvent;
import org.spout.api.event.player.PlayerInteractEvent.Action;
import org.spout.api.event.player.PlayerLeaveEvent;
import org.spout.api.event.server.PreCommandEvent;
import org.spout.api.event.world.WorldLoadEvent;
import org.spout.api.generator.biome.BiomeGenerator;
import org.spout.api.geo.discrete.Point;
import org.spout.api.scheduler.TaskPriority;
import java.util.Arrays;
import java.util.List;
/**
* Handles all events thrown in relation to a Player
*/
public class WorldEditListener implements Listener {
/**
* Plugin.
*/
private WorldEditPlugin plugin;
private boolean ignoreLeftClickAir = false;
/**
* Construct the object;
*
* @param plugin
*/
public WorldEditListener(WorldEditPlugin plugin) {
this.plugin = plugin;
}
/**
* Called when a player leaves a server
*
* @param event Relevant event details
*/
@EventHandler
public void onPlayerQuit(PlayerLeaveEvent event) {
plugin.getWorldEdit().markExpire(plugin.wrapPlayer(event.getPlayer()));
}
/**
* Called when a player attempts to use a command
*
* @param event Relevant event details
*/
@EventHandler(order = Order.EARLY)
public void onPlayerCommandPreprocess(PreCommandEvent event) {
if (event.getCommand().startsWith("nowe:")) {
event.setCommand(event.getCommand().substring(5));
return;
}
List<ChatSection> args = event.getArguments().toSections(ChatSection.SplitType.WORD);
if (args.size() > 0) {
String[] split = new String[args.size() + 1];
split[0] = "/" + event.getCommand();
for (int i = 0; i < args.size(); ++i) {
split[i + 1] = args.get(i).getPlainString();
}
String[] newSplit = plugin.getWorldEdit().commandDetection(split);
if (!Arrays.equals(split, newSplit)) {
event.setCommand(newSplit[0]);
ChatArguments newArgs = new ChatArguments();
for (int i = 1; i < newSplit.length; ++i) {
newArgs.append(newSplit[i]);
}
event.setArguments(newArgs);
}
}
}
/**
* Called when a player interacts
*
* @param event Relevant event details
*/
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
final LocalPlayer player = plugin.wrapPlayer(event.getPlayer());
final LocalWorld world = player.getWorld();
final WorldEdit we = plugin.getWorldEdit();
PlayerInteractEvent.Action action = event.getAction();
if (action == Action.LEFT_CLICK) {
if (event.isAir() && ignoreLeftClickAir) {
return;
}
if (!event.isAir()) {
final Point clickedBlock = event.getInteractedPoint();
final WorldVector pos = new WorldVector(world, clickedBlock.getBlockX(),
clickedBlock.getBlockY(), clickedBlock.getBlockZ());
if (we.handleBlockLeftClick(player, pos)) {
event.setCancelled(true);
}
}
if (we.handleArmSwing(player)) {
event.setCancelled(true);
}
if (!event.isAir() && !ignoreLeftClickAir) {
final int taskId = Spout.getEngine().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
ignoreLeftClickAir = false;
}
}, 100, TaskPriority.NORMAL).getTaskId();
if (taskId != -1) {
ignoreLeftClickAir = true;
}
}
} else if (action == Action.RIGHT_CLICK) {
if (!event.isAir()) {
final Point clickedBlock = event.getInteractedPoint();
final WorldVector pos = new WorldVector(world, clickedBlock.getBlockX(),
clickedBlock.getBlockY(), clickedBlock.getBlockZ());
if (we.handleBlockRightClick(player, pos)) {
event.setCancelled(true);
}
}
if (we.handleRightClick(player)) {
event.setCancelled(true);
}
}
}
@EventHandler
public void onWorldLoad(WorldLoadEvent event) {
if (event.getWorld().getGenerator() instanceof BiomeGenerator) {
plugin.getServerInterface().getBiomes().registerBiomeTypes((BiomeGenerator) event.getWorld().getGenerator());
}
}
}