PageRenderTime 84ms CodeModel.GetById 50ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/SpoutBackpack/src/me/neatmonster/spoutbackpack/SBInputListener.java

https://github.com/st-remy/SpoutBackpack
Java | 420 lines | 412 code | 8 blank | 0 comment | 316 complexity | fdec0cfef52f219e0cd4b3d7690f64d6 MD5 | raw file
  1package me.neatmonster.spoutbackpack;
  2
  3import java.util.logging.Logger;
  4
  5import net.minecraft.server.EntityPlayer;
  6import net.minecraft.server.Packet100OpenWindow;
  7import net.minecraft.server.Packet101CloseWindow;
  8
  9import org.bukkit.ChatColor;
 10import org.bukkit.Material;
 11import org.bukkit.craftbukkit.entity.CraftPlayer;
 12import org.bukkit.inventory.Inventory;
 13import org.getspout.spout.inventory.CustomInventory;
 14import org.getspout.spoutapi.event.input.InputListener;
 15import org.getspout.spoutapi.event.input.KeyPressedEvent;
 16import org.getspout.spoutapi.gui.Color;
 17import org.getspout.spoutapi.gui.GenericLabel;
 18import org.getspout.spoutapi.gui.ScreenType;
 19import org.getspout.spoutapi.keyboard.Keyboard;
 20import org.getspout.spoutapi.player.SpoutPlayer;
 21
 22public class SBInputListener extends InputListener {
 23
 24	private SpoutBackpack plugin;
 25	public static Logger logger = Logger.getLogger("minecraft");
 26
 27	public SBInputListener(SpoutBackpack plugin) {
 28		this.plugin = plugin;
 29	}
 30
 31	@SuppressWarnings("deprecation")
 32	public void onKeyPressedEvent(KeyPressedEvent event) {
 33		if (event.getKey() == getKeyInConfig("Backpack.Key", "B")) {
 34			ScreenType screentype = event.getScreenType();
 35			if (screentype == ScreenType.WORKBENCH_INVENTORY) {
 36				return;
 37			}
 38			if (plugin.canOpenBackpack(event.getPlayer().getWorld(),
 39					event.getPlayer()) == false) {
 40				return;
 41			}
 42			if (screentype == ScreenType.CHEST_INVENTORY) {
 43				CraftPlayer player = (CraftPlayer) event.getPlayer();
 44				if (!plugin.openedInventoriesOthers.containsKey(player
 45						.getName())) {
 46					if (plugin.openedInventories.containsKey(player.getName())) {
 47						if (plugin.widgets.containsKey(player.getName())
 48								&& plugin.useWidget == true) {
 49							plugin.widgets.get(player.getName())
 50									.setVisible(false).setDirty(true);
 51						}
 52						Inventory inv = plugin.openedInventories.get(player
 53								.getName());
 54						plugin.inventories.put(player.getName(),
 55								inv.getContents());
 56						player.getHandle().y();
 57					}
 58				} else {
 59					if (plugin.openedInventories
 60							.containsKey(plugin.openedInventoriesOthers
 61									.get(player.getName()))) {
 62						Inventory inv = plugin.openedInventories
 63								.get(plugin.openedInventoriesOthers.get(player
 64										.getName()));
 65						plugin.inventories.put(plugin.openedInventoriesOthers
 66								.get(player.getName()), inv.getContents());
 67						plugin.openedInventoriesOthers.remove(player.getName());
 68						player.getHandle().y();
 69					}
 70				}
 71			}
 72			if (!plugin.openedInventoriesOthers.containsValue(event.getPlayer()
 73					.getName())) {
 74				if (screentype != null
 75						&& (screentype == ScreenType.GAME_SCREEN
 76								|| screentype == ScreenType.PLAYER_INVENTORY
 77								|| screentype == ScreenType.DISPENSER_INVENTORY
 78								|| screentype == ScreenType.FURNACE_INVENTORY || screentype == ScreenType.WORKBENCH_INVENTORY)) {
 79					SpoutPlayer player = event.getPlayer();
 80					if (plugin.Method != null && plugin.useWidget == true) {
 81						if (plugin.widgets.containsKey(player.getName())) {
 82							plugin.widgets.get(player.getName())
 83									.setVisible(true).setDirty(true);
 84						} else {
 85							GenericLabel widget = new GenericLabel("");
 86							if (!plugin.Method.hasAccount(player.getName())) {
 87								return;
 88							}
 89							widget.setText(
 90									plugin.li.getMessage("money")
 91											+ String.format(plugin.Method.format(plugin.Method
 92													.getAccount(
 93															player.getName())
 94													.balance())))
 95									.setTextColor(new Color(1, 1, 1))
 96									.setX(plugin.widgetX).setY(plugin.widgetY);
 97							player.getMainScreen().attachWidget(widget);
 98							widget.setVisible(true).setDirty(true);
 99							plugin.widgets.put(player.getName(), widget);
100						}
101					}
102					CustomInventory inv = new CustomInventory(
103							plugin.allowedSize(player.getWorld(), player, true),
104							plugin.inventoryName);
105					plugin.openedInventories.put(player.getName(), inv);
106					if (plugin.inventories.containsKey(player.getName())) {
107						inv.setContents(plugin.inventories.get(player.getName()));
108					}
109					player.openInventoryWindow((Inventory) inv);
110				}
111			} else {
112				event.getPlayer().sendMessage(
113						plugin.li.getMessage("someoneisusingyour")
114								+ ChatColor.RED + plugin.inventoryName
115								+ ChatColor.WHITE + plugin.li.getMessage("!"));
116			}
117		} else if (event.getKey() == getKeyInConfig("Workbench.Key", "W")
118				&& plugin.workbenchEnabled == true) {
119			if (!plugin.openedInventoriesOthers.containsKey(event.getPlayer()
120					.getName())) {
121				if (!plugin.openedInventories.containsKey(event.getPlayer()
122						.getName())) {
123					if ((plugin.permissionHandler != null || plugin.permissionsBukkitPlugin != null)
124							&& !plugin.userHasPermission(event.getPlayer(),
125									"backpack.workbench")) {
126						return;
127					}
128					if (plugin.workbenchInventory == true
129							&& !event.getPlayer().getInventory()
130									.contains(Material.WORKBENCH)) {
131						return;
132					}
133					final int windowNumber = 1;
134					ScreenType screentype = event.getScreenType();
135					if (screentype == ScreenType.WORKBENCH_INVENTORY) {
136						SpoutPlayer player = event.getPlayer();
137						final EntityPlayer entityPlayer = ((CraftPlayer) player)
138								.getHandle();
139						entityPlayer.netServerHandler
140								.sendPacket(new Packet101CloseWindow(
141										windowNumber));
142					} else if (screentype != null
143							&& (screentype == ScreenType.GAME_SCREEN
144									|| screentype == ScreenType.PLAYER_INVENTORY
145									|| screentype == ScreenType.DISPENSER_INVENTORY
146									|| screentype == ScreenType.FURNACE_INVENTORY || screentype == ScreenType.CHEST_INVENTORY)) {
147						SpoutPlayer player = event.getPlayer();
148						final EntityPlayer entityPlayer = ((CraftPlayer) player)
149								.getHandle();
150						entityPlayer.netServerHandler
151								.sendPacket(new Packet100OpenWindow(
152										windowNumber, 1, "Crafting", 9));
153						entityPlayer.activeContainer = new SBWorkbench(
154								entityPlayer, windowNumber);
155					}
156				}
157			}
158		}
159	}
160
161	private Keyboard getKeyInConfig(String configProperty, String configDefault) {
162		plugin.config.load();
163		String keyInConfig = plugin.config.getString(configProperty,
164				configDefault);
165		plugin.config.save();
166		Keyboard key = Keyboard.KEY_B;
167		if (keyInConfig.equalsIgnoreCase("A")) {
168			key = Keyboard.KEY_A;
169		} else if (keyInConfig.equalsIgnoreCase("B")) {
170			key = Keyboard.KEY_B;
171		} else if (keyInConfig.equalsIgnoreCase("C")) {
172			key = Keyboard.KEY_C;
173		} else if (keyInConfig.equalsIgnoreCase("D")) {
174			key = Keyboard.KEY_D;
175		} else if (keyInConfig.equalsIgnoreCase("E")) {
176			key = Keyboard.KEY_E;
177		} else if (keyInConfig.equalsIgnoreCase("F")) {
178			key = Keyboard.KEY_F;
179		} else if (keyInConfig.equalsIgnoreCase("G")) {
180			key = Keyboard.KEY_G;
181		} else if (keyInConfig.equalsIgnoreCase("H")) {
182			key = Keyboard.KEY_H;
183		} else if (keyInConfig.equalsIgnoreCase("I")) {
184			key = Keyboard.KEY_I;
185		} else if (keyInConfig.equalsIgnoreCase("J")) {
186			key = Keyboard.KEY_J;
187		} else if (keyInConfig.equalsIgnoreCase("K")) {
188			key = Keyboard.KEY_K;
189		} else if (keyInConfig.equalsIgnoreCase("L")) {
190			key = Keyboard.KEY_L;
191		} else if (keyInConfig.equalsIgnoreCase("M")) {
192			key = Keyboard.KEY_M;
193		} else if (keyInConfig.equalsIgnoreCase("N")) {
194			key = Keyboard.KEY_N;
195		} else if (keyInConfig.equalsIgnoreCase("O")) {
196			key = Keyboard.KEY_O;
197		} else if (keyInConfig.equalsIgnoreCase("P")) {
198			key = Keyboard.KEY_P;
199		} else if (keyInConfig.equalsIgnoreCase("Q")) {
200			key = Keyboard.KEY_Q;
201		} else if (keyInConfig.equalsIgnoreCase("R")) {
202			key = Keyboard.KEY_R;
203		} else if (keyInConfig.equalsIgnoreCase("S")) {
204			key = Keyboard.KEY_S;
205		} else if (keyInConfig.equalsIgnoreCase("T")) {
206			key = Keyboard.KEY_T;
207		} else if (keyInConfig.equalsIgnoreCase("U")) {
208			key = Keyboard.KEY_U;
209		} else if (keyInConfig.equalsIgnoreCase("V")) {
210			key = Keyboard.KEY_V;
211		} else if (keyInConfig.equalsIgnoreCase("W")) {
212			key = Keyboard.KEY_W;
213		} else if (keyInConfig.equalsIgnoreCase("X")) {
214			key = Keyboard.KEY_X;
215		} else if (keyInConfig.equalsIgnoreCase("Y")) {
216			key = Keyboard.KEY_Y;
217		} else if (keyInConfig.equalsIgnoreCase("Z")) {
218			key = Keyboard.KEY_Z;
219		} else if (keyInConfig.equalsIgnoreCase("0")) {
220			key = Keyboard.KEY_0;
221		} else if (keyInConfig.equalsIgnoreCase("1")) {
222			key = Keyboard.KEY_1;
223		} else if (keyInConfig.equalsIgnoreCase("2")) {
224			key = Keyboard.KEY_2;
225		} else if (keyInConfig.equalsIgnoreCase("3")) {
226			key = Keyboard.KEY_3;
227		} else if (keyInConfig.equalsIgnoreCase("4")) {
228			key = Keyboard.KEY_4;
229		} else if (keyInConfig.equalsIgnoreCase("5")) {
230			key = Keyboard.KEY_5;
231		} else if (keyInConfig.equalsIgnoreCase("6")) {
232			key = Keyboard.KEY_6;
233		} else if (keyInConfig.equalsIgnoreCase("7")) {
234			key = Keyboard.KEY_7;
235		} else if (keyInConfig.equalsIgnoreCase("8")) {
236			key = Keyboard.KEY_8;
237		} else if (keyInConfig.equalsIgnoreCase("9")) {
238			key = Keyboard.KEY_9;
239		} else if (keyInConfig.equalsIgnoreCase("NONE")) {
240			key = Keyboard.KEY_NONE;
241		} else if (keyInConfig.equalsIgnoreCase("ESCAPE")) {
242			key = Keyboard.KEY_ESCAPE;
243		} else if (keyInConfig.equalsIgnoreCase("MINUS")) {
244			key = Keyboard.KEY_MINUS;
245		} else if (keyInConfig.equalsIgnoreCase("EQUALS")) {
246			key = Keyboard.KEY_EQUALS;
247		} else if (keyInConfig.equalsIgnoreCase("BACK")) {
248			key = Keyboard.KEY_BACK;
249		} else if (keyInConfig.equalsIgnoreCase("TAB")) {
250			key = Keyboard.KEY_TAB;
251		} else if (keyInConfig.equalsIgnoreCase("LBRACKET")) {
252			key = Keyboard.KEY_LBRACKET;
253		} else if (keyInConfig.equalsIgnoreCase("RBRACKET")) {
254			key = Keyboard.KEY_RBRACKET;
255		} else if (keyInConfig.equalsIgnoreCase("RETURN")) {
256			key = Keyboard.KEY_RETURN;
257		} else if (keyInConfig.equalsIgnoreCase("LCONTROL")) {
258			key = Keyboard.KEY_LCONTROL;
259		} else if (keyInConfig.equalsIgnoreCase("SEMICOLON")) {
260			key = Keyboard.KEY_SEMICOLON;
261		} else if (keyInConfig.equalsIgnoreCase("APOSTROPHE")) {
262			key = Keyboard.KEY_APOSTROPHE;
263		} else if (keyInConfig.equalsIgnoreCase("GRAVE")) {
264			key = Keyboard.KEY_GRAVE;
265		} else if (keyInConfig.equalsIgnoreCase("LSHIFT")) {
266			key = Keyboard.KEY_LSHIFT;
267		} else if (keyInConfig.equalsIgnoreCase("BACKSLASH")) {
268			key = Keyboard.KEY_BACKSLASH;
269		} else if (keyInConfig.equalsIgnoreCase("COMMA")) {
270			key = Keyboard.KEY_COMMA;
271		} else if (keyInConfig.equalsIgnoreCase("PERIOD")) {
272			key = Keyboard.KEY_PERIOD;
273		} else if (keyInConfig.equalsIgnoreCase("SLASH")) {
274			key = Keyboard.KEY_SLASH;
275		} else if (keyInConfig.equalsIgnoreCase("RSHIFT")) {
276			key = Keyboard.KEY_RSHIFT;
277		} else if (keyInConfig.equalsIgnoreCase("MULTIPLY")) {
278			key = Keyboard.KEY_MULTIPLY;
279		} else if (keyInConfig.equalsIgnoreCase("LMENU")) {
280			key = Keyboard.KEY_LMENU;
281		} else if (keyInConfig.equalsIgnoreCase("SPACE")) {
282			key = Keyboard.KEY_SPACE;
283		} else if (keyInConfig.equalsIgnoreCase("F1")) {
284			key = Keyboard.KEY_F1;
285		} else if (keyInConfig.equalsIgnoreCase("F2")) {
286			key = Keyboard.KEY_F2;
287		} else if (keyInConfig.equalsIgnoreCase("F3")) {
288			key = Keyboard.KEY_F3;
289		} else if (keyInConfig.equalsIgnoreCase("F4")) {
290			key = Keyboard.KEY_F4;
291		} else if (keyInConfig.equalsIgnoreCase("F5")) {
292			key = Keyboard.KEY_F5;
293		} else if (keyInConfig.equalsIgnoreCase("F6")) {
294			key = Keyboard.KEY_F6;
295		} else if (keyInConfig.equalsIgnoreCase("F7")) {
296			key = Keyboard.KEY_F7;
297		} else if (keyInConfig.equalsIgnoreCase("F8")) {
298			key = Keyboard.KEY_F8;
299		} else if (keyInConfig.equalsIgnoreCase("F9")) {
300			key = Keyboard.KEY_F9;
301		} else if (keyInConfig.equalsIgnoreCase("F10")) {
302			key = Keyboard.KEY_F10;
303		} else if (keyInConfig.equalsIgnoreCase("NUMLOCK")) {
304			key = Keyboard.KEY_NUMLOCK;
305		} else if (keyInConfig.equalsIgnoreCase("SCROLL")) {
306			key = Keyboard.KEY_SCROLL;
307		} else if (keyInConfig.equalsIgnoreCase("SUBTRACT")) {
308			key = Keyboard.KEY_SUBTRACT;
309		} else if (keyInConfig.equalsIgnoreCase("ADD")) {
310			key = Keyboard.KEY_ADD;
311		} else if (keyInConfig.equalsIgnoreCase("DECIMAL")) {
312			key = Keyboard.KEY_DECIMAL;
313		} else if (keyInConfig.equalsIgnoreCase("F11")) {
314			key = Keyboard.KEY_F11;
315		} else if (keyInConfig.equalsIgnoreCase("F12")) {
316			key = Keyboard.KEY_F12;
317		} else if (keyInConfig.equalsIgnoreCase("F13")) {
318			key = Keyboard.KEY_F13;
319		} else if (keyInConfig.equalsIgnoreCase("F14")) {
320			key = Keyboard.KEY_F14;
321		} else if (keyInConfig.equalsIgnoreCase("F15")) {
322			key = Keyboard.KEY_F15;
323		} else if (keyInConfig.equalsIgnoreCase("NUMPAD0")) {
324			key = Keyboard.KEY_NUMPAD0;
325		} else if (keyInConfig.equalsIgnoreCase("NUMPAD1")) {
326			key = Keyboard.KEY_NUMPAD1;
327		} else if (keyInConfig.equalsIgnoreCase("NUMPAD2")) {
328			key = Keyboard.KEY_NUMPAD2;
329		} else if (keyInConfig.equalsIgnoreCase("NUMPAD3")) {
330			key = Keyboard.KEY_NUMPAD3;
331		} else if (keyInConfig.equalsIgnoreCase("NUMPAD4")) {
332			key = Keyboard.KEY_NUMPAD4;
333		} else if (keyInConfig.equalsIgnoreCase("NUMPAD5")) {
334			key = Keyboard.KEY_NUMPAD5;
335		} else if (keyInConfig.equalsIgnoreCase("NUMPAD6")) {
336			key = Keyboard.KEY_NUMPAD6;
337		} else if (keyInConfig.equalsIgnoreCase("NUMPAD7")) {
338			key = Keyboard.KEY_NUMPAD7;
339		} else if (keyInConfig.equalsIgnoreCase("NUMPAD8")) {
340			key = Keyboard.KEY_NUMPAD8;
341		} else if (keyInConfig.equalsIgnoreCase("NUMPAD9")) {
342			key = Keyboard.KEY_NUMPAD9;
343		} else if (keyInConfig.equalsIgnoreCase("KANA")) {
344			key = Keyboard.KEY_KANA;
345		} else if (keyInConfig.equalsIgnoreCase("CONVERT")) {
346			key = Keyboard.KEY_CONVERT;
347		} else if (keyInConfig.equalsIgnoreCase("NOCONVERT")) {
348			key = Keyboard.KEY_NOCONVERT;
349		} else if (keyInConfig.equalsIgnoreCase("YEN")) {
350			key = Keyboard.KEY_YEN;
351		} else if (keyInConfig.equalsIgnoreCase("NUMPADEQUALS")) {
352			key = Keyboard.KEY_NUMPADEQUALS;
353		} else if (keyInConfig.equalsIgnoreCase("CIRCUMFLEX")) {
354			key = Keyboard.KEY_CIRCUMFLEX;
355		} else if (keyInConfig.equalsIgnoreCase("AT")) {
356			key = Keyboard.KEY_AT;
357		} else if (keyInConfig.equalsIgnoreCase("COLON")) {
358			key = Keyboard.KEY_COLON;
359		} else if (keyInConfig.equalsIgnoreCase("UNDERLINE")) {
360			key = Keyboard.KEY_UNDERLINE;
361		} else if (keyInConfig.equalsIgnoreCase("KANJI")) {
362			key = Keyboard.KEY_KANJI;
363		} else if (keyInConfig.equalsIgnoreCase("STOP")) {
364			key = Keyboard.KEY_STOP;
365		} else if (keyInConfig.equalsIgnoreCase("AX")) {
366			key = Keyboard.KEY_AX;
367		} else if (keyInConfig.equalsIgnoreCase("UNLABELED")) {
368			key = Keyboard.KEY_UNLABELED;
369		} else if (keyInConfig.equalsIgnoreCase("NUMPADENTER")) {
370			key = Keyboard.KEY_NUMPADENTER;
371		} else if (keyInConfig.equalsIgnoreCase("RCONTROL")) {
372			key = Keyboard.KEY_RCONTROL;
373		} else if (keyInConfig.equalsIgnoreCase("NUMPADCOMMA")) {
374			key = Keyboard.KEY_NUMPADCOMMA;
375		} else if (keyInConfig.equalsIgnoreCase("DIVIDE")) {
376			key = Keyboard.KEY_DIVIDE;
377		} else if (keyInConfig.equalsIgnoreCase("SYSRQ")) {
378			key = Keyboard.KEY_SYSRQ;
379		} else if (keyInConfig.equalsIgnoreCase("RMENU")) {
380			key = Keyboard.KEY_RMENU;
381		} else if (keyInConfig.equalsIgnoreCase("PAUSE")) {
382			key = Keyboard.KEY_PAUSE;
383		} else if (keyInConfig.equalsIgnoreCase("HOME")) {
384			key = Keyboard.KEY_HOME;
385		} else if (keyInConfig.equalsIgnoreCase("UP")) {
386			key = Keyboard.KEY_UP;
387		} else if (keyInConfig.equalsIgnoreCase("PRIOR")) {
388			key = Keyboard.KEY_PRIOR;
389		} else if (keyInConfig.equalsIgnoreCase("LEFT")) {
390			key = Keyboard.KEY_LEFT;
391		} else if (keyInConfig.equalsIgnoreCase("RIGHT")) {
392			key = Keyboard.KEY_RIGHT;
393		} else if (keyInConfig.equalsIgnoreCase("END")) {
394			key = Keyboard.KEY_END;
395		} else if (keyInConfig.equalsIgnoreCase("DOWN")) {
396			key = Keyboard.KEY_DOWN;
397		} else if (keyInConfig.equalsIgnoreCase("NEXT")) {
398			key = Keyboard.KEY_NEXT;
399		} else if (keyInConfig.equalsIgnoreCase("INSERT")) {
400			key = Keyboard.KEY_INSERT;
401		} else if (keyInConfig.equalsIgnoreCase("DELETE")) {
402			key = Keyboard.KEY_DELETE;
403		} else if (keyInConfig.equalsIgnoreCase("LMETA")) {
404			key = Keyboard.KEY_LMETA;
405		} else if (keyInConfig.equalsIgnoreCase("LWIN")) {
406			key = Keyboard.KEY_LWIN;
407		} else if (keyInConfig.equalsIgnoreCase("RMETA")) {
408			key = Keyboard.KEY_RMETA;
409		} else if (keyInConfig.equalsIgnoreCase("RWIN")) {
410			key = Keyboard.KEY_RWIN;
411		} else if (keyInConfig.equalsIgnoreCase("APPS")) {
412			key = Keyboard.KEY_APPS;
413		} else if (keyInConfig.equalsIgnoreCase("POWER")) {
414			key = Keyboard.KEY_POWER;
415		} else if (keyInConfig.equalsIgnoreCase("SLEEP")) {
416			key = Keyboard.KEY_SLEEP;
417		}
418		return key;
419	}
420}