-
Notifications
You must be signed in to change notification settings - Fork 1
/
InteractListener.java
216 lines (182 loc) · 8.71 KB
/
InteractListener.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
package me.Aubli.ZvP.Listeners;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import me.Aubli.ZvP.ZvP;
import me.Aubli.ZvP.ZvPCommands;
import me.Aubli.ZvP.Game.Arena;
import me.Aubli.ZvP.Game.GameManager;
import me.Aubli.ZvP.Game.ZvPPlayer;
import me.Aubli.ZvP.Shop.ShopItem;
import me.Aubli.ZvP.Shop.ShopManager;
import me.Aubli.ZvP.Shop.ShopManager.ItemCategory;
import me.Aubli.ZvP.Sign.InteractSign;
import me.Aubli.ZvP.Sign.ShopSign;
import me.Aubli.ZvP.Sign.SignManager;
import me.Aubli.ZvP.Sign.SignManager.SignType;
import me.Aubli.ZvP.Translation.MessageKeys.arena;
import me.Aubli.ZvP.Translation.MessageKeys.error;
import me.Aubli.ZvP.Translation.MessageKeys.game;
import me.Aubli.ZvP.Translation.MessageKeys.manage;
import me.Aubli.ZvP.Translation.MessageManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
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.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class InteractListener implements Listener {
private HashMap<Action, Location> clickLoc = new HashMap<Action, Location>();
private static List<Location> locationList = new ArrayList<Location>();
private SignManager sm = SignManager.getManager();
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerInteract(PlayerInteractEvent event) {
Player eventPlayer = event.getPlayer();
if (event.getItem() != null) {
if (ZvP.equalsItemStack(event.getItem(), ZvP.getTool(""))) {
if (eventPlayer.hasPermission("zvp.manage.arena")) {
String toolString = ChatColor.stripColor(event.getItem().getItemMeta().getLore().get(0));
event.setCancelled(true);
if (toolString.equals(ZvP.ADDARENA_SINGLE)) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
this.clickLoc.put(event.getAction(), event.getClickedBlock().getLocation().clone());
eventPlayer.sendMessage(MessageManager.getMessage(manage.right_saved));
}
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
this.clickLoc.put(event.getAction(), event.getClickedBlock().getLocation().clone());
eventPlayer.sendMessage(MessageManager.getMessage(manage.left_saved));
}
if (this.clickLoc.containsKey(Action.RIGHT_CLICK_BLOCK) && this.clickLoc.containsKey(Action.LEFT_CLICK_BLOCK)) {
Arena arena = GameManager.getManager().addArena(this.clickLoc.get(Action.LEFT_CLICK_BLOCK), this.clickLoc.get(Action.RIGHT_CLICK_BLOCK));
if (arena != null) {
eventPlayer.sendMessage(MessageManager.getFormatedMessage(manage.arena_saved, arena.getID()));
} else {
eventPlayer.sendMessage(MessageManager.getMessage(error.arena_place));
}
this.clickLoc.clear();
}
} else if (toolString.equals(ZvP.ADDARENA_POLYGON)) {
locationList.add(event.getClickedBlock().getLocation().clone());
eventPlayer.sendMessage(MessageManager.getFormatedMessage(manage.position_saved_poly, locationList.size()));
} else if (toolString.equals(ZvP.ADDPOSITION)) {
for (Arena arena : GameManager.getManager().getArenas()) {
if (arena.containsLocation(eventPlayer.getLocation())) {
boolean success = arena.getArea().addSpawnPosition(event.getClickedBlock().getLocation().clone().add(0, 1, 0));
if (success) {
eventPlayer.sendMessage(MessageManager.getFormatedMessage(manage.position_saved, "Position in arena " + arena.getID()));
} else {
eventPlayer.sendMessage(MessageManager.getMessage(manage.position_not_saved));
}
return;
}
}
eventPlayer.sendMessage(MessageManager.getMessage(manage.position_not_in_arena));
}
} else {
ZvP.removeTool(eventPlayer);
ZvPCommands.commandDenied(eventPlayer);
}
return;
}
}
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
ZvP.getPluginLogger().log(getClass(), Level.ALL, "action == RIGHT_CLICK_BLOCK; Block:" + event.getClickedBlock().getType() + " isSign:" + this.sm.isZVPSign(event.getClickedBlock().getLocation()), true, true);
if (this.sm.isZVPSign(event.getClickedBlock().getLocation())) {
ZvP.getPluginLogger().log(getClass(), Level.ALL, "signType:" + this.sm.getType(event.getClickedBlock().getLocation()), true, true);
if (this.sm.getType(event.getClickedBlock().getLocation()) == SignType.INTERACT_SIGN) { // Player join per Sign
if (!GameManager.getManager().isInGame(eventPlayer)) {
if (eventPlayer.hasPermission("zvp.play")) {
InteractSign sign = (InteractSign) this.sm.getSign(event.getClickedBlock().getLocation());
if (sign.getArena() != null) {
if (sign.getArena().isOnline()) {
boolean success = GameManager.getManager().createPlayer(eventPlayer, sign.getArena(), sign.getLobby());
if (!success) {
event.setCancelled(true);
eventPlayer.sendMessage(MessageManager.getMessage(arena.not_ready));
return;
}
} else {
event.setCancelled(true);
eventPlayer.sendMessage(MessageManager.getMessage(arena.offline));
return;
}
} else {
event.setCancelled(true);
eventPlayer.sendMessage(MessageManager.getMessage(error.arena_not_available));
return;
}
} else {
event.setCancelled(true);
ZvPCommands.commandDenied(eventPlayer);
return;
}
} else {
event.setCancelled(true);
eventPlayer.sendMessage(MessageManager.getMessage(game.already_in_game));
return;
}
} else if (this.sm.getType(event.getClickedBlock().getLocation()) == SignType.SHOP_SIGN) { // player clicked shop sign
ZvP.getPluginLogger().log(getClass(), Level.ALL, "InGame:" + GameManager.getManager().isInGame(eventPlayer) + " hasPerm:" + eventPlayer.hasPermission("zvp.play"), true, true);
if (GameManager.getManager().isInGame(eventPlayer)) {
try {
if (eventPlayer.hasPermission("zvp.play")) {
ZvPPlayer player = GameManager.getManager().getPlayer(eventPlayer);
ZvP.getPluginLogger().log(getClass(), Level.ALL, "zvpplayer==null:" + (player == null) + " allowInteraction:" + player.getArena().getArenaMode().allowPlayerInteraction(player), true, true);
if (player.getArena().getArenaMode().allowPlayerInteraction(player)) {
event.setCancelled(true);
ShopSign shopSign = (ShopSign) this.sm.getSign(event.getClickedBlock().getLocation());
ItemCategory cat = shopSign.getCategory();
Inventory shopInv = Bukkit.createInventory(eventPlayer, ((int) Math.ceil(ShopManager.getManager().getItems(cat).size() / 9.0)) * 9, "Items: " + cat.toString());
for (ShopItem shopItem : ShopManager.getManager().getItems(cat)) {
ItemStack item = shopItem.getItem();
ItemMeta meta = item.getItemMeta();
List<String> lore = new ArrayList<String>();
lore.add("Category: " + shopItem.getCategory().toString());
lore.add(player.getArena().getScore().getScore(player) >= shopItem.getBuyPrice() ? (ChatColor.GREEN + "Cost: " + shopItem.getBuyPrice()) : (ChatColor.RED + "Cost: " + shopItem.getBuyPrice()));
lore.add(ChatColor.GOLD + "Refund: " + shopItem.getSellPrice());
meta.setLore(lore);
item.setItemMeta(meta);
shopInv.addItem(item);
}
ZvP.getPluginLogger().log(getClass(), Level.ALL, "shopsign==null:" + (shopSign == null) + " cat==null:" + (shopSign.getCategory() == null) + " cat:" + shopSign.getCategory(), true, true);
eventPlayer.closeInventory();
eventPlayer.openInventory(shopInv);
ZvP.getPluginLogger().log(getClass(), Level.ALL, "InvSize:" + shopInv.getSize() + " shopInvTitle:" + shopInv.getTitle() + " playerOpenInv:" + eventPlayer.getOpenInventory().getTitle(), true, true);
return;
}
} else {
event.setCancelled(true);
ZvPCommands.commandDenied(eventPlayer);
return;
}
} catch (Exception e) {
ZvP.getPluginLogger().log(getClass(), Level.ALL, "Error in shop interaction: " + e.getMessage(), true, true, e);
}
} else {
event.setCancelled(true);
eventPlayer.sendMessage(MessageManager.getMessage(game.not_in_game));
return;
}
}
}
} else if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
if (GameManager.getManager().isInGame(eventPlayer)) {
event.setCancelled(true);
return;
}
}
}
public static void clearPositionList() {
locationList.clear();
}
public static Arena createArenaFromList() {
return GameManager.getManager().addArena(locationList);
}
}