/
HMWorldEditListener.java
executable file
·234 lines (214 loc) · 7.3 KB
/
HMWorldEditListener.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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/>.
*/
import java.util.Map;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.Handler;
import java.util.logging.FileHandler;
import java.io.*;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.snapshots.*;
/**
* The event listener for WorldEdit in hMod.
*
* @author sk89q
*/
public class HMWorldEditListener extends PluginListener {
/**
* Logger.
*/
private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
/**
* Properties file.
*/
private PropertiesFile properties;
/**
* Main WorldEdit controller.
*/
private WorldEditController controller;
/**
* A copy of the server instance. This is where all world<->WorldEdit calls
* will go through.
*/
private ServerInterface server;
/**
* Constructs an instance.
*
* @param server
*/
public HMWorldEditListener(ServerInterface server) {
this.server = server;
}
/**
*
* @param player
*/
@Override
public void onDisconnect(Player player) {
controller.handleDisconnect(wrapPlayer(player));
}
/**
* Called on arm swing.
*
* @param player
*/
public void onArmSwing(Player player) {
controller.handleArmSwing(wrapPlayer(player));
}
/**
* Called on right click.
*
* @param player
* @param blockPlaced
* @param blockClicked
* @param itemInHand
* @return false if you want the action to go through
*/
@Override
@SuppressWarnings("deprecation")
public boolean onBlockCreate(Player player, Block blockPlaced,
Block blockClicked, int itemInHand) {
Vector pos = new Vector(blockClicked.getX(),
blockClicked.getY(),
blockClicked.getZ());
return controller.handleBlockRightClick(wrapPlayer(player), pos);
}
/**
* Called on left click.
*
* @param player
* @param blockClicked
* @param itemInHand
* @return false if you want the action to go through
*/
@Override
public boolean onBlockDestroy(Player player, Block blockClicked) {
Vector pos = new Vector(blockClicked.getX(),
blockClicked.getY(),
blockClicked.getZ());
return controller.handleBlockLeftClick(wrapPlayer(player), pos);
}
/**
*
* @param player
* @param split
* @return whether the command was processed
*/
@Override
public boolean onCommand(Player player, String[] split) {
return controller.handleCommand(wrapPlayer(player), split);
}
/**
* Loads the configuration.
*/
public void loadConfiguration() {
if (properties == null) {
properties = new PropertiesFile("worldedit.properties");
} else {
try {
properties.load();
} catch (IOException e) {
logger.warning("worldedit.properties could not be loaded: "
+ e.getMessage());
}
}
controller.profile = properties.getBoolean("debug-profile", false);
controller.wandItem = properties.getInt("wand-item", 271);
controller.defaultChangeLimit = Math.max(-1, properties.getInt("default-max-blocks-changed", -1));
controller.maxChangeLimit = Math.max(-1, properties.getInt("max-blocks-changed", -1));
controller.maxRadius = Math.max(-1, properties.getInt("max-radius", -1));
controller.maxSuperPickaxeSize = Math.max(1, properties.getInt("max-super-pickaxe-size", 5));
controller.registerHelp = properties.getBoolean("register-help", true);
controller.logComands = properties.getBoolean("log-commands", false);
controller.superPickaxeDrop = properties.getBoolean("super-pickaxe-drop-items", true);
controller.superPickaxeManyDrop = properties.getBoolean("super-pickaxe-many-drop-items", false);
controller.noDoubleSlash = properties.getBoolean("no-double-slash", false);
controller.useInventory = properties.getBoolean("use-inventory", false);
controller.useInventoryOverride = properties.getBoolean("use-inventory-override", false);
// Get allowed blocks
controller.allowedBlocks = new HashSet<Integer>();
for (String b : properties.getString("allowed-blocks",
WorldEditController.getDefaultAllowedBlocks()).split(",")) {
try {
controller.allowedBlocks.add(Integer.parseInt(b));
} catch (NumberFormatException e) {
}
}
String snapshotsDir = properties.getString("snapshots-dir", "");
if (!snapshotsDir.trim().equals("")) {
controller.snapshotRepo = new SnapshotRepository(snapshotsDir);
} else {
controller.snapshotRepo = null;
}
String type = properties.getString("shell-save-type", "").trim();
controller.shellSaveType = type.equals("") ? null : type;
String logFile = properties.getString("log-file", "");
if (!logFile.equals("")) {
try {
FileHandler handler = new FileHandler(logFile, true);
handler.setFormatter(new LogFormat());
logger.addHandler(handler);
} catch (IOException e) {
logger.log(Level.WARNING, "Could not use log file " + logFile + ": "
+ e.getMessage());
}
} else {
for (Handler handler : logger.getHandlers()) {
logger.removeHandler(handler);
}
}
}
/**
* Register commands with help.
*/
public void registerCommands() {
if (controller.registerHelp) {
for (Map.Entry<String,String> entry : controller.getCommands().entrySet()) {
etc.getInstance().addCommand(entry.getKey(), entry.getValue());
}
}
}
/**
* De-register commands.
*/
public void deregisterCommands() {
for (String key : controller.getCommands().keySet()) {
etc.getInstance().removeCommand(key);
}
}
/**
* Clear sessions.
*/
public void clearSessions() {
controller.clearSessions();
}
/**
* Gets the WorldEditLibrary session for a player. Used for the bridge.
*
* @param player
* @return
*/
public LocalSession _bridgeSession(Player player) {
return controller.getBridgeSession(wrapPlayer(player));
}
private LocalPlayer wrapPlayer(Player player) {
return new HMPlayer(server, player);
}
}