This repository has been archived by the owner on Feb 17, 2019. It is now read-only.
/
IslandGuardNew.java
218 lines (206 loc) · 8.94 KB
/
IslandGuardNew.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
/*******************************************************************************
* This file is part of ASkyBlock.
*
* ASkyBlock 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.
*
* ASkyBlock 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 ASkyBlock. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
package com.wasteofplastic.acidisland.listeners;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
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.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import com.wasteofplastic.acidisland.ASkyBlock;
import com.wasteofplastic.acidisland.Island;
import com.wasteofplastic.acidisland.Island.Flags;
import com.wasteofplastic.acidisland.Settings;
import com.wasteofplastic.acidisland.util.Util;
import com.wasteofplastic.acidisland.util.VaultHelper;
/**
* @author tastybento
* Provides protection to islands - handles newer events that may not
* exist in older servers
*/
public class IslandGuardNew implements Listener {
private final ASkyBlock plugin;
private final boolean debug = false;
public IslandGuardNew(final ASkyBlock plugin) {
this.plugin = plugin;
}
/**
* Handle interaction with armor stands V1.8
*
* @param e
*/
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInteract(final PlayerInteractAtEntityEvent e) {
if (debug) {
plugin.getLogger().info(e.getEventName());
}
if (!IslandGuard.inWorld(e.getPlayer())) {
return;
}
if (e.getPlayer().isOp()) {
return;
}
// This permission bypasses protection
if (VaultHelper.checkPerm(e.getPlayer(), Settings.PERMPREFIX + "mod.bypassprotect")) {
return;
}
if (e.getRightClicked() != null && e.getRightClicked().getType().equals(EntityType.ARMOR_STAND)) {
// Check island
Island island = plugin.getGrid().getIslandAt(e.getRightClicked().getLocation());
if (island !=null) {
if (island.isSpawn()) {
if (Settings.allowSpawnArmorStandUse) {
return;
}
} else {
if (island.getMembers().contains(e.getPlayer().getUniqueId()) || island.getIgsFlag(Flags.allowArmorStandUse)) {
return;
}
}
}
// plugin.getLogger().info("DEBUG: Armor stand clicked off island");
e.setCancelled(true);
e.getPlayer().sendMessage(ChatColor.RED + plugin.myLocale(e.getPlayer().getUniqueId()).islandProtected);
}
}
/**
* Handle V1.8 blocks that need special treatment
* Tilling of coarse dirt into dirt
* Usually prevented because it could lead to an endless supply of dirt with gravel
*
* @param e
*/
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInteract(final PlayerInteractEvent e) {
if (debug) {
plugin.getLogger().info(e.getEventName());
}
if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
return;
}
if (!IslandGuard.inWorld(e.getPlayer())) {
return;
}
if (e.getPlayer().isOp()) {
return;
}
// This permission bypasses protection
if (VaultHelper.checkPerm(e.getPlayer(), Settings.PERMPREFIX + "mod.bypassprotect")
|| VaultHelper.checkPerm(e.getPlayer(), Settings.PERMPREFIX + "craft.dirt")) {
return;
}
// Prevents tilling of coarse dirt into dirt
ItemStack inHand = e.getPlayer().getItemInHand();
if (inHand.getType() == Material.WOOD_HOE || inHand.getType() == Material.IRON_HOE || inHand.getType() == Material.GOLD_HOE
|| inHand.getType() == Material.DIAMOND_HOE || inHand.getType() == Material.STONE_HOE) {
// plugin.getLogger().info("DEBUG: hoe in hand");
Block block = e.getClickedBlock();
// plugin.getLogger().info("DEBUG: block is " + block.getType() +
// ":" + block.getData());
// Check if coarse dirt
if (block.getType() == Material.DIRT && block.getData() == (byte) 1) {
// plugin.getLogger().info("DEBUG: hitting coarse dirt!");
e.setCancelled(true);
}
}
}
// Armor stand events
@EventHandler(priority = EventPriority.LOWEST)
void placeArmorStandEvent(PlayerInteractEvent e) {
Player p = e.getPlayer();
if (debug) {
plugin.getLogger().info("Armor stand place " + e.getEventName());
}
if (!IslandGuard.inWorld(p)) {
return;
}
if (p.isOp() || VaultHelper.checkPerm(p, Settings.PERMPREFIX + "mod.bypassprotect")) {
// You can do anything if you are Op
return;
}
// Check if they are holding armor stand
ItemStack inHand = e.getPlayer().getItemInHand();
if (inHand != null && inHand.getType().equals(Material.ARMOR_STAND)) {
// Check island
Island island = plugin.getGrid().getIslandAt(e.getPlayer().getLocation());
if (island !=null && (island.getMembers().contains(p.getUniqueId()) || island.getIgsFlag(Flags.allowPlaceBlocks))) {
//plugin.getLogger().info("DEBUG: armor stand place check");
if (Settings.limitedBlocks.containsKey("ARMOR_STAND") && Settings.limitedBlocks.get("ARMOR_STAND") > -1) {
//plugin.getLogger().info("DEBUG: count armor stands");
int count = island.getTileEntityCount(Material.ARMOR_STAND);
//plugin.getLogger().info("DEBUG: count is " + count + " limit is " + Settings.limitedBlocks.get("ARMOR_STAND"));
if (Settings.limitedBlocks.get("ARMOR_STAND") <= count) {
e.getPlayer().sendMessage(ChatColor.RED + (plugin.myLocale(e.getPlayer().getUniqueId()).entityLimitReached.replace("[entity]",
Util.prettifyText(Material.ARMOR_STAND.toString()))).replace("[number]", String.valueOf(Settings.limitedBlocks.get("ARMOR_STAND"))));
e.setCancelled(true);
return;
}
}
return;
}
// plugin.getLogger().info("DEBUG: stand place cancelled");
e.setCancelled(true);
e.getPlayer().sendMessage(ChatColor.RED + plugin.myLocale(e.getPlayer().getUniqueId()).islandProtected);
e.getPlayer().updateInventory();
}
}
@EventHandler(priority = EventPriority.LOW)
public void ArmorStandDestroy(EntityDamageByEntityEvent e) {
if (debug) {
plugin.getLogger().info("IslandGuard New " + e.getEventName());
}
if (!(e.getEntity() instanceof LivingEntity)) {
return;
}
if (!IslandGuard.inWorld(e.getEntity())) {
return;
}
final LivingEntity livingEntity = (LivingEntity) e.getEntity();
if (!livingEntity.getType().equals(EntityType.ARMOR_STAND)) {
return;
}
if (e.getDamager() instanceof Player) {
Player p = (Player) e.getDamager();
if (p.isOp() || VaultHelper.checkPerm(p, Settings.PERMPREFIX + "mod.bypassprotect")) {
return;
}
// Check if on island
if (plugin.getGrid().playerIsOnIsland(p)) {
return;
}
// Check island
Island island = plugin.getGrid().getIslandAt(e.getEntity().getLocation());
if (island != null && island.isSpawn() && Settings.allowSpawnBreakBlocks) {
return;
}
if (island != null && island.getIgsFlag(Flags.allowBreakBlocks)) {
return;
}
p.sendMessage(ChatColor.RED + plugin.myLocale(p.getUniqueId()).islandProtected);
e.setCancelled(true);
}
}
}